Skip to main content
Version: 0.0.98

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, except event_uuid) that does not occur in a non-empty frame emits a :class:~ts_shape.errors.DataQualityWarning naming the uuids that do -- instead of silently returning an empty result;
  • a frame without the uuid column raises :class:~ts_shape.errors.ColumnNotFoundError (a ValueError) instead of a bare KeyError deep 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.ColumnNotFoundError listing 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 in value_double read from value_integer) emits a :class:~ts_shape.errors.DataQualityWarning naming 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.DataQualityWarning naming the argument to pass. A specs= 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.

Functions
NameDescription
remember_uuidsRecord uuids (as text) as the values of frame[column].
take_signal_requestThe signal uuids the constructor named, when frame is its input.
remember_sourceRecord that obj.dataframe holds only the kept signals of frame.
signal_source(input frame, row count, kept uuids) of a signal-rows detector.
check_signalsWarn about signal uuids that are not in obj.dataframe.
warn_if_values_elsewhereWarn 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_columnskwargs with value-column defaults replaced where the data says so.
check_blank_idsWarn when an identifier signal holds only empty text.
checkedWrap a detector method with :func:check_signals (outermost call only).
checked_initWrap a detector constructor: column check before, uuid check after.

remember_uuids​

functionsource
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​

functionsource
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​

functionsource
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​

functionsource
signal_source(obj: Any) -> tuple[pd.DataFrame, int, frozenset[str]] | None

(input frame, row count, kept uuids) of a signal-rows detector.

check_signals​

functionsource
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​

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

Parameters
NameTypeDescriptionDefault
rowsDataFrameThe signal's rows.required
signal_uuidstrThe signal's uuid (for the message).required
value_columnstrThe column the values are read from.required
wherestr | NonePrefix for the message (e.g. the detector's class name).None
stacklevelint | Nonewarnings.warn stacklevel; default: the first frame outside ts_shape.None
Returns
TypeDescription
boolWhether a warning was emitted.

warn_if_values_elsewhere_once​

functionsource
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​

functionsource
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​

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

Parameters
NameTypeDescriptionDefault
objAnyThe detector.required
fnCallable..., [Any]The wrapped method (or constructor).required
argstupleIts positional arguments (without obj).required
kwargsdictIts keyword arguments.required
frameAnyThe frame the signals are looked up in.required
uuid_columnstrThe frame's uuid column.'uuid'
Returns
TypeDescription
dictThe keyword arguments to call fn with (kwargs itself when
dictnothing changes).

check_blank_ids​

functionsource
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​

functionsource
checked(fn: Callable[..., Any]) -> Callable[..., Any]

Wrap a detector method with :func:check_signals (outermost call only).

checked_init​

functionsource
checked_init(fn: Callable[..., Any]) -> Callable[..., Any]

Wrap a detector constructor: column check before, uuid check after.