fosanalysis
A framework to evaluate distributed fiber optic sensor data
Loading...
Searching...
No Matches
repair.py
Go to the documentation of this file.
2r"""
3Contains class implementations, for strain function repair algorithms.
4Those can be used to attempt the reconstruction of more or less heavily destroyed strain profiles.
5
6\author Bertram Richter
7\date 2022
8"""
9
10from abc import abstractmethod
11import copy
12
13import numpy as np
14import scipy.interpolate
15
16from . import base
17from fosanalysis.utils.interpolation import scipy_interpolate1d
18
20 r"""
21 Base class for algorithms to replace/remove missing data with plausible values.
22 The sub-classes will take data containing dropouts (`NaN`s) and will return dropout-free data.
23 This is done by replacing the dropouts by plausible values and/or removing dropouts.
24 Because the shape of the arrays might be altered, \ref run() expects
25 and returns
26 - `x`: array of the the positional data along the sensor.
27 - `y`: array of the time stamps, and
28 - `z`: array of the strain data.
29 """
30
32 r"""
33 A filter, that removes any columns from a given number of data sets (matrix), that contain `not a number` entries.
34 """
35 def __init__(self,
36 axis: int = 0,
37 *args, **kwargs):
38 r"""
39 Construct an instance of the class.
40 \param axis \copydoc axis
41 \param *args Additional positional arguments, will be passed to the superconstructor.
42 \param **kwargs Additional keyword arguments will be passed to the superconstructor.
43 """
44 super().__init__(*args, **kwargs)
45
50 self.axis = axis
51 def run(self,
52 x: np.array,
53 y: np.array,
54 z: np.array,
55 axis: int = None,
56 make_copy: bool = True,
57 *args, **kwargs) -> tuple:
58 r"""
59 From the given `z` array, all columns or rows (depending on `axis`),
60 which contain contain `NaN`.
61 Corresponding entries of the coordinate vector (`x` or `y`) are removed aswell.
62 \param x Array of measuring point positions.
63 \param y Array of time stamps.
64 \param z Array of strain data in accordance to `x` and `y`.
65 \param axis \copydoc axis
66 \param make_copy Switch, whether a deepcopy of the passed data should be done.
67 Defaults to `True`.
68 \param *args Additional positional arguments to customize the behaviour.
69 \param **kwargs Additional keyword arguments to customize the behaviour.
70 \return Returns a tuple like `(x, y, z)`.
71 They correspond to the input variables of the same name
72 without columns or rows (depending on `axis`) containing `NaN`s.
73 """
74 axis = axis if axis is not None else self.axis
75 x, y, z = super().run(x, y, z, make_copy=make_copy, *args, **kwargs)
76 if z.ndim == 1:
77 keep_array = np.isfinite(z)
78 z = z[keep_array]
79 elif z.ndim == 2:
80 keep_array = np.all(np.isfinite(z), axis=axis)
81 keep_indices = np.arange(keep_array.shape[0])[keep_array]
82 z = np.take(z.T, keep_indices, axis=axis).T
83 if axis == 0 and x.ndim == 1:
84 x = x[keep_array]
85 if axis == 1 and y.ndim == 1:
86 y = y[keep_array]
87 return x, y, z
88
90 r"""
91 Replace dropouts (`NaN`s) with values interpolated by the given method.
92 The following steps are carried out:
93 1. The `NaN` values are removed using \ref NaNFilter.
94 Hence, an extra dropout removal before is not beneficial.
95 2. An interpolation function is calulated based on the dropout-free `x` and `z`.
96 3. The interpolation function is evaluated on the original `x`.
97
98 This is a wrapper for \ref fosanalysis.utils.interpolation.scipy_interpolate1d().
99 """
100 def __init__(self,
101 method: str = "Akima1DInterpolator",
102 method_kwargs: dict = None,
103 *args, **kwargs):
104 r"""
105 Construct an ScipyInterpolation1D object.
106 \param method \copydoc method
107 \param method_kwargs \copydoc method_kwargs
108 \param *args Additional positional arguments, will be passed to the superconstructor.
109 \param **kwargs Additional keyword arguments will be passed to the superconstructor.
110 """
111 super().__init__(*args, **kwargs)
112
114
130 self.method = method
131
134 self.method_kwargs = method_kwargs if method_kwargs is not None else {}
135 def run(self,
136 x: np.array,
137 y: np.array,
138 z: np.array,
139 timespace: str = None,
140 make_copy: bool = True,
141 method: str = None,
142 method_kwargs: dict = None,
143 *args, **kwargs) -> tuple:
144 r"""
145 \copydoc ScipyInterpolation1D
146 \copydetails preprocessing.base.Task.run()
147 \param method \copydoc method
148 \param method_kwargs \copydoc method_kwargs
149 """
150 return super().run(x, y, z,
151 timespace=timespace,
152 make_copy=make_copy,
153 method=method,
154 method_kwargs=method_kwargs,
155 *args, **kwargs)
156 def _run_1d(self,
157 x: np.array,
158 z: np.array,
159 method: str = None,
160 method_kwargs: dict = None,
161 *args, **kwargs) -> tuple:
162 r"""
163 Replace dropouts (`NaN`s) with values interpolated by the given method.
164 \param x Array of measuring point positions in accordance to `z`.
165 \param z Array of strain data in accordance to `x`.
166 \param method \copydoc method
167 \param method_kwargs \copydoc method_kwargs
168 \param *args Additional positional arguments, ignored.
169 \param **kwargs Additional keyword arguments, ignored.
170 \return Returns a tuple of like `(x, z)` of `np.array`s of the same shape.
171 """
172 method = method if method is not None else self.method
173 method_kwargs = method_kwargs if method_kwargs is not None else self.method_kwargs
174 method_kwargs = method_kwargs if method_kwargs is not None else {}
175 x_clean, y_clean, z_clean = self.nanfilter.run(x, None, z)
176 z_new = scipy_interpolate1d(
177 x=x_clean,
178 y=z_clean,
179 x_new=x,
180 method=method,
181 **method_kwargs)
182 return x, z_new
183 def _run_2d(self,
184 x: np.array,
185 y: np.array,
186 z: np.array,
187 SRA_array: np.array,
188 *args, **kwargs) -> tuple:
189 r"""
190 ScipyInterpolation1D has no true 2D operation mode.
191 Set \ref timespace to `"1d_space"`!
192 """
193 raise NotImplementedError("ScipyInterpolation1D does not support true 2D operation. Please use `timepace='1d_space'` instead.")
Abstract base class for preprocessing classes.
Definition base.py:16
Abstract base class for preprocessing task classes.
Definition base.py:56
A filter, that removes any columns from a given number of data sets (matrix), that contain not a numb...
Definition repair.py:31
axis
Axis of slices to be removed, if they contain any NaNs.
Definition repair.py:50
tuple run(self, np.array x, np.array y, np.array z, int axis=None, bool make_copy=True, *args, **kwargs)
From the given z array, all columns or rows (depending on axis), which contain contain NaN.
Definition repair.py:57
__init__(self, int axis=0, *args, **kwargs)
Construct an instance of the class.
Definition repair.py:37
Base class for algorithms to replace/remove missing data with plausible values.
Definition repair.py:19
Replace dropouts (NaNs) with values interpolated by the given method.
Definition repair.py:89
tuple _run_1d(self, np.array x, np.array z, str method=None, dict method_kwargs=None, *args, **kwargs)
Replace dropouts (NaNs) with values interpolated by the given method.
Definition repair.py:161
tuple _run_2d(self, np.array x, np.array y, np.array z, np.array SRA_array, *args, **kwargs)
ScipyInterpolation1D has no true 2D operation mode.
Definition repair.py:188
method_kwargs
This dictionary contains optional keyword arguments for method.
Definition repair.py:134
__init__(self, str method="Akima1DInterpolator", dict method_kwargs=None, *args, **kwargs)
Construct an ScipyInterpolation1D object.
Definition repair.py:103
method
Name of the interpolation function to use.
Definition repair.py:130
nanfilter
NaNFilter object used to temporarily remove NaNs.
Definition repair.py:113
tuple run(self, np.array x, np.array y, np.array z, str timespace=None, bool make_copy=True, str method=None, dict method_kwargs=None, *args, **kwargs)
Replace dropouts (NaNs) with values interpolated by the given method.
Definition repair.py:143
Contains functionality for interpolating data.