hdf5scan.py 28.3 KB
Newer Older
payno's avatar
payno committed
1
# coding: utf-8
payno's avatar
payno committed
2
# /*##########################################################################
payno's avatar
payno committed
3
# Copyright (C) 2016-2020 European Synchrotron Radiation Facility
payno's avatar
payno committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#
# 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.
#
#############################################################################*/

payno's avatar
payno committed
25
26
"""contains EDFTomoScan, class to be used with HDF5 acquisition"""

payno's avatar
payno committed
27
28
29
30
31
32

__authors__ = ["H.Payno"]
__license__ = "MIT"
__date__ = "09/08/2018"


payno's avatar
payno committed
33
from ..scanbase import TomoScanBase, _FOV
payno's avatar
payno committed
34
35
36
37
38
39
import json
import io
import os
import h5py
import numpy
from silx.io.url import DataUrl
40
from silx.utils.enum import Enum as _Enum
payno's avatar
payno committed
41
from tomoscan.utils import docstring
42
from tomoscan.io import HDF5File
43
from silx.io.utils import get_data
44
from ..unitsystem import metricsystem
45
from .utils import get_compacted_dataslices
46
import typing
payno's avatar
payno committed
47
import logging
payno's avatar
payno committed
48
49
50
51

_logger = logging.getLogger(__name__)


52
class ImageKey(_Enum):
53
54
55
    ALIGNMENT = -1
    PROJECTION = 0
    FLAT_FIELD = 1
56
57
58
59
    DARK_FIELD = 2
    INVALID = 3


payno's avatar
payno committed
60
61
62
63
64
65
66
67
68
69
70
71
class HDF5TomoScan(TomoScanBase):
    """
    This is the implementation of a TomoBase class for an acquisition stored
    in a HDF5 file.

    For now several property of the acquisition is accessible thought a getter
    (like get_scan_range) and a property (scan_range).

    This is done to be compliant with TomoBase instantiation. But his will be
    replace progressively by properties at the 'TomoBase' level

    :param scan: scan directory or scan masterfile.h5
72
73
74
75
76
    :param Union[str, None] entry: name of the NXtomo entry to select. If given
                                   index is ignored.
    :param Union[int, None] index: of the NXtomo entry to select. Ignored if
                                   an entry is specified. For consistency
                                   entries are ordered alphabetically
payno's avatar
payno committed
77
78
    """

payno's avatar
payno committed
79
    _TYPE = "hdf5"
payno's avatar
payno committed
80

payno's avatar
payno committed
81
    _DICT_ENTRY_KEY = "entry"
payno's avatar
payno committed
82

payno's avatar
payno committed
83
    _PROJ_PATH = "instrument/detector/data"
payno's avatar
payno committed
84

payno's avatar
payno committed
85
    _SCAN_META_PATH = "scan_meta/technique/scan"
payno's avatar
payno committed
86

payno's avatar
payno committed
87
    _DET_META_PATH = "scan_meta/technique/detector"
88

payno's avatar
payno committed
89
    _ROTATION_ANGLE_PATH = "sample/rotation_angle"
90

payno's avatar
payno committed
91
    _IMG_KEY_PATH = "instrument/detector/image_key"
92

payno's avatar
payno committed
93
    _IMG_KEY_CONTROL_PATH = "instrument/detector/image_key_control"
94

payno's avatar
payno committed
95
    _X_PIXEL_SIZE_PATH = "instrument/detector/x_pixel_size"
96

payno's avatar
payno committed
97
    _Y_PIXEL_SIZE_PATH = "instrument/detector/y_pixel_size"
98

payno's avatar
payno committed
99
    _X_PIXEL_MAG_SIZE_PATH = "instrument/detector/x_magnified_pixel_size"
100

payno's avatar
payno committed
101
    _Y_PIXEL_MAG_SIZE_PATH = "instrument/detector/y_magnified_pixel_size"
payno's avatar
payno committed
102

payno's avatar
payno committed
103
    _DISTANCE_PATH = "instrument/detector/distance"
payno's avatar
payno committed
104

payno's avatar
payno committed
105
    _FOV_PATH = "instrument/detector/field_of_view"
106

payno's avatar
payno committed
107
108
109
    _ENERGY_PATH = "beam/incident_energy"

    _SCHEME = "silx"
payno's avatar
payno committed
110

