prefab 0.5.1__py3-none-any.whl → 1.0.0__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
@@ -1,47 +1,19 @@
1
1
  """
2
- The PreFab module provides functionality for predicting and processing photonics
3
- device layouts, and utilities for interacting with GDSII files.
2
+ The prefab module predicts and corrects nanofabrication variations in photonic devices.
4
3
 
5
4
  Usage:
6
5
  import prefab as pf
7
6
  """
8
7
 
9
- # Prediction and Correction
10
- from prefab.predictor import predict
11
- from prefab.predictor import correct
8
+ from . import compare, geometry, read
9
+ from .device import BufferSpec, Device
10
+ from .models import models
12
11
 
13
- # I/O Utilities
14
- # Function to load/save device images and gds files
15
- from prefab.io import (
16
- load_device_img, # Load device from an image file
17
- load_device_gds, # Load device from a GDSII file
18
- device_to_cell, # Convert a device layout to a gdstk cell
19
- )
20
-
21
- # Import image processing utilities
22
- # Functions to modify and manipulate device images
23
- from prefab.processor import (
24
- binarize, # Soft binarization of grayscale images
25
- binarize_hard, # Hard binarization of grayscale images
26
- ternarize, # Ternarization of grayscale images
27
- remove_padding, # Trims excess padding from device images
28
- zero_boundary, # Applies zero boundary to device images
29
- generate_device_contour, # Generates contour of device images
30
- calculate_prediction_uncertainty, # Computes prediction uncertainty for device images
31
- mse, # Computes mean squared error between two images
32
- )
33
-
34
- __all__ = (
35
- "predict",
36
- "load_device_img",
37
- "load_device_gds",
38
- "device_to_cell",
39
- "binarize",
40
- "binarize_hard",
41
- "ternarize",
42
- "remove_padding",
43
- "zero_boundary",
44
- "generate_device_contour",
45
- "calculate_prediction_uncertainty",
46
- "mse",
47
- )
12
+ __all__ = [
13
+ "Device",
14
+ "BufferSpec",
15
+ "geometry",
16
+ "read",
17
+ "compare",
18
+ "models",
19
+ ]
prefab/__main__.py CHANGED
@@ -1,17 +1,18 @@
1
- """Main entry point for the Prefab CLI."""
1
+ """Provides the main entry point for the Prefab CLI."""
2
+
2
3
  import argparse
3
4
  import os
4
5
  import threading
5
6
  import webbrowser
7
+ from contextlib import suppress
6
8
  from http.server import BaseHTTPRequestHandler, HTTPServer
7
9
 
8
10
  import toml
9
11
 
10
12
 
11
13
  def store_jwt_securely(jwt, refresh_token):
12
- """
13
- Store the JWT and refresh token securely in a TOML file.
14
- """
14
+ """Store the JWT and refresh token securely in a TOML file."""
15
+
15
16
  prefab_file_path = os.path.expanduser("~/.prefab.toml")
16
17
  with open(prefab_file_path, "w", encoding="utf-8") as toml_file:
17
18
  toml.dump({"access_token": jwt, "refresh_token": refresh_token}, toml_file)
@@ -28,9 +29,7 @@ class GracefulHTTPServer(HTTPServer):
28
29
 
29
30
 
30
31
  class CallbackHandler(BaseHTTPRequestHandler):
31
- """
32
- A request handler for the HTTP server that handles the OAuth callback.
33
- """
32
+ """A request handler for the HTTP server that handles the OAuth callback."""
34
33
 
35
34
  def do_GET(self):
36
35
  if self.path.startswith("/callback"):
@@ -56,6 +55,7 @@ class CallbackHandler(BaseHTTPRequestHandler):
56
55
 
57
56
 
58
57
  def main():
58
+ """Main function for the Prefab CLI."""
59
59
  parser = argparse.ArgumentParser(description="Prefab CLI")
60
60
  parser.add_argument("command", help="The command to run", choices=["setup"])
61
61
  parser.add_argument(
@@ -68,10 +68,8 @@ def main():
68
68
  webbrowser.open("https://www.prefabphotonics.com/token-flow")
69
69
  httpd = GracefulHTTPServer(("localhost", args.port), CallbackHandler)
70
70
  print("Started token authentication flow on the web browser...")
71
- try:
71
+ with suppress(KeyboardInterrupt):
72
72
  httpd.serve_forever()
73
- except KeyboardInterrupt:
74
- pass
75
73
  httpd.server_close()
76
74
  else:
77
75
  print(f"Command {args.command} not recognized.")
prefab/compare.py ADDED
@@ -0,0 +1,92 @@
1
+ """Provides functions to measure the similarity between devices."""
2
+
3
+ import numpy as np
4
+
5
+ from .device import Device
6
+
7
+
8
+ def mean_squared_error(device_a: Device, device_b: Device) -> float:
9
+ """
10
+ Calculate the mean squared error (MSE) between two non-binarized devices.
11
+
12
+ Parameters
13
+ ----------
14
+ device_a : Device
15
+ The first device.
16
+ device_b : Device
17
+ The second device.
18
+
19
+ Returns
20
+ -------
21
+ float
22
+ The mean squared error between two devices. A lower value indicates more
23
+ similarity.
24
+ """
25
+ return np.mean((device_a.device_array - device_b.device_array) ** 2)
26
+
27
+
28
+ def intersection_over_union(device_a: Device, device_b: Device) -> float:
29
+ """
30
+ Calculates the Intersection over Union (IoU) between two binary devices.
31
+
32
+ Parameters
33
+ ----------
34
+ device_a : Device
35
+ The first device.
36
+ device_b : Device
37
+ The second device.
38
+
39
+ Returns
40
+ -------
41
+ float
42
+ The Intersection over Union between two devices. A value closer to 1 indicates
43
+ more similarity (more overlap).
44
+ """
45
+ return np.sum(
46
+ np.logical_and(device_a.device_array, device_b.device_array)
47
+ ) / np.sum(np.logical_or(device_a.device_array, device_b.device_array))
48
+
49
+
50
+ def hamming_distance(device_a: Device, device_b: Device) -> int:
51
+ """
52
+ Calculates the Hamming distance between two binary devices.
53
+
54
+ Parameters
55
+ ----------
56
+ device_a : Device
57
+ The first device.
58
+ device_b : Device
59
+ The second device.
60
+
61
+ Returns
62
+ -------
63
+ int
64
+ The Hamming distance between two devices. A lower value indicates more
65
+ similarity.
66
+ """
67
+ return np.sum(device_a.device_array != device_b.device_array)
68
+
69
+
70
+ def dice_coefficient(device_a: Device, device_b: Device) -> float:
71
+ """
72
+ Calculates the Dice coefficient between two binary devices.
73
+
74
+ Parameters
75
+ ----------
76
+ device_a : Device
77
+ The first device.
78
+ device_b : Device
79
+ The second device.
80
+
81
+ Returns
82
+ -------
83
+ float
84
+ The Dice coefficient between two devices. A value closer to 1 indicates more
85
+ similarity.
86
+ """
87
+ intersection = 2.0 * np.sum(
88
+ np.logical_and(device_a.device_array, device_b.device_array)
89
+ )
90
+ size_a = np.sum(device_a.device_array)
91
+ size_b = np.sum(device_b.device_array)
92
+ return intersection / (size_a + size_b)