Ignore:
Timestamp:
Sep 25, 2017 3: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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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: 
Note: See TracChangeset for help on using the changeset viewer.