111
112
    _EPSILON_ROT_ANGLE = 0.02

payno's avatar
payno committed
113
114
115
    def __init__(
        self, scan: str, entry: str = None, index: typing.Union[int, None] = 0
    ):
116
117
        if entry is not None:
            index = None
payno's avatar
payno committed
118
119
        # if the user give the master file instead of the scan dir...
        if scan is not None:
payno's avatar
payno committed
120
            if not os.path.exists(scan) and "." in os.path.split(scan)[-1]:
121
122
                self.master_file = scan
                scan = os.path.dirname(scan)
Tomas Farago's avatar
Tomas Farago committed
123
            elif os.path.isfile(scan) or ():
payno's avatar
payno committed
124
125
126
                self.master_file = scan
                scan = os.path.dirname(scan)
            else:
127
                self.master_file = self.get_master_file(scan)
payno's avatar
payno committed
128
129
130
131
132
        else:
            self.master_file = None

        super(HDF5TomoScan, self).__init__(scan=scan, type_=HDF5TomoScan._TYPE)

133
134
135
        if scan is None:
            self._entry = None
        else:
payno's avatar
payno committed
136
137
138
            self._entry = entry or self._get_entry_at(
                index=index, file_path=self.master_file
            )
139
            if self._entry is None:
payno's avatar
payno committed
140
141
142
                raise ValueError(
                    "unable to find a valid entry for %s" % self.master_file
                )
payno's avatar
payno committed
143
144
145
        # for now the default entry is 1_tomo but should change with time

        # data caches
payno's avatar
payno committed
146
        self._projections = None
147
        self._projections_compacted = None
148
149
        self._flats = None
        self._darks = None
payno's avatar
payno committed
150
151
152
153
154
155
156
157
158
159
        self._tomo_n = None
        # number of projections / radios
        self._dark_n = None
        # number of dark image made during acquisition
        self._ref_n = None
        # number of flat field made during acquisition
        self._scan_range = None
        # scan range, in degree
        self._dim_1, self._dim_2 = None, None
        # image dimensions
160
161
        self._x_pixel_size = None
        self._y_pixel_size = None
162
163
        self._x_magnified_pixel_size = None
        self._y_magnified_pixel_size = None
payno's avatar
payno committed
164
        # pixel dimensions (tuple)
165
166
        self._frames = None
        self._image_keys = None
167
        self._image_keys_control = None
168
        self._rotation_angles = None
payno's avatar
payno committed
169
        self._distance = None
payno's avatar
payno committed
170
        self._fov = None
171
        self._energy = None
172

173
174
175
176
177
178
    @staticmethod
    def get_master_file(scan_path):
        if os.path.isfile(scan_path):
            master_file = scan_path
        else:
            master_file = os.path.join(scan_path, os.path.basename(scan_path))
payno's avatar
payno committed
179
180
181
182
183
184
            if os.path.exists(master_file + ".nx"):
                master_file = master_file + ".nx"
            elif os.path.exists(master_file + ".hdf5"):
                master_file = master_file + ".hdf5"
            elif os.path.exists(master_file + ".h5"):
                master_file = master_file + ".h5"
185
            else:
payno's avatar
payno committed
186
                master_file = master_file + ".nx"
187
188
        return master_file

189
    @docstring(TomoScanBase.clear_caches)
payno's avatar
payno committed
190
    def clear_caches(self) -> None:
payno's avatar
payno committed
191
        super().clear_caches()
192
        self._projections = None
193
        self._projections_compacted = None
194
195
196
197
198
199
200
        self._flats = None
        self._darks = None
        self._tomo_n = None
        self._dark_n = None
        self._ref_n = None
        self._scan_range = None
        self._dim_1, self._dim_2 = None, None
201
202
        self._x_pixel_size = None
        self._y_pixel_size = None
203
204
        self._x_magnified_pixel_size = None
        self._y_magnified_pixel_size = None
205
        self._rotation_angles = None
payno's avatar
payno committed
206
        self._distance = None
payno's avatar
payno committed
207
        self._fov = None
208
        self._image_keys_control = None
209
210

    @staticmethod
payno's avatar
payno committed
211
    def _get_entry_at(index: int, file_path: str) -> str:
212
213
214
215
216
217
        """

        :param index:
        :param file_path:
        :return:
        """
