Test Setup Failed
Push — master ( cdf983...92b8ea )
by Nicola
02:11 queued 17s
created

savu.plugins.reconstructions.projectors.forward_projector_cpu   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 106
Duplicated Lines 71.7 %

Importance

Changes 0
Metric Value
wmc 9
eloc 66
dl 76
loc 106
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A ForwardProjectorCpu.get_max_frames() 2 2 1
A ForwardProjectorCpu.new_shape() 5 5 1
A ForwardProjectorCpu.pre_process() 5 5 1
A ForwardProjectorCpu.nInput_datasets() 2 2 1
A ForwardProjectorCpu.nOutput_datasets() 2 2 1
A ForwardProjectorCpu.__init__() 2 2 1
A ForwardProjectorCpu.process_frames() 12 12 1
A ForwardProjectorCpu.setup() 36 36 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
@register_plugin
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
31
class ForwardProjectorCpu(Plugin, CpuPlugin):
32
33
    def __init__(self):
34
        super(ForwardProjectorCpu, self).__init__('ForwardProjectorCpu')
35
36
    def pre_process(self):
37
        #getting metadata
38
        in_meta_data = self.get_in_meta_data()[0]
39
        self.cor = in_meta_data.get('centre_of_rotation')
40
        self.cor=self.cor[0]
41
42
    def setup(self):
43
        in_dataset, out_dataset = self.get_datasets()
44
        in_pData, out_pData = self.get_plugin_datasets()
45
        in_pData[0].plugin_data_setup('VOLUME_XZ', 'single')
46
47
        if (self.parameters['angles_deg'] is None):
48
            # data extracted geometry parameters
49
            in_meta_data=self.get_in_meta_data()[0]
50
            angles_meta_deg = in_meta_data.get('rotation_angle')
51
            self.angles_rad = np.deg2rad(angles_meta_deg)
52
            self.detectors_horiz = in_meta_data.get('detector_x_length')
53
        else:
54
            # user-set parameters
55
            angles_list=self.parameters['angles_deg']
56
            self.cor=self.parameters['centre_of_rotation']
57
            self.angles_rad = np.deg2rad(np.linspace(angles_list[0], angles_list[1], angles_list[2], dtype=np.float))
58
            self.detectors_horiz = self.parameters['det_horiz']
59
60
        self.det_horiz_half=0.5*self.detectors_horiz
61
        self.angles_total = len(self.angles_rad)
62
63
        out_shape_sino = self.new_shape(in_dataset[0].get_shape(), in_dataset[0])
64
        labels = ['rotation_angle.degrees', 'detector_y.pixel', 'detector_x.pixel']
65
        pattern = {'name': 'SINOGRAM', 'slice_dims': (1,),
66
                   'core_dims': (2,0)}
67
        out_dataset[0].create_dataset(axis_labels=labels, shape=out_shape_sino)
68
        out_dataset[0].add_pattern(pattern['name'],
69
                                   slice_dims=pattern['slice_dims'],
70
                                   core_dims=pattern['core_dims'])
71
        pattern2 = {'name': 'PROJECTION', 'slice_dims': (0,),
72
                   'core_dims': (1,2)}
73
        out_dataset[0].add_pattern(pattern2['name'],
74
                                   slice_dims=pattern['slice_dims'],
75
                                   core_dims=pattern['core_dims'])
76
        out_pData[0].plugin_data_setup(pattern['name'], self.get_max_frames())
77
        out_dataset[0].meta_data.set('rotation_angle', angles_meta_deg)
0 ignored issues
show
introduced by
The variable angles_meta_deg does not seem to be defined for all execution paths.
Loading history...
78
79
    def process_frames(self, data):
80
        image = data[0].astype(np.float32)
81
        image = np.where(np.isfinite(image), image, 0)
82
        objsize_image = np.shape(image)[0]
83
        RectoolsDIR = RecToolsDIR(DetectorsDimH = self.detectors_horiz,  # DetectorsDimH # detector dimension (horizontal)
84
                            DetectorsDimV = None,  # DetectorsDimV # detector dimension (vertical) for 3D case only
85
                            CenterRotOffset = -self.cor+self.det_horiz_half-0.5, # Center of Rotation (CoR) scalar
86
                            AnglesVec = self.angles_rad, # array of angles in radians
87
                            ObjSize = objsize_image, # a scalar to define reconstructed object dimensions
88
                            device_projector='cpu')
89
        sinogram_new = RectoolsDIR.FORWPROJ(image)
90
        return sinogram_new
91
92
    def new_shape(self, full_shape, data):
93
        # calculate a new output data shape based on the input data shape
94
        new_shape_sino_orig = list(full_shape)
95
        new_shape_sino= (self.angles_total, new_shape_sino_orig[1], self.detectors_horiz)
96
        return tuple(new_shape_sino)
97
98
    def get_max_frames(self):
99
        return 'single'
100
101
    def nInput_datasets(self):
102
        return 1
103
104
    def nOutput_datasets(self):
105
        return 1
106