fosanalysis
A framework to evaluate distributed fiber optic sensor data
Loading...
Searching...
No Matches
filehandler.py
Go to the documentation of this file.
1r"""
2Contains class implementations to get file content of ODiSI generated data.
3Depending on the file extension the according file reader will be called.
4The FileHandler reads all data including metadata, tare, x-axis and strain.
5
6\author Anett Kielreiter
7\date 2024
8"""
9
10import os
11from datetime import datetime
12from collections.abc import Generator
13
14from . import filereader
15
16
18 r"""
19 File handler class to get measurement data from sensors.
20 Currently raw data and exported TSV files from
21 ODiSI 6100 series interrogators by Luna Inc LunaInnovations2020
22 are supported, see \cite LunaInnovations2020.
23 """
24 def __init__(self, filename: str, chunk_size: int = 1000):
25 r"""
26 Provides the handler for parsing measuring data files.
27 Different types of files are supported (atm *.dat and *.tsv )
28 \param filename Path to file to get measurement data.
29 \param chunk_size \copybrief chunk_size For more, see \ref chunk_size.
30 """
31
32 self.reader = None
33
35 self.metadata = None
36
38 self.sensors = None
39
44 self.chunk_size = chunk_size
45
46 ext = os.path.splitext(filename)[1].lower()
47 if ext == ".tsv":
48 self.reader = filereader.TsvReader(filename)
49 elif ext == ".dat":
50 self.reader = filereader.DatReader(filename)
51 else:
52 raise ValueError("No valid file format")
53
54 self.sensors, self.metadata = self.reader.read_meta_infos()
55
56 def close_file(self):
57 r"""
58 Close the handle to the file and reset all function variables.
59 """
60 if self.reader is not None:
61 self.reader.file.close()
62 self.reader = None
63 self.metadata = None
64 self.sensors = None
65
66#region Functions to get measurements (All, parts or by gage/segment names)
67
69 self,
70 channel: int = None,
71 start_time: datetime = None,
72 end_time: datetime = None,
73 segments_gages=None
74 ) -> tuple[list, dict]:
75 r"""
76 Reads all measurement files and returns the strain values, x_axis,
77 tare and timestamps filtered by given parameters.
78 The values are read in blocks to avoid memory issues.
79 The values will be split to gages/segments,
80 if segments_gages are given.
81 \param channel Selected channel of the file, defines the sensor.
82 \param start_time Read measurements from given timestamp.
83 \param end_time Read measurements till given timestamp.
84 \param segments_gages String or list of strings of gages/segments.
85 \return List of available timestamps.
86 \return Data of measurement (strain, xaxis, tare) as dict.
87 If no segment/gage selected, the dict contains ['All'] key.
88 """
89 timestamps = []
90 meas_dict = {}
91 if channel is not None:
92 sensor = next((x for x in self.sensors if x.channel == channel), None)
93 if sensor is None:
94 return [], {}
95 else:
96 sensor = self.sensors[0]
97 if segments_gages is not None and isinstance(segments_gages, str):
98 segments_gages = [segments_gages]
99
100 self.reader.file.seek(0) # reset the file pointer
101 for times, strain in self.yield_measurements_in_chunks(
102 self.chunk_size,
103 channel,
104 start_time,
105 end_time):
106 if segments_gages is not None:
107 self._split_to_gages_segments(meas_dict, sensor,
108 segments_gages, strain)
109 else:
110 if 'All' not in meas_dict:
111 meas_dict['All'] = {}
112 if meas_dict['All'].get('x_axis', None) is None:
113 meas_dict['All']['strain'] = strain
114 meas_dict['All']['x_axis'] = sensor.x_axis
115 meas_dict['All']['tare'] = sensor.tare
116 else:
117 meas_dict['All']['strain'].extend(strain)
118 timestamps.extend(times)
119
120 return timestamps, meas_dict
121
122 def yield_dat_file_values(self) -> Generator[dict]:
123 r"""
124 Generator to iterate over all .dat files of current measurement.
125 Reads all values of the file and separate it to the channels.
126 \return Dict with all measurement data (timestamps, strain) by channel.
127 """
128 if not self.reader.measurement_files_info:
129 return {}
130
131 for data_file_info in self.reader.measurement_files_info:
132 data_file = os.path.join(os.path.dirname(self.reader.file.name), data_file_info['filename'])
133 data_dict = self.reader.read_dat_file(data_file)
134 yield data_dict
135
137 self,
138 chunk_size: int = None,
139 channel: int = None,
140 start_time: datetime = None,
141 end_time: datetime = None
142 ) -> Generator[list, list]:
143 r"""
144 Generator to iterate over the whole file
145 and yields data with size of chunk.
146 Returns lists of timestamps and strain values.
147 \param chunk_size Maximum number of lines to read in lists, see \ref chunk_size.
148 \param channel Selected channel of the file, defines the sensor.
149 \param start_time Read measurements from given timestamp.
150 \param end_time Read measurements till given timestamp.
151 \return List of available timestamps.
152 \return Strain data as list.
153 """
154 timestamps = []
155 strain = []
156 chunk_size = self.chunk_size if chunk_size is None else chunk_size
157 if channel is None:
158 channel = self.sensors[0].channel
159
160 self.reader.file.seek(0) # reset the file pointer
161 for entry in self.reader.read_next_measurement(start_time=start_time,
162 end_time=end_time,
163 channel=channel):
164 timestamps.append(entry[0])
165 strain.append(entry[1])
166
167 if len(timestamps) >= chunk_size:
168 yield timestamps, strain
169 timestamps = []
170 strain = []
171 if len(timestamps) > 0:
172 yield timestamps, strain
173
175 self,
176 segments_gages,
177 chunk_size: int = None,
178 channel: int = None,
179 start_time: datetime = None,
180 end_time: datetime = None
181 ) -> Generator[list, dict]:
182 r"""
183 Generator to iterate over a dictionary with all strain data,
184 x-axis and tare for the given segment or gage names of chunk_size.
185 Separately from it, it returns the timestamps of file.
186 \param segments_gages String or list of gage/segment name(s).
187 \param chunk_size Number of lines to be read in one chunk as int.
188 \param channel Selected channel of the file, defines the sensor.
189 \param start_time Read measurements from given timestamp.
190 \param end_time Read measurements till given timestamp.
191 \return timestamps List of available timestamps.
192 \return meas_dict Dict with all measurement data of segment/gage.
193 """
194 chunk_size = self.chunk_size if chunk_size is None else chunk_size
195 if channel is not None:
196 sensor = next((x for x in self.sensors if x.channel == channel),
197 None)
198 if sensor is None:
199 return [], {}
200 else:
201 sensor = self.sensors[0]
202 if isinstance(segments_gages, str):
203 segments_gages = [segments_gages]
204
205 for timestamps, strain in self.yield_measurements_in_chunks(channel,
206 chunk_size,
207 start_time,
208 end_time):
209 meas_dict = {}
210 self._split_to_gages_segments(meas_dict, sensor,
211 segments_gages, strain)
212 yield timestamps, meas_dict
213
215 self,
216 meas_dict: dict,
217 sensor: filereader.SensorInfo,
218 segments_gages,
219 strain: list
220 ):
221 r"""
222 Fill the meas_dict dictionary with all strain data, x-axis and tare
223 for the given segment or gage names.
224 \param meas_dict Dict with all measurement data of segment/gage.
225 \param sensor Sensor object with axis, tare, etc.
226 \param segments_gages String or list of gage/segment name(s).
227 \param strain Strain values as list of arrays.
228 """
229 for name in segments_gages:
230 if name not in meas_dict:
231 meas_dict[name] = {}
232 start, end = self._get_range_of_segment_gage(name, sensor)
233 if start is None:
234 break
235 if meas_dict[name].get('x_axis', None) is None:
236 meas_dict[name]['strain'] = ([i[start:end] for i in strain])
237 meas_dict[name]['x_axis'] = sensor.x_axis[start:end]
238 meas_dict[name]['tare'] = sensor.tare[start:end]
239 else:
240 meas_dict[name]['strain'].extend(
241 ([i[start:end] for i in strain]))
242
244 self,
245 name: str,
247 ) -> tuple[int, int]:
248 r"""
249 Get start and end index of gage/segment.
250 \param name Name of the segment/gage.
251 \param sensor Sensor object with axis, tare, etc.
252 \return start Start index of segment or index of gage.
253 \return end End index of segment or index+1 if gage.
254 """
255 if name in sensor.gages:
256 target = sensor.gages
257 elif name in sensor.segments:
258 target = sensor.segments
259 else:
260 return None, None
261 start = target[name]['index']
262
263 if name in sensor.segments:
264 end = start + target[name]['length']
265 else:
266 end = start + 1
267 return start, end
File handler class to get measurement data from sensors.
sensors
List of SensorInfo object available in file, contains all sensor specific infos (incl.
__init__(self, str filename, int chunk_size=1000)
Provides the handler for parsing measuring data files.
Generator[list, dict] yield_gages_segments_in_chunks(self, segments_gages, int chunk_size=None, int channel=None, datetime start_time=None, datetime end_time=None)
Generator to iterate over a dictionary with all strain data, x-axis and tare for the given segment or...
chunk_size
Size of chunks when reading multiple readings at once.
metadata
Dictionary, which stores metadata for each file imported.
tuple[list, dict] get_measurements(self, int channel=None, datetime start_time=None, datetime end_time=None, segments_gages=None)
Reads all measurement files and returns the strain values, x_axis, tare and timestamps filtered by gi...
_split_to_gages_segments(self, dict meas_dict, filereader.SensorInfo sensor, segments_gages, list strain)
Fill the meas_dict dictionary with all strain data, x-axis and tare for the given segment or gage nam...
Generator[list, list] yield_measurements_in_chunks(self, int chunk_size=None, int channel=None, datetime start_time=None, datetime end_time=None)
Generator to iterate over the whole file and yields data with size of chunk.
tuple[int, int] _get_range_of_segment_gage(self, str name, filereader.SensorInfo sensor)
Get start and end index of gage/segment.
Generator[dict] yield_dat_file_values(self)
Generator to iterate over all .dat files of current measurement.
close_file(self)
Close the handle to the file and reset all function variables.
reader
Holds the file reader for selected file.
File reader class for the .dat measurement files recorded by the ODiSI 6100 series interrogators by L...
Class to hold sensor specific data (name, serial number, ...).
Definition filereader.py:24
File reader class for the .tsv measurement files exported by the ODiSI 6100 series interrogators by L...