hdf5scan.py 25.9 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
33
34
35
36
37
38
39

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


from ..scanbase import TomoScanBase
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
payno's avatar
payno committed
44
import logging
45
import typing
payno's avatar
payno committed
46
47
48
49

_logger = logging.getLogger(__name__)


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


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

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

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

    _SCAN_META_PATH = 'scan_meta/technique/scan'

    _DET_META_PATH = 'scan_meta/technique/detector'

86
87
88
89
    _ROTATION_ANGLE_PATH = 'sample/rotation_angle'

    _IMG_KEY_PATH = 'instrument/detector/image_key'

90
91
    _IMG_KEY_CONTROL_PATH = 'instrument/detector/image_key_control'

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

    _Y_PIXEL_SIZE_PATH = 'instrument/detector/y_pixel_size'

96
97
98
99
    _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
100
101
    _DISTANCE_PATH = 'instrument/detector/distance'

102
103
    _ENERGY_PATH = 'beam/incident_energy'

payno's avatar
payno committed
104
105
    _SCHEME = 'silx'

106
107
    _EPSILON_ROT_ANGLE = 0.02

payno's avatar
payno committed
108
109
    def __init__(self, scan: str, entry: str = None,
                 index: typing.Union[int, None] = 0):
110
111
        if entry is not None:
            index = None
payno's avatar
payno committed
112
113
114
115
116
117
        # if the user give the master file instead of the scan dir...
        if scan is not None:
            if os.path.isfile(scan):
                self.master_file = scan
                scan = os.path.dirname(scan)
            else:
118
119
                self.master_file = self.get_master_file(scan)

payno's avatar
payno committed
120
                self.master_file = os.path.join(scan, os.path.basename(scan))
121
122
123
                if os.path.exists(self.master_file + '.nx'):
                    self.master_file = self.master_file + '.nx'
                elif os.path.exists(self.master_file + '.hdf5'):
payno's avatar
payno committed
124
                    self.master_file = self.master_file + '.hdf5'
125
                elif os.path.exists(self.master_file + '.h5'):
payno's avatar
payno committed
126
                    self.master_file = self.master_file + '.h5'
127
128
                else:
                    self.master_file = self.master_file + '.nx'
payno's avatar
payno committed
129
130
131
132
133
        else:
            self.master_file = None

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

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

        # data caches
payno's avatar
payno committed
144
        self._projections = None
145
146
        self._flats = None
        self._darks = None
payno's avatar
payno committed
147
148
149
150
151
152
153
154
155
156
        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
157
158
        self._x_pixel_size = None
        self._y_pixel_size = None
payno's avatar
payno committed
159
        # pixel dimensions (tuple)
160
161
        self._frames = None
        self._image_keys = None
162
        self._image_keys_control = None
163
        self._rotation_angles = None
payno's avatar
payno committed
164
        self._distance = 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
186
187
188
189
190
191
192
        self._projections = None
        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
193
194
        self._x_pixel_size = None
        self._y_pixel_size = None
195
196
        self._x_magnified_pixel_size = None
        self._y_magnified_pixel_size = None
197
        self._rotation_angles = None
payno's avatar
payno committed
198
        self._distance = None
199
        self._image_keys_control = None
200
201

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

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

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

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

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

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

    @staticmethod
payno's avatar
payno committed
243
    def node_is_nxtomo(node: h5py.Group) -> bool:
244
245
246
247
248
249
250
251
252
253
254
        """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
255
256
257

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

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

272
273
274
275
276
277
    @docstring(TomoScanBase.to_dict)
    def to_dict(self) -> dict:
        res = super().to_dict()
        res[self._DICT_ENTRY_KEY] = self.entry
        return res

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

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

        :param _dict:
        :return:
        """
        if isinstance(_dict, io.TextIOWrapper):
            data = json.load(_dict)
        else:
            data = _dict
295
        if not (self.DICT_TYPE_KEY in data and data[self.DICT_TYPE_KEY] == self._TYPE):
296
297
298
            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
299

300
301
        assert self.DICT_PATH_KEY in data
        self.path = data[self.DICT_PATH_KEY]
