Changeset 574adc7 in sasview for src/sas/sascalc/dataloader
- Timestamp:
- Sep 22, 2017 4:01:32 PM (7 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 34d7b35
- Parents:
- 9706d88
- Location:
- src/sas/sascalc/dataloader
- Files:
-
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/dataloader/__init__.py
rb699768 r574adc7 1 from data_info import *2 from manipulations import *3 from readers import *1 from .data_info import * 2 from .manipulations import * 3 from .readers import * -
src/sas/sascalc/dataloader/data_info.py
r17e257b5 r574adc7 716 716 self.y_unit = '1/cm' 717 717 except: # the data is not recognized/supported, and the user is notified 718 raise (TypeError,'data not recognized, check documentation for supported 1D data formats')718 raise TypeError('data not recognized, check documentation for supported 1D data formats') 719 719 720 720 def __str__(self): … … 796 796 len(self.y) != len(other.y): 797 797 msg = "Unable to perform operation: data length are not equal" 798 raise ValueError , msg798 raise ValueError(msg) 799 799 # Here we could also extrapolate between data points 800 800 TOLERANCE = 0.01 … … 802 802 if math.fabs((self.x[i] - other.x[i])/self.x[i]) > TOLERANCE: 803 803 msg = "Incompatible data sets: x-values do not match" 804 raise ValueError , msg804 raise ValueError(msg) 805 805 806 806 # Check that the other data set has errors, otherwise … … 876 876 if not isinstance(other, Data1D): 877 877 msg = "Unable to perform operation: different types of data set" 878 raise ValueError , msg878 raise ValueError(msg) 879 879 return True 880 880 … … 948 948 949 949 if len(self.detector) > 0: 950 raise RuntimeError , "Data2D: Detector bank already filled at init"950 raise RuntimeError("Data2D: Detector bank already filled at init") 951 951 952 952 def __str__(self): … … 1020 1020 len(self.qy_data) != len(other.qy_data): 1021 1021 msg = "Unable to perform operation: data length are not equal" 1022 raise ValueError , msg1022 raise ValueError(msg) 1023 1023 for ind in range(len(self.data)): 1024 1024 if math.fabs((self.qx_data[ind] - other.qx_data[ind])/self.qx_data[ind]) > TOLERANCE: 1025 1025 msg = "Incompatible data sets: qx-values do not match: %s %s" % (self.qx_data[ind], other.qx_data[ind]) 1026 raise ValueError , msg1026 raise ValueError(msg) 1027 1027 if math.fabs((self.qy_data[ind] - other.qy_data[ind])/self.qy_data[ind]) > TOLERANCE: 1028 1028 msg = "Incompatible data sets: qy-values do not match: %s %s" % (self.qy_data[ind], other.qy_data[ind]) 1029 raise ValueError , msg1029 raise ValueError(msg) 1030 1030 1031 1031 # Check that the scales match … … 1108 1108 if not isinstance(other, Data2D): 1109 1109 msg = "Unable to perform operation: different types of data set" 1110 raise ValueError , msg1110 raise ValueError(msg) 1111 1111 return True 1112 1112 -
src/sas/sascalc/dataloader/file_reader_base_class.py
rae69c690 r574adc7 8 8 import re 9 9 import logging 10 from abc import abstractmethod 11 10 12 import numpy as np 11 from abc import abstractmethod 12 from loader_exceptions import NoKnownLoaderException, FileContentsException,\ 13 from .loader_exceptions import NoKnownLoaderException, FileContentsException,\ 13 14 DataReaderException, DefaultReaderException 14 from data_info import Data1D, Data2D, DataInfo, plottable_1D, plottable_2D,\15 from .data_info import Data1D, Data2D, DataInfo, plottable_1D, plottable_2D,\ 15 16 combine_data_info_with_plottable 16 17 -
src/sas/sascalc/dataloader/loader.py
rdcb91cf r574adc7 26 26 import time 27 27 from zipfile import ZipFile 28 28 29 from sas.sascalc.data_util.registry import ExtensionRegistry 30 29 31 # Default readers are defined in the readers sub-module 30 import readers31 from loader_exceptions import NoKnownLoaderException, FileContentsException,\32 from . import readers 33 from .loader_exceptions import NoKnownLoaderException, FileContentsException,\ 32 34 DefaultReaderException 33 from readers import ascii_reader34 from readers import cansas_reader35 from readers import cansas_reader_HDF535 from .readers import ascii_reader 36 from .readers import cansas_reader 37 from .readers import cansas_reader_HDF5 36 38 37 39 logger = logging.getLogger(__name__) … … 341 343 # Raise an error if there are no matching extensions 342 344 if len(writers) == 0: 343 raise ValueError , "Unknown file type for " + path345 raise ValueError("Unknown file type for " + path) 344 346 # All done 345 347 return writers … … 360 362 try: 361 363 return fn(path, data) 362 except :364 except Exception: 363 365 pass # give other loaders a chance to succeed 364 366 # If we get here it is because all loaders failed -
src/sas/sascalc/dataloader/manipulations.py
r324e0bf r574adc7 26 26 27 27 #from data_info import plottable_2D 28 from data_info import Data1D28 from .data_info import Data1D 29 29 30 30 -
src/sas/sascalc/dataloader/readers/__init__.py
r7a5d066 r574adc7 1 1 # Method to associate extensions to default readers 2 from associations import read_associations2 from .associations import read_associations 3 3 4 4 -
src/sas/sascalc/dataloader/readers/abs_reader.py
rad92c5a r574adc7 31 31 # List of allowed extensions 32 32 ext = ['.abs'] 33 33 34 34 def get_file_contents(self): 35 """ 35 """ 36 36 Get the contents of the file 37 37 38 38 :raise RuntimeError: when the file can't be opened 39 39 :raise ValueError: when the length of the data vectors are inconsistent -
src/sas/sascalc/dataloader/readers/ascii_reader.py
rf994e8b1 r574adc7 130 130 # Reset # of lines of data candidates 131 131 candidate_lines = 0 132 132 133 133 if not is_data: 134 134 self.set_all_to_none() -
src/sas/sascalc/dataloader/readers/associations.py
rce8c7bd r574adc7 40 40 """ 41 41 # For each FileType entry, get the associated reader and extension 42 for ext, reader in settings.ite ritems():42 for ext, reader in settings.items(): 43 43 if reader is not None and ext is not None: 44 44 # Associate the extension with a particular reader … … 47 47 # and remove the extra line below. 48 48 try: 49 exec "import %s" % reader50 exec "loader.associate_file_type('%s', %s)" % (ext.lower(),51 reader)52 exec "loader.associate_file_type('%s', %s)" % (ext.upper(),53 reader)49 exec("from . import %s" % reader) 50 exec("loader.associate_file_type('%s', %s)" 51 % (ext.lower(), reader)) 52 exec("loader.associate_file_type('%s', %s)" 53 % (ext.upper(), reader)) 54 54 except: 55 55 msg = "read_associations: skipping association" -
src/sas/sascalc/dataloader/readers/cansas_reader.py
rae69c690 r574adc7 1 1 import logging 2 import numpy as np3 2 import os 4 3 import sys 5 4 import datetime 6 5 import inspect 7 # For saving individual sections of data 8 from sas.sascalc.dataloader.data_info import Data1D, Data2D, DataInfo, \ 9 plottable_1D, plottable_2D 10 from sas.sascalc.dataloader.data_info import Collimation, TransmissionSpectrum, \ 11 Detector, Process, Aperture 12 from sas.sascalc.dataloader.data_info import \ 13 combine_data_info_with_plottable as combine_data 14 import sas.sascalc.dataloader.readers.xml_reader as xml_reader 15 from sas.sascalc.dataloader.readers.xml_reader import XMLreader 16 from sas.sascalc.dataloader.readers.cansas_constants import CansasConstants, CurrentLevel 17 from sas.sascalc.dataloader.loader_exceptions import FileContentsException, \ 18 DefaultReaderException, DataReaderException 6 7 import numpy as np 19 8 20 9 # The following 2 imports *ARE* used. Do not remove either. … … 23 12 24 13 from lxml import etree 14 15 from sas.sascalc.data_util.nxsunit import Converter 16 17 # For saving individual sections of data 18 from ..data_info import Data1D, Data2D, DataInfo, plottable_1D, plottable_2D, \ 19 Collimation, TransmissionSpectrum, Detector, Process, Aperture, \ 20 combine_data_info_with_plottable as combine_data 21 from ..loader_exceptions import FileContentsException, DefaultReaderException, \ 22 DataReaderException 23 from . import xml_reader 24 from .xml_reader import XMLreader 25 from .cansas_constants import CansasConstants, CurrentLevel 25 26 26 27 logger = logging.getLogger(__name__) … … 34 35 "as much of the data as possible.\n\n" 35 36 HAS_CONVERTER = True 36 try:37 from sas.sascalc.data_util.nxsunit import Converter38 except ImportError:39 HAS_CONVERTER = False40 37 41 38 CONSTANTS = CansasConstants() … … 163 160 raise fc_exc 164 161 except Exception as e: # Convert all other exceptions to FileContentsExceptions 165 raise FileContentsException(e.message) 162 raise 163 raise FileContentsException(str(e)) 166 164 167 165 … … 632 630 else: 633 631 save_in = "current_datainfo" 634 exec "default_unit = self.{0}.{1}".format(save_in, unitname) 635 if local_unit and default_unit and local_unit.lower() != default_unit.lower() \ 636 and local_unit.lower() != "none": 637 if HAS_CONVERTER == True: 632 exec("default_unit = self.{0}.{1}".format(save_in, unitname)) 633 if (local_unit and default_unit 634 and local_unit.lower() != default_unit.lower() 635 and local_unit.lower() != "none"): 636 if HAS_CONVERTER: 638 637 # Check local units - bad units raise KeyError 639 638 data_conv_q = Converter(local_unit) … … 654 653 err_msg += "expecting [{0}]".format(default_unit) 655 654 value_unit = local_unit 656 except :655 except Exception: 657 656 err_msg = "CanSAS reader: unknown error converting " 658 657 err_msg += "\"{0}\" unit [{1}]" … … 908 907 point = self.create_element("Idata") 909 908 node.append(point) 910 qx = ','.join( [str(datainfo.qx_data[i]) for i in xrange(len(datainfo.qx_data))])911 qy = ','.join( [str(datainfo.qy_data[i]) for i in xrange(len(datainfo.qy_data))])912 intensity = ','.join( [str(datainfo.data[i]) for i in xrange(len(datainfo.data))])909 qx = ','.join(str(v) for v in datainfo.qx_data) 910 qy = ','.join(str(v) for v in datainfo.qy_data) 911 intensity = ','.join(str(v) for v in datainfo.data) 913 912 914 913 self.write_node(point, "Qx", qx, … … 919 918 {'unit': datainfo._zunit}) 920 919 if datainfo.err_data is not None: 921 err = ','.join([str(datainfo.err_data[i]) for i in 922 xrange(len(datainfo.err_data))]) 920 err = ','.join(str(v) for v in datainfo.err_data) 923 921 self.write_node(point, "Idev", err, 924 922 {'unit': datainfo._zunit}) 925 923 if datainfo.dqy_data is not None: 926 dqy = ','.join([str(datainfo.dqy_data[i]) for i in 927 xrange(len(datainfo.dqy_data))]) 924 dqy = ','.join(str(v) for v in datainfo.dqy_data) 928 925 self.write_node(point, "Qydev", dqy, 929 926 {'unit': datainfo._yunit}) 930 927 if datainfo.dqx_data is not None: 931 dqx = ','.join([str(datainfo.dqx_data[i]) for i in 932 xrange(len(datainfo.dqx_data))]) 928 dqx = ','.join(str(v) for v in datainfo.dqx_data) 933 929 self.write_node(point, "Qxdev", dqx, 934 930 {'unit': datainfo._xunit}) 935 931 if datainfo.mask is not None: 936 mask = ','.join( 937 ["1" if datainfo.mask[i] else "0" 938 for i in xrange(len(datainfo.mask))]) 932 mask = ','.join("1" if v else "0" for v in datainfo.mask) 939 933 self.write_node(point, "Mask", mask) 940 934 … … 1280 1274 try: 1281 1275 value = float(entry.text) 1282 except :1276 except ValueError: 1283 1277 value = None 1284 1278 … … 1289 1283 if units is not None: 1290 1284 toks = variable.split('.') 1291 local_unit = None 1292 exec "local_unit = storage.%s_unit" % toks[0] 1285 exec("local_unit = storage.%s_unit" % toks[0]) 1293 1286 if local_unit is not None and units.lower() != local_unit.lower(): 1294 1287 if HAS_CONVERTER == True: 1295 1288 try: 1296 1289 conv = Converter(units) 1297 exec "storage.%s = %g" % \1298 (variable, conv(value, units=local_unit))1299 except :1290 exec("storage.%s = %g" % 1291 - (variable, conv(value, units=local_unit))) 1292 except Exception: 1300 1293 _, exc_value, _ = sys.exc_info() 1301 1294 err_mess = "CanSAS reader: could not convert" … … 1306 1299 logger.info(err_mess) 1307 1300 else: 1308 raise ValueError , err_mess1301 raise ValueError(err_mess) 1309 1302 else: 1310 1303 err_mess = "CanSAS reader: unrecognized %s unit [%s];"\ … … 1315 1308 logger.info(err_mess) 1316 1309 else: 1317 raise ValueError , err_mess1310 raise ValueError(err_mess) 1318 1311 else: 1319 exec "storage.%s = value" % variable1312 exec("storage.%s = value" % variable) 1320 1313 else: 1321 exec "storage.%s = value" % variable1314 exec("storage.%s = value" % variable) 1322 1315 1323 1316 # DO NOT REMOVE - used in saving and loading panel states. … … 1339 1332 entry = get_content(location, node) 1340 1333 if entry is not None and entry.text is not None: 1341 exec "storage.%s = entry.text.strip()" % variable1334 exec("storage.%s = entry.text.strip()" % variable) 1342 1335 1343 1336 # DO NOT REMOVE Called by outside packages: -
src/sas/sascalc/dataloader/readers/danse_reader.py
ra78a02f r574adc7 14 14 import math 15 15 import os 16 import logging 17 16 18 import numpy as np 17 import logging 18 from sas.sascalc.dataloader.data_info import plottable_2D, DataInfo, Detector19 from sas.sascalc.dataloader.manipulations import reader2D_converter20 from sas.sascalc.dataloader.file_reader_base_class import FileReader21 from sas.sascalc.dataloader.loader_exceptions import FileContentsException, DataReaderException19 20 from ..data_info import plottable_2D, DataInfo, Detector 21 from ..manipulations import reader2D_converter 22 from ..file_reader_base_class import FileReader 23 from ..loader_exceptions import FileContentsException, DataReaderException 22 24 23 25 logger = logging.getLogger(__name__) -
src/sas/sascalc/dataloader/readers/sesans_reader.py
rbe43448 r574adc7 6 6 Jurrian Bakker 7 7 """ 8 import os 9 8 10 import numpy as np 9 import os 10 from sas.sascalc.dataloader.file_reader_base_class import FileReader11 from sas.sascalc.dataloader.data_info import plottable_1D, DataInfo12 from sas.sascalc.dataloader.loader_exceptions import FileContentsException, DataReaderException11 12 from ..file_reader_base_class import FileReader 13 from ..data_info import plottable_1D, DataInfo 14 from ..loader_exceptions import FileContentsException, DataReaderException 13 15 14 16 # Check whether we have a converter available -
src/sas/sascalc/dataloader/readers/tiff_reader.py
r959eb01 r574adc7 2 2 #This software was developed by the University of Tennessee as part of the 3 3 #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) 4 #project funded by the US National Science Foundation. 4 #project funded by the US National Science Foundation. 5 5 #See the license text in license.txt 6 6 #copyright 2008, University of Tennessee … … 31 31 ## Extension 32 32 ext = ['.tif', '.tiff'] 33 33 34 34 def read(self, filename=None): 35 35 """ 36 36 Open and read the data in a file 37 37 38 38 :param file: path of the file 39 39 """ … … 44 44 except: 45 45 msg = "tiff_reader: could not load file. Missing Image module." 46 raise RuntimeError , msg47 46 raise RuntimeError(msg) 47 48 48 # Instantiate data object 49 49 output = Data2D() 50 50 output.filename = os.path.basename(filename) 51 51 52 52 # Read in the image 53 53 try: 54 54 im = Image.open(filename) 55 55 except: 56 raise RuntimeError , "cannot open %s"%(filename)56 raise RuntimeError("cannot open %s"%(filename)) 57 57 data = im.getdata() 58 58 … … 61 61 output.err_data = np.zeros([im.size[0], im.size[1]]) 62 62 output.mask = np.ones([im.size[0], im.size[1]], dtype=bool) 63 63 64 64 # Initialize 65 65 x_vals = [] … … 69 69 for i_x in range(im.size[0]): 70 70 x_vals.append(i_x) 71 71 72 72 itot = 0 73 73 for i_y in range(im.size[1]): … … 80 80 logger.error("tiff_reader: had to skip a non-float point") 81 81 continue 82 82 83 83 # Get bin number 84 84 if math.fmod(itot, im.size[0]) == 0: … … 87 87 else: 88 88 i_x += 1 89 89 90 90 output.data[im.size[1] - 1 - i_y][i_x] = value 91 91 92 92 itot += 1 93 93 94 94 output.xbins = im.size[0] 95 95 output.ybins = im.size[1] … … 102 102 output.ymin = 0 103 103 output.ymax = im.size[0] - 1 104 104 105 105 # Store loading process information 106 106 output.meta_data['loader'] = self.type_name -
src/sas/sascalc/dataloader/readers/xml_reader.py
rcd57c7d4 r574adc7 16 16 17 17 import logging 18 18 19 from lxml import etree 19 20 from lxml.builder import E 21 20 22 from sas.sascalc.dataloader.file_reader_base_class import FileReader 21 23 … … 151 153 Converts an etree element into a string 152 154 """ 153 return etree.tostring(elem, pretty_print=pretty_print, \155 return etree.tostring(elem, pretty_print=pretty_print, 154 156 encoding=encoding) 155 157
Note: See TracChangeset
for help on using the changeset viewer.