/
TomoP3D.pyx
557 lines (486 loc) · 26.3 KB
/
TomoP3D.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
"""
Cython recipe to create an interface to C-functions (3D version)
Copyright 2017 Srikanth Nagella / Daniil Kazantsev/ Edoardo Pasca
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# cython and ctypes
import cython
import ctypes
# import numpy and the Cython declarations for numpy
import numpy as np
cimport numpy as np
from numbers import Number
from enum import Enum
import sys
# declare the interface to the C code
cdef extern float TomoP3DModel_core(float *A, int ModelSelected, long N1, long N2, long N3, long Z1, long Z2, char* ModelParametersFilename)
cdef extern float TomoP3DObject_core(float *A, long N1, long N2, long N3, long Z1, long Z2, char *Object, float C0, float x0, float y0, float z0, float a, float b, float c, float psi1, float psi2, float psi3, long tt)
cdef extern float TomoP3DModelSino_core(float *A, int ModelSelected, long Horiz_det, long Vert_det, long Z1, long Z2, long N, float *Theta_proj, int AngTot, char* ModelParametersFilename)
cdef extern float TomoP3DObjectSino_core(float *A, long Horiz_det, long Vert_det, long Z1, long Z2, long N, float *Angl_vector, int AngTot, char *Object,float C0, float x0, float y0, float z0, float a, float b, float c, float psi1, float psi2, float psi3, long tt)
cdef extern float checkParams3D(int *params_switch, int ModelSelected, char *ModelParametersFilename)
cdef packed struct object_3d:
char[22] Obj
np.float32_t C0
np.float32_t x0
np.float32_t y0
np.float32_t z0
np.float32_t a
np.float32_t b
np.float32_t c
np.float32_t psi1
class Objects3D(Enum):
'''Enumeration with the available objects for 3D phantoms'''
GAUSSIAN = 'gaussian'
PARABOLOID = 'paraboloid'
ELLIPSOID = 'ellipsoid'
CONE = 'cone'
CUBOID = 'cuboid'
ELLIPCYLINDER = 'elliptical_cylinder'
@cython.boundscheck(False)
@cython.wraparound(False)
def Model(int model_id, phantom_size, str model_parameters_filename):
"""
Create stationary 3D model : Model(model_id, phantom_size, model_parameters_filename)
Takes in an input model_id and phantom_size as a scalar or a tuple and returns a phantom-model (3D) of phantom_size of type float32 numpy array.
param: model_parameters_filename -- filename for the model parameters
param: model_id -- a model id from the functions file
param: phantom_size -- a scalar or a tuple with phantom dimesnsions. Can be phantom_size[1] (a scalar for the cubic phantom), or phantom_size[3] = [N1,N2,N3]
returns: numpy float32 phantom array
"""
cdef long N1,N2,N3
if type(phantom_size) == tuple:
N1,N2,N3 = [int(i) for i in phantom_size]
else:
N1 = N2 = N3 = phantom_size
cdef np.ndarray[np.float32_t, ndim=3, mode="c"] phantom = np.zeros([N1, N2, N3], dtype='float32')
cdef float ret_val
py_byte_string = model_parameters_filename.encode('UTF-8')
cdef char* c_string = py_byte_string
cdef np.ndarray[int, ndim=1, mode="c"] params
params = np.ascontiguousarray(np.zeros([12], dtype=ctypes.c_int))
checkParams3D(¶ms[0], model_id, c_string)
testParams3D(params) # check parameters and terminate before running the core
if params[3] == 1:
ret_val = TomoP3DModel_core(&phantom[0,0,0], model_id, N3, N2, N1, 0l, N1, c_string)
else:
print("The selected model is temporal (4D), use 'ModelTemporal' function instead")
return phantom
@cython.boundscheck(False)
@cython.wraparound(False)
def ModelSub(int model_id, phantom_size, sub_index, str model_parameters_filename):
"""
Create a subset (vertical cutoff) of stationary 3D model : Model(model_id, phantom_size, sub_index, model_parameters_filename)
Takes in an input model_id and phantom_size as a scalar or a tuple, then a sub_index a tuple defines selected vertical subset
and returns a phantom-model (3D) of phantom_size of type float32 numpy array.
param: model_id -- a model id from the functions file
param: sub_index -- a tuple containing 2 indeces [lower, upper] to select a vertical subset needed to be extracted
param: phantom_size -- a scalar or a tuple with phantom dimesnsions. Can be phantom_size[1] (a scalar for the cubic phantom), or phantom_size[3] = [N1,N2,N3]
param: model_parameters_filename -- filename for the model parameters
returns: numpy float32 phantom array
"""
cdef long N1,N2,N3,Z1,Z2
if type(phantom_size) == tuple:
N1,N2,N3 = [int(i) for i in phantom_size]
else:
N1 = N2 = N3 = phantom_size
Z1,Z2 = [int(i) for i in sub_index]
rangecheck = Z2 > Z1
if not rangecheck:
raise ValueError('Upper index must be larger than the lower one')
rangecheck = Z1 >= 0 and Z1 < N3
if not rangecheck:
raise ValueError('Range of the lower index is incorrect')
rangecheck = Z2 >= 0 and Z2 <= N3
if not rangecheck:
raise ValueError('Range of the higher index is incorrect')
sub_size = Z2 - Z1 # the size of the vertical slab
cdef np.ndarray[np.float32_t, ndim=3, mode="c"] phantom = np.zeros([sub_size, N2, N3], dtype='float32')
cdef float ret_val
py_byte_string = model_parameters_filename.encode('UTF-8')
cdef char* c_string = py_byte_string
cdef np.ndarray[int, ndim=1, mode="c"] params
params = np.ascontiguousarray(np.zeros([12], dtype=ctypes.c_int))
checkParams3D(¶ms[0], model_id, c_string)
testParams3D(params) # check parameters and terminate before running the core
if params[3] == 1:
ret_val = TomoP3DModel_core(&phantom[0,0,0], model_id, N3, N2, N1, Z1, Z2, c_string)
else:
print("The selected model is temporal (4D), use 'ModelTemporal' function instead")
return phantom
@cython.boundscheck(False)
@cython.wraparound(False)
def ModelTemporal(int model_id, phantom_size, str model_parameters_filename):
"""
Create temporal 4D (3D + time) model :Model(model_id, phantom_size,model_parameters_filename)
Takes in a input model_id and phantom_size and returns a phantom-model (4D) of tuple size x Time-frames of type float32 numpy array.
param: model_parameters_filename -- filename for the model parameters
param: model_id -- a model id from the functions file
param: phantom_size -- a scalar or a tuple with phantom dimesnsions. Can be phantom_size[1] (a scalar for the cubic phantom), or phantom_size[3] = [N1,N2,N3]
returns: numpy float32 phantom array
"""
cdef long N1,N2,N3
if type(phantom_size) == tuple:
N1,N2,N3 = [int(i) for i in phantom_size]
else:
N1 = N2 = N3 = phantom_size
cdef float ret_val
py_byte_string = model_parameters_filename.encode('UTF-8')
cdef char* c_string = py_byte_string
cdef np.ndarray[int, ndim=1, mode="c"] params
params = np.ascontiguousarray(np.zeros([12], dtype=ctypes.c_int))
checkParams3D(¶ms[0], model_id, c_string)
testParams3D(params) # check parameters and terminate before running the core
cdef np.ndarray[np.float32_t, ndim=4, mode="c"] phantom = np.zeros([params[3], N1, N2, N3], dtype='float32')
if params[3] == 1:
print("The selected model is stationary (3D), use 'Model' function instead")
else:
ret_val = TomoP3DModel_core(&phantom[0,0,0,0], model_id, N3, N2, N1, 0l, N1, c_string)
return phantom
@cython.boundscheck(False)
@cython.wraparound(False)
def ModelTemporalSub(int model_id, phantom_size, sub_index, str model_parameters_filename):
"""
Create a subset of temporal 4D (3D + time) model: Model(model_id, phantom_size, sub_index, model_parameters_filename)
Takes in a input model_id. phantom_size, sub_index a tuple defines selected vertical subset and returns a phantom-model (4D) of tuple size x Time-frames of type float32 numpy array.
param: model_parameters_filename -- filename for the model parameters
param: phantom_size -- a scalar or a tuple with phantom dimesnsions. Can be phantom_size[1] (a scalar for the cubic phantom), or phantom_size[3] = [N1,N2,N3]
param: sub_index -- a tuple containing 2 indeces [lower, upper] to select a vertical subset needed to be extracted
param: model_id -- a model id from the functions file
returns: numpy float32 phantom array
"""
cdef long N1,N2,N3
if type(phantom_size) == tuple:
N1,N2,N3 = [int(i) for i in phantom_size]
else:
N1 = N2 = N3 = phantom_size
Z1,Z2 = [int(i) for i in sub_index]
rangecheck = Z2 > Z1
if not rangecheck:
raise ValueError('Upper index must be larger than the lower one')
rangecheck = Z1 >= 0 and Z1 < N3
if not rangecheck:
raise ValueError('Range of the lower index is incorrect')
rangecheck = Z2 >= 0 and Z2 <= N3
if not rangecheck:
raise ValueError('Range of the higher index is incorrect')
sub_size = Z2 - Z1 # the size of the vertical slab
cdef float ret_val
py_byte_string = model_parameters_filename.encode('UTF-8')
cdef char* c_string = py_byte_string
cdef np.ndarray[int, ndim=1, mode="c"] params
params = np.ascontiguousarray(np.zeros([12], dtype=ctypes.c_int))
checkParams3D(¶ms[0], model_id, c_string)
testParams3D(params) # check parameters and terminate before running the core
cdef np.ndarray[np.float32_t, ndim=4, mode="c"] phantom = np.zeros([params[3], sub_size, N2, N3], dtype='float32')
if params[3] == 1:
print("The selected model is stationary (3D), use 'Model' function instead")
else:
ret_val = TomoP3DModel_core(&phantom[0,0,0,0], model_id, N3, N2, N1, Z1, Z2, c_string)
return phantom
@cython.boundscheck(False)
@cython.wraparound(False)
def Object(phantom_size, objlist):
"""
Object (phantom_size,objlist)
Takes in a input object description (list) and phantom_size and returns a phantom-object (3D) of a tuple size of type float32 numpy array.
param: obj_params -- object parameters list
param: phantom_size -- a scalar or a tuple with phantom dimesnsions. Can be phantom_size[1] (a scalar for the cubic phantom), or phantom_size[3] = [N1,N2,N3]
returns: numpy float32 phantom array
"""
cdef long N1,N2,N3
if type(phantom_size) == tuple:
N1,N2,N3 = [int(i) for i in phantom_size]
else:
N1 = N2 = N3 = phantom_size
cdef long tt,Z1
cdef float C0, y0, x0, z0, a, b, c, phi1, phi2, phi3
tt=0
Z1=0
cdef np.ndarray[np.float32_t, ndim=3, mode="c"] phantom = np.zeros([N1, N2, N3], dtype='float32')
cdef float ret_val
if type(objlist) is dict:
objlist = [objlist]
for obj in objlist:
if testParamsPY(obj):
if sys.version_info.major == 3:
objectName = bytes(obj['Obj'].value, 'ascii')
elif sys.version_info.major == 2:
objectName = bytes(obj['Obj'].value)
C0 = obj['C0']
x0 = obj['x0']
y0 = obj['y0']
z0 = obj['z0']
a = obj['a']
b = obj['b']
c = obj['c']
phi1 = obj['phi1']
phi2 = 0.0
phi3 = 0.0
ret_val = TomoP3DObject_core(&phantom[0,0,0], N3, N2, N1, Z1, N1,
objectName,
C0,y0,x0,z0,a,b,c,
phi1,phi2,phi3,tt)
return phantom
@cython.boundscheck(False)
@cython.wraparound(False)
def ModelSino(int model_id, phantom_size, Horiz_det, Vert_det, np.ndarray[np.float32_t, ndim=1, mode="c"] angles, str model_parameters_filename):
"""
Creates 3D analytical projection data of the dimension [AngTot, Vert_det, Horiz_det]
Takes in a input model_id. phantom_size, detector sizes, array of projection angles
param: model_id -- a model id from the functions file
param: phantom_size -- a scalar or a tuple with phantom dimesnsions. Can be phantom_size[1] (a scalar for the cubic phantom)
param: Horiz_det -- horizontal detector size
param: Vert_det -- vertical detector size (currently Vert_det = phantom_size)
params: angles -- 1D array of projection angles in degrees
param: model_parameters_filename -- filename for the model parameters
returns: numpy float32 phantom array (3D)
"""
if type(phantom_size) == tuple:
raise ValueError('Please give a scalar for phantom size, projection data cannot be obtained from non-cubic phantom')
cdef int AngTot = angles.shape[0]
cdef np.ndarray[np.float32_t, ndim=3, mode="c"] projdata = np.zeros([AngTot, Vert_det, Horiz_det], dtype='float32')
cdef float ret_val
py_byte_string = model_parameters_filename.encode('UTF-8')
cdef char* c_string = py_byte_string
cdef np.ndarray[int, ndim=1, mode="c"] params
params = np.ascontiguousarray(np.zeros([12], dtype=ctypes.c_int))
checkParams3D(¶ms[0], model_id, c_string)
testParams3D(params) # check parameters and terminate before running the core
if params[3] == 1:
ret_val = TomoP3DModelSino_core(&projdata[0,0,0], model_id, Horiz_det, Vert_det, 0l, Vert_det, phantom_size, &angles[0], AngTot, c_string)
else:
print("The selected model is temporal (4D), use 'ModelTemporalSino' function instead")
return np.swapaxes(projdata,0,1)
@cython.boundscheck(False)
@cython.wraparound(False)
def ModelSinoTemporal(int model_id, phantom_size, Horiz_det, Vert_det, np.ndarray[np.float32_t, ndim=1, mode="c"] angles, str model_parameters_filename):
"""
Creates 4D (3D + time) analytical projection data of the dimension [TimeFrames, AngTot, Vert_det, Horiz_det]
Takes in a input model_id. phantom_size, detector sizes, array of projection angles
param: model_id -- a model id from the functions file
param: phantom_size -- a scalar or a tuple with phantom dimesnsions. Can be phantom_size[1] (a scalar for the cubic phantom)
param: Horiz_det -- horizontal detector size
param: Vert_det -- vertical detector size (currently Vert_det = phantom_size)
params: angles -- 1D array of projection angles in degrees
param: model_parameters_filename -- filename for the model parameters
returns: numpy float32 phantom array (4D)
"""
if type(phantom_size) == tuple:
raise ValueError('Please give a scalar for phantom size, projection data cannot be obtained from non-cubic phantom')
cdef int AngTot = angles.shape[0]
cdef float ret_val
py_byte_string = model_parameters_filename.encode('UTF-8')
cdef char* c_string = py_byte_string
cdef np.ndarray[int, ndim=1, mode="c"] params
params = np.ascontiguousarray(np.zeros([12], dtype=ctypes.c_int))
checkParams3D(¶ms[0], model_id, c_string)
testParams3D(params) # check parameters and terminate before running the core
cdef np.ndarray[np.float32_t, ndim=4, mode="c"] projdata = np.zeros([params[3], AngTot, Vert_det, Horiz_det], dtype='float32')
if params[3] == 1:
print("The selected model is stationary (3D), use 'ModelSino' function instead")
else:
ret_val = TomoP3DModelSino_core(&projdata[0,0,0,0], model_id, Horiz_det, Vert_det, 0l, Vert_det, phantom_size, &angles[0], AngTot, c_string)
return np.swapaxes(projdata,1,2)
@cython.boundscheck(False)
@cython.wraparound(False)
def ModelSinoSub(int model_id, phantom_size, Horiz_det, Vert_det, sub_index, np.ndarray[np.float32_t, ndim=1, mode="c"] angles, str model_parameters_filename):
"""
Creates 3D analytical projection data of the dimension [AngTot, Subset_size, Horiz_det]
sub_index - defines a subset of the vertical detector dimension
Takes in a input model_id. phantom_size, detector sizes, array of projection angles
param: model_id -- a model id from the functions file
param: phantom_size -- a scalar or a tuple with phantom dimesnsions. Can be phantom_size[1] (a scalar for the cubic phantom)
param: Horiz_det -- horizontal detector size
param: Vert_det -- vertical detector size (currently Vert_det = phantom_size)
params: angles -- 1D array of projection angles in degrees
param: model_parameters_filename -- filename for the model parameters
returns: numpy float32 phantom array (3D)
"""
if type(phantom_size) == tuple:
raise ValueError('Please give a scalar for phantom size, projection data cannot be obtained from non-cubic phantom')
cdef int AngTot = angles.shape[0]
Z1,Z2 = [int(i) for i in sub_index]
rangecheck = Z2 > Z1
if not rangecheck:
raise ValueError('Upper index must be larger than the lower one')
rangecheck = Z1 >= 0 and Z1 < Vert_det
if not rangecheck:
raise ValueError('Range of the lower index is incorrect')
rangecheck = Z2 >= 0 and Z2 <= Vert_det
if not rangecheck:
raise ValueError('Range of the higher index is incorrect')
sub_size = Z2 - Z1 # the size of the vertical slab
cdef np.ndarray[np.float32_t, ndim=3, mode="c"] projdata = np.zeros([AngTot, sub_size, Horiz_det], dtype='float32')
cdef float ret_val
py_byte_string = model_parameters_filename.encode('UTF-8')
cdef char* c_string = py_byte_string
cdef np.ndarray[int, ndim=1, mode="c"] params
params = np.ascontiguousarray(np.zeros([12], dtype=ctypes.c_int))
checkParams3D(¶ms[0], model_id, c_string)
testParams3D(params) # check parameters and terminate before running the core
if params[3] == 1:
ret_val = TomoP3DModelSino_core(&projdata[0,0,0], model_id, Horiz_det, Vert_det, Z1, Z2, phantom_size, &angles[0], AngTot, c_string)
else:
print("The selected model is temporal (4D), use 'ModelTemporalSino' function instead")
return np.swapaxes(projdata,0,1)
@cython.boundscheck(False)
@cython.wraparound(False)
def ModelSinoTemporalSub(int model_id, phantom_size, Horiz_det, Vert_det, sub_index, np.ndarray[np.float32_t, ndim=1, mode="c"] angles, str model_parameters_filename):
"""
Creates 4D analytical projection data of the dimension [AngTot, Subset_size, Horiz_det]
sub_index - defines a subset of the vertical detector dimension
Takes in a input model_id. phantom_size, detector sizes, array of projection angles
param: model_id -- a model id from the functions file
param: phantom_size -- a scalar or a tuple with phantom dimesnsions. Can be phantom_size[1] (a scalar for the cubic phantom)
param: Horiz_det -- horizontal detector size
param: Vert_det -- vertical detector size (currently Vert_det = phantom_size)
params: angles -- 1D array of projection angles in degrees
param: model_parameters_filename -- filename for the model parameters
returns: numpy float32 phantom array (3D)
"""
if type(phantom_size) == tuple:
raise ValueError('Please give a scalar for phantom size, projection data cannot be obtained from non-cubic phantom')
cdef int AngTot = angles.shape[0]
Z1,Z2 = [int(i) for i in sub_index]
rangecheck = Z2 > Z1
if not rangecheck:
raise ValueError('Upper index must be larger than the lower one')
rangecheck = Z1 >= 0 and Z1 < Vert_det
if not rangecheck:
raise ValueError('Range of the lower index is incorrect')
rangecheck = Z2 >= 0 and Z2 <= Vert_det
if not rangecheck:
raise ValueError('Range of the higher index is incorrect')
sub_size = Z2 - Z1 # the size of the vertical slab
cdef float ret_val
py_byte_string = model_parameters_filename.encode('UTF-8')
cdef char* c_string = py_byte_string
cdef np.ndarray[int, ndim=1, mode="c"] params
params = np.ascontiguousarray(np.zeros([12], dtype=ctypes.c_int))
checkParams3D(¶ms[0], model_id, c_string)
testParams3D(params) # check parameters and terminate before running the core
cdef np.ndarray[np.float32_t, ndim=4, mode="c"] projdata = np.zeros([params[3], AngTot, sub_size, Horiz_det], dtype='float32')
if params[3] == 1:
print("The selected model is stationary (3D), use 'ModelSino' function instead")
else:
ret_val = TomoP3DModelSino_core(&projdata[0,0,0,0], model_id, Horiz_det, Vert_det, Z1, Z2, phantom_size, &angles[0], AngTot, c_string)
return np.swapaxes(projdata,1,2)
@cython.boundscheck(False)
@cython.wraparound(False)
def ObjectSino(phantom_size, Horiz_det, Vert_det, np.ndarray[np.float32_t, ndim=1, mode="c"] angles, objlist):
"""
Creates 3D analytical projection data of the dimension [AngTot, Vert_det, Horiz_det]
Takes in a input model_id. phantom_size, detector sizes, array of projection angles
param: phantom_size -- a scalar or a tuple with phantom dimesnsions. Can be phantom_size[1] (a scalar for the cubic phantom)
param: Horiz_det -- horizontal detector size
param: Vert_det -- vertical detector size (currently Vert_det = phantom_size)
params: angles -- 1D array of projection angles in degrees
param: objlist -- a list of parameters for the object
returns: numpy float32 phantom array (3D)
"""
if type(phantom_size) == tuple:
raise ValueError('Please give a scalar for phantom size, projection data cannot be obtained from non-cubic phantom')
cdef int AngTot = angles.shape[0]
cdef np.ndarray[np.float32_t, ndim=3, mode="c"] projdata = np.zeros([AngTot, Vert_det, Horiz_det], dtype='float32')
cdef float ret_val
if type(objlist) is dict:
objlist = [objlist]
cdef long tt, Z1
cdef float C0, y0, x0, z0, a, b, c, phi1, phi2, phi3
tt=0
Z1=0
for obj in objlist:
if testParamsPY(obj):
if sys.version_info.major == 3:
objectName = bytes(obj['Obj'].value, 'ascii')
elif sys.version_info.major == 2:
objectName = bytes(obj['Obj'].value)
C0 = obj['C0']
x0 = obj['x0']
y0 = obj['y0']
z0 = obj['z0']
a = obj['a']
b = obj['b']
c = obj['c']
phi1 = obj['phi1']
phi2 = obj['phi1']
phi3 = obj['phi1']
if (("gaussian" in str(objectName)) or ("paraboloid" in str(objectName)) or ("ellipsoid" in str(objectName))):
ret_val = TomoP3DObjectSino_core(&projdata[0,0,0], (Horiz_det), (Vert_det), Z1, (Vert_det), (phantom_size), &angles[0], AngTot,
objectName,
C0, y0, -z0, -x0, b, a, c, phi3, phi2, phi1, tt)
elif ("elliptical_cylinder" in str(objectName)):
ret_val = TomoP3DObjectSino_core(&projdata[0,0,0], (Horiz_det), (Vert_det), Z1, (Vert_det), (phantom_size), &angles[0], AngTot,
objectName,
C0, x0, -y0, z0, b, a, c, phi3, phi2, phi1, tt)
else:
ret_val = TomoP3DObjectSino_core(&projdata[0,0,0], (Horiz_det), (Vert_det), Z1, (Vert_det), (phantom_size), &angles[0], AngTot,
objectName,
C0, x0, y0, z0, a, b, c, phi3, phi2, -phi1, tt)
return np.swapaxes(projdata,0,1)
@cython.boundscheck(False)
@cython.wraparound(False)
def testParamsPY(obj):
'''Performs a simple type check of the input parameters and a range check'''
if not type(obj) is dict:
raise TypeError('obj is not a dict {0}'.format(type(obj)))
# type check
for k,v in obj.items():
if not isinstance(v, Number):
if not k == 'Obj':
raise TypeError(k, 'is not a Number')
typecheck = True
# range check
rangecheck = obj['x0'] >= -1 and obj['x0'] <= 1
if not rangecheck:
raise ValueError('x0 is out of range. Must be between -1 and 1')
rangecheck = rangecheck and obj['y0'] >= -1 and obj['y0'] <= 1
if not rangecheck:
raise ValueError('y0 is out of range. Must be between -1 and 1')
rangecheck = rangecheck and obj['z0'] >= -1 and obj['z0'] <= 1
if not rangecheck:
raise ValueError('z0 is out of range. Must be between -1 and 1')
rangecheck = rangecheck and obj['a'] > 0 and obj['a'] <= 2
if not rangecheck:
raise ValueError('a (object size) must be positive in [0,2] range')
rangecheck = rangecheck and obj['b'] > 0 and obj['b'] <= 2
if not rangecheck:
raise ValueError('b (object size) must be positive in [0,2] range')
rangecheck = rangecheck and obj['c'] > 0 and obj['c'] <= 2
if not rangecheck:
raise ValueError('c (object size) must be positive in [0,2] range')
return rangecheck and typecheck
def testParams3D(obj):
if obj[0] == 0:
raise TypeError('Check if the library file <Phantom3DLibrary.dat> exists, the given path is correct and the syntax is valid')
if obj[1] == 0:
raise TypeError('The given model is not found, check available models in <Phantom3DLibrary.dat> file')
if obj[2] == 0:
raise TypeError('Components number cannot be negative, check <Phantom3DLibrary.dat> file')
if obj[3] == 0:
raise TypeError('TimeSteps cannot be negative, check <Phantom3DLibrary.dat> file')
if obj[4] == 0:
raise TypeError('Unknown name of the object, check <Phantom3DLibrary.dat> file')
if obj[5] == 0:
raise TypeError('C0 should not be equal to zero, check <Phantom3DLibrary.dat> file')
if obj[6] == 0:
raise TypeError('x0 (object position) must be in [-1,1] range, check <Phantom3DLibrary.dat> file')
if obj[7] == 0:
raise TypeError('y0 (object position) must be in [-1,1] range, check <Phantom3DLibrary.dat> file')
if obj[8] == 0:
raise TypeError('z0 (object position) must be in [-1,1] range, check <Phantom3DLibrary.dat> file')
if obj[9] == 0:
raise TypeError('a (object size) must be positive in [0,2] range, check <Phantom3DLibrary.dat> file')
if obj[10] == 0:
raise TypeError('b (object size) must be positive in [0,2] range, check <Phantom3DLibrary.dat> file')
if obj[11] == 0:
raise TypeError('c (object size) must be positive in [0,2] range, check <Phantom3DLibrary.dat> file')
return 0