biomedisa 2024.5.14__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.
Files changed (44) hide show
  1. biomedisa/__init__.py +53 -0
  2. biomedisa/__main__.py +18 -0
  3. biomedisa/biomedisa_features/DataGenerator.py +299 -0
  4. biomedisa/biomedisa_features/DataGeneratorCrop.py +121 -0
  5. biomedisa/biomedisa_features/PredictDataGenerator.py +87 -0
  6. biomedisa/biomedisa_features/PredictDataGeneratorCrop.py +74 -0
  7. biomedisa/biomedisa_features/__init__.py +0 -0
  8. biomedisa/biomedisa_features/active_contour.py +434 -0
  9. biomedisa/biomedisa_features/amira_to_np/__init__.py +0 -0
  10. biomedisa/biomedisa_features/amira_to_np/amira_data_stream.py +980 -0
  11. biomedisa/biomedisa_features/amira_to_np/amira_grammar.py +369 -0
  12. biomedisa/biomedisa_features/amira_to_np/amira_header.py +290 -0
  13. biomedisa/biomedisa_features/amira_to_np/amira_helper.py +72 -0
  14. biomedisa/biomedisa_features/assd.py +167 -0
  15. biomedisa/biomedisa_features/biomedisa_helper.py +801 -0
  16. biomedisa/biomedisa_features/create_slices.py +286 -0
  17. biomedisa/biomedisa_features/crop_helper.py +586 -0
  18. biomedisa/biomedisa_features/curvop_numba.py +149 -0
  19. biomedisa/biomedisa_features/django_env.py +172 -0
  20. biomedisa/biomedisa_features/keras_helper.py +1219 -0
  21. biomedisa/biomedisa_features/nc_reader.py +179 -0
  22. biomedisa/biomedisa_features/pid.py +52 -0
  23. biomedisa/biomedisa_features/process_image.py +253 -0
  24. biomedisa/biomedisa_features/pycuda_test.py +84 -0
  25. biomedisa/biomedisa_features/random_walk/__init__.py +0 -0
  26. biomedisa/biomedisa_features/random_walk/gpu_kernels.py +183 -0
  27. biomedisa/biomedisa_features/random_walk/pycuda_large.py +826 -0
  28. biomedisa/biomedisa_features/random_walk/pycuda_large_allx.py +806 -0
  29. biomedisa/biomedisa_features/random_walk/pycuda_small.py +414 -0
  30. biomedisa/biomedisa_features/random_walk/pycuda_small_allx.py +493 -0
  31. biomedisa/biomedisa_features/random_walk/pyopencl_large.py +760 -0
  32. biomedisa/biomedisa_features/random_walk/pyopencl_small.py +441 -0
  33. biomedisa/biomedisa_features/random_walk/rw_large.py +390 -0
  34. biomedisa/biomedisa_features/random_walk/rw_small.py +310 -0
  35. biomedisa/biomedisa_features/remove_outlier.py +399 -0
  36. biomedisa/biomedisa_features/split_volume.py +274 -0
  37. biomedisa/deeplearning.py +519 -0
  38. biomedisa/interpolation.py +371 -0
  39. biomedisa/mesh.py +406 -0
  40. biomedisa-2024.5.14.dist-info/LICENSE +191 -0
  41. biomedisa-2024.5.14.dist-info/METADATA +306 -0
  42. biomedisa-2024.5.14.dist-info/RECORD +44 -0
  43. biomedisa-2024.5.14.dist-info/WHEEL +5 -0
  44. biomedisa-2024.5.14.dist-info/top_level.txt +1 -0
