normalization.py 7.04 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
# 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"


31
import functools
payno's avatar
payno committed
32
import logging
33
34
35
36
import multiprocessing

from PyMca5.PyMcaPhysics.xas.XASClass import XASClass

payno's avatar
payno committed
37
38
from est.core.process.process import Process, Progress
from est.core.types import Spectrum, XASObject
39

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


43
def process_spectr_norm(spectrum, configuration, overwrite=True, callbacks=None,
44
                        output=None, output_dict=None):
45
46
    """

47
48
49
50
51
52
    :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
53
    :param callbacks: callback to execute.
54
55
    :param output: list to store the result, needed for pool processing
    :type: multiprocessing.manager.list
56
57
58
    :param output_dict: key is input spectrum, value is index in the output
                        list.
    :type: dict
59
    :return: processed spectrum
60
61
    :rtype: tuple (configuration, spectrum)
    """
62
    _logger.debug('start normalization on spectrum (%s, %s)' % (spectrum.x, spectrum.y))
63
64
65
    if spectrum.energy is None or spectrum.mu is None:
        _logger.error('Energy and or Mu is/are not specified, unable to '
                      'compute exafs')
66
        return None, None
67
68
69
    pymca_xas = XASClass()
    pymca_xas.setSpectrum(energy=spectrum.energy,
                          mu=spectrum.mu)
70
71
    if configuration is not None:
        pymca_xas.setConfiguration(configuration)
72
    configuration = pymca_xas.getConfiguration()
73
74
75
76
77
78
79
80
    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
81
82
83
    if callbacks:
        for callback in callbacks:
            callback()
84
85
86
87
88
89
90
91
92
93
94
95
96

    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_)
97
98


99
def pymca_normalization(xas_obj):
payno's avatar
payno committed
100
    """
101

102
103
104
105
    :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
106
    :rtype: dict
payno's avatar
payno committed
107
    """
108
109
    normalization_obj = PyMca_normalization()
    return normalization_obj.process(xas_obj=xas_obj)
110
111


112
113
114
115
_USE_MULTIPROCESSING_POOL = False
# note: we cannot use multiprocessing pool with push workflow for now.


116
class PyMca_normalization(Process):
117
    def __init__(self):
118
        Process.__init__(self, 'normalization')
119
        self._advancement = Progress(self.name)
120
121
122
123
124
125
126
127

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

    def process(self, xas_obj):
        """

128
129
130
        :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
131
132
        :return: updated XASObject
        :rtype: :class:`.XASObject`
133
        """
134
        _xas_obj = self.getXasObject(xas_obj)
135
136
137
138
139

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

140
141
142
        if self._settings:
            _xas_obj.configuration['Normalization'] = self._settings

143
        self._advancement.reset(max_=_xas_obj.n_spectrum)
144
        self._advancement.startProcess()
145
        self._pool_process(xas_obj=_xas_obj)
146
        self._advancement.endProcess()
147
        self.register_process(_xas_obj, data_keys=("NormalizedEnergy",
148
149
                                                   "NormalizedMu",
                                                   "NormalizedSignal"))
150
151
152
153
        return _xas_obj

    def _pool_process(self, xas_obj):
        """process normalization from a pool"""
154
155
156
157
158
        assert isinstance(xas_obj, XASObject)
        if not _USE_MULTIPROCESSING_POOL:
            for spectrum in xas_obj.spectra:
                process_spectr_norm(spectrum=spectrum,
                                    configuration=xas_obj.configuration,
159
                                    callbacks=self.callbacks,
160
161
162
163
164
165
166
167
168
169
170
171
172
                                    overwrite=True)
        else:
            from multiprocessing import Manager
            manager = Manager()
            output_dict = {}
            res_list = manager.list()
            for i_spect, spect in enumerate(xas_obj.spectra):
                res_list.append(None)
                output_dict[spect] = i_spect

            with multiprocessing.Pool(5) as p:
                partial_ = functools.partial(process_spectr_norm,
                                             configuration=xas_obj.configuration,
173
                                             callbacks=self.callbacks,
174
175
176
177
178
179
180
181
182
                                             overwrite=False,
                                             output=res_list,
                                             output_dict=output_dict)
                p.map(partial_, xas_obj.spectra)

            # then update local spectrum
            for spectrum, res in zip(xas_obj.spectra, res_list):
                spectrum.update(res)

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

    def program_version(self):
        import PyMca5
        return PyMca5.version()

    def program_name(self):
191
        return 'pymca_normalization'
192
193

    __call__ = process