218
        entries = HDF5TomoScan.get_valid_entries(file_path)
219
220
221
222
223
224
        if len(entries) == 0:
            return None
        else:
            return entries[index]

    @staticmethod
225
    def get_valid_entries(file_path: str) -> tuple:
226
227
228
229
230
231
232
233
234
235
236
237
        """
        return the list of 'Nxtomo' entries at the root level

        :param str file_path:
        :return: list of valid Nxtomo node (ordered alphabetically)
        :rtype: tuple

        ..note: entries are sorted to insure consistency
        """
        res = []
        res_buf = []

238
        if not os.path.isfile(file_path):
payno's avatar
payno committed
239
            raise ValueError("given file path should be a file")
240

payno's avatar
payno committed
241
        with HDF5File(file_path, "r", swmr=True) as h5f:
242
243
244
            for root_node in h5f.keys():
                node = h5f[root_node]
                if HDF5TomoScan.node_is_nxtomo(node) is True:
245
                    res_buf.append(root_node)  # cannnot be node because of sym links
246

247
            [res.append(node) for node in res_buf]
248
249
250
251
        res.sort()
        return tuple(res)

    @staticmethod
payno's avatar
payno committed
252
    def node_is_nxtomo(node: h5py.Group) -> bool:
253
        """check if the given h5py node is an nxtomo node or not"""
payno's avatar
payno committed
254
255
        if "NX_class" in node.attrs or "NXclass" in node.attrs:
            _logger.info(node.name + " is recognized as an nx class.")
256
        else:
payno's avatar
payno committed
257
            _logger.info(node.name + " is node an nx class.")
258
            return False
payno's avatar
payno committed
259
260
        if "definition" in node.attrs and node.attrs["definition"].lower() == "nxtomo":
            _logger.info(node.name + " is recognized as an NXtomo class.")
261
262
263
            return True
        else:
            return False
payno's avatar
payno committed
264
265
266

    @docstring(TomoScanBase.is_tomoscan_dir)
    @staticmethod
267
268
269
    def is_tomoscan_dir(directory: str, **kwargs) -> bool:
        if os.path.isfile(directory):
            master_file = directory
payno's avatar
payno committed
270
        else:
271
272
            master_file = HDF5TomoScan.get_master_file(scan_path=directory)
        if master_file:
273
            entries = HDF5TomoScan.get_valid_entries(file_path=master_file)
274
            return len(entries) > 0
payno's avatar
payno committed
275
276
277
278
279
280

    @docstring(TomoScanBase.is_abort)
    def is_abort(self, **kwargs):
        # for now there is no abort definition in .hdf5
        return False

281
282
283
    @docstring(TomoScanBase.to_dict)
    def to_dict(self) -> dict:
        res = super().to_dict()
284
        res[self.DICT_PATH_KEY] = self.master_file
285
286
287
        res[self._DICT_ENTRY_KEY] = self.entry
        return res

payno's avatar
payno committed
288
    @staticmethod
payno's avatar
payno committed
289
    def from_dict(_dict: dict):
payno's avatar
payno committed
290
291
292
293
294
        scan = HDF5TomoScan(scan=None)
        scan.load_from_dict(_dict=_dict)
        return scan

    @docstring(TomoScanBase.load_from_dict)
payno's avatar
payno committed
295
    def load_from_dict(self, _dict: dict) -> TomoScanBase:
payno's avatar
payno committed
296
297
298
299
300
301
302
303
304
        """

        :param _dict:
        :return:
        """
        if isinstance(_dict, io.TextIOWrapper):
            data = json.load(_dict)
        else:
            data = _dict
305
        if not (self.DICT_TYPE_KEY in data and data[self.DICT_TYPE_KEY] == self._TYPE):
payno's avatar
payno committed
306
            raise ValueError("Description is not an HDF5Scan json description")
307
        if HDF5TomoScan._DICT_ENTRY_KEY not in data:
payno's avatar
payno committed
308
            raise ValueError("No hdf5 entry specified")
payno's avatar
payno committed
309

310
        assert self.DICT_PATH_KEY in data
311
        self._entry = data[self._DICT_ENTRY_KEY]
payno's avatar
payno committed
312
313
314
315
316
317
        self.master_file = self.get_master_file(data[self.DICT_PATH_KEY])

        if os.path.isdir(data[self.DICT_PATH_KEY]):
            self.path = data[self.DICT_PATH_KEY]
        else:
            self.path = os.path.dirname(data[self.DICT_PATH_KEY])
