biomedisa 24.5.23__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 +49 -0
  2. biomedisa/__main__.py +18 -0
  3. biomedisa/deeplearning.py +529 -0
  4. biomedisa/features/DataGenerator.py +299 -0
  5. biomedisa/features/DataGeneratorCrop.py +121 -0
  6. biomedisa/features/PredictDataGenerator.py +87 -0
  7. biomedisa/features/PredictDataGeneratorCrop.py +74 -0
  8. biomedisa/features/__init__.py +0 -0
  9. biomedisa/features/active_contour.py +430 -0
  10. biomedisa/features/amira_to_np/__init__.py +0 -0
  11. biomedisa/features/amira_to_np/amira_data_stream.py +980 -0
  12. biomedisa/features/amira_to_np/amira_grammar.py +369 -0
  13. biomedisa/features/amira_to_np/amira_header.py +290 -0
  14. biomedisa/features/amira_to_np/amira_helper.py +72 -0
  15. biomedisa/features/assd.py +167 -0
  16. biomedisa/features/biomedisa_helper.py +842 -0
  17. biomedisa/features/create_slices.py +277 -0
  18. biomedisa/features/crop_helper.py +581 -0
  19. biomedisa/features/curvop_numba.py +149 -0
  20. biomedisa/features/django_env.py +171 -0
  21. biomedisa/features/keras_helper.py +1195 -0
  22. biomedisa/features/nc_reader.py +179 -0
  23. biomedisa/features/pid.py +52 -0
  24. biomedisa/features/process_image.py +251 -0
  25. biomedisa/features/pycuda_test.py +85 -0
  26. biomedisa/features/random_walk/__init__.py +0 -0
  27. biomedisa/features/random_walk/gpu_kernels.py +184 -0
  28. biomedisa/features/random_walk/pycuda_large.py +826 -0
  29. biomedisa/features/random_walk/pycuda_large_allx.py +806 -0
  30. biomedisa/features/random_walk/pycuda_small.py +414 -0
  31. biomedisa/features/random_walk/pycuda_small_allx.py +493 -0
  32. biomedisa/features/random_walk/pyopencl_large.py +760 -0
  33. biomedisa/features/random_walk/pyopencl_small.py +441 -0
  34. biomedisa/features/random_walk/rw_large.py +389 -0
  35. biomedisa/features/random_walk/rw_small.py +307 -0
  36. biomedisa/features/remove_outlier.py +396 -0
  37. biomedisa/features/split_volume.py +167 -0
  38. biomedisa/interpolation.py +369 -0
  39. biomedisa/mesh.py +403 -0
  40. biomedisa-24.5.23.dist-info/LICENSE +191 -0
  41. biomedisa-24.5.23.dist-info/METADATA +261 -0
  42. biomedisa-24.5.23.dist-info/RECORD +44 -0
  43. biomedisa-24.5.23.dist-info/WHEEL +5 -0
  44. biomedisa-24.5.23.dist-info/top_level.txt +1 -0
