Changeset dcd6efd in sasview
- Timestamp:
- Jan 2, 2019 3:57:45 PM (6 years ago)
- Branches:
- master, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249
- Children:
- ef74a8b, a13c41b, 462e019
- Parents:
- cb9640f (diff), d96744de (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 Butler <butlerpd@…> (01/02/19 15:57:45)
- git-committer:
- GitHub <noreply@…> (01/02/19 15:57:45)
- Files:
-
- 7 added
- 5 deleted
- 25 edited
- 8 moved
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/data_util/nxsunit.py
r574adc7 rb011ecb 136 136 sld = { '10^-6 Angstrom^-2': 1e-6, 'Angstrom^-2': 1 } 137 137 Q = { 'invA': 1, 'invAng': 1, 'invAngstroms': 1, '1/A': 1, 138 '1/Angstrom': 1, '1/angstrom': 1, 'A^{-1}': 1, 'cm^{-1}': 1e-8, 138 139 '10^-3 Angstrom^-1': 1e-3, '1/cm': 1e-8, '1/m': 1e-10, 139 'nm^ -1': 0.1, '1/nm': 0.1, 'n_m^-1': 0.1 }140 'nm^{-1}': 1, 'nm^-1': 0.1, '1/nm': 0.1, 'n_m^-1': 0.1 } 140 141 141 142 _caret_optional(sld) … … 157 158 # units for that particular dimension. 158 159 # Note: don't have support for dimensionless units. 159 unknown = {None:1, '???':1, '': 1, 'a.u.': 1 }160 unknown = {None:1, '???':1, '': 1, 'a.u.': 1, 'Counts': 1, 'counts': 1} 160 161 161 162 def __init__(self, name): -
src/sas/sascalc/dataloader/data_info.py
r9e6aeaf r4fdcc65 954 954 _str += "Data:\n" 955 955 _str += " Type: %s\n" % self.__class__.__name__ 956 _str += " X- & Y-axis: %s\t[%s]\n" % (self._yaxis, self._yunit) 956 _str += " X-axis: %s\t[%s]\n" % (self._xaxis, self._xunit) 957 _str += " Y-axis: %s\t[%s]\n" % (self._yaxis, self._yunit) 957 958 _str += " Z-axis: %s\t[%s]\n" % (self._zaxis, self._zunit) 958 959 _str += " Length: %g \n" % (len(self.data)) … … 983 984 qx_data=qx_data, qy_data=qy_data, 984 985 q_data=q_data, mask=mask) 986 987 clone._xaxis = self._xaxis 988 clone._yaxis = self._yaxis 989 clone._zaxis = self._zaxis 990 clone._xunit = self._xunit 991 clone._yunit = self._yunit 992 clone._zunit = self._zunit 993 clone.x_bins = self.x_bins 994 clone.y_bins = self.y_bins 985 995 986 996 clone.title = self.title … … 1153 1163 def combine_data_info_with_plottable(data, datainfo): 1154 1164 """ 1155 A function that combines the DataInfo data in self.current_datainto with a plottable_1D or 2D data object. 1165 A function that combines the DataInfo data in self.current_datainto with a 1166 plottable_1D or 2D data object. 1156 1167 1157 1168 :param data: A plottable_1D or plottable_2D data object … … 1171 1182 final_dataset.yaxis(data._yaxis, data._yunit) 1172 1183 elif isinstance(data, plottable_2D): 1173 final_dataset = Data2D(data.data, data.err_data, data.qx_data, data.qy_data, data.q_data, 1174 data.mask, data.dqx_data, data.dqy_data) 1184 final_dataset = Data2D(data.data, data.err_data, data.qx_data, 1185 data.qy_data, data.q_data, data.mask, 1186 data.dqx_data, data.dqy_data) 1175 1187 final_dataset.xaxis(data._xaxis, data._xunit) 1176 1188 final_dataset.yaxis(data._yaxis, data._yunit) 1177 1189 final_dataset.zaxis(data._zaxis, data._zunit) 1178 if len(data.data.shape) == 2: 1179 n_rows, n_cols = data.data.shape 1180 final_dataset.y_bins = data.qy_data[0::int(n_cols)] 1181 final_dataset.x_bins = data.qx_data[:int(n_cols)] 1190 final_dataset.y_bins = data.y_bins 1191 final_dataset.x_bins = data.x_bins 1182 1192 else: 1183 return_string = "Should Never Happen: _combine_data_info_with_plottable input is not a plottable1d or " + \ 1184 "plottable2d data object" 1193 return_string = ("Should Never Happen: _combine_data_info_with_plottabl" 1194 "e input is not a plottable1d or plottable2d data " 1195 "object") 1185 1196 return return_string 1186 1197 -
src/sas/sascalc/dataloader/file_reader_base_class.py
rfee520ec 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.qy_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
r4a8d55c rb1ec23d 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
ra32c19c rc7c8143 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
r2469df7 r058f6c3 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/cansas_reader_HDF5.py
r61f329f0 r109afbd 1 1 """ 2 CanSAS 2Ddata reader for reading HDF5 formatted CanSAS files.2 NXcanSAS data reader for reading HDF5 formatted CanSAS files. 3 3 """ 4 4 … … 12 12 Data1D, Data2D, DataInfo, Process, Aperture, Collimation, \ 13 13 TransmissionSpectrum, Detector 14 from ..data_info import combine_data_info_with_plottable15 14 from ..loader_exceptions import FileContentsException, DefaultReaderException 16 15 from ..file_reader_base_class import FileReader, decode 17 16 17 try: 18 basestring 19 except NameError: # CRUFT: python 2 support 20 basestring = str 21 22 18 23 def h5attr(node, key, default=None): 19 24 return decode(node.attrs.get(key, default)) 20 25 26 21 27 class Reader(FileReader): 22 28 """ 23 A class for reading in CanSAS v2.0 data files. The existing iteration opens24 Mantid generated HDF5 formatted files with file extension .h5/.H5. Any25 number of data sets may be present within the file and any dimensionality26 of data may be used. Currently 1D and 2D SAS data sets are supported, but27 future implementations will include 1D and 2D SESANS data.28 29 Any number of SASdata sets may be present in a SASentry and the data within 30 can be either 1D I(Q) or 2D I(Qx, Qy).31 32 Also supports reading NXcanSAS formatted HDF5 files29 A class for reading in NXcanSAS data files. The current implementation has 30 been tested to load data generated by multiple facilities, all of which are 31 known to produce NXcanSAS standards compliant data. Any number of data sets 32 may be present within the file and any dimensionality of data may be used. 33 Currently 1D and 2D SAS data sets are supported, but should be immediately 34 extensible to SESANS data. 35 36 Any number of SASdata groups may be present in a SASentry and the data 37 within each SASdata group can be a single 1D I(Q), multi-framed 1D I(Q), 38 2D I(Qx, Qy) or multi-framed 2D I(Qx, Qy). 33 39 34 40 :Dependencies: 35 The CanSAS HDF5 reader requires h5py => v2.5.0 or later.41 The NXcanSAS HDF5 reader requires h5py => v2.5.0 or later. 36 42 """ 37 43 38 44 # CanSAS version 39 45 cansas_version = 2.0 40 # Logged warnings or messages41 logging = None42 # List of errors for the current data set43 errors = None44 # Raw file contents to be processed45 raw_data = None46 # List of plottable1D objects that should be linked to the current_datainfo47 data1d = None48 # List of plottable2D objects that should be linked to the current_datainfo49 data2d = None50 46 # Data type name 51 type_name = " CanSAS 2.0"47 type_name = "NXcanSAS" 52 48 # Wildcards 53 type = [" CanSAS 2.0 HDF5 Files (*.h5)|*.h5"]49 type = ["NXcanSAS HDF5 Files (*.h5)|*.h5|"] 54 50 # List of allowed extensions 55 51 ext = ['.h5', '.H5'] … … 81 77 except Exception as e: 82 78 if extension not in self.ext: 83 msg = "CanSAS2.0 HDF5 Reader could not load file {}".format(basename + extension) 79 msg = "NXcanSAS Reader could not load file {}".format( 80 basename + extension) 84 81 raise DefaultReaderException(msg) 85 82 raise FileContentsException(e.message) … … 95 92 self.raw_data.close() 96 93 97 for dataset in self.output: 98 if isinstance(dataset, Data1D): 99 if dataset.x.size < 5: 100 self.output = [] 101 raise FileContentsException("Fewer than 5 data points found.") 94 for data_set in self.output: 95 if isinstance(data_set, Data1D): 96 if data_set.x.size < 5: 97 exception = FileContentsException( 98 "Fewer than 5 data points found.") 99 data_set.errors.append(exception) 102 100 103 101 def reset_state(self): … … 109 107 self.data2d = [] 110 108 self.raw_data = None 111 self.errors = set() 109 self.multi_frame = False 110 self.data_frames = [] 111 self.data_uncertainty_frames = [] 112 self.errors = [] 112 113 self.logging = [] 114 self.q_names = [] 115 self.mask_name = u'' 116 self.i_name = u'' 117 self.i_node = u'' 118 self.i_uncertainties_name = u'' 119 self.q_uncertainty_names = [] 120 self.q_resolution_names = [] 113 121 self.parent_class = u'' 114 122 self.detector = Detector() … … 131 139 value = data.get(key) 132 140 class_name = h5attr(value, u'canSAS_class') 141 if isinstance(class_name, (list, tuple, np.ndarray)): 142 class_name = class_name[0] 133 143 if class_name is None: 134 144 class_name = h5attr(value, u'NX_class') … … 140 150 if isinstance(value, h5py.Group): 141 151 # Set parent class before recursion 152 last_parent_class = self.parent_class 142 153 self.parent_class = class_name 143 154 parent_list.append(key) … … 147 158 self.add_data_set(key) 148 159 elif class_prog.match(u'SASdata'): 149 self._initialize_new_data_set(parent_list) 160 self._find_data_attributes(value) 161 self._initialize_new_data_set(value) 150 162 # Recursion step to access data within the group 151 163 self.read_children(value, parent_list) 164 self.add_intermediate() 152 165 # Reset parent class when returning from recursive method 153 self.parent_class = class_name 154 self.add_intermediate() 166 self.parent_class = last_parent_class 155 167 parent_list.remove(key) 156 168 157 169 elif isinstance(value, h5py.Dataset): 158 170 # If this is a dataset, store the data appropriately 159 data_set = data[key][:]171 data_set = value.value 160 172 unit = self._get_unit(value) 161 162 # I and Q Data163 if key == u'I':164 if isinstance(self.current_dataset, plottable_2D):165 self.current_dataset.data = data_set166 self.current_dataset.zaxis("Intensity", unit)167 else:168 self.current_dataset.y = data_set.flatten()169 self.current_dataset.yaxis("Intensity", unit)170 continue171 elif key == u'Idev':172 if isinstance(self.current_dataset, plottable_2D):173 self.current_dataset.err_data = data_set.flatten()174 else:175 self.current_dataset.dy = data_set.flatten()176 continue177 elif key == u'Q':178 self.current_dataset.xaxis("Q", unit)179 if isinstance(self.current_dataset, plottable_2D):180 self.current_dataset.q = data_set.flatten()181 else:182 self.current_dataset.x = data_set.flatten()183 continue184 elif key == u'Qdev':185 self.current_dataset.dx = data_set.flatten()186 continue187 elif key == u'dQw':188 self.current_dataset.dxw = data_set.flatten()189 continue190 elif key == u'dQl':191 self.current_dataset.dxl = data_set.flatten()192 continue193 elif key == u'Qy':194 self.current_dataset.yaxis("Q_y", unit)195 self.current_dataset.qy_data = data_set.flatten()196 continue197 elif key == u'Qydev':198 self.current_dataset.dqy_data = data_set.flatten()199 continue200 elif key == u'Qx':201 self.current_dataset.xaxis("Q_x", unit)202 self.current_dataset.qx_data = data_set.flatten()203 continue204 elif key == u'Qxdev':205 self.current_dataset.dqx_data = data_set.flatten()206 continue207 elif key == u'Mask':208 self.current_dataset.mask = data_set.flatten()209 continue210 # Transmission Spectrum211 elif (key == u'T'212 and self.parent_class == u'SAStransmission_spectrum'):213 self.trans_spectrum.transmission = data_set.flatten()214 continue215 elif (key == u'Tdev'216 and self.parent_class == u'SAStransmission_spectrum'):217 self.trans_spectrum.transmission_deviation = \218 data_set.flatten()219 continue220 elif (key == u'lambda'221 and self.parent_class == u'SAStransmission_spectrum'):222 self.trans_spectrum.wavelength = data_set.flatten()223 continue224 173 225 174 for data_point in data_set: … … 231 180 # Top Level Meta Data 232 181 if key == u'definition': 233 self.current_datainfo.meta_data['reader'] = data_point 182 if isinstance(data_set, basestring): 183 self.current_datainfo.meta_data['reader'] = data_set 184 break 185 else: 186 self.current_datainfo.meta_data[ 187 'reader'] = data_point 188 # Run 234 189 elif key == u'run': 235 self.current_datainfo.run.append(data_point)236 190 try: 237 191 run_name = h5attr(value, 'name') 238 run_dict = {data_ point: run_name}192 run_dict = {data_set: run_name} 239 193 self.current_datainfo.run_name = run_dict 240 194 except Exception: 241 195 pass 196 if isinstance(data_set, basestring): 197 self.current_datainfo.run.append(data_set) 198 break 199 else: 200 self.current_datainfo.run.append(data_point) 201 # Title 242 202 elif key == u'title': 243 self.current_datainfo.title = data_point 203 if isinstance(data_set, basestring): 204 self.current_datainfo.title = data_set 205 break 206 else: 207 self.current_datainfo.title = data_point 208 # Note 244 209 elif key == u'SASnote': 245 self.current_datainfo.notes.append(data_ point)246 210 self.current_datainfo.notes.append(data_set) 211 break 247 212 # Sample Information 248 # CanSAS 2.0 format 249 elif key == u'Title' and self.parent_class == u'SASsample': 250 self.current_datainfo.sample.name = data_point 251 # NXcanSAS format 252 elif key == u'name' and self.parent_class == u'SASsample': 253 self.current_datainfo.sample.name = data_point 254 # NXcanSAS format 255 elif key == u'ID' and self.parent_class == u'SASsample': 256 self.current_datainfo.sample.name = data_point 257 elif (key == u'thickness' 258 and self.parent_class == u'SASsample'): 259 self.current_datainfo.sample.thickness = data_point 260 elif (key == u'temperature' 261 and self.parent_class == u'SASsample'): 262 self.current_datainfo.sample.temperature = data_point 263 elif (key == u'transmission' 264 and self.parent_class == u'SASsample'): 265 self.current_datainfo.sample.transmission = data_point 266 elif (key == u'x_position' 267 and self.parent_class == u'SASsample'): 268 self.current_datainfo.sample.position.x = data_point 269 elif (key == u'y_position' 270 and self.parent_class == u'SASsample'): 271 self.current_datainfo.sample.position.y = data_point 272 elif key == u'pitch' and self.parent_class == u'SASsample': 273 self.current_datainfo.sample.orientation.x = data_point 274 elif key == u'yaw' and self.parent_class == u'SASsample': 275 self.current_datainfo.sample.orientation.y = data_point 276 elif key == u'roll' and self.parent_class == u'SASsample': 277 self.current_datainfo.sample.orientation.z = data_point 278 elif (key == u'details' 279 and self.parent_class == u'SASsample'): 280 self.current_datainfo.sample.details.append(data_point) 281 213 elif self.parent_class == u'SASsample': 214 self.process_sample(data_point, key) 282 215 # Instrumental Information 283 216 elif (key == u'name' 284 217 and self.parent_class == u'SASinstrument'): 285 218 self.current_datainfo.instrument = data_point 286 elif key == u'name' and self.parent_class == u'SASdetector': 287 self.detector.name = data_point 288 elif key == u'SDD' and self.parent_class == u'SASdetector': 289 self.detector.distance = float(data_point) 290 self.detector.distance_unit = unit 291 elif (key == u'slit_length' 292 and self.parent_class == u'SASdetector'): 293 self.detector.slit_length = float(data_point) 294 self.detector.slit_length_unit = unit 295 elif (key == u'x_position' 296 and self.parent_class == u'SASdetector'): 297 self.detector.offset.x = float(data_point) 298 self.detector.offset_unit = unit 299 elif (key == u'y_position' 300 and self.parent_class == u'SASdetector'): 301 self.detector.offset.y = float(data_point) 302 self.detector.offset_unit = unit 303 elif (key == u'pitch' 304 and self.parent_class == u'SASdetector'): 305 self.detector.orientation.x = float(data_point) 306 self.detector.orientation_unit = unit 307 elif key == u'roll' and self.parent_class == u'SASdetector': 308 self.detector.orientation.z = float(data_point) 309 self.detector.orientation_unit = unit 310 elif key == u'yaw' and self.parent_class == u'SASdetector': 311 self.detector.orientation.y = float(data_point) 312 self.detector.orientation_unit = unit 313 elif (key == u'beam_center_x' 314 and self.parent_class == u'SASdetector'): 315 self.detector.beam_center.x = float(data_point) 316 self.detector.beam_center_unit = unit 317 elif (key == u'beam_center_y' 318 and self.parent_class == u'SASdetector'): 319 self.detector.beam_center.y = float(data_point) 320 self.detector.beam_center_unit = unit 321 elif (key == u'x_pixel_size' 322 and self.parent_class == u'SASdetector'): 323 self.detector.pixel_size.x = float(data_point) 324 self.detector.pixel_size_unit = unit 325 elif (key == u'y_pixel_size' 326 and self.parent_class == u'SASdetector'): 327 self.detector.pixel_size.y = float(data_point) 328 self.detector.pixel_size_unit = unit 329 elif (key == u'distance' 330 and self.parent_class == u'SAScollimation'): 331 self.collimation.length = data_point 332 self.collimation.length_unit = unit 333 elif (key == u'name' 334 and self.parent_class == u'SAScollimation'): 335 self.collimation.name = data_point 336 elif (key == u'shape' 337 and self.parent_class == u'SASaperture'): 338 self.aperture.shape = data_point 339 elif (key == u'x_gap' 340 and self.parent_class == u'SASaperture'): 341 self.aperture.size.x = data_point 342 elif (key == u'y_gap' 343 and self.parent_class == u'SASaperture'): 344 self.aperture.size.y = data_point 345 219 # Detector 220 elif self.parent_class == u'SASdetector': 221 self.process_detector(data_point, key, unit) 222 # Collimation 223 elif self.parent_class == u'SAScollimation': 224 self.process_collimation(data_point, key, unit) 225 # Aperture 226 elif self.parent_class == u'SASaperture': 227 self.process_aperture(data_point, key) 346 228 # Process Information 347 elif (key == u'Title' 348 and self.parent_class == u'SASprocess'): # CanSAS 2.0 349 self.process.name = data_point 350 elif (key == u'name' 351 and self.parent_class == u'SASprocess'): # NXcanSAS 352 self.process.name = data_point 353 elif (key == u'description' 354 and self.parent_class == u'SASprocess'): 355 self.process.description = data_point 356 elif key == u'date' and self.parent_class == u'SASprocess': 357 self.process.date = data_point 358 elif key == u'term' and self.parent_class == u'SASprocess': 359 self.process.term = data_point 360 elif self.parent_class == u'SASprocess': 361 self.process.notes.append(data_point) 362 229 elif self.parent_class == u'SASprocess': # CanSAS 2.0 230 self.process_process(data_point, key) 363 231 # Source 364 elif (key == u'wavelength' 365 and self.parent_class == u'SASdata'): 366 self.current_datainfo.source.wavelength = data_point 367 self.current_datainfo.source.wavelength_unit = unit 368 elif (key == u'incident_wavelength' 369 and self.parent_class == 'SASsource'): 370 self.current_datainfo.source.wavelength = data_point 371 self.current_datainfo.source.wavelength_unit = unit 372 elif (key == u'wavelength_max' 373 and self.parent_class == u'SASsource'): 374 self.current_datainfo.source.wavelength_max = data_point 375 self.current_datainfo.source.wavelength_max_unit = unit 376 elif (key == u'wavelength_min' 377 and self.parent_class == u'SASsource'): 378 self.current_datainfo.source.wavelength_min = data_point 379 self.current_datainfo.source.wavelength_min_unit = unit 380 elif (key == u'incident_wavelength_spread' 381 and self.parent_class == u'SASsource'): 382 self.current_datainfo.source.wavelength_spread = \ 383 data_point 384 self.current_datainfo.source.wavelength_spread_unit = \ 385 unit 386 elif (key == u'beam_size_x' 387 and self.parent_class == u'SASsource'): 388 self.current_datainfo.source.beam_size.x = data_point 389 self.current_datainfo.source.beam_size_unit = unit 390 elif (key == u'beam_size_y' 391 and self.parent_class == u'SASsource'): 392 self.current_datainfo.source.beam_size.y = data_point 393 self.current_datainfo.source.beam_size_unit = unit 394 elif (key == u'beam_shape' 395 and self.parent_class == u'SASsource'): 396 self.current_datainfo.source.beam_shape = data_point 397 elif (key == u'radiation' 398 and self.parent_class == u'SASsource'): 399 self.current_datainfo.source.radiation = data_point 400 elif (key == u'transmission' 401 and self.parent_class == u'SASdata'): 402 self.current_datainfo.sample.transmission = data_point 403 232 elif self.parent_class == u'SASsource': 233 self.process_source(data_point, key, unit) 404 234 # Everything else goes in meta_data 235 elif self.parent_class == u'SASdata': 236 if isinstance(self.current_dataset, plottable_2D): 237 self.process_2d_data_object(data_set, key, unit) 238 else: 239 self.process_1d_data_object(data_set, key, unit) 240 241 break 242 elif self.parent_class == u'SAStransmission_spectrum': 243 self.process_trans_spectrum(data_set, key) 244 break 405 245 else: 406 246 new_key = self._create_unique_key( … … 410 250 else: 411 251 # I don't know if this reachable code 412 self.errors.add("ShouldNeverHappenException") 252 self.errors.append("ShouldNeverHappenException") 253 254 def process_1d_data_object(self, data_set, key, unit): 255 """ 256 SASdata processor method for 1d data items 257 :param data_set: data from HDF5 file 258 :param key: canSAS_class attribute 259 :param unit: unit attribute 260 """ 261 if key == self.i_name: 262 if self.multi_frame: 263 for x in range(0, data_set.shape[0]): 264 self.data_frames.append(data_set[x].flatten()) 265 else: 266 self.current_dataset.y = data_set.flatten() 267 self.current_dataset.yaxis("Intensity", unit) 268 elif key == self.i_uncertainties_name: 269 if self.multi_frame: 270 for x in range(0, data_set.shape[0]): 271 self.data_uncertainty_frames.append(data_set[x].flatten()) 272 self.current_dataset.dy = data_set.flatten() 273 elif key in self.q_names: 274 self.current_dataset.xaxis("Q", unit) 275 self.current_dataset.x = data_set.flatten() 276 elif key in self.q_resolution_names: 277 if (len(self.q_resolution_names) > 1 278 and np.where(self.q_resolution_names == key)[0] == 0): 279 self.current_dataset.dxw = data_set.flatten() 280 elif (len(self.q_resolution_names) > 1 281 and np.where(self.q_resolution_names == key)[0] == 1): 282 self.current_dataset.dxl = data_set.flatten() 283 else: 284 self.current_dataset.dx = data_set.flatten() 285 elif key in self.q_uncertainty_names: 286 if (len(self.q_uncertainty_names) > 1 287 and np.where(self.q_uncertainty_names == key)[0] == 0): 288 self.current_dataset.dxw = data_set.flatten() 289 elif (len(self.q_uncertainty_names) > 1 290 and np.where(self.q_uncertainty_names == key)[0] == 1): 291 self.current_dataset.dxl = data_set.flatten() 292 else: 293 self.current_dataset.dx = data_set.flatten() 294 elif key == self.mask_name: 295 self.current_dataset.mask = data_set.flatten() 296 elif key == u'wavelength': 297 self.current_datainfo.source.wavelength = data_set[0] 298 self.current_datainfo.source.wavelength_unit = unit 299 300 def process_2d_data_object(self, data_set, key, unit): 301 if key == self.i_name: 302 self.current_dataset.data = data_set 303 self.current_dataset.zaxis("Intensity", unit) 304 elif key == self.i_uncertainties_name: 305 self.current_dataset.err_data = data_set.flatten() 306 elif key in self.q_names: 307 self.current_dataset.xaxis("Q_x", unit) 308 self.current_dataset.yaxis("Q_y", unit) 309 if self.q_names[0] == self.q_names[1]: 310 # All q data in a single array 311 self.current_dataset.qx_data = data_set[0] 312 self.current_dataset.qy_data = data_set[1] 313 elif self.q_names.index(key) == 0: 314 self.current_dataset.qx_data = data_set 315 elif self.q_names.index(key) == 1: 316 self.current_dataset.qy_data = data_set 317 elif key in self.q_uncertainty_names or key in self.q_resolution_names: 318 if ((self.q_uncertainty_names[0] == self.q_uncertainty_names[1]) or 319 (self.q_resolution_names[0] == self.q_resolution_names[1])): 320 # All q data in a single array 321 self.current_dataset.dqx_data = data_set[0].flatten() 322 self.current_dataset.dqy_data = data_set[1].flatten() 323 elif (self.q_uncertainty_names.index(key) == 0 or 324 self.q_resolution_names.index(key) == 0): 325 self.current_dataset.dqx_data = data_set.flatten() 326 elif (self.q_uncertainty_names.index(key) == 1 or 327 self.q_resolution_names.index(key) == 1): 328 self.current_dataset.dqy_data = data_set.flatten() 329 self.current_dataset.yaxis("Q_y", unit) 330 elif key == self.mask_name: 331 self.current_dataset.mask = data_set.flatten() 332 elif key == u'Qy': 333 self.current_dataset.yaxis("Q_y", unit) 334 self.current_dataset.qy_data = data_set.flatten() 335 elif key == u'Qydev': 336 self.current_dataset.dqy_data = data_set.flatten() 337 elif key == u'Qx': 338 self.current_dataset.xaxis("Q_x", unit) 339 self.current_dataset.qx_data = data_set.flatten() 340 elif key == u'Qxdev': 341 self.current_dataset.dqx_data = data_set.flatten() 342 343 def process_trans_spectrum(self, data_set, key): 344 """ 345 SAStransmission_spectrum processor 346 :param data_set: data from HDF5 file 347 :param key: canSAS_class attribute 348 """ 349 if key == u'T': 350 self.trans_spectrum.transmission = data_set.flatten() 351 elif key == u'Tdev': 352 self.trans_spectrum.transmission_deviation = data_set.flatten() 353 elif key == u'lambda': 354 self.trans_spectrum.wavelength = data_set.flatten() 355 356 def process_sample(self, data_point, key): 357 """ 358 SASsample processor 359 :param data_point: Single point from an HDF5 data file 360 :param key: class name data_point was taken from 361 """ 362 if key == u'Title': 363 self.current_datainfo.sample.name = data_point 364 elif key == u'name': 365 self.current_datainfo.sample.name = data_point 366 elif key == u'ID': 367 self.current_datainfo.sample.name = data_point 368 elif key == u'thickness': 369 self.current_datainfo.sample.thickness = data_point 370 elif key == u'temperature': 371 self.current_datainfo.sample.temperature = data_point 372 elif key == u'transmission': 373 self.current_datainfo.sample.transmission = data_point 374 elif key == u'x_position': 375 self.current_datainfo.sample.position.x = data_point 376 elif key == u'y_position': 377 self.current_datainfo.sample.position.y = data_point 378 elif key == u'pitch': 379 self.current_datainfo.sample.orientation.x = data_point 380 elif key == u'yaw': 381 self.current_datainfo.sample.orientation.y = data_point 382 elif key == u'roll': 383 self.current_datainfo.sample.orientation.z = data_point 384 elif key == u'details': 385 self.current_datainfo.sample.details.append(data_point) 386 387 def process_detector(self, data_point, key, unit): 388 """ 389 SASdetector processor 390 :param data_point: Single point from an HDF5 data file 391 :param key: class name data_point was taken from 392 :param unit: unit attribute from data set 393 """ 394 if key == u'name': 395 self.detector.name = data_point 396 elif key == u'SDD': 397 self.detector.distance = float(data_point) 398 self.detector.distance_unit = unit 399 elif key == u'slit_length': 400 self.detector.slit_length = float(data_point) 401 self.detector.slit_length_unit = unit 402 elif key == u'x_position': 403 self.detector.offset.x = float(data_point) 404 self.detector.offset_unit = unit 405 elif key == u'y_position': 406 self.detector.offset.y = float(data_point) 407 self.detector.offset_unit = unit 408 elif key == u'pitch': 409 self.detector.orientation.x = float(data_point) 410 self.detector.orientation_unit = unit 411 elif key == u'roll': 412 self.detector.orientation.z = float(data_point) 413 self.detector.orientation_unit = unit 414 elif key == u'yaw': 415 self.detector.orientation.y = float(data_point) 416 self.detector.orientation_unit = unit 417 elif key == u'beam_center_x': 418 self.detector.beam_center.x = float(data_point) 419 self.detector.beam_center_unit = unit 420 elif key == u'beam_center_y': 421 self.detector.beam_center.y = float(data_point) 422 self.detector.beam_center_unit = unit 423 elif key == u'x_pixel_size': 424 self.detector.pixel_size.x = float(data_point) 425 self.detector.pixel_size_unit = unit 426 elif key == u'y_pixel_size': 427 self.detector.pixel_size.y = float(data_point) 428 self.detector.pixel_size_unit = unit 429 430 def process_collimation(self, data_point, key, unit): 431 """ 432 SAScollimation processor 433 :param data_point: Single point from an HDF5 data file 434 :param key: class name data_point was taken from 435 :param unit: unit attribute from data set 436 """ 437 if key == u'distance': 438 self.collimation.length = data_point 439 self.collimation.length_unit = unit 440 elif key == u'name': 441 self.collimation.name = data_point 442 443 def process_aperture(self, data_point, key): 444 """ 445 SASaperture processor 446 :param data_point: Single point from an HDF5 data file 447 :param key: class name data_point was taken from 448 """ 449 if key == u'shape': 450 self.aperture.shape = data_point 451 elif key == u'x_gap': 452 self.aperture.size.x = data_point 453 elif key == u'y_gap': 454 self.aperture.size.y = data_point 455 456 def process_source(self, data_point, key, unit): 457 """ 458 SASsource processor 459 :param data_point: Single point from an HDF5 data file 460 :param key: class name data_point was taken from 461 :param unit: unit attribute from data set 462 """ 463 if key == u'incident_wavelength': 464 self.current_datainfo.source.wavelength = data_point 465 self.current_datainfo.source.wavelength_unit = unit 466 elif key == u'wavelength_max': 467 self.current_datainfo.source.wavelength_max = data_point 468 self.current_datainfo.source.wavelength_max_unit = unit 469 elif key == u'wavelength_min': 470 self.current_datainfo.source.wavelength_min = data_point 471 self.current_datainfo.source.wavelength_min_unit = unit 472 elif key == u'incident_wavelength_spread': 473 self.current_datainfo.source.wavelength_spread = data_point 474 self.current_datainfo.source.wavelength_spread_unit = unit 475 elif key == u'beam_size_x': 476 self.current_datainfo.source.beam_size.x = data_point 477 self.current_datainfo.source.beam_size_unit = unit 478 elif key == u'beam_size_y': 479 self.current_datainfo.source.beam_size.y = data_point 480 self.current_datainfo.source.beam_size_unit = unit 481 elif key == u'beam_shape': 482 self.current_datainfo.source.beam_shape = data_point 483 elif key == u'radiation': 484 self.current_datainfo.source.radiation = data_point 485 486 def process_process(self, data_point, key): 487 """ 488 SASprocess processor 489 :param data_point: Single point from an HDF5 data file 490 :param key: class name data_point was taken from 491 """ 492 term_match = re.compile(u'^term[0-9]+$') 493 if key == u'Title': # CanSAS 2.0 494 self.process.name = data_point 495 elif key == u'name': # NXcanSAS 496 self.process.name = data_point 497 elif key == u'description': 498 self.process.description = data_point 499 elif key == u'date': 500 self.process.date = data_point 501 elif term_match.match(key): 502 self.process.term.append(data_point) 503 else: 504 self.process.notes.append(data_point) 413 505 414 506 def add_intermediate(self): … … 440 532 self.data2d.append(self.current_dataset) 441 533 elif isinstance(self.current_dataset, plottable_1D): 442 self.data1d.append(self.current_dataset) 534 if self.multi_frame: 535 for x in range(0, len(self.data_frames)): 536 self.current_dataset.y = self.data_frames[x] 537 if len(self.data_uncertainty_frames) > x: 538 self.current_dataset.dy = \ 539 self.data_uncertainty_frames[x] 540 self.data1d.append(self.current_dataset) 541 else: 542 self.data1d.append(self.current_dataset) 443 543 444 544 def final_data_cleanup(self): … … 452 552 spectrum_list = [] 453 553 for spectrum in self.current_datainfo.trans_spectrum: 454 spectrum.transmission = np.delete(spectrum.transmission, [0])455 554 spectrum.transmission = spectrum.transmission.astype(np.float64) 456 spectrum.transmission_deviation = np.delete(457 spectrum.transmission_deviation, [0])458 555 spectrum.transmission_deviation = \ 459 556 spectrum.transmission_deviation.astype(np.float64) 460 spectrum.wavelength = np.delete(spectrum.wavelength, [0])461 557 spectrum.wavelength = spectrum.wavelength.astype(np.float64) 462 558 if len(spectrum.transmission) > 0: … … 466 562 # Append errors to dataset and reset class errors 467 563 self.current_datainfo.errors = self.errors 468 self.errors .clear()564 self.errors = [] 469 565 470 566 # Combine all plottables with datainfo and append each to output 471 567 # Type cast data arrays to float64 and find min/max as appropriate 472 568 for dataset in self.data2d: 473 zeros = np.ones(dataset.data.size, dtype=bool)474 try:475 for i in range(0, dataset.mask.size - 1):476 zeros[i] = dataset.mask[i]477 except:478 self.errors.add(sys.exc_value)479 dataset.mask = zeros480 569 # Calculate the actual Q matrix 481 570 try: … … 484 573 * dataset.qx_data 485 574 + dataset.qy_data 486 * dataset.qy_data) 575 * dataset.qy_data).flatten() 487 576 except: 488 577 dataset.q_data = None 489 578 490 579 if dataset.data.ndim == 2: 491 (n_rows, n_cols) = dataset.data.shape 492 dataset.y_bins = dataset.qy_data[0::n_cols] 493 dataset.x_bins = dataset.qx_data[:n_cols] 580 dataset.y_bins = np.unique(dataset.qy_data.flatten()) 581 dataset.x_bins = np.unique(dataset.qx_data.flatten()) 494 582 dataset.data = dataset.data.flatten() 583 dataset.qx_data = dataset.qx_data.flatten() 584 dataset.qy_data = dataset.qy_data.flatten() 585 586 try: 587 iter(dataset.mask) 588 dataset.mask = np.invert(np.asarray(dataset.mask, dtype=bool)) 589 except TypeError: 590 dataset.mask = np.ones(dataset.data.shape, dtype=bool) 495 591 self.current_dataset = dataset 496 592 self.send_to_output() … … 511 607 if self.current_datainfo and self.current_dataset: 512 608 self.final_data_cleanup() 609 self.data_frames = [] 610 self.data_uncertainty_frames = [] 513 611 self.data1d = [] 514 612 self.data2d = [] 515 613 self.current_datainfo = DataInfo() 516 614 517 518 def _initialize_new_data_set(self, parent_list=None): 615 def _initialize_new_data_set(self, value=None): 519 616 """ 520 617 A private class method to generate a new 1D or 2D data object based on … … 524 621 :param parent_list: List of names of parent elements 525 622 """ 526 527 if parent_list is None: 528 parent_list = [] 529 if self._find_intermediate(parent_list, "Qx"): 623 if self._is_2d_not_multi_frame(value): 530 624 self.current_dataset = plottable_2D() 531 625 else: … … 535 629 self.current_datainfo.filename = self.raw_data.filename 536 630 537 def _find_intermediate(self, parent_list, basename=""): 538 """ 539 A private class used to find an entry by either using a direct key or 540 knowing the approximate basename. 541 542 :param parent_list: List of parents nodes in the HDF5 file 631 @staticmethod 632 def as_list_or_array(iterable): 633 """ 634 Return value as a list if not already a list or array. 635 :param iterable: 636 :return: 637 """ 638 if not (isinstance(iterable, np.ndarray) or isinstance(iterable, list)): 639 iterable = iterable.split(",") if isinstance(iterable, basestring)\ 640 else [iterable] 641 return iterable 642 643 def _find_data_attributes(self, value): 644 """ 645 A class to find the indices for Q, the name of the Qdev and Idev, and 646 the name of the mask. 647 :param value: SASdata/NXdata HDF5 Group 648 """ 649 # Initialize values to base types 650 self.mask_name = u'' 651 self.i_name = u'' 652 self.i_node = u'' 653 self.i_uncertainties_name = u'' 654 self.q_names = [] 655 self.q_uncertainty_names = [] 656 self.q_resolution_names = [] 657 # Get attributes 658 attrs = value.attrs 659 signal = attrs.get("signal", "I") 660 i_axes = attrs.get("I_axes", ["Q"]) 661 q_indices = attrs.get("Q_indices", [0]) 662 i_axes = self.as_list_or_array(i_axes) 663 keys = value.keys() 664 # Assign attributes to appropriate class variables 665 self.q_names = [i_axes[int(v)] for v in self.as_list_or_array(q_indices)] 666 self.mask_name = attrs.get("mask") 667 self.i_name = signal 668 self.i_node = value.get(self.i_name) 669 for item in self.q_names: 670 if item in keys: 671 q_vals = value.get(item) 672 if q_vals.attrs.get("uncertainties") is not None: 673 self.q_uncertainty_names = q_vals.attrs.get("uncertainties") 674 elif q_vals.attrs.get("uncertainty") is not None: 675 self.q_uncertainty_names = q_vals.attrs.get("uncertainty") 676 if isinstance(self.q_uncertainty_names, basestring): 677 self.q_uncertainty_names = self.q_uncertainty_names.split(",") 678 if q_vals.attrs.get("resolutions") is not None: 679 self.q_resolution_names = q_vals.attrs.get("resolutions") 680 if isinstance(self.q_resolution_names, basestring): 681 self.q_resolution_names = self.q_resolution_names.split(",") 682 if self.i_name in keys: 683 i_vals = value.get(self.i_name) 684 self.i_uncertainties_name = i_vals.attrs.get("uncertainties") 685 if self.i_uncertainties_name is None: 686 self.i_uncertainties_name = i_vals.attrs.get("uncertainty") 687 688 def _is_2d_not_multi_frame(self, value, i_base="", q_base=""): 689 """ 690 A private class to determine if the data set is 1d or 2d. 691 692 :param value: Nexus/NXcanSAS data group 543 693 :param basename: Approximate name of an entry to search for 544 :return: 545 """ 546 547 entry = False 548 key_prog = re.compile(basename) 549 top = self.raw_data 550 for parent in parent_list: 551 top = top.get(parent) 552 for key in top.keys(): 553 if key_prog.match(key): 554 entry = True 555 break 556 return entry 694 :return: True if 2D, otherwise false 695 """ 696 i_basename = i_base if i_base != "" else self.i_name 697 i_vals = value.get(i_basename) 698 q_basename = q_base if q_base != "" else self.q_names 699 q_vals = value.get(q_basename[0]) 700 self.multi_frame = (i_vals is not None and q_vals is not None 701 and len(i_vals.shape) != 1 702 and len(q_vals.shape) == 1) 703 return (i_vals is not None and len(i_vals.shape) != 1 704 and not self.multi_frame) 557 705 558 706 def _create_unique_key(self, dictionary, name, numb=0): … … 583 731 if unit is None: 584 732 unit = h5attr(value, u'unit') 585 # Convert the unit formats586 if unit == "1/A":587 unit = "A^{-1}"588 elif unit == "1/cm":589 unit = "cm^{-1}"590 733 return unit -
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 -
src/sas/sascalc/file_converter/nxcansas_writer.py
r574adc7 r109afbd 8 8 import os 9 9 10 from sas.sascalc.dataloader.readers.cansas_reader_HDF5 import Reader as Cansas2Reader10 from sas.sascalc.dataloader.readers.cansas_reader_HDF5 import Reader 11 11 from sas.sascalc.dataloader.data_info import Data1D, Data2D 12 12 13 class NXcanSASWriter( Cansas2Reader):13 class NXcanSASWriter(Reader): 14 14 """ 15 15 A class for writing in NXcanSAS data files. Any number of data sets may be … … 87 87 entry[names[2]].attrs['units'] = units 88 88 89 valid_data = all([is subclass(d.__class__, (Data1D, Data2D)) for d in dataset])89 valid_data = all([isinstance(d, (Data1D, Data2D)) for d in dataset]) 90 90 if not valid_data: 91 raise ValueError("All entries of dataset must be Data1D or Data2D objects") 91 raise ValueError("All entries of dataset must be Data1D or Data2D" 92 "objects") 92 93 93 94 # Get run name and number from first Data object … … 109 110 sasentry.attrs['version'] = '1.0' 110 111 111 i = 1 112 113 for data_obj in dataset: 114 data_entry = sasentry.create_group("sasdata{0:0=2d}".format(i)) 112 for i, data_obj in enumerate(dataset): 113 data_entry = sasentry.create_group("sasdata{0:0=2d}".format(i+1)) 115 114 data_entry.attrs['canSAS_class'] = 'SASdata' 116 115 if isinstance(data_obj, Data1D): … … 118 117 elif isinstance(data_obj, Data2D): 119 118 self._write_2d_data(data_obj, data_entry) 120 i += 1121 119 122 120 data_info = dataset[0] … … 148 146 sample_entry.create_dataset('details', data=details) 149 147 150 # Instrum ment metadata148 # Instrument metadata 151 149 instrument_entry = sasentry.create_group('sasinstrument') 152 150 instrument_entry.attrs['canSAS_class'] = 'SASinstrument' … … 176 174 units=data_info.source.beam_size_unit, write_fn=_write_h5_float) 177 175 178 179 176 # Collimation metadata 180 177 if len(data_info.collimation) > 0: 181 i = 1 182 for coll_info in data_info.collimation: 178 for i, coll_info in enumerate(data_info.collimation): 183 179 collimation_entry = instrument_entry.create_group( 184 'sascollimation{0:0=2d}'.format(i ))180 'sascollimation{0:0=2d}'.format(i + 1)) 185 181 collimation_entry.attrs['canSAS_class'] = 'SAScollimation' 186 182 if coll_info.length is not None: 187 183 _write_h5_float(collimation_entry, coll_info.length, 'SDD') 188 collimation_entry['SDD'].attrs['units'] = coll_info.length_unit 184 collimation_entry['SDD'].attrs['units'] =\ 185 coll_info.length_unit 189 186 if coll_info.name is not None: 190 187 collimation_entry['name'] = _h5_string(coll_info.name) 191 188 else: 192 # Create a blank one - at least 1 set of collimation metadata 193 # required by format 194 collimation_entry = instrument_entry.create_group('sascollimation01') 189 # Create a blank one - at least 1 collimation required by format 190 instrument_entry.create_group('sascollimation01') 195 191 196 192 # Detector metadata 197 193 if len(data_info.detector) > 0: 198 194 i = 1 199 for det_info in data_info.detector:195 for i, det_info in enumerate(data_info.detector): 200 196 detector_entry = instrument_entry.create_group( 201 'sasdetector{0:0=2d}'.format(i ))197 'sasdetector{0:0=2d}'.format(i + 1)) 202 198 detector_entry.attrs['canSAS_class'] = 'SASdetector' 203 199 if det_info.distance is not None: 204 200 _write_h5_float(detector_entry, det_info.distance, 'SDD') 205 detector_entry['SDD'].attrs['units'] = det_info.distance_unit 201 detector_entry['SDD'].attrs['units'] =\ 202 det_info.distance_unit 206 203 if det_info.name is not None: 207 204 detector_entry['name'] = _h5_string(det_info.name) … … 209 206 detector_entry['name'] = _h5_string('') 210 207 if det_info.slit_length is not None: 211 _write_h5_float(detector_entry, det_info.slit_length, 'slit_length') 212 detector_entry['slit_length'].attrs['units'] = det_info.slit_length_unit 208 _write_h5_float(detector_entry, det_info.slit_length, 209 'slit_length') 210 detector_entry['slit_length'].attrs['units'] =\ 211 det_info.slit_length_unit 213 212 _write_h5_vector(detector_entry, det_info.offset) 214 213 # NXcanSAS doesn't save information about pitch, only roll … … 224 223 names=['x_pixel_size', 'y_pixel_size'], 225 224 write_fn=_write_h5_float, units=det_info.pixel_size_unit) 226 227 i += 1228 225 else: 229 226 # Create a blank one - at least 1 detector required by format … … 231 228 detector_entry.attrs['canSAS_class'] = 'SASdetector' 232 229 detector_entry.attrs['name'] = '' 230 231 # Process meta data 232 for i, process in enumerate(data_info.process): 233 process_entry = sasentry.create_group('sasprocess{0:0=2d}'.format( 234 i + 1)) 235 process_entry.attrs['canSAS_class'] = 'SASprocess' 236 if process.name: 237 name = _h5_string(process.name) 238 process_entry.create_dataset('name', data=name) 239 if process.date: 240 date = _h5_string(process.date) 241 process_entry.create_dataset('date', data=date) 242 if process.description: 243 desc = _h5_string(process.description) 244 process_entry.create_dataset('description', data=desc) 245 for j, term in enumerate(process.term): 246 # Don't save empty terms 247 if term: 248 h5_term = _h5_string(term) 249 process_entry.create_dataset('term{0:0=2d}'.format( 250 j + 1), data=h5_term) 251 for j, note in enumerate(process.notes): 252 # Don't save empty notes 253 if note: 254 h5_note = _h5_string(note) 255 process_entry.create_dataset('note{0:0=2d}'.format( 256 j + 1), data=h5_note) 257 258 # Transmission Spectrum 259 for i, trans in enumerate(data_info.trans_spectrum): 260 trans_entry = sasentry.create_group( 261 'sastransmission_spectrum{0:0=2d}'.format(i + 1)) 262 trans_entry.attrs['canSAS_class'] = 'SAStransmission_spectrum' 263 trans_entry.attrs['signal'] = 'T' 264 trans_entry.attrs['T_axes'] = 'T' 265 trans_entry.attrs['name'] = trans.name 266 if trans.timestamp is not '': 267 trans_entry.attrs['timestamp'] = trans.timestamp 268 transmission = trans_entry.create_dataset('T', 269 data=trans.transmission) 270 transmission.attrs['unertainties'] = 'Tdev' 271 trans_entry.create_dataset('Tdev', 272 data=trans.transmission_deviation) 273 trans_entry.create_dataset('lambda', data=trans.wavelength) 233 274 234 275 note_entry = sasentry.create_group('sasnote'.format(i)) … … 254 295 data_entry.attrs['signal'] = 'I' 255 296 data_entry.attrs['I_axes'] = 'Q' 256 data_entry.attrs['I_uncertainties'] = 'Idev' 257 data_entry.attrs['Q_indicies'] = 0 258 259 dI = data_obj.dy 260 if dI is None: 261 dI = np.zeros((data_obj.y.shape)) 262 263 data_entry.create_dataset('Q', data=data_obj.x) 264 data_entry.create_dataset('I', data=data_obj.y) 265 data_entry.create_dataset('Idev', data=dI) 297 data_entry.attrs['Q_indices'] = [0] 298 q_entry = data_entry.create_dataset('Q', data=data_obj.x) 299 q_entry.attrs['units'] = data_obj.x_unit 300 i_entry = data_entry.create_dataset('I', data=data_obj.y) 301 i_entry.attrs['units'] = data_obj.y_unit 302 if data_obj.dy is not None: 303 i_entry.attrs['uncertainties'] = 'Idev' 304 i_dev_entry = data_entry.create_dataset('Idev', data=data_obj.dy) 305 i_dev_entry.attrs['units'] = data_obj.y_unit 306 if data_obj.dx is not None: 307 q_entry.attrs['resolutions'] = 'dQ' 308 dq_entry = data_entry.create_dataset('dQ', data=data_obj.dx) 309 dq_entry.attrs['units'] = data_obj.x_unit 310 elif data_obj.dxl is not None: 311 q_entry.attrs['resolutions'] = ['dQl','dQw'] 312 dql_entry = data_entry.create_dataset('dQl', data=data_obj.dxl) 313 dql_entry.attrs['units'] = data_obj.x_unit 314 dqw_entry = data_entry.create_dataset('dQw', data=data_obj.dxw) 315 dqw_entry.attrs['units'] = data_obj.x_unit 266 316 267 317 def _write_2d_data(self, data, data_entry): … … 273 323 """ 274 324 data_entry.attrs['signal'] = 'I' 275 data_entry.attrs['I_axes'] = 'Q,Q' 276 data_entry.attrs['I_uncertainties'] = 'Idev' 277 data_entry.attrs['Q_indicies'] = [0,1] 325 data_entry.attrs['I_axes'] = 'Qx,Qy' 326 data_entry.attrs['Q_indices'] = [0,1] 278 327 279 328 (n_rows, n_cols) = (len(data.y_bins), len(data.x_bins)) 280 329 281 if n_rows == 0 and n_cols == 0:330 if (n_rows == 0 and n_cols == 0) or (n_cols*n_rows != data.data.size): 282 331 # Calculate rows and columns, assuming detector is square 283 332 # Same logic as used in PlotPanel.py _get_bins … … 288 337 raise ValueError("Unable to calculate dimensions of 2D data") 289 338 290 I = np.reshape(data.data, (n_rows, n_cols)) 291 dI = np.zeros((n_rows, n_cols)) 292 if not all(data.err_data == [None]): 293 dI = np.reshape(data.err_data, (n_rows, n_cols)) 294 qx = np.reshape(data.qx_data, (n_rows, n_cols)) 339 intensity = np.reshape(data.data, (n_rows, n_cols)) 340 qx = np.reshape(data.qx_data, (n_rows, n_cols)) 295 341 qy = np.reshape(data.qy_data, (n_rows, n_cols)) 296 342 297 I_entry = data_entry.create_dataset('I', data=I) 298 I_entry.attrs['units'] = data.I_unit 299 Qx_entry = data_entry.create_dataset('Qx', data=qx) 300 Qx_entry.attrs['units'] = data.Q_unit 301 Qy_entry = data_entry.create_dataset('Qy', data=qy) 302 Qy_entry.attrs['units'] = data.Q_unit 303 Idev_entry = data_entry.create_dataset('Idev', data=dI) 304 Idev_entry.attrs['units'] = data.I_unit 343 i_entry = data_entry.create_dataset('I', data=intensity) 344 i_entry.attrs['units'] = data.I_unit 345 qx_entry = data_entry.create_dataset('Qx', data=qx) 346 qx_entry.attrs['units'] = data.Q_unit 347 qy_entry = data_entry.create_dataset('Qy', data=qy) 348 qy_entry.attrs['units'] = data.Q_unit 349 if (data.err_data is not None 350 and not all(v is None for v in data.err_data)): 351 d_i = np.reshape(data.err_data, (n_rows, n_cols)) 352 i_entry.attrs['uncertainties'] = 'Idev' 353 i_dev_entry = data_entry.create_dataset('Idev', data=d_i) 354 i_dev_entry.attrs['units'] = data.I_unit 355 if (data.dqx_data is not None 356 and not all(v is None for v in data.dqx_data)): 357 qx_entry.attrs['resolutions'] = 'dQx' 358 dqx_entry = data_entry.create_dataset('dQx', data=data.dqx_data) 359 dqx_entry.attrs['units'] = data.Q_unit 360 if (data.dqy_data is not None 361 and not all(v is None for v in data.dqy_data)): 362 qy_entry.attrs['resolutions'] = 'dQy' 363 dqy_entry = data_entry.create_dataset('dQy', data=data.dqy_data) 364 dqy_entry.attrs['units'] = data.Q_unit 365 if data.mask is not None and not all(v is None for v in data.mask): 366 data_entry.attrs['mask'] = "mask" 367 mask = np.invert(np.asarray(data.mask, dtype=bool)) 368 data_entry.create_dataset('mask', data=mask) -
src/sas/sasgui/guiframe/gui_manager.py
r47be5a1 rdcd6efd 46 46 from sas.sasgui.guiframe.CategoryManager import CategoryManager 47 47 from sas.sascalc.dataloader.loader import Loader 48 from sas.sascalc.file_converter.nxcansas_writer import NXcanSASWriter 48 49 from sas.sasgui.guiframe.proxy import Connection 49 50 … … 2407 2408 default_name = fname 2408 2409 wildcard = "Text files (*.txt)|*.txt|"\ 2409 "CanSAS 1D files(*.xml)|*.xml" 2410 path = None 2410 "CanSAS 1D files (*.xml)|*.xml|"\ 2411 "NXcanSAS files (*.h5)|*.h5|" 2412 options = [".txt", ".xml",".h5"] 2411 2413 dlg = wx.FileDialog(self, "Choose a file", 2412 2414 self._default_save_location, … … 2418 2420 # This is MAC Fix 2419 2421 ext_num = dlg.GetFilterIndex() 2420 if ext_num == 0: 2421 ext_format = '.txt' 2422 else: 2423 ext_format = '.xml' 2422 2423 ext_format = options[ext_num] 2424 2424 path = os.path.splitext(path)[0] + ext_format 2425 2425 mypath = os.path.basename(path) 2426 2427 # Instantiate a loader 2428 loader = Loader() 2429 ext_format = ".txt" 2430 if os.path.splitext(mypath)[1].lower() == ext_format: 2426 fName = os.path.splitext(path)[0] + ext_format 2427 2428 if os.path.splitext(mypath)[1].lower() == options[0]: 2431 2429 # Make sure the ext included in the file name 2432 2430 # especially on MAC 2433 fName = os.path.splitext(path)[0] + ext_format2434 2431 self._onsaveTXT(data, fName) 2435 ext_format = ".xml" 2436 if os.path.splitext(mypath)[1].lower() == ext_format: 2432 elif os.path.splitext(mypath)[1].lower() == options[1]: 2437 2433 # Make sure the ext included in the file name 2438 2434 # especially on MAC 2439 fName = os.path.splitext(path)[0] + ext_format 2435 # Instantiate a loader 2436 loader = Loader() 2440 2437 loader.save(fName, data, ext_format) 2438 elif os.path.splitext(mypath)[1].lower() == options[2]: 2439 nxcansaswriter = NXcanSASWriter() 2440 nxcansaswriter.write([data], fName) 2441 2441 try: 2442 2442 self._default_save_location = os.path.dirname(path) … … 2465 2465 if has_errors: 2466 2466 if data.dx is not None and data.dx != []: 2467 out.write("<X> <Y> <dY><dX>\n")2467 out.write("<X>\t<Y>\t<dY>\t<dX>\n") 2468 2468 else: 2469 out.write("<X> <Y><dY>\n")2469 out.write("<X>\t<Y>\t<dY>\n") 2470 2470 else: 2471 out.write("<X> 2471 out.write("<X>\t<Y>\n") 2472 2472 2473 2473 for i in range(len(data.x)): … … 2513 2513 text += 'dY_min = %s: dY_max = %s\n' % (min(data.dy), max(data.dy)) 2514 2514 text += '\nData Points:\n' 2515 x_st = "X" 2515 text += "<index> \t<X> \t<Y> \t<dY> " 2516 text += "\t<dXl> \t<dXw>\n" if(data.dxl is not None and 2517 data.dxw is not None) else "\t<dX>\n" 2516 2518 for index in range(len(data.x)): 2517 2519 if data.dy is not None and len(data.dy) > index: … … 2524 2526 dx_val = 0.0 2525 2527 if data.dxl is not None and len(data.dxl) > index: 2526 if index == 0:2527 x_st = "Xl"2528 2528 dx_val = data.dxl[index] 2529 elif data.dxw is not None and len(data.dxw) > index: 2530 if index == 0: 2531 x_st = "Xw" 2532 dx_val = data.dxw[index] 2533 2534 if index == 0: 2535 text += "<index> \t<X> \t<Y> \t<dY> \t<d%s>\n" % x_st 2529 if data.dxw is not None and len(data.dxw) > index: 2530 dx_val = "%s \t%s" % (data.dxl[index], data.dxw[index]) 2531 2536 2532 text += "%s \t%s \t%s \t%s \t%s\n" % (index, 2537 2533 data.x[index], … … 2550 2546 """ 2551 2547 default_name = fname 2552 wildcard = "IGOR/DAT 2D file in Q_map (*.dat)|*.DAT" 2548 wildcard = "IGOR/DAT 2D file in Q_map (*.dat)|*.DAT|"\ 2549 "NXcanSAS files (*.h5)|*.h5|" 2553 2550 dlg = wx.FileDialog(self, "Choose a file", 2554 2551 self._default_save_location, … … 2562 2559 if ext_num == 0: 2563 2560 ext_format = '.dat' 2561 elif ext_num == 1: 2562 ext_format = '.h5' 2564 2563 else: 2565 2564 ext_format = '' … … 2569 2568 # Instantiate a loader 2570 2569 loader = Loader() 2571 2572 ext_format = ".dat" 2573 if os.path.splitext(mypath)[1].lower() == ext_format: 2570 ext = os.path.splitext(mypath)[1].lower() 2571 if ext == '.dat': 2574 2572 # Make sure the ext included in the file name 2575 2573 # especially on MAC 2576 2574 fileName = os.path.splitext(path)[0] + ext_format 2577 2575 loader.save(fileName, data, ext_format) 2576 elif ext == '.h5': 2577 # Make sure the ext included in the file name 2578 # especially on MAC 2579 fileName = os.path.splitext(path)[0] + ext_format 2580 nxcansaswriter = NXcanSASWriter() 2581 nxcansaswriter.write([data], fileName) 2578 2582 try: 2579 2583 self._default_save_location = os.path.dirname(path) -
src/sas/sasgui/guiframe/local_perspectives/data_loader/data_loader.py
r9c7e2b8 r5218180 205 205 except NoKnownLoaderException as e: 206 206 exception_occurred = True 207 error_message = "Loading data failed! " + e.message207 error_message = "Loading data failed!\n" + e.message 208 208 file_errors[basename] = [error_message] 209 209 except Exception as e: … … 224 224 for message in error_array: 225 225 error_message += message + "\n" 226 error_message = error_message[:-1] 226 227 self.load_complete(output=output, 227 228 message=error_message, … … 231 232 self.load_complete(output=output, message="Loading data complete!", 232 233 info="info") 233 else:234 self.load_complete(output=None, message=error_message, info="error")235 234 236 235 def load_update(self, message="", info="warning"): -
test/sasdataloader/test/test_data/avg_testdata.txt
r8c9ffde r7fd5e2a 1 1 0.00019987186878 -0.01196215 0.148605728355 2 0.000453772721237 0.02091606 0. 06802830293343 0.000750492390439 -0.01337855 0. 04449029107574 0.00103996394336 0.03062 0. 05803128945285 0.0013420198959 0.0811008333333 0. 05404692891086 0.001652061869 0.167022288372 0. 06518913200317 0.00196086470492 27.5554711176 0.7350533 009572 0.000453772721237 0.02091606 0.23372601 3 0.000750492390439 -0.01337855 0.17169562 4 0.00103996394336 0.03062 0.13136407 5 0.0013420198959 0.0811008333333 0.10681163 6 0.001652061869 0.167022288372 0.10098903 7 0.00196086470492 27.5554711176 0.7350533 8 8 0.00226262401224 105.031578947 1.35744586624 9 9 0.00256734439716 82.1791776119 1.10749938588 -
test/sasdataloader/test/test_data/ring_testdata.txt
r400155b r7fd5e2a 4 4 0.628318530718 0.964040908176 0.0790933208542 5 5 0.942477796077 0.922142905769 0.0781616076625 6 1.25663706144 1.02710537736 0.08 08758975386 1.25663706144 1.02710537736 0.08136351514804 7 7 1.57079632679 1.01448978075 0.0808313893873 8 8 1.88495559215 1.04677136013 0.0828850195035 -
test/sasdataloader/test/test_data/sectorphi_testdata.txt
r8c9ffde r7fd5e2a 13 13 0.981747704247 0.893411561538 0.151685984204 14 14 1.06028752059 0.86231787 0.152618707077 15 1.13882733693 1.0607364925 0.16 427615031615 1.13882733693 1.0607364925 0.166167546658 16 16 1.21736715327 1.0684421475 0.163649496829 17 1.29590696961 1.09330437436 0.16 787164526317 1.29590696961 1.09330437436 0.16981858402 18 18 1.37444678595 0.88759347 0.150974201439 19 19 1.45298660229 1.1352002 0.172191803977 -
test/sasdataloader/test/test_data/sectorq_testdata.txt
r8c9ffde r7fd5e2a 17 17 0.00913119845523 0.405669568421 0.0705339106673 18 18 0.00938052380065 0.331241946 0.061307573431 19 0.00962825731078 0.237315993939 0.05 7865476989319 0.00962825731078 0.237315993939 0.059602636160850493 20 20 0.00987552050718 0.296916590385 0.0592796733987 -
test/sasdataloader/test/test_data/slabx_testdata.txt
r8c9ffde r7fd5e2a 21 21 -0.00184475260646 2.40154 1.09579651396 22 22 -0.00143541414791 0.065281 0.198049867458 23 -0.00102607559383 -0.04767235 0. 15438968553624 -0.000616736954402 -0.0090503 0. 096010546295723 -0.00102607559383 -0.04767235 0.52329358394690839 24 -0.000616736954402 -0.0090503 0.36635778277525377 25 25 -0.000207398273925 0.03109325 0.246629023029 26 0.000201940423805 -0.027508775 0. 08292884751426 0.000201940423805 -0.027508775 0.36314899662535211 27 27 0.000611279108096 0.03251315 0.246951260373 28 0.00102061774154 -0.00987975 0. 14423353458929 0.00142995630705 0.075937 0. 1948550743528 0.00102061774154 -0.00987975 0.38184199939241886 29 0.00142995630705 0.075937 0.53662696540520582 30 30 0.00183929475361 10.60918375 1.62858709853 31 31 0.00224863307777 106.2485 7.2886384188 -
test/sasdataloader/test/test_data/slaby_testdata.txt
r8c9ffde r7fd5e2a 1 -0.00981587154747 0.197046827778 0.0 8722263092611 -0.00981587154747 0.197046827778 0.09153902 2 2 -0.00940654133769 0.2466434 0.124972263589 3 3 -0.0089972103454 0.218745969444 0.0838510368061 4 4 -0.00858787875434 0.126093522222 0.107482002513 5 -0.00817854644886 0.310427366667 0.10 09452898525 -0.00817854644886 0.310427366667 0.10469745 6 6 -0.0077692135991 0.0843802722222 0.103942898914 7 7 -0.00735988010303 0.246036369444 0.0916479235889 -
test/sasdataloader/test/utest_abs_reader.py
rbd5c3b1 rd96744de 77 77 78 78 def test_usans_negative_dxl(self): 79 data_abs = Loader().load(find("sam14_cor.ABS")) 80 data_cor = Loader().load(find("sam14_cor.cor")) 81 for i in range(0, len(data_abs) - 1): 82 self.assertEquals(data_abs.x[i], data_cor.x[i]) 83 self.assertEquals(data_abs.y[i], data_cor.y[i]) 84 self.assertEquals(data_abs.dxl[i], data_cor.dxl[i]) 85 self.assertEquals(data_abs.dxw[i], data_cor.dxw[i]) 86 self.assertTrue(data_abs.dxl > 0) 79 data_abs = Loader().load(find("sam14_cor.ABS"))[0] 80 data_cor = Loader().load(find("sam14_cor.txt"))[0] 81 for i in range(0, len(data_abs.x) - 1): 82 self.assertEqual(data_abs.x[i], data_cor.x[i]) 83 self.assertEqual(data_abs.y[i], data_cor.y[i]) 84 self.assertEqual(data_abs.dxl[i], -data_cor.dx[i]) 85 self.assertTrue(data_abs.dxl[i] > 0) 87 86 88 87 … … 118 117 self.assertEqual(self.data.detector[0].beam_center.y, center_y) 119 118 120 self.assertEqual(self.data.I_unit, ' 1/cm')119 self.assertEqual(self.data.I_unit, 'cm^{-1}') 121 120 self.assertEqual(self.data.data[0], 1.57831) 122 121 self.assertEqual(self.data.data[1], 2.70983) -
test/sasdataloader/test/utest_ascii.py
rdb5196d r9fb4572 10 10 import unittest 11 11 from sas.sascalc.dataloader.loader import Loader 12 from sas.sascalc.dataloader.data_info import Data2D 12 13 13 14 … … 121 122 self.assertFalse(math.isnan(f_1d.y[i])) 122 123 self.assertFalse(math.isnan(f_1d.dy[i])) 124 self.assertTrue(isinstance(f_2d, Data2D)) 123 125 f_2d.data = f_2d.data.flatten() 124 126 f_2d.qx_data = f_2d.qx_data.flatten() -
test/sasdataloader/test/utest_averaging.py
rf53d684 rf4e2f22 106 106 107 107 def setUp(self): 108 filepath = find(' MAR07232_rest.h5')108 filepath = find('test_data' + os.sep + 'MAR07232_rest.h5') 109 109 self.data_list = Loader().load(filepath) 110 110 self.data = self.data_list[0] … … 121 121 122 122 o = r(self.data) 123 filepath = find(' ring_testdata.txt')123 filepath = find('test_data' + os.sep + 'ring_testdata.txt') 124 124 answer_list = Loader().load(filepath) 125 125 answer = answer_list[0] … … 142 142 o = r(self.data) 143 143 144 filepath = find(' avg_testdata.txt')144 filepath = find('test_data' + os.sep + 'avg_testdata.txt') 145 145 answer = Loader().load(filepath)[0] 146 146 for i in range(r.nbins_phi): … … 158 158 s, ds, npoints = r(self.data) 159 159 self.assertAlmostEqual(s, 34.278990899999997, 4) 160 self.assertAlmostEqual(ds, 7.8007981835194293, 4)160 self.assertAlmostEqual(ds, 8.237259999538685, 4) 161 161 self.assertAlmostEqual(npoints, 324.0000, 4) 162 162 … … 164 164 s, ds = r(self.data) 165 165 self.assertAlmostEqual(s, 0.10579935462962962, 4) 166 self.assertAlmostEqual(ds, 0.02 4076537603455028, 4)166 self.assertAlmostEqual(ds, 0.02542364197388483, 4) 167 167 168 168 def test_slabX(self): … … 177 177 o = r(self.data) 178 178 179 filepath = find(' slabx_testdata.txt')179 filepath = find('test_data' + os.sep + 'slabx_testdata.txt') 180 180 answer = Loader().load(filepath)[0] 181 181 for i in range(len(o.x)): … … 195 195 o = r(self.data) 196 196 197 filepath = find(' slaby_testdata.txt')197 filepath = find('test_data' + os.sep + 'slaby_testdata.txt') 198 198 answer = Loader().load(filepath)[0] 199 199 for i in range(len(o.x)): … … 221 221 o = r(self.data) 222 222 223 filepath = find(' ring_testdata.txt')223 filepath = find('test_data' + os.sep + 'ring_testdata.txt') 224 224 answer = Loader().load(filepath)[0] 225 225 for i in range(len(o.x)): … … 238 238 o = r(self.data) 239 239 240 filepath = find(' sectorphi_testdata.txt')240 filepath = find('test_data' + os.sep + 'sectorphi_testdata.txt') 241 241 answer = Loader().load(filepath)[0] 242 242 for i in range(len(o.x)): … … 255 255 o = r(self.data) 256 256 257 filepath = find(' sectorq_testdata.txt')257 filepath = find('test_data' + os.sep + 'sectorq_testdata.txt') 258 258 answer = Loader().load(filepath)[0] 259 259 for i in range(len(o.x)): -
test/sasdataloader/test/utest_cansas.py
rf53d684 rf4e2f22 91 91 reader = XMLreader(self.xml_valid, self.schema_1_0) 92 92 valid = reader.validate_xml() 93 if valid: 94 self.assertTrue(valid) 95 else: 96 self.assertFalse(valid) 93 self.assertTrue(valid) 97 94 98 95 def _check_data(self, data): … … 193 190 def test_save_cansas_v1_0(self): 194 191 xmlreader = XMLreader(self.isis_1_0, self.schema_1_0) 195 valid = xmlreader.validate_xml() 196 self.assertTrue(valid) 192 self.assertTrue(xmlreader.validate_xml()) 197 193 reader_generic = Loader() 198 194 dataloader = reader_generic.load(self.isis_1_0) … … 207 203 return_data = reader2.read(self.write_1_0_filename) 208 204 written_data = return_data[0] 209 XMLreader(self.write_1_0_filename, self.schema_1_0) 210 valid = xmlreader.validate_xml() 211 self.assertTrue(valid) 205 xmlreader = XMLreader(self.write_1_0_filename, self.schema_1_0) 206 self.assertTrue(xmlreader.validate_xml()) 212 207 self._check_data(written_data) 213 208 if os.path.isfile(self.write_1_0_filename): … … 260 255 self.loader = Loader() 261 256 self.datafile_basic = find("simpleexamplefile.h5") 262 self.datafile_multiplesasentry = find("cansas_1Dand2D_samedatafile.h5") 263 self.datafile_multiplesasdata = find("cansas_1Dand2D_samesasentry.h5") 264 self.datafile_multiplesasdata_multiplesasentry = find("cansas_1Dand2D_multiplesasentry_multiplesasdata.h5") 257 self.datafile_multiplesasentry = find( 258 "test_data" + os.sep + "nxcansas_1Dand2D_multisasentry.h5") 259 self.datafile_multiplesasdata = find( 260 "test_data" + os.sep + "nxcansas_1Dand2D_multisasdata.h5") 261 self.datafile_multiplesasdata_multiplesasentry = find( 262 "test_data" + os.sep + "nxcansas_1Dand2D_multisasentry_multisasdata.h5") 265 263 266 264 def test_real_data(self): … … 273 271 self._check_multiple_data(self.data[0]) 274 272 self._check_multiple_data(self.data[1]) 275 self._check_1d_data(self.data[0]) 273 if isinstance(self.data[0], Data1D): 274 self._check_1d_data(self.data[0]) 275 self._check_2d_data(self.data[1]) 276 else: 277 self._check_1d_data(self.data[1]) 278 self._check_2d_data(self.data[0]) 279 280 def test_multiple_sasdatas(self): 281 self.data = self.loader.load(self.datafile_multiplesasdata) 282 self.assertTrue(len(self.data) == 2) 283 self._check_multiple_data(self.data[0]) 284 self._check_multiple_data(self.data[1]) 285 if isinstance(self.data[0], Data1D): 286 self._check_1d_data(self.data[0]) 287 self._check_2d_data(self.data[1]) 288 else: 289 self._check_1d_data(self.data[1]) 290 self._check_2d_data(self.data[0]) 291 292 def test_multiple_sasentries_multiplesasdatas(self): 293 self.data = self.loader.load( 294 self.datafile_multiplesasdata_multiplesasentry) 295 self.assertTrue(len(self.data) == 4) 296 self._check_multiple_data(self.data[0]) 297 self._check_multiple_data(self.data[1]) 298 self._check_multiple_data(self.data[2]) 299 self._check_multiple_data(self.data[3]) 300 for data in self.data: 301 if isinstance(data, Data1D): 302 self._check_1d_data(data) 303 else: 304 self._check_2d_data(data) 276 305 277 306 def _check_multiple_data(self, data): 278 self.assertTrue(data.title == "MH4_5deg_16T_SLOW") 279 self.assertTrue(data.run[0] == '33837') 280 self.assertTrue(len(data.run) == 1) 281 self.assertTrue(data.instrument == "SANS2D") 282 self.assertTrue(data.source.radiation == "Spallation Neutron Source") 283 self.assertTrue(len(data.detector) == 1) 284 self.assertTrue(data.detector[0].name == "rear-detector") 285 self.assertTrue(data.detector[0].distance == 4.385281) 286 self.assertTrue(data.detector[0].distance_unit == 'm') 287 self.assertTrue(len(data.trans_spectrum) == 1) 307 self.assertEqual(data.title, "MH4_5deg_16T_SLOW") 308 self.assertEqual(data.run[0], '33837') 309 self.assertEqual(len(data.run), 1) 310 self.assertEqual(data.instrument, "SANS2D") 311 self.assertEqual(data.source.radiation, "Spallation Neutron Source") 312 self.assertEqual(len(data.detector), 2) 313 self.assertTrue(data.detector[0].name == "rear-detector" 314 or data.detector[1].name == "rear-detector") 315 self.assertTrue(data.detector[0].name == "front-detector" 316 or data.detector[1].name == "front-detector") 317 self.assertAlmostEqual(data.detector[0].distance + 318 data.detector[1].distance, 7230.54, 2) 319 self.assertEqual(data.detector[0].distance_unit, 'mm') 320 self.assertEqual(len(data.trans_spectrum), 1) 288 321 289 322 def _check_1d_data(self, data): 290 self.assertTrue(isinstance(data, Data1D)) 291 self.assertTrue(len(data.x) == 66) 292 self.assertTrue(len(data.x) == len(data.y)) 293 self.assertTrue(data.dy[10] == 0.20721350111248701) 294 self.assertTrue(data.y[10] == 24.193889608153476) 295 self.assertTrue(data.x[10] == 0.008981127988654792) 323 self.assertEqual(len(data.x), 66) 324 self.assertEqual(len(data.x), len(data.y)) 325 self.assertAlmostEqual(data.dy[10], 0.207214) 326 self.assertAlmostEqual(data.y[10], 24.1939) 327 self.assertAlmostEqual(data.x[10], 0.00898113) 296 328 297 329 def _check_2d_data(self, data): 298 330 self.assertTrue(isinstance(data, Data2D)) 299 self.assertTrue(len(data.x) == 66) 300 self.assertTrue(len(data.x) == len(data.y)) 301 self.assertTrue(data.dy[10] == 0.20721350111248701) 302 self.assertTrue(data.y[10] == 24.193889608153476) 303 self.assertTrue(data.x[10] == 0.008981127988654792) 331 self.assertEqual(len(data.q_data), 150*150) 332 self.assertEqual(len(data.q_data), len(data.data)) 333 self.assertAlmostEqual(data.err_data[10], 0.186723989418) 334 self.assertAlmostEqual(data.data[10], 0.465181) 335 self.assertAlmostEqual(data.qx_data[10], -0.129) 336 self.assertAlmostEqual(data.qy_data[10], -0.149) 304 337 305 338 def _check_example_data(self, data): 306 self.assert True(data.title =="")307 self.assert True(data.x.size ==100)308 self.assert True(data._xunit =="A^{-1}")309 self.assert True(data._yunit =="cm^{-1}")310 self.assert True(data.y.size ==100)339 self.assertEqual(data.title, "") 340 self.assertEqual(data.x.size, 100) 341 self.assertEqual(data._xunit, "A^{-1}") 342 self.assertEqual(data._yunit, "cm^{-1}") 343 self.assertEqual(data.y.size, 100) 311 344 self.assertAlmostEqual(data.y[40], 0.952749011516985) 312 345 self.assertAlmostEqual(data.x[40], 0.3834415188257777) -
test/sasdataloader/test/utest_generic_file_reader_class.py
r4a8d55c r282bc3f 45 45 last_f = f[0] 46 46 if hasattr(last_f, "errors"): 47 self.assertEqual s(len(last_f.errors), 1)47 self.assertEqual(len(last_f.errors), 1) 48 48 else: 49 49 self.fail("Errors did not propogate to the file properly.") … … 51 51 def test_same_file_unknown_extensions(self): 52 52 # Five files, all with the same content, but different file extensions 53 no_ext = find("test_data //TestExtensions")54 not_xml = find("test_data //TestExtensions.notxml")53 no_ext = find("test_data" + os.sep + "TestExtensions") 54 not_xml = find("test_data" + os.sep + "TestExtensions.notxml") 55 55 # Deprecated extensions 56 asc_dep = find("test_data //TestExtensions.asc")57 nxs_dep = find("test_data //TestExtensions.nxs")56 asc_dep = find("test_data" + os.sep + "TestExtensions.asc") 57 nxs_dep = find("test_data" + os.sep + "TestExtensions.nxs") 58 58 # Native extension as a baseline 59 xml_native = find("test_data //TestExtensions.xml")59 xml_native = find("test_data" + os.sep + "TestExtensions.xml") 60 60 # Load the files and check contents 61 61 no_ext_load = self.generic_reader.load(no_ext) … … 70 70 self.check_unknown_extension(xml_load[0]) 71 71 # Be sure the deprecation warning is passed with the file 72 self.assertEqual s(len(asc_load[0].errors), 1)73 self.assertEqual s(len(nxs_load[0].errors), 1)72 self.assertEqual(len(asc_load[0].errors), 1) 73 self.assertEqual(len(nxs_load[0].errors), 0) 74 74 75 75 def check_unknown_extension(self, data): 76 76 self.assertTrue(isinstance(data, Data1D)) 77 self.assertEqual s(len(data.x), 138)78 self.assertEqual s(data.sample.ID, "TK49 c10_SANS")79 self.assertEqual s(data.meta_data["loader"], "CanSAS XML 1D")77 self.assertEqual(len(data.x), 138) 78 self.assertEqual(data.sample.ID, "TK49 c10_SANS") 79 self.assertEqual(data.meta_data["loader"], "CanSAS XML 1D") 80 80 81 81 def tearDown(self): -
test/sasdataloader/test/utest_red2d_reader.py
rf53d684 rfc51d06 31 31 self.assertEqual(f.qx_data[0],-0.03573497) 32 32 self.assertEqual(f.qx_data[36863],0.2908819) 33 self.assertEqual(f.Q_unit, ' 1/A')34 self.assertEqual(f.I_unit, ' 1/cm')33 self.assertEqual(f.Q_unit, 'A^{-1}') 34 self.assertEqual(f.I_unit, 'cm^{-1}') 35 35 36 36 self.assertEqual(f.meta_data['loader'],"IGOR/DAT 2D Q_map") -
test/sasdataloader/test/utest_sesans.py
rf53d684 rf4e2f22 25 25 Test .SES in the full loader to make sure that the file type is correctly accepted 26 26 """ 27 file = Loader().load(find("sesans_examples/sphere2micron.ses")) 27 file = Loader().load(find("sesans_examples" + os.sep + 28 "sphere2micron.ses")) 28 29 f = file[0] 29 30 # self.assertEqual(f, 5) … … 44 45 Test .SES loading on a TOF dataset 45 46 """ 46 file = self.loader(find("sesans_examples /sphere_isis.ses"))47 file = self.loader(find("sesans_examples" + os.sep + "sphere_isis.ses")) 47 48 f = file[0] 48 49 self.assertEqual(len(file), 1) … … 62 63 FileContentsException, 63 64 self.loader, 64 find("sesans_examples /sesans_no_data.ses"))65 find("sesans_examples" + os.sep + "sesans_no_data.ses")) 65 66 66 67 def test_sesans_no_spin_echo_unit(self): … … 71 72 FileContentsException, 72 73 self.loader, 73 find("sesans_examples /no_spin_echo_unit.ses"))74 find("sesans_examples" + os.sep + "no_spin_echo_unit.ses")) 74 75 75 76 def test_sesans_future_version(self): … … 80 81 FileContentsException, 81 82 self.loader, 82 find("sesans_examples /next_gen.ses"))83 find("sesans_examples" + os.sep + "next_gen.ses")) 83 84 84 85 def test_sesans_mandatory_headers(self): … … 89 90 FileContentsException, 90 91 self.loader, 91 find("sesans_examples /no_wavelength.ses"))92 find("sesans_examples" + os.sep + "no_wavelength.ses")) 92 93 93 94 def test_sesans_columns_match_headers(self): … … 98 99 FileContentsException, 99 100 self.loader, 100 find("sesans_examples /too_many_headers.ses"))101 find("sesans_examples" + os.sep + "too_many_headers.ses")) 101 102 102 103 if __name__ == "__main__": -
src/sas/_config.py
rb963b20 r93b34cc 86 86 def setup_custom_config(app_dir, user_dir): 87 87 path = make_custom_config_path(user_dir) 88 #logger.info("custom config path %s", path) 88 89 if not os.path.isfile(path): 89 90 try: … … 94 95 logger.error("Could not copy default custom config.") 95 96 97 custom_config = load_custom_config(path) 98 96 99 #Adding SAS_OPENCL if it doesn't exist in the config file 97 100 # - to support backcompability 98 if not "SAS_OPENCL" in open(path).read(): 101 if not hasattr(custom_config, "SAS_OPENCL"): 102 custom_config.SAS_OPENCL = None 99 103 try: 100 open( config_file, "a+").write("SAS_OPENCL = \"None\"\n")104 open(path, "a+").write("SAS_OPENCL = \"None\"\n") 101 105 except Exception: 102 106 logger.error("Could not update custom config with SAS_OPENCL.") 103 107 104 custom_config = load_custom_config(path)105 108 return custom_config 106 107 109 108 110 def load_custom_config(path): -
src/sas/sasgui/perspectives/calculator/model_editor.py
r9bf40e7 r1752f38 965 965 out_f.write(' """Absolute scattering"""\n') 966 966 if "scipy." in func_str: 967 out_f.write(' import scipy ')967 out_f.write(' import scipy\n') 968 968 if "numpy." in func_str: 969 out_f.write(' import numpy ')969 out_f.write(' import numpy\n') 970 970 if "np." in func_str: 971 out_f.write(' import numpy as np ')971 out_f.write(' import numpy as np\n') 972 972 for func_line in func_str.split('\n'): 973 973 out_f.write('%s%s\n' % (' ', func_line)) -
src/sas/sasgui/perspectives/fitting/fitting.py
raba4559 ra5cffe5 776 776 :param weight: current dy data 777 777 """ 778 # If we are not dealing with a specific fit problem, then 779 # there is no point setting the weights. 780 if fid is None: 781 return 778 # Note: this is used to set the data weights for the fit based on 779 # the weight selection in the GUI. 782 780 if uid in self.page_finder.keys(): 783 781 self.page_finder[uid].set_weight(flag=flag, is2d=is2d) -
src/sas/sasgui/perspectives/fitting/gpu_options.py
r8e109f9 r895703d 23 23 import sasmodels 24 24 import sasmodels.model_test 25 import sasmodels. kernelcl25 import sasmodels.sasview_model 26 26 27 27 from sas.sasgui.guiframe.documentation_window import DocumentationWindow … … 239 239 if "SAS_OPENCL" in os.environ: 240 240 del os.environ["SAS_OPENCL"] 241 sasmodels. kernelcl.reset_environment()241 sasmodels.sasview_model.reset_environment() 242 242 event.Skip() 243 243 … … 265 265 if "SAS_OPENCL" in os.environ: 266 266 del os.environ["SAS_OPENCL"] 267 sasmodels. kernelcl.reset_environment()267 sasmodels.sasview_model.reset_environment() 268 268 269 269 try:
Note: See TracChangeset
for help on using the changeset viewer.