-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprinting_tools.py
More file actions
161 lines (113 loc) · 5.11 KB
/
Copy pathprinting_tools.py
File metadata and controls
161 lines (113 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import matplotlib.pyplot as plt
from tqdm import tqdm
import jax
import jax.numpy as jnp
from jax import random, grad, jit
import numpy as np
def make_hollow_box(cube_size, cube_size_z, edge_width, N_target, dx, z_distances):
x = jnp.linspace(-N_target/2, N_target/2, N_target) * dx
y = jnp.linspace(-N_target/2, N_target/2, N_target) * dx
X, Y = jnp.meshgrid(x, y, indexing='ij')
X = X.reshape(1, 1, N_target, N_target)
Y = Y.reshape(1, 1, N_target, N_target)
Z = z_distances.reshape(1, z_distances.shape[0], 1, 1)
# Define 500µm cube centered at origin
half_size = cube_size / 2
half_width = edge_width / 2
solid_cube = (jnp.abs(X) < cube_size / 2) *(jnp.abs(Y) < cube_size / 2) * (jnp.abs(Z) < cube_size_z / 2)
subtract_cube_1 = (jnp.abs(X) < cube_size / 2 - edge_width) * (jnp.abs(Y) < cube_size / 2 - edge_width) * 1.0
subtract_cube_2 = (jnp.abs(Z) < cube_size_z / 2 - edge_width) * (jnp.abs(Y) < cube_size / 2 - edge_width) * 1.0
subtract_cube_3 = (jnp.abs(Z) < cube_size_z / 2 - edge_width) * (jnp.abs(X) < cube_size / 2 - edge_width) * 1.0
hollow_box = ((solid_cube - subtract_cube_1 - subtract_cube_2 - subtract_cube_3) > 0) * 1.0
return hollow_box
def make_hollow_box2(cube_size, cube_size_z, edge_width, N_target, dx, z_distances):
x = jnp.linspace(-N_target/2, N_target/2, N_target) * dx
y = jnp.linspace(-N_target/2, N_target/2, N_target) * dx
X, Y = jnp.meshgrid(x, y, indexing='ij')
X = X.reshape(1, 1, N_target, N_target)
Y = Y.reshape(1, 1, N_target, N_target)
Z = z_distances.reshape(1, z_distances.shape[0], 1, 1)
z_max = jnp.max(Z)
# Define 500µm cube centered at origin
half_size = cube_size / 2
half_width = edge_width / 2
solid_cube = (jnp.abs(X) < cube_size / 2) *(jnp.abs(Y) < cube_size / 2) * (jnp.abs(Z - z_max / 2) < cube_size_z / 2)
subtract_cube_1 = (jnp.abs(X) < cube_size / 2 - edge_width) * (jnp.abs(Y) < cube_size / 2 - edge_width) * 1.0
subtract_cube_2 = (jnp.abs(Z-z_max / 2) < cube_size_z / 2 - edge_width * 2) * (jnp.abs(Y) < cube_size / 2 - edge_width) * 1.0
subtract_cube_3 = (jnp.abs(Z-z_max / 2) < cube_size_z / 2 - edge_width * 2) * (jnp.abs(X) < cube_size / 2 - edge_width) * 1.0
hollow_box = ((solid_cube - subtract_cube_1 - subtract_cube_2 - subtract_cube_3) > 0) * 1.0
return hollow_box
@jit
def calculate_iou(prediction, target, threshold):
"""
Calculates the Intersection over Union (IoU) for JAX arrays.
Args:
prediction: The predicted output array with continuous values.
target: The ground truth binary mask (0s and 1s).
threshold: The value to binarize the prediction array.
Returns:
The IoU score as a scalar float.
"""
# 1. Binarize the prediction based on the threshold
predicted_mask = (prediction >= threshold).astype(jnp.float32)
# 2. Ensure the target is also treated as a binary mask
target_mask = target.astype(jnp.float32)
# 3. Calculate intersection: The area where both masks are active (1)
intersection = jnp.sum(predicted_mask * target_mask)
# 4. Calculate union: The total area covered by either mask
# Union(A, B) = A + B - Intersection(A, B)
union = jnp.sum(predicted_mask) + jnp.sum(target_mask) - intersection
# 5. Compute IoU, adding a small epsilon to avoid division by zero
iou = intersection / (union + 1e-8)
return iou
# save as .npz
def save_patterns(filename, patterns):
"""
Saves patterns to a .npz file.
Args:
filename: The name of the file to save the patterns to.
patterns: A list or array of patterns to save.
"""
np.savez_compressed(filename, patterns)
print(f"Patterns saved to {filename}")
return 0
def load_patterns(filename):
"""
Loads patterns from a .npz file.
Args:
filename: The name of the file to load the patterns from.
Returns:
A list of loaded patterns.
"""
data = jnp.load(filename)
patterns = data["patterns"]
print(f"Patterns loaded from {filename}")
return patterns
import numpy as np
import jax.numpy as jnp
from PIL import Image
import os
def save_patterns(arr, bitmap_path, npy_filename):
arr = jnp.fft.fftshift(arr, axes=(-2,-1))
arr = arr.squeeze(1)
pad_width = 1920 - 1200
pad_left = pad_width // 2
pad_right = pad_width - pad_left
# Pad array: (batch, height, width)
phase = np.pad(arr, ((0, 0), (0, 0), (pad_left, pad_right)), mode='constant', constant_values=0)
# Calculate phase and normalize to 0-255
phase = jnp.angle(jnp.exp(1j * phase)) + jnp.pi
phase_image = phase / (2 * np.pi) * 255
# Convert to numpy
phase_np = np.array(phase)
# Create directory for npy file
npy_dir = os.path.dirname(npy_filename)
if npy_dir:
os.makedirs(npy_dir, exist_ok=True)
# Save as .npy
np.save(npy_filename, phase_np)
# Create directory and save as 20 individual 8-bit bitmaps
os.makedirs(bitmap_path, exist_ok=True)
for i in range(arr.shape[0]):
img = Image.fromarray(np.array(phase_image)[i], mode='L')
img.save(os.path.join(bitmap_path, f'pattern_{i:02d}.bmp'))