Changeset 7af652d in sasview for src/sas/sascalc/dataloader
- Timestamp:
- Mar 5, 2019 2:26:14 PM (6 years ago)
- Branches:
- master, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1249
- Children:
- 1342f6a
- Parents:
- dbfd307 (diff), 8c9e65c (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:
- Paul Kienzle <pkienzle@…> (03/05/19 14:26:14)
- git-committer:
- GitHub <noreply@…> (03/05/19 14:26:14)
- Location:
- src/sas/sascalc/dataloader
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/dataloader/data_info.py
re090ba90 r8c9e65c 957 957 _str += "Data:\n" 958 958 _str += " Type: %s\n" % self.__class__.__name__ 959 _str += " X- & Y-axis: %s\t[%s]\n" % (self._yaxis, self._yunit) 959 _str += " X-axis: %s\t[%s]\n" % (self._xaxis, self._xunit) 960 _str += " Y-axis: %s\t[%s]\n" % (self._yaxis, self._yunit) 960 961 _str += " Z-axis: %s\t[%s]\n" % (self._zaxis, self._zunit) 961 962 _str += " Length: %g \n" % (len(self.data)) … … 986 987 qx_data=qx_data, qy_data=qy_data, 987 988 q_data=q_data, mask=mask) 989 990 clone._xaxis = self._xaxis 991 clone._yaxis = self._yaxis 992 clone._zaxis = self._zaxis 993 clone._xunit = self._xunit 994 clone._yunit = self._yunit 995 clone._zunit = self._zunit 996 clone.x_bins = self.x_bins 997 clone.y_bins = self.y_bins 988 998 989 999 clone.title = self.title … … 1156 1166 def combine_data_info_with_plottable(data, datainfo): 1157 1167 """ 1158 A function that combines the DataInfo data in self.current_datainto with a plottable_1D or 2D data object. 1168 A function that combines the DataInfo data in self.current_datainto with a 1169 plottable_1D or 2D data object. 1159 1170 1160 1171 :param data: A plottable_1D or plottable_2D data object … … 1174 1185 final_dataset.yaxis(data._yaxis, data._yunit) 1175 1186 elif isinstance(data, plottable_2D): 1176 final_dataset = Data2D(data.data, data.err_data, data.qx_data, data.qy_data, data.q_data, 1177 data.mask, data.dqx_data, data.dqy_data) 1187 final_dataset = Data2D(data.data, data.err_data, data.qx_data, 1188 data.qy_data, data.q_data, data.mask, 1189 data.dqx_data, data.dqy_data) 1178 1190 final_dataset.xaxis(data._xaxis, data._xunit) 1179 1191 final_dataset.yaxis(data._yaxis, data._yunit) 1180 1192 final_dataset.zaxis(data._zaxis, data._zunit) 1181 if len(data.data.shape) == 2: 1182 n_rows, n_cols = data.data.shape 1183 final_dataset.y_bins = data.qy_data[0::int(n_cols)] 1184 final_dataset.x_bins = data.qx_data[:int(n_cols)] 1193 final_dataset.y_bins = data.y_bins 1194 final_dataset.x_bins = data.x_bins 1185 1195 else: 1186 return_string = "Should Never Happen: _combine_data_info_with_plottable input is not a plottable1d or " + \ 1187 "plottable2d data object" 1196 return_string = ("Should Never Happen: _combine_data_info_with_plottabl" 1197 "e input is not a plottable1d or plottable2d data " 1198 "object") 1188 1199 return return_string 1189 1200 -
src/sas/sascalc/dataloader/file_reader_base_class.py
r4a8d55c rc222c27 16 16 from .data_info import Data1D, Data2D, DataInfo, plottable_1D, plottable_2D,\ 17 17 combine_data_info_with_plottable 18 from sas.sascalc.data_util.nxsunit import Converter 18 19 19 20 logger = logging.getLogger(__name__) … … 37 38 "SasView cannot guarantee the accuracy of the data.") 38 39 40 39 41 class FileReader(object): 40 42 # String to describe the type of data this reader can load … … 45 47 ext = ['.txt'] 46 48 # Deprecated extensions 47 deprecated_extensions = ['.asc' , '.nxs']49 deprecated_extensions = ['.asc'] 48 50 # Bypass extension check and try to load anyway 49 51 allow_all = False … … 98 100 if len(self.output) > 0: 99 101 # Sort the data that's been loaded 100 self. sort_one_d_data()101 self.sort_ two_d_data()102 self.convert_data_units() 103 self.sort_data() 102 104 else: 103 105 msg = "Unable to find file at: {}\n".format(filepath) … … 140 142 Returns the entire file as a string. 141 143 """ 142 #return self.f_open.read()143 144 return decode(self.f_open.read()) 144 145 … … 166 167 self.output.append(data_obj) 167 168 168 def sort_ one_d_data(self):169 def sort_data(self): 169 170 """ 170 171 Sort 1D data along the X axis for consistency … … 174 175 # Normalize the units for 175 176 data.x_unit = self.format_unit(data.x_unit) 177 data._xunit = data.x_unit 176 178 data.y_unit = self.format_unit(data.y_unit) 179 data._yunit = data.y_unit 177 180 # Sort data by increasing x and remove 1st point 178 181 ind = np.lexsort((data.y, data.x)) … … 203 206 data.ymin = np.min(data.y) 204 207 data.ymax = np.max(data.y) 208 elif isinstance(data, Data2D): 209 # Normalize the units for 210 data.Q_unit = self.format_unit(data.Q_unit) 211 data.I_unit = self.format_unit(data.I_unit) 212 data._xunit = data.Q_unit 213 data._yunit = data.Q_unit 214 data._zunit = data.I_unit 215 data.data = data.data.astype(np.float64) 216 data.qx_data = data.qx_data.astype(np.float64) 217 data.xmin = np.min(data.qx_data) 218 data.xmax = np.max(data.qx_data) 219 data.qy_data = data.qy_data.astype(np.float64) 220 data.ymin = np.min(data.qy_data) 221 data.ymax = np.max(data.qy_data) 222 data.q_data = np.sqrt(data.qx_data * data.qx_data 223 + data.qy_data * data.qy_data) 224 if data.err_data is not None: 225 data.err_data = data.err_data.astype(np.float64) 226 if data.dqx_data is not None: 227 data.dqx_data = data.dqx_data.astype(np.float64) 228 if data.dqy_data is not None: 229 data.dqy_data = data.dqy_data.astype(np.float64) 230 if data.mask is not None: 231 data.mask = data.mask.astype(dtype=bool) 232 233 if len(data.data.shape) == 2: 234 n_rows, n_cols = data.data.shape 235 data.y_bins = data.qy_data[0::int(n_cols)] 236 data.x_bins = data.qx_data[:int(n_cols)] 237 data.data = data.data.flatten() 238 data = self._remove_nans_in_data(data) 239 if len(data.data) > 0: 240 data.xmin = np.min(data.qx_data) 241 data.xmax = np.max(data.qx_data) 242 data.ymin = np.min(data.qy_data) 243 data.ymax = np.max(data.qy_data) 205 244 206 245 @staticmethod … … 242 281 return data 243 282 244 def sort_two_d_data(self): 245 for dataset in self.output: 246 if isinstance(dataset, Data2D): 247 # Normalize the units for 248 dataset.x_unit = self.format_unit(dataset.Q_unit) 249 dataset.y_unit = self.format_unit(dataset.I_unit) 250 dataset.data = dataset.data.astype(np.float64) 251 dataset.qx_data = dataset.qx_data.astype(np.float64) 252 dataset.xmin = np.min(dataset.qx_data) 253 dataset.xmax = np.max(dataset.qx_data) 254 dataset.qy_data = dataset.qy_data.astype(np.float64) 255 dataset.ymin = np.min(dataset.qy_data) 256 dataset.ymax = np.max(dataset.qy_data) 257 dataset.q_data = np.sqrt(dataset.qx_data * dataset.qx_data 258 + dataset.qy_data * dataset.qy_data) 259 if dataset.err_data is not None: 260 dataset.err_data = dataset.err_data.astype(np.float64) 261 if dataset.dqx_data is not None: 262 dataset.dqx_data = dataset.dqx_data.astype(np.float64) 263 if dataset.dqy_data is not None: 264 dataset.dqy_data = dataset.dqy_data.astype(np.float64) 265 if dataset.mask is not None: 266 dataset.mask = dataset.mask.astype(dtype=bool) 267 268 if len(dataset.data.shape) == 2: 269 n_rows, n_cols = dataset.data.shape 270 dataset.y_bins = dataset.qy_data[0::int(n_cols)] 271 dataset.x_bins = dataset.qx_data[:int(n_cols)] 272 dataset.data = dataset.data.flatten() 273 dataset = self._remove_nans_in_data(dataset) 274 if len(dataset.data) > 0: 275 dataset.xmin = np.min(dataset.qx_data) 276 dataset.xmax = np.max(dataset.qx_data) 277 dataset.ymin = np.min(dataset.qy_data) 278 dataset.ymax = np.max(dataset.qx_data) 283 @staticmethod 284 def set_default_1d_units(data): 285 """ 286 Set the x and y axes to the default 1D units 287 :param data: 1D data set 288 :return: 289 """ 290 data.xaxis(r"\rm{Q}", '1/A') 291 data.yaxis(r"\rm{Intensity}", "1/cm") 292 return data 293 294 @staticmethod 295 def set_default_2d_units(data): 296 """ 297 Set the x and y axes to the default 2D units 298 :param data: 2D data set 299 :return: 300 """ 301 data.xaxis("\\rm{Q_{x}}", '1/A') 302 data.yaxis("\\rm{Q_{y}}", '1/A') 303 data.zaxis("\\rm{Intensity}", "1/cm") 304 return data 305 306 def convert_data_units(self, default_q_unit="1/A"): 307 """ 308 Converts al; data to the sasview default of units of A^{-1} for Q and 309 cm^{-1} for I. 310 :param default_q_unit: The default Q unit used by Sasview 311 """ 312 convert_q = True 313 new_output = [] 314 for data in self.output: 315 if data.isSesans: 316 new_output.append(data) 317 continue 318 try: 319 file_x_unit = data._xunit 320 data_conv_x = Converter(file_x_unit) 321 except KeyError: 322 logger.info("Unrecognized Q units in data file. No data " 323 "conversion attempted") 324 convert_q = False 325 try: 326 327 if isinstance(data, Data1D): 328 if convert_q: 329 data.x = data_conv_x(data.x, units=default_q_unit) 330 data._xunit = default_q_unit 331 data.x_unit = default_q_unit 332 if data.dx is not None: 333 data.dx = data_conv_x(data.dx, 334 units=default_q_unit) 335 if data.dxl is not None: 336 data.dxl = data_conv_x(data.dxl, 337 units=default_q_unit) 338 if data.dxw is not None: 339 data.dxw = data_conv_x(data.dxw, 340 units=default_q_unit) 341 elif isinstance(data, Data2D): 342 if convert_q: 343 data.qx_data = data_conv_x(data.qx_data, 344 units=default_q_unit) 345 if data.dqx_data is not None: 346 data.dqx_data = data_conv_x(data.dqx_data, 347 units=default_q_unit) 348 try: 349 file_y_unit = data._yunit 350 data_conv_y = Converter(file_y_unit) 351 data.qy_data = data_conv_y(data.qy_data, 352 units=default_q_unit) 353 if data.dqy_data is not None: 354 data.dqy_data = data_conv_y(data.dqy_data, 355 units=default_q_unit) 356 except KeyError: 357 logger.info("Unrecognized Qy units in data file. No" 358 " data conversion attempted") 359 except KeyError: 360 message = "Unable to convert Q units from {0} to 1/A." 361 message.format(default_q_unit) 362 data.errors.append(message) 363 new_output.append(data) 364 self.output = new_output 279 365 280 366 def format_unit(self, unit=None): … … 367 453 self.current_dataset.qy_data)) 368 454 if has_error_dy: 369 self.current_dataset.err_data = self.current_dataset.err_data[x != 0] 455 self.current_dataset.err_data = self.current_dataset.err_data[ 456 x != 0] 370 457 if has_error_dqx: 371 self.current_dataset.dqx_data = self.current_dataset.dqx_data[x != 0] 458 self.current_dataset.dqx_data = self.current_dataset.dqx_data[ 459 x != 0] 372 460 if has_error_dqy: 373 self.current_dataset.dqy_data = self.current_dataset.dqy_data[x != 0] 461 self.current_dataset.dqy_data = self.current_dataset.dqy_data[ 462 x != 0] 374 463 if has_mask: 375 464 self.current_dataset.mask = self.current_dataset.mask[x != 0] -
src/sas/sascalc/dataloader/loader.py
re090ba90 r8c9e65c 367 367 try: 368 368 return fn(path, data) 369 except Exception: 370 pass # give other loaders a chance to succeed 371 # If we get here it is because all loaders failed 372 raise # reraises last exception 369 except Exception as exc: 370 msg = "Saving file {} using the {} writer failed.\n".format( 371 path, type(fn).__name__) 372 msg += str(exc) 373 logger.exception(msg) # give other loaders a chance to succeed 373 374 374 375 -
src/sas/sascalc/dataloader/readers/abs_reader.py
rbd5c3b1 rd96744de 48 48 detector = Detector() 49 49 data_line = 0 50 x_index = 4 50 51 self.reset_data_list(len(lines)) 51 52 self.current_datainfo.detector.append(detector) … … 63 64 for line in lines: 64 65 # Information line 1 66 if line.find(".bt5") > 0: 67 x_index = 0 65 68 if is_info: 66 69 is_info = False … … 171 174 172 175 try: 173 _x = float(toks[ 4])176 _x = float(toks[x_index]) 174 177 _y = float(toks[1]) 175 178 _dy = float(toks[2]) … … 225 228 raise ValueError("ascii_reader: could not load file") 226 229 230 self.current_dataset = self.set_default_1d_units(self.current_dataset) 227 231 if data_conv_q is not None: 228 232 self.current_dataset.xaxis("\\rm{Q}", base_q_unit) 229 else:230 self.current_dataset.xaxis("\\rm{Q}", 'A^{-1}')231 233 if data_conv_i is not None: 232 234 self.current_dataset.yaxis("\\rm{Intensity}", base_i_unit) 233 else:234 self.current_dataset.yaxis("\\rm{Intensity}", "cm^{-1}")235 235 236 236 # Store loading process information -
src/sas/sascalc/dataloader/readers/ascii_reader.py
r9e6aeaf r3bab401 157 157 158 158 self.remove_empty_q_values() 159 self.current_dataset.xaxis("\\rm{Q}", 'A^{-1}') 160 self.current_dataset.yaxis("\\rm{Intensity}", "cm^{-1}") 159 self.current_dataset = self.set_default_1d_units(self.current_dataset) 161 160 162 161 # Store loading process information -
src/sas/sascalc/dataloader/readers/associations.py
re090ba90 r8c9e65c 23 23 ".ses": "sesans_reader", 24 24 ".h5": "cansas_reader_HDF5", 25 ".nxs": "cansas_reader_HDF5", 25 26 ".txt": "ascii_reader", 26 27 ".dat": "red2d_reader", -
src/sas/sascalc/dataloader/readers/cansas_reader.py
re090ba90 r8c9e65c 812 812 node.append(point) 813 813 self.write_node(point, "Q", datainfo.x[i], 814 {'unit': datainfo. x_unit})814 {'unit': datainfo._xunit}) 815 815 if len(datainfo.y) >= i: 816 816 self.write_node(point, "I", datainfo.y[i], 817 {'unit': datainfo. y_unit})817 {'unit': datainfo._yunit}) 818 818 if datainfo.dy is not None and len(datainfo.dy) > i: 819 819 self.write_node(point, "Idev", datainfo.dy[i], 820 {'unit': datainfo. y_unit})820 {'unit': datainfo._yunit}) 821 821 if datainfo.dx is not None and len(datainfo.dx) > i: 822 822 self.write_node(point, "Qdev", datainfo.dx[i], 823 {'unit': datainfo. x_unit})823 {'unit': datainfo._xunit}) 824 824 if datainfo.dxw is not None and len(datainfo.dxw) > i: 825 825 self.write_node(point, "dQw", datainfo.dxw[i], 826 {'unit': datainfo. x_unit})826 {'unit': datainfo._xunit}) 827 827 if datainfo.dxl is not None and len(datainfo.dxl) > i: 828 828 self.write_node(point, "dQl", datainfo.dxl[i], 829 {'unit': datainfo. x_unit})829 {'unit': datainfo._xunit}) 830 830 if datainfo.isSesans: 831 831 sesans_attrib = {'x_axis': datainfo._xaxis, -
src/sas/sascalc/dataloader/readers/danse_reader.py
r2469df7 rfc51d06 180 180 detector.beam_center.y = center_y * pixel 181 181 182 183 self.current_dataset.xaxis("\\rm{Q_{x}}", 'A^{-1}') 184 self.current_dataset.yaxis("\\rm{Q_{y}}", 'A^{-1}') 185 self.current_dataset.zaxis("\\rm{Intensity}", "cm^{-1}") 186 182 self.current_dataset = self.set_default_2d_units(self.current_dataset) 187 183 self.current_dataset.x_bins = x_vals 188 184 self.current_dataset.y_bins = y_vals -
src/sas/sascalc/dataloader/readers/red2d_reader.py
rc8321cfc r058f6c3 317 317 318 318 # Units of axes 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}") 319 self.current_dataset = self.set_default_2d_units(self.current_dataset) 322 320 323 321 # Store loading process information
Note: See TracChangeset
for help on using the changeset viewer.