302
        self._entry = data[self._DICT_ENTRY_KEY]
payno's avatar
payno committed
303
304
        return self

305
    @property
payno's avatar
payno committed
306
    def entry(self) -> str:
307
308
        return self._entry

payno's avatar
payno committed
309
310
    @property
    @docstring(TomoScanBase.projections)
311
    def projections(self) -> typing.Union[dict, None]:
312
313
314
        """projections / radio, does not include the return projections"""
        if self._projections is None:
            if self.frames:
315
                proj_frames = tuple(filter(lambda x: x.image_key == ImageKey.PROJECTION and x.is_control == False, self.frames))
316
317
318
                self._projections = {}
                for proj_frame in proj_frames:
                    self._projections[proj_frame.index] = proj_frame.url
payno's avatar
payno committed
319
320
321
        return self._projections

    @projections.setter
322
    def projections(self, projections: dict):
payno's avatar
payno committed
323
324
325
326
        self._projections = projections

    @property
    @docstring(TomoScanBase.darks)
327
    def darks(self) -> typing.Union[dict, None]:
328
329
        if self._darks is None:
            if self.frames:
330
                dark_frames = tuple(filter(lambda x: x.image_key == ImageKey.DARK_FIELD, self.frames))
331
332
333
                self._darks = {}
                for dark_frame in dark_frames:
                    self._darks[dark_frame.index] = dark_frame.url
334
        return self._darks
payno's avatar
payno committed
335
336
337

    @property
    @docstring(TomoScanBase.flats)
338
    def flats(self) -> typing.Union[dict, None]:
339
340
        if self._flats is None:
            if self.frames:
341
                flat_frames = tuple(filter(lambda x: x.image_key == ImageKey.FLAT_FIELD, self.frames))
342
343
344
                self._flats = {}
                for flat_frame in flat_frames:
                    self._flats[flat_frame.index] = flat_frame.url
345
        return self._flats
payno's avatar
payno committed
346
347

    @docstring(TomoScanBase.update)
payno's avatar
payno committed
348
    def update(self) -> None:
payno's avatar
payno committed
349
350
        """update list of radio and reconstruction by parsing the scan folder
        """
351
        if self.master_file is None or not os.path.exists(self.master_file):
payno's avatar
payno committed
352
            return
payno's avatar
payno committed
353
        self.projections = self._get_projections_url()
payno's avatar
payno committed
354
355
        # TODO: update darks and flats too

payno's avatar
payno committed
356
    @docstring(TomoScanBase.get_proj_angle_url)
payno's avatar
payno committed
357
358
359
    def _get_projections_url(self):
        if self.master_file is None or not os.path.exists(self.master_file):
            return
360
361
        frames = self.frames
        if frames is not None:
362
            urls = {}
363
364
365
            for frame in frames:
                if frame.image_key is ImageKey.PROJECTION:
                    urls[frame.index] = frame.url
payno's avatar
payno committed
366
            return urls
367
368
        else:
            return None
payno's avatar
payno committed
369
370
371

    @docstring(TomoScanBase.tomo_n)
    @property
payno's avatar
payno committed
372
    def tomo_n(self) -> typing.Union[None, int]:
373
374
375
376
        """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
377
378
        if (self._tomo_n is None and self.master_file and
                os.path.exists(self.master_file)):
379
            if self.projections:
380
                return len(self.projections)
381
382
383
384
385
386
            else:
                return None
        else:
            return None

    @property
payno's avatar
payno committed
387
    def return_projs(self) -> typing.Union[None, list]:
388
389
390
        """"""
        frames = self.frames
        if frames:
391
            return_frames = list(filter(lambda x: x.is_control == True, frames))
392
393
394
395
396
            return return_frames
        else:
            return None

    @property
payno's avatar
payno committed
397
    def rotation_angle(self) -> typing.Union[None, list]:
398
        if self._rotation_angles is None:
399
            self._check_hdf5scan_validity()
400
            with h5py.File(self.master_file, 'r') as h5_file:
401
402
403
404
405
406
                _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
407
    def image_key(self) -> typing.Union[list, None]:
408
409
        if self._entry and self._image_keys is None:
            self._check_hdf5scan_validity()
410
411
412
            with h5py.File(self.master_file, 'r') as h5_file:
                self._image_keys = h5_file[self._entry][self._IMG_KEY_PATH][()]
        return self._image_keys
payno's avatar
payno committed
413

414
415
416
417
418
419
420
421
422
423
424
    @property
    def image_key_control(self) -> typing.Union[list, None]:
        if self._entry and self._image_keys_control is None:
            self._check_hdf5scan_validity()
            with h5py.File(self.master_file, 'r') as h5_file:
                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
425
426
    @docstring(TomoScanBase.dark_n)
    @property
payno's avatar
payno committed
427
    def dark_n(self) -> typing.Union[None, int]:
428
429
430
431
        if self.darks is not None:
            return len(self.darks)
        else:
            return None
payno's avatar
payno committed
432
433
434

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

payno's avatar
payno committed
441
    @docstring(TomoScanBase.ff_interval)
payno's avatar
payno committed
442
    @property
payno's avatar
payno committed
443
    def ff_interval(self):
444
445
        raise NotImplementedError('not implemented for hdf5. But we have '
                                  'acquisition sequence instead.')
payno's avatar
payno committed
446
447
448

    @docstring(TomoScanBase.scan_range)
    @property
payno's avatar
payno committed
449
    def scan_range(self) -> typing.Union[None, int]:
450
451
452
453
454
455
456
457
458
459
460
        """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
461
462
463
        return self._scan_range

    @property
payno's avatar
payno committed
464
    def dim_1(self) -> typing.Union[None, int]:
465
466
        if self._dim_1 is None:
            self._get_dim1_dim2()
payno's avatar
payno committed
467
468
469
        return self._dim_1

    @property
payno's avatar
payno committed
470
    def dim_2(self) -> typing.Union[None, int]:
471
472
        if self._dim_2 is None:
            self._get_dim1_dim2()
payno's avatar
payno committed
473
474
475
        return self._dim_2

    @property
payno's avatar
payno committed
476
    def pixel_size(self) -> typing.Union[None, float]:
477
478
479
        return self.x_pixel_size

    @property
payno's avatar
payno committed
480
481
482
    def x_pixel_size(self) -> typing.Union[None, float]:
        if (self._x_pixel_size is None and self.master_file and
                os.path.exists(self.master_file)):
483
484
485
486
487
488
489
490
            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"""
        with h5py.File(self.master_file, 'r') as h5_file:
            x_pixel_dataset = h5_file[self._entry][self._X_PIXEL_SIZE_PATH]
491
            _x_pixel_size = self._get_value(x_pixel_dataset, default_unit='micrometer')
492
            y_pixel_dataset = h5_file[self._entry][self._Y_PIXEL_SIZE_PATH]
493
            _y_pixel_size = self._get_value(y_pixel_dataset, default_unit='micrometer')
494
495
        return _x_pixel_size, _y_pixel_size

496
497
498
499
500
501
502
503
    def _get_x_y_magnified_pixel_values(self):
        with h5py.File(self.master_file, 'r') as h5_file:
            x_m_pixel_dataset = h5_file[self._entry][self._X_PIXEL_MAG_SIZE_PATH]
            _x_m_pixel_size = self._get_value(x_m_pixel_dataset, default_unit='micrometer')
            y_m_pixel_dataset = h5_file[self._entry][self._Y_PIXEL_MAG_SIZE_PATH]
            _y_m_pixel_size = self._get_value(y_m_pixel_dataset, default_unit='micrometer')
        return _x_m_pixel_size, _y_m_pixel_size

504
505
506
507
508
509
    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

510
    @property
payno's avatar
payno committed
511
512
513
    def y_pixel_size(self) -> typing.Union[None, float]:
        if (self._y_pixel_size is None and self.master_file and
                os.path.exists(self.master_file)):
514
515
            self._x_pixel_size, self._y_pixel_size = self._get_x_y_pixel_values()
        return self._y_pixel_size
payno's avatar
payno committed
516

517
518
519
520
521
522
523
524
525
526
527
528
529
530
    @property
    def x_magnified_pixel_size(self) -> typing.Union[None, float]:
        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]:
        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
531
    @property
payno's avatar
payno committed
532
533
534
    def distance(self) -> typing.Union[None, float]:
        if (self._distance is None and self.master_file and
                os.path.exists(self.master_file)):
535
            self._check_hdf5scan_validity()
payno's avatar
payno committed
536
537
538
539
540
            with h5py.File(self.master_file, 'r') as h5_file:
                distance_dataset = h5_file[self._entry][self._DISTANCE_PATH]
                self._distance = self._get_value(distance_dataset, default_unit='m')
        return self._distance

541
542
543
544
    @property
    def energy(self) -> typing.Union[None, float]:
        if (self._energy is None and self.master_file and
                os.path.exists(self.master_file)):
545
            self._check_hdf5scan_validity()
546
547
548
549
550
            with h5py.File(self.master_file, 'r') as h5_file:
                energy_dataset = h5_file[self._entry][self._ENERGY_PATH]
                self._energy = self._get_value(energy_dataset, default_unit='keV')
        return self._energy

551
    @property
payno's avatar
payno committed
552
    def frames(self) -> typing.Union[None, tuple]:
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
        """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)
