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,28 +27,27 @@
27
27
  ##########################################################################
28
28
 
29
29
  import os
30
- from biomedisa_features.keras_helper import read_img_list, remove_extracted_data
31
- from biomedisa_features.biomedisa_helper import img_resize, load_data, save_data, set_labels_to_zero
30
+ from biomedisa.features.keras_helper import read_img_list
31
+ from biomedisa.features.biomedisa_helper import img_resize, load_data, save_data, set_labels_to_zero
32
32
  from tensorflow.python.framework.errors_impl import ResourceExhaustedError
33
33
  from tensorflow.keras.applications import DenseNet121, densenet
34
34
  from tensorflow.keras.optimizers import Adam
35
35
  from tensorflow.keras.models import Model, load_model
36
36
  from tensorflow.keras.layers import Input, GlobalAveragePooling2D, Dropout, Dense
37
37
  from tensorflow.keras.callbacks import Callback, ModelCheckpoint
38
- from biomedisa_features.DataGeneratorCrop import DataGeneratorCrop
39
- from biomedisa_features.PredictDataGeneratorCrop import PredictDataGeneratorCrop
38
+ from biomedisa.features.DataGeneratorCrop import DataGeneratorCrop
39
+ from biomedisa.features.PredictDataGeneratorCrop import PredictDataGeneratorCrop
40
40
  import tensorflow as tf
41
41
  import numpy as np
42
42
  from glob import glob
43
43
  import h5py
44
44
  import tarfile
45
45
  import matplotlib.pyplot as plt
46
+ import tempfile
46
47
 
47
48
  class InputError(Exception):
48
- def __init__(self, message=None, img_names=[], label_names=[]):
49
+ def __init__(self, message=None):
49
50
  self.message = message
50
- self.img_names = img_names
51
- self.label_names = label_names
52
51
 
53
52
  def save_history(history, path_to_model):
54
53
  # summarize history for accuracy
@@ -98,124 +97,122 @@ def make_densenet(inputshape):
98
97
  def load_cropping_training_data(normalize, img_list, label_list, x_scale, y_scale, z_scale,
99
98
  labels_to_compute, labels_to_remove, img_in, label_in, normalization_parameters=None, channels=None):
100
99
 
101
- # read image lists
102
- if any(img_list):
103
- img_names, label_names = read_img_list(img_list, label_list)
104
- InputError.img_names = img_names
105
- InputError.label_names = label_names
106
-
107
- # load first label
108
- if any(img_list):
109
- a, _, _ = load_data(label_names[0], 'first_queue', True)
110
- if a is None:
111
- InputError.message = f'Invalid label data "{os.path.basename(label_names[0])}"'
112
- raise InputError()
113
- elif type(label_in) is list:
114
- a = label_in[0]
115
- else:
116
- a = label_in
117
- a = a.astype(np.uint8)
118
- a = set_labels_to_zero(a, labels_to_compute, labels_to_remove)
119
- label_z = np.any(a,axis=(1,2))
120
- label_y = np.any(a,axis=(0,2))
121
- label_x = np.any(a,axis=(0,1))
122
- label = np.append(label_z,label_y,axis=0)
123
- label = np.append(label,label_x,axis=0)
124
-
125
- # load first img
126
- if any(img_list):
127
- img, _ = load_data(img_names[0], 'first_queue')
128
- if img is None:
129
- InputError.message = f'Invalid image data "{os.path.basename(img_names[0])}"'
130
- raise InputError()
131
- elif type(img_in) is list:
132
- img = img_in[0]
133
- else:
134
- img = img_in
135
- # handle all images having channels >=1
136
- if len(img.shape)==3:
137
- z_shape, y_shape, x_shape = img.shape
138
- img = img.reshape(z_shape, y_shape, x_shape, 1)
139
- if channels is None:
140
- channels = img.shape[3]
141
- if img.shape[3] != channels:
142
- InputError.message = f'Number of channels must be {channels} for "{os.path.basename(img_names[0])}"'
143
- raise InputError()
144
- img = img.astype(np.float32)
145
- img_z = img_resize(img, a.shape[0], y_scale, x_scale)
146
- img_y = np.swapaxes(img_resize(img, z_scale, a.shape[1], x_scale),0,1)
147
- img_x = np.swapaxes(img_resize(img, z_scale, y_scale, a.shape[2]),0,2)
148
- img = np.append(img_z,img_y,axis=0)
149
- img = np.append(img,img_x,axis=0)
100
+ # make temporary directories
101
+ with tempfile.TemporaryDirectory() as temp_img_dir:
102
+ with tempfile.TemporaryDirectory() as temp_label_dir:
150
103
 
