Changeset dcb91cf in sasview
- Timestamp:
- Aug 21, 2017 10:16:13 AM (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, costrafo411, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 7b15990
- Parents:
- 44daa56
- Files:
-
- 2 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/dataloader/file_reader_base_class.py
r83ee7258 rdcb91cf 84 84 :param msg: Error message 85 85 """ 86 if isinstance(self.current_datainfo, DataInfo): 86 if len(self.output) > 0: 87 self.output[-1].errors.append(msg) 88 elif isinstance(self.current_datainfo, DataInfo): 87 89 self.current_datainfo.errors.append(msg) 88 90 else: … … 120 122 if data.dlam is not None: 121 123 data.dlam = np.asarray([data.dlam[i] for i in ind]).astype(np.float64) 122 if len(data.x > 0):124 if len(data.x) > 0: 123 125 data.xmin = np.min(data.x) 124 126 data.xmax = np.max(data.x) -
src/sas/sascalc/dataloader/loader.py
r3a81cd9 rdcb91cf 69 69 readers if no reader was registered for the file's extension. 70 70 """ 71 # Gets set to a string if the file has an associated reader that fails 72 msg_from_reader = None 71 73 try: 72 74 return super(Registry, self).load(path, format=format) 73 75 except NoKnownLoaderException as nkl_e: 74 pass # try the ASCII reader76 pass # Try the ASCII reader 75 77 except FileContentsException as fc_exc: 76 # File has an associated reader but it failed 77 raise RuntimeError(fc_exc.message) 78 # File has an associated reader but it failed. 79 # Save the error message to display later, but try the 3 default loaders 80 msg_from_reader = fc_exc.message 78 81 except Exception: 79 82 pass 80 83 81 # File has no associated reader - try the ASCII reader 84 # File has no associated reader, or the associated reader failed. 85 # Try the ASCII reader 82 86 try: 83 87 ascii_loader = ascii_reader.Reader() … … 86 90 pass # Loader specific error to try the cansas XML reader 87 91 except FileContentsException as e: 88 raise RuntimeError(e.message) 92 if msg_from_reader is None: 93 raise RuntimeError(e.message) 89 94 90 95 # ASCII reader failed - try CanSAS xML reader … … 95 100 pass # Loader specific error to try the NXcanSAS reader 96 101 except FileContentsException as e: 97 raise RuntimeError(e.message) 98 except Exception as csr: 102 if msg_from_reader is None: 103 raise RuntimeError(e.message) 104 except Exception: 99 105 pass 100 106 … … 106 112 logging.error("No default loader can load the data") 107 113 # No known reader available. Give up and throw an error 108 msg = "\n\tUnknown data format: %s.\n\tThe file is not a " % path 109 msg += "known format that can be loaded by SasView.\n" 110 raise NoKnownLoaderException(msg) 114 if msg_from_reader is None: 115 msg = "\nUnknown data format: {}.\nThe file is not a ".format(path) 116 msg += "known format that can be loaded by SasView.\n" 117 raise NoKnownLoaderException(msg) 118 else: 119 # Associated reader and default readers all failed. 120 # Show error message from associated reader 121 raise RuntimeError(msg_from_reader) 111 122 except FileContentsException as e: 112 raise RuntimeError(e.message) 123 err_msg = msg_from_reader if msg_from_reader is not None else e.message 124 raise RuntimeError(err_msg) 113 125 114 126 def find_plugins(self, dir): -
src/sas/sascalc/dataloader/readers/cansas_reader.py
r248ff73 rdcb91cf 157 157 raise DataReaderException(invalid_xml) # Handled by base class 158 158 except FileContentsException as fc_exc: 159 if not self.extension in self.ext: # If the file has no associated loader160 raise DefaultReaderException(msg)161 159 msg = "CanSAS Reader could not load the file {}".format(xml_file) 162 160 if fc_exc.message is not None: # Propagate error messages from earlier 163 161 msg = fc_exc.message 162 if not self.extension in self.ext: # If the file has no associated loader 163 raise DefaultReaderException(msg) 164 164 raise FileContentsException(msg) 165 165 pass … … 179 179 self.set_xml_file(xml_file) 180 180 except etree.XMLSyntaxError: # File isn't valid XML so can't be loaded 181 msg = " Cansas cannot load {}.\nInvalid XML syntax".format(xml_file)181 msg = "SasView cannot load {}.\nInvalid XML syntax".format(xml_file) 182 182 raise FileContentsException(msg) 183 183 … … 207 207 return True # Why is this required? 208 208 # If we get to this point then file isn't valid CanSAS 209 logger.warning("File doesn't meet CanSAS schema. Trying to load anyway.") 209 210 raise FileContentsException("The file is not valid CanSAS") 210 211 -
src/sas/sascalc/dataloader/readers/cansas_reader_HDF5.py
r8dec7e7 rdcb91cf 82 82 raise DefaultReaderException(msg) 83 83 raise FileContentsException(e.message) 84 # Read in all child elements of top level SASroot 85 self.read_children(self.raw_data, []) 86 # Add the last data set to the list of outputs 87 self.add_data_set() 88 # Close the data file 89 self.raw_data.close() 90 # Return data set(s) 91 return self.output 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.") 92 100 93 101 def reset_class_variables(self): -
src/sas/sascalc/file_converter/ascii2d_loader.py
r0f010c9 rdcb91cf 13 13 # 2: q_y axis label and units 14 14 # 3: Intensity axis label and units 15 # 4: n UseRec - number of lines of user content following this line16 # 5 - 5+nUseRec: user content15 # 4: n_use_rec - number of lines of user content following this line 16 # 5 to (5+n_use_rec): user content 17 17 # Number of qx points 18 18 # List of qx points … … 34 34 35 35 :return: A Data2D instance containing data from the file 36 :raises ValueError: Raises a ValueError if the file is incorrectly formatted 36 :raises ValueError: Raises a ValueError if the file is incorrectly 37 formatted 37 38 """ 38 file_handle = open(self.data_path, 'r')39 file_buffer = file_handle.read()40 all_lines = file_buffer.splitlines()39 with open(self.data_path, 'r') as file_handle: 40 file_buffer = file_handle.read() 41 all_lines = file_buffer.splitlines() 41 42 42 # Load num_points line-by-line from lines into a numpy array, starting43 #on line number start_line44 def _load_points(lines, start_line, num_points):45 qs = np.zeros(num_points)46 n = start_line47 filled = 048 while filled < num_points:49 row = np.fromstring(lines[n], dtype=np.float32, sep=' ')50 qs[filled:filled+len(row)] = row51 filled += len(row)52 n += 153 return n, qs43 # Load num_points line-by-line from lines into a numpy array, 44 # starting on line number start_line 45 def _load_points(lines, start_line, num_points): 46 qs = np.zeros(num_points) 47 n = start_line 48 filled = 0 49 while filled < num_points: 50 row = np.fromstring(lines[n], dtype=np.float32, sep=' ') 51 qs[filled:filled+len(row)] = row 52 filled += len(row) 53 n += 1 54 return n, qs 54 55 55 current_line = 4 56 try: 57 # Skip nUseRec lines 58 nUseRec = int(all_lines[current_line].strip()[0]) 59 current_line += nUseRec + 1 60 # Read qx data 61 num_qs = int(all_lines[current_line].strip()) 56 current_line = 4 57 try: 58 # Skip n_use_rec lines 59 n_use_rec = int(all_lines[current_line].strip()[0]) 60 current_line += n_use_rec + 1 61 # Read qx data 62 num_qs = int(all_lines[current_line].strip()) 63 current_line += 1 64 current_line, qx = _load_points(all_lines, current_line, num_qs) 65 66 # Read qy data 67 num_qs = int(all_lines[current_line].strip()) 68 current_line += 1 69 current_line, qy = _load_points(all_lines, current_line, num_qs) 70 except ValueError as e: 71 err_msg = "File incorrectly formatted.\n" 72 if str(e).find('broadcast') != -1: 73 err_msg += "Incorrect number of q data points provided. " 74 err_msg += "Expected {}.".format(num_qs) 75 elif str(e).find('invalid literal') != -1: 76 err_msg += ("Expected integer on line {}. " 77 "Instead got '{}'").format(current_line + 1, 78 all_lines[current_line]) 79 else: 80 err_msg += str(e) 81 raise ValueError(err_msg) 82 83 # dimensions: [width, height, scale] 84 try: 85 dimensions = np.fromstring(all_lines[current_line], 86 dtype=np.float32, sep=' ') 87 if len(dimensions) != 3: raise ValueError() 88 width = int(dimensions[0]) 89 height = int(dimensions[1]) 90 except ValueError as e: 91 err_msg = "File incorrectly formatted.\n" 92 err_msg += ("Expected line {} to be of the form: <num_qx> " 93 "<num_qy> <scale>.").format(current_line + 1) 94 err_msg += " Instead got '{}'.".format(all_lines[current_line]) 95 raise ValueError(err_msg) 96 97 if width > len(qx) or height > len(qy): 98 err_msg = "File incorrectly formatted.\n" 99 err_msg += ("Line {} says to use {}x{} points. " 100 "Only {}x{} provided.").format(current_line + 1, width, 101 height, len(qx), len(qy)) 102 raise ValueError(err_msg) 103 104 # More qx and/or qy points can be provided than are actually used 105 qx = qx[:width] 106 qy = qy[:height] 107 62 108 current_line += 1 63 current_line, qx = _load_points(all_lines, current_line, num_qs) 109 # iflag = 1 => Only intensity data (not dealt with here) 110 # iflag = 2 => q axis and intensity data 111 # iflag = 3 => q axis, intensity and error data 112 try: 113 iflag = int(all_lines[current_line].strip()[0]) 114 if iflag <= 0 or iflag > 3: raise ValueError() 115 except: 116 err_msg = "File incorrectly formatted.\n" 117 iflag = all_lines[current_line].strip()[0] 118 err_msg += ("Expected iflag on line {} to be 1, 2 or 3. " 119 "Instead got '{}'.").format(current_line+1, iflag) 120 raise ValueError(err_msg) 64 121 65 # Read qy data66 num_qs = int(all_lines[current_line].strip())67 122 current_line += 1 68 current_line, qy = _load_points(all_lines, current_line, num_qs)69 except ValueError as e:70 err_msg = "File incorrectly formatted.\n"71 if str(e).find('broadcast') != -1:72 err_msg += "Incorrect number of q data points provided. "73 err_msg += "Expected {}.".format(num_qs)74 elif str(e).find('invalid literal') != -1:75 err_msg += "Expected integer on line {}. Instead got '{}'".format(current_line + 1,76 all_lines[current_line])77 else:78 err_msg += str(e)79 raise ValueError(err_msg)80 123 81 # dimensions: [width, height, scale] 82 try: 83 dimensions = np.fromstring(all_lines[current_line], dtype=np.float32, sep=' ') 84 if len(dimensions) != 3: raise ValueError() 85 width = int(dimensions[0]) 86 height = int(dimensions[1]) 87 except ValueError as e: 88 err_msg = "File incorrectly formatted.\n" 89 err_msg += "Expected line {} to be of the form: <num_qx> <num_qy> <scale>.".format(current_line + 1) 90 err_msg += " Instead got '{}'.".format(all_lines[current_line]) 91 raise ValueError(err_msg) 124 try: 125 current_line, I = _load_points(all_lines, current_line, 126 width * height) 127 dI = np.zeros(width*height) 92 128 93 if width > len(qx) or height > len(qy): 94 err_msg = "File incorrectly formatted.\n" 95 err_msg += ("Line {} says to use {}x{} points. " 96 "Only {}x{} provided.").format(current_line + 1, width, height, 97 len(qx), len(qy)) 98 raise ValueError(err_msg) 129 # Load error data if it's provided 130 if iflag == 3: 131 _, dI = _load_points(all_lines, current_line, width*height) 132 except Exception as e: 133 err_msg = "File incorrectly formatted.\n" 134 if str(e).find("list index") != -1: 135 err_msg += ("Incorrect number of data points. Expected {}" 136 " intensity").format(width * height) 137 if iflag == 3: 138 err_msg += " and error" 139 err_msg += " points." 140 else: 141 err_msg += str(e) 142 raise ValueError(err_msg) 99 143 100 # More qx and/or qy points can be provided than are actually used101 qx = qx[:width]102 qy = qy[:height]144 # Format data for use with Data2D 145 qx = list(qx) * height 146 qy = np.array([[y] * width for y in qy]).flatten() 103 147 104 current_line += 1 105 # iflag = 1 => Only intensity data (not dealt with here) 106 # iflag = 2 => q axis and intensity data 107 # iflag = 3 => q axis, intensity and error data 108 try: 109 iflag = int(all_lines[current_line].strip()[0]) 110 if iflag <= 0 or iflag > 3: raise ValueError() 111 except: 112 err_msg = "File incorrectly formatted.\n" 113 iflag = all_lines[current_line].strip()[0] 114 err_msg += "Expected iflag on line {} to be 1, 2 or 3. Instead got '{}'.".format(current_line+1, iflag) 115 raise ValueError(err_msg) 116 117 current_line += 1 118 119 try: 120 current_line, I = _load_points(all_lines, current_line, width*height) 121 dI = np.zeros(width*height) 122 123 # Load error data if it's provided 124 if iflag == 3: 125 _, dI = _load_points(all_lines, current_line, width*height) 126 except Exception as e: 127 err_msg = "File incorrectly formatted.\n" 128 if str(e).find("list index") != -1: 129 err_msg += ("Incorrect number of data points. Expected {} intensity").format(width*height) 130 if iflag == 3: 131 err_msg += " and error" 132 err_msg += " points." 133 else: 134 err_msg += str(e) 135 raise ValueError(err_msg) 136 137 # Format data for use with Data2D 138 qx = list(qx) * height 139 qy = np.array([[y] * width for y in qy]).flatten() 140 141 data = Data2D(qx_data=qx, qy_data=qy, data=I, err_data=dI) 148 data = Data2D(qx_data=qx, qy_data=qy, data=I, err_data=dI) 142 149 143 150 return data -
src/sas/sasgui/guiframe/local_perspectives/data_loader/data_loader.py
rfafe52a rdcb91cf 224 224 except NoKnownLoaderException as e: 225 225 exception_occurred = True 226 logg ing.error(e.message)226 logger.error(e.message) 227 227 228 228 error_message = "Loading data failed!\n" + e.message -
test/fileconverter/test/cansas1d.xml
rc476457 rdcb91cf 26 26 <Shadowfactor><!-- Shadowfactor is optional --></Shadowfactor> 27 27 </Idata> 28 <Idata> 29 <Q unit="1/A">0.04</Q> 30 <I unit="1/cm">1002</I> 31 <Idev unit="1/cm">4</Idev> 32 <Qdev unit="1/A">0.02</Qdev> 33 <Qmean unit="1/A"><!-- Qmean is optional --></Qmean> 34 <Shadowfactor><!-- Shadowfactor is optional --></Shadowfactor> 35 </Idata> 36 <Idata> 37 <Q unit="1/A">0.05</Q> 38 <I unit="1/cm">1003</I> 39 <Idev unit="1/cm">4</Idev> 40 <Qdev unit="1/A">0.02</Qdev> 41 <Qmean unit="1/A"><!-- Qmean is optional --></Qmean> 42 <Shadowfactor><!-- Shadowfactor is optional --></Shadowfactor> 43 </Idata> 44 <Idata> 45 <Q unit="1/A">0.06</Q> 46 <I unit="1/cm">1004</I> 47 <Idev unit="1/cm">4</Idev> 48 <Qdev unit="1/A">0.02</Qdev> 49 <Qmean unit="1/A"><!-- Qmean is optional --></Qmean> 50 <Shadowfactor><!-- Shadowfactor is optional --></Shadowfactor> 51 </Idata> 28 52 </SASdata> 29 53 <SASsample name='my sample'> … … 47 71 Some text here 48 72 </details> 49 73 50 74 </SASsample> 51 75 <SASinstrument>
Note: See TracChangeset
for help on using the changeset viewer.