biomedisa 2024.5.18__py3-none-any.whl → 2024.5.20__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 (45) hide show
  1. biomedisa/__init__.py +2 -6
  2. biomedisa/deeplearning.py +88 -91
  3. biomedisa/{biomedisa_features → features}/DataGenerator.py +1 -1
  4. biomedisa/{biomedisa_features → features}/DataGeneratorCrop.py +1 -1
  5. biomedisa/{biomedisa_features → features}/PredictDataGenerator.py +1 -1
  6. biomedisa/{biomedisa_features → features}/PredictDataGeneratorCrop.py +1 -1
  7. biomedisa/{biomedisa_features → features}/active_contour.py +15 -18
  8. biomedisa/{biomedisa_features → features}/assd.py +1 -1
  9. biomedisa/{biomedisa_features → features}/biomedisa_helper.py +107 -117
  10. biomedisa/{biomedisa_features → features}/create_slices.py +51 -60
  11. biomedisa/{biomedisa_features → features}/crop_helper.py +111 -116
  12. biomedisa/{biomedisa_features → features}/curvop_numba.py +1 -1
  13. biomedisa/{biomedisa_features → features}/django_env.py +9 -10
  14. biomedisa/{biomedisa_features → features}/keras_helper.py +143 -170
  15. biomedisa/{biomedisa_features → features}/nc_reader.py +1 -1
  16. biomedisa/{biomedisa_features → features}/pid.py +2 -2
  17. biomedisa/{biomedisa_features → features}/process_image.py +12 -14
  18. biomedisa/{biomedisa_features → features}/pycuda_test.py +2 -1
  19. biomedisa/{biomedisa_features → features}/random_walk/gpu_kernels.py +2 -1
  20. biomedisa/{biomedisa_features → features}/random_walk/pycuda_large.py +2 -2
  21. biomedisa/{biomedisa_features → features}/random_walk/pycuda_large_allx.py +2 -2
  22. biomedisa/{biomedisa_features → features}/random_walk/pycuda_small.py +2 -2
  23. biomedisa/{biomedisa_features → features}/random_walk/pycuda_small_allx.py +2 -2
  24. biomedisa/{biomedisa_features → features}/random_walk/pyopencl_large.py +1 -1
  25. biomedisa/{biomedisa_features → features}/random_walk/pyopencl_small.py +1 -1
  26. biomedisa/{biomedisa_features → features}/random_walk/rw_large.py +11 -11
  27. biomedisa/{biomedisa_features → features}/random_walk/rw_small.py +12 -12
  28. biomedisa/{biomedisa_features → features}/remove_outlier.py +13 -16
  29. biomedisa/features/split_volume.py +167 -0
  30. biomedisa/interpolation.py +10 -12
  31. biomedisa/mesh.py +9 -12
  32. {biomedisa-2024.5.18.dist-info → biomedisa-2024.5.20.dist-info}/METADATA +15 -16
  33. biomedisa-2024.5.20.dist-info/RECORD +44 -0
  34. biomedisa/biomedisa_features/split_volume.py +0 -274
  35. biomedisa-2024.5.18.dist-info/RECORD +0 -44
  36. /biomedisa/{biomedisa_features → features}/__init__.py +0 -0
  37. /biomedisa/{biomedisa_features → features}/amira_to_np/__init__.py +0 -0
  38. /biomedisa/{biomedisa_features → features}/amira_to_np/amira_data_stream.py +0 -0
  39. /biomedisa/{biomedisa_features → features}/amira_to_np/amira_grammar.py +0 -0
  40. /biomedisa/{biomedisa_features → features}/amira_to_np/amira_header.py +0 -0
  41. /biomedisa/{biomedisa_features → features}/amira_to_np/amira_helper.py +0 -0
  42. /biomedisa/{biomedisa_features → features}/random_walk/__init__.py +0 -0
  43. {biomedisa-2024.5.18.dist-info → biomedisa-2024.5.20.dist-info}/LICENSE +0 -0
  44. {biomedisa-2024.5.18.dist-info → biomedisa-2024.5.20.dist-info}/WHEEL +0 -0
  45. {biomedisa-2024.5.18.dist-info → biomedisa-2024.5.20.dist-info}/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  ##########################################################################
2
2
  ## ##
3
- ## Copyright (c) 2024 Philipp Lösel. All rights reserved. ##
3
+ ## Copyright (c) 2019-2024 Philipp Lösel. All rights reserved. ##
4
4
  ## ##
5
5
  ## This file is part of the open source project biomedisa. ##
6
6
  ## ##
@@ -27,9 +27,9 @@
27
27
  ##########################################################################
28
28
 
29
29
  import os
