prefab 0.5.1__py3-none-any.whl → 1.1.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
prefab/processor.py DELETED
@@ -1,248 +0,0 @@
1
- """
2
- This module provides tools for processing and analyzing device matrices in nanofabrication
3
- prediction tasks. It includes functionality for image binarization, contour generation, and
4
- prediction uncertainty computation.
5
- """
6
-
7
- from typing import Optional
8
- import numpy as np
9
- import cv2
10
-
11
-
12
- def binarize(device: np.ndarray, eta: float = 0.5, beta: float = np.inf) -> np.ndarray:
13
- """
14
- Applies soft binarization to a device image using a sigmoid function.
15
-
16
- The binarization process can be controlled by adjusting the thresholding level (`eta`)
17
- and the steepness of the sigmoid function (`beta`). `eta` influences the threshold level
18
- for binarization, simulating under-etching for smaller values and over-etching for larger
19
- values. `beta` controls the steepness of the sigmoid function, thereby determining the
20
- degree of binarization.
21
-
22
- Parameters
23
- ----------
24
- device : np.ndarray
25
- A 2D numpy array representing the grayscale device image to be binarized.
26
-
27
- eta : float, optional
28
- Threshold level for binarization, with values between 0 and 1. Default is 0.5.
29
-
30
- beta : float, optional
31
- Controls the steepness of the sigmoid function and thereby the degree of
32
- binarization. Default is infinity, resulting in maximum binarization.
33
-
34
- Returns
35
- -------
36
- np.ndarray
37
- A 2D numpy array representing the binarized device image.
38
- """
39
- numerator = np.tanh(beta * eta) + np.tanh(beta * (device - eta))
40
- denominator = np.tanh(beta * eta) + np.tanh(beta * (1 - eta))
41
- device_bin = numerator / denominator
42
- return device_bin
43
-
44
-
45
- def binarize_hard(device: np.ndarray, eta: float = 0.5) -> np.ndarray:
46
- """
47
- Applies hard binarization to a device image using a step function.
48
-
49
- The binarization process depends solely on the threshold level (`eta`), which
50
- controls the demarcation point for determining the binary values in the output image.
51
- Smaller `eta` values simulate under-etching (more pixels are turned off), while
52
- larger `eta` values simulate over-etching (more pixels are turned on). Compared to the
53
- sigmoid binarization function, this hard binarization method is less likely to produce
54
- NaN values and may sometimes yield better results.
55
-
56
- Parameters
57
- ----------
58
- device : np.ndarray
59
- A 2D numpy array representing the grayscale device image to be binarized.
60
-
61
- eta : float, optional
62
- Threshold level for binarization, with values between 0 and 1. Default is 0.5.
63
-
64
- Returns
65
- -------
66
- np.ndarray
67
- A 2D numpy array representing the binarized device image.
68
- """
69
- device_bin = np.copy(device)
70
- device_bin[device_bin < eta] = 0
71
- device_bin[device_bin >= eta] = 1
72
- return device_bin
73
-
74
-
75
- def ternarize(device: np.ndarray, eta1: float = 0.33, eta2: float = 0.66) -> np.ndarray:
76
- """
77
- Applies ternarization to a device image using two thresholds.
78
-
79
- This function performs a ternarization process on a given device image, dividing it into three
80
- distinct regions based on two threshold values (`eta1` and `eta2`). It assigns three different
81
- values (0, 1, or 2) to each pixel based on its intensity in relation to the thresholds.
82
- Pixels with intensity less than `eta1` are assigned 0, pixels with intensity greater than or
83
- equal to `eta2` are assigned 2, and pixels with intensity between `eta1` and `eta2` are
84
- assigned 1. This function can be useful for categorizing different regions in a device image.
85
-
86
- Parameters
87
- ----------
88
- device : np.ndarray
89
- A 2D numpy array representing the grayscale device image to be ternarized.
90
-
91
- eta1 : float, optional
92
- First threshold level for ternarization, with values between 0 and 1. Default is 0.33.
93
-
94
- eta2 : float, optional
95
- Second threshold level for ternarization, with values between 0 and 1. Default is 0.66.
96
-
97
- Returns
98
- -------
99
- np.ndarray
100
- A 2D numpy array representing the ternarized device image.
101
- """
102
- device_ter = np.copy(device)
103
- device_ter[device_ter < eta1] = 0
104
- device_ter[device_ter >= eta2] = 1
105
- device_ter[(device_ter >= eta1) & (device_ter < eta2)] = 0.5
106
- return device_ter
107
-
108
-
109
- def remove_padding(device: np.ndarray) -> np.ndarray:
110
- """
111
- Removes the empty padding from the edges of a device.
112
-
113
- This function eliminates rows and columns from the edges of the device matrix
114
- that are entirely zeros, effectively removing any unnecessary padding present
115
- in the device representation.
116
-
117
- Parameters
118
- ----------
119
- device : np.ndarray
120
- A 2D numpy array representing the shape of a binary device.
121
-
122
- Returns
123
- -------
124
- np.ndarray
125
- A 2D numpy array representing the shape of a device without any extraneous padding,
126
- of equal or smaller size compared to the input device.
127
- """
128
- nonzero_rows, nonzero_cols = np.nonzero(device)
129
- trimmed_device = device[
130
- nonzero_rows.min() : nonzero_rows.max() + 1,
131
- nonzero_cols.min() : nonzero_cols.max() + 1,
132
- ]
133
- return trimmed_device
134
-
135
-
136
- def zero_boundary(device: np.ndarray, margin: int) -> np.ndarray:
137
- """
138
- Sets the boundaries of a device matrix to zero up to a specified margin.
139
-
140
- This function zeroes the outermost rows and columns of the device matrix
141
- up to a distance (margin) from the boundaries, effectively creating a
142
- "zeroed" frame around the device representation.
143
-
144
- Parameters
145
- ----------
146
- device : np.ndarray
147
- A 2D numpy array representing the shape of a device.
148
-
149
- margin : int
150
- The distance (in pixels) from the boundaries that should be zeroed.
151
-
152
- Returns
153
- -------
154
- np.ndarray
155
- A 2D numpy array representing the shape of the device with its outermost
156
- rows and columns up to 'margin' distance set to zero.
157
- """
158
- zeroed_device = device.copy()
159
- zeroed_device[:margin, :] = 0
160
- zeroed_device[-margin:, :] = 0
161
- zeroed_device[:, :margin] = 0
162
- zeroed_device[:, -margin:] = 0
163
- return zeroed_device
164
-
165
-
166
- def generate_device_contour(
167
- device: np.ndarray, linewidth: Optional[int] = None
168
- ) -> np.ndarray:
169
- """
170
- Generates a contour of a device for visualization purposes.
171
-
172
- This function generates a binary contour of a device's shape which can be overlaid
173
- on top of the device's image for better visualization. The thickness of the contour
174
- line can be specified, with a default value calculated as 1% of the device's height.
175
-
176
- Parameters
177
- ----------
178
- device : np.ndarray
179
- A 2D numpy array representing the device's shape.
180
-
181
- linewidth : int, optional
182
- The width of the contour line. If not provided, the linewidth is set
183
- to 1% of the device's height.
184
-
185
- Returns
186
- -------
187
- np.ndarray
188
- A 2D numpy array (same shape as the input device) representing the device's contour.
189
- """
190
- if linewidth is None:
191
- linewidth = device.shape[0] // 100
192
-
193
- binary_device = binarize_hard(device).astype(np.uint8)
194
- contours, _ = cv2.findContours(
195
- binary_device, mode=cv2.RETR_CCOMP, method=cv2.CHAIN_APPROX_SIMPLE
196
- )
197
-
198
- contour_overlay = np.zeros_like(device)
199
- cv2.drawContours(contour_overlay, contours, -1, (255, 255, 255), linewidth)
200
-
201
- return np.ma.masked_where(contour_overlay == 0, contour_overlay)
202
-
203
-
204
- def calculate_prediction_uncertainty(prediction: np.ndarray) -> np.ndarray:
205
- """
206
- Computes the uncertainty profile of a non-binary prediction matrix.
207
-
208
- This function quantifies the level of uncertainty in a given prediction matrix by
209
- identifying the areas between the core (value 1) and cladding (value 0). These regions
210
- often correspond to the boundaries of the predicted structure and are represented by
211
- pixel values ranging between 0 and 1 in the prediction matrix. The function calculates
212
- the uncertainty as the distance from the pixel value to the nearest extreme (0 or 1),
213
- highlighting regions of maximum uncertainty.
214
-
215
- Parameters
216
- ----------
217
- prediction : np.ndarray
218
- A 2D numpy array representing the non-binary prediction matrix of a device shape.
219
-
220
- Returns
221
- -------
222
- np.ndarray
223
- A 2D numpy array (same shape as the input prediction matrix) representing the
224
- uncertainty profile of the prediction. Higher values correspond to areas of higher
225
- uncertainty.
226
- """
227
- uncertainty = 1 - 2 * np.abs(0.5 - prediction)
228
- return uncertainty
229
-
230
-
231
- def mse(prediction: np.ndarray, device: np.ndarray) -> float:
232
- """
233
- Computes the mean squared error (MSE) between a prediction and a device matrix.
234
-
235
- Parameters
236
- ----------
237
- prediction : np.ndarray
238
- A 2D numpy array representing the non-binary prediction matrix of a device shape.
239
-
240
- device : np.ndarray
241
- A 2D numpy array representing the non-binary device matrix of a device shape.
242
-
243
- Returns
244
- -------
245
- float
246
- The mean squared error between the prediction and device matrices.
247
- """
248
- return np.mean((prediction - device) ** 2)
@@ -1,9 +0,0 @@
1
- prefab/__init__.py,sha256=0r0FhWL0XnAE_rq9OuqrNsXNOWjU8eI3-wMvHVDBHWc,1461
2
- prefab/__main__.py,sha256=xmZcbvON0Fqc8aXozxGIww9I3qGcTlzwSh5ny94zZvA,2710
3
- prefab/io.py,sha256=9tm_KCd8YqG-LYkNF9iqBu1kzXvkyKEVFWX8RcDp-GQ,6587
4
- prefab/predictor.py,sha256=5UEY1RomqvWZDTovvjiClPFx3UFxe-M_tWshZQrqVRA,7379
5
- prefab/processor.py,sha256=lmSOTouyFOb8UZo7JLXi78VjdUz6eJoOyPZMn6IKraw,8996
6
- prefab-0.5.1.dist-info/METADATA,sha256=WrdAivvTIHQIaynbOLOBJ6c5jAC0hM2nQK9nfQdWSOY,35537
7
- prefab-0.5.1.dist-info/WHEEL,sha256=TJPnKdtrSue7xZ_AVGkp9YXcvDrobsjBds1du3Nx6dc,87
8
- prefab-0.5.1.dist-info/licenses/LICENSE,sha256=IMF9i4xIpgCADf0U-V1cuf9HBmqWQd3qtI3FSuyW4zE,26526
9
- prefab-0.5.1.dist-info/RECORD,,