Changeset 35ac8df in sasview for src/sas/sascalc
- Timestamp:
- Aug 21, 2018 7:20:27 AM (6 years ago)
- Branches:
- master, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, unittest-saveload
- Children:
- c2525bf
- Parents:
- 96d06a4 (diff), ec52ea1 (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. - Location:
- src/sas/sascalc
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/dataloader/readers/abs_reader.py
r3bab401 r35ac8df 171 171 172 172 try: 173 _x = float(toks[ 0])173 _x = float(toks[4]) 174 174 _y = float(toks[1]) 175 175 _dy = float(toks[2]) -
src/sas/sascalc/fit/pagestate.py
r3b070a0 r59873e1 13 13 ################################################################################ 14 14 import time 15 import re 15 16 import os 16 17 import sys … … 962 963 if node.get('version'): 963 964 # Get the version for model conversion purposes 964 self.version = tuple(int(e) for e in965 str.split(node.get('version'), "."))965 x = re.sub('[^\d.]', '', node.get('version')) 966 self.version = tuple(int(e) for e in str.split(x, ".")) 966 967 # The tuple must be at least 3 items long 967 968 while len(self.version) < 3: -
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/file_reader_base_class.py
r4a8d55c r8f882fe 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__) … … 98 99 if len(self.output) > 0: 99 100 # Sort the data that's been loaded 100 self. sort_one_d_data()101 self.sort_ two_d_data()101 self.convert_data_units() 102 self.sort_data() 102 103 else: 103 104 msg = "Unable to find file at: {}\n".format(filepath) … … 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.qx_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("\\rm{Q}", '1/A') 291 data.yaxis("\\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", default_i_unit="1/cm"): 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_x_unit: The default x unit used by Sasview 311 :param default_y_unit: The default y unit used by Sasview 312 """ 313 new_output = [] 314 for data in self.output: 315 if data.isSesans: 316 new_output.append(data) 317 continue 318 file_x_unit = data._xunit 319 data_conv_x = Converter(file_x_unit) 320 file_y_unit = data._yunit 321 data_conv_y = Converter(file_y_unit) 322 if isinstance(data, Data1D): 323 try: 324 data.x = data_conv_x(data.x, units=default_q_unit) 325 data._xunit = default_q_unit 326 data.x_unit = default_q_unit 327 if data.dx is not None: 328 data.dx = data_conv_x(data.dx, units=default_q_unit) 329 if data.dxl is not None: 330 data.dxl = data_conv_x(data.dxl, units=default_q_unit) 331 if data.dxw is not None: 332 data.dxw = data_conv_x(data.dxw, units=default_q_unit) 333 except KeyError: 334 message = "Unable to convert Q units from {0} to 1/A." 335 message.format(default_q_unit) 336 data.errors.append(message) 337 try: 338 data.y = data_conv_y(data.y, units=default_i_unit) 339 data._yunit = default_i_unit 340 data.y_unit = default_i_unit 341 if data.dy is not None: 342 data.dy = data_conv_y(data.dy, units=default_i_unit) 343 except KeyError: 344 message = "Unable to convert I units from {0} to 1/cm." 345 message.format(default_q_unit) 346 data.errors.append(message) 347 elif isinstance(data, Data2D): 348 try: 349 data.qx_data = data_conv_x(data.qx_data, units=default_q_unit) 350 if data.dqx_data is not None: 351 data.dqx_data = data_conv_x(data.dqx_data, units=default_q_unit) 352 data.qy_data = data_conv_y(data.qy_data, units=default_q_unit) 353 if data.dqy_data is not None: 354 data.dqy_data = data_conv_y(data.dqy_data, units=default_q_unit) 355 except KeyError: 356 message = "Unable to convert Q units from {0} to 1/A." 357 message.format(default_q_unit) 358 data.errors.append(message) 359 try: 360 file_z_unit = data._zunit 361 data_conv_z = Converter(file_z_unit) 362 data.data = data_conv_z(data.data, units=default_i_unit) 363 if data.err_data is not None: 364 data.err_data = data_conv_z(data.err_data, units=default_i_unit) 365 except KeyError: 366 message = "Unable to convert I units from {0} to 1/cm." 367 message.format(default_q_unit) 368 data.errors.append(message) 369 else: 370 # TODO: Throw error of some sort... 371 pass 372 new_output.append(data) 373 self.output = new_output 279 374 280 375 def format_unit(self, unit=None): -
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/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 r96d06a4 38 38 # CanSAS version 39 39 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 40 # Data type name 51 type_name = " CanSAS 2.0"41 type_name = "NXcanSAS" 52 42 # Wildcards 53 type = [" CanSAS 2.0 HDF5 Files (*.h5)|*.h5"]43 type = ["NXcanSAS HDF5 Files (*.h5)|*.h5|"] 54 44 # List of allowed extensions 55 45 ext = ['.h5', '.H5'] … … 81 71 except Exception as e: 82 72 if extension not in self.ext: 83 msg = " CanSAS2.0HDF5 Reader could not load file {}".format(basename + extension)73 msg = "NXcanSAS HDF5 Reader could not load file {}".format(basename + extension) 84 74 raise DefaultReaderException(msg) 85 75 raise FileContentsException(e.message) … … 111 101 self.errors = set() 112 102 self.logging = [] 103 self.q_name = [] 104 self.mask_name = u'' 105 self.i_name = u'' 106 self.i_node = u'' 107 self.q_uncertainties = [] 108 self.q_resolutions = [] 109 self.i_uncertainties = u'' 113 110 self.parent_class = u'' 114 111 self.detector = Detector() … … 140 137 if isinstance(value, h5py.Group): 141 138 # Set parent class before recursion 139 last_parent_class = self.parent_class 142 140 self.parent_class = class_name 143 141 parent_list.append(key) … … 147 145 self.add_data_set(key) 148 146 elif class_prog.match(u'SASdata'): 149 self._initialize_new_data_set(parent_list) 147 self._initialize_new_data_set(value) 148 self._find_data_attributes(value) 150 149 # Recursion step to access data within the group 151 150 self.read_children(value, parent_list) 151 self.add_intermediate() 152 152 # Reset parent class when returning from recursive method 153 self.parent_class = class_name 154 self.add_intermediate() 153 self.parent_class = last_parent_class 155 154 parent_list.remove(key) 156 155 … … 159 158 data_set = data[key][:] 160 159 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 160 225 161 for data_point in data_set: … … 232 168 if key == u'definition': 233 169 self.current_datainfo.meta_data['reader'] = data_point 170 # Run 234 171 elif key == u'run': 235 172 self.current_datainfo.run.append(data_point) … … 240 177 except Exception: 241 178 pass 179 # Title 242 180 elif key == u'title': 243 181 self.current_datainfo.title = data_point 182 # Note 244 183 elif key == u'SASnote': 245 184 self.current_datainfo.notes.append(data_point) 246 247 185 # 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 186 elif self.parent_class == u'SASsample': 187 self.process_sample(data_point, key) 282 188 # Instrumental Information 283 189 elif (key == u'name' 284 190 and self.parent_class == u'SASinstrument'): 285 191 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 192 # Detector 193 elif self.parent_class == u'SASdetector': 194 self.process_detector(data_point, key, unit) 195 # Collimation 196 elif self.parent_class == u'SAScollimation': 197 self.process_collimation(data_point, key, unit) 198 # Aperture 199 elif self.parent_class == u'SASaperture': 200 self.process_aperture(data_point, key) 346 201 # 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 202 elif self.parent_class == u'SASprocess': # CanSAS 2.0 203 self.process_process(data_point, key) 363 204 # 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 205 elif self.parent_class == u'SASsource': 206 self.process_source(data_point, key, unit) 404 207 # Everything else goes in meta_data 208 elif self.parent_class == u'SASdata': 209 if isinstance(self.current_dataset, plottable_2D): 210 self.process_2d_data_object(data_set, key, unit) 211 else: 212 self.process_1d_data_object(data_set, key, unit) 213 214 break 215 elif self.parent_class == u'SAStransmission_spectrum': 216 self.process_trans_spectrum(data_set, key) 217 break 405 218 else: 406 219 new_key = self._create_unique_key( … … 411 224 # I don't know if this reachable code 412 225 self.errors.add("ShouldNeverHappenException") 226 227 def process_1d_data_object(self, data_set, key, unit): 228 """ 229 SASdata processor method for 1d data items 230 :param data_set: data from HDF5 file 231 :param key: canSAS_class attribute 232 :param unit: unit attribute 233 """ 234 if key == self.i_name: 235 self.current_dataset.y = data_set.flatten() 236 self.current_dataset.yaxis("Intensity", unit) 237 elif key == self.i_uncertainties: 238 self.current_dataset.dy = data_set.flatten() 239 elif key in self.q_name: 240 self.current_dataset.xaxis("Q", unit) 241 self.current_dataset.x = data_set.flatten() 242 elif key in self.q_uncertainties or key in self.q_resolutions: 243 if (len(self.q_resolutions) > 1 244 and np.where(self.q_resolutions == key)[0] == 0): 245 self.current_dataset.dxw = data_set.flatten() 246 elif (len(self.q_resolutions) > 1 247 and np.where(self.q_resolutions == key)[0] == 1): 248 self.current_dataset.dxl = data_set.flatten() 249 else: 250 self.current_dataset.dx = data_set.flatten() 251 elif key == self.mask_name: 252 self.current_dataset.mask = data_set.flatten() 253 elif key == u'wavelength': 254 self.current_datainfo.source.wavelength = data_set[0] 255 self.current_datainfo.source.wavelength_unit = unit 256 257 def process_2d_data_object(self, data_set, key, unit): 258 if key == self.i_name: 259 self.current_dataset.data = data_set 260 self.current_dataset.zaxis("Intensity", unit) 261 elif key == self.i_uncertainties: 262 self.current_dataset.err_data = data_set.flatten() 263 elif key in self.q_name: 264 self.current_dataset.xaxis("Q", unit) 265 self.current_dataset.yaxis("Q", unit) 266 #FIXME: This is broken - need to properly handle 2D data 267 # TODO: Check shape of array (2d - cash money, homey!) 268 # TODO: 3D - check dims, etc. 269 # TODO: Put data where it belongs 270 pass 271 elif key in self.q_uncertainties or key in self.q_resolutions: 272 # FIXME: This isn't right either. 273 # TODO: find resolution/uncertainty specific to q_name 274 pass 275 elif key == u'Qy': 276 self.current_dataset.yaxis("Q_y", unit) 277 self.current_dataset.qy_data = data_set.flatten() 278 elif key == u'Qydev': 279 self.current_dataset.dqy_data = data_set.flatten() 280 elif key == u'Qx': 281 self.current_dataset.xaxis("Q_x", unit) 282 self.current_dataset.qx_data = data_set.flatten() 283 elif key == u'Qxdev': 284 self.current_dataset.dqx_data = data_set.flatten() 285 286 def process_trans_spectrum(self, data_set, key): 287 """ 288 SAStransmission_spectrum processor 289 :param data_set: data from HDF5 file 290 :param key: canSAS_class attribute 291 """ 292 if key == u'T': 293 self.trans_spectrum.transmission = data_set.flatten() 294 elif key == u'Tdev': 295 self.trans_spectrum.transmission_deviation = data_set.flatten() 296 elif key == u'lambda': 297 self.trans_spectrum.wavelength = data_set.flatten() 298 299 def process_sample(self, data_point, key): 300 """ 301 SASsample processor 302 :param data_point: Single point from an HDF5 data file 303 :param key: class name data_point was taken from 304 """ 305 if key == u'Title': 306 self.current_datainfo.sample.name = data_point 307 elif key == u'name': 308 self.current_datainfo.sample.name = data_point 309 elif key == u'ID': 310 self.current_datainfo.sample.name = data_point 311 elif key == u'thickness': 312 self.current_datainfo.sample.thickness = data_point 313 elif key == u'temperature': 314 self.current_datainfo.sample.temperature = data_point 315 elif key == u'transmission': 316 self.current_datainfo.sample.transmission = data_point 317 elif key == u'x_position': 318 self.current_datainfo.sample.position.x = data_point 319 elif key == u'y_position': 320 self.current_datainfo.sample.position.y = data_point 321 elif key == u'pitch': 322 self.current_datainfo.sample.orientation.x = data_point 323 elif key == u'yaw': 324 self.current_datainfo.sample.orientation.y = data_point 325 elif key == u'roll': 326 self.current_datainfo.sample.orientation.z = data_point 327 elif key == u'details': 328 self.current_datainfo.sample.details.append(data_point) 329 330 def process_detector(self, data_point, key, unit): 331 """ 332 SASdetector processor 333 :param data_point: Single point from an HDF5 data file 334 :param key: class name data_point was taken from 335 :param unit: unit attribute from data set 336 """ 337 if key == u'name': 338 self.detector.name = data_point 339 elif key == u'SDD': 340 self.detector.distance = float(data_point) 341 self.detector.distance_unit = unit 342 elif key == u'slit_length': 343 self.detector.slit_length = float(data_point) 344 self.detector.slit_length_unit = unit 345 elif key == u'x_position': 346 self.detector.offset.x = float(data_point) 347 self.detector.offset_unit = unit 348 elif key == u'y_position': 349 self.detector.offset.y = float(data_point) 350 self.detector.offset_unit = unit 351 elif key == u'pitch': 352 self.detector.orientation.x = float(data_point) 353 self.detector.orientation_unit = unit 354 elif key == u'roll': 355 self.detector.orientation.z = float(data_point) 356 self.detector.orientation_unit = unit 357 elif key == u'yaw': 358 self.detector.orientation.y = float(data_point) 359 self.detector.orientation_unit = unit 360 elif key == u'beam_center_x': 361 self.detector.beam_center.x = float(data_point) 362 self.detector.beam_center_unit = unit 363 elif key == u'beam_center_y': 364 self.detector.beam_center.y = float(data_point) 365 self.detector.beam_center_unit = unit 366 elif key == u'x_pixel_size': 367 self.detector.pixel_size.x = float(data_point) 368 self.detector.pixel_size_unit = unit 369 elif key == u'y_pixel_size': 370 self.detector.pixel_size.y = float(data_point) 371 self.detector.pixel_size_unit = unit 372 373 def process_collimation(self, data_point, key, unit): 374 """ 375 SAScollimation processor 376 :param data_point: Single point from an HDF5 data file 377 :param key: class name data_point was taken from 378 :param unit: unit attribute from data set 379 """ 380 if key == u'distance': 381 self.collimation.length = data_point 382 self.collimation.length_unit = unit 383 elif key == u'name': 384 self.collimation.name = data_point 385 386 def process_aperture(self, data_point, key): 387 """ 388 SASaperture processor 389 :param data_point: Single point from an HDF5 data file 390 :param key: class name data_point was taken from 391 """ 392 if key == u'shape': 393 self.aperture.shape = data_point 394 elif key == u'x_gap': 395 self.aperture.size.x = data_point 396 elif key == u'y_gap': 397 self.aperture.size.y = data_point 398 399 def process_source(self, data_point, key, unit): 400 """ 401 SASsource processor 402 :param data_point: Single point from an HDF5 data file 403 :param key: class name data_point was taken from 404 :param unit: unit attribute from data set 405 """ 406 if key == u'incident_wavelength': 407 self.current_datainfo.source.wavelength = data_point 408 self.current_datainfo.source.wavelength_unit = unit 409 elif key == u'wavelength_max': 410 self.current_datainfo.source.wavelength_max = data_point 411 self.current_datainfo.source.wavelength_max_unit = unit 412 elif key == u'wavelength_min': 413 self.current_datainfo.source.wavelength_min = data_point 414 self.current_datainfo.source.wavelength_min_unit = unit 415 elif key == u'incident_wavelength_spread': 416 self.current_datainfo.source.wavelength_spread = data_point 417 self.current_datainfo.source.wavelength_spread_unit = unit 418 elif key == u'beam_size_x': 419 self.current_datainfo.source.beam_size.x = data_point 420 self.current_datainfo.source.beam_size_unit = unit 421 elif key == u'beam_size_y': 422 self.current_datainfo.source.beam_size.y = data_point 423 self.current_datainfo.source.beam_size_unit = unit 424 elif key == u'beam_shape': 425 self.current_datainfo.source.beam_shape = data_point 426 elif key == u'radiation': 427 self.current_datainfo.source.radiation = data_point 428 429 def process_process(self, data_point, key): 430 """ 431 SASprocess processor 432 :param data_point: Single point from an HDF5 data file 433 :param key: class name data_point was taken from 434 """ 435 term_match = re.compile(u'^term[0-9]+$') 436 if key == u'Title': # CanSAS 2.0 437 self.process.name = data_point 438 elif key == u'name': # NXcanSAS 439 self.process.name = data_point 440 elif key == u'description': 441 self.process.description = data_point 442 elif key == u'date': 443 self.process.date = data_point 444 elif term_match.match(key): 445 self.process.term.append(data_point) 446 else: 447 self.process.notes.append(data_point) 413 448 414 449 def add_intermediate(self): … … 452 487 spectrum_list = [] 453 488 for spectrum in self.current_datainfo.trans_spectrum: 454 spectrum.transmission = np.delete(spectrum.transmission, [0])455 489 spectrum.transmission = spectrum.transmission.astype(np.float64) 456 spectrum.transmission_deviation = np.delete(457 spectrum.transmission_deviation, [0])458 490 spectrum.transmission_deviation = \ 459 491 spectrum.transmission_deviation.astype(np.float64) 460 spectrum.wavelength = np.delete(spectrum.wavelength, [0])461 492 spectrum.wavelength = spectrum.wavelength.astype(np.float64) 462 493 if len(spectrum.transmission) > 0: … … 491 522 (n_rows, n_cols) = dataset.data.shape 492 523 dataset.y_bins = dataset.qy_data[0::n_cols] 493 dataset.x_bins = dataset.qx_data[ :n_cols]524 dataset.x_bins = dataset.qx_data[0::n_rows] 494 525 dataset.data = dataset.data.flatten() 495 526 self.current_dataset = dataset … … 515 546 self.current_datainfo = DataInfo() 516 547 517 518 def _initialize_new_data_set(self, parent_list=None): 548 def _initialize_new_data_set(self, value=None): 519 549 """ 520 550 A private class method to generate a new 1D or 2D data object based on … … 524 554 :param parent_list: List of names of parent elements 525 555 """ 526 527 if parent_list is None: 528 parent_list = [] 529 if self._find_intermediate(parent_list, "Qx"): 556 if self._is2d(value): 530 557 self.current_dataset = plottable_2D() 531 558 else: … … 534 561 self.current_dataset = plottable_1D(x, y) 535 562 self.current_datainfo.filename = self.raw_data.filename 536 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. 563 self.mask_name = "" 564 self.i_name = "" 565 self.i_node = "" 566 self.q_name = [] 567 self.q_uncertainties = [] 568 self.q_resolutions = [] 569 self.i_uncertainties = "" 570 571 @staticmethod 572 def check_is_list_or_array(iterable): 573 try: 574 iter(iterable) 575 if (not isinstance(iterable, np.ndarray)) or (isinstance(iterable, str) 576 or isinstance(iterable, unicode)): 577 raise TypeError 578 except TypeError: 579 iterable = iterable.split(",") 580 return iterable 581 582 def _find_data_attributes(self, value): 583 """ 584 A class to find the indices for Q, the name of the Qdev and Idev, and 585 the name of the mask. 586 :param value: SASdata/NXdata HDF5 Group 587 """ 588 attrs = value.attrs 589 signal = attrs.get("signal", "I") 590 i_axes = attrs.get("I_axes", ["Q"]) 591 q_indices = attrs.get("Q_indices", [0]) 592 q_indices = map(int, self.check_is_list_or_array(q_indices)) 593 i_axes = self.check_is_list_or_array(i_axes) 594 keys = value.keys() 595 self.mask_name = attrs.get("mask") 596 for val in q_indices: 597 self.q_name.append(i_axes[val]) 598 self.i_name = signal 599 self.i_node = value.get(self.i_name) 600 for item in self.q_name: 601 if item in keys: 602 q_vals = value.get(item) 603 if q_vals.attrs.get("uncertainties") is not None: 604 self.q_uncertainties = q_vals.attrs.get("uncertainties") 605 elif q_vals.attrs.get("uncertainty") is not None: 606 self.q_uncertainties = q_vals.attrs.get("uncertainty") 607 if isinstance(self.q_uncertainties, str) is not None: 608 self.q_uncertainties = [self.q_uncertainties] 609 if q_vals.attrs.get("resolutions") is not None: 610 self.q_resolutions = q_vals.attrs.get("resolutions") 611 if isinstance(self.q_resolutions, str): 612 self.q_resolutions = self.q_resolutions.split(",") 613 if self.i_name in keys: 614 i_vals = value.get(self.i_name) 615 self.i_uncertainties = i_vals.attrs.get("uncertainties") 616 if self.i_uncertainties is None: 617 self.i_uncertainties = i_vals.attrs.get("uncertainty") 618 619 def _is2d(self, value, basename="I"): 620 """ 621 A private class to determine if the data set is 1d or 2d. 541 622 542 623 :param parent_list: List of parents nodes in the HDF5 file 543 624 :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 625 :return: True if 2D, otherwise false 626 """ 627 628 vals = value.get(basename) 629 return (vals is not None and vals.shape is not None 630 and len(vals.shape) != 1) 557 631 558 632 def _create_unique_key(self, dictionary, name, numb=0): … … 583 657 if unit is None: 584 658 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 659 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 rcf29187 175 175 names=['beam_size_x', 'beam_size_y'], 176 176 units=data_info.source.beam_size_unit, write_fn=_write_h5_float) 177 178 177 179 178 # Collimation metadata … … 232 231 detector_entry.attrs['name'] = '' 233 232 233 # Process meta data 234 if len(data_info.process) > 0 and not data_info.process[0].is_empty(): 235 i = 1 236 for process in data_info.process: 237 process_entry = sasentry.create_group( 238 'sasprocess{0:0=2d}'.format(i)) 239 process_entry.attrs['canSAS_class'] = 'SASprocess' 240 if process.name: 241 name = _h5_string(process.name) 242 process_entry.create_dataset('name', data=name) 243 if process.date: 244 date = _h5_string(process.date) 245 process_entry.create_dataset('date', data=date) 246 if process.description: 247 desc = _h5_string(process.description) 248 process_entry.create_dataset('description', data=desc) 249 j = 1 250 for term in process.term: 251 if term: 252 h5_term = _h5_string(term) 253 process_entry.create_dataset('term{0:0=2d}'.format(j), 254 data=h5_term) 255 j += 1 256 j = 1 257 for note in process.notes: 258 if note: 259 h5_note = _h5_string(note) 260 process_entry.create_dataset('note{0:0=2d}'.format(j), 261 data=h5_note) 262 j += 1 263 i += 1 264 265 # Transmission Spectrum 266 if len(data_info.trans_spectrum) > 0: 267 i = 1 268 for trans in data_info.trans_spectrum: 269 trans_entry = sasentry.create_group( 270 'sastransmission_spectrum{0:0=2d}'.format(i)) 271 trans_entry.attrs['canSAS_class'] = 'SAStransmission_spectrum' 272 trans_entry.attrs['signal'] = 'T' 273 trans_entry.attrs['T_axes'] = 'T' 274 trans_entry.attrs['name'] = trans.name 275 if trans.timestamp is not '': 276 trans_entry.attrs['timestamp'] = trans.timestamp 277 transmission = trans_entry.create_dataset( 278 'T', data=trans.transmission) 279 transmission.attrs['unertainties'] = 'Tdev' 280 trans_entry.create_dataset('Tdev', 281 data = trans.transmission_deviation) 282 trans_entry.create_dataset('lambda', data=trans.wavelength) 283 234 284 note_entry = sasentry.create_group('sasnote'.format(i)) 235 285 note_entry.attrs['canSAS_class'] = 'SASnote' … … 254 304 data_entry.attrs['signal'] = 'I' 255 305 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) 306 data_entry.attrs['Q_indices'] = [0] 307 q_entry = data_entry.create_dataset('Q', data=data_obj.x) 308 q_entry.attrs['units'] = data_obj.x_unit 309 i_entry = data_entry.create_dataset('I', data=data_obj.y) 310 i_entry.attrs['units'] = data_obj.y_unit 311 if data_obj.dy is not None: 312 i_entry.attrs['uncertainties'] = 'Idev' 313 i_dev_entry = data_entry.create_dataset('Idev', data=data_obj.dy) 314 i_dev_entry.attrs['units'] = data_obj.y_unit 315 if data_obj.dx is not None: 316 q_entry.attrs['resolutions'] = 'dQ' 317 dq_entry = data_entry.create_dataset('dQ', data=data_obj.dx) 318 dq_entry.attrs['units'] = data_obj.x_unit 319 elif data_obj.dxl is not None: 320 q_entry.attrs['resolutions'] = ['dQl','dQw'] 321 dql_entry = data_entry.create_dataset('dQl', data=data_obj.dxl) 322 dql_entry.attrs['units'] = data_obj.x_unit 323 dqw_entry = data_entry.create_dataset('dQw', data=data_obj.dxw) 324 dqw_entry.attrs['units'] = data_obj.x_unit 266 325 267 326 def _write_2d_data(self, data, data_entry): … … 273 332 """ 274 333 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] 334 data_entry.attrs['I_axes'] = 'Qx,Qy' 335 data_entry.attrs['Q_indices'] = [0,1] 278 336 279 337 (n_rows, n_cols) = (len(data.y_bins), len(data.x_bins)) … … 288 346 raise ValueError("Unable to calculate dimensions of 2D data") 289 347 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)) 348 intensity = np.reshape(data.data, (n_rows, n_cols)) 349 qx = np.reshape(data.qx_data, (n_rows, n_cols)) 295 350 qy = np.reshape(data.qy_data, (n_rows, n_cols)) 296 351 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 352 i_entry = data_entry.create_dataset('I', data=intensity) 353 i_entry.attrs['units'] = data.I_unit 354 qx_entry = data_entry.create_dataset('Qx', data=qx) 355 qx_entry.attrs['units'] = data.Q_unit 356 qy_entry = data_entry.create_dataset('Qy', data=qy) 357 qy_entry.attrs['units'] = data.Q_unit 358 if data.err_data is not None and not all(data.err_data == [None]): 359 d_i = np.reshape(data.err_data, (n_rows, n_cols)) 360 i_entry.attrs['uncertainties'] = 'Idev' 361 i_dev_entry = data_entry.create_dataset('Idev', data=d_i) 362 i_dev_entry.attrs['units'] = data.I_unit 363 if data.dqx_data is not None and not all(data.dqx_data == [None]): 364 qx_entry.attrs['resolutions'] = 'dQx' 365 dqx_entry = data_entry.create_dataset('dQx', data=data.dqx_data) 366 dqx_entry.attrs['units'] = data.Q_unit 367 if data.dqy_data is not None and not all(data.dqy_data == [None]): 368 qy_entry.attrs['resolutions'] = 'dQy' 369 dqy_entry = data_entry.create_dataset('dQy', data=data.dqy_data) 370 dqy_entry.attrs['units'] = data.Q_unit
Note: See TracChangeset
for help on using the changeset viewer.