payno's avatar
payno committed
318
319
        return self

320
    @property
payno's avatar
payno committed
321
    def entry(self) -> str:
322
323
        return self._entry

payno's avatar
payno committed
324
325
    @property
    @docstring(TomoScanBase.projections)
326
    def projections(self) -> typing.Union[dict, None]:
327
328
329
        """projections / radio, does not include the return projections"""
        if self._projections is None:
            if self.frames:
payno's avatar
payno committed
330
331
332
333
334
335
336
                proj_frames = tuple(
                    filter(
                        lambda x: x.image_key == ImageKey.PROJECTION
                        and x.is_control == False,
                        self.frames,
                    )
                )
337
338
339
                self._projections = {}
                for proj_frame in proj_frames:
                    self._projections[proj_frame.index] = proj_frame.url
payno's avatar
payno committed
340
341
342
        return self._projections

    @projections.setter
343
    def projections(self, projections: dict):
payno's avatar
payno committed
344
345
346
347
        self._projections = projections

    @property
    @docstring(TomoScanBase.darks)
348
    def darks(self) -> typing.Union[dict, None]:
349
350
        if self._darks is None:
            if self.frames:
payno's avatar
payno committed
351
352
353
                dark_frames = tuple(
                    filter(lambda x: x.image_key == ImageKey.DARK_FIELD, self.frames)
                )
354
355
356
                self._darks = {}
                for dark_frame in dark_frames:
                    self._darks[dark_frame.index] = dark_frame.url
357
        return self._darks
payno's avatar
payno committed
358
359
360

    @property
    @docstring(TomoScanBase.flats)
361
    def flats(self) -> typing.Union[dict, None]:
362
363
        if self._flats is None:
            if self.frames:
payno's avatar
payno committed
364
365
366
                flat_frames = tuple(
                    filter(lambda x: x.image_key == ImageKey.FLAT_FIELD, self.frames)
                )
367
368
369
                self._flats = {}
                for flat_frame in flat_frames:
                    self._flats[flat_frame.index] = flat_frame.url
370
        return self._flats
payno's avatar
payno committed
371
372

    @docstring(TomoScanBase.update)
payno's avatar
payno committed
373
    def update(self) -> None:
payno's avatar
payno committed
374
        """update list of radio and reconstruction by parsing the scan folder"""
375
        if self.master_file is None or not os.path.exists(self.master_file):
payno's avatar
payno committed
376
            return
payno's avatar
payno committed
377
        self.projections = self._get_projections_url()
payno's avatar
payno committed
378
379
        # TODO: update darks and flats too

payno's avatar
payno committed
380
    @docstring(TomoScanBase.get_proj_angle_url)
payno's avatar
payno committed
381
382
383
    def _get_projections_url(self):
        if self.master_file is None or not os.path.exists(self.master_file):
            return
384
385
        frames = self.frames
        if frames is not None:
386
            urls = {}
387
388
389
            for frame in frames:
                if frame.image_key is ImageKey.PROJECTION:
                    urls[frame.index] = frame.url
payno's avatar
payno committed
390
            return urls
391
392
        else:
            return None
payno's avatar
payno committed
393
394
395

    @docstring(TomoScanBase.tomo_n)
    @property
payno's avatar
payno committed
396
    def tomo_n(self) -> typing.Union[None, int]:
397
398
399
400
        """we are making two asumptions for computing tomo_n:
        - if a rotation = scan_range +/- EPSILON this is a return projection
        - The delta between each projections is constant
        """
payno's avatar
payno committed
401
402
403
404
405
        if (
            self._tomo_n is None
            and self.master_file
            and os.path.exists(self.master_file)
        ):
406
            if self.projections:
407
                return len(self.projections)
408
409
410
411
412
413
            else:
                return None
        else:
            return None

    @property
payno's avatar
payno committed
414
    def return_projs(self) -> typing.Union[None, list]:
415
416
417
        """"""
        frames = self.frames
        if frames:
418
            return_frames = list(filter(lambda x: x.is_control == True, frames))
419
420
421
422
423
            return return_frames
        else:
            return None

    @property
payno's avatar
payno committed
424
    def rotation_angle(self) -> typing.Union[None, list]:
