Changeset 9e6aeaf in sasview for src/sas/sascalc/dataloader/readers


Ignore:
Timestamp:
Sep 25, 2017 5:35:29 PM (7 years ago)
Author:
Paul Kienzle <pkienzle@…>
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, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
3cb3a51
Parents:
9efdb29 (diff), 0315b63 (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.
Message:

Merge branch 'ticket-853-fit-gui-to-calc' into py3

Location:
src/sas/sascalc/dataloader/readers
Files:
12 edited

Legend:

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

    r46cf4c9 r9e6aeaf  
    104104                # Sample thickness in mm 
    105105                try: 
    106                     value = float(line_toks[5]) 
     106                    # ABS writer adds 'C' with no space to the end of the 
     107                    # thickness column.  Remove it if it is there before 
     108                    # converting the thickness. 
     109                    if line_toks[5][:-1] not in '012345679.': 
     110                        value = float(line_toks[5][:-1]) 
     111                    else: 
     112                        value = float(line_toks[5]) 
    107113                    if self.current_datainfo.sample.thickness_unit != 'cm': 
    108114                        conv = Converter('cm') 
     
    196202                is_data_started = True 
    197203 
    198         self.remove_empty_q_values(True, True) 
     204        self.remove_empty_q_values() 
    199205 
    200206        # Sanity check 
  • src/sas/sascalc/dataloader/readers/ascii_reader.py

    rf7d720f r9e6aeaf  
    156156            raise FileContentsException(msg) 
    157157 
    158         self.remove_empty_q_values(has_error_dx, has_error_dy) 
     158        self.remove_empty_q_values() 
    159159        self.current_dataset.xaxis("\\rm{Q}", 'A^{-1}') 
    160160        self.current_dataset.yaxis("\\rm{Intensity}", "cm^{-1}") 
  • src/sas/sascalc/dataloader/readers/cansas_reader.py

    r9efdb29 r9e6aeaf  
    100100            xml_file = self.f_open.name 
    101101        # We don't sure f_open since lxml handles opnening/closing files 
    102         if not self.f_open.closed: 
    103             self.f_open.close() 
    104  
    105         basename, _ = os.path.splitext(os.path.basename(xml_file)) 
    106  
    107102        try: 
    108103            # Raises FileContentsException 
    109104            self.load_file_and_schema(xml_file, schema_path) 
    110             self.current_datainfo = DataInfo() 
    111             # Raises FileContentsException if file doesn't meet CanSAS schema 
     105            # Parse each SASentry 
     106            entry_list = self.xmlroot.xpath('/ns:SASroot/ns:SASentry', 
     107                                            namespaces={ 
     108                                                'ns': self.cansas_defaults.get( 
     109                                                    "ns") 
     110                                            }) 
    112111            self.is_cansas(self.extension) 
    113             self.invalid = False # If we reach this point then file must be valid CanSAS 
    114  
    115             # Parse each SASentry 
    116             entry_list = self.xmlroot.xpath('/ns:SASroot/ns:SASentry', namespaces={ 
    117                 'ns': self.cansas_defaults.get("ns") 
    118             }) 
    119             # Look for a SASentry 
    120             self.names.append("SASentry") 
    121112            self.set_processing_instructions() 
    122  
    123113            for entry in entry_list: 
    124                 self.current_datainfo.filename = basename + self.extension 
    125                 self.current_datainfo.meta_data["loader"] = "CanSAS XML 1D" 
    126                 self.current_datainfo.meta_data[PREPROCESS] = self.processing_instructions 
    127114                self._parse_entry(entry) 
    128115                self.data_cleanup() 
     
    146133                    invalid_xml = self.find_invalid_xml() 
    147134                    if invalid_xml != "": 
     135                        basename, _ = os.path.splitext( 
     136                            os.path.basename(self.f_open.name)) 
    148137                        invalid_xml = INVALID_XML.format(basename + self.extension) + invalid_xml 
    149138                        raise DataReaderException(invalid_xml) # Handled by base class 
     
    160149        except Exception as e: # Convert all other exceptions to FileContentsExceptions 
    161150            raise FileContentsException(str(e)) 
    162  
     151        finally: 
     152            if not self.f_open.closed: 
     153                self.f_open.close() 
    163154 
    164155    def load_file_and_schema(self, xml_file, schema_path=""): 
     
    205196        if not self._is_call_local() and not recurse: 
    206197            self.reset_state() 
     198        if not recurse: 
     199            self.current_datainfo = DataInfo() 
     200            # Raises FileContentsException if file doesn't meet CanSAS schema 
     201            self.invalid = False 
     202            # Look for a SASentry 
    207203            self.data = [] 
    208             self.current_datainfo = DataInfo() 
     204            self.parent_class = "SASentry" 
    209205            self.names.append("SASentry") 
    210             self.parent_class = "SASentry" 
     206            self.current_datainfo.meta_data["loader"] = "CanSAS XML 1D" 
     207            self.current_datainfo.meta_data[ 
     208                PREPROCESS] = self.processing_instructions 
     209        if self._is_call_local() and not recurse: 
     210            basename, _ = os.path.splitext(os.path.basename(self.f_open.name)) 
     211            self.current_datainfo.filename = basename + self.extension 
    211212        # Create an empty dataset if no data has been passed to the reader 
    212213        if self.current_dataset is None: 
    213             self.current_dataset = plottable_1D(np.empty(0), np.empty(0), 
    214                 np.empty(0), np.empty(0)) 
     214            self._initialize_new_data_set(dom) 
    215215        self.base_ns = "{" + CANSAS_NS.get(self.cansas_version).get("ns") + "}" 
    216216 
     
    224224            tagname_original = tagname 
    225225            # Skip this iteration when loading in save state information 
    226             if tagname == "fitting_plug_in" or tagname == "pr_inversion" or tagname == "invariant": 
     226            if tagname in ["fitting_plug_in", "pr_inversion", "invariant", "corfunc"]: 
    227227                continue 
    228228            # Get where to store content 
     
    254254                self._add_intermediate() 
    255255            else: 
     256                # TODO: Clean this up to make it faster (fewer if/elifs) 
    256257                if isinstance(self.current_dataset, plottable_2D): 
    257258                    data_point = node.text 
     
    498499            self.sort_two_d_data() 
    499500            self.reset_data_list() 
    500             empty = None 
    501             return self.output[0], empty 
    502  
    503     def data_cleanup(self): 
    504         """ 
    505         Clean up the data sets and refresh everything 
    506         :return: None 
    507         """ 
    508         has_error_dx = self.current_dataset.dx is not None 
    509         has_error_dxl = self.current_dataset.dxl is not None 
    510         has_error_dxw = self.current_dataset.dxw is not None 
    511         has_error_dy = self.current_dataset.dy is not None 
    512         self.remove_empty_q_values(has_error_dx=has_error_dx, 
    513                                    has_error_dxl=has_error_dxl, 
    514                                    has_error_dxw=has_error_dxw, 
    515                                    has_error_dy=has_error_dy) 
    516         self.send_to_output()  # Combine datasets with DataInfo 
    517         self.current_datainfo = DataInfo()  # Reset DataInfo 
     501            return self.output[0], None 
    518502 
    519503    def _is_call_local(self): 
     
    549533            self.aperture = Aperture() 
    550534        elif self.parent_class == 'SASdata': 
    551             self._check_for_empty_resolution() 
    552535            self.data.append(self.current_dataset) 
    553536 
     
    605588        if 'unit' in attr and attr.get('unit') is not None: 
    606589            try: 
    607                 local_unit = attr['unit'] 
     590                unit = attr['unit'] 
     591                unit_list = unit.split("|") 
     592                if len(unit_list) > 1: 
     593                    self.current_dataset.xaxis(unit_list[0].strip(), 
     594                                               unit_list[1].strip()) 
     595                    local_unit = unit_list[1] 
     596                else: 
     597                    local_unit = unit 
    608598                unitname = self.ns_list.current_level.get("unit", "") 
    609599                if "SASdetector" in self.names: 
     
    659649        return node_value, value_unit 
    660650 
    661     def _check_for_empty_resolution(self): 
    662         """ 
    663         a method to check all resolution data sets are the same size as I and q 
    664         """ 
    665         dql_exists = False 
    666         dqw_exists = False 
    667         dq_exists = False 
    668         di_exists = False 
    669         if self.current_dataset.dxl is not None: 
    670             dql_exists = True 
    671         if self.current_dataset.dxw is not None: 
    672             dqw_exists = True 
    673         if self.current_dataset.dx is not None: 
    674             dq_exists = True 
    675         if self.current_dataset.dy is not None: 
    676             di_exists = True 
    677         if dqw_exists and not dql_exists: 
    678             array_size = self.current_dataset.dxw.size 
    679             self.current_dataset.dxl = np.zeros(array_size) 
    680         elif dql_exists and not dqw_exists: 
    681             array_size = self.current_dataset.dxl.size 
    682             self.current_dataset.dxw = np.zeros(array_size) 
    683         elif not dql_exists and not dqw_exists and not dq_exists: 
    684             array_size = self.current_dataset.x.size 
    685             self.current_dataset.dx = np.append(self.current_dataset.dx, 
    686                                                 np.zeros([array_size])) 
    687         if not di_exists: 
    688             array_size = self.current_dataset.y.size 
    689             self.current_dataset.dy = np.append(self.current_dataset.dy, 
    690                                                 np.zeros([array_size])) 
    691  
    692651    def _initialize_new_data_set(self, node=None): 
    693652        if node is not None: 
  • src/sas/sascalc/dataloader/readers/__init__.py

    r488f3a5 raaa801e  
    11# Method to associate extensions to default readers 
    2 from associations import read_associations 
     2from .associations import read_associations 
  • src/sas/sascalc/dataloader/readers/anton_paar_saxs_reader.py

    rfafe52a ra5bd87a  
    6363        ## Reinitialize the class when loading a new data file to reset all class variables 
    6464        self.reset_state() 
    65         buff = self.f_open.read() 
     65        buff = self.readall() 
    6666        self.raw_data = buff.splitlines() 
    6767        self.read_data() 
  • src/sas/sascalc/dataloader/readers/associations.py

    rce8c7bd r574adc7  
    4040    """ 
    4141    # For each FileType entry, get the associated reader and extension 
    42     for ext, reader in settings.iteritems(): 
     42    for ext, reader in settings.items(): 
    4343        if reader is not None and ext is not None: 
    4444            # Associate the extension with a particular reader 
     
    4747            # and remove the extra line below. 
    4848            try: 
    49                 exec "import %s" % reader 
    50                 exec "loader.associate_file_type('%s', %s)" % (ext.lower(), 
    51                                                                 reader) 
    52                 exec "loader.associate_file_type('%s', %s)" % (ext.upper(), 
    53                                                                 reader) 
     49                exec("from . import %s" % reader) 
     50                exec("loader.associate_file_type('%s', %s)" 
     51                     % (ext.lower(), reader)) 
     52                exec("loader.associate_file_type('%s', %s)" 
     53                     % (ext.upper(), reader)) 
    5454            except: 
    5555                msg = "read_associations: skipping association" 
  • src/sas/sascalc/dataloader/readers/cansas_reader_HDF5.py

    rcd57c7d4 r7b50f14  
    99import sys 
    1010 
    11 from sas.sascalc.dataloader.data_info import plottable_1D, plottable_2D,\ 
     11from ..data_info import plottable_1D, plottable_2D,\ 
    1212    Data1D, Data2D, DataInfo, Process, Aperture, Collimation, \ 
    1313    TransmissionSpectrum, Detector 
    14 from sas.sascalc.dataloader.data_info import combine_data_info_with_plottable 
    15 from sas.sascalc.dataloader.loader_exceptions import FileContentsException, DefaultReaderException 
    16 from sas.sascalc.dataloader.file_reader_base_class import FileReader 
    17  
     14from ..data_info import combine_data_info_with_plottable 
     15from ..loader_exceptions import FileContentsException, DefaultReaderException 
     16from ..file_reader_base_class import FileReader, decode 
     17 
     18def h5attr(node, key, default=None): 
     19    return decode(node.attrs.get(key, default)) 
    1820 
    1921class Reader(FileReader): 
     
    130132            # Get all information for the current key 
    131133            value = data.get(key) 
    132             if value.attrs.get(u'canSAS_class') is not None: 
    133                 class_name = value.attrs.get(u'canSAS_class') 
    134             else: 
    135                 class_name = value.attrs.get(u'NX_class') 
     134            class_name = h5attr(value, u'canSAS_class') 
     135            if class_name is None: 
     136                class_name = h5attr(value, u'NX_class') 
    136137            if class_name is not None: 
    137138                class_prog = re.compile(class_name) 
     
    225226 
    226227                for data_point in data_set: 
     228                    if data_point.dtype.char == 'S': 
     229                        data_point = decode(bytes(data_point)) 
    227230                    # Top Level Meta Data 
    228231                    if key == u'definition': 
     
    231234                        self.current_datainfo.run.append(data_point) 
    232235                        try: 
    233                             run_name = value.attrs['name'] 
     236                            run_name = h5attr(value, 'name') 
    234237                            run_dict = {data_point: run_name} 
    235238                            self.current_datainfo.run_name = run_dict 
    236                         except: 
     239                        except Exception: 
    237240                            pass 
    238241                    elif key == u'title': 
     
    576579        :return: unit for the value passed to the method 
    577580        """ 
    578         unit = value.attrs.get(u'units') 
     581        unit = h5attr(value, u'units') 
    579582        if unit is None: 
    580             unit = value.attrs.get(u'unit') 
     583            unit = h5attr(value, u'unit') 
    581584        # Convert the unit formats 
    582585        if unit == "1/A": 
  • src/sas/sascalc/dataloader/readers/danse_reader.py

    ra78a02f raf3e9f5  
    1414import math 
    1515import os 
     16import logging 
     17 
    1618import numpy as np 
    17 import logging 
    18 from sas.sascalc.dataloader.data_info import plottable_2D, DataInfo, Detector 
    19 from sas.sascalc.dataloader.manipulations import reader2D_converter 
    20 from sas.sascalc.dataloader.file_reader_base_class import FileReader 
    21 from sas.sascalc.dataloader.loader_exceptions import FileContentsException, DataReaderException 
     19 
     20from ..data_info import plottable_2D, DataInfo, Detector 
     21from ..manipulations import reader2D_converter 
     22from ..file_reader_base_class import FileReader 
     23from ..loader_exceptions import FileContentsException, DataReaderException 
    2224 
    2325logger = logging.getLogger(__name__) 
     
    7880        data_start_line = 1 
    7981        while read_on: 
    80             line = self.f_open.readline() 
     82            line = self.nextline() 
    8183            data_start_line += 1 
    8284            if line.find("DATA:") >= 0: 
     
    112114            raise FileContentsException(msg) 
    113115 
    114         for line_num, data_str in enumerate(self.f_open.readlines()): 
     116        for line_num, data_str in enumerate(self.nextlines()): 
    115117            toks = data_str.split() 
    116118            try: 
  • src/sas/sascalc/dataloader/readers/red2d_reader.py

    r2f85af7 rc8321cfc  
    1010###################################################################### 
    1111import os 
     12import math 
     13import time 
     14 
    1215import numpy as np 
    13 import math 
    14 from sas.sascalc.dataloader.data_info import plottable_2D, DataInfo, Detector 
    15 from sas.sascalc.dataloader.file_reader_base_class import FileReader 
    16 from sas.sascalc.dataloader.loader_exceptions import FileContentsException 
    17  
    18 # Look for unit converter 
    19 has_converter = True 
    20 try: 
    21     from sas.sascalc.data_util.nxsunit import Converter 
    22 except: 
    23     has_converter = False 
     16 
     17from sas.sascalc.data_util.nxsunit import Converter 
     18 
     19from ..data_info import plottable_2D, DataInfo, Detector 
     20from ..file_reader_base_class import FileReader 
     21from ..loader_exceptions import FileContentsException 
    2422 
    2523 
     
    3129    try: 
    3230        return float(x_point) 
    33     except: 
     31    except Exception: 
    3432        return 0 
    3533 
     
    5149        :param data: data2D 
    5250        """ 
    53         import time 
    5451        # Write the file 
    5552        try: 
     
    7269    def get_file_contents(self): 
    7370        # Read file 
    74         buf = self.f_open.read() 
     71        buf = self.readall() 
    7572        self.f_open.close() 
    7673        # Instantiate data object 
     
    119116                try: 
    120117                    wavelength = float(line_toks[1]) 
    121                     # Units 
    122                     if has_converter == True and \ 
    123                     self.current_datainfo.source.wavelength_unit != 'A': 
     118                    # Wavelength is stored in angstroms; convert if necessary 
     119                    if self.current_datainfo.source.wavelength_unit != 'A': 
    124120                        conv = Converter('A') 
    125121                        wavelength = conv(wavelength, 
    126122                                          units=self.current_datainfo.source.wavelength_unit) 
    127                 except: 
    128                     #Not required 
    129                     pass 
    130                 # Distance in mm 
     123                except Exception: 
     124                    pass  # Not required 
    131125                try: 
    132126                    distance = float(line_toks[3]) 
    133                     # Units 
    134                     if has_converter == True and self.current_datainfo.detector[0].distance_unit != 'm': 
     127                    # Distance is stored in meters; convert if necessary 
     128                    if self.current_datainfo.detector[0].distance_unit != 'm': 
    135129                        conv = Converter('m') 
    136130                        distance = conv(distance, 
    137131                            units=self.current_datainfo.detector[0].distance_unit) 
    138                 except: 
    139                     #Not required 
    140                     pass 
    141  
    142                 # Distance in meters 
     132                except Exception: 
     133                    pass  # Not required 
     134 
    143135                try: 
    144136                    transmission = float(line_toks[4]) 
    145                 except: 
    146                     #Not required 
    147                     pass 
     137                except Exception: 
     138                    pass  # Not required 
    148139 
    149140            if line.count("LAMBDA") > 0: 
     
    170161 
    171162            ## Read and get data. 
    172             if data_started == True: 
     163            if data_started: 
    173164                line_toks = line.split() 
    174165                if len(line_toks) == 0: 
     
    178169                col_num = len(line_toks) 
    179170                break 
     171 
    180172        # Make numpy array to remove header lines using index 
    181173        lines_array = np.array(lines) 
     
    203195        # Change it(string) into float 
    204196        #data_list = map(float,data_list) 
    205         data_list1 = map(check_point, data_list) 
     197        data_list1 = list(map(check_point, data_list)) 
    206198 
    207199        # numpy array form 
     
    211203        try: 
    212204            data_point = data_array.reshape(row_num, col_num).transpose() 
    213         except: 
     205        except Exception: 
    214206            msg = "red2d_reader can't read this file: Incorrect number of data points provided." 
    215207            raise FileContentsException(msg) 
     
    325317 
    326318        # Units of axes 
    327         self.current_dataset.xaxis("\\rm{Q_{x}}", 'A^{-1}') 
    328         self.current_dataset.yaxis("\\rm{Q_{y}}", 'A^{-1}') 
    329         self.current_dataset.zaxis("\\rm{Intensity}", "cm^{-1}") 
     319        self.current_dataset.xaxis(r"\rm{Q_{x}}", 'A^{-1}') 
     320        self.current_dataset.yaxis(r"\rm{Q_{y}}", 'A^{-1}') 
     321        self.current_dataset.zaxis(r"\rm{Intensity}", "cm^{-1}") 
    330322 
    331323        # Store loading process information 
  • src/sas/sascalc/dataloader/readers/sesans_reader.py

    rbe43448 r849094a  
    66    Jurrian Bakker 
    77""" 
     8import os 
     9 
    810import numpy as np 
    9 import os 
    10 from sas.sascalc.dataloader.file_reader_base_class import FileReader 
    11 from sas.sascalc.dataloader.data_info import plottable_1D, DataInfo 
    12 from sas.sascalc.dataloader.loader_exceptions import FileContentsException, DataReaderException 
     11 
     12from ..file_reader_base_class import FileReader 
     13from ..data_info import plottable_1D, DataInfo 
     14from ..loader_exceptions import FileContentsException, DataReaderException 
    1315 
    1416# Check whether we have a converter available 
     
    4244        self.output = [] 
    4345 
    44         line = self.f_open.readline() 
     46        line = self.nextline() 
    4547        params = {} 
    4648        while not line.startswith("BEGIN_DATA"): 
     
    4850            if len(terms) >= 2: 
    4951                params[terms[0]] = " ".join(terms[1:]) 
    50             line = self.f_open.readline() 
     52            line = self.nextline() 
    5153        self.params = params 
    5254 
     
    6870                               "handled by other software.") 
    6971 
    70         headers = self.f_open.readline().split() 
     72        headers = self.nextline().split() 
    7173 
    7274        self._insist_header(headers, "SpinEchoLength") 
  • src/sas/sascalc/dataloader/readers/tiff_reader.py

    r959eb01 r574adc7  
    22#This software was developed by the University of Tennessee as part of the 
    33#Distributed Data Analysis of Neutron Scattering Experiments (DANSE) 
    4 #project funded by the US National Science Foundation.  
     4#project funded by the US National Science Foundation. 
    55#See the license text in license.txt 
    66#copyright 2008, University of Tennessee 
     
    3131    ## Extension 
    3232    ext = ['.tif', '.tiff'] 
    33          
     33 
    3434    def read(self, filename=None): 
    3535        """ 
    3636        Open and read the data in a file 
    37          
     37 
    3838        :param file: path of the file 
    3939        """ 
     
    4444        except: 
    4545            msg = "tiff_reader: could not load file. Missing Image module." 
    46             raise RuntimeError, msg 
    47          
     46            raise RuntimeError(msg) 
     47 
    4848        # Instantiate data object 
    4949        output = Data2D() 
    5050        output.filename = os.path.basename(filename) 
    51              
     51 
    5252        # Read in the image 
    5353        try: 
    5454            im = Image.open(filename) 
    5555        except: 
    56             raise  RuntimeError, "cannot open %s"%(filename) 
     56            raise  RuntimeError("cannot open %s"%(filename)) 
    5757        data = im.getdata() 
    5858 
     
    6161        output.err_data = np.zeros([im.size[0], im.size[1]]) 
    6262        output.mask = np.ones([im.size[0], im.size[1]], dtype=bool) 
    63          
     63 
    6464        # Initialize 
    6565        x_vals = [] 
     
    6969        for i_x in range(im.size[0]): 
    7070            x_vals.append(i_x) 
    71              
     71 
    7272        itot = 0 
    7373        for i_y in range(im.size[1]): 
     
    8080                logger.error("tiff_reader: had to skip a non-float point") 
    8181                continue 
    82              
     82 
    8383            # Get bin number 
    8484            if math.fmod(itot, im.size[0]) == 0: 
     
    8787            else: 
    8888                i_x += 1 
    89                  
     89 
    9090            output.data[im.size[1] - 1 - i_y][i_x] = value 
    91              
     91 
    9292            itot += 1 
    93                  
     93 
    9494        output.xbins = im.size[0] 
    9595        output.ybins = im.size[1] 
     
    102102        output.ymin = 0 
    103103        output.ymax = im.size[0] - 1 
    104          
     104 
    105105        # Store loading process information 
    106106        output.meta_data['loader'] = self.type_name 
  • src/sas/sascalc/dataloader/readers/xml_reader.py

    rcd57c7d4 r7b50f14  
    1616 
    1717import logging 
     18 
    1819from lxml import etree 
    1920from lxml.builder import E 
    20 from sas.sascalc.dataloader.file_reader_base_class import FileReader 
     21 
     22from ..file_reader_base_class import FileReader, decode 
    2123 
    2224logger = logging.getLogger(__name__) 
     
    151153        Converts an etree element into a string 
    152154        """ 
    153         return etree.tostring(elem, pretty_print=pretty_print, \ 
    154                               encoding=encoding) 
     155        return decode(etree.tostring(elem, pretty_print=pretty_print, 
     156                                     encoding=encoding)) 
    155157 
    156158    def break_processing_instructions(self, string, dic): 
Note: See TracChangeset for help on using the changeset viewer.