Test Failed
Pull Request — master (#934)
by
unknown
04:09
created

Rotate90.__init__()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
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:: rotate_90
17
   :platform: Unix
18
   :synopsis: (Change this) A template to create a simple plugin that takes 
19
    one dataset as input and returns a similar dataset as output.
20
21
.. moduleauthor:: Jacob Williamson <[email protected]>
22
"""
23
from copy import deepcopy
24
25
from savu.plugins.utils import register_plugin
26
from savu.plugins.plugin import Plugin
27
from savu.plugins.driver.cpu_plugin import CpuPlugin
28
from savu.core.iterate_plugin_group_utils import enable_iterative_loop, \
29
    check_if_end_plugin_in_iterate_group, setup_extra_plugin_data_padding
30
from savu.data.data_structures.data_types.data_plus_darks_and_flats \
31
    import ImageKey
32
33
import numpy as np
34
import h5py
35
@register_plugin
36
class Rotate90(Plugin, CpuPlugin):
37
# Each class must inherit from the Plugin class and a driver
38
39
    def __init__(self):
40
        super(Rotate90, self).__init__("Rotate90")
41
42
    def nInput_datasets(self):
43
        return 1
44
45
46
    def nOutput_datasets(self):
47
        return 1
48
49
50
    def setup(self):
51
52
        # assumes 3D data and 2D frames
53
54
        if self.exp.meta_data.get("pre_run"):
55
            self.stats_obj.calc_stats = False
56
57
        in_dataset, out_dataset = self.get_datasets()
58
        pattern = self.parameters["pattern"]
59
        data_info = in_dataset[0].data_info
60
        core_dims = data_info["data_patterns"][pattern]["core_dims"]
61
        c0, c1 = core_dims[0], core_dims[1]                        # core dimensions
62
        s0 = data_info["data_patterns"][pattern]["slice_dims"][0]  # slice dimension
63
64
        # swapping round core dimensions in the shape due to rotation
65
        new_shape = list(data_info["shape"])
66
        new_shape[c0], new_shape[c1] = data_info["shape"][c1], data_info["shape"][c0]
67
        if self.exp.meta_data.get("pre_run"):
68
            new_shape[0] = in_dataset[0].data.image_key.shape[0]
69
        new_shape = tuple(new_shape)
70
71
        # swapping round core dimensions in axis labels
72
        new_axis_labels = deepcopy(data_info["axis_labels"])
73
        new_axis_labels[c0], new_axis_labels[c1] = data_info["axis_labels"][c1], data_info["axis_labels"][c0]
74
75
        # swapping round core dimensions in data patterns
76
        new_data_patterns = deepcopy(data_info["data_patterns"])
77
        for pattern in new_data_patterns:
78
            for dims in new_data_patterns[pattern]:
79
                if dims != "main_dir":  # not sure what main_dir is ( = slice dim?)
80
                    dims_list = list(new_data_patterns[pattern][dims])
81
                    for i, dim in enumerate(dims_list):
82
                        if dim == c0:
83
                            dims_list[i] = c1
84
                        elif dim == c1:
85
                            dims_list[i] = c0
86
                    new_data_patterns[pattern][dims] = tuple(dims_list)
87
88
        dtype = in_dataset[0].dtype
89
        if dtype is None:
90
            dtype = in_dataset[0].data.data.dtype
91
92
        # creating output dataset with new axis, shape and data patterns to reflect rotated image
93
        if dtype:
94
            out_dataset[0].create_dataset(shape=new_shape, axis_labels=new_axis_labels, dtype=dtype)
95
        else:
96
            out_dataset[0].create_dataset(shape=new_shape, axis_labels=new_axis_labels)
97
        out_dataset[0].data_info.set("data_patterns", new_data_patterns)
98
99
100
        in_pData, out_pData = self.get_plugin_datasets()
101
102
        in_pData[0].plugin_data_setup(self.parameters['pattern'], 'single')
103
        out_pData[0].plugin_data_setup(self.parameters['pattern'], 'single')
104
105
106
    def pre_process(self):
107
        if self.exp.meta_data.get("pre_run"):
108
            in_dataset, out_dataset = self.get_datasets()
109
            dark = in_dataset[0].data.dark()
110
            flat = in_dataset[0].data.flat()
111
            if dark.size:
112
                in_dataset[0].data.update_dark(self.process_frames_3d(dark))
113
            if flat.size:
114
                in_dataset[0].data.update_flat(self.process_frames_3d(flat))
115
            out_dataset[0].data.image_key = in_dataset[0].data.image_key
116
117
    def process_frames(self, data):
118
        # assumes 2D frame
119
        if self.parameters["direction"] == "ACW":
120
            data[0] = np.rot90(data[0], axes=(0, 1))
121
        elif self.parameters["direction"] == "CW":
122
            data[0] = np.rot90(data[0], axes=(1, 0))
123
        return data[0]
124
125
    def process_frames_3d(self, data):
126
        # assumes 3D frame
127
        if self.parameters["direction"] == "ACW":
128
            data = np.rot90(data, axes=(1, 2))
129
        elif self.parameters["direction"] == "CW":
130
            data = np.rot90(data, axes=(2, 1))
131
        return data
132
133
    def post_process(self):
134
        if self.exp.meta_data.get("pre_run"):
135
            in_dataset, out_dataset = self.get_datasets()
136
            image_key = in_dataset[0].data.image_key
137
138
            dark = in_dataset[0].data.dark_updated
139
            flat = in_dataset[0].data.flat_updated
140
141
            new_image_key = np.array([0.] * len(image_key))
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable len does not seem to be defined.
Loading history...
142
            new_image_key[- len(dark):] = [2.] * len(dark)
143
            new_image_key[- len(dark) - len(flat): - len(dark)] = [1.] * len(flat)
144
145
            out_dataset[0].data[- len(dark):] = dark
146
            out_dataset[0].data[- len(dark) - len(flat): - len(dark)] = flat
147
148
            out_dataset[0].data.image_key = new_image_key
149
150
            out_dataset[0].data = ImageKey(out_dataset[0], new_image_key, 0)
151