source: sasview/src/sas/sascalc/dataloader/file_reader_base_class.py @ 61f329f0

magnetic_scattrelease-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 61f329f0 was 61f329f0, checked in by krzywon, 7 years ago

Reset file reader class data state each time a new data file is loaded.

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