Test Failed
Push — master ( 15d240...157f6a )
by Daniil
01:26 queued 18s
created

examples.plugin_examples.plugin_templates.general.plugin_template3   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 109
Duplicated Lines 49.54 %

Importance

Changes 0
Metric Value
eloc 46
dl 54
loc 109
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A PluginTemplate3.pre_process() 0 2 1
A PluginTemplate3.process_frames() 0 3 1
A PluginTemplate3.post_process() 0 2 1
A PluginTemplate3.nOutput_datasets() 0 2 1
A PluginTemplate3.__init__() 0 2 1
A PluginTemplate3.nInput_datasets() 0 2 1
A PluginTemplate3.setup() 54 54 3

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:: plugin_template3
17
   :platform: Unix
18
   :synopsis: A template to create a plugin that reduces the data dimensions.
19
20
.. moduleauthor:: Developer Name <[email protected]>
21
22
"""
23
24
import copy
25
import numpy as np
26
27
from savu.plugins.plugin import Plugin
28
from savu.plugins.utils import register_plugin
29
from savu.plugins.driver.cpu_plugin import CpuPlugin
30
31
32
@register_plugin
33
class PluginTemplate3(Plugin, CpuPlugin):
34
35
    def __init__(self):
36
        super(PluginTemplate3, self).__init__('PluginTemplate3')
37
38
    def nInput_datasets(self):
39
        return 1
40
41
    def nOutput_datasets(self):
42
        return 1
43
44 View Code Duplication
    def setup(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
45
        in_dataset, out_dataset = self.get_datasets()
46
47
        #=================== populate output dataset ==========================
48
        # Due to the reduction in dimensions, the out_dataset will have
49
        # different axis_labels, patterns and shape to the in_dataset and
50
        # these will need to be defined.
51
        # For more information about the syntax used here see:
52
        # http://savu.readthedocs.io/en/latest/api_plugin/savu.data.data_structures.data_create
53
54
        # AMEND THE PATTERNS: The output dataset will have one dimension less
55
        # than the in_dataset, so remove the final slice dimension from any
56
        # patterns you want to keep.
57
        rm_dim = str(in_dataset[0].get_data_patterns()
58
                     ['SINOGRAM']['slice_dims'][-1])
59
        patterns = ['SINOGRAM.' + rm_dim, 'PROJECTION.' + rm_dim]
60
61
        # AMEND THE AXIS LABELS: Find the dimensions to remove using their
62
        # axis_labels to ensure the plugin is as generic as possible and will
63
        # work for data in all orientations.
64
        axis_labels = copy.copy(in_dataset[0].get_axis_labels())
65
        rm_labels = ['detector_x', 'detector_y']
66
        rm_dims = sorted([in_dataset[0].get_data_dimension_by_axis_label(a)
67
                          for a in rm_labels])[::-1]
68
        for d in rm_dims:
69
            del axis_labels[d]
70
        # Add a new axis label to the list
71
        axis_labels.append({'Q': 'Angstrom^-1'})
72
73
        # AMEND THE SHAPE: Remove the two unrequired dimensions from the
74
        # original shape and add a new dimension shape.
75
        shape = list(in_dataset[0].get_shape())
76
        for d in rm_dims:
77
            del shape[d]
78
        shape += (self.get_parameters('num_bins'),)
79
80
        # populate the output dataset
81
        out_dataset[0].create_dataset(
82
                patterns={in_dataset[0]: patterns},
83
                axis_labels=axis_labels,
84
                shape=tuple(shape))
85
86
        # ASSOCIATE AN EXTRA PATTERN WITH THE DATASET: SINOGRAM and PROJECTION
87
        # patterns are already asssociated with the output dataset, but add 
88
        # another one.
89
        spectrum = \
90
            {'core_dims': (-1,), 'slice_dims': tuple(range(len(shape)-1))}
91
        out_dataset[0].add_pattern("SPECTRUM", **spectrum)
92
        #======================================================================
93
94
        #================== populate plugin datasets ==========================
95
        in_pData, out_pData = self.get_plugin_datasets()
96
        in_pData[0].plugin_data_setup('DIFFRACTION', 'single')
97
        out_pData[0].plugin_data_setup('SPECTRUM', 'single')
98
        #======================================================================
99
100
    def pre_process(self):
101
        pass
102
103
    def process_frames(self, data):
104
        # do some processing here
105
        return np.arange(self.parameters['num_bins'])
106
107
    def post_process(self):
108
        pass
109