425
        if self._rotation_angles is None:
426
            self._check_hdf5scan_validity()
payno's avatar
payno committed
427
            with HDF5File(self.master_file, "r", swmr=True) as h5_file:
428
429
                _rotation_angles = h5_file[self._entry][self._ROTATION_ANGLE_PATH][()]
                # cast in float
payno's avatar
payno committed
430
431
432
                self._rotation_angles = tuple(
                    [float(angle) for angle in _rotation_angles]
                )
433
434
435
        return self._rotation_angles

    @property
payno's avatar
payno committed
436
    def image_key(self) -> typing.Union[list, None]:
437
438
        if self._entry and self._image_keys is None:
            self._check_hdf5scan_validity()
payno's avatar
payno committed
439
            with HDF5File(self.master_file, "r", swmr=True) as h5_file:
440
441
                self._image_keys = h5_file[self._entry][self._IMG_KEY_PATH][()]
        return self._image_keys
payno's avatar
payno committed
442

443
444
445
446
    @property
    def image_key_control(self) -> typing.Union[list, None]:
        if self._entry and self._image_keys_control is None:
            self._check_hdf5scan_validity()
payno's avatar
payno committed
447
            with HDF5File(self.master_file, "r", swmr=True) as h5_file:
448
                if self._IMG_KEY_CONTROL_PATH in h5_file[self._entry]:
payno's avatar
payno committed
449
450
451
                    self._image_keys_control = h5_file[self._entry][
                        self._IMG_KEY_CONTROL_PATH
                    ][()]
452
453
454
455
                else:
                    self._image_keys_control = None
        return self._image_keys_control

payno's avatar
payno committed
456
457
    @docstring(TomoScanBase.dark_n)
    @property
payno's avatar
payno committed
458
    def dark_n(self) -> typing.Union[None, int]:
459
460
461
462
        if self.darks is not None:
            return len(self.darks)
        else:
            return None
payno's avatar
payno committed
463
464
465

    @docstring(TomoScanBase.ref_n)
    @property
payno's avatar
payno committed
466
    def ref_n(self) -> typing.Union[None, int]:
467
468
469
470
        if self.flats is not None:
            return len(self.flats)
        else:
            return None
payno's avatar
payno committed
471

payno's avatar
payno committed
472
    @docstring(TomoScanBase.ff_interval)
payno's avatar
payno committed
473
    @property
payno's avatar
payno committed
474
    def ff_interval(self):
payno's avatar
payno committed
475
476
477
        raise NotImplementedError(
            "not implemented for hdf5. But we have " "acquisition sequence instead."
        )
payno's avatar
payno committed
478
479
480

    @docstring(TomoScanBase.scan_range)
    @property
payno's avatar
payno committed
481
    def scan_range(self) -> typing.Union[None, int]:
482
        """For now scan range should return 180 or 360. We don't expect other value."""
payno's avatar
payno committed
483
484
485
486
487
488
        if (
            self._scan_range is None
            and self.master_file
            and os.path.exists(self.master_file)
            and self._entry is not None
        ):
489
490
491
492
493
494
495
496
            rotation_angle = self.rotation_angle
            if rotation_angle is not None:
                dist_to180 = abs(180 - numpy.max(rotation_angle))
                dist_to360 = abs(360 - numpy.max(rotation_angle))
                if dist_to180 < dist_to360:
                    self._scan_range = 180
                else:
                    self._scan_range = 360
payno's avatar
payno committed
497
498
499
        return self._scan_range

    @property
payno's avatar
payno committed
500
    def dim_1(self) -> typing.Union[None, int]:
501
502
        if self._dim_1 is None:
            self._get_dim1_dim2()
payno's avatar
payno committed
503
504
505
        return self._dim_1

    @property
payno's avatar
payno committed
506
    def dim_2(self) -> typing.Union[None, int]:
507
508
        if self._dim_2 is None:
            self._get_dim1_dim2()
payno's avatar
payno committed
509
510
511
        return self._dim_2

    @property
payno's avatar
payno committed
512
    def pixel_size(self) -> typing.Union[None, float]:
513
        """return x pixel size in meter"""
514
515
516
        return self.x_pixel_size

    @property
payno's avatar
payno committed
517
    def x_pixel_size(self) -> typing.Union[None, float]:
518
        """return x pixel size in meter"""
