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

savu.plugins.reconstructions.projectors.forward_projector_cpu   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

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

8 Methods

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