Modifiers
Regular Modifiers
Demux
- class crappy.modifier.Demux(labels: str | Iterable[str], stream_label: str = 'stream', mean: bool = False, time_label: str = 't(s)', transpose: bool = False)[source]
Modifier converting a stream into a regular data flow interpretable by most Blocks.
It is meant to be used on a
Link
taking anIOBlock
in streamer mode as an input. It converts the stream to make it readable by most Blocks, and also splits the stream in several labels if necessary.It takes a stream as an input, i.e. a
dict
whose values arenumpy.array
, and outputs anotherdict
whose values arefloat
. If the numpy arrays contains several columns (corresponding to several acquired channels), it splits them into several labels.Important
In the process of converting the stream data to regular labeled data, much information is lost ! This Modifier is intended to format the stream data for low-frequency plotting, or low-frequency decision-making. To save all the stream data, use the
HDFRecorder
Block.New in version 1.4.0.
- __init__(labels: str | Iterable[str], stream_label: str = 'stream', mean: bool = False, time_label: str = 't(s)', transpose: bool = False) None [source]
Sets the args and initializes the parent class.
- Parameters:
labels – The labels corresponding to the rows or columns of the stream. It can be either a single label, or an iterable of labels (like a
list
or atuple
). They must be given in the same order as they appear in the stream. If fewer labels are given than there are rows or columns in the stream, only the data from the first rows or columns will be retrieved.stream_label – The label carrying the stream.
mean – If
True
, the returned value will be the average of the row or column. Otherwise, it will be the first value.time_label – The label carrying the time information.
transpose – If
True
, each label corresponds to a row in the stream. Otherwise, a label corresponds to a column in the stream.
Changed in version 1.5.10: renamed stream argument to stream_label
- __call__(data: Dict[str, ndarray]) Dict[str, Any] [source]
Retrieves for each label its value in the stream, also gets the corresponding timestamp, and returns them.
Changed in version 1.5.10: merge evaluate_mean and evaluate_nomean methods into evaluate
Changed in version 2.0.0: renamed from evaluate to __call__
Differentiate
- class crappy.modifier.Diff(label: str, time_label: str = 't(s)', out_label: str | None = None)[source]
This Modifier calculates the time derivative of a given label and adds the derivative to the returned data.
New in version 1.4.0.
- __init__(label: str, time_label: str = 't(s)', out_label: str | None = None) None [source]
Sets the args and initializes the parent class.
- Parameters:
label – The label whose time derivative to compute.
time_label – The label carrying the time information.
out_label – The label carrying the calculated derivative. If not given, defaults to
'd_<label>'
.
Changed in version 1.5.10: renamed time argument to time_label
Integrate
- class crappy.modifier.Integrate(label: str, time_label: str = 't(s)', out_label: str | None = None)[source]
This Modifier integrates the data of a label over time and adds the integration value to the returned data.
New in version 1.4.0.
- __init__(label: str, time_label: str = 't(s)', out_label: str | None = None) None [source]
Sets the args and initializes the parent class.
- Parameters:
label – The label whose data to integrate over time.
time_label – The label carrying the time information.
out_label – The label carrying the integration value. If not given, defaults to
'i_<label>'
.
Changed in version 1.5.10: renamed time argument to time_label
Mean
- class crappy.modifier.Mean(n_points: int = 100)[source]
Modifier waiting for a given number of data points to be received, then returning their average, and starting all over again.
Unlike
MovingAvg
, it only returns a value once everyn_points
points.New in version 1.4.0.
- __init__(n_points: int = 100) None [source]
Sets the args and initializes the parent class.
- Parameters:
n_points – The number of points on which to compute the average.
Changed in version 1.5.10: renamed npoints argument to n_points
- __call__(data: Dict[str, Any]) Dict[str, Any] | None [source]
Receives data from the upstream Block, and computes the average of every label once the right number of points have been received. Then empties the buffer and returns the averages.
If there are not enough points, doesn’t return anything.
Changed in version 2.0.0: renamed from evaluate to __call__
Median
- class crappy.modifier.Median(n_points: int = 100)[source]
Modifier waiting for a given number of data points to be received, then returning their median, and starting all over again.
Unlike
MovingMed
, it only returns a value once everyn_points
points.New in version 1.4.0.
- __init__(n_points: int = 100) None [source]
Sets the args and initializes the parent class.
- Parameters:
n_points – The number of points on which to compute the median.
Changed in version 1.5.10: renamed npoints argument to n_points
- __call__(data: Dict[str, Any]) Dict[str, Any] | None [source]
Receives data from the upstream Block, and computes the median of every label once the right number of points have been received. Then empties the buffer and returns the medians.
If there are not enough points, doesn’t return anything.
Changed in version 2.0.0: renamed from evaluate to __call__
Moving Average
- class crappy.modifier.MovingAvg(n_points: int = 100)[source]
Modifier replacing the data of each label with its average value over a chosen number of points.
Unlike
Mean
, it returns a value each time data is received from the upstream Block.New in version 1.4.0.
Changed in version 2.0.0: renamed from Moving_avg to MovingAvg
Moving Median
- class crappy.modifier.MovingMed(n_points: int = 100)[source]
Modifier replacing the data of each label with its median value over a chosen number of points.
Unlike
Median
, it returns a value each time data is received from the upstream Block.New in version 1.4.0.
Changed in version 2.0.0: renamed from Moving_med to MovingMed
Offset
- class crappy.modifier.Offset(labels: str | Iterable[str], offsets: float | Iterable[float])[source]
This Modifier offsets every value of the given labels by a constant. This constant is calculated so that for each label the first returned value is equal to a user-defined target.
For example if for a given label the target is 6 and the first received value is 3, the Modifier will add 3 to each value received over this label.
This Modifier can be used for example when measuring a variable that should start at 0 but doesn’t because of a sensor offset. It can also just be used to plot nicer figures. It is not very accurate as it is only based on a single data point for the offset calculation. The
make_zero
argument of theIOBlock
is a better alternative if precision is required when offsetting a sensor.New in version 1.5.10.
Trig on change
- class crappy.modifier.TrigOnChange(label: str)[source]
Modifier passing the data to the downstream Block only when the value of a given label changes.
It also transmits the first received data. Can be used to trigger a Block upon change of a label value.
New in version 1.4.0.
Changed in version 2.0.0: renamed from Trig_on_change to TrigOnChange
Trig on value
- class crappy.modifier.TrigOnValue(label: str, values: Any | Iterable[Any])[source]
Modifier passing the data to the downstream Block only if the value carried by a given label matches a given set of accepted values.
Mostly useful to trigger Blocks in predefined situations.
New in version 1.4.0.
Changed in version 2.0.0: renamed from Trig_on_value to TrigOnValue
Parent Modifier
Modifier
- class crappy.modifier.Modifier(*_, **__)[source]
The base class for all Modifier classes, simply allowing to keep track of them.
The Modifiers allow altering data from an input
Block
before it gets sent to an output Block. Each Modifier is associated to aLink
linking the two Blocks. It is passed as an argument of thelink()
method instantiating the Link.It is preferable for every Modifier to be a child of this class, although that is not mandatory. A Modifier only needs to be a callable, i.e. a class defining the
__call__()
method or a function.New in version 1.4.0.
- __init__(*_, **__) None [source]
Sets the logger attribute.
Changed in version 2.0.0: now accepts args and kwargs
- __call__(data: Dict[str, Any]) Dict[str, Any] | None [source]
The main method altering the inout data and returning the altered data.
It should take a
dict
as its only argument, and return anotherdict
. Both dicts should have their keys asstr
, representing the labels. Their values constitute the data flowing through theLink
.- Parameters:
- Returns:
Data to send to the output
Block
, as adict
. It is also fine for this method to returnNone
, in which case no message is transmitted to the output Block.
New in version 2.0.0.