3Contains functionality for resizing data:
4- Reduce 2D to 1D (or 0D): \ref Aggregate.
5- Combining several readings into one using aggregate functions: \ref Downsampler.
6- Changing spatial or temporal spacing of data using interpolation: \ref Resampler.
12from abc
import abstractmethod
24 Base class for algorithms to replace/remove missing data with plausible values.
25 The sub-classes will take data containing dropouts (`NaN`s) and will return dropout-free data.
26 This is done by replacing the dropouts by plausible values and/or removing dropouts.
27 Because the shape of the arrays might be altered, \ref run() expects
29 - `x`: array of the the positional data along the sensor.
30 - `y`: array of the time stamps, and
31 - `z`: array of the strain data.
36 Change the dimension of an array using aggregate functions (such as
37 mean, median, min or max).
38 It can be used to reduce 2D to 1D strain data.
41 method: str
or callable,
43 timespace: str =
"1d_space",
46 Construct an instance of the class.
47 \param method A string or callable representing the method.
48 \param module The module (default is numpy).
49 \param timespace \copybrief timespace For more, see \ref timespace.
50 \param *args Additional positional arguments, will be passed to the superconstructor.
51 \param **kwargs Additional keyword arguments, will be passed to the superconstructor.
68 self.
setup(method, module)
70 method: str
or callable,
73 Set the \ref kernel method for data processing, which can be
74 either a string representing function name in a given namespace
75 \ref module or a custom callable function.
76 If `"method"` is a string, retrieve the corresponding function from the specified module.
77 If `"method"` is a custom callable function, directly set it as the processing method.
78 \param method \copydoc method
79 \param module \copydoc module
89 self.
kernel = method
if callable(method)
else getattr(module, method)
94 timespace: str =
None,
95 make_copy: bool =
True,
96 *args, **kwargs) -> np.array:
98 Reduce a 2D array to a 1D array using aggregate functions.
99 The aggregate function is implemented in \ref reduce().
100 The array of the crushed axis is set to `np.array(None)`.
101 \param x Array of measuring point positions.
102 \param y Array of time stamps.
103 \param z Array of strain data in accordance to `x` and `y`.
104 \param timespace \copybrief timespace For more, see \ref timespace.
105 \param make_copy Switch, whether a deepcopy of the passed data should be done.
107 \param *args Additional positional arguments to customize the behaviour.
108 \param **kwargs Additional keyword arguments to customize the behaviour.
109 \return Returns a tuple like `(x, y, z)`.
110 They correspond to the input variables of the same name.
111 The resulting shape of the return values depending on \ref
112 timespace is as follows:
114 |`"timespace"`| x | y | z |
115 |:----------: |:--------------:|:--------------:|:----------------------:|
116 |`"1d_space"` | x |`np.array(None)`|1d array, same size as x|
117 |`"1d_time"` |`np.array(None)`| y |1d array, same size as y|
118 | `"2d"` |`np.array(None)`|`np.array(None)`| np.array(float) |
120 x, y, z = super().
run(x, y, z, make_copy=make_copy, *args, **kwargs)
121 timespace = timespace
if timespace
is not None else self.
timespace
126 if timespace.lower() ==
"1d_space":
129 elif timespace.lower() ==
"1d_time":
132 reduced_array = self.
reduce(z, axis, *args, **kwargs).flatten()
133 return x, y, reduced_array
135 raise ValueError(
"Array is neither 1D nor 2D.")
139 *args, **kwargs) -> np.array:
141 Reduce the given array using the \ref kernel funcition.
142 \param data Array of data with functional data according to `data`.
143 \param axis Axis in which the data should be consolidated.
144 This is in accordance with the `numpy` axis definitions.
145 \param *args Additional positional arguments, passed to \ref kernel.
146 \param **kwargs Additional keyword arguments, passed to \ref kernel.
147 \return Returns an array, where multiple readings are combined to one single array.
149 return self.
kernel(data, axis=axis, *args, **kwargs)
153 Object, for cropping data sets and saving the preset.
156 start_pos: float =
None,
157 end_pos: float =
None,
158 length: float =
None,
159 offset: float =
None,
162 Construct an instance of the class.
163 \param start_pos \copydoc start_pos
164 \param end_pos \copydoc end_pos
165 \param length \copydoc length
166 \param offset \copydoc offset
167 \param *args Additional positional arguments, will be passed to the superconstructor.
168 \param **kwargs Additional keyword arguments, will be passed to the superconstructor.
187 start_pos: float =
None,
188 end_pos: float =
None,
189 length: float =
None,
190 offset: float =
None,
191 *args, **kwargs) -> tuple:
193 This is a wrapper around \ref cropping.cropping() which.
194 \param x Array of measuring point positions.
195 \param y Array of time stamps.
196 \param z Array of strain data in accordance to `x` and `y`.
197 \param start_pos The starting position \f$s\f$ specifies the length of the sensor, before entering the measurement area.
198 Defaults to `None` (no data is removed at the beginning).
199 \param end_pos The end position \f$s\f$ specifies the length of the sensor, when leaving the measurement area.
200 If both `length` and `end_pos` are provided, `end_pos` takes precedence.
201 Defaults to `None` (no data is removed at the end).
202 \param length Length of the data excerpt. If set, it is used to determine the `end_pos`.
203 If both `length` and `end_pos` are provided, `end_pos` takes precedence.
204 \param offset Before cropping, \f$x\f$ data is shifted by the offset \f$o\f$, such that \f$x \gets x + o\f$, defaults to `0`.
205 \param *args Additional positional arguments, passed to \ref cropping.cropping().
206 \param **kwargs Additional keyword arguments, passed to \ref cropping.cropping().
208 start_pos = start_pos
if start_pos
is not None else self.
start_pos
209 end_pos = end_pos
if end_pos
is not None else self.
end_pos
210 length = length
if length
is not None else self.
length
211 offset = offset
if offset
is not None else self.
offset
212 x_cropped, z_cropped = cropping.cropping(x_values=x,
219 return x_cropped, y, z_cropped
223 Class for reducing strain data size while keeping the data loss small
224 by combining several values into one value.
225 To achieve this, windows with a specified size (see \ref radius) are
226 placed on the original data in a regular grid of fixed \ref step_size
227 and a fixed \ref start_pixel.
228 Each window is then aggregated to one value, see \ref Aggregate.
229 In contrast to \ref Resampler, the grid is specified by array indices.
232 aggregator: Aggregate,
234 start_pixel: int =
None,
235 step_size: int =
None,
238 Initialize the down sampler.
239 This method can be extended for any necessary initialization logic.
240 \param aggregator An instance of `Aggregate` used for aggregation.
241 \param radius \copydoc radius
242 \param start_pixel \copydoc start_pixel
243 \param step_size \copydoc step_size
244 \param *args Additional positional arguments, will be passed to the superconstructor.
245 \param **kwargs Additional keyword arguments, will be passed to the superconstructor.
274 radius: tuple =
None,
275 start_pixel: tuple =
None,
276 step_size: tuple =
None,
279 This method downsamples 2D and 1D Strain data using specified parameters.
280 \param x Array of x-axis values.
281 \param y Array of time-axis values.
282 \param z 2D array of strain data.
283 \param radius \copydoc radius
284 \param start_pixel \copydoc start_pixel
285 \param step_size \copydoc step_size
286 \return Tuple containing `(target_x_points, target_time_points, new_z)`.
287 \retval target_x_points The x-axis values after downsampling.
288 \retval target_time_points The time-axis values after downsampling.
289 \retval new_z Array of downsampled strain data.
295 radius = radius
if radius
is not None else self.
radius
296 start_pixel = start_pixel
if start_pixel
is not None else self.
start_pixel
297 step_size = step_size
if step_size
is not None else self.
step_size
299 moving_params = windows.determine_moving_parameters(
300 z, radius, start_pixel, step_size
302 orig_index_lists, radius, start_pixel, step_size = moving_params
303 target_x = x[orig_index_lists[0]]
if x
is not None else None
305 target_time = y[orig_index_lists[1]]
if y
is not None else None
307 target_time = y[orig_index_lists[0]]
if y
is not None else None
309 raise ValueError(
"Invalid input z.ndim defined")
311 new_z = np.zeros([len(l)
for l
in orig_index_lists], dtype=float)
313 for orig_pixel, target_pixel, window_content
in windows.moving(z, radius, start_pixel, step_size):
314 downsampled_strain = self.
aggregator.reduce(window_content, axis=
None)
315 new_z[target_pixel] = downsampled_strain
316 return target_x, target_time, new_z
320 Class for resampling one-dimensional or two-dimensional strain data.
321 In contrast, to \ref Downsampler, the target points are given in
322 the respective dimensions (`datetime.datetime` objects in time; sensor
323 coordinates in space) and irregular spacing along an axis is possible.
326 target_x: np.array =
None,
327 target_time: np.array =
None,
329 method_kwargs: dict =
None,
330 timespace: str =
"1d_space",
333 Construct a Resampler instance.
334 \param target_x \copydoc target_x
335 \param target_time \copydoc target_time
336 \param method \copydoc method
337 \param method_kwargs \copydoc method_kwargs
338 \param timespace \copydoc timespace
339 \param *args Additional positional arguments, will be passed to the superconstructor.
340 \param **kwargs Additional keyword arguments, will be passed to the superconstructor.
342 super().
__init__(timespace=timespace, *args, **kwargs)
362 *args, **kwargs) -> tuple:
364 Resamples (by interpolating) one-dimensional data.
365 \param x Array of measuring point positions or time stamps
366 \param z Array of strain data in accordance to `x`.
367 \param *args Additional positional arguments, ignored.
368 \param **kwargs Additional keyword arguments, ignored.
369 \return Returns a tuple like `(target_x, target_z)`.
370 \retval target_x Array of target points (space or time).
371 \retval target_z Array of resampled strain.
375 x = misc.datetime_to_timestamp(x)
376 target_coord = misc.datetime_to_timestamp(self.
target_time)
382 if target_coord
is None:
383 raise ValueError(
"Target coordinates are `None`, must be set before resampling.")
384 method = self.
method if self.
method is not None else "interp1d"
385 target_z = scipy_interpolate1d(x, z, target_coord, method=method, **self.
method_kwargs)
386 return target_x, target_z
391 *args, **kwargs) -> tuple:
393 Resample a strain array using both spatial and temporal coordinates.
394 \param x: Original spatial (x-coordinate) data.
395 \param y: Original temporal (y-coordinate) data.
396 \param z: Original strain values.
397 \param *args: Additional positional arguments, ignored.
398 \param **kwargs: Additional keyword arguments, ignored.
399 \return Returns a tuple like `(x, y, z)`.
400 \retval x This is the \ref target_x
401 \retval y \ref target_time
402 \retval z Resampled strain array, according to \ref target_x and
406 raise ValueError(
"Target x and time points must be set before resampling.")
408 y = misc.datetime_to_timestamp(y)
411 target_time = misc.datetime_to_timestamp(self.
target_time)
413 method = self.
method if self.
method is not None else "linear"
414 interpolated_strain = scipy.interpolate.interpn(
416 xi=(target_time[:, np.newaxis], self.
target_x),
Abstract base class for preprocessing classes.
Abstract base class for preprocessing task classes.
Change the dimension of an array using aggregate functions (such as mean, median, min or max).
np.array reduce(self, np.array data, int axis, *args, **kwargs)
Reduce the given array using the kernel funcition.
__init__(self, str or callable method, module=np, str timespace="1d_space", *args, **kwargs)
Construct an instance of the class.
module
A Python module or namespace in which method is looked up when method is not a callable.
np.array run(self, np.array x, np.array y, np.array z, str timespace=None, bool make_copy=True, *args, **kwargs)
Reduce a 2D array to a 1D array using aggregate functions.
timespace
Setting, how to compact the 2d array.
kernel
Callable function used to aggregate the data.
setup(self, str or callable method, module=np)
Set the kernel method for data processing, which can be either a string representing function name in...
method
Could be either a callable (function object) or a string representing a function name in the namespac...
Object, for cropping data sets and saving the preset.
length
Length of the data excerpt.
tuple run(self, np.array x, np.array y, np.array z, float start_pos=None, float end_pos=None, float length=None, float offset=None, *args, **kwargs)
This is a wrapper around cropping.cropping() which.
start_pos
The starting position specifies the length of the sensor, before entering the measurement area.
__init__(self, float start_pos=None, float end_pos=None, float length=None, float offset=None, *args, **kwargs)
Construct an instance of the class.
end_pos
The end position specifies the length of the sensor, when leaving the measurement area.
offset
Before cropping, data is shifted by the offset , such that , defaults to 0.
Class for reducing strain data size while keeping the data loss small by combining several values int...
step_size
Step size how far the window moves in one step.
radius
Inradius of the window's rectangle.
__init__(self, Aggregate aggregator, int radius=None, int start_pixel=None, int step_size=None, *args, **kwargs)
Initialize the down sampler.
aggregator
Aggregator to use, see Aggregate.
tuple run(self, np.array x, np.array y, np.array z, tuple radius=None, tuple start_pixel=None, tuple step_size=None)
This method downsamples 2D and 1D Strain data using specified parameters.
start_pixel
Index of the first window's central pixel.
Class for resampling one-dimensional or two-dimensional strain data.
target_time
Points in time, where strain values should be resampled.
target_x
Points in space, where strain values should be resampled.
method_kwargs
Additional keyword arguments for the interpolation function.
tuple _run_1d(self, np.array x, np.array z, *args, **kwargs)
Resamples (by interpolating) one-dimensional data.
method
Name of the interpolation method used.
tuple _run_2d(self, np.array x, np.array y, np.array z, *args, **kwargs)
Resample a strain array using both spatial and temporal coordinates.
__init__(self, np.array target_x=None, np.array target_time=None, str method=None, dict method_kwargs=None, str timespace="1d_space", *args, **kwargs)
Construct a Resampler instance.
Base class for algorithms to replace/remove missing data with plausible values.
Contains functionality for interpolating data.
Contains utility modules with general pupose.