normalization.py 4.12 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
33
from .progress import Progress
payno's avatar
payno committed
34
35
36
37
import logging
_logger = logging.getLogger(__name__)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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

59
def pymca_normalization(xas_obj):
payno's avatar
payno committed
60
    """
61

62
63
64
65
    :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
66
    :rtype: dict
payno's avatar
payno committed
67
    """
68
69
    normalization_obj = PyMca_normalization()
    return normalization_obj.process(xas_obj=xas_obj)
70
71
72
73
74


class PyMca_normalization(object):
    def __init__(self):
        self._settings = None
75
        self._advancement = Progress('normalization')
76
77
78
79
80
81
82
83

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

    def process(self, xas_obj):
        """

84
85
86
87
        :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
88
89
90
        :rtype: dict
        """
        if isinstance(xas_obj, dict):
91
            _xas_obj = XASObject.from_dict(xas_obj)
92
93
        else:
            _xas_obj = xas_obj
94
95
96
97
98

        if _xas_obj.energy is None:
            _logger.error('Energy not specified, unable to normalize spectra')
            return

99
100
101
        if self._settings:
            _xas_obj.configuration['Normalization'] = self._settings

102
103
104
105
106
107
108
109
110
        self._advancement.startProcess()
        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)
            self._advancement.setAdvancement(
                int(i_spectrum / _xas_obj.n_spectrum) * 100)
        self._advancement.endProcess()
        return _xas_obj
111
112

    __call__ = process