hdf5scan.py 27.6 KB
Newer Older
payno's avatar
payno committed
1
2
# coding: utf-8
#/*##########################################################################
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 silx.io.utils import get_data
43
from ..unitsystem import metricsystem
44
from .utils import get_compacted_dataslices
payno's avatar
payno committed
45
import logging
46
import typing
payno's avatar
payno committed
47
48
49
50

_logger = logging.getLogger(__name__)


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


payno's avatar
payno committed
59
60
61
62
63
64
65
66
67
68
69
70
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
71
72
73
74
75
    :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
76
77
78
    """
    _TYPE = 'hdf5'

79
    _DICT_ENTRY_KEY = 'entry'
payno's avatar
payno committed
80

81
    _PROJ_PATH = 'instrument/detector/data'
payno's avatar
payno committed
82
83
84
85
86

    _SCAN_META_PATH = 'scan_meta/technique/scan'

    _DET_META_PATH = 'scan_meta/technique/detector'

87
88
89
90
    _ROTATION_ANGLE_PATH = 'sample/rotation_angle'

    _IMG_KEY_PATH = 'instrument/detector/image_key'

91
92
    _IMG_KEY_CONTROL_PATH = 'instrument/detector/image_key_control'

93
94
95
96
    _X_PIXEL_SIZE_PATH = 'instrument/detector/x_pixel_size'

    _Y_PIXEL_SIZE_PATH = 'instrument/detector/y_pixel_size'

97
98
99
100
    _X_PIXEL_MAG_SIZE_PATH = 'instrument/detector/x_magnified_pixel_size'

    _Y_PIXEL_MAG_SIZE_PATH = 'instrument/detector/y_magnified_pixel_size'

payno's avatar
payno committed
101
102
    _DISTANCE_PATH = 'instrument/detector/distance'

payno's avatar
payno committed
103
104
    _FOV_PATH = 'instrument/detector/field_of_view'

105
106
    _ENERGY_PATH = 'beam/incident_energy'

payno's avatar
payno committed
107
108
    _SCHEME = 'silx'

109
110
    _EPSILON_ROT_ANGLE = 0.02

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

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

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

        # data caches
payno's avatar
payno committed
140
        self._projections = None
141
        self._projections_compacted = None
142
143
        self._flats = None
        self._darks = None
payno's avatar
payno committed
144
145
146
147
148
149
150
151
152
153
        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
154
155
        self._x_pixel_size = None
        self._y_pixel_size = None
156
157
        self._x_magnified_pixel_size = None
        self._y_magnified_pixel_size = None
payno's avatar
payno committed
158
        # pixel dimensions (tuple)
159
160
        self._frames = None
        self._image_keys = None
161
        self._image_keys_control = None
162
        self._rotation_angles = None
payno's avatar
payno committed
163
        self._distance = None
payno's avatar
payno committed
164
        self._fov = None
165
        self._energy = None
166

167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
    @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))
            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'
            else:
                master_file = master_file + '.nx'
        return master_file

183
    @docstring(TomoScanBase.clear_caches)
payno's avatar
payno committed
184
    def clear_caches(self) -> None:
185
        self._projections = None
186
        self._projections_compacted = None
187
188
189
190
191
192
193
        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
194
195
        self._x_pixel_size = None
        self._y_pixel_size = None
196
197
        self._x_magnified_pixel_size = None
        self._y_magnified_pixel_size = None
198
        self._rotation_angles = None
payno's avatar
payno committed
199
        self._distance = None
payno's avatar
payno committed
200
        self._fov = None
201
        self._image_keys_control = None
202
203

    @staticmethod
payno's avatar
payno committed
204
    def _get_entry_at(index: int, file_path: str) -> str:
205
206
207
208
209
210
        """

        :param index:
        :param file_path:
        :return:
        """
211
        entries = HDF5TomoScan.get_valid_entries(file_path)
212
213
214
215
216
217
        if len(entries) == 0:
            return None
        else:
            return entries[index]

    @staticmethod
218
    def get_valid_entries(file_path: str) -> tuple:
219
220
221
222
223
224
225
226
227
228
229
230
        """
        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 = []

231
232
233
        if not os.path.isfile(file_path):
            raise ValueError('given file path should be a file')

payno's avatar
payno committed
234
        with h5py.File(file_path, 'r', swmr=True) as h5f:
235
236
237
            for root_node in h5f.keys():
                node = h5f[root_node]
                if HDF5TomoScan.node_is_nxtomo(node) is True:
238
                    res_buf.append(root_node)  # cannnot be node because of sym links
239

240
            [res.append(node) for node in res_buf]
241
242
243
244
        res.sort()
        return tuple(res)

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

    @docstring(TomoScanBase.is_tomoscan_dir)
    @staticmethod
260
261
262
    def is_tomoscan_dir(directory: str, **kwargs) -> bool:
        if os.path.isfile(directory):
            master_file = directory
payno's avatar
payno committed
263
        else:
264
265
            master_file = HDF5TomoScan.get_master_file(scan_path=directory)
        if master_file:
266
            entries = HDF5TomoScan.get_valid_entries(file_path=master_file)
267
            return len(entries) > 0
payno's avatar
payno committed
268
269
270
271
272
273

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

274
275
276
    @docstring(TomoScanBase.to_dict)
    def to_dict(self) -> dict:
        res = super().to_dict()
277
        res[self.DICT_PATH_KEY] = self.master_file
278
279
280
        res[self._DICT_ENTRY_KEY] = self.entry
        return res

payno's avatar
payno committed
281
    @staticmethod
payno's avatar
payno committed
282
    def from_dict(_dict: dict):
payno's avatar
payno committed
283
284
285
286
287
        scan = HDF5TomoScan(scan=None)
        scan.load_from_dict(_dict=_dict)
        return scan

    @docstring(TomoScanBase.load_from_dict)
payno's avatar
payno committed
288
    def load_from_dict(self, _dict: dict) -> TomoScanBase:
payno's avatar
payno committed
289
290
291
292
293
294
295
296
297
        """

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

303
        assert self.DICT_PATH_KEY in data
304
        self._entry = data[self._DICT_ENTRY_KEY]
305
306
307
308
309
310
        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
311
312
        return self

313
    @property
payno's avatar
payno committed
314
    def entry(self) -> str:
315
316
        return self._entry

payno's avatar
payno committed
317
318
    @property
    @docstring(TomoScanBase.projections)
319
    def projections(self) -> typing.Union[dict, None]:
320
321
322
        """projections / radio, does not include the return projections"""
        if self._projections is None:
            if self.frames:
323
                proj_frames = tuple(filter(lambda x: x.image_key == ImageKey.PROJECTION and x.is_control == False, self.frames))
324
325
326
                self._projections = {}
                for proj_frame in proj_frames:
                    self._projections[proj_frame.index] = proj_frame.url
payno's avatar
payno committed
327
328
329
        return self._projections

    @projections.setter
330
    def projections(self, projections: dict):
payno's avatar
payno committed
331
332
333
334
        self._projections = projections

    @property
    @docstring(TomoScanBase.darks)
335
    def darks(self) -> typing.Union[dict, None]:
336
337
        if self._darks is None:
            if self.frames:
338
                dark_frames = tuple(filter(lambda x: x.image_key == ImageKey.DARK_FIELD, self.frames))
339
340
341
                self._darks = {}
                for dark_frame in dark_frames:
                    self._darks[dark_frame.index] = dark_frame.url
342
        return self._darks
payno's avatar
payno committed
343
344
345

    @property
    @docstring(TomoScanBase.flats)
346
    def flats(self) -> typing.Union[dict, None]:
347
348
        if self._flats is None:
            if self.frames:
349
                flat_frames = tuple(filter(lambda x: x.image_key == ImageKey.FLAT_FIELD, self.frames))
350
351
352
                self._flats = {}
                for flat_frame in flat_frames:
                    self._flats[flat_frame.index] = flat_frame.url
353
        return self._flats
payno's avatar
payno committed
354
355

    @docstring(TomoScanBase.update)
payno's avatar
payno committed
356
    def update(self) -> None:
payno's avatar
payno committed
357
358
        """update list of radio and reconstruction by parsing the scan folder
        """
359
        if self.master_file is None or not os.path.exists(self.master_file):
payno's avatar
payno committed
360
            return
payno's avatar
payno committed
361
        self.projections = self._get_projections_url()
payno's avatar
payno committed
362
363
        # TODO: update darks and flats too

payno's avatar
payno committed
364
    @docstring(TomoScanBase.get_proj_angle_url)
payno's avatar
payno committed
365
366
367
    def _get_projections_url(self):
        if self.master_file is None or not os.path.exists(self.master_file):
            return
368
369
        frames = self.frames
        if frames is not None:
370
            urls = {}
371
372
373
            for frame in frames:
                if frame.image_key is ImageKey.PROJECTION:
                    urls[frame.index] = frame.url
payno's avatar
payno committed
374
            return urls
375
376
        else:
            return None
payno's avatar
payno committed
377
378
379

    @docstring(TomoScanBase.tomo_n)
    @property
payno's avatar
payno committed
380
    def tomo_n(self) -> typing.Union[None, int]:
381
382
383
384
        """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