payno's avatar
payno committed
519
520
521
522
523
        if (
            self._x_pixel_size is None
            and self.master_file
            and os.path.exists(self.master_file)
        ):
524
525
526
527
528
529
            self._x_pixel_size, self._y_pixel_size = self._get_x_y_pixel_values()

        return self._x_pixel_size

    def _get_x_y_pixel_values(self):
        """read x and y pixel values"""
payno's avatar
payno committed
530
        with HDF5File(self.master_file, "r", swmr=True) as h5_file:
531
            x_pixel_dataset = h5_file[self._entry][self._X_PIXEL_SIZE_PATH]
payno's avatar
payno committed
532
            _x_pixel_size = self._get_value(x_pixel_dataset, default_unit="meter")
533
            y_pixel_dataset = h5_file[self._entry][self._Y_PIXEL_SIZE_PATH]
payno's avatar
payno committed
534
            _y_pixel_size = self._get_value(y_pixel_dataset, default_unit="meter")
535
536
        return _x_pixel_size, _y_pixel_size

537
    def _get_x_y_magnified_pixel_values(self):
payno's avatar
payno committed
538
        with HDF5File(self.master_file, "r", swmr=True) as h5_file:
539
            x_m_pixel_dataset = h5_file[self._entry][self._X_PIXEL_MAG_SIZE_PATH]
payno's avatar
payno committed
540
            _x_m_pixel_size = self._get_value(x_m_pixel_dataset, default_unit="meter")
541
            y_m_pixel_dataset = h5_file[self._entry][self._Y_PIXEL_MAG_SIZE_PATH]
payno's avatar
payno committed
542
            _y_m_pixel_size = self._get_value(y_m_pixel_dataset, default_unit="meter")
543
544
        return _x_m_pixel_size, _y_m_pixel_size

payno's avatar
payno committed
545
    def _get_fov(self):
payno's avatar
payno committed
546
        with HDF5File(self.master_file, "r", swmr=True, libver="latest") as h5_file:
payno's avatar
payno committed
547
548
549
550
551
552
            if self._FOV_PATH in h5_file[self._entry]:
                fov = h5_file[self._entry][self._FOV_PATH][()]
                return _FOV.from_value(fov)
            else:
                return None

553
554
555
556
    def _get_dim1_dim2(self):
        if self.master_file and os.path.exists(self.master_file):
            if self.projections is not None:
                if len(self.projections) > 0:
payno's avatar
payno committed
557
558
559
                    self._dim_2, self._dim_1 = get_data(
                        list(self.projections.values())[0]
                    ).shape
560

561
    @property
payno's avatar
payno committed
562
    def y_pixel_size(self) -> typing.Union[None, float]:
563
        """return y pixel size in meter"""
payno's avatar
payno committed
564
565
566
567
568
        if (
            self._y_pixel_size is None
            and self.master_file
            and os.path.exists(self.master_file)
        ):
569
570
            self._x_pixel_size, self._y_pixel_size = self._get_x_y_pixel_values()
        return self._y_pixel_size
payno's avatar
payno committed
571

572
573
    @property
    def x_magnified_pixel_size(self) -> typing.Union[None, float]:
574
        """return x magnified pixel size in meter"""
payno's avatar
payno committed
575
576
577
578
579
580
581
582
583
        if (
            self._x_magnified_pixel_size is None
            and self.master_file
            and os.path.exists(self.master_file)
        ):
            (
                self._x_magnified_pixel_size,
                self._y_magnified_pixel_size,
            ) = self._get_x_y_magnified_pixel_values()
584
585
586
587
        return self._x_magnified_pixel_size

    @property
    def y_magnified_pixel_size(self) -> typing.Union[None, float]:
588
        """return y magnified pixel size in meter"""
payno's avatar
payno committed
589
590
591
592
593
594
595
596
597
        if (
            self._y_magnified_pixel_size is None
            and self.master_file
            and os.path.exists(self.master_file)
        ):
            (
                self._x_magnified_pixel_size,
                self._y_magnified_pixel_size,
            ) = self._get_x_y_magnified_pixel_values()
598
599
        return self._y_magnified_pixel_size

payno's avatar
payno committed
600
    @property
payno's avatar
payno committed
601
    def distance(self) -> typing.Union[None, float]:
602
        """return sample detector distance in meter"""
