source: sasview/src/sas/sascalc/dataloader/readers/cansas_reader_HDF5.py @ 4ba4b69

Last change on this file since 4ba4b69 was bbd0f37, checked in by krzywon, 8 years ago

#826: CanSAS HDF5 reader reads in the Qdev data now.

  • Property mode set to 100644
File size: 25.0 KB
RevLine 
[68aa210]1"""
2    CanSAS 2D data reader for reading HDF5 formatted CanSAS files.
3"""
4
5import h5py
6import numpy as np
7import re
8import os
9import sys
10
[d72567e]11from sas.sascalc.dataloader.data_info import plottable_1D, plottable_2D, Data1D, Data2D, DataInfo, Process, Aperture
12from sas.sascalc.dataloader.data_info import Collimation, TransmissionSpectrum, Detector
13from sas.sascalc.dataloader.data_info import combine_data_info_with_plottable
14
[68aa210]15
16
17class Reader():
18    """
[ad52d31]19    A class for reading in CanSAS v2.0 data files. The existing iteration opens Mantid generated HDF5 formatted files
20    with file extension .h5/.H5. Any number of data sets may be present within the file and any dimensionality of data
21    may be used. Currently 1D and 2D SAS data sets are supported, but future implementations will include 1D and 2D
[d72567e]22    SESANS data.
23
24    Any number of SASdata sets may be present in a SASentry and the data within can be either 1D I(Q) or 2D I(Qx, Qy).
[68aa210]25
[5e906207]26    Also supports reading NXcanSAS formatted HDF5 files
27
[68aa210]28    :Dependencies:
[d72567e]29        The CanSAS HDF5 reader requires h5py => v2.5.0 or later.
[68aa210]30    """
31
32    ## CanSAS version
33    cansas_version = 2.0
34    ## Logged warnings or messages
35    logging = None
36    ## List of errors for the current data set
37    errors = None
38    ## Raw file contents to be processed
39    raw_data = None
[d72567e]40    ## Data info currently being read in
41    current_datainfo = None
42    ## SASdata set currently being read in
[68aa210]43    current_dataset = None
[d72567e]44    ## List of plottable1D objects that should be linked to the current_datainfo
45    data1d = None
46    ## List of plottable2D objects that should be linked to the current_datainfo
47    data2d = None
[68aa210]48    ## Data type name
[ad52d31]49    type_name = "CanSAS 2.0"
[68aa210]50    ## Wildcards
[ad52d31]51    type = ["CanSAS 2.0 HDF5 Files (*.h5)|*.h5"]
[68aa210]52    ## List of allowed extensions
53    ext = ['.h5', '.H5']
54    ## Flag to bypass extension check
55    allow_all = False
56    ## List of files to return
57    output = None
58
59    def read(self, filename):
60        """
[ad52d31]61        This is the general read method that all SasView data_loaders must have.
[68aa210]62
63        :param filename: A path for an HDF5 formatted CanSAS 2D data file.
[d72567e]64        :return: List of Data1D/2D objects and/or a list of errors.
[68aa210]65        """
[ad52d31]66        ## Reinitialize the class when loading a new data file to reset all class variables
[d72567e]67        self.reset_class_variables()
[68aa210]68        ## Check that the file exists
69        if os.path.isfile(filename):
70            basename = os.path.basename(filename)
71            _, extension = os.path.splitext(basename)
72            # If the file type is not allowed, return empty list
73            if extension in self.ext or self.allow_all:
74                ## Load the data file
75                self.raw_data = h5py.File(filename, 'r')
76                ## Read in all child elements of top level SASroot
[d72567e]77                self.read_children(self.raw_data, [])
[ad52d31]78                ## Add the last data set to the list of outputs
[68aa210]79                self.add_data_set()
[995f4eb]80                ## Close the data file
81                self.raw_data.close()
[68aa210]82        ## Return data set(s)
83        return self.output
84
[d72567e]85    def reset_class_variables(self):
86        """
87        Create the reader object and define initial states for class variables
88        """
89        self.current_datainfo = None
90        self.current_dataset = None
91        self.data1d = []
92        self.data2d = []
93        self.raw_data = None
94        self.errors = set()
95        self.logging = []
96        self.output = []
97        self.parent_class = u''
98        self.detector = Detector()
99        self.collimation = Collimation()
100        self.aperture = Aperture()
101        self.process = Process()
102        self.trans_spectrum = TransmissionSpectrum()
103
104    def read_children(self, data, parent_list):
[68aa210]105        """
[ad52d31]106        A recursive method for stepping through the hierarchical data file.
[68aa210]107
108        :param data: h5py Group object of any kind
109        :param parent: h5py Group parent name
110        """
111
112        ## Loop through each element of the parent and process accordingly
113        for key in data.keys():
114            ## Get all information for the current key
115            value = data.get(key)
[d398285]116            if value.attrs.get(u'canSAS_class') is not None:
117                class_name = value.attrs.get(u'canSAS_class')
118            else:
119                class_name = value.attrs.get(u'NX_class')
[68aa210]120            if class_name is not None:
121                class_prog = re.compile(class_name)
122            else:
123                class_prog = re.compile(value.name)
124
125            if isinstance(value, h5py.Group):
[d72567e]126                self.parent_class = class_name
127                parent_list.append(key)
128                ## If this is a new sasentry, store the current data sets and create a fresh Data1D/2D object
[68aa210]129                if class_prog.match(u'SASentry'):
130                    self.add_data_set(key)
[d72567e]131                elif class_prog.match(u'SASdata'):
132                    self._initialize_new_data_set(parent_list)
[ad52d31]133                ## Recursion step to access data within the group
[d72567e]134                self.read_children(value, parent_list)
135                self.add_intermediate()
136                parent_list.remove(key)
[68aa210]137
138            elif isinstance(value, h5py.Dataset):
139                ## If this is a dataset, store the data appropriately
140                data_set = data[key][:]
[7bd6860a]141                unit = self._get_unit(value)
[ac370c5]142
[7bd6860a]143                ## I and Q Data
144                if key == u'I':
145                    if type(self.current_dataset) is plottable_2D:
[ac370c5]146                        self.current_dataset.data = data_set
[7bd6860a]147                        self.current_dataset.zaxis("Intensity", unit)
148                    else:
149                        self.current_dataset.y = data_set.flatten()
150                        self.current_dataset.yaxis("Intensity", unit)
151                    continue
152                elif key == u'Idev':
153                    if type(self.current_dataset) is plottable_2D:
154                        self.current_dataset.err_data = data_set.flatten()
155                    else:
156                        self.current_dataset.dy = data_set.flatten()
157                    continue
158                elif key == u'Q':
159                    self.current_dataset.xaxis("Q", unit)
160                    if type(self.current_dataset) is plottable_2D:
161                        self.current_dataset.q = data_set.flatten()
162                    else:
163                        self.current_dataset.x = data_set.flatten()
164                    continue
[bbd0f37]165                elif key == u'Qdev':
166                    self.current_dataset.dx = data_set.flatten()
167                    continue
[7bd6860a]168                elif key == u'Qy':
169                    self.current_dataset.yaxis("Q_y", unit)
170                    self.current_dataset.qy_data = data_set.flatten()
171                    continue
172                elif key == u'Qydev':
173                    self.current_dataset.dqy_data = data_set.flatten()
174                    continue
175                elif key == u'Qx':
176                    self.current_dataset.xaxis("Q_x", unit)
177                    self.current_dataset.qx_data = data_set.flatten()
178                    continue
179                elif key == u'Qxdev':
180                    self.current_dataset.dqx_data = data_set.flatten()
181                    continue
182                elif key == u'Mask':
183                    self.current_dataset.mask = data_set.flatten()
184                    continue
[68aa210]185
186                for data_point in data_set:
187                    ## Top Level Meta Data
188                    if key == u'definition':
[d72567e]189                        self.current_datainfo.meta_data['reader'] = data_point
[68aa210]190                    elif key == u'run':
[d72567e]191                        self.current_datainfo.run.append(data_point)
[be88076]192                        try:
193                            run_name = value.attrs['name']
194                            run_dict = {data_point: run_name}
195                            self.current_datainfo.run_name = run_dict
196                        except:
197                            pass
[68aa210]198                    elif key == u'title':
[d72567e]199                        self.current_datainfo.title = data_point
[68aa210]200                    elif key == u'SASnote':
[d72567e]201                        self.current_datainfo.notes.append(data_point)
[68aa210]202
203                    ## Sample Information
[eb98f24]204                    elif key == u'Title' and self.parent_class == u'SASsample': # CanSAS 2.0 format
[d72567e]205                        self.current_datainfo.sample.name = data_point
[ac3353d]206                    elif key == u'ID' and self.parent_class == u'SASsample': # NXcanSAS format
[88d85c6]207                        self.current_datainfo.sample.name = data_point
[d72567e]208                    elif key == u'thickness' and self.parent_class == u'SASsample':
209                        self.current_datainfo.sample.thickness = data_point
210                    elif key == u'temperature' and self.parent_class == u'SASsample':
211                        self.current_datainfo.sample.temperature = data_point
[5e906207]212                    elif key == u'transmission' and self.parent_class == u'SASsample':
213                        self.current_datainfo.sample.transmission = data_point
214                    elif key == u'x_position' and self.parent_class == u'SASsample':
215                        self.current_datainfo.sample.position.x = data_point
216                    elif key == u'y_position' and self.parent_class == u'SASsample':
217                        self.current_datainfo.sample.position.y = data_point
218                    elif key == u'polar_angle' and self.parent_class == u'SASsample':
219                        self.current_datainfo.sample.orientation.x = data_point
220                    elif key == u'azimuthal_angle' and self.parent_class == u'SASsample':
221                        self.current_datainfo.sample.orientation.z = data_point
222                    elif key == u'details' and self.parent_class == u'SASsample':
223                        self.current_datainfo.sample.details.append(data_point)
[68aa210]224
[ad52d31]225                    ## Instrumental Information
[d72567e]226                    elif key == u'name' and self.parent_class == u'SASinstrument':
227                        self.current_datainfo.instrument = data_point
228                    elif key == u'name' and self.parent_class == u'SASdetector':
[ad52d31]229                        self.detector.name = data_point
[d72567e]230                    elif key == u'SDD' and self.parent_class == u'SASdetector':
231                        self.detector.distance = float(data_point)
[d398285]232                        self.detector.distance_unit = unit
[5e906207]233                    elif key == u'slit_length' and self.parent_class == u'SASdetector':
234                        self.detector.slit_length = float(data_point)
235                        self.detector.slit_length_unit = unit
236                    elif key == u'x_position' and self.parent_class == u'SASdetector':
237                        self.detector.offset.x = float(data_point)
238                        self.detector.offset_unit = unit
239                    elif key == u'y_position' and self.parent_class == u'SASdetector':
240                        self.detector.offset.y = float(data_point)
241                        self.detector.offset_unit = unit
242                    elif key == u'polar_angle' and self.parent_class == u'SASdetector':
243                        self.detector.orientation.x = float(data_point)
244                        self.detector.orientation_unit = unit
245                    elif key == u'azimuthal_angle' and self.parent_class == u'SASdetector':
246                        self.detector.orientation.z = float(data_point)
247                        self.detector.orientation_unit = unit
248                    elif key == u'beam_center_x' and self.parent_class == u'SASdetector':
249                        self.detector.beam_center.x = float(data_point)
250                        self.detector.beam_center_unit = unit
251                    elif key == u'beam_center_y' and self.parent_class == u'SASdetector':
252                        self.detector.beam_center.y = float(data_point)
253                        self.detector.beam_center_unit = unit
254                    elif key == u'x_pixel_size' and self.parent_class == u'SASdetector':
255                        self.detector.pixel_size.x = float(data_point)
256                        self.detector.pixel_size_unit = unit
257                    elif key == u'y_pixel_size' and self.parent_class == u'SASdetector':
258                        self.detector.pixel_size.y = float(data_point)
259                        self.detector.pixel_size_unit = unit
[d72567e]260                    elif key == u'SSD' and self.parent_class == u'SAScollimation':
[ad52d31]261                        self.collimation.length = data_point
[d398285]262                        self.collimation.length_unit = unit
[d72567e]263                    elif key == u'name' and self.parent_class == u'SAScollimation':
[ad52d31]264                        self.collimation.name = data_point
265
[68aa210]266                    ## Process Information
[d72567e]267                    elif key == u'name' and self.parent_class == u'SASprocess':
[68aa210]268                        self.process.name = data_point
[eb98f24]269                    elif key == u'Title' and self.parent_class == u'SASprocess': # CanSAS 2.0 format
[68aa210]270                        self.process.name = data_point
[eb98f24]271                    elif key == u'name' and self.parent_class == u'SASprocess': # NXcanSAS format
[88d85c6]272                        self.process.name = data_point
[d72567e]273                    elif key == u'description' and self.parent_class == u'SASprocess':
[68aa210]274                        self.process.description = data_point
[d72567e]275                    elif key == u'date' and self.parent_class == u'SASprocess':
[68aa210]276                        self.process.date = data_point
[d72567e]277                    elif self.parent_class == u'SASprocess':
[ad52d31]278                        self.process.notes.append(data_point)
279
280                    ## Transmission Spectrum
[d72567e]281                    elif key == u'T' and self.parent_class == u'SAStransmission_spectrum':
[ad52d31]282                        self.trans_spectrum.transmission.append(data_point)
[d72567e]283                    elif key == u'Tdev' and self.parent_class == u'SAStransmission_spectrum':
[ad52d31]284                        self.trans_spectrum.transmission_deviation.append(data_point)
[d72567e]285                    elif key == u'lambda' and self.parent_class == u'SAStransmission_spectrum':
[ad52d31]286                        self.trans_spectrum.wavelength.append(data_point)
287
[5e906207]288                    ## Source
[d72567e]289                    elif key == u'wavelength' and self.parent_class == u'SASdata':
290                        self.current_datainfo.source.wavelength = data_point
[5e906207]291                        self.current_datainfo.source.wavelength_unit = unit
292                    elif key == u'incident_wavelength' and self.parent_class == u'SASsource':
293                        self.current_datainfo.source.wavelength = data_point
294                        self.current_datainfo.source.wavelength_unit = unit
295                    elif key == u'wavelength_max' and self.parent_class == u'SASsource':
296                        self.current_datainfo.source.wavelength_max = data_point
297                        self.current_datainfo.source.wavelength_max_unit = unit
298                    elif key == u'wavelength_min' and self.parent_class == u'SASsource':
299                        self.current_datainfo.source.wavelength_min = data_point
300                        self.current_datainfo.source.wavelength_min_unit = unit
301                    elif key == u'wavelength_spread' and self.parent_class == u'SASsource':
302                        self.current_datainfo.source.wavelength_spread = data_point
303                        self.current_datainfo.source.wavelength_spread_unit = unit
304                    elif key == u'beam_size_x' and self.parent_class == u'SASsource':
305                        self.current_datainfo.source.beam_size.x = data_point
306                        self.current_datainfo.source.beam_size_unit = unit
307                    elif key == u'beam_size_y' and self.parent_class == u'SASsource':
308                        self.current_datainfo.source.beam_size.y = data_point
309                        self.current_datainfo.source.beam_size_unit = unit
310                    elif key == u'beam_shape' and self.parent_class == u'SASsource':
311                        self.current_datainfo.source.beam_shape = data_point
[d72567e]312                    elif key == u'radiation' and self.parent_class == u'SASsource':
313                        self.current_datainfo.source.radiation = data_point
314                    elif key == u'transmission' and self.parent_class == u'SASdata':
315                        self.current_datainfo.sample.transmission = data_point
[68aa210]316
317                    ## Everything else goes in meta_data
318                    else:
[d72567e]319                        new_key = self._create_unique_key(self.current_datainfo.meta_data, key)
320                        self.current_datainfo.meta_data[new_key] = data_point
[68aa210]321
322            else:
323                ## I don't know if this reachable code
324                self.errors.add("ShouldNeverHappenException")
325
[d72567e]326    def add_intermediate(self):
[ad52d31]327        """
328        This method stores any intermediate objects within the final data set after fully reading the set.
329
330        :param parent: The NXclass name for the h5py Group object that just finished being processed
331        """
332
[d72567e]333        if self.parent_class == u'SASprocess':
334            self.current_datainfo.process.append(self.process)
[ad52d31]335            self.process = Process()
[d72567e]336        elif self.parent_class == u'SASdetector':
337            self.current_datainfo.detector.append(self.detector)
[ad52d31]338            self.detector = Detector()
[d72567e]339        elif self.parent_class == u'SAStransmission_spectrum':
340            self.current_datainfo.trans_spectrum.append(self.trans_spectrum)
[ad52d31]341            self.trans_spectrum = TransmissionSpectrum()
[d72567e]342        elif self.parent_class == u'SAScollimation':
343            self.current_datainfo.collimation.append(self.collimation)
[ad52d31]344            self.collimation = Collimation()
[d72567e]345        elif self.parent_class == u'SASaperture':
[ad52d31]346            self.collimation.aperture.append(self.aperture)
347            self.aperture = Aperture()
[d72567e]348        elif self.parent_class == u'SASdata':
349            if type(self.current_dataset) is plottable_2D:
350                self.data2d.append(self.current_dataset)
351            elif type(self.current_dataset) is plottable_1D:
352                self.data1d.append(self.current_dataset)
[68aa210]353
354    def final_data_cleanup(self):
355        """
[d72567e]356        Does some final cleanup and formatting on self.current_datainfo and all data1D and data2D objects and then
357        combines the data and info into Data1D and Data2D objects
[68aa210]358        """
359
[d72567e]360        ## Type cast data arrays to float64
361        if len(self.current_datainfo.trans_spectrum) > 0:
[ad52d31]362            spectrum_list = []
[d72567e]363            for spectrum in self.current_datainfo.trans_spectrum:
[ad52d31]364                spectrum.transmission = np.delete(spectrum.transmission, [0])
365                spectrum.transmission = spectrum.transmission.astype(np.float64)
366                spectrum.transmission_deviation = np.delete(spectrum.transmission_deviation, [0])
367                spectrum.transmission_deviation = spectrum.transmission_deviation.astype(np.float64)
368                spectrum.wavelength = np.delete(spectrum.wavelength, [0])
369                spectrum.wavelength = spectrum.wavelength.astype(np.float64)
[d72567e]370                if len(spectrum.transmission) > 0:
371                    spectrum_list.append(spectrum)
372            self.current_datainfo.trans_spectrum = spectrum_list
[68aa210]373
374        ## Append errors to dataset and reset class errors
[d72567e]375        self.current_datainfo.errors = self.errors
[68aa210]376        self.errors.clear()
377
[d72567e]378        ## Combine all plottables with datainfo and append each to output
379        ## Type cast data arrays to float64 and find min/max as appropriate
380        for dataset in self.data2d:
381            dataset.data = dataset.data.astype(np.float64)
382            dataset.err_data = dataset.err_data.astype(np.float64)
383            if dataset.qx_data is not None:
384                dataset.xmin = np.min(dataset.qx_data)
385                dataset.xmax = np.max(dataset.qx_data)
386                dataset.qx_data = dataset.qx_data.astype(np.float64)
387            if dataset.dqx_data is not None:
388                dataset.dqx_data = dataset.dqx_data.astype(np.float64)
389            if dataset.qy_data is not None:
390                dataset.ymin = np.min(dataset.qy_data)
391                dataset.ymax = np.max(dataset.qy_data)
392                dataset.qy_data = dataset.qy_data.astype(np.float64)
393            if dataset.dqy_data is not None:
394                dataset.dqy_data = dataset.dqy_data.astype(np.float64)
395            if dataset.q_data is not None:
396                dataset.q_data = dataset.q_data.astype(np.float64)
397            zeros = np.ones(dataset.data.size, dtype=bool)
398            try:
399                for i in range (0, dataset.mask.size - 1):
400                    zeros[i] = dataset.mask[i]
401            except:
402                self.errors.add(sys.exc_value)
403            dataset.mask = zeros
404            ## Calculate the actual Q matrix
405            try:
406                if dataset.q_data.size <= 1:
407                    dataset.q_data = np.sqrt(dataset.qx_data * dataset.qx_data + dataset.qy_data * dataset.qy_data)
408            except:
409                dataset.q_data = None
[ac370c5]410
411            if dataset.data.ndim == 2:
412                (n_rows, n_cols) = dataset.data.shape
[479799c]413                dataset.y_bins = dataset.qy_data[0::n_cols]
[ac370c5]414                dataset.x_bins = dataset.qx_data[:n_cols]
415                dataset.data = dataset.data.flatten()
416
[d72567e]417            final_dataset = combine_data_info_with_plottable(dataset, self.current_datainfo)
418            self.output.append(final_dataset)
419
420        for dataset in self.data1d:
421            if dataset.x is not None:
422                dataset.x = dataset.x.astype(np.float64)
423                dataset.xmin = np.min(dataset.x)
424                dataset.xmax = np.max(dataset.x)
425            if dataset.y is not None:
426                dataset.y = dataset.y.astype(np.float64)
427                dataset.ymin = np.min(dataset.y)
428                dataset.ymax = np.max(dataset.y)
429            if dataset.dx is not None:
430                dataset.dx = dataset.dx.astype(np.float64)
431            if dataset.dxl is not None:
432                dataset.dxl = dataset.dxl.astype(np.float64)
433            if dataset.dxw is not None:
434                dataset.dxw = dataset.dxw.astype(np.float64)
435            if dataset.dy is not None:
436                dataset.dy = dataset.dy.astype(np.float64)
437            final_dataset = combine_data_info_with_plottable(dataset, self.current_datainfo)
438            self.output.append(final_dataset)
439
[68aa210]440    def add_data_set(self, key=""):
441        """
442        Adds the current_dataset to the list of outputs after preforming final processing on the data and then calls a
443        private method to generate a new data set.
444
445        :param key: NeXus group name for current tree level
446        """
[d72567e]447
448        if self.current_datainfo and self.current_dataset:
[68aa210]449            self.final_data_cleanup()
[d72567e]450        self.data1d = []
451        self.data2d = []
452        self.current_datainfo = DataInfo()
[68aa210]453
[54ba66e]454
[d72567e]455    def _initialize_new_data_set(self, parent_list = None):
[68aa210]456        """
457        A private class method to generate a new 1D or 2D data object based on the type of data within the set.
458        Outside methods should call add_data_set() to be sure any existing data is stored properly.
459
[d72567e]460        :param parent_list: List of names of parent elements
[68aa210]461        """
[d72567e]462
463        if parent_list is None:
464            parent_list = []
465        if self._find_intermediate(parent_list, "Qx"):
466            self.current_dataset = plottable_2D()
[68aa210]467        else:
468            x = np.array(0)
469            y = np.array(0)
[d72567e]470            self.current_dataset = plottable_1D(x, y)
471        self.current_datainfo.filename = self.raw_data.filename
[68aa210]472
[d72567e]473    def _find_intermediate(self, parent_list, basename=""):
[ad52d31]474        """
475        A private class used to find an entry by either using a direct key or knowing the approximate basename.
476
[d72567e]477        :param parent_list: List of parents to the current level in the HDF5 file
478        :param basename: Approximate name of an entry to search for
[ad52d31]479        :return:
480        """
[d72567e]481
482        entry = False
483        key_prog = re.compile(basename)
484        top = self.raw_data
485        for parent in parent_list:
486            top = top.get(parent)
487        for key in top.keys():
488            if (key_prog.match(key)):
489                entry = True
490                break
[ad52d31]491        return entry
492
[68aa210]493    def _create_unique_key(self, dictionary, name, numb=0):
494        """
495        Create a unique key value for any dictionary to prevent overwriting
496        Recurses until a unique key value is found.
497
498        :param dictionary: A dictionary with any number of entries
499        :param name: The index of the item to be added to dictionary
500        :param numb: The number to be appended to the name, starts at 0
[d72567e]501        :return: The new name for the dictionary entry
[68aa210]502        """
503        if dictionary.get(name) is not None:
504            numb += 1
505            name = name.split("_")[0]
506            name += "_{0}".format(numb)
507            name = self._create_unique_key(dictionary, name, numb)
[d398285]508        return name
509
510    def _get_unit(self, value):
511        """
512        Find the unit for a particular value within the h5py dictionary
513
514        :param value: attribute dictionary for a particular value set
[d72567e]515        :return: unit for the value passed to the method
[d398285]516        """
517        unit = value.attrs.get(u'units')
518        if unit == None:
519            unit = value.attrs.get(u'unit')
520        ## Convert the unit formats
521        if unit == "1/A":
522            unit = "A^{-1}"
523        elif unit == "1/cm":
524            unit = "cm^{-1}"
[54ba66e]525        return unit
Note: See TracBrowser for help on using the repository browser.