@@ -0,0 +1,277 @@
1
+ #!/usr/bin/python3
2
+ ##########################################################################
3
+ ## ##
4
+ ## Copyright (c) 2019-2024 Philipp Lösel. All rights reserved. ##
5
+ ## ##
6
+ ## This file is part of the open source project biomedisa. ##
7
+ ## ##
8
+ ## Licensed under the European Union Public Licence (EUPL) ##
9
+ ## v1.2, or - as soon as they will be approved by the ##
10
+ ## European Commission - subsequent versions of the EUPL; ##
11
+ ## ##
12
+ ## You may redistribute it and/or modify it under the terms ##
13
+ ## of the EUPL v1.2. You may not use this work except in ##
14
+ ## compliance with this Licence. ##
15
+ ## ##
16
+ ## You can obtain a copy of the Licence at: ##
17
+ ## ##
18
+ ## https://joinup.ec.europa.eu/page/eupl-text-11-12 ##
19
+ ## ##
20
+ ## Unless required by applicable law or agreed to in ##
21
+ ## writing, software distributed under the Licence is ##
22
+ ## distributed on an "AS IS" basis, WITHOUT WARRANTIES ##
23
+ ## OR CONDITIONS OF ANY KIND, either express or implied. ##
24
+ ## ##
25
+ ## See the Licence for the specific language governing ##
26
+ ## permissions and limitations under the Licence. ##
27
+ ## ##
28
+ ##########################################################################
29
+
30
+ import sys, os
31
+ from biomedisa.features.biomedisa_helper import load_data, color_to_gray, img_to_uint8, img_resize, id_generator
32
+ from PIL import Image
33
+ import numpy as np
34
+ from glob import glob
35
+ import numba
36
+ import shutil
37
+ import cv2
38
+ import tarfile
39
+ import tempfile
40
+
41
+ def unique(arr):
42
+ arr = arr.astype(np.uint8)
43
+ counts = np.zeros(256, dtype=int)
44
+ zsh, ysh, xsh = arr.shape
45
+ @numba.jit(nopython=True)
46
+ def __unique__(arr, zsh, ysh, xsh, counts):
47
+ for k in range(zsh):
48
+ for l in range(ysh):
49
+ for m in range(xsh):
50
+ index = arr[k,l,m]
51
+ counts[index] += 1
52
+ return counts
53
+ counts = __unique__(arr, zsh, ysh, xsh, counts)
54
+ labels = np.where(counts)[0]
55
+ counts = counts[labels]
56
+ return labels, counts
57
+
58
+ def percentile(a, limit):
59
+ values, counts = unique(a)
60
+ total_positive = np.sum(counts[1:])
61
+ limit = total_positive / 100.0 * limit
62
+ k, tmp = 1, 0
63
+ while tmp < limit:
64
+ tmp += counts[k]
65
+ k += 1
66
+ low = values[k-1]
67
+ limit = total_positive - limit
68
+ k, tmp = -1, total_positive
69
+ while tmp > limit:
70
+ tmp -= counts[k]
71
+ k -= 1
72
+ high = values[k+1]
73
+ return low, high
74
+
75
+ def contrast(arr):
76
+ bottom_quantile, top_quantile = percentile(arr, 0.5)
77
+ arr[arr<bottom_quantile] = bottom_quantile
78
+ arr[arr>top_quantile] = top_quantile
79
+ values, counts = unique(arr)
80
+ mean = np.sum(values * counts) / np.sum(counts)
81
+ std = np.sqrt(np.sum(counts * (values - mean)**2) / (np.sum(counts) - 1))
82
+ arr = arr.astype(np.float16)
83
+ arr = (arr - mean) / std
84
+ amin = (bottom_quantile - mean) / std
85
+ arr -= amin
86
+ arr /= (top_quantile - mean) / std - amin
87
+ arr *= 255.0
88
+ arr = arr.astype(np.uint8)
89
+ return arr
90
+
91
+ def create_slices(path_to_data, path_to_label, on_site=False):
92
+
93
+ try:
94
+ if on_site:
95
+ path_to_slices = os.path.dirname(path_to_data) + '/slices.' + os.path.splitext(os.path.basename(path_to_data))[0]
96
+ if os.path.isdir(path_to_slices):
97
+ shutil.rmtree(path_to_slices)
98
+ if path_to_label:
99
+ path_to_label_slices = os.path.dirname(path_to_label) + '/slices.' + os.path.splitext(os.path.basename(path_to_label))[0]
100
+ if os.path.isdir(path_to_label_slices):
101
+ shutil.rmtree(path_to_label_slices)
102
+ else:
103
+ path_to_slices = path_to_data.replace('images', 'sliceviewer', 1)
104
+ if path_to_label:
105
+ path_to_label_slices = path_to_label.replace('images', 'sliceviewer', 1)
106
+
107
+ if not os.path.isdir(path_to_slices) or (path_to_label and not os.path.isdir(path_to_label_slices)):
108
+
109
+ # load data and reduce data size
110
+ path_to_dir, extension = os.path.splitext(path_to_data)
111
+ if extension == '.gz':
112
+ extension = os.path.splitext(path_to_dir)[1]
113
+ if extension == '.tar' or os.path.isdir(path_to_data):
114
+ with tempfile.TemporaryDirectory() as temp_dir:
115
+ # extract files
116
+ if extension == '.tar':
117
+ tar = tarfile.open(path_to_data)
118
+ tar.extractall(path=temp_dir)
119
+ tar.close()
120
+ path_to_data = temp_dir
121
+ # load files
122
+ img_names = []
123
+ for data_type in ['.tif','.tiff','.am','.hdr','.mhd','.mha','.nrrd','.nii','.nii.gz','.zip','.mrc']:
124
+ img_names.extend(glob(path_to_data+'/**/*'+data_type, recursive=True))
125
+ img_names = sorted(img_names)
126
+ raw = None
127
+ for name in img_names:
128
+ arr, _ = load_data(name, 'create_slices')
129
+ if arr is not None and raw is None:
130
+ zsh, ysh, xsh = arr.shape
131
+ scale = float(256) / float(max(zsh, ysh, xsh))
132
+ z_scale = int(zsh * scale)
133
+ y_scale = int(ysh * scale)
134
+ x_scale = int(xsh * scale)
135
+ raw = img_resize(arr, z_scale, y_scale, x_scale)
136
+ elif arr is not None:
137
+ arr = img_resize(arr, z_scale, y_scale, x_scale)
138
+ raw = np.append(raw, arr, axis=0)
139
+ else:
140
+ raw, _ = load_data(path_to_data, 'create_slices')
141
+ zsh, ysh, xsh = raw.shape
142
+ if min(ysh, xsh) > 400 and not on_site:
143
+ scale = float(400) / float(min(ysh, xsh))
144
+ ysh = int(ysh * scale)
145
+ xsh = int(xsh * scale)
146
+ tmp = np.empty((zsh, ysh, xsh), dtype=raw.dtype)
147
+ for k in range(zsh):
148
+ tmp[k] = cv2.resize(raw[k], (xsh, ysh), interpolation=cv2.INTER_AREA)
149
+ raw = tmp
150
+
151
+ # increase contrast
152
+ raw = img_to_uint8(raw)
153
+ raw = contrast(raw)
154
+
155
+ # create slices for slice viewer
156
+ if not os.path.isdir(path_to_slices):
157
+
158
+ # make directory
159
+ os.makedirs(path_to_slices)
160
+ os.chmod(path_to_slices, 0o770)
161
+
162
+ # save slices
163
+ for k in range(raw.shape[0]):
164
+ im = Image.fromarray(raw[k])
165
+ im.save(path_to_slices + f'/{k}.png')
166
+
167
+ if path_to_label and not os.path.isdir(path_to_label_slices):
168
+
169
+ # load data
170
+ path_to_dir, extension = os.path.splitext(path_to_label)
171
+ if extension == '.gz':
172
+ extension = os.path.splitext(path_to_dir)[1]
173
+ if extension == '.tar' or os.path.isdir(path_to_label):
174
+ with tempfile.TemporaryDirectory() as temp_dir:
175
+ # extract files
176
+ if extension == '.tar':
177
+ tar = tarfile.open(path_to_label)
178
+ tar.extractall(path=temp_dir)
179
+ tar.close()
180
+ path_to_label = temp_dir
181
+ # load files
182
+ img_names = []
183
+ for data_type in ['.tif','.tiff','.am','.hdr','.mhd','.mha','.nrrd','.nii','.nii.gz','.zip','.mrc']:
184
+ img_names.extend(glob(path_to_label+'/**/*'+data_type, recursive=True))
185
+ img_names = sorted(img_names)
186
+ # load and scale label data to corresponding img data
187
+ mask = np.zeros((0, y_scale, x_scale), dtype=np.uint8)
188
+ for name in img_names:
189
+ arr, _ = load_data(name, 'create_slices')
190
+ if arr is not None:
191
+ arr = color_to_gray(arr)
192
+ arr = arr.astype(np.uint8)
193
+ arr = img_resize(arr, z_scale, y_scale, x_scale, labels=True)
194
+ mask = np.append(mask, arr, axis=0)
195
+ else:
196
+ mask, _ = load_data(path_to_label, 'create_slices')
197
+ mask = color_to_gray(mask)
198
+ mask = mask.astype(np.uint8)
199
+ zsh, ysh, xsh = mask.shape
200
+ if min(ysh, xsh) > 400 and not on_site:
201
+ scale = float(400) / float(min(ysh, xsh))
202
+ ysh = int(ysh * scale)
203
+ xsh = int(xsh * scale)
204
+ tmp = np.zeros((zsh, ysh, xsh), dtype=mask.dtype)
205
+ for k in range(zsh):
206
+ if np.any(mask[k]):
207
+ tmp[k] = cv2.resize(mask[k], (xsh, ysh), interpolation=cv2.INTER_NEAREST)
208
+ mask = tmp
209
+
210
+ # create slices for slice viewer
211
+ if mask.shape==raw.shape:
212
+
213
+ # make directory
214
+ os.makedirs(path_to_label_slices)
215
+ os.chmod(path_to_label_slices, 0o770)
216
+
217
+ # define colors
218
+ Color = [(255,0,0),(255,255,0),(0,0,255),(0,100,0),(0,255,0),(255,165,0),(139,0,0),(255,20,147),(255,105,180),(255,0,0),(139,0,139),(255,0,255),(160,32,240),(184,134,11),(255,185,15),(255,215,0),(0,191,255),(16,78,139),(104,131,139),(255,64,64),(165,42,42),(255,127,36),(139,90,43),(110,139,61),(0,255,127),(255,127,80),(139,10,80),(219,112,147),(178,34,34),(255,48,48),(205,79,57),(160,32,240),(255,100,0)] * 8
219
+ labels, _ = unique(mask)
220
+ labels = labels[1:]
221
+ Color = Color[:len(labels)]
222
+ Color = np.array(Color, dtype=np.uint8)
223
+
224
+ # allocate memory
225
+ zsh, ysh, xsh = raw.shape
226
+ if extension == '.tar' or os.path.isdir(path_to_label):
227
+ x_shape = 2*xsh
228
+ else:
229
+ x_shape = xsh
230
+ out = np.empty((ysh, x_shape, 3), dtype=np.uint8)
231
+ gradient = np.empty((ysh, xsh), dtype=np.uint8)
232
+
233
+ # save preview slice by slice
234
+ for k in range(zsh):
235
+ raw_tmp = raw[k]
236
+ mask_tmp = mask[k]
237
+ out.fill(0)
238
+
239
+ # create output slice
240
+ for l in range(3):
241
+ out[:,:xsh,l] = raw_tmp
242
+
243
+ if np.any(mask_tmp):
244
+
245
+ # compute gradient
246
+ gradient.fill(0)
247
+ tmp = np.abs(mask_tmp[:-1] - mask_tmp[1:])
248
+ tmp[tmp>0] = 1
249
+ gradient[:-1] += tmp
250
+ gradient[1:] += tmp
251
+ tmp = np.abs(mask_tmp[:,:-1] - mask_tmp[:,1:])
252
+ tmp[tmp>0] = 1
253
+ gradient[:,:-1] += tmp
254
+ gradient[:,1:] += tmp
255
+
256
+ # colorize
257
+ for j, label in enumerate(labels):
258
+ C = Color[j]
259
+ tmp = np.logical_and(gradient>0, mask_tmp==label)
260
+ out[:,:xsh][tmp] = C
261
+ if extension == '.tar' or os.path.isdir(path_to_label):
262
+ out[:,xsh:][mask_tmp==label] = C
263
+
264
+ # save slice
265
+ im = Image.fromarray(out)
266
+ im.save(path_to_label_slices + f'/{k}.png')
267
+
268
+ except Exception as e:
269
+ print(e)
270
+
271
+ if __name__ == "__main__":
272
+ if len(sys.argv)==2:
273
+ path_to_labels = None
274
+ else:
275
+ path_to_labels = sys.argv[2]
276
+ create_slices(sys.argv[1], path_to_labels, True)
277
+