151
- # normalize image data
152
- for c in range(channels):
153
- img[:,:,:,c] -= np.amin(img[:,:,:,c])
154
- img[:,:,:,c] /= np.amax(img[:,:,:,c])
155
- if normalization_parameters is None:
156
- normalization_parameters = np.zeros((2,channels))
157
- normalization_parameters[0,c] = np.mean(img[:,:,:,c])
158
- normalization_parameters[1,c] = np.std(img[:,:,:,c])
159
- elif normalize:
160
- mean, std = np.mean(img[:,:,:,c]), np.std(img[:,:,:,c])
161
- img[:,:,:,c] = (img[:,:,:,c] - mean) / std
162
- img[:,:,:,c] = img[:,:,:,c] * normalization_parameters[1,c] + normalization_parameters[0,c]
163
-
164
- # loop over list of images
165
- if any(img_list) or type(img_in) is list:
166
- number_of_images = len(img_names) if any(img_list) else len(img_in)
167
-
168
- for k in range(1, number_of_images):
104
+ # read image lists
105
+ if any(img_list):
106
+ img_names, label_names = read_img_list(img_list, label_list, temp_img_dir, temp_label_dir)
169
107
 
170
- # append label
171
- if any(label_list):
172
- a, _ = load_data(label_names[k], 'first_queue')
108
+ # load first label
109
+ if any(img_list):
110
+ a, _, _ = load_data(label_names[0], 'first_queue', True)
173
111
  if a is None:
174
- InputError.message = f'Invalid label data "{os.path.basename(label_names[k])}"'
112
+ InputError.message = f'Invalid label data "{os.path.basename(label_names[0])}"'
175
113
  raise InputError()
114
+ elif type(label_in) is list:
115
+ a = label_in[0]
176
116
  else:
177
- a = label_in[k]
117
+ a = label_in
178
118
  a = a.astype(np.uint8)
179
119
  a = set_labels_to_zero(a, labels_to_compute, labels_to_remove)
180
- next_label_z = np.any(a,axis=(1,2))
181
- next_label_y = np.any(a,axis=(0,2))
182
- next_label_x = np.any(a,axis=(0,1))
183
- label = np.append(label,next_label_z,axis=0)
184
- label = np.append(label,next_label_y,axis=0)
185
- label = np.append(label,next_label_x,axis=0)
186
-
187
- # append image
120
+ label_z = np.any(a,axis=(1,2))
121
+ label_y = np.any(a,axis=(0,2))
122
+ label_x = np.any(a,axis=(0,1))
123
+ label = np.append(label_z,label_y,axis=0)
124
+ label = np.append(label,label_x,axis=0)
125
+
126
+ # load first img
188
127
  if any(img_list):
189
- a, _ = load_data(img_names[k], 'first_queue')
190
- if a is None:
191
- InputError.message = f'Invalid image data "{os.path.basename(img_names[k])}"'
128
+ img, _ = load_data(img_names[0], 'first_queue')
129
+ if img is None:
130
+ InputError.message = f'Invalid image data "{os.path.basename(img_names[0])}"'
192
131
  raise InputError()
132
+ elif type(img_in) is list:
133
+ img = img_in[0]
193
134
  else:
194
- a = img_in[k]
195
- if len(a.shape)==3:
196
- z_shape, y_shape, x_shape = a.shape
197
- a = a.reshape(z_shape, y_shape, x_shape, 1)
198
- if a.shape[3] != channels:
199
- InputError.message = f'Number of channels must be {channels} for "{os.path.basename(img_names[k])}"'
135
+ img = img_in
136
+ # handle all images having channels >=1
137
+ if len(img.shape)==3:
138
+ z_shape, y_shape, x_shape = img.shape
139
+ img = img.reshape(z_shape, y_shape, x_shape, 1)
140
+ if channels is None:
141
+ channels = img.shape[3]
142
+ if img.shape[3] != channels:
143
+ InputError.message = f'Number of channels must be {channels} for "{os.path.basename(img_names[0])}"'
200
144
  raise InputError()
