fosanalysis
A framework to evaluate distributed fiber optic sensor data
Loading...
Searching...
No Matches
integration.py
Go to the documentation of this file.
2r"""
3Contains functionality for integrating discretized funtions.
4\author Bertram Richter
5\date 2023
6"""
7
8import numpy as np
9import scipy.integrate
10
11from . import base
12
14 r"""
15 Object to integrate a function \f$y = f(x)\f$ given by discrete argument data \f$x\f$ and associated values \f$y\f$.
16 """
17 def __init__(self,
18 interpolation: str = "trapezoidal",
19 *args, **kwargs):
20 r"""
21 Constructs an Integrator object.
22 \param interpolation \copybrief interpolation For more, see \ref interpolation.
23 \param *args Additional positional arguments, will be passed to the superconstructor.
24 \param **kwargs Additional keyword arguments, will be passed to the superconstructor.
25 """
26 super().__init__(*args, **kwargs)
27
31 self.interpolation = interpolation
33 x_values: np.array,
34 y_values: np.array,
35 initial: float = 0.0,
36 interpolation: str = None,
37 *args, **kwargs) -> np.array:
38 r"""
39 Calculates the antiderivative \f$F(x) = \int f(x) dx + C\f$ to the given function over the given segment (indicated by `start_index` and `end_index`).
40 The given values are assumed to be preprocessed (`NaN`s are stripped already).
41 \param x_values List of x-positions \f$x\f$.
42 \param y_values List of y-values \f$y\f$ matching \f$x\f$.
43 \param initial The interpolation constant \f$C\f$.
44 \param interpolation \copybrief interpolation Defaults to \ref interpolation. For more, see \ref interpolation.
45 \param *args Additional positional arguments, will be passed to the called integration function.
46 \param **kwargs Additional keyword arguments, will be passed to the called integration function.
47 """
48 interpolation = interpolation if interpolation is not None else self.interpolation
49 # Prepare the segments
50 if interpolation == "trapezoidal":
51 return scipy.integrate.cumulative_trapezoid(y=y_values, x=x_values, initial=initial, *args, **kwargs)
52 else:
53 raise RuntimeError("No such option '{}' known for `interpolation`.".format(interpolation))
55 x_values: np.array,
56 y_values: np.array,
57 start_index: int = None,
58 end_index: int = None,
59 initial: float = 0.0,
60 interpolation: str = None,
61 *args, **kwargs) -> float:
62 r"""
63 Calculates integral over the given segment (indicated by `start_index` and `end_index`) \f$F(x)|_{a}^{b} = \int_{a}^{b} f(x) dx + C\f$.
64 The given values are assumed to be preprocessed (`NaN`s are stripped already).
65 \param x_values List of x-positions \f$x\f$.
66 \param y_values List of y-values \f$y\f$ matching \f$x\f$.
67 \param start_index Index, where the integration should start (index of \f$a\f$). Defaults to the first item of `x_values` (`0`).
68 \param end_index Index, where the integration should stop (index of \f$b\f$). This index is included. Defaults to the first item of `x_values` (`len(x_values) -1`).
69 \param initial The interpolation constant \f$C\f$.
70 \param interpolation \copybrief interpolation For more, see \ref interpolation.
71 \param *args Additional positional arguments, will be passed to the called integration function.
72 \param **kwargs Additional keyword arguments, will be passed to the called integration function.
73 """
74 interpolation = interpolation if interpolation is not None else self.interpolation
75 start_index = start_index if start_index is not None else 0
76 end_index = end_index if end_index is not None else len(x_values) - 1
77 x_segment = x_values[start_index:end_index+1]
78 y_segment = y_values[start_index:end_index+1]
79 # Prepare the segments
80 if interpolation == "trapezoidal":
81 return scipy.integrate.trapezoid(y=y_segment, x=x_segment, *args, **kwargs) + initial
82 else:
83 raise RuntimeError("No such option '{}' known for `interpolation`.".format(interpolation))
This intermediate class indicates, that a sub-class is implementing a task.
Definition base.py:26
Object to integrate a function given by discrete argument data and associated values .
interpolation
Algorithm, which should be used to interpolate between data points.
__init__(self, str interpolation="trapezoidal", *args, **kwargs)
Constructs an Integrator object.
float integrate_segment(self, np.array x_values, np.array y_values, int start_index=None, int end_index=None, float initial=0.0, str interpolation=None, *args, **kwargs)
Calculates integral over the given segment (indicated by start_index and end_index) .
np.array antiderivative(self, np.array x_values, np.array y_values, float initial=0.0, str interpolation=None, *args, **kwargs)
Calculates the antiderivative to the given function over the given segment (indicated by start_index...