30
- BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
31
- from biomedisa_features.amira_to_np.amira_helper import amira_to_np, np_to_amira
32
- from biomedisa_features.nc_reader import nc_to_np, np_to_nc
30
+ import biomedisa
31
+ from biomedisa.features.amira_to_np.amira_helper import amira_to_np, np_to_amira
32
+ from biomedisa.features.nc_reader import nc_to_np, np_to_nc
33
33
  from tifffile import imread, imwrite
34
34
  from medpy.io import load, save
35
35
  import SimpleITK as sitk
@@ -41,10 +41,10 @@ import cv2
41
41
  import time
42
42
  import zipfile
43
43
  import numba
44
- import shutil
45
44
  import subprocess
46
45
  import re
47
46
  import math
47
+ import tempfile
48
48
 
49
49
  def silent_remove(filename):
50
50
  try:
@@ -53,7 +53,7 @@ def silent_remove(filename):
53
53
  pass
54
54
 
55
55
  # create a unique filename
56
- def unique_file_path(path, dir_path=BASE_DIR+'/private_storage/'):
56
+ def unique_file_path(path, dir_path=biomedisa.BASE_DIR+'/private_storage/'):
57
57
 
58
58
  # get extension
59
59
  username = os.path.basename(os.path.dirname(path))
@@ -116,7 +116,7 @@ def Dice_score(ground_truth, result, average_dice=False):
116
116
 
117
117
  def ASSD(ground_truth, result):
118
118
  try:
119
- from biomedisa_features.assd import ASSD_one_label
119
+ from biomedisa.features.assd import ASSD_one_label
120
120
  number_of_elements = 0
121
121
  distances = 0
122
122
  hausdorff = 0
@@ -280,92 +280,85 @@ def load_data(path_to_data, process='None', return_extension=False):
280
280
  data, header = None, None
281
281
 
282
282
  elif extension == '.zip' or os.path.isdir(path_to_data):
283
- # extract files
284
- if extension=='.zip':
285
- path_to_dir = BASE_DIR + '/tmp/' + id_generator(40)
286
- try:
287
- zip_ref = zipfile.ZipFile(path_to_data, 'r')
288
- zip_ref.extractall(path=path_to_dir)
289
- zip_ref.close()
290
- except Exception as e:
291
- print(e)
292
- print('Using unzip package...')
293
- try:
294
- success = subprocess.Popen(['unzip',path_to_data,'-d',path_to_dir]).wait()
295
- if success != 0:
296
- if os.path.isdir(path_to_dir):
297
- shutil.rmtree(path_to_dir)
298
- data, header = None, None
299
- except Exception as e:
300
- print(e)
301
- if os.path.isdir(path_to_dir):
302
- shutil.rmtree(path_to_dir)
303
- data, header = None, None
304
- path_to_data = path_to_dir
305
-
306
- # load files
307
- if os.path.isdir(path_to_data):
308
- files = []
309
- for data_type in ['.[pP][nN][gG]','.[tT][iI][fF]','.[tT][iI][fF][fF]','.[dD][cC][mM]','.[dD][iI][cC][oO][mM]','.[bB][mM][pP]','.[jJ][pP][gG]','.[jJ][pP][eE][gG]','.nc','.nc.bz2']:
310
- files += [file for file in glob.glob(path_to_data+'/**/*'+data_type, recursive=True) if not os.path.basename(file).startswith('.')]
311
- nc_extension = False
312
- for file in files:
313
- if os.path.splitext(file)[1] == '.nc' or os.path.splitext(os.path.splitext(file)[0])[1] == '.nc':
314
- nc_extension = True
315
- if nc_extension:
283
+ with tempfile.TemporaryDirectory() as temp_dir:
284
+
285
+ # extract files
286
+ if extension=='.zip':
316
287
  try:
317
- data, header = nc_to_np(path_to_data)
288
+ zip_ref = zipfile.ZipFile(path_to_data, 'r')
289
+ zip_ref.extractall(path=temp_dir)
290
+ zip_ref.close()
318
291
  except Exception as e:
319
292
  print(e)
