Ignore:
Timestamp:
Aug 22, 2017 2:55:17 PM (7 years ago)
Author:
GitHub <noreply@…>
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
fca1f50, 17e257b5, f9ba422, 783c1b5
Parents:
a06ee7e (diff), dcb91cf (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
git-author:
Jeff Krzywon <krzywon@…> (08/22/17 14:55:17)
git-committer:
GitHub <noreply@…> (08/22/17 14:55:17)
Message:

Merge pull request #95 from lewisodriscoll/ticket-876

Refactor File Loaders - fixes #889 #950 #849 #876 #922 #983 #970

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/dataloader/readers/cansas_reader_HDF5.py

    rc94280c rdcb91cf  
    1313    TransmissionSpectrum, Detector 
    1414from sas.sascalc.dataloader.data_info import combine_data_info_with_plottable 
    15  
    16  
    17 class Reader(): 
     15from sas.sascalc.dataloader.loader_exceptions import FileContentsException, DefaultReaderException 
     16from sas.sascalc.dataloader.file_reader_base_class import FileReader 
     17 
     18 
     19class Reader(FileReader): 
    1820    """ 
    1921    A class for reading in CanSAS v2.0 data files. The existing iteration opens 
     
    4042    # Raw file contents to be processed 
    4143    raw_data = None 
    42     # Data info currently being read in 
    43     current_datainfo = None 
    44     # SASdata set currently being read in 
    45     current_dataset = None 
    4644    # List of plottable1D objects that should be linked to the current_datainfo 
    4745    data1d = None 
     
    5654    # Flag to bypass extension check 
    5755    allow_all = True 
    58     # List of files to return 
    59     output = None 
    60  
    61     def read(self, filename): 
     56 
     57    def get_file_contents(self): 
    6258        """ 
    6359        This is the general read method that all SasView data_loaders must have. 
     
    6864        # Reinitialize when loading a new data file to reset all class variables 
    6965        self.reset_class_variables() 
     66 
     67        filename = self.f_open.name 
     68        self.f_open.close() # IO handled by h5py 
     69 
    7070        # Check that the file exists 
    7171        if os.path.isfile(filename): 
     
    7575            if extension in self.ext or self.allow_all: 
    7676                # Load the data file 
    77                 self.raw_data = h5py.File(filename, 'r') 
    78                 # Read in all child elements of top level SASroot 
    79                 self.read_children(self.raw_data, []) 
    80                 # Add the last data set to the list of outputs 
    81                 self.add_data_set() 
    82                 # Close the data file 
    83                 self.raw_data.close() 
    84         # Return data set(s) 
    85         return self.output 
     77                try: 
     78                    self.raw_data = h5py.File(filename, 'r') 
     79                except Exception as e: 
     80                    if extension not in self.ext: 
     81                        msg = "CanSAS2.0 HDF5 Reader could not load file {}".format(basename + extension) 
     82                        raise DefaultReaderException(msg) 
     83                    raise FileContentsException(e.message) 
     84                try: 
     85                    # Read in all child elements of top level SASroot 
     86                    self.read_children(self.raw_data, []) 
     87                    # Add the last data set to the list of outputs 
     88                    self.add_data_set() 
     89                except Exception as exc: 
     90                    raise FileContentsException(exc.message) 
     91                finally: 
     92                    # Close the data file 
     93                    self.raw_data.close() 
     94 
     95                for dataset in self.output: 
     96                    if isinstance(dataset, Data1D): 
     97                        if dataset.x.size < 5: 
     98                            self.output = [] 
     99                            raise FileContentsException("Fewer than 5 data points found.") 
    86100 
    87101    def reset_class_variables(self): 
     
    427441        Data1D and Data2D objects 
    428442        """ 
    429  
    430443        # Type cast data arrays to float64 
    431444        if len(self.current_datainfo.trans_spectrum) > 0: 
     
    451464        # Type cast data arrays to float64 and find min/max as appropriate 
    452465        for dataset in self.data2d: 
    453             dataset.data = dataset.data.astype(np.float64) 
    454             dataset.err_data = dataset.err_data.astype(np.float64) 
    455             if dataset.qx_data is not None: 
    456                 dataset.xmin = np.min(dataset.qx_data) 
    457                 dataset.xmax = np.max(dataset.qx_data) 
    458                 dataset.qx_data = dataset.qx_data.astype(np.float64) 
    459             if dataset.dqx_data is not None: 
    460                 dataset.dqx_data = dataset.dqx_data.astype(np.float64) 
    461             if dataset.qy_data is not None: 
    462                 dataset.ymin = np.min(dataset.qy_data) 
    463                 dataset.ymax = np.max(dataset.qy_data) 
    464                 dataset.qy_data = dataset.qy_data.astype(np.float64) 
    465             if dataset.dqy_data is not None: 
    466                 dataset.dqy_data = dataset.dqy_data.astype(np.float64) 
    467             if dataset.q_data is not None: 
    468                 dataset.q_data = dataset.q_data.astype(np.float64) 
    469466            zeros = np.ones(dataset.data.size, dtype=bool) 
    470467            try: 
     
    489486                dataset.x_bins = dataset.qx_data[:n_cols] 
    490487                dataset.data = dataset.data.flatten() 
    491  
    492             final_dataset = combine_data_info_with_plottable( 
    493                 dataset, self.current_datainfo) 
    494             self.output.append(final_dataset) 
     488            self.current_dataset = dataset 
     489            self.send_to_output() 
    495490 
    496491        for dataset in self.data1d: 
    497             if dataset.x is not None: 
    498                 dataset.x = dataset.x.astype(np.float64) 
    499                 dataset.xmin = np.min(dataset.x) 
    500                 dataset.xmax = np.max(dataset.x) 
    501             if dataset.y is not None: 
    502                 dataset.y = dataset.y.astype(np.float64) 
    503                 dataset.ymin = np.min(dataset.y) 
    504                 dataset.ymax = np.max(dataset.y) 
    505             if dataset.dx is not None: 
    506                 dataset.dx = dataset.dx.astype(np.float64) 
    507             if dataset.dxl is not None: 
    508                 dataset.dxl = dataset.dxl.astype(np.float64) 
    509             if dataset.dxw is not None: 
    510                 dataset.dxw = dataset.dxw.astype(np.float64) 
    511             if dataset.dy is not None: 
    512                 dataset.dy = dataset.dy.astype(np.float64) 
    513             final_dataset = combine_data_info_with_plottable( 
    514                 dataset, self.current_datainfo) 
    515             self.output.append(final_dataset) 
     492            self.current_dataset = dataset 
     493            self.send_to_output() 
    516494 
    517495    def add_data_set(self, key=""): 
Note: See TracChangeset for help on using the changeset viewer.