prefab 1.1.3__py3-none-any.whl → 1.1.4__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/__init__.py CHANGED
@@ -5,7 +5,7 @@ Usage:
5
5
  import prefab as pf
6
6
  """
7
7
 
8
- __version__ = "1.1.3"
8
+ __version__ = "1.1.4"
9
9
 
10
10
  from . import compare, geometry, predict, read, shapes
11
11
  from .device import BufferSpec, Device
prefab/predict.py CHANGED
@@ -1,3 +1,5 @@
1
+ """Provides prediction functions for ndarrays of device geometries."""
2
+
1
3
  import base64
2
4
  import io
3
5
  import json
@@ -6,6 +8,8 @@ import os
6
8
  import numpy as np
7
9
  import requests
8
10
  import toml
11
+ from autograd import primitive
12
+ from autograd.extend import defvjp
9
13
  from PIL import Image
10
14
  from tqdm import tqdm
11
15
 
@@ -23,24 +27,34 @@ def predict_array(
23
27
  gpu: bool = False,
24
28
  ) -> np.ndarray:
25
29
  """
26
- Predicts the output array for a given device array using a specified model.
30
+ Predict the nanofabrication outcome of a device array using a specified model.
27
31
 
28
- This function sends the device array to a prediction service, which uses a machine
29
- learning model to predict the outcome of the nanofabrication process. The prediction
30
- can be performed on a GPU if specified.
32
+ This function sends the device array to a serverless prediction service, which uses
33
+ a specified machine learning model to predict the outcome of the nanofabrication
34
+ process. The prediction can be performed on a GPU if specified.
31
35
 
32
36
  Parameters
33
37
  ----------
34
38
  device_array : np.ndarray
35
- The input device array to be predicted.
39
+ A 2D array representing the planar geometry of the device. This array undergoes
40
+ various transformations to predict the nanofabrication process.
36
41
  model : Model
37
- The model to use for prediction.
42
+ The model to use for prediction, representing a specific fabrication process and
43
+ dataset. This model encapsulates details about the fabrication foundry, process,
44
+ material, technology, thickness, and sidewall presence, as defined in
45
+ `models.py`. Each model is associated with a version and dataset that detail its
46
+ creation and the data it was trained on, ensuring the prediction is tailored to
47
+ specific fabrication parameters.
38
48
  model_type : str
39
49
  The type of model to use (e.g., 'p', 'c', 's').
40
50
  binarize : bool
41
- Whether to binarize the output.
51
+ If True, the predicted device geometry will be binarized using a threshold
52
+ method. This is useful for converting probabilistic predictions into binary
53
+ geometries.
42
54
  gpu : bool, optional
43
- Whether to use GPU for prediction. Defaults to False.
55
+ If True, the prediction will be performed on a GPU. Defaults to False. Note: The
56
+ GPU option has more overhead and will take longer for small devices, but will be
57
+ faster for larger devices.
44
58
 
45
59
  Returns
46
60
  -------
