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.
- biomedisa/__init__.py +53 -0
- biomedisa/__main__.py +18 -0
- biomedisa/biomedisa_features/DataGenerator.py +299 -0
- biomedisa/biomedisa_features/DataGeneratorCrop.py +121 -0
- biomedisa/biomedisa_features/PredictDataGenerator.py +87 -0
- biomedisa/biomedisa_features/PredictDataGeneratorCrop.py +74 -0
- biomedisa/biomedisa_features/__init__.py +0 -0
- biomedisa/biomedisa_features/active_contour.py +434 -0
- biomedisa/biomedisa_features/amira_to_np/__init__.py +0 -0
- biomedisa/biomedisa_features/amira_to_np/amira_data_stream.py +980 -0
- biomedisa/biomedisa_features/amira_to_np/amira_grammar.py +369 -0
- biomedisa/biomedisa_features/amira_to_np/amira_header.py +290 -0
- biomedisa/biomedisa_features/amira_to_np/amira_helper.py +72 -0
- biomedisa/biomedisa_features/assd.py +167 -0
- biomedisa/biomedisa_features/biomedisa_helper.py +801 -0
- biomedisa/biomedisa_features/create_slices.py +286 -0
- biomedisa/biomedisa_features/crop_helper.py +586 -0
- biomedisa/biomedisa_features/curvop_numba.py +149 -0
- biomedisa/biomedisa_features/django_env.py +172 -0
- biomedisa/biomedisa_features/keras_helper.py +1219 -0
- biomedisa/biomedisa_features/nc_reader.py +179 -0
- biomedisa/biomedisa_features/pid.py +52 -0
- biomedisa/biomedisa_features/process_image.py +253 -0
- biomedisa/biomedisa_features/pycuda_test.py +84 -0
- biomedisa/biomedisa_features/random_walk/__init__.py +0 -0
- biomedisa/biomedisa_features/random_walk/gpu_kernels.py +183 -0
- biomedisa/biomedisa_features/random_walk/pycuda_large.py +826 -0
- biomedisa/biomedisa_features/random_walk/pycuda_large_allx.py +806 -0
- biomedisa/biomedisa_features/random_walk/pycuda_small.py +414 -0
- biomedisa/biomedisa_features/random_walk/pycuda_small_allx.py +493 -0
- biomedisa/biomedisa_features/random_walk/pyopencl_large.py +760 -0
- biomedisa/biomedisa_features/random_walk/pyopencl_small.py +441 -0
- biomedisa/biomedisa_features/random_walk/rw_large.py +390 -0
- biomedisa/biomedisa_features/random_walk/rw_small.py +310 -0
- biomedisa/biomedisa_features/remove_outlier.py +399 -0
- biomedisa/biomedisa_features/split_volume.py +274 -0
- biomedisa/deeplearning.py +519 -0
- biomedisa/interpolation.py +371 -0
- biomedisa/mesh.py +406 -0
- biomedisa-2024.5.14.dist-info/LICENSE +191 -0
- biomedisa-2024.5.14.dist-info/METADATA +306 -0
- biomedisa-2024.5.14.dist-info/RECORD +44 -0
- biomedisa-2024.5.14.dist-info/WHEEL +5 -0
- biomedisa-2024.5.14.dist-info/top_level.txt +1 -0
@@ -0,0 +1,286 @@
|
|
1
|
+
#!/usr/bin/python3
|
2
|
+
##########################################################################
|
3
|
+
## ##
|
4
|
+
## Copyright (c) 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
|
+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
32
|
+
sys.path.append(BASE_DIR)
|
33
|
+
from biomedisa_features.biomedisa_helper import load_data, color_to_gray, img_to_uint8, img_resize, id_generator
|
34
|
+
from PIL import Image
|
35
|
+
import numpy as np
|
36
|
+
from glob import glob
|
37
|
+
import numba
|
38
|
+
import shutil
|
39
|
+
import cv2
|
40
|
+
import tarfile
|
41
|
+
|
42
|
+
def unique(arr):
|
43
|
+
arr = arr.astype(np.uint8)
|
44
|
+
counts = np.zeros(256, dtype=int)
|
45
|
+
zsh, ysh, xsh = arr.shape
|
46
|
+
@numba.jit(nopython=True)
|
47
|
+
def __unique__(arr, zsh, ysh, xsh, counts):
|
48
|
+
for k in range(zsh):
|
49
|
+
for l in range(ysh):
|
50
|
+
for m in range(xsh):
|
51
|
+
index = arr[k,l,m]
|
52
|
+
counts[index] += 1
|
53
|
+
return counts
|
54
|
+
counts = __unique__(arr, zsh, ysh, xsh, counts)
|
55
|
+
labels = np.where(counts)[0]
|
56
|
+
counts = counts[labels]
|
57
|
+
return labels, counts
|
58
|
+
|
59
|
+
def percentile(a, limit):
|
60
|
+
values, counts = unique(a)
|
61
|
+
total_positive = np.sum(counts[1:])
|
62
|
+
limit = total_positive / 100.0 * limit
|
63
|
+
k, tmp = 1, 0
|
64
|
+
while tmp < limit:
|
65
|
+
tmp += counts[k]
|
66
|
+
k += 1
|
67
|
+
low = values[k-1]
|
68
|
+
limit = total_positive - limit
|
69
|
+
k, tmp = -1, total_positive
|
70
|
+
while tmp > limit:
|
71
|
+
tmp -= counts[k]
|
72
|
+
k -= 1
|
73
|
+
high = values[k+1]
|
74
|
+
return low, high
|
75
|
+
|
76
|
+
def contrast(arr):
|
77
|
+
bottom_quantile, top_quantile = percentile(arr, 0.5)
|
78
|
+
arr[arr<bottom_quantile] = bottom_quantile
|
79
|
+
arr[arr>top_quantile] = top_quantile
|
80
|
+
values, counts = unique(arr)
|
81
|
+
mean = np.sum(values * counts) / np.sum(counts)
|
82
|
+
std = np.sqrt(np.sum(counts * (values - mean)**2) / (np.sum(counts) - 1))
|
83
|
+
arr = arr.astype(np.float16)
|
84
|
+
arr = (arr - mean) / std
|
85
|
+
amin = (bottom_quantile - mean) / std
|
86
|
+
arr -= amin
|
87
|
+
arr /= (top_quantile - mean) / std - amin
|
88
|
+
arr *= 255.0
|
89
|
+
arr = arr.astype(np.uint8)
|
90
|
+
return arr
|
91
|
+
|
92
|
+
def create_slices(path_to_data, path_to_label, on_site=False):
|
93
|
+
|
94
|
+
try:
|
95
|
+
if on_site:
|
96
|
+
path_to_slices = os.path.dirname(path_to_data) + '/slices.' + os.path.splitext(os.path.basename(path_to_data))[0]
|
97
|
+
if os.path.isdir(path_to_slices):
|
98
|
+
shutil.rmtree(path_to_slices)
|
99
|
+
if path_to_label:
|
100
|
+
path_to_label_slices = os.path.dirname(path_to_label) + '/slices.' + os.path.splitext(os.path.basename(path_to_label))[0]
|
101
|
+
if os.path.isdir(path_to_label_slices):
|
102
|
+
shutil.rmtree(path_to_label_slices)
|
103
|
+
else:
|
104
|
+
path_to_slices = path_to_data.replace('images', 'sliceviewer', 1)
|
105
|
+
if path_to_label:
|
106
|
+
path_to_label_slices = path_to_label.replace('images', 'sliceviewer', 1)
|
107
|
+
|
108
|
+
if not os.path.isdir(path_to_slices) or (path_to_label and not os.path.isdir(path_to_label_slices)):
|
109
|
+
|
110
|
+
# load data and reduce data size
|
111
|
+
path_to_dir, extension = os.path.splitext(path_to_data)
|
112
|
+
if extension == '.gz':
|
113
|
+
path_to_dir, extension = os.path.splitext(path_to_dir)
|
114
|
+
if extension == '.tar' or os.path.isdir(path_to_data):
|
115
|
+
# extract files
|
116
|
+
if extension == '.tar':
|
117
|
+
path_to_dir = BASE_DIR + '/tmp/' + id_generator(40)
|
118
|
+
tar = tarfile.open(path_to_data)
|
119
|
+
tar.extractall(path=path_to_dir)
|
120
|
+
tar.close()
|
121
|
+
else:
|
122
|
+
path_to_dir = path_to_data
|
123
|
+
# load files
|
124
|
+
img_names = []
|
125
|
+
for data_type in ['.tif','.tiff','.am','.hdr','.mhd','.mha','.nrrd','.nii','.nii.gz','.zip','.mrc']:
|
126
|
+
img_names.extend(glob(path_to_dir+'/**/*'+data_type, recursive=True))
|
127
|
+
img_names = sorted(img_names)
|
128
|
+
raw = None
|
129
|
+
for name in img_names:
|
130
|
+
arr, _ = load_data(name, 'create_slices')
|
131
|
+
if arr is not None and raw is None:
|
132
|
+
zsh, ysh, xsh = arr.shape
|
133
|
+
scale = float(256) / float(max(zsh, ysh, xsh))
|
134
|
+
z_scale = int(zsh * scale)
|
135
|
+
y_scale = int(ysh * scale)
|
136
|
+
x_scale = int(xsh * scale)
|
137
|
+
raw = img_resize(arr, z_scale, y_scale, x_scale)
|
138
|
+
elif arr is not None:
|
139
|
+
arr = img_resize(arr, z_scale, y_scale, x_scale)
|
140
|
+
raw = np.append(raw, arr, axis=0)
|
141
|
+
# remove extracted files
|
142
|
+
if extension == '.tar' and os.path.isdir(path_to_dir):
|
143
|
+
shutil.rmtree(path_to_dir)
|
144
|
+
else:
|
145
|
+
raw, _ = load_data(path_to_data, 'create_slices')
|
146
|
+
zsh, ysh, xsh = raw.shape
|
147
|
+
if min(ysh, xsh) > 400 and not on_site:
|
148
|
+
scale = float(400) / float(min(ysh, xsh))
|
149
|
+
ysh = int(ysh * scale)
|
150
|
+
xsh = int(xsh * scale)
|
151
|
+
tmp = np.empty((zsh, ysh, xsh), dtype=raw.dtype)
|
152
|
+
for k in range(zsh):
|
153
|
+
tmp[k] = cv2.resize(raw[k], (xsh, ysh), interpolation=cv2.INTER_AREA)
|
154
|
+
raw = tmp
|
155
|
+
|
156
|
+
# increase contrast
|
157
|
+
raw = img_to_uint8(raw)
|
158
|
+
raw = contrast(raw)
|
159
|
+
|
160
|
+
# create slices for slice viewer
|
161
|
+
if not os.path.isdir(path_to_slices):
|
162
|
+
|
163
|
+
# make directory
|
164
|
+
os.makedirs(path_to_slices)
|
165
|
+
os.chmod(path_to_slices, 0o770)
|
166
|
+
|
167
|
+
# save slices
|
168
|
+
for k in range(raw.shape[0]):
|
169
|
+
im = Image.fromarray(raw[k])
|
170
|
+
im.save(path_to_slices + f'/{k}.png')
|
171
|
+
|
172
|
+
if path_to_label and not os.path.isdir(path_to_label_slices):
|
173
|
+
|
174
|
+
# load data
|
175
|
+
path_to_dir, extension = os.path.splitext(path_to_label)
|
176
|
+
if extension == '.gz':
|
177
|
+
path_to_dir, extension = os.path.splitext(path_to_dir)
|
178
|
+
if extension == '.tar' or os.path.isdir(path_to_label):
|
179
|
+
# extract files
|
180
|
+
if extension == '.tar':
|
181
|
+
path_to_dir = BASE_DIR + '/tmp/' + id_generator(40)
|
182
|
+
tar = tarfile.open(path_to_label)
|
183
|
+
tar.extractall(path=path_to_dir)
|
184
|
+
tar.close()
|
185
|
+
else:
|
186
|
+
path_to_dir = path_to_label
|
187
|
+
# load files
|
188
|
+
img_names = []
|
189
|
+
for data_type in ['.tif','.tiff','.am','.hdr','.mhd','.mha','.nrrd','.nii','.nii.gz','.zip','.mrc']:
|
190
|
+
img_names.extend(glob(path_to_dir+'/**/*'+data_type, recursive=True))
|
191
|
+
img_names = sorted(img_names)
|
192
|
+
# load and scale label data to corresponding img data
|
193
|
+
mask = np.zeros((0, y_scale, x_scale), dtype=np.uint8)
|
194
|
+
for name in img_names:
|
195
|
+
arr, _ = load_data(name, 'create_slices')
|
196
|
+
if arr is not None:
|
197
|
+
arr = color_to_gray(arr)
|
198
|
+
arr = arr.astype(np.uint8)
|
199
|
+
arr = img_resize(arr, z_scale, y_scale, x_scale, labels=True)
|
200
|
+
mask = np.append(mask, arr, axis=0)
|
201
|
+
# remove extracted files
|
202
|
+
if extension == '.tar' and os.path.isdir(path_to_dir):
|
203
|
+
shutil.rmtree(path_to_dir)
|
204
|
+
else:
|
205
|
+
mask, _ = load_data(path_to_label, 'create_slices')
|
206
|
+
mask = color_to_gray(mask)
|
207
|
+
mask = mask.astype(np.uint8)
|
208
|
+
zsh, ysh, xsh = mask.shape
|
209
|
+
if min(ysh, xsh) > 400 and not on_site:
|
210
|
+
scale = float(400) / float(min(ysh, xsh))
|
211
|
+
ysh = int(ysh * scale)
|
212
|
+
xsh = int(xsh * scale)
|
213
|
+
tmp = np.zeros((zsh, ysh, xsh), dtype=mask.dtype)
|
214
|
+
for k in range(zsh):
|
215
|
+
if np.any(mask[k]):
|
216
|
+
tmp[k] = cv2.resize(mask[k], (xsh, ysh), interpolation=cv2.INTER_NEAREST)
|
217
|
+
mask = tmp
|
218
|
+
|
219
|
+
# create slices for slice viewer
|
220
|
+
if mask.shape==raw.shape:
|
221
|
+
|
222
|
+
# make directory
|
223
|
+
os.makedirs(path_to_label_slices)
|
224
|
+
os.chmod(path_to_label_slices, 0o770)
|
225
|
+
|
226
|
+
# define colors
|
227
|
+
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
|
228
|
+
labels, _ = unique(mask)
|
229
|
+
labels = labels[1:]
|
230
|
+
Color = Color[:len(labels)]
|
231
|
+
Color = np.array(Color, dtype=np.uint8)
|
232
|
+
|
233
|
+
# allocate memory
|
234
|
+
zsh, ysh, xsh = raw.shape
|
235
|
+
if extension == '.tar' or os.path.isdir(path_to_label):
|
236
|
+
x_shape = 2*xsh
|
237
|
+
else:
|
238
|
+
x_shape = xsh
|
239
|
+
out = np.empty((ysh, x_shape, 3), dtype=np.uint8)
|
240
|
+
gradient = np.empty((ysh, xsh), dtype=np.uint8)
|
241
|
+
|
242
|
+
# save preview slice by slice
|
243
|
+
for k in range(zsh):
|
244
|
+
raw_tmp = raw[k]
|
245
|
+
mask_tmp = mask[k]
|
246
|
+
out.fill(0)
|
247
|
+
|
248
|
+
# create output slice
|
249
|
+
for l in range(3):
|
250
|
+
out[:,:xsh,l] = raw_tmp
|
251
|
+
|
252
|
+
if np.any(mask_tmp):
|
253
|
+
|
254
|
+
# compute gradient
|
255
|
+
gradient.fill(0)
|
256
|
+
tmp = np.abs(mask_tmp[:-1] - mask_tmp[1:])
|
257
|
+
tmp[tmp>0] = 1
|
258
|
+
gradient[:-1] += tmp
|
259
|
+
gradient[1:] += tmp
|
260
|
+
tmp = np.abs(mask_tmp[:,:-1] - mask_tmp[:,1:])
|
261
|
+
tmp[tmp>0] = 1
|
262
|
+
gradient[:,:-1] += tmp
|
263
|
+
gradient[:,1:] += tmp
|
264
|
+
|
265
|
+
# colorize
|
266
|
+
for j, label in enumerate(labels):
|
267
|
+
C = Color[j]
|
268
|
+
tmp = np.logical_and(gradient>0, mask_tmp==label)
|
269
|
+
out[:,:xsh][tmp] = C
|
270
|
+
if extension == '.tar' or os.path.isdir(path_to_label):
|
271
|
+
out[:,xsh:][mask_tmp==label] = C
|
272
|
+
|
273
|
+
# save slice
|
274
|
+
im = Image.fromarray(out)
|
275
|
+
im.save(path_to_label_slices + f'/{k}.png')
|
276
|
+
|
277
|
+
except Exception as e:
|
278
|
+
print(e)
|
279
|
+
|
280
|
+
if __name__ == "__main__":
|
281
|
+
if len(sys.argv)==2:
|
282
|
+
path_to_labels = None
|
283
|
+
else:
|
284
|
+
path_to_labels = sys.argv[2]
|
285
|
+
create_slices(sys.argv[1], path_to_labels, True)
|
286
|
+
|