385
386
        if (self._tomo_n is None and self.master_file and
                os.path.exists(self.master_file)):
387
            if self.projections:
388
                return len(self.projections)
389
390
391
392
393
394
            else:
                return None
        else:
            return None

    @property
payno's avatar
payno committed
395
    def return_projs(self) -> typing.Union[None, list]:
396
397
398
        """"""
        frames = self.frames
        if frames:
399
            return_frames = list(filter(lambda x: x.is_control == True, frames))
400
401
402
403
404
            return return_frames
        else:
            return None

    @property
payno's avatar
payno committed
405
    def rotation_angle(self) -> typing.Union[None, list]:
406
        if self._rotation_angles is None:
407
            self._check_hdf5scan_validity()
payno's avatar
payno committed
408
            with h5py.File(self.master_file, 'r', swmr=True) as h5_file:
409
410
411
412
413
414
                _rotation_angles = h5_file[self._entry][self._ROTATION_ANGLE_PATH][()]
                # cast in float
                self._rotation_angles = tuple([float(angle) for angle in _rotation_angles])
        return self._rotation_angles

    @property
payno's avatar
payno committed
415
    def image_key(self) -> typing.Union[list, None]:
416
417
        if self._entry and self._image_keys is None:
            self._check_hdf5scan_validity()
payno's avatar
payno committed
418
            with h5py.File(self.master_file, 'r', swmr=True) as h5_file:
419
420
                self._image_keys = h5_file[self._entry][self._IMG_KEY_PATH][()]
        return self._image_keys
payno's avatar
payno committed
421

422
423
424
425
    @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
426
            with h5py.File(self.master_file, 'r', swmr=True) as h5_file:
427
428
429
430
431
432
                if self._IMG_KEY_CONTROL_PATH in h5_file[self._entry]:
                    self._image_keys_control = h5_file[self._entry][self._IMG_KEY_CONTROL_PATH][()]
                else:
                    self._image_keys_control = None
        return self._image_keys_control

payno's avatar
payno committed
433
434
    @docstring(TomoScanBase.dark_n)
    @property
payno's avatar
payno committed
435
    def dark_n(self) -> typing.Union[None, int]:
436
437
438
439
        if self.darks is not None:
            return len(self.darks)
        else:
            return None
payno's avatar
payno committed
440
441
442

    @docstring(TomoScanBase.ref_n)
    @property
payno's avatar
payno committed
443
    def ref_n(self) -> typing.Union[None, int]:
444
445
446
447
        if self.flats is not None:
            return len(self.flats)
        else:
            return None
payno's avatar
payno committed
448

payno's avatar
payno committed
449
    @docstring(TomoScanBase.ff_interval)
payno's avatar
payno committed
450
    @property
payno's avatar
payno committed
451
    def ff_interval(self):
452
453
        raise NotImplementedError('not implemented for hdf5. But we have '
                                  'acquisition sequence instead.')
payno's avatar
payno committed
454
455
456

    @docstring(TomoScanBase.scan_range)
    @property
payno's avatar
payno committed
457
    def scan_range(self) -> typing.Union[None, int]:
458
459
460
461
462
463
464
465
466
467
468
        """For now scan range should return 180 or 360. We don't expect other value."""
        if (self._scan_range is None and self.master_file and
                os.path.exists(self.master_file) and self._entry is not None):
            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
469
470
471
        return self._scan_range

    @property
payno's avatar
payno committed
472
    def dim_1(self) -> typing.Union[None, int]:
473
474
        if self._dim_1 is None:
            self._get_dim1_dim2()
payno's avatar
payno committed
475
476
477
        return self._dim_1

    @property
payno's avatar
payno committed
478
    def dim_2(self) -> typing.Union[None, int]:
479
480
        if self._dim_2 is None:
            self._get_dim1_dim2()
payno's avatar
payno committed
481
482
483
        return self._dim_2

    @property
payno's avatar
payno committed
484
    def pixel_size(self) -> typing.Union[None, float]:
485
        """return x pixel size in meter"""
486
487
488
        return self.x_pixel_size

    @property
payno's avatar
payno committed
489
    def x_pixel_size(self) -> typing.Union[None, float]:
490
        """return x pixel size in meter"""
payno's avatar
payno committed
491
492
        if (self._x_pixel_size is None and self.master_file and
                os.path.exists(self.master_file)):
493
494
495
496
497
498
            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
499
        with h5py.File(self.master_file, 'r', swmr=True) as h5_file:
500
            x_pixel_dataset = h5_file[self._entry][self._X_PIXEL_SIZE_PATH]
501
            _x_pixel_size = self._get_value(x_pixel_dataset, default_unit='meter')
502
            y_pixel_dataset = h5_file[self._entry][self._Y_PIXEL_SIZE_PATH]
503
            _y_pixel_size = self._get_value(y_pixel_dataset, default_unit='meter')
504
505
        return _x_pixel_size, _y_pixel_size

506
    def _get_x_y_magnified_pixel_values(self):
payno's avatar
payno committed
507
        with h5py.File(self.master_file, 'r', swmr=True) as h5_file:
508
            x_m_pixel_dataset = h5_file[self._entry][self._X_PIXEL_MAG_SIZE_PATH]
509
            _x_m_pixel_size = self._get_value(x_m_pixel_dataset, default_unit='meter')
510
            y_m_pixel_dataset = h5_file[self._entry][self._Y_PIXEL_MAG_SIZE_PATH]
511
            _y_m_pixel_size = self._get_value(y_m_pixel_dataset, default_unit='meter')
512
513
        return _x_m_pixel_size, _y_m_pixel_size

payno's avatar
payno committed
514
515
516
517
518
519
520
521
    def _get_fov(self):
        with h5py.File(self.master_file, 'r', swmr=True, libver='latest') as h5_file:
            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

522
523
524
525
526
527
    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:
                    self._dim_2, self._dim_1 = get_data(list(self.projections.values())[0]).shape

528
    @property
payno's avatar
payno committed
529
    def y_pixel_size(self) -> typing.Union[None, float]:
530
        """return y pixel size in meter"""
payno's avatar
payno committed
531
532
        if (self._y_pixel_size is None and self.master_file and
                os.path.exists(self.master_file)):
533
534
            self._x_pixel_size, self._y_pixel_size = self._get_x_y_pixel_values()
        return self._y_pixel_size
payno's avatar
payno committed
535

536
537
    @property
    def x_magnified_pixel_size(self) -> typing.Union[None, float]:
538
        """return x magnified pixel size in meter"""
539
540
541
542
543
544
545
        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()
        return self._x_magnified_pixel_size

    @property
    def y_magnified_pixel_size(self) -> typing.Union[None, float]:
546
        """return y magnified pixel size in meter"""
547
548
549
550
551
        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()
        return self._y_magnified_pixel_size

payno's avatar
payno committed
552
    @property
payno's avatar
payno committed
553
    def distance(self) -> typing.Union[None, float]:
554
        """return sample detector distance in meter"""
payno's avatar
payno committed
555
556
        if (self._distance is None and self.master_file and
                os.path.exists(self.master_file)):
557
            self._check_hdf5scan_validity()
payno's avatar
payno committed
558
            with h5py.File(self.master_file, 'r', swmr=True) as h5_file:
payno's avatar
payno committed
559
560
561
562
                distance_dataset = h5_file[self._entry][self._DISTANCE_PATH]
                self._distance = self._get_value(distance_dataset, default_unit='m')
        return self._distance

payno's avatar
payno committed
563
564
565
566
567
568
569
570
571
572
    @property
    def field_of_view(self):
        """

        :return: field of view of the scan. None if unknow else Full or Half
        """
        if (self._fov is None and self.master_file and os.path.exists(self.master_file)):
            self._fov = self._get_fov()
        return self._fov

573
574
    @property
    def energy(self) -> typing.Union[None, float]:
575
        """energy in keV"""
576
577
        if (self._energy is None and self.master_file and
                os.path.exists(self.master_file)):
578
            self._check_hdf5scan_validity()
payno's avatar
payno committed
579
            with h5py.File(self.master_file, 'r', swmr=True) as h5_file:
580
581
582
583
                energy_dataset = h5_file[self._entry][self._ENERGY_PATH]
                self._energy = self._get_value(energy_dataset, default_unit='keV')
        return self._energy

584
    @property
