normalization.py 4.08 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
25
26
27
28
29
30
31
# 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__ = "06/11/2019"


from PyMca5.PyMcaPhysics.xas.XASClass import XASClass
32
from xas.core.types import XASObject, Spectrum
payno's avatar
payno committed
33
34
35
36
import logging
_logger = logging.getLogger(__name__)


37
def pymca_normalization(xas_obj):
payno's avatar
payno committed
38
    """
39

40
41
42
43
    :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
44
    :rtype: dict
payno's avatar
payno committed
45
    """
46
    if isinstance(xas_obj, dict):
47
        _xas_obj = XASObject().load_frm_dict(xas_obj)
48
49
    else:
        _xas_obj = xas_obj
50
51
52

    if _xas_obj.energy is None:
        _logger.error('Energy not specified, unable to normalize spectra')
payno's avatar
payno committed
53
        return
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

    def _process_spectrum(spectrum, configuration):
        if spectrum.mu is None:
            _logger.error('Mu is not specified, unable to normalize')
            return
        pymca_xas = XASClass()
        pymca_xas.setSpectrum(energy=spectrum.energy,
                              mu=spectrum.mu)
        pymca_xas.setConfiguration(configuration)
        configuration = pymca_xas.getConfiguration()
        try:
            pymca_xas.processSpectrum()
            # TODO: next line can be removed
            ddict = spectrum.to_dict()
            res = pymca_xas.normalize()
            ddict.update(res)
            spectrum = Spectrum.from_dict(ddict)
        except (IndexError, ValueError) as e:
            _logger.error(e)

        return configuration, spectrum
    # TODO: this part could be optimized using vectorization or multiprocess for
    # example.
    for i_spectrum, spectrum in enumerate(_xas_obj.spectra):
        _xas_obj.configuration, _xas_obj.spectra[i_spectrum] = _process_spectrum(spectrum=spectrum,
                                                                                 configuration=_xas_obj.configuration)
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    return _xas_obj


class PyMca_normalization(object):
    def __init__(self):
        self._settings = None

    def setProperties(self, properties):
        if '_pymcaSettings' in properties:
            self._settings = properties['_pymcaSettings']

    def process(self, xas_obj):
        """

94
95
96
97
        :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
98
99
100
        :rtype: dict
        """
        if isinstance(xas_obj, dict):
101
            _xas_obj = XASObject.from_dict(xas_obj)
102
103
104
105
106
107
108
109
        else:
            _xas_obj = xas_obj
        if self._settings:
            _xas_obj.configuration['Normalization'] = self._settings

        return pymca_normalization(_xas_obj)

    __call__ = process