source: sasview/src/sas/sascalc/dataloader/file_reader_base_class.py @ 26183bf

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalcmagnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 26183bf was 26183bf, checked in by Paul Kienzle <pkienzle@…>, 7 years ago

flow data reads through a single interface so we can control bytes to unicode conversion

  • Property mode set to 100644
File size: 10.0 KB
Line 
1"""
2This is the base file reader class most file readers should inherit from.
3All generic functionality required for a file loader/reader is built into this
4class
5"""
6
7import os
8import re
9import logging
10from abc import abstractmethod
11
12import numpy as np
13from .loader_exceptions import NoKnownLoaderException, FileContentsException,\
14    DataReaderException, DefaultReaderException
15from .data_info import Data1D, Data2D, DataInfo, plottable_1D, plottable_2D,\
16    combine_data_info_with_plottable
17
18logger = logging.getLogger(__name__)
19
20
21class FileReader(object):
22    # List of Data1D and Data2D objects to be sent back to data_loader
23    output = []
24    # Current plottable_(1D/2D) object being loaded in
25    current_dataset = None
26    # Current DataInfo object being loaded in
27    current_datainfo = None
28    # String to describe the type of data this reader can load
29    type_name = "ASCII"
30    # Wildcards to display
31    type = ["Text files (*.txt|*.TXT)"]
32    # List of allowed extensions
33    ext = ['.txt']
34    # Bypass extension check and try to load anyway
35    allow_all = False
36    # Able to import the unit converter
37    has_converter = True
38    # Open file handle
39    f_open = None
40    # Default value of zero
41    _ZERO = 1e-16
42
43    def read(self, filepath):
44        """
45        Basic file reader
46
47        :param filepath: The full or relative path to a file to be loaded
48        """
49        if os.path.isfile(filepath):
50            basename, extension = os.path.splitext(os.path.basename(filepath))
51            self.extension = extension.lower()
52            # If the file type is not allowed, return nothing
53            if self.extension in self.ext or self.allow_all:
54                # Try to load the file, but raise an error if unable to.
55                try:
56                    self.f_open = open(filepath, 'rb')
57                    self.get_file_contents()
58
59                except DataReaderException as e:
60                    self.handle_error_message(e.message)
61                except OSError as e:
62                    # If the file cannot be opened
63                    msg = "Unable to open file: {}\n".format(filepath)
64                    msg += e.message
65                    self.handle_error_message(msg)
66                finally:
67                    # Close the file handle if it is open
68                    if not self.f_open.closed:
69                        self.f_open.close()
70                    if len(self.output) > 0:
71                        # Sort the data that's been loaded
72                        self.sort_one_d_data()
73                        self.sort_two_d_data()
74        else:
75            msg = "Unable to find file at: {}\n".format(filepath)
76            msg += "Please check your file path and try again."
77            self.handle_error_message(msg)
78
79        # Return a list of parsed entries that data_loader can manage
80        return self.output
81
82    def nextline(self):
83        """
84        Returns the next line in the file as a string.
85        """
86        #return self.f_open.readline()
87        return self.f_open.readline().decode()
88
89    def nextlines(self):
90        """
91        Returns the next line in the file as a string.
92        """
93        for line in self.f_open:
94            #yield line
95            yield line.decode()
96
97    def readall(self):
98        """
99        Returns the entire file as a string.
100        """
101        #return self.f_open.read()
102        return self.f_open.read().decode()
103
104    def handle_error_message(self, msg):
105        """
106        Generic error handler to add an error to the current datainfo to
107        propogate the error up the error chain.
108        :param msg: Error message
109        """
110        if len(self.output) > 0:
111            self.output[-1].errors.append(msg)
112        elif isinstance(self.current_datainfo, DataInfo):
113            self.current_datainfo.errors.append(msg)
114        else:
115            logger.warning(msg)
116
117    def send_to_output(self):
118        """
119        Helper that automatically combines the info and set and then appends it
120        to output
121        """
122        data_obj = combine_data_info_with_plottable(self.current_dataset,
123                                                    self.current_datainfo)
124        self.output.append(data_obj)
125
126    def sort_one_d_data(self):
127        """
128        Sort 1D data along the X axis for consistency
129        """
130        for data in self.output:
131            if isinstance(data, Data1D):
132                # Normalize the units for
133                data.x_unit = self.format_unit(data.x_unit)
134                data.y_unit = self.format_unit(data.y_unit)
135                # Sort data by increasing x and remove 1st point
136                ind = np.lexsort((data.y, data.x))
137                data.x = np.asarray([data.x[i] for i in ind]).astype(np.float64)
138                data.y = np.asarray([data.y[i] for i in ind]).astype(np.float64)
139                if data.dx is not None:
140                    if len(data.dx) == 0:
141                        data.dx = None
142                        continue
143                    data.dx = np.asarray([data.dx[i] for i in ind]).astype(np.float64)
144                if data.dxl is not None:
145                    data.dxl = np.asarray([data.dxl[i] for i in ind]).astype(np.float64)
146                if data.dxw is not None:
147                    data.dxw = np.asarray([data.dxw[i] for i in ind]).astype(np.float64)
148                if data.dy is not None:
149                    if len(data.dy) == 0:
150                        data.dy = None
151                        continue
152                    data.dy = np.asarray([data.dy[i] for i in ind]).astype(np.float64)
153                if data.lam is not None:
154                    data.lam = np.asarray([data.lam[i] for i in ind]).astype(np.float64)
155                if data.dlam is not None:
156                    data.dlam = np.asarray([data.dlam[i] for i in ind]).astype(np.float64)
157                if len(data.x) > 0:
158                    data.xmin = np.min(data.x)
159                    data.xmax = np.max(data.x)
160                    data.ymin = np.min(data.y)
161                    data.ymax = np.max(data.y)
162
163    def sort_two_d_data(self):
164        for dataset in self.output:
165            if isinstance(dataset, Data2D):
166                # Normalize the units for
167                dataset.x_unit = self.format_unit(dataset.Q_unit)
168                dataset.y_unit = self.format_unit(dataset.I_unit)
169                dataset.data = dataset.data.astype(np.float64)
170                dataset.qx_data = dataset.qx_data.astype(np.float64)
171                dataset.xmin = np.min(dataset.qx_data)
172                dataset.xmax = np.max(dataset.qx_data)
173                dataset.qy_data = dataset.qy_data.astype(np.float64)
174                dataset.ymin = np.min(dataset.qy_data)
175                dataset.ymax = np.max(dataset.qy_data)
176                dataset.q_data = np.sqrt(dataset.qx_data * dataset.qx_data
177                                         + dataset.qy_data * dataset.qy_data)
178                if dataset.err_data is not None:
179                    dataset.err_data = dataset.err_data.astype(np.float64)
180                if dataset.dqx_data is not None:
181                    dataset.dqx_data = dataset.dqx_data.astype(np.float64)
182                if dataset.dqy_data is not None:
183                    dataset.dqy_data = dataset.dqy_data.astype(np.float64)
184                if dataset.mask is not None:
185                    dataset.mask = dataset.mask.astype(dtype=bool)
186
187                if len(dataset.data.shape) == 2:
188                    n_rows, n_cols = dataset.data.shape
189                    dataset.y_bins = dataset.qy_data[0::int(n_cols)]
190                    dataset.x_bins = dataset.qx_data[:int(n_cols)]
191                dataset.data = dataset.data.flatten()
192
193    def format_unit(self, unit=None):
194        """
195        Format units a common way
196        :param unit:
197        :return:
198        """
199        if unit:
200            split = unit.split("/")
201            if len(split) == 1:
202                return unit
203            elif split[0] == '1':
204                return "{0}^".format(split[1]) + "{-1}"
205            else:
206                return "{0}*{1}^".format(split[0], split[1]) + "{-1}"
207
208    def set_all_to_none(self):
209        """
210        Set all mutable values to None for error handling purposes
211        """
212        self.current_dataset = None
213        self.current_datainfo = None
214        self.output = []
215
216    def remove_empty_q_values(self, has_error_dx=False, has_error_dy=False,
217                              has_error_dxl=False, has_error_dxw=False):
218        """
219        Remove any point where Q == 0
220        """
221        x = self.current_dataset.x
222        self.current_dataset.x = self.current_dataset.x[x != 0]
223        self.current_dataset.y = self.current_dataset.y[x != 0]
224        if has_error_dy:
225            self.current_dataset.dy = self.current_dataset.dy[x != 0]
226        if has_error_dx:
227            self.current_dataset.dx = self.current_dataset.dx[x != 0]
228        if has_error_dxl:
229            self.current_dataset.dxl = self.current_dataset.dxl[x != 0]
230        if has_error_dxw:
231            self.current_dataset.dxw = self.current_dataset.dxw[x != 0]
232
233    def reset_data_list(self, no_lines=0):
234        """
235        Reset the plottable_1D object
236        """
237        # Initialize data sets with arrays the maximum possible size
238        x = np.zeros(no_lines)
239        y = np.zeros(no_lines)
240        dx = np.zeros(no_lines)
241        dy = np.zeros(no_lines)
242        self.current_dataset = plottable_1D(x, y, dx, dy)
243
244    @staticmethod
245    def splitline(line):
246        """
247        Splits a line into pieces based on common delimeters
248        :param line: A single line of text
249        :return: list of values
250        """
251        # Initial try for CSV (split on ,)
252        toks = line.split(',')
253        # Now try SCSV (split on ;)
254        if len(toks) < 2:
255            toks = line.split(';')
256        # Now go for whitespace
257        if len(toks) < 2:
258            toks = line.split()
259        return toks
260
261    @abstractmethod
262    def get_file_contents(self):
263        """
264        Reader specific class to access the contents of the file
265        All reader classes that inherit from FileReader must implement
266        """
267        pass
Note: See TracBrowser for help on using the repository browser.