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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"id": "separated-percentage",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": [
|
10 |
+
"from scipy.stats.qmc import PoissonDisk\n",
|
11 |
+
"import numpy as np\n",
|
12 |
+
"from scipy.spatial.distance import cdist\n",
|
13 |
+
"import napari"
|
14 |
+
]
|
15 |
+
},
|
16 |
+
{
|
17 |
+
"cell_type": "code",
|
18 |
+
"execution_count": 2,
|
19 |
+
"id": "flush-howard",
|
20 |
+
"metadata": {},
|
21 |
+
"outputs": [],
|
22 |
+
"source": [
|
23 |
+
"np.random.seed(42)"
|
24 |
+
]
|
25 |
+
},
|
26 |
+
{
|
27 |
+
"cell_type": "code",
|
28 |
+
"execution_count": 3,
|
29 |
+
"id": "legitimate-defense",
|
30 |
+
"metadata": {},
|
31 |
+
"outputs": [],
|
32 |
+
"source": [
|
33 |
+
"scale = 128\n",
|
34 |
+
"radius = 10 / scale\n",
|
35 |
+
"border = 1.5\n",
|
36 |
+
"fraction = 0.5\n",
|
37 |
+
"\n",
|
38 |
+
"k0=0.85\n",
|
39 |
+
"k1 = 1.5\n",
|
40 |
+
"delta = 0.01\n",
|
41 |
+
"mean_scale = 0.95\n"
|
42 |
+
]
|
43 |
+
},
|
44 |
+
{
|
45 |
+
"cell_type": "code",
|
46 |
+
"execution_count": 4,
|
47 |
+
"id": "adapted-tourism",
|
48 |
+
"metadata": {},
|
49 |
+
"outputs": [],
|
50 |
+
"source": [
|
51 |
+
"obj = PoissonDisk(d=3, radius=radius, hypersphere='volume', ncandidates=30, optimization=None, seed=None)"
|
52 |
+
]
|
53 |
+
},
|
54 |
+
{
|
55 |
+
"cell_type": "code",
|
56 |
+
"execution_count": 5,
|
57 |
+
"id": "comparable-coast",
|
58 |
+
"metadata": {
|
59 |
+
"scrolled": false
|
60 |
+
},
|
61 |
+
"outputs": [],
|
62 |
+
"source": [
|
63 |
+
"tmp = obj.fill_space()*scale\n",
|
64 |
+
"selz = (tmp[:,0] > border*scale*radius) & (tmp[:,0]<(scale-border*scale*radius))\n",
|
65 |
+
"sely = (tmp[:,1] > border*scale*radius) & (tmp[:,1]<(scale-border*scale*radius))\n",
|
66 |
+
"selx = (tmp[:,2] > border*scale*radius) & (tmp[:,2]<(scale-border*scale*radius))\n",
|
67 |
+
"sel = selz & sely & selx \n",
|
68 |
+
"tmp = tmp[sel]"
|
69 |
+
]
|
70 |
+
},
|
71 |
+
{
|
72 |
+
"cell_type": "code",
|
73 |
+
"execution_count": 6,
|
74 |
+
"id": "existing-clerk",
|
75 |
+
"metadata": {},
|
76 |
+
"outputs": [],
|
77 |
+
"source": [
|
78 |
+
"volume = np.zeros( (int(scale), int(scale), int(scale)) )\n",
|
79 |
+
"labels = np.zeros_like(volume)"
|
80 |
+
]
|
81 |
+
},
|
82 |
+
{
|
83 |
+
"cell_type": "code",
|
84 |
+
"execution_count": 7,
|
85 |
+
"id": "intense-soviet",
|
86 |
+
"metadata": {
|
87 |
+
"scrolled": false
|
88 |
+
},
|
89 |
+
"outputs": [],
|
90 |
+
"source": [
|
91 |
+
"sphere_radius = radius*scale*k0/2.0 # Set your desired radius for the sphere\n",
|
92 |
+
"k2 = 1.0 / np.sqrt(k1)\n",
|
93 |
+
"major_axis = sphere_radius*k1\n",
|
94 |
+
"minor_axis = sphere_radius*k2\n",
|
95 |
+
"\n",
|
96 |
+
"\n",
|
97 |
+
"\n",
|
98 |
+
"def fill_sphere(center, radius, volume, labels, label =1 ):\n",
|
99 |
+
" # Create an array with the coordinates of each point in the volume\n",
|
100 |
+
" x, y, z = np.indices(volume.shape)\n",
|
101 |
+
"\n",
|
102 |
+
" # Calculate the squared distance from each point to the center\n",
|
103 |
+
" dist_sq = (x - center[0])**2 + (y - center[1])**2 + (z - center[2])**2\n",
|
104 |
+
"\n",
|
105 |
+
" # A point is inside the sphere if its distance to the center is less than the radius\n",
|
106 |
+
" inside_sphere = dist_sq < (radius**2)\n",
|
107 |
+
"\n",
|
108 |
+
" # Set the value of points inside the sphere to 1\n",
|
109 |
+
" volume[inside_sphere] = 1 \n",
|
110 |
+
" labels[inside_sphere] = label \n",
|
111 |
+
"\n",
|
112 |
+
"def fill_ellipsoid_simple(center, major_axis, minor_axis, volume, labels, label = 2):\n",
|
113 |
+
" # Create an array with the coordinates of each point in the volume\n",
|
114 |
+
" x, y, z = np.indices(volume.shape)\n",
|
115 |
+
"\n",
|
116 |
+
" # Calculate the normalized squared distances along each axis\n",
|
117 |
+
" dist_sq_x = ((x - center[0]) / major_axis)**2\n",
|
118 |
+
" dist_sq_y = ((y - center[1]) / minor_axis)**2\n",
|
119 |
+
" dist_sq_z = ((z - center[2]) / minor_axis)**2\n",
|
120 |
+
"\n",
|
121 |
+
" # A point is inside the ellipsoid if the sum of the squared distances is less than 1\n",
|
122 |
+
" inside_ellipsoid = dist_sq_x + dist_sq_y + dist_sq_z < 1\n",
|
123 |
+
"\n",
|
124 |
+
" # Set the value of points inside the ellipsoid to 1\n",
|
125 |
+
" volume[inside_ellipsoid] = 1\n",
|
126 |
+
" labels[inside_ellipsoid] = label\n",
|
127 |
+
"\n",
|
128 |
+
"import numpy as np\n",
|
129 |
+
"\n",
|
130 |
+
"def random_rotation_matrix():\n",
|
131 |
+
" theta, phi, z = np.random.uniform(0, 2*np.pi, 3)\n",
|
132 |
+
"\n",
|
133 |
+
" # Rotation about the z-axis\n",
|
134 |
+
" rz = np.array([\n",
|
135 |
+
" [np.cos(z), -np.sin(z), 0],\n",
|
136 |
+
" [np.sin(z), np.cos(z), 0],\n",
|
137 |
+
" [0, 0, 1]\n",
|
138 |
+
" ])\n",
|
139 |
+
"\n",
|
140 |
+
" # Rotation about the y-axis\n",
|
141 |
+
" ry = np.array([\n",
|
142 |
+
" [np.cos(phi), 0, np.sin(phi)],\n",
|
143 |
+
" [0, 1, 0],\n",
|
144 |
+
" [-np.sin(phi), 0, np.cos(phi)]\n",
|
145 |
+
" ])\n",
|
146 |
+
"\n",
|
147 |
+
" # Rotation about the x-axis\n",
|
148 |
+
" rx = np.array([\n",
|
149 |
+
" [1, 0, 0],\n",
|
150 |
+
" [0, np.cos(theta), -np.sin(theta)],\n",
|
151 |
+
" [0, np.sin(theta), np.cos(theta)]\n",
|
152 |
+
" ])\n",
|
153 |
+
"\n",
|
154 |
+
" return np.dot(rz, np.dot(ry, rx))\n",
|
155 |
+
"\n",
|
156 |
+
"def fill_ellipsoid(center, major_axis, minor_axis, volume, labels, label=2):\n",
|
157 |
+
" # Create an array with the coordinates of each point in the volume\n",
|
158 |
+
" x, y, z = np.indices(volume.shape).astype(float)\n",
|
159 |
+
"\n",
|
160 |
+
" # Center the points\n",
|
161 |
+
" x -= center[0]\n",
|
162 |
+
" y -= center[1]\n",
|
163 |
+
" z -= center[2]\n",
|
164 |
+
"\n",
|
165 |
+
" # Apply rotation\n",
|
166 |
+
" rotation_matrix = random_rotation_matrix()\n",
|
167 |
+
" rotated_coords = np.dot(rotation_matrix, np.array([x.ravel(), y.ravel(), z.ravel()]))\n",
|
168 |
+
" \n",
|
169 |
+
" x_rotated, y_rotated, z_rotated = rotated_coords.reshape(3, *volume.shape)\n",
|
170 |
+
"\n",
|
171 |
+
" # Calculate the normalized squared distances\n",
|
172 |
+
" dist_sq_x = (x_rotated / major_axis)**2\n",
|
173 |
+
" dist_sq_y = (y_rotated / minor_axis)**2\n",
|
174 |
+
" dist_sq_z = (z_rotated / minor_axis)**2\n",
|
175 |
+
"\n",
|
176 |
+
" # Check if points are inside the ellipsoid\n",
|
177 |
+
" inside_ellipsoid = dist_sq_x + dist_sq_y + dist_sq_z < 1\n",
|
178 |
+
"\n",
|
179 |
+
" # Set the value of points inside the ellipsoid to 1\n",
|
180 |
+
" volume[inside_ellipsoid] = 1\n",
|
181 |
+
" labels[inside_ellipsoid] = label\n",
|
182 |
+
"\n"
|
183 |
+
]
|
184 |
+
},
|
185 |
+
{
|
186 |
+
"cell_type": "code",
|
187 |
+
"execution_count": 8,
|
188 |
+
"id": "integral-dynamics",
|
189 |
+
"metadata": {},
|
190 |
+
"outputs": [],
|
191 |
+
"source": [
|
192 |
+
"for point in tmp:\n",
|
193 |
+
" shape = 'sphere' if np.random.rand() > fraction else 'ellipsoid'\n",
|
194 |
+
" multi = np.random.rand()\n",
|
195 |
+
" multi = multi*delta - delta / 2.0 + mean_scale\n",
|
196 |
+
" \n",
|
197 |
+
" if shape == 'sphere':\n",
|
198 |
+
" fill_sphere(center=point, radius=sphere_radius*multi, volume=volume, labels=labels)\n",
|
199 |
+
" else:\n",
|
200 |
+
" # For ellipsoids, we can randomize the orientation and axis lengths\n",
|
201 |
+
" major_axis = sphere_radius * k1 * multi\n",
|
202 |
+
" minor_axis = sphere_radius * k2 * multi\n",
|
203 |
+
" fill_ellipsoid(center=point, \n",
|
204 |
+
" major_axis=major_axis, \n",
|
205 |
+
" minor_axis=minor_axis, \n",
|
206 |
+
" volume=volume,\n",
|
207 |
+
" labels = labels\n",
|
208 |
+
" )\n"
|
209 |
+
]
|
210 |
+
},
|
211 |
+
{
|
212 |
+
"cell_type": "code",
|
213 |
+
"execution_count": null,
|
214 |
+
"id": "younger-dialogue",
|
215 |
+
"metadata": {},
|
216 |
+
"outputs": [],
|
217 |
+
"source": [
|
218 |
+
"v = napari.view_image(volume)\n",
|
219 |
+
"_ = v.add_labels(labels.astype(np.int8))"
|
220 |
+
]
|
221 |
+
},
|
222 |
+
{
|
223 |
+
"cell_type": "code",
|
224 |
+
"execution_count": 9,
|
225 |
+
"id": "advance-latest",
|
226 |
+
"metadata": {},
|
227 |
+
"outputs": [],
|
228 |
+
"source": [
|
229 |
+
"np.save(\"benchmark_volume\", volume)\n",
|
230 |
+
"np.save(\"benchmark_labels\", labels)"
|
231 |
+
]
|
232 |
+
},
|
233 |
+
{
|
234 |
+
"cell_type": "code",
|
235 |
+
"execution_count": null,
|
236 |
+
"id": "molecular-visiting",
|
237 |
+
"metadata": {},
|
238 |
+
"outputs": [],
|
239 |
+
"source": []
|
240 |
+
}
|
241 |
+
],
|
242 |
+
"metadata": {
|
243 |
+
"kernelspec": {
|
244 |
+
"display_name": "dlsia-new",
|
245 |
+
"language": "python",
|
246 |
+
"name": "dlsia-new"
|
247 |
+
},
|
248 |
+
"language_info": {
|
249 |
+
"codemirror_mode": {
|
250 |
+
"name": "ipython",
|
251 |
+
"version": 3
|
252 |
+
},
|
253 |
+
"file_extension": ".py",
|
254 |
+
"mimetype": "text/x-python",
|
255 |
+
"name": "python",
|
256 |
+
"nbconvert_exporter": "python",
|
257 |
+
"pygments_lexer": "ipython3",
|
258 |
+
"version": "3.9.18"
|
259 |
+
}
|
260 |
+
},
|
261 |
+
"nbformat": 4,
|
262 |
+
"nbformat_minor": 5
|
263 |
+
}
|
build_benchmark_training.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|