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