fosanalysis
A framework to evaluate distributed fiber optic sensor data
Loading...
Searching...
No Matches
cropping.py
Go to the documentation of this file.
2r"""
3Contains functionality to restrict data to a given area of interest.
4\author Bertram Richter
5\date 2023
6"""
7import copy
8import warnings
9
10import numpy as np
11
12def cropping(x_values: np.array,
13 z_values: np.array,
14 start_pos: float = None,
15 end_pos: float = None,
16 length: float = None,
17 offset: float = None,
18 *args, **kwargs) -> tuple:
19 r"""
20 Crop a data set \f$x_i,\: z_i\f$ based on locational data \f$x\f$.
21 The process consists of two steps:
22 1. The \f$x\f$ data is shifted by the offset \f$o\f$, such that \f$x \gets x + o\f$.
23 2. Cropping (restricting entries): \f$x_i:\: x_i \in [s,\: e]\f$ and \f$z_i:\: x_i \in [s,\: e]\f$
24
25 \param x_values One-dimensional array of x-positions \f$x\f$.
26 \param z_values Array of z-values \f$z\f$ matching \f$x\f$.
27 Can be a 1D or 2D array.
28 \param start_pos The starting position \f$s\f$ specifies the length of the sensor, before entering the measurement area.
29 Defaults to `None` (no data is removed at the beginning).
30 \param end_pos The end position \f$s\f$ specifies the length of the sensor, when leaving the measurement area.
31 If both `length` and `end_pos` are provided, `end_pos` takes precedence.
32 Defaults to `None` (no data is removed at the end).
33 \param length Length of the data excerpt. If set, it is used to determine the `end_pos`.
34 If both `length` and `end_pos` are provided, `end_pos` takes precedence.
35 \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`.
36 \param *args Additional positional arguments, ignored.
37 \param **kwargs Additional keyword arguments, ignored.
38 \return Returns the cropped lists \f$(x_i,\: z_i)\f$:
39 \retval x_cropped Array, such that \f$x_i:\: x_i \in [s,\: e]\f$.
40 \retval z_cropped Array, such that, \f$z_i:\: x_i \in [s,\: e]\f$.
41
42 To reduce/avoid boundary effects, genrally crop the data after smoothing.
43 """
44 x_shift = np.array(copy.deepcopy(x_values))
45 z_cropped = np.array(copy.deepcopy(z_values))
46 assert z_cropped.ndim in [1, 2], "Dimensions of y_values ({}) not conformant. y_values must be a 1D or 2D array".format(z_cropped.ndim)
47 assert x_shift.shape[-1] == z_cropped.shape[-1], "Number of entries do not match! (x_values: {}, y_values: {}.)".format(x_shift.shape[-1], z_cropped.shape[-1])
48 if offset is not None:
49 x_shift = x_values + offset
50 start_pos = start_pos if start_pos is not None else x_shift[0]
51 end_pos = end_pos if end_pos is not None else start_pos + length if length is not None else x_shift[-1]
52 # find start index
53 start_index = np.searchsorted(x_shift, start_pos, side="left")
54 end_index = np.searchsorted(x_shift, end_pos, side="right")
55 x_cropped = x_shift[start_index:end_index]
56 if z_cropped.ndim == 1:
57 z_cropped = z_cropped[start_index:end_index]
58 elif z_cropped.ndim == 2:
59 z_cropped = z_cropped[:, start_index:end_index]
60 if 0 in z_cropped.shape or 0 in x_cropped.shape:
61 warnings.warn("Cropping result contains an empty axis (no entries). Recheck cropping parameters.", RuntimeWarning)
62 return x_cropped, z_cropped
63