Test Failed
Pull Request — master (#875)
by Daniil
04:09
created

savu.plugins.simulation.tomo_phantom_artifacts   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 64
dl 0
loc 110
rs 10
c 0
b 0
f 0
wmc 18

6 Methods

Rating   Name   Duplication   Size   Complexity  
D TomoPhantomArtifacts.process_frames() 0 58 13
A TomoPhantomArtifacts.nOutput_datasets() 0 2 1
A TomoPhantomArtifacts.get_max_frames() 0 2 1
A TomoPhantomArtifacts.setup() 0 6 1
A TomoPhantomArtifacts.nInput_datasets() 0 2 1
A TomoPhantomArtifacts.__init__() 0 2 1
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:: tomo_phantom_artifacts
17
   :platform: Unix
18
   :synopsis: Adding artifacts to real or generated synthetic projection data using TomoPhantom
19
20
.. moduleauthor:: Daniil Kazantsev <[email protected]>
21
"""
22
23
import savu.plugins.utils as pu
24
from savu.plugins.plugin import Plugin
25
from savu.plugins.driver.cpu_plugin import CpuPlugin
26
from savu.plugins.utils import register_plugin
27
28
from tomophantom.supp.artifacts import _Artifacts_
29
import numpy as np
30
31
@register_plugin
32
class TomoPhantomArtifacts(Plugin, CpuPlugin):
33
    def __init__(self):
34
        super(TomoPhantomArtifacts, self).__init__('TomoPhantomArtifacts')
35
36
    def setup(self):
37
        in_dataset, out_dataset = self.get_datasets()
38
        out_dataset[0].create_dataset(in_dataset[0])
39
        in_pData, out_pData = self.get_plugin_datasets()
40
        in_pData[0].plugin_data_setup(self.parameters['pattern'], self.get_max_frames())
41
        out_pData[0].plugin_data_setup(self.parameters['pattern'], self.get_max_frames())
42
43
    def process_frames(self, data):
44
        proj_data = data[0]
45
46
        if self.parameters['pattern'] == 'PROJECTION':
47
            proj_data = np.expand_dims(proj_data, axis=1)
48
49
        # apply a variety of artifacts to the generated data:
50
        _noise_ = {}
51
        if self.parameters['artifacts_noise_type'] is not None:
52
            _noise_ = {'noise_type': self.parameters['artifacts_noise_type'],
53
                       'noise_amplitude': self.parameters['artifacts_noise_amplitude'],
54
                       'noise_seed': 0,
55
                       'verbose': False}
56
57
        # misalignment dictionary
58
        _datashifts_ = {}
59
        if self.parameters['datashifts_maxamplitude_pixel'] is not None:
60
            _datashifts_ = {'datashifts_maxamplitude_pixel': self.parameters['datashifts_maxamplitude_pixel']}
61
        if self.parameters['datashifts_maxamplitude_subpixel'] is not None:
62
            _datashifts_ = {'datashifts_maxamplitude_subpixel': self.parameters['datashifts_maxamplitude_subpixel']}
63
64
        # adding zingers
65
        _zingers_ = {}
66
        if self.parameters['artifacts_zingers_percentage'] is not None:
67
            _zingers_ = {'zingers_percentage': self.parameters['artifacts_zingers_percentage'],
68
                         'zingers_modulus': self.parameters['artifacts_zingers_modulus']}
69
        _stripes_ = {}
70
71
        # adding stripes
72
        if self.parameters['pattern'] == 'SINOGRAM':
73
            if self.parameters['artifacts_stripes_percentage'] is not None:
74
                _stripes_ = {'stripes_percentage': self.parameters['artifacts_stripes_percentage'],
75
                             'stripes_maxthickness': self.parameters['artifacts_stripes_maxthickness'],
76
                             'stripes_intensity': self.parameters['artifacts_stripes_intensity'],
77
                             'stripes_type': self.parameters['artifacts_stripes_type'],
78
                             'stripes_variability': self.parameters['artifacts_stripes_variability']}
79
80
        # partial volume effect dictionary
81
        _pve_ = {}
82
        if self.parameters['artifacts_pve'] is not None:
83
            _pve_ = {'pve_strength': self.parameters['artifacts_pve']}
84
85
        # fresnel propagator
86
        _fresnel_propagator_ = {}
87
        if self.parameters['artifacts_fresnel_distance'] is not None:
88
            _fresnel_propagator_ = {'fresnel_dist_observation': self.parameters['artifacts_fresnel_distance'],
89
                                    'fresnel_scale_factor': self.parameters['artifacts_fresnel_scale_factor'],
90
                                    'fresnel_wavelenght': self.parameters['artifacts_fresnel_wavelenght']}
91
92
        if (self.parameters['datashifts_maxamplitude_pixel']) or (self.parameters['datashifts_maxamplitude_subpixel']) is not None:
93
            [data_artifacts, shifts] = _Artifacts_(proj_data.copy(), **_noise_, **_zingers_, **_stripes_, **_datashifts_, **_pve_, **_fresnel_propagator_)
94
        else:
95
            data_artifacts = _Artifacts_(proj_data.copy(), **_noise_, **_zingers_, **_stripes_, **_datashifts_, **_pve_, **_fresnel_propagator_)
96
97
        if self.parameters['pattern'] == 'PROJECTION':
98
            data_artifacts = data_artifacts[:, 0, :]
0 ignored issues
show
introduced by
The variable data_artifacts does not seem to be defined for all execution paths.
Loading history...
99
100
        return data_artifacts
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