fitresults.py 9.97 KB
Newer Older
Damien Naudet's avatar
Damien Naudet 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
32
33
34
35
#!/usr/bin/python
# coding: utf8
# /*##########################################################################
#
# Copyright (c) 2015-2016 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.
#
# ###########################################################################*/

from __future__ import absolute_import

__authors__ = ["D. Naudet"]
__date__ = "01/06/2016"
__license__ = "MIT"


from collections import OrderedDict

Damien Naudet's avatar
Damien Naudet committed
36
37
import numpy as np

Damien Naudet's avatar
Damien Naudet committed
38
from ...io.FitH5 import FitH5Writer, FitH5QAxis
Damien Naudet's avatar
Damien Naudet committed
39

Damien Naudet's avatar
Damien Naudet committed
40
41
42
43
44
45

class FitStatus(object):
    """
    Enum for the fit status
    Starting at 1 for compatibility reasons.
    """
46
    UNKNOWN, OK, FAILED = range(0, 3)
Damien Naudet's avatar
Damien Naudet committed
47

Damien Naudet's avatar
Damien Naudet committed
48
49
50
51
52
53
54
55
56
57

class FitResult(object):
    """
    Fit results
    """
    _AXIS = QX_AXIS, QY_AXIS, QZ_AXIS = range(3)
    _AXIS_NAMES = ('qx', 'qy', 'qz')

    def __init__(self, entry,
                 q_x, q_y, q_z,
Thomas Vincent's avatar
Thomas Vincent committed
58
59
                 sample_x, sample_y,
                 background_mode):
Damien Naudet's avatar
Damien Naudet committed
60
61
62
63
64
65
66
67
68
69
70
        super(FitResult, self).__init__()

        self._entry = entry

        self._sample_x = sample_x
        self._sample_y = sample_y

        self._q_x = q_x
        self._q_y = q_y
        self._q_z = q_z

Thomas Vincent's avatar
Thomas Vincent committed
71
72
        self._background_mode = background_mode

Damien Naudet's avatar
Damien Naudet committed
73
74
        self._processes = OrderedDict()

Thomas Vincent's avatar
Thomas Vincent committed
75
        self._n_pts = len(sample_x)
Damien Naudet's avatar
Damien Naudet committed
76

Thomas Vincent's avatar
Thomas Vincent committed
77
78
79
        self._status = OrderedDict([('qx_status', np.zeros(self._n_pts)),
                                    ('qy_status', np.zeros(self._n_pts)),
                                    ('qz_status', np.zeros(self._n_pts))])
Damien Naudet's avatar
Damien Naudet committed
80

Damien Naudet's avatar
Damien Naudet committed
81
82
        self._infos = OrderedDict()

Damien Naudet's avatar
Damien Naudet committed
83
84
85
86
87
88
89
90
91
92
    def processes(self):
        """
        Returns the process names
        :return:
        """
        return self._processes.keys()

    def params(self, process):
        return self._get_process(process, create=False)['params'].keys()

Damien Naudet's avatar
Damien Naudet committed
93
94
95
96
97
98
    def status(self, axis):
        """
        Returns the status for the given axis.
        :param axis:
        :return:
        """
Damien Naudet's avatar
Damien Naudet committed
99
100
        assert axis in self._AXIS

Damien Naudet's avatar
Damien Naudet committed
101
        return self._status[self._AXIS_NAMES[axis]][:]
Damien Naudet's avatar
Damien Naudet committed
102

Damien Naudet's avatar
Damien Naudet committed
103
    def qx_status(self):
Damien Naudet's avatar
Damien Naudet committed
104
        """
Damien Naudet's avatar
Damien Naudet committed
105
        Returns qx fit status
Damien Naudet's avatar
Damien Naudet committed
106
107
        :return:
        """
Damien Naudet's avatar
Damien Naudet committed
108
        return self.status(self.QX_AXIS)
Damien Naudet's avatar
Damien Naudet committed
109

Damien Naudet's avatar
Damien Naudet committed
110
    def qy_status(self):
Damien Naudet's avatar
Damien Naudet committed
111
        """
Damien Naudet's avatar
Damien Naudet committed
112
        Returns qy fit status
Damien Naudet's avatar
Damien Naudet committed
113
114
        :return:
        """
Damien Naudet's avatar
Damien Naudet committed
115
        return self.status(self.QY_AXIS)
Damien Naudet's avatar
Damien Naudet committed
116

Damien Naudet's avatar
Damien Naudet committed
117
    def qz_status(self):
Damien Naudet's avatar
Damien Naudet committed
118
        """
Damien Naudet's avatar
Damien Naudet committed
119
        Returns qz fit status
Damien Naudet's avatar
Damien Naudet committed
120
121
        :return:
        """
Damien Naudet's avatar
Damien Naudet committed
122
        return self.status(self.QZ_AXIS)
Damien Naudet's avatar
Damien Naudet committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

    def results(self, process, param, axis=None):
        """
        Returns the fitted parameter results for a given process.
        :param process: process name
        :param param: param name
        :param axis: if provided, returns only the result for the given axis
        :return:
        """

        param = self._get_param(process, param, create=False)

        if axis is not None:
            assert axis in self._AXIS
            return param[self._AXIS_NAMES[axis]]
        return param

    def qx_results(self, process, param):
        """
        Returns qx fit results for the given process
        :param process:
        :param param: param name
        :return:
        """
        return self.results(process, param, axis=self.QX_AXIS)

    def qy_results(self, process, param):
        """
        Returns qy fit results for the given process
        :param process:
        :param param: param name
        :return:
        """
        return self.results(process, param, axis=self.QY_AXIS)

    def qz_results(self, process, param):
        """
        Returns qz fit results for the given process
        :param process:
        :param param: param name
        :return:
        """
        return self.results(process, param, axis=self.QZ_AXIS)

