Changeset 9e6aeaf in sasview for src/sas/sascalc/dataloader/readers/cansas_reader.py
- Timestamp:
- Sep 25, 2017 3:35:29 PM (7 years ago)
- 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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/dataloader/readers/cansas_reader.py
r9efdb29 r9e6aeaf 100 100 xml_file = self.f_open.name 101 101 # 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 107 102 try: 108 103 # Raises FileContentsException 109 104 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 }) 112 111 self.is_cansas(self.extension) 113 self.invalid = False # If we reach this point then file must be valid CanSAS114 115 # Parse each SASentry116 entry_list = self.xmlroot.xpath('/ns:SASroot/ns:SASentry', namespaces={117 'ns': self.cansas_defaults.get("ns")118 })119 # Look for a SASentry120 self.names.append("SASentry")121 112 self.set_processing_instructions() 122 123 113 for entry in entry_list: 124 self.current_datainfo.filename = basename + self.extension125 self.current_datainfo.meta_data["loader"] = "CanSAS XML 1D"126 self.current_datainfo.meta_data[PREPROCESS] = self.processing_instructions127 114 self._parse_entry(entry) 128 115 self.data_cleanup() … … 146 133 invalid_xml = self.find_invalid_xml() 147 134 if invalid_xml != "": 135 basename, _ = os.path.splitext( 136 os.path.basename(self.f_open.name)) 148 137 invalid_xml = INVALID_XML.format(basename + self.extension) + invalid_xml 149 138 raise DataReaderException(invalid_xml) # Handled by base class … … 160 149 except Exception as e: # Convert all other exceptions to FileContentsExceptions 161 150 raise FileContentsException(str(e)) 162 151 finally: 152 if not self.f_open.closed: 153 self.f_open.close() 163 154 164 155 def load_file_and_schema(self, xml_file, schema_path=""): … … 205 196 if not self._is_call_local() and not recurse: 206 197 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 207 203 self.data = [] 208 self. current_datainfo = DataInfo()204 self.parent_class = "SASentry" 209 205 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 211 212 # Create an empty dataset if no data has been passed to the reader 212 213 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) 215 215 self.base_ns = "{" + CANSAS_NS.get(self.cansas_version).get("ns") + "}" 216 216 … … 224 224 tagname_original = tagname 225 225 # 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"]: 227 227 continue 228 228 # Get where to store content … … 254 254 self._add_intermediate() 255 255 else: 256 # TODO: Clean this up to make it faster (fewer if/elifs) 256 257 if isinstance(self.current_dataset, plottable_2D): 257 258 data_point = node.text … … 498 499 self.sort_two_d_data() 499 500 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 518 502 519 503 def _is_call_local(self): … … 549 533 self.aperture = Aperture() 550 534 elif self.parent_class == 'SASdata': 551 self._check_for_empty_resolution()552 535 self.data.append(self.current_dataset) 553 536 … … 605 588 if 'unit' in attr and attr.get('unit') is not None: 606 589 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 608 598 unitname = self.ns_list.current_level.get("unit", "") 609 599 if "SASdetector" in self.names: … … 659 649 return node_value, value_unit 660 650 661 def _check_for_empty_resolution(self):662 """663 a method to check all resolution data sets are the same size as I and q664 """665 dql_exists = False666 dqw_exists = False667 dq_exists = False668 di_exists = False669 if self.current_dataset.dxl is not None:670 dql_exists = True671 if self.current_dataset.dxw is not None:672 dqw_exists = True673 if self.current_dataset.dx is not None:674 dq_exists = True675 if self.current_dataset.dy is not None:676 di_exists = True677 if dqw_exists and not dql_exists:678 array_size = self.current_dataset.dxw.size679 self.current_dataset.dxl = np.zeros(array_size)680 elif dql_exists and not dqw_exists:681 array_size = self.current_dataset.dxl.size682 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.size685 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.size689 self.current_dataset.dy = np.append(self.current_dataset.dy,690 np.zeros([array_size]))691 692 651 def _initialize_new_data_set(self, node=None): 693 652 if node is not None:
Note: See TracChangeset
for help on using the changeset viewer.