Test Failed
Pull Request — master (#648)
by Daniil
03:44
created

savu.plugins.corrections.crop_projections   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 128
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A CropProjections.post_process() 0 2 1
A CropProjections.process_frames() 0 8 1
A CropProjections.pre_process() 0 2 1
B CropProjections.setup() 0 60 7
A CropProjections.__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:: crop_projections
17
   :platform: Unix
18
   :synopsis: A plugin to crop projections images without the need to specify
19
   preview dimensions.
20
.. moduleauthor:: Malte Storm<[email protected]>
21
"""
22
23
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
@register_plugin
29
class CropProjections(Plugin, CpuPlugin):
30
    """
31
    A plugin to apply apply a crop to projection images without the need to \
32
    specify preview dimensions. The crop will always be applied symmetrically \
33
    to the original image.
34
35
    :u*param cropX: Crop in pixels applied to the x-dimensions (on each side).\
36
    Default: 0.
37
    :u*param cropY: Crop in pixels applied to the y-dimensions (on each side).\
38
    Default: 0.
39
    :u*param mode: Select the mode: (absolute|relative|automatic). \
40
    Absolute will use the sizeX/Y parameters to determine the final size, \
41
    relative will crop the images by the amount specified in cropX/Y and \
42
    automatic will use metadata to determine the cropping size. \
43
    Default: 'absolute'.
44
    :u*param sizeX: The x-size of the cropped image in pixels. A setting of -1\
45
    means the original size will be preserved. Default: -1.
46
    :u*param sizeY: The y-size of the cropped image in pixels. A setting of -1\
47
    means the original size will be preserved. Default: -1.
48
    """
49
50
    def __init__(self):
51
        super(CropProjections, self).__init__("CropProjections")
52
53
    def pre_process(self):
54
        pass
55
56
    def process_frames(self, data):
57
        #in_dataset, out_dataset = self.get_datasets()
58
        #print(in_dataset[0].meta_data.get("indices2crop"))
59
        #indices2crop = in_dataset[0].meta_data.get('indices2crop')
60
        #shapeX = int(indices2crop[1]-indices2crop[0])
61
        #shapeY = int(indices2crop[3]-indices2crop[2])
62
        #print(shapeX, shapeY)
63
        return data[0][self.new_slice]
64
65
    def post_process(self):
66
        pass
67
68
    def setup(self):
69
        in_dataset, out_dataset = self.get_datasets()
70
        in_pData, out_pData = self.get_plugin_datasets()
71
        in_pData[0].plugin_data_setup('PROJECTION', 'single')
72
        det_y = in_dataset[0].get_data_dimension_by_axis_label('detector_y')
73
        det_x = in_dataset[0].get_data_dimension_by_axis_label('detector_x')
74
75
        self.shape = list(in_dataset[0].get_shape())
76
        self.core_dims = in_pData[0].get_core_dimensions()
77
        img_dims = self.get_in_datasets()[0].get_shape()
78
79
        if self.parameters['mode'] == 'absolute':
80
            sizeX = self.parameters['sizeX']
81
            sizeY = self.parameters['sizeY']
82
            if sizeX > 0:
83
                xslice = slice(self.shape[det_x] / 2 - sizeX / 2,
84
                self.shape[det_x] / 2 + sizeX / 2 + sizeX % 2)
85
                self.shape[det_x] = 2 * (sizeX / 2) + sizeX % 2
86
            else:
87
                xslice = slice(0, self.shape[det_x])
88
            if sizeY > 0:
89
                yslice = slice(self.shape[det_y] / 2 - sizeY / 2,
90
                self.shape[det_y] / 2 + sizeY / 2 + sizeY % 2)
91
                self.shape[det_y] = 2 * (sizeY / 2) + sizeY % 2
92
            else:
93
                yslice = slice(0, self.shape[det_y])
94
            self.new_slice = [yslice, xslice]
95
        elif self.parameters['mode'] == 'relative':
96
            cropX = self.parameters['cropX']
97
            cropY = self.parameters['cropY']
98
            self.new_slice = [slice(cropY, img_dims[det_y] - cropY),
99
                 slice(cropX, img_dims[det_x] - cropX)]
100
            self.shape[det_x] -= 2 * cropX
101
            self.shape[det_y] -= 2 * cropY
102
        elif self.parameters['mode'] == 'automatic':
103
            print(in_dataset[0].get_name())
104
            print(in_dataset[0].meta_data.get_dictionary().keys())         
105
            for key, value in in_dataset[0].meta_data.get_dictionary().iteritems():
106
                print (key, value)
107
            
108
            #print(in_dataset[0].meta_data.get('indices2crop'))
109
            # getting indices to crop the data            
110
            #sample_data[int(indices2crop[2]):int(indices2crop[3]),int(indices2crop[0]):int(indices2crop[1])]
111
            #indices2crop = in_dataset[0].meta_data.get('indices2crop')
112
            #shapeX = int(indices2crop[1]-indices2crop[0])
113
            #shapeY = int(indices2crop[3]-indices2crop[2])
114
            #self.new_slice = [slice(indices2crop[0], indices2crop[1]),
115
            #     slice(indices2crop[2], indices2crop[3])]
116
            #self.new_slice = [slice(indices2crop[2], indices2crop[3]),
117
            #     slice(indices2crop[0], indices2crop[1])]
118
            #print(shapeX, shapeY)
119
            #self.shape[det_x] = shapeX
120
            #self.shape[det_y] = shapeY
121
            #print(img_dims[det_x], img_dims[det_y])            
122
            self.new_slice = (None, None)
123
124
        out_dataset[0].create_dataset(patterns=in_dataset[0],
125
                                      axis_labels=in_dataset[0],
126
                                      shape=tuple(self.shape))
127
        out_pData[0].plugin_data_setup('PROJECTION', 'single')  
128