201
- a = a.astype(np.float32)
202
- img_z = img_resize(a, a.shape[0], y_scale, x_scale)
203
- img_y = np.swapaxes(img_resize(a, z_scale, a.shape[1], x_scale),0,1)
204
- img_x = np.swapaxes(img_resize(a, z_scale, y_scale, a.shape[2]),0,2)
205
- next_img = np.append(img_z,img_y,axis=0)
206
- next_img = np.append(next_img,img_x,axis=0)
145
+ img = img.astype(np.float32)
146
+ img_z = img_resize(img, a.shape[0], y_scale, x_scale)
147
+ img_y = np.swapaxes(img_resize(img, z_scale, a.shape[1], x_scale),0,1)
148
+ img_x = np.swapaxes(img_resize(img, z_scale, y_scale, a.shape[2]),0,2)
149
+ img = np.append(img_z,img_y,axis=0)
150
+ img = np.append(img,img_x,axis=0)
151
+
152
+ # normalize image data
207
153
  for c in range(channels):
208
- next_img[:,:,:,c] -= np.amin(next_img[:,:,:,c])
209
- next_img[:,:,:,c] /= np.amax(next_img[:,:,:,c])
210
- if normalize:
211
- mean, std = np.mean(next_img[:,:,:,c]), np.std(next_img[:,:,:,c])
212
- next_img[:,:,:,c] = (next_img[:,:,:,c] - mean) / std
213
- next_img[:,:,:,c] = next_img[:,:,:,c] * normalization_parameters[1,c] + normalization_parameters[0,c]
214
- img = np.append(img, next_img, axis=0)
215
-
216
- # remove extracted data
217
- if any(img_list):
218
- remove_extracted_data(img_names, label_names)
154
+ img[:,:,:,c] -= np.amin(img[:,:,:,c])
155
+ img[:,:,:,c] /= np.amax(img[:,:,:,c])
156
+ if normalization_parameters is None:
157
+ normalization_parameters = np.zeros((2,channels))
158
+ normalization_parameters[0,c] = np.mean(img[:,:,:,c])
159
+ normalization_parameters[1,c] = np.std(img[:,:,:,c])
160
+ elif normalize:
161
+ mean, std = np.mean(img[:,:,:,c]), np.std(img[:,:,:,c])
162
+ img[:,:,:,c] = (img[:,:,:,c] - mean) / std
163
+ img[:,:,:,c] = img[:,:,:,c] * normalization_parameters[1,c] + normalization_parameters[0,c]
164
+
165
+ # loop over list of images
166
+ if any(img_list) or type(img_in) is list:
167
+ number_of_images = len(img_names) if any(img_list) else len(img_in)
168
+
169
+ for k in range(1, number_of_images):
170
+
171
+ # append label
172
+ if any(label_list):
173
+ a, _ = load_data(label_names[k], 'first_queue')
174
+ if a is None:
175
+ InputError.message = f'Invalid label data "{os.path.basename(label_names[k])}"'
176
+ raise InputError()
177
+ else:
178
+ a = label_in[k]
179
+ a = a.astype(np.uint8)
180
+ a = set_labels_to_zero(a, labels_to_compute, labels_to_remove)
181
+ next_label_z = np.any(a,axis=(1,2))
182
+ next_label_y = np.any(a,axis=(0,2))
183
+ next_label_x = np.any(a,axis=(0,1))
184
+ label = np.append(label,next_label_z,axis=0)
185
+ label = np.append(label,next_label_y,axis=0)
186
+ label = np.append(label,next_label_x,axis=0)
187
+
188
+ # append image
189
+ if any(img_list):
190
+ a, _ = load_data(img_names[k], 'first_queue')
191
+ if a is None:
192
+ InputError.message = f'Invalid image data "{os.path.basename(img_names[k])}"'
193
+ raise InputError()
194
+ else:
195
+ a = img_in[k]
196
+ if len(a.shape)==3:
197
+ z_shape, y_shape, x_shape = a.shape
198
+ a = a.reshape(z_shape, y_shape, x_shape, 1)
199
+ if a.shape[3] != channels:
200
+ InputError.message = f'Number of channels must be {channels} for "{os.path.basename(img_names[k])}"'
201
+ raise InputError()
202
+ a = a.astype(np.float32)
203
+ img_z = img_resize(a, a.shape[0], y_scale, x_scale)
204
+ img_y = np.swapaxes(img_resize(a, z_scale, a.shape[1], x_scale),0,1)
205
+ img_x = np.swapaxes(img_resize(a, z_scale, y_scale, a.shape[2]),0,2)
206
+ next_img = np.append(img_z,img_y,axis=0)
207
+ next_img = np.append(next_img,img_x,axis=0)
208
+ for c in range(channels):
209
+ next_img[:,:,:,c] -= np.amin(next_img[:,:,:,c])
210
+ next_img[:,:,:,c] /= np.amax(next_img[:,:,:,c])
211
+ if normalize:
212
+ mean, std = np.mean(next_img[:,:,:,c]), np.std(next_img[:,:,:,c])
213
+ next_img[:,:,:,c] = (next_img[:,:,:,c] - mean) / std
214
+ next_img[:,:,:,c] = next_img[:,:,:,c] * normalization_parameters[1,c] + normalization_parameters[0,c]
215
+ img = np.append(img, next_img, axis=0)
219
216
 
