normalization.py 6.64 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
"""wrapper to pymca `normalization` process"""
payno's avatar
payno committed
26
27
28

__authors__ = ["H. Payno"]
__license__ = "MIT"
29
__date__ = "05/10/2021"
payno's avatar
payno committed
30
31


32
import functools
payno's avatar
payno committed
33
import logging
34
35
import multiprocessing
from PyMca5.PyMcaPhysics.xas.XASClass import XASClass
Wout De Nolf's avatar
flake8    
Wout De Nolf committed
36
from est.core.process.process import Process
37
from est.core.process.process import _NexusDatasetDef
payno's avatar
payno committed
38
from est.core.types import Spectrum, XASObject
39

payno's avatar
payno committed
40
41
42
_logger = logging.getLogger(__name__)


payno's avatar
payno committed
43
def process_spectr_norm(
payno's avatar
payno committed
44
    spectrum: Spectrum,
payno's avatar
payno committed
45
46
47
48
49
50
    configuration,
    overwrite=True,
    callbacks=None,
    output=None,
    output_dict=None,
):
51
52
    """

53
54
55
56
57
58
    :param spectrum: spectrum to process
    :type: :class:`.Spectrum`
    :param configuration: configuration of the pymca normalization
    :type: dict
    :param overwrite: False if we want to return a new Spectrum instance
    :type: bool
59
    :param callbacks: callback to execute.
60
61
    :param output: list to store the result, needed for pool processing
    :type: multiprocessing.manager.list
62
63
64
    :param output_dict: key is input spectrum, value is index in the output
                        list.
    :type: dict
65
    :return: processed spectrum
66
67
    :rtype: tuple (configuration, spectrum)
    """
payno's avatar
payno committed
68
    assert isinstance(spectrum, Spectrum)
payno's avatar
payno committed
69
    _logger.debug("start normalization on spectrum (%s, %s)" % (spectrum.x, spectrum.y))
70
    if spectrum.energy is None or spectrum.mu is None:
payno's avatar
payno committed
71
72
73
        _logger.error(
            "Energy and or Mu is/are not specified, unable to " "compute exafs"
        )
74
        return None, None
75
    pymca_xas = XASClass()
payno's avatar
payno committed
76
    pymca_xas.setSpectrum(energy=spectrum.energy, mu=spectrum.mu)
77
    if configuration is not None:
payno's avatar
payno committed
78
79
80
        if "e0" in configuration:
            configuration["E0Value"] = configuration["e0"]
            configuration["E0Method"] = "Manual"
81
        pymca_xas.setConfiguration(configuration)
82
    configuration = pymca_xas.getConfiguration()
83
84
85
86
87
88
89
90
    try:
        res = pymca_xas.normalize()
        ddict = spectrum.to_dict()
        ddict.update(res)
        spectrum_ = Spectrum.from_dict(ddict)
    except (IndexError, ValueError) as e:
        _logger.error(e)
        return None, None
91
92
93
    if callbacks:
        for callback in callbacks:
            callback()
94
95
96
97
98
99
100
101
102
103
104
105
106

    def get_output(orignal_spec, res_spec):
        if overwrite:
            orignal_spec.update(res_spec)
            return orignal_spec
        else:
            return res_spec

    if output is not None:
        assert output_dict is not None
        output[output_dict[spectrum]] = get_output(spectrum, spectrum_)

    return configuration, get_output(spectrum, spectrum_)
107
108


109
def pymca_normalization(xas_obj):
payno's avatar
payno committed
110
    """
111

112
113
114
115
    :param xas_obj: object containing the configuration and spectra to process
    :type: Union[XASObject, dict]. If is a dict, should contain configuration or
                                 spectra keys. Otherwise is simply the spectra
    :return: spectra dict
116
    :rtype: dict
payno's avatar
payno committed
117
    """
payno's avatar
payno committed
118
119
    normalization_obj = PyMca_normalization(inputs={"xas_obj": xas_obj})
    return normalization_obj.run()
120
121


122
class PyMca_normalization(
123
124
125
126
127
    Process,
    name="normalization",
    input_names=["xas_obj"],
    output_names=["xas_obj"],
    optional_input_names=["normalization"],
128
):
129
    def set_properties(self, properties):
payno's avatar
payno committed
130
131
        if "_pymcaSettings" in properties:
            self._settings = properties["_pymcaSettings"]
132

133
    def run(self):
134
135
        """

136
137
138
        :param xas_obj: object containing the configuration and spectra to process
        :type: Union[XASObject, dict]. If is a dict, should contain configuration or
                                     spectra keys. Otherwise is simply the spectra
139
140
        :return: updated XASObject
        :rtype: :class:`.XASObject`
141
        """
142
        xas_obj = self.inputs.xas_obj
143
        if xas_obj is None:
144
            raise ValueError("xas_obj should be provided")
145
        _xas_obj = self.getXasObject(xas_obj)
146
147

        if _xas_obj.energy is None:
payno's avatar
payno committed
148
            _logger.error("Energy not specified, unable to normalize spectra")
149
150
            return

151
152
153
        if self.inputs.normalization:
            self.setConfiguration(self.inputs.normalization)
            _xas_obj.configuration["Normalization"] = self.inputs.normalization
154
        self.progress = 0.0
155
        self._pool_process(xas_obj=_xas_obj)
156
        self.progress = 100.0
157
158
        if _xas_obj.normalized_energy is None:
            raise ValueError("Fail to compute normalize energy")
payno's avatar
payno committed
159
        self.register_process(
160
161
162
163
164
165
            _xas_obj,
            data_keys=(
                _NexusDatasetDef("NormalizedEnergy"),
                _NexusDatasetDef("NormalizedMu"),
                _NexusDatasetDef("NormalizedSignal"),
            ),
payno's avatar
payno committed
166
        )
167
        self.outputs.xas_obj = _xas_obj.to_dict()
168
169
170
171
        return _xas_obj

    def _pool_process(self, xas_obj):
        """process normalization from a pool"""
172
        assert isinstance(xas_obj, XASObject)
173
174
175
176
177
178
179
180
181
        n_s = len(xas_obj.spectra.data.flat)
        for i_s, spectrum in enumerate(xas_obj.spectra.data.flat):
            process_spectr_norm(
                spectrum=spectrum,
                configuration=xas_obj.configuration,
                callbacks=self.callbacks,
                overwrite=True,
            )
            self.progress = i_s / n_s * 100.0
182

183
184
185
186
187
    def definition(self):
        return "Normalization of the spectrum"

    def program_version(self):
        import PyMca5
payno's avatar
payno committed
188

189
190
        return PyMca5.version()

191
192
    @staticmethod
    def program_name():
payno's avatar
payno committed
193
        return "pymca_normalization"
194

195
    __call__ = run