scanfactory.py 4.87 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#
# 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.
#
#############################################################################*/
"""Contains the ScanFactory class and dedicated functions"""

__authors__ = ["H.Payno"]
__license__ = "MIT"
__date__ = "27/02/2019"


from .scanbase import TomoScanBase
from .esrf.edfscan import EDFTomoScan
from .esrf.hdf5scan import HDF5TomoScan
import json
import os


class ScanFactory:
    """
    Factory for TomoScanBase instances
    """

    @staticmethod
44
    def create_scan_object(scan_path: str) -> TomoScanBase:
payno's avatar
payno committed
45
46
        """
        
47
        :param str scan_path: path to the scan directory or file
payno's avatar
payno committed
48
49
50
51
52
53
54
55
56
        :return: ScanBase instance fitting the scan folder or scan path
        :rtype: TomoScanBase
        """
        # remove any final separator (otherwise basename might fail)
        scan_path = scan_path.rstrip(os.path.sep)
        if EDFTomoScan.is_tomoscan_dir(scan_path):
            return EDFTomoScan(scan=scan_path)
        elif HDF5TomoScan.is_tomoscan_dir(scan_path):
            return HDF5TomoScan(scan=scan_path)
payno's avatar
payno committed
57
58
        else:
            raise ValueError('%s is not a valid scan path' % scan_path)
payno's avatar
payno committed
59
60

    @staticmethod
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    def create_scan_objects(scan_path: str) -> tuple:
        """

        :param str scan_path: path to the scan directory or file
        :return: all possible instances of TomoScanBase contained in the given
                 path
        :rtype: tuple
        """
        scan_path = scan_path.rstrip(os.path.sep)
        if EDFTomoScan.is_tomoscan_dir(scan_path):
            return (EDFTomoScan(scan=scan_path), )
        elif HDF5TomoScan.is_tomoscan_dir(scan_path):
            scans = []
            master_file = HDF5TomoScan.get_master_file(scan_path=scan_path)
75
            entries = HDF5TomoScan.get_valid_entries(master_file)
76
77
78
79
80
81
82
83
84
            for entry in entries:
                scans.append(HDF5TomoScan(scan=scan_path, entry=entry,
                                          index=None))
            return tuple(scans)

        raise ValueError('%s is not a valid scan path' % scan_path)

    @staticmethod
    def create_scan_object_frm_dict(_dict: dict) -> TomoScanBase:
payno's avatar
payno committed
85
86
87
88
89
90
91
92
        """
        Create a TomoScanBase instance from a dictionary. It should contains
        the TomoScanBase._DICT_TYPE_KEY key at least.

        :param _dict: dictionary to be converted
        :return: instance of TomoScanBase
        :rtype: TomoScanBase
        """
93
        if TomoScanBase.DICT_TYPE_KEY not in _dict:
payno's avatar
payno committed
94
            raise ValueError('given dict is not recognized. Cannot find'
95
96
                             '', TomoScanBase.DICT_TYPE_KEY)
        elif _dict[TomoScanBase.DICT_TYPE_KEY] == EDFTomoScan._TYPE:
payno's avatar
payno committed
97
98
            return EDFTomoScan(scan=None).load_from_dict(_dict)
        else:
99
            raise ValueError('Scan type', _dict[TomoScanBase.DICT_TYPE_KEY],
payno's avatar
payno committed
100
101
102
                             'is not managed')

    @staticmethod
103
    def is_tomoscan_dir(scan_path: str) -> bool:
payno's avatar
payno committed
104
105
106
107
108
109
110
111
112
113
114
115
        """
        
        :param str scan_path: path to the scan directory or file
        :return: True if the given path is a root folder of an acquisition.
        :rtype: bool
        """
        return (
                HDF5TomoScan.is_tomoscan_dir(scan_path) or
                EDFTomoScan.is_tomoscan_dir(scan_path)
        )

    @staticmethod
116
    def create_from_json(desc: dict) -> TomoScanBase:
payno's avatar
payno committed
117
118
119
        """Create a ScanBase instance from a json description"""
        data = json.load(desc)

120
        if TomoScanBase.DICT_TYPE_KEY not in data:
payno's avatar
payno committed
121
            raise ValueError('json not recognize')
122
        elif data[TomoScanBase.DICT_TYPE_KEY] == EDFTomoScan._TYPE:
payno's avatar
payno committed
123
124
125
126
            scan = EDFTomoScan(scan=None).load_from_dict(data)
            return scan
        else:
            raise ValueError('Type', data[TomoScanBase.type], 'is not managed')