220
217
  # limit intensity range
221
218
  img[img<0] = 0
@@ -359,8 +356,6 @@ def load_data_to_crop(path_to_img, channels, x_scale, y_scale, z_scale,
359
356
  # read image data
360
357
  if img is None:
361
358
  img, _, _ = load_data(path_to_img, 'first_queue', return_extension=True)
362
- InputError.img_names = [path_to_img]
363
- InputError.label_names = []
364
359
  img_data = np.copy(img, order='C')
365
360
  if img is None:
366
361
  InputError.message = "Invalid image data %s." %(os.path.basename(path_to_img))
@@ -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
  ## ##
@@ -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
  ## ##
@@ -26,15 +26,14 @@
26
26
  ## ##
27
27
  ##########################################################################
28
28
 
29
- import sys, os
30
- BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
31
- import subprocess
29
+ import os
30
+ import biomedisa
32
31
 
33
32
  def create_error_object(message, remote=False, queue=None, img_id=None):
34
33
 
35
34
  # remote server
36
35
  if remote:
37
- with open(BASE_DIR + f'/log/error_{queue}', 'w') as errorfile:
36
+ with open(biomedisa.BASE_DIR + f'/log/error_{queue}', 'w') as errorfile:
38
37
  print(message, file=errorfile)
39
38
 
40
39
  # local server
@@ -57,7 +56,7 @@ def create_pid_object(pid, remote=False, queue=None, img_id=None, path_to_model=
57
56
 
58
57
  # remote server
59
58
  if remote:
60
- with open(BASE_DIR + f'/log/pid_{queue}', 'w') as pidfile:
59
+ with open(biomedisa.BASE_DIR + f'/log/pid_{queue}', 'w') as pidfile:
61
60
  print(pid, file=pidfile)
62
61
 
63
62
  # local server
@@ -74,7 +73,7 @@ def post_processing(path_to_final, time_str, server_name, remote, queue, dice=1.
74
73
 
75
74
  # remote server
76
75
  if remote:
77
- with open(BASE_DIR + f'/log/config_{queue}', 'w') as configfile:
76
+ with open(biomedisa.BASE_DIR + f'/log/config_{queue}', 'w') as configfile:
78
77
  print(path_to_final, path_to_uq, path_to_smooth, uncertainty, smooth, str(time_str).replace(' ','-'), server_name, path_to_model, path_to_cropped_image, dice, file=configfile)
79
78
 
80
79
  # local server
@@ -83,9 +82,9 @@ def post_processing(path_to_final, time_str, server_name, remote, queue, dice=1.
83
82
  django.setup()
84
83
  from biomedisa_app.models import Upload
85
84
  from biomedisa_app.views import send_notification
86
- from biomedisa_features.active_contour import init_active_contour
87
- from biomedisa_features.remove_outlier import init_remove_outlier
88
- from biomedisa_features.create_slices import create_slices
85
+ from biomedisa.features.active_contour import init_active_contour
86
+ from biomedisa.features.remove_outlier import init_remove_outlier
87
+ from biomedisa.features.create_slices import create_slices
89
88
  from redis import Redis
90
89
  from rq import Queue
91
90