dataset_analyzer.py 3.14 KB
Newer Older
Pierre Paleo's avatar
Pierre Paleo committed
1
import os
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from tomoscan.esrf.edfscan import EDFTomoScan
from tomoscan.esrf.hdf5scan import HDF5TomoScan


dataset_infos = {
    "num_radios": None,
    "num_darks": None,
    "num_flats": None,
    "radios": None,
    "darks": None,
    "flats": None,
    "frame_dims": None,
    "energy_kev": None,
    "distance_m": None,
    "pixel_size_microns": None,
}


class DatasetAnalyzer(object):
    """
    Base class for datasets analyzers.
    """
    def __init__(self, location):
        self.location = location


    def _init_dataset_scan(self, filetype, **kwargs):
        scanners = {
            "edf": EDFTomoScan,
            "hdf5": HDF5TomoScan,
        }
        if filetype not in scanners.keys():
            raise ValueError("No scanner for file type: %s" % filetype)
        scanner = scanners[filetype]
        self.dataset_scanner = scanner(self.location, **kwargs)
Pierre Paleo's avatar
Pierre Paleo committed
37
38
39
        self.projections = self.dataset_scanner.projections
        self.flats = self.dataset_scanner.flats
        self.darks = self.dataset_scanner.darks
Pierre Paleo's avatar
Pierre Paleo committed
40
41
42
43
        self.n_angles = self.dataset_scanner.tomo_n
        self.radio_dims = (self.dataset_scanner.dim_1, self.dataset_scanner.dim_2)


44
45
46

    @property
    def energy(self):
Pierre Paleo's avatar
Pierre Paleo committed
47
48
49
        """
        Return the energy in kev.
        """
Pierre Paleo's avatar
Pierre Paleo committed
50
        return self.dataset_scanner.energy
51
52
53

    @property
    def distance(self):
Pierre Paleo's avatar
Pierre Paleo committed
54
55
56
        """
        Return the sample-detector distance in meters.
        """
Pierre Paleo's avatar
Pierre Paleo committed
57
        return abs(self.dataset_scanner.distance)
58

Pierre Paleo's avatar
Pierre Paleo committed
59
60
61
62
63
    @property
    def pixel_size(self):
        """
        Return the pixel size in microns.
        """
Pierre Paleo's avatar
Pierre Paleo committed
64
        return self.dataset_scanner.pixel_size * 1e6 # TODO X and Y pixel size
65
66
67
68
69
70


class EDFDatasetAnalyzer(DatasetAnalyzer):
    """
    EDF Dataset analyzer for legacy ESRF acquisitions
    """
Pierre Paleo's avatar
Pierre Paleo committed
71
    def __init__(self, location, n_frames=1):
72
73
74
75
76
77
78
79
80
81
82
        """
        EDF Dataset analyzer.

        Parameters
        -----------
        location: str
            Location of the folder containing EDF files
        """
        super().__init__(location)
        if not(os.path.isdir(location)):
            raise ValueError("%s is not a directory" % location)
Pierre Paleo's avatar
Pierre Paleo committed
83
        self._init_dataset_scan("edf", n_frames=n_frames)
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101


class HDF5DatasetAnalyzer(DatasetAnalyzer):
    """
    HDF5 dataset analyzer
    """
    def __init__(self, location):
        """
        HDF5 Dataset analyzer.

        Parameters
        -----------
        location: str
            Location of the HDF5 master file
        """
        super().__init__(location)
        if not(os.path.isfile(location)):
            raise ValueError("%s is not a file" % location)
Pierre Paleo's avatar
Pierre Paleo committed
102
        self._init_dataset_scan("hdf5")
Pierre Paleo's avatar
Pierre Paleo committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118




def analyze_dataset(dataset_path):
    if not(os.path.isdir(dataset_path)):
        if not(os.path.isfile(dataset_path)):
            raise ValueError("Error: %s no such file or directory")
        if os.path.splitext(dataset_path)[-1] not in [".hdf5", ".h5"]:
            raise ValueError("Error: expected a HDF5 file")
        dataset_analyzer_class = HDF5DatasetAnalyzer
    else: # directory -> assuming EDF
        dataset_analyzer_class = EDFDatasetAnalyzer
    dataset_structure = dataset_analyzer_class(dataset_path)
    return dataset_structure