payno's avatar
payno committed
603
604
605
606
607
        if (
            self._distance is None
            and self.master_file
            and os.path.exists(self.master_file)
        ):
608
            self._check_hdf5scan_validity()
payno's avatar
payno committed
609
            with HDF5File(self.master_file, "r", swmr=True) as h5_file:
payno's avatar
payno committed
610
                distance_dataset = h5_file[self._entry][self._DISTANCE_PATH]
payno's avatar
payno committed
611
                self._distance = self._get_value(distance_dataset, default_unit="m")
payno's avatar
payno committed
612
613
        return self._distance

payno's avatar
payno committed
614
615
616
617
618
619
    @property
    def field_of_view(self):
        """

        :return: field of view of the scan. None if unknow else Full or Half
        """
payno's avatar
payno committed
620
        if self._fov is None and self.master_file and os.path.exists(self.master_file):
payno's avatar
payno committed
621
622
623
            self._fov = self._get_fov()
        return self._fov

624
625
    @property
    def energy(self) -> typing.Union[None, float]:
626
        """energy in keV"""
payno's avatar
payno committed
627
628
629
630
631
        if (
            self._energy is None
            and self.master_file
            and os.path.exists(self.master_file)
        ):
632
            self._check_hdf5scan_validity()
payno's avatar
payno committed
633
            with HDF5File(self.master_file, "r", swmr=True) as h5_file:
634
                energy_dataset = h5_file[self._entry][self._ENERGY_PATH]
payno's avatar
payno committed
635
                self._energy = self._get_value(energy_dataset, default_unit="keV")
636
637
        return self._energy

638
    @property
payno's avatar
payno committed
639
    def frames(self) -> typing.Union[None, tuple]:
640
641
642
643
644
        """return tuple of frames. Frames contains """
        if self._frames is None:
            image_keys = self.image_key
            rotation_angles = self.rotation_angle
            if len(image_keys) != len(rotation_angles):
payno's avatar
payno committed
645
646
647
648
649
                raise ValueError(
                    "`rotation_angle` and `image_key` have "
                    "incoherent size (%s vs %s). Unable to "
                    "deduce frame properties" % (len(rotation_angles), len(image_keys))
                )
650
651
            self._frames = []

payno's avatar
payno committed
652
653
654
            def is_return(
                lframe, llast_proj_frame, ldelta_angle, return_already_reach
            ) -> tuple:
655
656
657
658
                """return is_return, delta_angle"""
                if ImageKey.from_value(img_key) is not ImageKey.PROJECTION:
                    return False, None
                if ldelta_angle is None and llast_proj_frame is not None:
payno's avatar
payno committed
659
660
661
                    delta_angle = (
                        lframe.rotation_angle - llast_proj_frame.rotation_angle
                    )
662
663
664
665
                    return False, delta_angle
                elif return_already_reach:
                    return True, ldelta_angle
                else:
payno's avatar
payno committed
666
667
668
669
                    current_angle = (
                        lframe.rotation_angle - llast_proj_frame.rotation_angle
                    )
                    return abs(current_angle) <= 2 * ldelta_angle, ldelta_angle
670
671
672
673

            delta_angle = None
            last_proj_frame = None
            return_already_reach = False
payno's avatar
payno committed
674
675
676
677
678
679
680
681
682
683
684
685
686
            for i_frame, rot_a, img_key in zip(
                range(len(rotation_angles)), rotation_angles, image_keys
            ):
                url = DataUrl(
                    file_path=self.master_file,
                    data_slice=(i_frame),
                    data_path=self.entry + "/instrument/detector/data",
                    scheme="silx",
                )

                frame = Frame(
                    index=i_frame, url=url, image_key=img_key, rotation_angle=rot_a
                )
687
                if self.image_key_control is not None:
payno's avatar
payno committed
688
689
690
                    is_control_frame = (
                        self.image_key_control[frame.index] == ImageKey.ALIGNMENT.value
                    )
691
                else:
payno's avatar
payno committed
692
693
694
695
696
697
                    return_already_reach, delta_angle = is_return(
                        lframe=frame,
                        llast_proj_frame=last_proj_frame,
                        ldelta_angle=delta_angle,
                        return_already_reach=return_already_reach,
                    )
698
                    is_control_frame = return_already_reach
699
                frame._is_control_frame = is_control_frame
