energyroi.py 4.45 KB
Newer Older
payno's avatar
payno committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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.
#
# ###########################################################################*/
25
"""contains `energy roi` related functions / classes"""
payno's avatar
payno committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

__authors__ = ["H. Payno"]
__license__ = "MIT"
__date__ = "27/01/2021"

from .process import Process
import logging

_logger = logging.getLogger(__name__)


def xas_energy_roi(xas_obj):
    """
    apply roi on the XASObject.spectra

    :param xas_obj: object containing the configuration and spectra to process
    :type: Union[:class:`.XASObject`, dict]
    :return: spectra dict
    :rtype: :class:`.XASObject`
    """
payno's avatar
payno committed
46
47
    xas_roi_process = EnergyROIProcess(inputs={"xas_obj": xas_obj})
    return xas_roi_process.run()
payno's avatar
payno committed
48
49
50
51
52
53
54
55


class ROI1D:
    def __init__(self, from_, to_):
        self.from_ = from_
        self.to_ = to_


56
57
58
59
60
class EnergyROIProcess(
    Process, name="energy roi", input_names=["xas_obj"], output_names=["xas_obj"]
):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
payno's avatar
payno committed
61
62
63
64
        self._roi = ROI1D(0, 9999999999)

    def set_properties(self, properties: dict):
        if "energy_roi" in properties:
65
66
67
68
69
70
71
72
            self._settings = properties["energy_roi"]
            self._read_roi(self._settings)

    def update_properties(self, properties):
        super().update_properties(properties=properties)
        self._read_roi(properties=self._settings)

    def _read_roi(self, properties):
payno's avatar
payno committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        if "minE" in properties:
            self._roi.from_ = properties["minE"]
        if "maxE" in properties:
            self._roi.to_ = properties["maxE"]

    def setRoi(self, from_, to_):
        self._roi = ROI1D(from_=from_, to_=to_)

    def _apply_roi(self, xas_obj, roi):
        mask = (xas_obj.spectra.energy <= roi.to_) & (
            xas_obj.spectra.energy >= roi.from_
        )
        xas_obj.spectra.energy = xas_obj.spectra.energy[mask]
        for spectrum in xas_obj.spectra.data.flat:
            spectrum.energy = spectrum.energy[mask]
            spectrum.mu = spectrum.mu[mask]
89
90
91
92
93
94
            for attached_key in ("I0", "I1", "I2", "mu_ref"):
                if hasattr(spectrum, attached_key):
                    values = getattr(spectrum, attached_key)[mask]
                    setattr(spectrum, attached_key, values)
        #     print(spectrum.__dict__)
        # print(xas_obj.__dict__)
payno's avatar
payno committed
95
96
        return xas_obj

97
    def run(self):
payno's avatar
payno committed
98
99
100
101
102
103
104
        """

        :param xas_obj: object containing the configuration and spectra to process
        :type: Union[:class:`.XASObject`, dict]
        :return: spectra dict
        :rtype: :class:`.XASObject`
        """
105
        xas_obj = self.inputs.xas_obj
106
        if xas_obj is None:
107
            raise ValueError("xas_obj should be provided")
payno's avatar
payno committed
108
109
110
111
112
        _xas_obj = self.getXasObject(xas_obj=xas_obj)

        # existing roi is priority. This is the case if called from pushworkflow
        # for example.
        if self._roi is not None:
113
114
115
            xas_obj = self._apply_roi(xas_obj=_xas_obj, roi=self._roi)

        self.register_process(_xas_obj, data_keys=("Mu", "energy"))
116
        self.outputs.xas_obj = _xas_obj.to_dict()
117
118
119
120
121
122
123
124
125
126
        return xas_obj

    def definition(self):
        return "apply a ROI on energy range"

    def program_version(self):
        import est.version

        return est.version.version

127
    @staticmethod
128
    def program_name():
129
        return "energy-roi"
payno's avatar
payno committed
130

131
    __call__ = run