320
- data, header = None, None
321
- else:
322
- try:
323
- # remove unreadable files or directories
324
- for name in files:
325
- if os.path.isfile(name):
326
- try:
327
- img, _ = load(name)
328
- except:
293
+ print('Using unzip package...')
294
+ try:
295
+ success = subprocess.Popen(['unzip',path_to_data,'-d',temp_dir]).wait()
296
+ if success != 0:
297
+ data, header = None, None
298
+ except Exception as e:
299
+ print(e)
300
+ data, header = None, None
301
+ path_to_data = temp_dir
302
+
303
+ # load files
304
+ if os.path.isdir(path_to_data):
305
+ files = []
306
+ for data_type in ['.[pP][nN][gG]','.[tT][iI][fF]','.[tT][iI][fF][fF]','.[dD][cC][mM]','.[dD][iI][cC][oO][mM]','.[bB][mM][pP]','.[jJ][pP][gG]','.[jJ][pP][eE][gG]','.nc','.nc.bz2']:
307
+ files += [file for file in glob.glob(path_to_data+'/**/*'+data_type, recursive=True) if not os.path.basename(file).startswith('.')]
308
+ nc_extension = False
309
+ for file in files:
310
+ if os.path.splitext(file)[1] == '.nc' or os.path.splitext(os.path.splitext(file)[0])[1] == '.nc':
311
+ nc_extension = True
312
+ if nc_extension:
313
+ try:
314
+ data, header = nc_to_np(path_to_data)
315
+ except Exception as e:
316
+ print(e)
317
+ data, header = None, None
318
+ else:
319
+ try:
320
+ # remove unreadable files or directories
321
+ for name in files:
322
+ if os.path.isfile(name):
323
+ try:
324
+ img, _ = load(name)
325
+ except:
326
+ files.remove(name)
327
+ else:
329
328
  files.remove(name)
329
+ files.sort()
330
+
331
+ # get data size
332
+ img, _ = load(files[0])
333
+ if len(img.shape)==3:
334
+ ysh, xsh, csh = img.shape[0], img.shape[1], img.shape[2]
335
+ channel = 'last'
336
+ if ysh < csh:
337
+ csh, ysh, xsh = img.shape[0], img.shape[1], img.shape[2]
338
+ channel = 'first'
330
339
  else:
331
- files.remove(name)
332
- files.sort()
333
-
334
- # get data size
335
- img, _ = load(files[0])
336
- if len(img.shape)==3:
337
- ysh, xsh, csh = img.shape[0], img.shape[1], img.shape[2]
338
- channel = 'last'
339
- if ysh < csh:
340
- csh, ysh, xsh = img.shape[0], img.shape[1], img.shape[2]
341
- channel = 'first'
342
- else:
343
- ysh, xsh = img.shape[0], img.shape[1]
344
- csh, channel = 0, None
345
-
346
- # load data slice by slice
347
- data = np.empty((len(files), ysh, xsh), dtype=img.dtype)
348
- header, image_data_shape = [], []
349
- for k, file_name in enumerate(files):
350
- img, img_header = load(file_name)
351
- if csh==3:
352
- img = rgb2gray(img, channel)
353
- elif csh==1 and channel=='last':
354
- img = img[:,:,0]
355
- elif csh==1 and channel=='first':
356
- img = img[0,:,:]
357
- data[k] = img
358
- header.append(img_header)
359
- header = [header, files, data.dtype]
360
- data = np.swapaxes(data, 1, 2)
361
- data = np.copy(data, order='C')
362
- except Exception as e:
363
- print(e)
364
- data, header = None, None
365
-
366
- # remove extracted files
367
- if extension=='.zip' and os.path.isdir(path_to_data):
368
- shutil.rmtree(path_to_data)
340
+ ysh, xsh = img.shape[0], img.shape[1]
341
+ csh, channel = 0, None
342
+
343
+ # load data slice by slice
344
+ data = np.empty((len(files), ysh, xsh), dtype=img.dtype)
345
+ header, image_data_shape = [], []
346
+ for k, file_name in enumerate(files):
347
+ img, img_header = load(file_name)
348
+ if csh==3:
349
+ img = rgb2gray(img, channel)
350
+ elif csh==1 and channel=='last':
351
+ img = img[:,:,0]
352
+ elif csh==1 and channel=='first':
353
+ img = img[0,:,:]
354
+ data[k] = img
355
+ header.append(img_header)
356
+ header = [header, files, data.dtype]
357
+ data = np.swapaxes(data, 1, 2)
358
+ data = np.copy(data, order='C')
359
+ except Exception as e:
360
+ print(e)
361
+ data, header = None, None
369
362
 
370
363
  elif extension == '.mrc':
371
364
  try:
@@ -396,7 +389,7 @@ def load_data(path_to_data, process='None', return_extension=False):
396
389
 
397
390
  def _error_(bm, message):
398
391
  if bm.django_env:
399
- from biomedisa_features.django_env import create_error_object
392
+ from biomedisa.features.django_env import create_error_object
400
393
  create_error_object(message, bm.remote, bm.queue, bm.img_id)
401
394
  with open(bm.path_to_logfile, 'a') as logfile:
402
395
  print('%s %s %s %s' %(time.ctime(), bm.username, bm.shortfilename, message), file=logfile)