@@ -0,0 +1,414 @@
1
+ ##########################################################################
2
+ ## ##
3
+ ## Copyright (c) 2023 Philipp Lösel. All rights reserved. ##
4
+ ## ##
5
+ ## This file is part of the open source project biomedisa. ##
6
+ ## ##
7
+ ## Licensed under the European Union Public Licence (EUPL) ##
8
+ ## v1.2, or - as soon as they will be approved by the ##
9
+ ## European Commission - subsequent versions of the EUPL; ##
10
+ ## ##
11
+ ## You may redistribute it and/or modify it under the terms ##
12
+ ## of the EUPL v1.2. You may not use this work except in ##
13
+ ## compliance with this Licence. ##
14
+ ## ##
15
+ ## You can obtain a copy of the Licence at: ##
16
+ ## ##
17
+ ## https://joinup.ec.europa.eu/page/eupl-text-11-12 ##
18
+ ## ##
19
+ ## Unless required by applicable law or agreed to in ##
20
+ ## writing, software distributed under the Licence is ##
21
+ ## distributed on an "AS IS" basis, WITHOUT WARRANTIES ##
22
+ ## OR CONDITIONS OF ANY KIND, either express or implied. ##
23
+ ## ##
24
+ ## See the Licence for the specific language governing ##
25
+ ## permissions and limitations under the Licence. ##
26
+ ## ##
27
+ ##########################################################################
28
+
29
+ import numpy as np
30
+ import pycuda.driver as cuda
31
+ import pycuda.gpuarray as gpuarray
32
+ from pycuda.compiler import SourceModule
33
+ from biomedisa_features.random_walk.gpu_kernels import _build_kernel_fill
34
+
35
+ def walk(data, slices, indices, indices_child, nbrw, sorw, name, ctx, queue):
36
+
37
+ labels = np.unique(slices)
38
+ slicesChunk = _extract_slices(slices, indices, indices_child)
39
+ labelsChunk = np.unique(slicesChunk)
40
+
41
+ # remove negative labels from list
42
+ index = np.argwhere(labels<0)
43
+ labels = np.delete(labels, index)
44
+ index = np.argwhere(labelsChunk<0)
45
+ labelsChunk = np.delete(labelsChunk, index)
46
+
47
+ walkmapChunk = _walk_on_current_gpu(data, slicesChunk, labelsChunk, indices_child, nbrw, sorw, name)
48
+
49
+ if walkmapChunk.shape[0] != len(labels):
50
+ walkmap = np.zeros((len(labels),)+data.shape, dtype=np.float32)
51
+ chunk2Walkmap = np.nonzero(np.in1d(labels, labelsChunk))[0]
52
+ for chunkIndex, walkmapIndex in enumerate(chunk2Walkmap):
53
+ walkmap[walkmapIndex] += walkmapChunk[chunkIndex]
54
+ else:
55
+ walkmap = walkmapChunk
56
+
57
+ return walkmap
58
+
59
+ def _extract_slices(slices, indices, indicesChunk):
60
+ extracted = np.zeros((0, slices.shape[1], slices.shape[2]), dtype=np.int32)
61
+ slicesIndicesToExtract = np.nonzero(np.in1d(indices, indicesChunk))[0]
62
+ for arraySliceIndex in slicesIndicesToExtract:
63
+ extracted = np.append(extracted, [slices[arraySliceIndex]], axis=0)
64
+ return extracted
65
+
66
+ def _walk_on_current_gpu(raw, slices, allLabels, indices, nbrw, sorw, name):
67
+
68
+ if raw.dtype == 'uint8':
69
+ kernel = _build_kernel_int8()
70
+ raw = (raw-128).astype('int8')
71
+ else:
72
+ kernel = _build_kernel_float32()
73
+ raw = raw.astype(np.float32)
74
+
75
+ fill_gpu = _build_kernel_fill()
76
+
77
+ walkmap = np.zeros((len(allLabels),)+raw.shape, dtype=np.float32)
78
+ zsh, ysh, xsh = raw.shape
79
+ slshape = slices.shape[0]
80
+
81
+ xsh_gpu = np.int32(xsh)
82
+ ysh_gpu = np.int32(ysh)
83
+ zsh_gpu = np.int32(zsh)
84
+ sorw = np.int32(sorw)
85
+ nbrw = np.int32(nbrw)
86
+ indices = np.array(indices, dtype=np.int32)
87
+ slices = np.array(slices, dtype=np.int32)
88
+
89
+ raw_gpu = gpuarray.to_gpu(raw)
90
+ indices_gpu = gpuarray.to_gpu(indices)
91
+ slices_gpu = gpuarray.to_gpu(slices)
92
+
93
+ a = np.empty(raw.shape, dtype=np.float32)
94
+ a_gpu = cuda.mem_alloc(a.nbytes)
95
+
96
+ block = (32, 32, 1)
97
+ x_grid = (xsh // 32) + 1
98
+ y_grid = (ysh // 32) + 1
99
+ grid = (int(x_grid), int(y_grid), int(slshape))
100
+ grid2 = (int(x_grid), int(y_grid), int(zsh))
101
+
102
+ for label_counter, segment in enumerate(allLabels):
103
+ print('%s:' %(name) + ' ' + str(label_counter+1) + '/' + str(len(allLabels)))
104
+ segment_gpu = np.int32(segment)
105
+ fill_gpu(a_gpu, xsh_gpu, ysh_gpu, block=block, grid=grid2)
106
+ kernel(segment_gpu, raw_gpu, slices_gpu, a_gpu, xsh_gpu, ysh_gpu, zsh_gpu, indices_gpu, sorw, nbrw, block=block, grid=grid)
107
+ cuda.memcpy_dtoh(a, a_gpu)
108
+ walkmap[label_counter] += a
109
+ return walkmap
110
+
111
+ def _build_kernel_int8():
112
+ code = """
113
+
114
+ __device__ float _calc_var(int position, int index, float B, float *raw, int segment, int *labels, int xsh) {
115
+ float dev = 0;
116
+ float summe = 0;
117
+ for (int n = -1; n < 2; n++) {
118
+ for (int o = -1; o < 2; o++) {
119
+ if (labels[index + n*xsh + o] == segment) {
120
+ float tmp = B - (float)(*((char*)(raw) + position + n*xsh + o));
121
+ dev += tmp * tmp;
122
+ summe += 1;
123
+ }
124
+ }
125
+ }
126
+ float var = dev / summe;
127
+ if (var < 1.0) {
128
+ var = 1.0;
129
+ }
130
+ return var;
131
+ }
132
+
133
+ __device__ float weight(float B, float *raw, float div1, int position) {
134
+ float tmp = B - (float)(*((char*)(raw) + position));
135
+ return exp( - tmp * tmp * div1 );
136
+ }
137
+
138
+ __global__ void Funktion(int segment, float *raw, int *slices, float *a, int xsh, int ysh, int zsh, int *indices, int sorw, int nbrw) {
139
+
140
+ int flat = xsh * ysh;
141
+ int column = blockIdx.x * blockDim.x + threadIdx.x;
142
+ int row = blockIdx.y * blockDim.y + threadIdx.y;
143
+ int slice = blockIdx.z;
144
+ int plane = indices[slice];
145
+ int index = slice * flat + row * xsh + column;
146
+
147
+ if (index < gridDim.z*flat && plane>0 && row>0 && column>0 && plane<zsh-1 && row<ysh-1 && column<xsh-1) {
148
+
149
+ if (slices[index]==segment) {
150
+
151
+ float rand;
152
+ float W0,W1,W2,W3,W4,W5;
153
+ int n,o,p;
154
+
155
+ /* Initialize MRG32k3a */
156
+ float norm = 2.328306549295728e-10;
157
+ float m1 = 4294967087.0;
158
+ float m2 = 4294944443.0;
159
+ float a12 = 1403580.0;
160
+ float a13n = 810728.0;
161
+ float a21 = 527612.0;
162
+ float a23n = 1370589.0;
163
+ long k1;
164
+ float p1, p2;
165
+ float s10 = index, s11 = index, s12 = index, s20 = index, s21 = index, s22 = index;
166
+
167
+ /* Compute standard deviation */
168
+ int position = plane*flat + row*xsh + column;
169
+ float B = (float)(*((char*)(raw) + position));
170
+ float var = _calc_var(position, index, B, raw, segment, slices, xsh);
171
+ float div1 = 1 / (2 * var);
172
+
173
+ int k = plane;
174
+ int l = row;
175
+ int m = column;
176
+
177
+ int step = 0;
178
+ int n_rw = 0;
179
+
180
+ /* Compute random walks */
181
+ while (n_rw < nbrw) {
182
+
183
+ /* Compute weights */
184
+ W0 = weight(B, raw, div1, position + flat);
185
+ W1 = weight(B, raw, div1, position - flat);
186
+ W2 = weight(B, raw, div1, position + xsh);
187
+ W3 = weight(B, raw, div1, position - xsh);
188
+ W4 = weight(B, raw, div1, position + 1);
189
+ W5 = weight(B, raw, div1, position - 1);
190
+
191
+ W1 += W0;
192
+ W2 += W1;
193
+ W3 += W2;
194
+ W4 += W3;
195
+ W5 += W4;
196
+
197
+ /* Compute random numbers with MRG32k3a */
198
+
199
+ /* Component 1 */
200
+ p1 = a12 * s11 - a13n * s10;
201
+ k1 = p1 / m1;
202
+ p1 -= k1 * m1;
203
+ if (p1 < 0.0){
204
+ p1 += m1;}
205
+ s10 = s11;
206
+ s11 = s12;
207
+ s12 = p1;
208
+
209
+ /* Component 2 */
210
+ p2 = a21 * s22 - a23n * s20;
211
+ k1 = p2 / m2;
212
+ p2 -= k1 * m2;
213
+ if (p2 < 0.0){
214
+ p2 += m2;}
215
+ s20 = s21;
216
+ s21 = s22;
217
+ s22 = p2;
218
+
219
+ /* Combination */
220
+ if (p1 <= p2) {
221
+ rand = W5 * ((p1 - p2 + m1) * norm);
222
+ }
223
+ else {
224
+ rand = W5 * ((p1 - p2) * norm);
225
+ }
226
+
227
+ /* Determine new direction of random walk */
228
+ if (rand<W0 || rand==0){n=1; o=0; p=0;}
229
+ else if (rand>=W0 && rand<W1){n=-1; o=0; p=0;}
230
+ else if (rand>=W1 && rand<W2){n=0; o=1; p=0;}
231
+ else if (rand>=W2 && rand<W3){n=0; o=-1; p=0;}
232
+ else if (rand>=W3 && rand<W4){n=0; o=0; p=1;}
233
+ else if (rand>=W4 && rand<=W5){n=0; o=0; p=-1;}
234
+
235
+ /* Move in new direction */
236
+ if (k+n>0 && k+n<zsh-1 && l+o>0 && l+o<ysh-1 && m+p>0 && m+p<xsh-1) {
237
+ k += n;
238
+ l += o;
239
+ m += p;
240
+ position = k*flat + l*xsh + m;
241
+ atomicAdd(&a[position], 1);
242
+ }
243
+
244
+ step += 1;
245
+
246
+ if (step==sorw) {
247
+ k = plane;
248
+ l = row;
249
+ m = column;
250
+ position = k*flat + l*xsh + m;
251
+ n_rw += 1;
252
+ step = 0;
253
+ }
254
+ }
255
+ }
256
+ }
257
+ }
258
+ """
259
+ mod = SourceModule(code)
260
+ kernel = mod.get_function("Funktion")
261
+ return kernel
262
+
263
+ def _build_kernel_float32():
264
+ code = """
265
+
266
+ __device__ float _calc_var(int position, int index, float B, float *raw, int segment, int *labels, int xsh) {
267
+ float dev = 0;
268
+ float summe = 0;
269
+ for (int n = -1; n < 2; n++) {
270
+ for (int o = -1; o < 2; o++) {
271
+ if (labels[index + n*xsh + o] == segment) {
272
+ float tmp = B - raw[position + n*xsh + o];
273
+ dev += tmp * tmp;
274
+ summe += 1;
275
+ }
276
+ }
277
+ }
278
+ float var = dev / summe;
279
+ if (var < 1.0) {
280
+ var = 1.0;
281
+ }
282
+ return var;
283
+ }
284
+
285
+ __device__ float weight(float B, float A, float div1) {
286
+ float tmp = B - A;
287
+ return exp( - tmp * tmp * div1 );
288
+ }
289
+
290
+ __global__ void Funktion(int segment, float *raw, int *slices, float *a, int xsh, int ysh, int zsh, int *indices, int sorw, int nbrw) {
291
+
292
+ int flat = xsh * ysh;
293
+ int column = blockIdx.x * blockDim.x + threadIdx.x;
294
+ int row = blockIdx.y * blockDim.y + threadIdx.y;
295
+ int slice = blockIdx.z;
296
+ int plane = indices[slice];
297
+ int index = slice * flat + row * xsh + column;
298
+
299
+ if (index < gridDim.z*flat && plane>0 && row>0 && column>0 && plane<zsh-1 && row<ysh-1 && column<xsh-1) {
300
+
301
+ if (slices[index]==segment) {
302
+
303
+ float rand;
304
+ float W0,W1,W2,W3,W4,W5;
305
+ int n,o,p;
306
+
307
+ /* Initialize MRG32k3a */
308
+ float norm = 2.328306549295728e-10;
309
+ float m1 = 4294967087.0;
310
+ float m2 = 4294944443.0;
311
+ float a12 = 1403580.0;
312
+ float a13n = 810728.0;
313
+ float a21 = 527612.0;
314
+ float a23n = 1370589.0;
315
+ long k1;
316
+ float p1, p2;
317
+ float s10 = index, s11 = index, s12 = index, s20 = index, s21 = index, s22 = index;
318
+
319
+ /* Compute standard deviation */
320
+ int position = plane*flat + row*xsh + column;
321
+ float B = raw[position];
322
+ float var = _calc_var(position, index, B, raw, segment, slices, xsh);
323
+ float div1 = 1 / (2 * var);
324
+
325
+ int k = plane;
326
+ int l = row;
327
+ int m = column;
328
+
329
+ int step = 0;
330
+ int n_rw = 0;
331
+
332
+ /* Compute random walks */
333
+ while (n_rw < nbrw) {
334
+
335
+ /* Compute weights */
336
+ W0 = weight(B, raw[position + flat], div1);
337
+ W1 = weight(B, raw[position - flat], div1);
338
+ W2 = weight(B, raw[position + xsh], div1);
339
+ W3 = weight(B, raw[position - xsh], div1);
340
+ W4 = weight(B, raw[position + 1], div1);
341
+ W5 = weight(B, raw[position - 1], div1);
342
+
343
+ W1 += W0;
344
+ W2 += W1;
345
+ W3 += W2;
346
+ W4 += W3;
347
+ W5 += W4;
348
+
349
+ /* Compute random numbers with MRG32k3a */
350
+
351
+ /* Component 1 */
352
+ p1 = a12 * s11 - a13n * s10;
353
+ k1 = p1 / m1;
354
+ p1 -= k1 * m1;
355
+ if (p1 < 0.0){
356
+ p1 += m1;}
357
+ s10 = s11;
358
+ s11 = s12;
359
+ s12 = p1;
360
+
361
+ /* Component 2 */
362
+ p2 = a21 * s22 - a23n * s20;
363
+ k1 = p2 / m2;
364
+ p2 -= k1 * m2;
365
+ if (p2 < 0.0){
366
+ p2 += m2;}
367
+ s20 = s21;
368
+ s21 = s22;
369
+ s22 = p2;
370
+
371
+ /* Combination */
372
+ if (p1 <= p2) {
373
+ rand = W5 * ((p1 - p2 + m1) * norm);
374
+ }
375
+ else {
376
+ rand = W5 * ((p1 - p2) * norm);
377
+ }
378
+
379
+ /* Determine new direction of random walk */
380
+ if (rand<W0 || rand==0){n=1; o=0; p=0;}
381
+ else if (rand>=W0 && rand<W1){n=-1; o=0; p=0;}
382
+ else if (rand>=W1 && rand<W2){n=0; o=1; p=0;}
383
+ else if (rand>=W2 && rand<W3){n=0; o=-1; p=0;}
384
+ else if (rand>=W3 && rand<W4){n=0; o=0; p=1;}
385
+ else if (rand>=W4 && rand<=W5){n=0; o=0; p=-1;}
386
+
387
+ /* Move in new direction */
388
+ if (k+n>0 && k+n<zsh-1 && l+o>0 && l+o<ysh-1 && m+p>0 && m+p<xsh-1) {
389
+ k += n;
390
+ l += o;
391
+ m += p;
392
+ position = k*flat + l*xsh + m;
393
+ atomicAdd(&a[position], 1);
394
+ }
395
+
396
+ step += 1;
397
+
398
+ if (step==sorw) {
399
+ k = plane;
400
+ l = row;
401
+ m = column;
402
+ position = k*flat + l*xsh + m;
403
+ n_rw += 1;
404
+ step = 0;
405
+ }
406
+ }
407
+ }
408
+ }
409
+ }
410
+ """
411
+ mod = SourceModule(code)
412
+ kernel = mod.get_function("Funktion")
413
+ return kernel
414
+