ts_shape.utils.signal_checks
Consistent input checks for detector calls (wired in by Base).
Every public method of a detector in ts_shape.events is wrapped so that
its signal arguments are checked the same way, whichever pack it is in:
- a signal uuid (any argument named
*_uuid/*_uuids, exceptevent_uuid) that does not occur in a non-empty frame emits a :class:~ts_shape.errors.DataQualityWarningnaming the uuids that do -- instead of silently returning an empty result; - a frame without the uuid column raises
:class:
~ts_shape.errors.ColumnNotFoundError(aValueError) instead of a bareKeyErrordeep inside the method; - a column argument (
value_column,value_column_<role>,<role>_column) that names a column the frame does not have raises :class:~ts_shape.errors.ColumnNotFoundErrorlisting the columns, when the method looks it up; - a signal whose values are in a different
value_*column than the one selected for it (a counter invalue_doubleread fromvalue_integer) emits a :class:~ts_shape.errors.DataQualityWarningnaming the column to pass (:func:warn_if_values_elsewhere). It is checked after the constructor on the signal rows the detector already selected, and after a method call that returned an empty frame; - a value-column argument the caller left at its default, whose signal has
no values in that column while exactly one other
value_*column holds them, is replaced by that column before the call (:func:resolve_value_columns), with a :class:~ts_shape.errors.DataQualityWarningnaming the argument to pass. Aspecs=argument (:class:~ts_shape.signals.spec.SignalSpecs) is followed first. Columns the caller passed are never changed.
Apart from the value-column resolution, the checks never change a result.
| Name | Description |
|---|---|
remember_uuids | Record uuids (as text) as the values of frame[column]. |
take_signal_request | The signal uuids the constructor named, when frame is its input. |
remember_source | Record that obj.dataframe holds only the kept signals of frame. |
signal_source | (input frame, row count, kept uuids) of a signal-rows detector. |
check_signals | Warn about signal uuids that are not in obj.dataframe. |
warn_if_values_elsewhere | Warn when signal_uuid's values are in another value_* column. |
warn_if_values_elsewhere_once | :func:warn_if_values_elsewhere at most once per (uuid, column) on obj. |
check_value_columns | :func:warn_if_values_elsewhere for each signal of a detector call. |
resolve_value_columns | kwargs with value-column defaults replaced where the data says so. |
check_blank_ids | Warn when an identifier signal holds only empty text. |
checked | Wrap a detector method with :func:check_signals (outermost call only). |
checked_init | Wrap a detector constructor: column check before, uuid check after. |
remember_uuids
remember_uuids(
obj: Any, frame: pd.DataFrame, column: str, uuids: set[str]
) -> None
Record uuids (as text) as the values of frame[column].
Used while obj.dataframe is still frame (the same object, with
the same length); a detector that replaces its frame gets its uuids
computed again.
take_signal_request
take_signal_request(obj: Any, frame: pd.DataFrame) -> frozenset[str] | None
The signal uuids the constructor named, when frame is its input.
Read (and cleared) by Base.__init__; None when the detector does
not declare :data:OWN_SIGNALS_ONLY or frame is not the frame its
constructor was given.
remember_source
remember_source(
obj: Any, frame: pd.DataFrame, n_rows: int, kept: frozenset[str]
) -> None
Record that obj.dataframe holds only the kept signals of frame.
signal_source
signal_source(obj: Any) -> tuple[pd.DataFrame, int, frozenset[str]] | None
(input frame, row count, kept uuids) of a signal-rows detector.
check_signals
check_signals(
obj: Any, fn: Callable[..., Any], args: tuple, kwargs: dict
) -> None
Warn about signal uuids that are not in obj.dataframe.
warn_if_values_elsewhere
warn_if_values_elsewhere(
rows: pd.DataFrame,
signal_uuid: str,
value_column: str,
*,
where: str | None = None,
stacklevel: int | None = None
) -> bool
Warn when signal_uuid's values are in another value_* column.
rows are the signal's rows (already selected; nothing else is
scanned). When value_column exists but holds no value in them while
another of value_double / value_integer / value_bool /
value_string does, the result computed from value_column is empty
or meaningless: a :class:~ts_shape.errors.DataQualityWarning names the
most populated column
(:func:ts_shape.signals.infer.most_populated_value_column). The result is
not changed.
| Name | Type | Description | Default |
|---|---|---|---|
rows | DataFrame | The signal's rows. | required |
signal_uuid | str | The signal's uuid (for the message). | required |
value_column | str | The column the values are read from. | required |
where | str | None | Prefix for the message (e.g. the detector's class name). | None |
stacklevel | int | None | warnings.warn stacklevel; default: the first frame outside ts_shape. | None |
| Type | Description |
|---|---|
bool | Whether a warning was emitted. |
warn_if_values_elsewhere_once
warn_if_values_elsewhere_once(
obj: Any, rows: pd.DataFrame, signal_uuid: str, value_column: str
) -> bool
:func:warn_if_values_elsewhere at most once per (uuid, column) on obj.
For a detector that selects a signal's rows itself (several signals per instance): it warns about a misconfigured signal even when the result is not empty, and the pair counts as checked, so the call wrapper does not warn about it again.
check_value_columns
check_value_columns(
obj: Any, named: Mapping[str, Any], *, lookup: bool
) -> None
:func:warn_if_values_elsewhere for each signal of a detector call.
named are the call's arguments (defaults applied). A call's value
columns go with the call's signals; signals named without a value-column
argument go with the detector's stored value_column, and the
constructor's pairs that could not be checked yet are added. Each
(uuid, column) pair is checked once per detector. The signal's rows
are the ones the detector already selected (an attribute holding only
that uuid's rows); with lookup=True (after a call that returned an
empty frame) they are otherwise selected from obj.dataframe.
resolve_value_columns
resolve_value_columns(
obj: Any,
fn: Callable[..., Any],
args: tuple,
kwargs: dict,
frame: Any,
uuid_column: str = "uuid",
) -> dict
kwargs with value-column defaults replaced where the data says so.
For each value_column / value_column_<role> argument the caller
did not pass, the signals it is read for (the uuid arguments it pairs
with, see :func:_value_pairs) are looked up in frame. When none of
them has a value in the default column and all of them hold their
values in the same other value_* column, that column is passed
instead and a :class:~ts_shape.errors.DataQualityWarning says so. A
specs= argument (SignalSpecs) decides first: its
value_column is used when it differs from the default. A detector
that reads a boolean run signal in place of an empty value_string
by itself lists those arguments in _bool_state_fallback; they are
left as they are.
| Name | Type | Description | Default |
|---|---|---|---|
obj | Any | The detector. | required |
fn | Callable..., [Any] | The wrapped method (or constructor). | required |
args | tuple | Its positional arguments (without obj). | required |
kwargs | dict | Its keyword arguments. | required |
frame | Any | The frame the signals are looked up in. | required |
uuid_column | str | The frame's uuid column. | 'uuid' |
| Type | Description |
|---|---|
dict | The keyword arguments to call fn with (kwargs itself when |
dict | nothing changes). |
check_blank_ids
check_blank_ids(
obj: Any, fn: Callable[..., Any], args: tuple, kwargs: dict
) -> None
Warn when an identifier signal holds only empty text.
An order or part-number signal that delivers only "" (an empty PLC
string) is present, so no "not found" warning fires, but every result
keyed by it is empty.
checked
checked(fn: Callable[..., Any]) -> Callable[..., Any]
Wrap a detector method with :func:check_signals (outermost call only).
checked_init
checked_init(fn: Callable[..., Any]) -> Callable[..., Any]
Wrap a detector constructor: column check before, uuid check after.