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 an IOBlock 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 are numpy.array, and outputs another dict whose values are float. 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 a tuple). 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.

    Changed in version 1.5.10: renamed from stream to stream_label

  • 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.

__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.

    Changed in version 1.5.10: renamed from time to time_label

  • out_label – The label carrying the calculated derivative. If not given, defaults to 'd_<label>'.

__call__(data: Dict[str, Any]) Dict[str, Any][source]

Gets the data from the upstream Block, updates the derivative value, appends it to the data and returns the data.

Changed in version 2.0.0: renamed from evaluate to __call__

DownSampler

class crappy.modifier.DownSampler(n_points: int = 10)[source]

Modifier waiting for a given number of data points to be received, then returning only the last received point.

Similar to Mean, except it discards the values that are not transmitted instead of averaging them. Useful for reducing the amount of data sent to a Block.

New in version 2.0.4.

__init__(n_points: int = 10) None[source]

Sets the args and initializes the parent class.

Parameters:

n_points – One value will be sent to the downstream Block only once every n_points received values.

__call__(data: Dict[str, Any]) Dict[str, Any] | None[source]

Receives data from the upstream Block, and if the counter matches the threshold, returns the data.

If the counter doesn’t match the threshold, doesn’t return anything and increments the counter.

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.

    Changed in version 1.5.10: renamed from time to time_label

  • out_label – The label carrying the integration value. If not given, defaults to 'i_<label>'.

__call__(data: Dict[str, Any]) Dict[str, Any][source]

Gets the data from the upstream Block, updates the integration value, adds it to the data and returns the data.

Changed in version 2.0.0: renamed from evaluate to __call__

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 every n_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 from npoints 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 every n_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 from npoints 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

__init__(n_points: int = 100) None[source]

Sets the args and initializes the parent class.

Parameters:

n_points

The maximum number of points on which to compute the average.

Changed in version 1.5.10: renamed from npoints to n_points

__call__(data: Dict[str, Any]) Dict[str, Any][source]

Receives data from the upstream Block, computes the average of every label and replaces the original data with it.

Changed in version 2.0.0: renamed from evaluate to __call__

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

__init__(n_points: int = 100) None[source]

Sets the args and initializes the parent class.

Parameters:

n_points

The maximum number of points on which to compute the median.

Changed in version 1.5.10: renamed from npoints to n_points

__call__(data: Dict[str, Any]) Dict[str, Any][source]

Receives data from the upstream Block, computes the median of every label and replaces the original data with it.

Changed in version 2.0.0: renamed from evaluate to __call__

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 the IOBlock is a better alternative if precision is required when offsetting a sensor.

New in version 1.5.10.

__init__(labels: str | Iterable[str], offsets: float | Iterable[float]) None[source]

Sets the args and initializes the parent class.

Parameters:
  • labels – The labels to offset. Can be given as a single label, a list of labels or a tuple of labels.

  • offsets – For each label, the target for the first received value. Can be given as a single value, a list of values or a tuple of values.

__call__(data: Dict[str, Any]) Dict[str, Any][source]

If the compensations are not set, sets them, and then offsets the required labels.

Changed in version 2.0.0: renamed from evaluate to __call__

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

__init__(label: str) None[source]

Sets the args and initializes the parent class.

Parameters:

label

The name of the label to monitor.

Changed in version 1.5.10: renamed from name to label

__call__(data: Dict[str, Any]) Dict[str, Any] | None[source]

Compares the received value with the last sent one, and if they’re different sends the received data and stores the latest value.

Changed in version 2.0.0: renamed from evaluate to __call__

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

__init__(label: str, values: Any | Iterable[Any]) None[source]

Sets the args and initializes the parent class.

Parameters:
  • label

    The name of the label to monitor.

    Changed in version 1.5.10: renamed from name to label

  • values – The values of label for which the data will be transmitted. Can be a single value, or an iterable of values (like a list or a tuple).

__call__(data: Dict[str, Any]) Dict[str, Any] | None[source]

Checks if the value of label is in the predefined set of accepted values, and if so transmits the data.

Changed in version 2.0.0: renamed from evaluate to __call__

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 a Link linking the two Blocks. It is passed as an argument of the link() 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 another dict. Both dicts should have their keys as str, representing the labels. Their values constitute the data flowing through the Link.

Parameters:

data – The data from the input Block, as a dict.

Returns:

Data to send to the output Block, as a dict. It is also fine for this method to return None, in which case no message is transmitted to the output Block.

New in version 2.0.0.

log(level: int, msg: str) None[source]

Records log messages for the Modifiers.

Also instantiates the logger when logging the first message.

Parameters:
  • level – An int indicating the logging level of the message.

  • msg – The message to log, as a str.

New in version 2.0.0.

Meta Modifier

class crappy.modifier.MetaModifier(name: str, bases: tuple, dct: dict)[source]

Metaclass keeping track of all the Modifiers, including the custom user-defined ones.

New in version 1.4.0.

Changed in version 1.5.10: not checking anymore for mandatory method in __init__()

__init__(name: str, bases: tuple, dct: dict) None[source]