588
                if self.image_key_control is not None:
589
                    is_control_frame = (self.image_key_control[frame.index] == ImageKey.ALIGNMENT.value)
590
591
592
593
594
595
                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
596
                frame._is_control_frame = is_control_frame
597
598
599
600
601
                self._frames.append(frame)
                last_proj_frame = frame
            self._frames = tuple(self._frames)
        return self._frames

payno's avatar
payno committed
602
    @docstring(TomoScanBase.get_proj_angle_url)
payno's avatar
payno committed
603
    def get_proj_angle_url(self) -> typing.Union[dict, None]:
604
605
606
607
        if self.frames is not None:
            res = {}
            for frame in self.frames:
                if frame.image_key is ImageKey.PROJECTION:
608
                    if frame.is_control is False:
609
610
611
612
613
614
                        res[frame.rotation_angle] = frame.url
                    else:
                        res[str(frame.rotation_angle) + '(1)'] = frame.url
            return res
        else:
            return None
615
616
617
618
619
620

    def __str__(self):
        return 'hdf5 scan(path: %s, master_file: %s, entry: %s)' % (self.path,
                                                                    self.master_file,
                                                                    self.entry)

621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
    @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']
        if 'units' in node.attrs:
            unit = node.attrs['units']
        else:
            unit = default_unit
        return value * metricsystem.MetricSystem.from_value(unit).value

636
637
638
639
640
641
642
643
644
645
    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')
        with h5py.File(self.master_file, 'r') as h5_file:
            if self._entry not in h5_file:
                raise ValueError('Given entry %s is not in the master '
                                 'file %s' % (self._entry, self.master_file))

646
647
648
649
650
651

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,
652
                 is_control_proj: bool = False):
653
654
655
656
657
        assert type(index) is int
        self._index = index
        self._image_key = ImageKey.from_value(image_key)
        self._rotation_angle = rotation_angle
        self._url = url
658
        self._is_control_frame = is_control_proj
659
660
661
        self._data = None

    @property
payno's avatar
payno committed
662
    def index(self) -> int:
663
664
665
        return self._index

    @property
payno's avatar
payno committed
666
    def image_key(self) -> ImageKey:
667
668
669
        return self._image_key

    @image_key.setter
payno's avatar
payno committed
670
    def image_key(self, image_key: ImageKey) -> None:
671
672
673
674
675
676
677
        self._image_key = image_key

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

    @rotation_angle.setter
payno's avatar
payno committed
678
    def rotation_angle(self, angle: float) -> None:
679
680
681
        self._rotation_angle = angle

    @property
payno's avatar
payno committed
682
    def url(self) -> DataUrl:
683
684
685
        return self._url

    @property
686
687
    def is_control(self) -> bool:
        return self._is_control_frame
688

689
690
691
    @is_control.setter
    def is_control(self, is_return: bool):
        self._is_control_frame = is_return