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,149 @@
1
+ ##########################################################################
2
+ ## ##
3
+ ## Copyright (c) 2019-2024 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 numba
31
+
32
+ @numba.jit(nopython=True)
33
+ def evolution(mean, A, data, alpha):
34
+ zsh, ysh, xsh = A.shape
35
+ B = np.copy(A)
36
+ for k in range(1, zsh-1):
37
+ for l in range(1, ysh-1):
38
+ for m in range(1, xsh-1):
39
+ refLabel = A[k,l,m]
40
+ finLabel = refLabel
41
+ img = data[k,l,m]
42
+ if refLabel == 0:
43
+ ref = alpha * abs(mean[refLabel] - img)
44
+ else:
45
+ ref = abs(mean[refLabel] - img)
46
+ for n in range(-1, 2):
47
+ for o in range(-1, 2):
48
+ for p in range(-1, 2):
49
+ tmpLabel = A[k+n, l+o, m+p]
50
+ if tmpLabel != refLabel:
51
+ if tmpLabel == 0:
52
+ val = alpha * abs(mean[tmpLabel] - img)
53
+ else:
54
+ val = abs(mean[tmpLabel] - img)
55
+ if val < ref:
56
+ ref = val
57
+ finLabel = tmpLabel
58
+ B[k,l,m] = finLabel
59
+ return B
60
+
61
+ @numba.jit(nopython=True)
62
+ def erosion(start, final, _P3, zsh, ysh, xsh, label):
63
+ for plane in range(1, zsh-1):
64
+ for row in range(1, ysh-1):
65
+ for column in range(1, xsh-1):
66
+ found = 0
67
+ for n in range(-1, 2):
68
+ for o in range(-1, 2):
69
+ for p in range(-1, 2):
70
+ if start[plane+n, row+o, column+p] != label:
71
+ found = 1
72
+ if start[plane, row, column] == label and found == 1:
73
+ t, found = 0, 0
74
+ while t < 9 and found == 0:
75
+ value = 0
76
+ for k in range(3):
77
+ for l in range(3):
78
+ for m in range(3):
79
+ if _P3[t,k,l,m] == 1:
80
+ value += abs(start[plane-1+k, row-1+l, column-1+m] - label)
81
+ if value == 0:
82
+ found = 1
83
+ t += 1
84
+ if found == 0:
85
+ subLabel = label
86
+ for n in range(-1, 2):
87
+ for o in range(-1, 2):
88
+ for p in range(-1, 2):
89
+ tmpLabel = start[plane+n, row+o, column+p]
90
+ if tmpLabel != label:
91
+ subLabel = tmpLabel
92
+ final[plane, row, column] = subLabel
93
+ return start, final
94
+
95
+ @numba.jit(nopython=True)
96
+ def dilation(start, final, _P3, zsh, ysh, xsh, label):
97
+ for plane in range(1, zsh-1):
98
+ for row in range(1, ysh-1):
99
+ for column in range(1, xsh-1):
100
+ found = 0
101
+ for n in range(-1, 2):
102
+ for o in range(-1, 2):
103
+ for p in range(-1, 2):
104
+ if start[plane+n, row+o, column+p] == label:
105
+ found = 1
106
+ if start[plane, row, column] != label and found == 1:
107
+ t, found = 0, 0
108
+ while t < 9 and found == 0:
109
+ value = 0
110
+ for k in range(3):
111
+ for l in range(3):
112
+ for m in range(3):
113
+ if _P3[t,k,l,m] == 1 and start[plane-1+k, row-1+l, column-1+m] != label:
114
+ value += 0
115
+ else:
116
+ value += 1
117
+ if value == 0:
118
+ found = 1
119
+ t += 1
120
+ if found == 0:
121
+ final[plane, row, column] = label
122
+ return start, final
123
+
124
+ def curvop(start, steps, label, allLabels):
125
+ zsh, ysh, xsh = start.shape
126
+ final = np.copy(start, order='C')
127
+ _P3 = np.zeros((9,3,3,3), dtype=np.int32)
128
+ _P3[0,:,:,1] = 1
129
+ _P3[1,:,1,:] = 1
130
+ _P3[2,1,:,:] = 1
131
+ _P3[3,:,[0,1,2],[0,1,2]] = 1
132
+ _P3[4,:,[0,1,2],[2,1,0]] = 1
133
+ _P3[5,[0,1,2],:,[0,1,2]] = 1
134
+ _P3[6,[0,1,2],:,[2,1,0]] = 1
135
+ _P3[7,[0,1,2],[0,1,2],:] = 1
136
+ _P3[8,[0,1,2],[2,1,0],:] = 1
137
+ for k in range(steps):
138
+ if k % 2 == 0:
139
+ start, final = erosion(start, final, _P3, zsh, ysh, xsh, label)
140
+ start = np.copy(final, order='C')
141
+ final, start = dilation(final, start, _P3, zsh, ysh, xsh, label)
142
+ final = np.copy(start, order='C')
143
+ else:
144
+ start, final = dilation(start, final, _P3, zsh, ysh, xsh, label)
145
+ start = np.copy(final, order='C')
146
+ final, start = erosion(final, start, _P3, zsh, ysh, xsh, label)
147
+ final = np.copy(start, order='C')
148
+ return start
149
+
@@ -0,0 +1,171 @@
1
+ ##########################################################################
2
+ ## ##
3
+ ## Copyright (c) 2019-2024 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 os
30
+ import biomedisa
31
+
32
+ def create_error_object(message, remote=False, queue=None, img_id=None):
33
+
34
+ # remote server
35
+ if remote:
36
+ with open(biomedisa.BASE_DIR + f'/log/error_{queue}', 'w') as errorfile:
37
+ print(message, file=errorfile)
38
+
39
+ # local server
40
+ else:
41
+ import django
42
+ django.setup()
43
+ from biomedisa_app.models import Upload
44
+ from biomedisa_app.views import send_error_message
45
+ image = Upload.objects.get(pk=img_id)
46
+ Upload.objects.create(user=image.user, project=image.project, log=1, imageType=None, shortfilename=message)
47
+ send_error_message(image.user.username, image.shortfilename, message)
48
+
49
+ # stop processing
50
+ image.path_to_model = ''
51
+ image.status = 0
52
+ image.pid = 0
53
+ image.save()
54
+
55
+ def create_pid_object(pid, remote=False, queue=None, img_id=None, path_to_model=''):
56
+
57
+ # remote server
58
+ if remote:
59
+ with open(biomedisa.BASE_DIR + f'/log/pid_{queue}', 'w') as pidfile:
60
+ print(pid, file=pidfile)
61
+
62
+ # local server
63
+ else:
64
+ import django
65
+ django.setup()
66
+ from biomedisa_app.models import Upload
67
+ image = Upload.objects.get(pk=img_id)
68
+ image.path_to_model = path_to_model
69
+ image.pid = pid
70
+ image.save()
71
+
72
+ def post_processing(path_to_final, time_str, server_name, remote, queue, dice=1.0, path_to_model=None, path_to_uq=None, path_to_smooth=None, path_to_cropped_image=None, uncertainty=False, smooth=False, img_id=None, label_id=None, train=False, predict=False, validation=False):
73
+
74
+ # remote server
75
+ if remote:
76
+ with open(biomedisa.BASE_DIR + f'/log/config_{queue}', 'w') as configfile:
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)
78
+
79
+ # local server
80
+ else:
81
+ import django
82
+ django.setup()
83
+ from biomedisa_app.models import Upload
84
+ from biomedisa_app.views import send_notification
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
88
+ from redis import Redis
89
+ from rq import Queue
90
+
91
+ # get object
92
+ image = Upload.objects.get(pk=img_id)
93
+
94
+ if train:
95
+ # create model object
96
+ shortfilename = os.path.basename(path_to_model)
97
+ filename = 'images/' + image.user.username + '/' + shortfilename
98
+ Upload.objects.create(pic=filename, user=image.user, project=image.project, imageType=4, shortfilename=shortfilename)
99
+
100
+ if validation:
101
+ # create acc object
102
+ shortfilename = os.path.basename(path_to_model.replace('.h5','_acc.png'))
103
+ filename = 'images/' + image.user.username + '/' + shortfilename
104
+ Upload.objects.create(pic=filename, user=image.user, project=image.project, imageType=6, shortfilename=shortfilename)
105
+
106
+ # create loss object
107
+ shortfilename = os.path.basename(path_to_model.replace('.h5','_loss.png'))
108
+ filename = 'images/' + image.user.username + '/' + shortfilename
109
+ Upload.objects.create(pic=filename, user=image.user, project=image.project, imageType=6, shortfilename=shortfilename)
110
+
111
+ else:
112
+ # create final objects
113
+ shortfilename = os.path.basename(path_to_final)
114
+ filename = 'images/' + image.user.username + '/' + shortfilename
115
+ final = Upload.objects.create(pic=filename, user=image.user, project=image.project, final=1, active=1, imageType=3, shortfilename=shortfilename)
116
+ final.friend = final.id
117
+ final.save()
118
+
119
+ if path_to_cropped_image:
120
+ shortfilename = os.path.basename(path_to_cropped_image)
121
+ filename = 'images/' + image.user.username + '/' + shortfilename
122
+ Upload.objects.create(pic=filename, user=image.user, project=image.project, final=9, active=0, imageType=3, shortfilename=shortfilename, friend=final.id)
123
+
124
+ if uncertainty:
125
+ shortfilename = os.path.basename(path_to_uq)
126
+ filename = 'images/' + image.user.username + '/' + shortfilename
127
+ uncertainty_obj = Upload.objects.create(pic=filename, user=image.user, project=image.project, final=4, imageType=3, shortfilename=shortfilename, friend=final.id)
128
+
129
+ if smooth:
130
+ shortfilename = os.path.basename(path_to_smooth)
131
+ filename = 'images/' + image.user.username + '/' + shortfilename
132
+ smooth_obj = Upload.objects.create(pic=filename, user=image.user, project=image.project, final=5, imageType=3, shortfilename=shortfilename, friend=final.id)
133
+
134
+ # create allaxes warning
135
+ if dice < 0.3:
136
+ Upload.objects.create(user=image.user, project=image.project,
137
+ log=1, imageType=None, shortfilename='Bad result! Activate "All axes" if you labeled axes other than the xy-plane.')
138
+
139
+ # acwe
140
+ if not (os.path.splitext(path_to_final)[1]=='.tar' or path_to_final[-7:]=='.tar.gz'):
141
+ q = Queue('acwe', connection=Redis())
142
+ job = q.enqueue_call(init_active_contour, args=(img_id, final.id, label_id, True,), timeout=-1)
143
+ job = q.enqueue_call(init_active_contour, args=(img_id, final.id, label_id,), timeout=-1)
144
+
145
+ # cleanup
146
+ if not (os.path.splitext(path_to_final)[1]=='.tar' or path_to_final[-7:]=='.tar.gz'):
147
+ q = Queue('cleanup', connection=Redis())
148
+ job = q.enqueue_call(init_remove_outlier, args=(img_id, final.id, label_id,), timeout=-1)
149
+ if smooth:
150
+ job = q.enqueue_call(init_remove_outlier, args=(img_id, smooth_obj.id, label_id, False,), timeout=-1)
151
+
152
+ # create slices
153
+ q = Queue('slices', connection=Redis())
154
+ job = q.enqueue_call(create_slices, args=(image.pic.path, final.pic.path,), timeout=-1)
155
+ if path_to_cropped_image:
156
+ q = Queue('slices', connection=Redis())
157
+ job = q.enqueue_call(create_slices, args=(path_to_cropped_image, None,), timeout=-1)
158
+ if smooth:
159
+ job = q.enqueue_call(create_slices, args=(image.pic.path, smooth_obj.pic.path,), timeout=-1)
160
+ if uncertainty:
161
+ job = q.enqueue_call(create_slices, args=(uncertainty_obj.pic.path, None,), timeout=-1)
162
+
163
+ # send notification
164
+ send_notification(image.user.username, image.shortfilename, time_str, server_name, train, predict)
165
+
166
+ # stop processing
167
+ image.path_to_model = ''
168
+ image.status = 0
169
+ image.pid = 0
170
+ image.save()
171
+