roi.py 3.07 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2017 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/


__authors__ = ["H. Payno"]
__license__ = "MIT"
__date__ = "15/10/2018"


from . import OverwritingOperation


class RoiOperation(OverwritingOperation):
    def __init__(self, experiment, origin, size):
        OverwritingOperation.__init__(self, experiment=experiment, name='roi')
        self._origin = origin
        self._size = size
        """
        roi, for now store as a tuple (origin, size). Only deal with rectangle
        roi for now.
        """
        self._cache_data = None
        """ numpy.ndarray used for caching"""

    def key(self):
payno's avatar
payno committed
48
        return ' '.join((self._name, 'origin:', str(self._origin), 'size:',
49
50
51
                         str(self._size)))

    def compute(self):
52
        self.data_flatten = self._compute(self.data)
53
54
55
56
57
        self.registerOperation()
        return self.data

    def dry_run(self, cache_data=None):
        if cache_data is None:
58
            self._cache_data = self.data_flatten[...]
59
        else:
60
            assert cache_data.ndim is 3
61
62
63
64
65
66
            self._cache_data = cache_data
        return self._compute(self._cache_data)

    def apply(self):
        if self._cache_data is None:
            raise ValueError('No data in cache')
67
        self.data_flatten = self._cache_data
68
69
70
71
72
73
74
        self.clear_cache()
        self.registerOperation()
        return self.data

    def _compute(self, data):
        ymin, ymax = int(self._origin[1]), int(self._origin[1] + self._size[1])
        xmin, xmax = int(self._origin[0]), int(self._origin[0] + self._size[0])
75
76
77
78
79
80
        if data.ndim is 3:
            data = data[:, ymin:ymax, xmin:xmax]
        elif data.ndim is 4:
            data = data[:, :, ymin:ymax, xmin:xmax]
        else:
            raise ValueError('data dimension is not managed (%s)' % data.ndim)
81
82
83
84
        return data

    def clear_cache(self):
        self._cache_data = None