700
701
702
703
704
                self._frames.append(frame)
                last_proj_frame = frame
            self._frames = tuple(self._frames)
        return self._frames

payno's avatar
payno committed
705
    @docstring(TomoScanBase.get_proj_angle_url)
payno's avatar
payno committed
706
    def get_proj_angle_url(self) -> typing.Union[dict, None]:
707
708
709
710
        if self.frames is not None:
            res = {}
            for frame in self.frames:
                if frame.image_key is ImageKey.PROJECTION:
711
                    if frame.is_control is False:
712
713
                        res[frame.rotation_angle] = frame.url
                    else:
payno's avatar
payno committed
714
                        res[str(frame.rotation_angle) + "(1)"] = frame.url
715
716
717
            return res
        else:
            return None
718

719
720
721
722
723
    @property
    def projections_compacted(self):
        """
        Return a compacted view of projection frames.

payno's avatar
payno committed
724
        :return: Dictionary where the key is a list of indices, and the value
725
            is the corresponding `silx.io.url.DataUrl` with merged data_slice
payno's avatar
payno committed
726
        :rtype: dict
727
728
        """
        if self._projections_compacted is None:
729
            self._projections_compacted = get_compacted_dataslices(self.projections)
730
731
        return self._projections_compacted

732
    def __str__(self):
payno's avatar
payno committed
733
734
735
736
737
        return "hdf5 scan(path: %s, master_file: %s, entry: %s)" % (
            self.path,
            self.master_file,
            self.entry,
        )
738

739
740
741
742
743
744
745
    @staticmethod
    def _get_value(node: h5py.Group, default_unit: str):
        """convert the value contained in the node to the adapted unit.
        Unit can be defined in on of the group attributes. It it is the case
        will pick this unit, otherwise will use the default unit
        """
        value = node[()]
payno's avatar
payno committed
746
747
748
749
        if "unit" in node.attrs:
            unit = node.attrs["unit"]
        elif "units" in node.attrs:
            unit = node.attrs["units"]
750
751
        else:
            unit = default_unit
752
        return value * metricsystem.MetricSystem.from_value(unit).value
753

754
755
    def _check_hdf5scan_validity(self):
        if self.master_file is None:
payno's avatar
payno committed
756
            raise ValueError("No master file provided")
757
        if self.entry is None:
payno's avatar
payno committed
758
759
            raise ValueError("No entry provided")
        with HDF5File(self.master_file, "r", swmr=True) as h5_file:
760
            if self._entry not in h5_file:
payno's avatar
payno committed
761
762
763
764
                raise ValueError(
                    "Given entry %s is not in the master "
                    "file %s" % (self._entry, self.master_file)
                )
765

766
767
768

class Frame:
    """class to store all metadata information of a frame"""
payno's avatar
payno committed
769
770
771
772
773
774
775
776
777

    def __init__(
        self,
        index: int,
        url: typing.Union[None, DataUrl] = None,
        image_key: typing.Union[None, ImageKey, int] = None,
        rotation_angle: typing.Union[None, float] = None,
        is_control_proj: bool = False,
    ):
778
779
780
781
782
        assert type(index) is int
        self._index = index
        self._image_key = ImageKey.from_value(image_key)
        self._rotation_angle = rotation_angle
        self._url = url
783
        self._is_control_frame = is_control_proj
784
785
786
        self._data = None

    @property
payno's avatar
payno committed
787
    def index(self) -> int:
788
789
790
        return self._index

    @property
payno's avatar
payno committed
791
    def image_key(self) -> ImageKey:
792
793
794
        return self._image_key

    @image_key.setter
payno's avatar
payno committed
795
    def image_key(self, image_key: ImageKey) -> None:
796
797
798
799
800
801
802
        self._image_key = image_key

    @property
    def rotation_angle(self) -> float:
        return self._rotation_angle

    @rotation_angle.setter
payno's avatar
payno committed
803
    def rotation_angle(self, angle: float) -> None:
804
805
806
        self._rotation_angle = angle

    @property
payno's avatar
payno committed
807
    def url(self) -> DataUrl:
808
809
810
        return self._url

    @property
811
812
    def is_control(self) -> bool:
        return self._is_control_frame
813

814
815
816
    @is_control.setter
    def is_control(self, is_return: bool):
        self._is_control_frame = is_return