@@ -69,38 +83,11 @@ def predict_array(
69
83
  raise RuntimeError(f"Request failed: {e}") from e
70
84
 
71
85
 
72
- def predict_array_with_grad(
73
- device_array: np.ndarray, model: Model, model_type: str
86
+ def _predict_array_with_grad(
87
+ device_array: np.ndarray, model: Model
74
88
  ) -> tuple[np.ndarray, np.ndarray]:
75
- """
76
- Predicts the output array and its gradient for a given device array using a
77
- specified model.
78
-
79
- This function sends the device array to a prediction service, which uses a machine
80
- learning model to predict both the outcome and the gradient of the nanofabrication
81
- process.
82
-
83
- Parameters
84
- ----------
85
- device_array : np.ndarray
86
- The input device array to be predicted.
87
- model : Model
88
- The model to use for prediction.
89
- model_type : str
90
- The type of model to use (e.g., 'p', 'c', 's').
91
-
92
- Returns
93
- -------
94
- tuple[np.ndarray, np.ndarray]
95
- A tuple containing the predicted output array and its gradient.
96
-
97
- Raises
98
- ------
99
- RuntimeError
100
- If the request to the prediction service fails.
101
- """
102
89
  headers = _prepare_headers()
103
- predict_data = _prepare_predict_data(device_array, model, model_type, False)
90
+ predict_data = _prepare_predict_data(device_array, model, "p", False)
104
91
  endpoint_url = f"{BASE_URL}-with-grad-v1.modal.run"
105
92
 
106
93
  response = requests.post(
@@ -116,8 +103,68 @@ def predict_array_with_grad(
116
103
  return (prediction_array, gradient_array)
117
104
 
118
105
 
106
+ @primitive
107
+ def predict_array_with_grad(device_array: np.ndarray, model: Model) -> np.ndarray:
108
+ """
109
+ Predict the nanofabrication outcome of a device array and compute its gradient.
110
+
111
+ This function predicts the outcome of the nanofabrication process for a given
112
+ device array using a specified model. It also computes the gradient of the
113
+ prediction with respect to the input device array.
114
+
115
+ Parameters
116
+ ----------
117
+ device_array : np.ndarray
118
+ A 2D array representing the planar geometry of the device.
119
+ model : Model
120
+ The model to use for prediction, representing a specific fabrication process.
121
+
122
+ Returns
123
+ -------
124
+ np.ndarray
125
+ The predicted output array.
126
+ """
127
+ prediction_array, gradient_array = _predict_array_with_grad(
128
+ device_array=device_array, model=model
129
+ )
130
+ predict_array_with_grad.gradient_array = gradient_array
131
+ return prediction_array
132
+
133
+
134
+ def predict_array_with_grad_vjp(ans: np.ndarray, x: np.ndarray, model: Model):
135
+ """
136
+ Define the vector-Jacobian product (VJP) for the prediction function.
137
+
138
+ This function provides the VJP for the `predict_array_with_grad` function,
139
+ which is used in reverse-mode automatic differentiation to compute gradients.
140
+
141
+ Parameters
142
+ ----------
143
+ ans : np.ndarray
144
+ The output of the `predict_array_with_grad` function.
145
+ x : np.ndarray
146
+ The input device array for which the gradient is computed.
147
+ model : Model
148
+ The model used for prediction.
149
+
150
+ Returns
151
+ -------
152
+ function
153
+ A function that computes the VJP given an upstream gradient `g`.
154
+ """
155
+ grad_x = predict_array_with_grad.gradient_array
156
+
157
+ def vjp(g: np.ndarray) -> np.ndarray:
158
+ return g * grad_x
159
+
160
+ return vjp
161
+
162
+
163
+ defvjp(predict_array_with_grad, predict_array_with_grad_vjp)
164
+
165
+
119
166
  def _encode_array(array):
120
- """Encode a numpy array as a PNG image and return the base64 encoded string."""
167
+ """Encode an ndarray as a base64 encoded image for transmission."""
121
168
  image = Image.fromarray(np.uint8(array * 255))
122
169
  buffered = io.BytesIO()
123
170
  image.save(buffered, format="PNG")
@@ -126,7 +173,7 @@ def _encode_array(array):
126
173
 
127
174
 
128
175
  def _decode_array(encoded_png):
129
- """Decode a base64 encoded PNG image and return a numpy array."""
176
+ """Decode a base64 encoded image and return an ndarray."""
130
177
  binary_data = base64.b64decode(encoded_png)
131
178
  image = Image.open(io.BytesIO(binary_data))
132
179
  return np.array(image) / 255
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: prefab
3
- Version: 1.1.3
3
+ Version: 1.1.4
4
4
  Summary: Artificial nanofabrication of integrated photonic circuits using deep learning
5
5
  Project-URL: Homepage, https://prefabphotonics.com
6
6
  Project-URL: Repository, https://github.com/PreFab-Photonics/PreFab
@@ -1,13 +1,13 @@
1
- prefab/__init__.py,sha256=DuARo1QC4MGMw-yzCBLEoRlOU3MsKZTbWDCAqUpQjWM,425
1
+ prefab/__init__.py,sha256=7MmkG6X6FZ7c4lS8eAF5cNu_NYnwIeDVgwCiX9ysYPw,425
2
2
  prefab/__main__.py,sha256=aAgt1WXa44k1nJqsiSD3uAfNeGpwtjWqMUYCHN5_Qrw,2759
3
3
  prefab/compare.py,sha256=2MKUT7N2A639tUGCnJHpfF9MmS-v3oARDkTqHbWJ9OM,3239
4
4
  prefab/device.py,sha256=l0etqB0559xa9Frb0IKl5N3kU56vWHlwHc-qehINTlw,52292
5
5
  prefab/geometry.py,sha256=0sa6ietUWZGkxOnUPUzD3q2QpFuOpWkSANoopGpPd6s,11035
6
6
  prefab/models.py,sha256=JpBqNFIqbo1ymKEl0NWWF4ZkhvK23rEVVBEFl05TXCI,3671
7
- prefab/predict.py,sha256=AuCh_vOMP0dD68u75WQaOTog6TMi1FG3nLQxv6UgIkA,8579
7
+ prefab/predict.py,sha256=upGTeNC6vY9Tdko6JEM0gKQH1PGUVYXACpep1OT8EVs,10593
8
8
  prefab/read.py,sha256=MuF-cugFQ7MWBJ8DOvQuwktIk0fJ8PXBeLye0ydrB8o,14734
9
9
  prefab/shapes.py,sha256=2qaqyNzu5WG3wVdk4oQzeNXmhwXRHcPnRZlgRrM4MoA,25576
10
- prefab-1.1.3.dist-info/METADATA,sha256=TGbeM7u0kktbeGeOwGIFS43dwp8rWvLSwHDYlXEcsfI,34824
11
- prefab-1.1.3.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
12
- prefab-1.1.3.dist-info/licenses/LICENSE,sha256=IMF9i4xIpgCADf0U-V1cuf9HBmqWQd3qtI3FSuyW4zE,26526
13
- prefab-1.1.3.dist-info/RECORD,,
10
+ prefab-1.1.4.dist-info/METADATA,sha256=FDqQukBN09Eg4BDhn7Q7p46nWOoLwxjWk9CP2VeRoxo,34824
11
+ prefab-1.1.4.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
12
+ prefab-1.1.4.dist-info/licenses/LICENSE,sha256=IMF9i4xIpgCADf0U-V1cuf9HBmqWQd3qtI3FSuyW4zE,26526
13
+ prefab-1.1.4.dist-info/RECORD,,
File without changes