1
|
|
|
# Copyright 2014 Diamond Light Source Ltd. |
2
|
|
|
# |
3
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); |
4
|
|
|
# you may not use this file except in compliance with the License. |
5
|
|
|
# You may obtain a copy of the License at |
6
|
|
|
# |
7
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
8
|
|
|
# |
9
|
|
|
# Unless required by applicable law or agreed to in writing, software |
10
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
11
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12
|
|
|
# See the License for the specific language governing permissions and |
13
|
|
|
# limitations under the License. |
14
|
|
|
|
15
|
|
|
""" |
16
|
|
|
.. module:: forward_projector_cpu |
17
|
|
|
:platform: Unix |
18
|
|
|
:synopsis: A forward data projector using ToMoBAR software |
19
|
|
|
|
20
|
|
|
.. moduleauthor:: Daniil Kazantsev <[email protected]> |
21
|
|
|
""" |
22
|
|
|
|
23
|
|
|
from savu.plugins.plugin import Plugin |
24
|
|
|
from savu.plugins.driver.cpu_plugin import CpuPlugin |
25
|
|
|
from savu.plugins.utils import register_plugin |
26
|
|
|
|
27
|
|
|
from tomobar.methodsDIR import RecToolsDIR |
28
|
|
|
import numpy as np |
29
|
|
|
|
30
|
|
|
@register_plugin |
31
|
|
|
class ForwardProjectorCpu(Plugin, CpuPlugin): |
32
|
|
|
""" |
33
|
|
|
This plugin uses ToMoBAR software and CPU Astra projector underneath to generate projection data. |
34
|
|
|
The plugin will project the given object using the metadata OR user-provided parallel-beam geometry. |
35
|
|
|
|
36
|
|
|
:param angles_deg: Projection angles in degrees in a format [start, stop, step]. Default: [0.0, 180.0, 0.5]. |
37
|
|
|
:param det_horiz: The size of the _horizontal_ detector. Default: 300. |
38
|
|
|
:param centre_of_rotation: The centre of rotation. Default: 0.0. |
39
|
|
|
:param out_datasets: Default out dataset names. Default: ['forw_proj'] |
40
|
|
|
""" |
41
|
|
|
|
42
|
|
|
def __init__(self): |
43
|
|
|
super(ForwardProjectorCpu, self).__init__('ForwardProjectorCpu') |
44
|
|
|
|
45
|
|
|
#def pre_process(self): |
46
|
|
|
# getting metadata |
47
|
|
|
#in_meta_data = self.get_in_meta_data()[0] |
48
|
|
|
#self.angles_meta_deg = in_meta_data.get('rotation_angle') |
49
|
|
|
#self.angles_rad = np.deg2rad(self.angles_meta_deg) |
50
|
|
|
#self.cor = in_meta_data.get('centre_of_rotation') |
51
|
|
|
#self.cor=self.cor[0] |
52
|
|
|
#print(self.cor) |
53
|
|
|
#self.detectors_horiz = in_meta_data.get('detector_x') |
54
|
|
|
|
55
|
|
|
def setup(self): |
56
|
|
|
in_dataset, out_dataset = self.get_datasets() |
57
|
|
|
in_pData, out_pData = self.get_plugin_datasets() |
58
|
|
|
in_pData[0].plugin_data_setup('VOLUME_XZ', 'single') |
59
|
|
|
|
60
|
|
|
#print(in_dataset[0].meta_data.get("rotation_angle")) |
61
|
|
|
in_meta_data2=self.get_in_meta_data()[0] |
62
|
|
|
angles_meta_deg = in_meta_data2.get('rotation_angle') |
63
|
|
|
|
64
|
|
|
# user-set parameters |
65
|
|
|
angles_list=self.parameters['angles_deg'] |
66
|
|
|
self.cor=self.parameters['centre_of_rotation'] |
67
|
|
|
self.angles_rad = np.deg2rad(np.arange(angles_list[0], angles_list[1], angles_list[2], dtype=np.float)) |
68
|
|
|
self.detectors_horiz = self.parameters['det_horiz'] |
69
|
|
|
self.angles_total = len(self.angles_rad) |
70
|
|
|
|
71
|
|
|
out_shape_sino = self.new_shape(in_dataset[0].get_shape(), in_dataset[0]) |
72
|
|
|
label = ['x.pixels', 'proj.angles', 'y.pixels'] |
73
|
|
|
pattern = {'name': 'SINOGRAM', 'slice_dims': (1,), |
74
|
|
|
'core_dims': (0,2)} |
75
|
|
|
out_dataset[0].create_dataset(axis_labels=label, shape=out_shape_sino) |
76
|
|
|
out_dataset[0].add_pattern(pattern['name'], |
77
|
|
|
slice_dims=pattern['slice_dims'], |
78
|
|
|
core_dims=pattern['core_dims']) |
79
|
|
|
out_pData[0].plugin_data_setup(pattern['name'], self.get_max_frames()) |
80
|
|
|
out_dataset[0].meta_data.set('rotation_angle', self.angles_rad) |
81
|
|
|
|
82
|
|
|
def process_frames(self, data): |
83
|
|
|
image = data[0].astype(np.float32) |
84
|
|
|
image = np.where(np.isfinite(image), image, 0) |
85
|
|
|
objsize_image = np.shape(image)[0] |
86
|
|
|
RectoolsDIR = RecToolsDIR(DetectorsDimH = self.detectors_horiz, # DetectorsDimH # detector dimension (horizontal) |
87
|
|
|
DetectorsDimV = None, # DetectorsDimV # detector dimension (vertical) for 3D case only |
88
|
|
|
CenterRotOffset = self.cor, # Center of Rotation (CoR) scalar |
89
|
|
|
AnglesVec = self.angles_rad, # array of angles in radians |
90
|
|
|
ObjSize = objsize_image, # a scalar to define reconstructed object dimensions |
91
|
|
|
device_projector='cpu') |
92
|
|
|
sinogram_new = RectoolsDIR.FORWPROJ(image) |
93
|
|
|
return sinogram_new |
94
|
|
|
|
95
|
|
|
def new_shape(self, full_shape, data): |
96
|
|
|
# calculate a new output data shape based on the input data shape |
97
|
|
|
new_shape_sino_orig = list(full_shape) |
98
|
|
|
new_shape_sino= (self.angles_total, new_shape_sino_orig[1], self.detectors_horiz) |
99
|
|
|
return tuple(new_shape_sino) |
100
|
|
|
|
101
|
|
|
def get_max_frames(self): |
102
|
|
|
return 'single' |
103
|
|
|
|
104
|
|
|
def nInput_datasets(self): |
105
|
|
|
return 1 |
106
|
|
|
|
107
|
|
|
def nOutput_datasets(self): |
108
|
|
|
return 1 |
109
|
|
|
|