@@ -495,34 +488,31 @@ def save_data(path_to_final, final, header=None, final_image_type=None, compress
495
488
  simg.CopyInformation(header)
496
489
  sitk.WriteImage(simg, path_to_final, useCompression=compress)
497
490
  elif final_image_type in ['.zip', 'directory', '']:
498
- # make results directory
499
- if final_image_type == '.zip':
500
- results_dir = BASE_DIR + '/tmp/' + id_generator(40)
501
- os.makedirs(results_dir)
502
- os.chmod(results_dir, 0o770)
503
- else:
504
- results_dir = path_to_final
505
- if not os.path.isdir(results_dir):
506
- os.makedirs(results_dir)
507
- os.chmod(results_dir, 0o770)
508
- # save data as NC blocks
509
- if os.path.splitext(header[1][0])[1] == '.nc':
510
- np_to_nc(results_dir, final, header)
511
- file_names = header[1]
512
- # save data as PNG, TIF, DICOM slices
513
- else:
514
- header, file_names, final_dtype = header[0], header[1], header[2]
515
- final = final.astype(final_dtype)
516
- final = np.swapaxes(final, 2, 1)
517
- for k, file in enumerate(file_names):
518
- save(final[k], results_dir + '/' + os.path.basename(file), header[k])
519
- # zip data
520
- if final_image_type == '.zip':
521
- with zipfile.ZipFile(path_to_final, 'w') as zip:
522
- for file in file_names:
523
- zip.write(results_dir + '/' + os.path.basename(file), os.path.basename(file))
524
- if os.path.isdir(results_dir):
525
- shutil.rmtree(results_dir)
491
+ with tempfile.TemporaryDirectory() as temp_dir:
492
+ # make results directory
493
+ if final_image_type == '.zip':
494
+ results_dir = temp_dir
495
+ else:
496
+ results_dir = path_to_final
497
+ if not os.path.isdir(results_dir):
498
+ os.makedirs(results_dir)
499
+ os.chmod(results_dir, 0o770)
500
+ # save data as NC blocks
501
+ if os.path.splitext(header[1][0])[1] == '.nc':
502
+ np_to_nc(results_dir, final, header)
503
+ file_names = header[1]
504
+ # save data as PNG, TIF, DICOM slices
505
+ else:
506
+ header, file_names, final_dtype = header[0], header[1], header[2]
507
+ final = final.astype(final_dtype)
508
+ final = np.swapaxes(final, 2, 1)
509
+ for k, file in enumerate(file_names):
510
+ save(final[k], results_dir + '/' + os.path.basename(file), header[k])
511
+ # zip data
512
+ if final_image_type == '.zip':
513
+ with zipfile.ZipFile(path_to_final, 'w') as zip:
514
+ for file in file_names:
515
+ zip.write(results_dir + '/' + os.path.basename(file), os.path.basename(file))
526
516
  else:
527
517
  imageSize = int(final.nbytes * 10e-7)
528
518
  bigtiff = True if imageSize > 2000 else False
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/python3
2
2
  ##########################################################################
3
3
  ## ##
4
- ## Copyright (c) 2024 Philipp Lösel. All rights reserved. ##
4
+ ## Copyright (c) 2019-2024 Philipp Lösel. All rights reserved. ##
5
5
  ## ##
6
6
  ## This file is part of the open source project biomedisa. ##
7
7
  ## ##
@@ -28,9 +28,7 @@
28
28
  ##########################################################################
29
29
 
30
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
31
+ from biomedisa.features.biomedisa_helper import load_data, color_to_gray, img_to_uint8, img_resize, id_generator
34
32
  from PIL import Image
35
33
  import numpy as np
36
34
  from glob import glob
@@ -38,6 +36,7 @@ import numba
38
36
  import shutil
39
37
  import cv2
40
38
  import tarfile
39
+ import tempfile
41
40
 
42
41
  def unique(arr):
43
42
  arr = arr.astype(np.uint8)
@@ -110,37 +109,33 @@ def create_slices(path_to_data, path_to_label, on_site=False):
110
109
  # load data and reduce data size
111
110
  path_to_dir, extension = os.path.splitext(path_to_data)
112
111
  if extension == '.gz':
113
- path_to_dir, extension = os.path.splitext(path_to_dir)
112
+ extension = os.path.splitext(path_to_dir)[1]
114
113
  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)
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)
144
139
  else:
145
140
  raw, _ = load_data(path_to_data, 'create_slices')
146
141
  zsh, ysh, xsh = raw.shape
@@ -174,33 +169,29 @@ def create_slices(path_to_data, path_to_label, on_site=False):
174
169
  # load data
175
170
  path_to_dir, extension = os.path.splitext(path_to_label)
176
171
  if extension == '.gz':
177
- path_to_dir, extension = os.path.splitext(path_to_dir)
172
+ extension = os.path.splitext(path_to_dir)[1]
178
173
  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)
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)
204
195
  else:
205
196
  mask, _ = load_data(path_to_label, 'create_slices')
206
197
  mask = color_to_gray(mask)