payno's avatar
payno committed
585
    def frames(self) -> typing.Union[None, tuple]:
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
        """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):
                raise ValueError('`rotation_angle` and `image_key` have '
                                 'incoherent size (%s vs %s). Unable to '
                                 'deduce frame properties' % (len(rotation_angles),
                                                              len(image_keys)))
            self._frames = []

            def is_return(lframe, llast_proj_frame, ldelta_angle, return_already_reach) -> tuple:
                """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:
                    delta_angle = lframe.rotation_angle - llast_proj_frame.rotation_angle
                    return False, delta_angle
                elif return_already_reach:
                    return True, ldelta_angle
                else:
                    current_angle = lframe.rotation_angle - llast_proj_frame.rotation_angle
                    return abs(current_angle) <= 2*ldelta_angle, ldelta_angle

            delta_angle = None
            last_proj_frame = None
            return_already_reach = False
            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)
621
                if self.image_key_control is not None:
622
                    is_control_frame = (self.image_key_control[frame.index] == ImageKey.ALIGNMENT.value)
623
624
625
626
627
628
                else:
                    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)
                    is_control_frame = return_already_reach
629
                frame._is_control_frame = is_control_frame
630
631
632
633
634
                self._frames.append(frame)
                last_proj_frame = frame
            self._frames = tuple(self._frames)
        return self._frames

payno's avatar
payno committed
635
    @docstring(TomoScanBase.get_proj_angle_url)
payno's avatar
payno committed
636
    def get_proj_angle_url(self) -> typing.Union[dict, None]:
637
638
639
640
        if self.frames is not None:
            res = {}
            for frame in self.frames:
                if frame.image_key is ImageKey.PROJECTION:
641
                    if frame.is_control is False:
642
643
644
645
646
647
                        res[frame.rotation_angle] = frame.url
                    else:
                        res[str(frame.rotation_angle) + '(1)'] = frame.url
            return res
        else:
            return None
648

649
650
651
652
653
654
655
656
657
658
659
660
    @property
    def projections_compacted(self):
        """
        Return a compacted view of projection frames.

        Returns
        --------
        merged_projections: dict
            Dictionary where the key is a list of indices, and the value
            is the corresponding `silx.io.url.DataUrl` with merged data_slice
        """
        if self._projections_compacted is None:
661
            self._projections_compacted = get_compacted_dataslices(self.projections)
662
663
        return self._projections_compacted

664
665
666
667
668
    def __str__(self):
        return 'hdf5 scan(path: %s, master_file: %s, entry: %s)' % (self.path,
                                                                    self.master_file,
                                                                    self.entry)

669
670
671
672
673
674
675
676
677
    @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[()]
        if 'unit' in node.attrs:
            unit = node.attrs['unit']
678
        elif 'units' in node.attrs:
679
680
681
            unit = node.attrs['units']
        else:
            unit = default_unit
682
        return value * metricsystem.MetricSystem.from_value(unit).value
683

684
685
686
687
688
    def _check_hdf5scan_validity(self):
        if self.master_file is None:
            raise ValueError('No master file provided')
        if self.entry is None:
            raise ValueError('No entry provided')
payno's avatar
payno committed
689
        with h5py.File(self.master_file, 'r', swmr=True) as h5_file:
690
691
692
693
            if self._entry not in h5_file:
                raise ValueError('Given entry %s is not in the master '
                                 'file %s' % (self._entry, self.master_file))

694
695
696
697
698
699

class Frame:
    """class to store all metadata information of a frame"""
    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,
700
                 is_control_proj: bool = False):
701
702
703
704
705
        assert type(index) is int
        self._index = index
        self._image_key = ImageKey.from_value(image_key)
        self._rotation_angle = rotation_angle
        self._url = url
706
        self._is_control_frame = is_control_proj
707
708
709
        self._data = None

    @property
payno's avatar
payno committed
710
    def index(self) -> int:
711
712
713
        return self._index

    @property
payno's avatar
payno committed
714
    def image_key(self) -> ImageKey:
715
716
717
        return self._image_key

    @image_key.setter
payno's avatar
payno committed
718
    def image_key(self, image_key: ImageKey) -> None:
719
720
721
722
723
724
725
        self._image_key = image_key

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

    @rotation_angle.setter
payno's avatar
payno committed
726
    def rotation_angle(self, angle: float) -> None:
727
728
729
        self._rotation_angle = angle

    @property
payno's avatar
payno committed
730
    def url(self) -> DataUrl:
731
732
733
        return self._url

    @property
734
735
    def is_control(self) -> bool:
        return self._is_control_frame
736

737
738
739
    @is_control.setter
    def is_control(self, is_return: bool):
        self._is_control_frame = is_return