Test Failed
Pull Request — master (#700)
by Daniil
03:23
created

savu.plugins.reconstructions.projectors.forward_projector_cpu   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 61
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A ForwardProjectorCpu.get_max_frames() 0 2 1
A ForwardProjectorCpu.new_shape() 0 5 1
A ForwardProjectorCpu.pre_process() 0 5 1
A ForwardProjectorCpu.nInput_datasets() 0 2 1
A ForwardProjectorCpu.nOutput_datasets() 0 2 1
A ForwardProjectorCpu.__init__() 0 2 1
A ForwardProjectorCpu.process_frames() 0 12 1
A ForwardProjectorCpu.setup() 0 32 2
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
    In case when angles set to None, the metadata projection geometry will be used.
36
37
    :param angles_deg: Projection angles in degrees in a format [start, stop, step]. Default: None.
38
    :param det_horiz: The size of the _horizontal_ detector. Default: 300.
39
    :param centre_of_rotation: The centre of rotation. Default: 85.5.
40
    :param out_datasets: Default out dataset names. Default: ['forw_proj']
41
    """
42
43
    def __init__(self):
44
        super(ForwardProjectorCpu, self).__init__('ForwardProjectorCpu')
45
46
    def pre_process(self):
47
        #getting metadata
48
        in_meta_data = self.get_in_meta_data()[0]
49
        self.cor = in_meta_data.get('centre_of_rotation')
50
        self.cor=self.cor[0]
51
52
    def setup(self):
53
        in_dataset, out_dataset = self.get_datasets()
54
        in_pData, out_pData = self.get_plugin_datasets()
55
        in_pData[0].plugin_data_setup('VOLUME_XZ', 'single')
56
57
        if (self.parameters['angles_deg'] is None):
58
            # data extracted geometry parameters
59
            in_meta_data=self.get_in_meta_data()[0]
60
            angles_meta_deg = in_meta_data.get('rotation_angle')
61
            self.angles_rad = np.deg2rad(angles_meta_deg)
62
            self.detectors_horiz = in_meta_data.get('detector_x')
63
        else:
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
70
        self.det_horiz_half=0.5*self.detectors_horiz
71
        self.angles_total = len(self.angles_rad)
72
73
        out_shape_sino = self.new_shape(in_dataset[0].get_shape(), in_dataset[0])
74
        labels = ['rotation_angle.degrees', 'detector_y.pixel', 'detector_x.pixel']
75
        #labels = ['x.pixels', 'proj.angles', 'y.pixels']
76
        pattern = {'name': 'SINOGRAM', 'slice_dims': (1,),
77
                   'core_dims': (2,0)}
78
        out_dataset[0].create_dataset(axis_labels=labels, shape=out_shape_sino)
79
        out_dataset[0].add_pattern(pattern['name'],
80
                                   slice_dims=pattern['slice_dims'],
81
                                   core_dims=pattern['core_dims'])
82
        out_pData[0].plugin_data_setup(pattern['name'], self.get_max_frames())
83
        out_dataset[0].meta_data.set('rotation_angle', self.angles_rad)
84
85
    def process_frames(self, data):
86
        image = data[0].astype(np.float32)
87
        image = np.where(np.isfinite(image), image, 0)
88
        objsize_image = np.shape(image)[0]
89
        RectoolsDIR = RecToolsDIR(DetectorsDimH = self.detectors_horiz,  # DetectorsDimH # detector dimension (horizontal)
90
                            DetectorsDimV = None,  # DetectorsDimV # detector dimension (vertical) for 3D case only
91
                            CenterRotOffset = -self.cor+self.det_horiz_half, # Center of Rotation (CoR) scalar
92
                            AnglesVec = self.angles_rad, # array of angles in radians
93
                            ObjSize = objsize_image, # a scalar to define reconstructed object dimensions
94
                            device_projector='cpu')
95
        sinogram_new = RectoolsDIR.FORWPROJ(image)
96
        return sinogram_new
97
98
    def new_shape(self, full_shape, data):
99
        # calculate a new output data shape based on the input data shape
100
        new_shape_sino_orig = list(full_shape)
101
        new_shape_sino= (self.angles_total, new_shape_sino_orig[1], self.detectors_horiz)
102
        return tuple(new_shape_sino)
103
104
    def get_max_frames(self):
105
        return 'single'
106
107
    def nInput_datasets(self):
108
        return 1
109
110
    def nOutput_datasets(self):
111
        return 1
112