167
168
    def add_result(self, axis, process, param, result):
        self._add_axis_result(process, self._AXIS_NAMES.index(axis), param, result)
Damien Naudet's avatar
Damien Naudet committed
169
170
171
172
173
174
175

    def _add_axis_result(self, process, axis, param, result):
        assert axis in self._AXIS

        param_data = self._get_param(process, param)
        param_data[self._AXIS_NAMES[axis]] = result

Damien Naudet's avatar
Damien Naudet committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
    def add_qx_info(self, name, data):
        """
        Add other misc. plottable info (e.g : chi2, ...) associated with the
        fit along the qx axis.
        data must be an array with the same number of elements
        :param name:
        :param data:
        :return:
        """
        self._add_info(name, self.QX_AXIS, data)

    def add_qy_info(self, name, data):
        """
        Add other misc. plottable info (e.g : chi2, ...) associated with the
        fit along the qy axis.
        data must be an array with the same number of elements
        :param name:
        :param data:
        :return:
        """
        self._add_info(name, self.QY_AXIS, data)

    def add_qz_info(self, name, data):
        """
        Add other misc. plottable info (e.g : chi2, ...) associated with the
        fit along the qz axis.
        data must be an array with the same number of elements
        :param name:
        :param data:
        :return:
        """
        self._add_info(name, self.QZ_AXIS, data)

    def _add_info(self, name, axis, data):
        """
        Add other misc. plottable info (e.g : chi2, ...) associated with the
        fit along the given axis.
        data must be an array with the same number of elements
        :param name:
        :param axis:
        :param data:
        :return:
        """
        assert axis in self._AXIS

        infos = self._infos

        if name not in infos:
            info = OrderedDict([('qx', None), ('qy', None), ('qz', None)])
            infos[name] = info
        else:
            info = infos[name]

        info[self._AXIS_NAMES[axis]] = data

231
232
    def set_status(self, axis, status):
        self._set_axis_status(self._AXIS_NAMES.index(axis), status)
Damien Naudet's avatar
Damien Naudet committed
233

Damien Naudet's avatar
Damien Naudet committed
234
    def _set_axis_status(self, axis, status):
Damien Naudet's avatar
Damien Naudet committed
235
236
        assert axis in self._AXIS

Damien Naudet's avatar
Damien Naudet committed
237
        self._status[self._AXIS_NAMES[axis]] = status
Damien Naudet's avatar
Damien Naudet committed
238
239
240
241
242
243

    def _get_process(self, process, create=True):

        if process not in self._processes:
            if not create:
                raise KeyError('Unknown process {0}.'.format(process))
Damien Naudet's avatar
Damien Naudet committed
244
            _process = OrderedDict([('params', OrderedDict())])
Damien Naudet's avatar
Damien Naudet committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
            self._processes[process] = _process
        else:
            _process = self._processes[process]

        return _process

    def _get_param(self, process, param, create=True):
        process = self._get_process(process, create=create)
        params = process['params']

        if param not in params:
            if not create:
                raise KeyError('Unknown param {0}.'.format(param))
            _param = OrderedDict()
            for axis in self._AXIS_NAMES:
                _param[axis] = None
            params[param] = _param
        else:
            _param = params[param]

        return _param

    entry = property(lambda self: self._entry)

    sample_x = property(lambda self: self._sample_x)
    sample_y = property(lambda self: self._sample_y)

    q_x = property(lambda self: self._q_x)
    q_y = property(lambda self: self._q_y)
    q_z = property(lambda self: self._q_z)

Damien Naudet's avatar
Damien Naudet committed
276
277
278
279
280
281
282
283
284
285
286
287
288
    def to_fit_h5(self, fit_h5, mode=None):
        with FitH5Writer(fit_h5, mode=mode) as fitH5:

            entry = self.entry
            fitH5.create_entry(entry)

            fitH5.set_scan_x(entry, self.sample_x)
            fitH5.set_scan_y(entry, self.sample_y)

            fitH5.set_qx(entry, self.q_x)
            fitH5.set_qy(entry, self.q_y)
            fitH5.set_qz(entry, self.q_z)

Thomas Vincent's avatar
Thomas Vincent committed
289
290
            fitH5.set_background_mode(entry, self._background_mode)

Damien Naudet's avatar
Damien Naudet committed
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
            processes = self.processes()

            for process in processes:
                fitH5.create_process(entry, process)

                for param in self.params(process):
                    xresult = self.results(process, param,
                                           self.QX_AXIS)
                    yresult = self.results(process, param,
                                           self.QY_AXIS)
                    zresult = self.results(process, param,
                                           self.QZ_AXIS)

                    fitH5.set_qx_result(entry,
                                        process,
                                        param,
                                        xresult)

                    fitH5.set_qy_result(entry,
                                        process,
                                        param,
                                        yresult)

                    fitH5.set_qz_result(entry,
                                        process,
                                        param,
                                        zresult)

            xstatus = self.qx_status()
            ystatus = self.qy_status()
            zstatus = self.qz_status()

            fitH5.set_status(entry,
                             FitH5QAxis.qx_axis,
                             xstatus)
            fitH5.set_status(entry,
                             FitH5QAxis.qy_axis,
                             ystatus)
            fitH5.set_status(entry,
                             FitH5QAxis.qz_axis,
                             zstatus)