Changeset da8bb53 in sasview for src/sas/sascalc
- Timestamp:
- Apr 17, 2017 4:30:10 PM (8 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- ad92c5a
- Parents:
- 8ffafd1
- git-author:
- Jeff Krzywon <krzywon@…> (04/17/17 16:30:10)
- git-committer:
- krzywon <krzywon@…> (04/17/17 16:30:10)
- Location:
- src/sas/sascalc/dataloader
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/dataloader/file_reader_base_class.py
rb09095a rda8bb53 10 10 from abc import abstractmethod 11 11 from loader_exceptions import NoKnownLoaderException, FileContentsException,\ 12 DataReaderException 12 DataReaderException, DefaultReaderException 13 13 from data_info import Data1D, Data2D, DataInfo, plottable_1D, plottable_2D,\ 14 14 combine_data_info_with_plottable … … 47 47 if os.path.isfile(filepath): 48 48 basename, extension = os.path.splitext(os.path.basename(filepath)) 49 self.extension = extension.lower() 49 50 # If the file type is not allowed, return nothing 50 if extension in self.ext or self.allow_all:51 if self.extension in self.ext or self.allow_all: 51 52 # Try to load the file, but raise an error if unable to. 52 53 try: 53 self. unit_converter()54 self.load_unit_converter() 54 55 self.f_open = open(filepath, 'rb') 55 56 self.get_file_contents() 56 57 self.sort_one_d_data() 57 except RuntimeError: 58 # Reader specific errors 59 # TODO: Give a specific error. 60 pass 58 except FileContentsException as e: 59 self.handle_error_message(e.message) 61 60 except OSError as e: 62 61 # If the file cannot be opened … … 64 63 msg += e.message 65 64 self.handle_error_message(msg) 66 except Exception as e:67 # Handle any other generic error68 # TODO: raise or log?69 raise70 65 finally: 66 # Close the file handle if it is open 71 67 if not self.f_open.closed: 72 68 self.f_open.close() … … 98 94 self.output.append(data_obj) 99 95 100 def unit_converter(self):96 def load_unit_converter(self): 101 97 """ 102 98 Generic unit conversion import … … 134 130 self.output = final_list 135 131 132 def set_all_to_none(self): 133 """ 134 Set all mutable values to None for error handling purposes 135 """ 136 self.current_dataset = None 137 self.current_datainfo = None 138 self.output = [] 139 136 140 @staticmethod 137 141 def splitline(line): -
src/sas/sascalc/dataloader/loader.py
r278ddee rda8bb53 25 25 import logging 26 26 import time 27 import mimetypes 27 28 from zipfile import ZipFile 28 29 from sas.sascalc.data_util.registry import ExtensionRegistry 29 30 # Default readers are defined in the readers sub-module 30 31 import readers 31 from loader_exceptions import NoKnownLoaderException, FileContentsException 32 from loader_exceptions import NoKnownLoaderException, FileContentsException,\ 33 DefaultReaderException 32 34 from readers import ascii_reader 33 35 from readers import cansas_reader … … 75 77 ascii_loader = ascii_reader.Reader() 76 78 return ascii_loader.read(path) 77 except FileContentsException:78 pass # try the cansas XML reader79 except DefaultReaderException: 80 pass # Loader specific error to try the cansas XML reader 79 81 try: 80 82 cansas_loader = cansas_reader.Reader() 81 83 return cansas_loader.read(path) 84 except DefaultReaderException: 85 pass # Loader specific error to try the cansas NeXuS reader 82 86 except FileContentsException: 83 pass # try the cansas NeXuS reader 87 # TODO: Handle errors properly 88 pass 89 except Exception as csr: 90 # TODO: Modify cansas reader to throw DefaultReaderException 91 pass 84 92 try: 85 93 cansas_nexus_loader = cansas_reader_HDF5.Reader() 86 94 return cansas_nexus_loader.read(path) 87 except FileContentsException:95 except DefaultReaderException: 88 96 logging.errors("No default loader can load the data") 89 97 # No known reader available. Give up and throw an error … … 92 100 msg += "Traceback:\n%s" % e.message 93 101 raise NoKnownLoaderException, msg 102 except FileContentsException: 103 # TODO: Handle error(s) properly 104 pass 94 105 95 106 def find_plugins(self, dir): -
src/sas/sascalc/dataloader/loader_exceptions.py
r270c882b rda8bb53 23 23 24 24 25 class DefaultReaderException(Exception): 26 """ 27 Exception for files with no associated reader. This should be thrown by 28 default readers only to tell Loader to try the next reader. 29 """ 30 def __init__(self, e): 31 self.message = e 32 33 25 34 class DataReaderException(Exception): 26 35 """ -
src/sas/sascalc/dataloader/readers/ascii_reader.py
r8ffafd1 rda8bb53 17 17 from sas.sascalc.dataloader.file_reader_base_class import FileReader 18 18 from sas.sascalc.dataloader.data_info import DataInfo, plottable_1D 19 from sas.sascalc.dataloader.loader_exceptions import FileContentsException,\ 20 DefaultReaderException 19 21 20 22 logger = logging.getLogger(__name__) … … 110 112 line_no += 1 111 113 except ValueError: 114 # ValueError is raised when non numeric strings conv. to float 112 115 # It is data and meet non - number, then stop reading 113 116 if is_data: … … 121 124 # Reset # of lines of data candidates 122 125 candidate_lines = 0 123 except Exception:124 # Handle any unexpected exceptions125 raise126 126 127 127 if not is_data: 128 # TODO: Check file extension - primary reader, throw error. 129 # TODO: Secondary check, pass and try next reader 130 msg = "ascii_reader: x has no data" 131 raise RuntimeError(msg) 128 self.set_all_to_none() 129 if self.extension in self.ext: 130 msg = "ASCII Reader error: Fewer than five Q data points found " 131 msg += "in {}.".format(filepath) 132 raise FileContentsException(msg) 133 else: 134 msg = "ASCII Reader could not load the file {}".format(filepath) 135 raise DefaultReaderException(msg) 132 136 # Sanity check 133 137 if has_error_dy and not len(self.current_dataset.y) == \ 134 138 len(self.current_dataset.dy): 135 msg = "ascii_reader: y and dy have different length" 136 raise RuntimeError(msg) 139 msg = "ASCII Reader error: Number of I and dI data points are" 140 msg += " different in {}.".format(filepath) 141 # TODO: Add error to self.current_datainfo.errors instead? 142 self.set_all_to_none() 143 raise FileContentsException(msg) 137 144 if has_error_dx and not len(self.current_dataset.x) == \ 138 145 len(self.current_dataset.dx): 139 msg = "ascii_reader: y and dy have different length" 140 raise RuntimeError(msg) 141 # If the data length is zero, consider this as 142 # though we were not able to read the file. 143 if len(self.current_dataset.x) < 1: 144 raise RuntimeError("ascii_reader: could not load file") 146 msg = "ASCII Reader error: Number of Q and dQ data points are" 147 msg += " different in {}.".format(filepath) 148 # TODO: Add error to self.current_datainfo.errors instead? 149 self.set_all_to_none() 150 raise FileContentsException(msg) 145 151 146 # Data152 # Remove any point where Q == 0 147 153 x = self.current_dataset.x 148 154 self.current_dataset.x = self.current_dataset.x[x != 0]
Note: See TracChangeset
for help on using the changeset viewer.