information.py 11.5 KB
Newer Older
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
# 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__ = "25/01/2021"


from est.core.types import Dim
from est.units import ur
33
from silx.io.url import DataUrl
34
from typing import Iterable, Union
35
36
37
38
39
40
41
42


class SpecInputInformation:
    """Input information relative to spec"""

    def __init__(
        self,
        spec_file,
43
44
45
46
        energy_abs_col_name=None,
        input_abs_col_name=None,
        input_monitor_col_name=None,
        scan_title=None,
47
48
49
50
51
52
53
54
55
56
57
58
59
    ):
        self.__spec_file = None
        self.__scan_title = None
        self.__energy_abs_col_name = None
        self.__input_abs_col_name = None
        self.__input_monitor_col_name = None

        self.spec_file = spec_file
        self.scan_title = scan_title
        self.energy_abs_col_name = energy_abs_col_name
        self.input_abs_col_name = input_abs_col_name
        self.input_monitor_col_name = input_monitor_col_name

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    @property
    def spectra_url(self):
        return DataUrl(
            file_path=self.spec_file,
            scheme="PyMca",
            data_path=self.input_abs_col_name,
        )

    @property
    def channel_url(self):
        return DataUrl(
            file_path=self.spec_file,
            scheme="PyMca",
            data_path=self.energy_abs_col_name,
        )

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    @property
    def spec_file(self):
        return self.__spec_file

    @spec_file.setter
    def spec_file(self, spec_file):
        self.__spec_file = spec_file

    @property
    def scan_title(self):
        return self.__scan_title

    @scan_title.setter
    def scan_title(self, title):
        self.__scan_title = title

    @property
    def energy_abs_col_name(self):
        return self.__energy_abs_col_name

    @energy_abs_col_name.setter
    def energy_abs_col_name(self, col_name):
        self.__energy_abs_col_name = col_name

    @property
    def input_abs_col_name(self):
        return self.__input_abs_col_name

    @input_abs_col_name.setter
    def input_abs_col_name(self, col_name):
        self.__input_abs_col_name = col_name

    @property
    def input_monitor_col_name(self):
        return self.__input_monitor_col_name

    @input_monitor_col_name.setter
    def input_monitor_col_name(self, col_name):
        self.__input_monitor_col_name = col_name


class _HDF5InputInformation:
    def __init__(self):
        pass


class InputInformation:
    """
    Utils class to store input information
    """

    def __init__(
        self,
129
130
        spectra_url=None,
        channel_url=None,
131
132
133
134
135
136
137
138
139
140
141
142
        config_url=None,
        dimensions=(Dim.DIM_2, Dim.DIM_1, Dim.DIM_0),
        energy_unit=ur.eV,
        spec_input=None,
        columns_names=None,
    ):
        # main information
        self.__spectra_url = None
        self.__channel_url = None
        self.__dimensions = None
        self.__config_url = None
        self.__energy_unit = None
143
        self.__spec_info = spec_input
144
145
146
147
148
149
150
151
        self.__columns_names = None

        # "fancy information"
        self.__I0_url = None
        self.__I1_url = None
        self.__I2_url = None
        self.__mu_ref_url = None

152
153
154
155
156
157
158
159
        if (
            spectra_url is not None or channel_url is not None
        ) and spec_input is not None:
            raise ValueError(
                "Either spec input or urls (spectra and channel) "
                "must be provided. Not both"
            )

160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
        self.spectra_url = spectra_url
        self.channel_url = channel_url
        self.config_url = config_url
        self.dimensions = dimensions
        self.energy_unit = energy_unit
        self.columns_names = columns_names

    def is_spec_input(self) -> bool:
        """return True if the input information (spectra...) comes from a
        spec file
        """
        return self.__spec_info is not None

    def has_url_information(self) -> bool:
        """
        Return True if this contains at least on of the following url:
               input_spectra_url or input_channel_url or input_configuration_url

        """
        return self.spectra_url or self.channel_url or self.configuration_url

    def is_valid(self):
        """Return True if at least a spec file is provided or some url
        information"""
184
        return self.has_url_information
185
186
187

    @property
    def spectra_url(self):
188
189
190
191
        if self.is_spec_input():
            return self.spec_info.spectra_url
        else:
            return self.__spectra_url
192
193
194

    @spectra_url.setter
    def spectra_url(self, url):
195
196
197
198
199
200
        if url is not None and self.is_spec_input():
            raise ValueError(
                "You cannot set an url for channel because a "
                "this instance is already associated to spec "
                "information."
            )
201
202
203
204
205
        # TODO: maybe we want to add a check to create a DataUrl if this is a string...
        self.__spectra_url = url

    @property
    def channel_url(self):
206
207
208
209
        if self.is_spec_input():
            return self.spec_info.channel_url
        else:
            return self.__channel_url
210
211
212

    @channel_url.setter
    def channel_url(self, url):
213
214
215
216
217
218
        if url is not None and self.is_spec_input():
            raise ValueError(
                "You cannot set an url for channel because a "
                "this instance is already associated to spec "
                "information."
            )
219
220
221
222
223
224
225
226
227
228
229
230
        self.__channel_url = url

    @property
    def config_url(self):
        return self.__config_url

    @config_url.setter
    def config_url(self, url):
        self.__config_url = url

    @property
    def dimensions(self):
231
        return self.__dimensions
232
233

    @dimensions.setter
234
235
    def dimensions(self, dims):
        self.__dimensions = dims
236
237
238
239
240
241
242
243
244
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
276
277
278
279
280
281
282

    @property
    def energy_unit(self):
        return self.__energy_unit

    @energy_unit.setter
    def energy_unit(self, unit):
        self.__energy_unit = unit

    @property
    def columns_names(self):
        return self.__columns_names

    @columns_names.setter
    def columns_names(self, columns_names):
        self.__columns_names = columns_names

    @property
    def I0_url(self):
        return self.__I0_url

    @I0_url.setter
    def I0_url(self, url):
        self.__I0_url = url

    @property
    def I1_url(self):
        return self.__I1_url

    @I1_url.setter
    def I1_url(self, url):
        self.__I1_url = url

    @property
    def I2_url(self):
        return self.__I2_url

    @I2_url.setter
    def I2_url(self, url):
        self.__I2_url = url

    @property
    def mu_ref_url(self):
        return self.__mu_ref_url

    @mu_ref_url.setter
    def mu_ref_url(self, url):
283
284
285
286
287
288
        if url is not None and self.is_spec_input():
            raise ValueError(
                "You cannot set an url for channel because a "
                "this instance is already associated to spec"
                "information."
            )
289
290
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
        self.__mu_ref_url = url

    @property
    def spec_info(self):
        return self.__spec_info

    @property
    def scan_title(self):
        if self.is_spec_input():
            return self.spec_info.scan_title
        else:
            return None

    @property
    def scan_title(self):
        if self.is_spec_input():
            return self.spec_info.scan_title
        else:
            return None

    @property
    def spec_file(self):
        if self.is_spec_input():
            return self.spec_info.spec_file
        else:
            return None

    @property
    def input_energy_col_name(self):
        if self.is_spec_input():
319
            return self.spec_info.energy_abs_col_name
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
        else:
            return None

    @property
    def input_abs_col_name(self):
        if self.is_spec_input():
            return self.spec_info.input_abs_col_name
        else:
            return None

    @property
    def input_monitor_col_name(self):
        if self.is_spec_input():
            return self.spec_info.input_monitor_col_name
        else:
            return None

    def __str__(self):
        return "\n".join(
            (
                "is spec: {}".format(self.is_spec_input()),
                "spectra url is: {}".format(self.spectra_url),
                "channel url is: {}".format(self.channel_url),
                "config url is: {}".format(self.config_url),
                "dimensions are: {}".format(self.dimensions),
                "energy unit is: {}".format(self.energy_unit),
                "columns names: {}".format(self.columns_names),
            )
        )
349
350
351
352
353
354
355
356

    def to_dict(self) -> dict:
        def dump_url(url):
            if url in (None, ""):
                return None
            else:
                return url.path()

357
358
359
360
361
362
363
364
365
366
367
        dimensions = self.dimensions
        if dimensions is None:
            pass
        else:
            if not isinstance(dimensions, Iterable):
                raise TypeError(
                    "dimensions is expected to be an Iterable "
                    "not {}".format(type(dimensions))
                )
            else:
                dimensions = tuple([dim.value for dim in self.dimensions])
368
369
370
        return {
            "spectra_url": dump_url(self.spectra_url),
            "channel_url": dump_url(self.channel_url),
371
            "dimensions": dimensions,
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
            "config_url": dump_url(self.config_url),
            "energy_unit": str(self.energy_unit),
            "columns_names": self.columns_names,
        }

    @staticmethod
    def from_dict(ddict: dict):
        spectra_url = ddict.get("spectra_url", None)
        channel_url = ddict.get("channel_url", None)
        dimensions = ddict.get("dimensions", None)
        config_url = ddict.get("config_url", None)
        energy_unit = ddict.get("energy_unit", None)
        columns_names = ddict.get("columns_names", None)
        from est.app.utils import get_unit

        def load_url(url):
            if url in (None, ""):
                return None
            else:
                return DataUrl(path=url)

393
394
395
396
397
        def load_dimensions(dimensions: Union[Iterable, None]):
            if dimensions is None:
                return None
            else:
                return tuple([Dim.from_value(dim) for dim in dimensions])
398
399
400
401
402
403
404
405
406

        return InputInformation(
            spectra_url=load_url(spectra_url),
            channel_url=load_url(channel_url),
            dimensions=load_dimensions(dimensions),
            config_url=load_url(config_url),
            energy_unit=get_unit(energy_unit),
            columns_names=columns_names,
        )