ts_shape.eventlog.objects
Object detection — turn identifier-bearing timeseries signals into OCEL 2.0 objects, relations, and time-varying attributes.
The event packs answer "what happened?". This module answers "to which things?" — the batches, serials, coils, recipes, tools, materials, drums, bundles, families, customer part-numbers (and any other object out there) that events refer to.
It is deliberately one generic, declarative layer, not another family of
detector classes: you describe which signals carry which object type with an
:class:ObjectSpec (or discover them from metadata), and a run-length kernel
(the same value-change segmentation that powers batch/traceability detection)
extracts every object's presence interval. New object types are added by data /
config, never by code.
Three steps, all feeding the OCEL 2.0 tables on :class:~ts_shape.eventlog.model.EventLog:
- :func:
object_intervals— segment id signals into(oid, type, start, end)presence intervals (an id is present until the next id on the same signal). - :func:
detect_objects— build theobjects/o2o/object_changestables.part_ofis asserted only along a declaredhierarchyof object types or id signals (it cannot be inferred from time alone); usage links (made_from/made_with/made_by) along declareduses; the symmetricco_occursfor every other overlap is opt-in (infer_o2o="co_occurs"). - :func:
attach_objects— link an existing event log's events to the detected objects by temporal overlap, optionally within ascope(e.g. a line), so the output of every event detector gains rich object references with no per-detector changes.
When you already hold the two lists — an event list and an object list (intervals) — the relationship derivation is automated and needs no raw signals:
- :func:
link_events_to_objects— all event-to-object (E2O) relations, by temporal overlap and/or directkey_columnsmatching. - :func:
derive_o2o— all object-to-object (O2O) relations (declaredpart_ofhierarchy, optionally the symmetricco_occurs). - :func:
relate— assemble both, plus objects andobject_changes, into one validated :class:EventLogready to ingest into an OCEL 2.0 database.
Presence semantics (shared by every function here)
- An id is present from its first sample until the next value on the same
signal: the next id, or an empty / missing reading (
"", whitespace,NaN/None/<NA>), which means "no object". A gap in the data does not end an object; only a different value does. - A misread (
NOREAD,#N/A,0,65535,"????????", a truncated id, ...; see :attr:ObjectSpec.exclude) is not an id: between two readings of the same id it is a gap (the id is held across it), anywhere else it is an empty reading. - Presence is half-open
[start, end): at a hand-over instant the incoming object owns the timestamp, never both. The last presence on a signal (and, in a user-supplied table, any presence no other object of its signal / type takes over from) is closed[start, end], its end being the last observation. - The same id appearing again later is the same object (one row in
objects) with several presence intervals. - With a change flag (
is_delta, see :attr:ObjectSpec.delta_column) on a signal with both flagged and unflagged rows, a flagged reading that repeats the id before it is a re-setup and starts a new presence of the same object. - With a cycle signal (:attr:
ObjectSpec.cycle_uuid) a presence spans the machine cycle[rise, fall)its first reading falls in. - All timestamps are UTC (OCEL 2.0); tz-aware input is converted, naive input is taken as UTC.
| Name | Description |
|---|---|
ObjectSpec | Declares that one signal carries the identity of one object type. |
| Name | Description |
|---|---|
object_intervals | Extract one presence interval per object presence from id signals. |
detect_objects | Detect object instances and build their OCEL 2.0 tables. |
attach_objects | Link an existing event log to detected objects by temporal overlap. |
relate | Assemble a full OCEL 2.0 log from an event list and an object list. |
derive_o2o | Derive object-to-object (O2O) relations from an interval table. |
object_specs_from_metadata | Build :class:ObjectSpec list from per-signal metadata tags. |
link_events_to_objects | Derive all event-to-object (E2O) relations from two lists. |
ObjectSpec
ObjectSpec(
uuid: str,
object_type: str,
value_column: str = "value_string",
min_duration: str | None = None,
id_template: str = "{value}",
attributes: Mapping[str, str] = dict(),
exclude: Exclude = MISREADS,
delta_column: str | None = "is_delta",
cycle_uuid: str | None = None,
cycle_value_column: str = "value_bool",
) -> None
Declares that one signal carries the identity of one object type.
| Name | Type | Description |
|---|---|---|
uuid | str | The signal (uuid value) whose value column is the object id. |
object_type | str | OCEL object type, e.g. "batch", "coil", "serial", "recipe". Auto-registered via :func:~ts_shape.eventlog.schema.register_object_type if unknown. |
value_column | str | Column carrying the id ("value_string" or "value_integer"). Empty strings and missing values mean "no object present". |
min_duration | str | None | Optional minimum presence (e.g. "1min", measured start to next value); shorter blips are dropped and the presence around them closes over the gap. The last id on a signal is open-ended and never dropped. |
id_template | str | How to render the object id from the raw value. Supports {value} and {type} — default "{value}"; use "{type}:{value}" to namespace ids across types. |
attributes | Mapping[str, str] | {attribute_name: signal_uuid} — the value in effect at each presence start (last non-missing sample at or before it) plus every change of that signal during the presence, emitted as object_changes. |
exclude | Exclude | Readings that are not ids (scanner no-reads, PLC defaults). "misreads" (default) applies :func:ts_shape.utils.id_codes.is_misread -- NOREAD, #N/A, NULL, ERR, 0, -1, 32767, 65535, "????????", fillers such as "XXXXXXXX", truncated reads. A collection of values excludes exactly those values (compared as text, so 0 matches "0"); a callable takes the signal's id values in time order and returns a boolean mask; None excludes nothing. An excluded reading never becomes an object and never splits a presence: between two readings of the same id it is a gap in the data (the id is held across it); anywhere else it is an empty reading (the presence before it ends there). |
delta_column | str | None | Column flagging the rows where the id was set (the historian's is_delta: True when the operator scans or sets up the id, False on the periodic repeats). When the column exists and the signal has both True and False rows (hold + repeat: the flag tells changes from snapshots), a flagged row whose value equals the reading before it starts a new presence (a re-setup with the same material number). On a signal whose rows are all True (a change-only store, where an unchanged row is a re-send) or all False / missing, the flag is not used. A value change starts a new presence with or without the flag. A flag right after a misread or an empty reading adds nothing (the change it marks is the one back from the misread), so A, NOREAD, A stays one presence. A missing flag never starts a presence and is not a False row. None ignores the column (value changes only). |
cycle_uuid | str | None | A bool signal whose True stretches are the machine cycles (e.g. "door closed"). Each presence is assigned to the cycle rise, fall) its first reading (the scan) falls in and then spans exactly that cycle: the serial scanned mid-cycle is the part made in that cycle. Several presences of one id in one cycle are one presence. A presence that starts outside every cycle keeps its own span (scan to next change), with one :class:~ts_shape.errors.DataQualityWarning per call. A cycle still True at the cycle signal's last reading ends at the last timestamp of the frame. None (default): no cycles. |
[cycle_value_column | str | Value column of cycle_uuid (default "value_bool"; numbers count as True when non-zero, text when it reads true / 1 / on / yes). |
object_intervals
object_intervals(
dataframe: pd.DataFrame,
specs: Sequence[ObjectSpec],
*,
uuid_column: str = "uuid",
time_column: str = "systime"
) -> pd.DataFrame
Extract one presence interval per object presence from id signals.
Returns a tidy frame with columns oid, type, start, end,
duration_seconds, uuid (the id signal), source_uuid (of the id
signal, when the frame has that column) plus one column per captured
attribute (its value at the presence start). Times are UTC.
Each id signal is run-length encoded: an id is present from its first
sample until the next value on the same signal (next id, or an empty /
missing reading). A signal whose last reading is still an id keeps that
object present until the last timestamp of the frame. An id that
re-appears later yields another row with the same oid. Readings
excluded by :attr:ObjectSpec.exclude (misreads by default) are never
objects and do not split a presence. A reading flagged in
:attr:ObjectSpec.delta_column (is_delta) that repeats the id before
it starts a new presence (a re-setup). With :attr:ObjectSpec.cycle_uuid
each presence spans the cycle its first reading falls in, sorted by
start. An empty frame or no matching signal gives an empty frame with
the full column set.
| Type | Description |
|---|---|
ValueError | When a spec's cycle_uuid is not in the frame. |
detect_objects
detect_objects(
dataframe: pd.DataFrame,
specs: Sequence[ObjectSpec],
*,
uuid_column: str = "uuid",
time_column: str = "systime",
hierarchy: Hierarchy | None = None,
infer_o2o: InferO2O = True,
validate: bool = True,
uses: Uses | None = None
) -> EventLog
Detect object instances and build their OCEL 2.0 tables.
Produces an :class:EventLog with no events — only objects, o2o
(object-to-object relations), and object_changes (presence lifecycle +
captured attributes). Compose it with event-detector logs via
:func:~ts_shape.eventlog.concat.concat.
hierarchy declares the compositional part_of relations, because
part_of cannot be inferred from time alone (a long-running tool
temporally contains every batch without owning it). Each key is a child
and each value a parent or a list of parents; a key or value is an
id-signal uuid when it names one of the specs' signals, else an
object type:
{"serial": "batch", "batch": "work_order"}-- type level: each serial presence is part of the batch presence it overlaps longest.{"L1:serial": "L1:batch", "L2:serial": "L2:batch"}-- signal level: a serial of line 1 is part of a line-1 batch only, so two lines stay apart.{"L1:serial": ["L1:batch", "L1:hu"]}-- several parents: onepart_ofper parent (a serial in a batch and a handling unit).
Each child presence gets one part_of per declared parent: to the
parent presence it overlaps longest (ties: the earlier parent).
uses declares usage (bill-of-materials) links: {child: {parent: qualifier}}, named like hierarchy entries, e.g. {"cell:serial": {"line:material": "made_from", "line:tool": "made_with", "line:operator": "made_by"}} (any qualifier text works). Each child presence is linked
to every parent presence it overlaps (half-open; touching is no
overlap), so every serial made while a material is held is made_from
it, also when the material never changes, and a serial whose cycle spans
a material change is made_from both. One row per (child, parent, qualifier). :func:~ts_shape.eventlog.where_used and
:func:~ts_shape.eventlog.where_from follow these qualifiers.
infer_o2o: True / "hierarchy" (default) emits the declared
part_of edges only -- no hierarchy, no o2o rows.
"co_occurs" also relates every two overlapping objects of different
types with the symmetric co_occurs qualifier (pairs already related
by part_of or uses excepted); on a plant this can be hundreds
of thousands of rows. False skips object-to-object relations (also
uses).
| Name | Type | Description | Default |
|---|---|---|---|
dataframe | DataFrame | Long-format frame with the id signals. | required |
specs | Sequence [ObjectSpec ] | Which signal carries which object type. | required |
uuid_column | str | Signal identifier column. | 'uuid' |
time_column | str | Timestamp column. | 'systime' |
hierarchy | Hierarchy | None | Child -> parent(s) for part_of relations, by object type or id-signal uuid. | None |
infer_o2o | InferO2O | Which object-to-object relations to derive. | True |
validate | bool | Check the result against the canonical schema. | True |
uses | Uses | None | Child -> {parent: qualifier} usage links, by id-signal uuid or object type. | None |
| Name | Type | Description |
|---|---|---|
An | EventLog | class:EventLog with objects, o2o and object_changes |
EventLog | and no events. |
| Type | Description |
|---|---|
ColumnNotFoundError | When dataframe lacks the uuid, time or a spec's value column (or a spec's cycle_value_column). |
ValueError | For an unknown infer_o2o value, a cycle_uuid that is not in the frame, or an empty uses qualifier. |
TypeError | When a uses entry is not a {parent: qualifier} mapping. |
attach_objects
attach_objects(
event_log: EventLog,
dataframe: pd.DataFrame,
specs: Sequence[ObjectSpec],
*,
qualifiers: Mapping[str, str] | None = None,
uuid_column: str = "uuid",
time_column: str = "systime",
hierarchy: Hierarchy | None = None,
infer_o2o: InferO2O = True,
scope: Scope | None = None,
fanout_threshold: int | None = FANOUT_THRESHOLD,
validate: bool = True,
uses: Uses | None = None
) -> EventLog
Link an existing event log to detected objects by temporal overlap.
Every event is linked with an event-to-object (E2O) relation to each
object whose presence overlaps the event (see
:func:link_events_to_objects, which also explains scope and
fanout_threshold). The detected objects, o2o and
object_changes are merged in. This enriches the output of any
event detector — no per-detector changes required. See
:func:detect_objects for hierarchy and infer_o2o.
This is a convenience over :func:relate: it first segments dataframe into an
object-interval table via :func:object_intervals, then relates the events
to it. When you already hold an interval/object table, call :func:relate
directly and skip the raw signals.
| Name | Type | Description | Default |
|---|---|---|---|
event_log | EventLog | The events to link. | required |
dataframe | DataFrame | Long-format frame with the id signals. Naive timestamps are read as UTC, like :func:~ts_shape.eventlog.to_event_log reads the detector frames. | required |
specs | Sequence [ObjectSpec ] | Which signal carries which object type. | required |
qualifiers | Mapping[str, str] | None | Relation qualifier per object type. | None |
uuid_column | str | Signal identifier column. | 'uuid' |
time_column | str | Timestamp column. | 'systime' |
hierarchy | Hierarchy | None | Child -> parent(s) for part_of relations. | None |
infer_o2o | InferO2O | Which object-to-object relations to derive. | True |
scope | Scope | None | {signal_uuid: scope_label} (e.g. the line of each id and event source signal); an event links only to objects of its own scope. | None |
fanout_threshold | int | None | Without scope, warn when an event links to more than this many objects of one type present at the same time; None disables the check. | FANOUT_THRESHOLD |
validate | bool | Check the result against the canonical schema. | True |
uses | Uses | None | Child -> {parent: qualifier} usage links (see :func:detect_objects). | None |
| Type | Description |
|---|---|
EventLog | event_log merged with the detected objects and the new relations. |
relate
relate(
event_log: EventLog,
intervals: pd.DataFrame,
*,
hierarchy: Hierarchy | None = None,
qualifiers: Mapping[str, str] | None = None,
key_columns: Sequence[str] | None = None,
infer_o2o: InferO2O = True,
scope: Scope | None = None,
fanout_threshold: int | None = FANOUT_THRESHOLD,
validate: bool = True,
uses: Uses | None = None
) -> EventLog
Assemble a full OCEL 2.0 log from an event list and an object list.
This is the automated relationship step: given an existing event_log
(with events) and an intervals table describing objects -- columns
oid, type and, for temporal linking, start / end (plus any
attribute columns) -- it derives all relations and returns a validated
:class:EventLog whose five tables are ready to ingest into an OCEL 2.0
database:
objectsfrom the distinct(oid, type)pairs,relations(E2O) via :func:link_events_to_objects(temporal overlap, optionally within ascope, plus optionalkey_columnsmatching),o2ovia :func:derive_o2o(declaredpart_ofhierarchy;co_occurson request),object_changes(lifecycle + captured attributes).
A missing start / end (NaT) means the presence is unbounded on
that side (e.g. an object that is still active). Every column other than
oid / type / start / end / duration_seconds / uuid / source_uuid is an
attribute, recorded at the presence start. A uuid column (the id
signal) enables signal-level hierarchy entries and scope.
Unlike :func:attach_objects, no raw signals or :class:ObjectSpec are
needed -- you bring the two lists, ts-shape derives the relationships.
| Name | Type | Description | Default |
|---|---|---|---|
event_log | EventLog | The events to link. | required |
intervals | DataFrame | Object table with oid, type and optionally start / end, uuid and attribute columns. | required |
hierarchy | Hierarchy | None | Child -> parent(s) for part_of relations, by object type or id-signal uuid (see :func:detect_objects). | None |
qualifiers | Mapping[str, str] | None | Relation qualifier per object type. | None |
key_columns | Sequence[str] | None | Event columns that name an object id directly. | None |
infer_o2o | InferO2O | Which object-to-object relations to derive. | True |
scope | Scope | None | {signal_uuid: scope_label}; see :func:link_events_to_objects. | None |
fanout_threshold | int | None | See :func:link_events_to_objects. | FANOUT_THRESHOLD |
validate | bool | Check the result against the canonical schema. | True |
uses | Uses | None | Child -> {parent: qualifier} usage links (see :func:detect_objects). | None |
| Type | Description |
|---|---|
EventLog | The validated five-table :class:EventLog. |
| Type | Description |
|---|---|
ColumnNotFoundError | When intervals lacks oid or type. |
derive_o2o
derive_o2o(
intervals: pd.DataFrame,
*,
hierarchy: Hierarchy | None = None,
co_occurs: bool = False,
uses: Uses | None = None
) -> pd.DataFrame
Derive object-to-object (O2O) relations from an interval table.
Public entry point over the overlap kernel: a declared hierarchy
(child -> parent or list of parents, each an object type or an id-signal
uuid; see :func:detect_objects) yields part_of from each child
presence to the parent presence it overlaps longest. co_occurs=True
also relates every two overlapping objects of different types with the
symmetric co_occurs qualifier (one row per pair, the type that sorts
first in ocel:oid; pairs related by part_of or uses
excepted). uses (child -> {parent: qualifier}) links each child
presence to every parent presence it overlaps (see
:func:detect_objects).
| Name | Type | Description | Default |
|---|---|---|---|
intervals | DataFrame | Object table with oid, type, start, end (and uuid for signal-level hierarchy entries). | required |
hierarchy | Hierarchy | None | Child -> parent(s). | None |
co_occurs | bool | Also emit co_occurs for every overlap of two types. | False |
uses | Uses | None | Child -> {parent: qualifier} usage links. | None |
| Type | Description |
|---|---|
DataFrame | The OCEL 2.0 o2o table, de-duplicated. |
object_specs_from_metadata
object_specs_from_metadata(
metadata: pd.DataFrame,
*,
type_field: str = "object_type",
value_column_field: str = "object_value_column",
uuid_column: str = "uuid"
) -> list[ObjectSpec]
Build :class:ObjectSpec list from per-signal metadata tags.
Scans a metadata table (e.g. MetadataJsonLoader.to_df()) for a column
naming the object type per signal — so new object types are onboarded by
tagging metadata, not by writing code. The uuid may be the index or a
column; the type/value-column fields are matched directly or with a
config_ / config. prefix (mkdocs metadata flattening).
| Name | Type | Description | Default |
|---|---|---|---|
metadata | DataFrame | Metadata table, one row per signal. | required |
type_field | str | Field naming the object type of a signal; rows without one are skipped. | 'object_type' |
value_column_field | str | Field naming the signal's id column (default value_string); must be one of value_string, value_integer or value_double. | 'object_value_column' |
uuid_column | str | Column (or index name) holding the signal uuid. | 'uuid' |
| Name | Type | Description |
|---|---|---|
One | list [ObjectSpec ] | class:ObjectSpec per tagged signal; empty when the table has |
list [ObjectSpec ] | no uuid or type field. |
| Type | Description |
|---|---|
ValueError | When a value-column entry is not an id column. |
link_events_to_objects
link_events_to_objects(
events: pd.DataFrame,
intervals: pd.DataFrame,
*,
qualifiers: Mapping[str, str] | None = None,
key_columns: Sequence[str] | None = None,
contain: bool = True,
scope: Scope | None = None,
fanout_threshold: int | None = FANOUT_THRESHOLD
) -> pd.DataFrame
Derive all event-to-object (E2O) relations from two lists.
Takes an OCEL events table and an object/interval table (oid,
type and, for temporal linking, start / end) and returns the
OCEL 2.0 relations table -- with no access to the raw signals. Two
complementary strategies are unioned and de-duplicated on
(eid, oid, type):
- temporal overlap (
contain, default on): link an event to every object whose presence overlaps the event. An interval event spansts_shape:start_timestamp..ocel:timestamp(half-open) and links to every object present during a stretch of it -- also one that left before the event ended, and every object of a hand-over inside it; merely touching at an end point is no overlap. A point event (no start, or start == end) links to the objects whose presence contains its timestamp. Presence is half-open[start, end)when another presence of the same signal (uuidcolumn) or, without one, of the same type starts atend-- so a point event at a hand-over instant belongs to the incoming object only -- and closed[start, end]otherwise (the last presence).NaTbounds are open. Requiresstart/endcolumns. - key match (opt-in via
key_columns): link an event to the object whoseoidequals the value in one of the event'skey_columns-- for events that already name their object (e.g. abatchattribute).scopedoes not apply.
Scope. Without scope an event links to the objects of every id
signal, so a machine-state event of line 1 also gets the serials and
batches of line 2. scope={signal_uuid: scope_label} labels the id
signals (the uuid column of intervals, else source_uuid) and
the event source signals (a source_uuid / <pack>:source_uuid
column of events); an event then links only to objects with the same
label. Events and objects without a label are linked to nothing by time,
with a :class:~ts_shape.errors.DataQualityWarning. Without scope,
a :class:~ts_shape.errors.DataQualityWarning suggests one when an
event links to more than fanout_threshold objects of one type
present at the same time (one signal carries one object at a time, so
this means several signals of that type, e.g. two lines).
Naive timestamps are read as UTC. When one table is tz-naive and the
other tz-aware a :class:~ts_shape.errors.DataQualityWarning says so
(local wall-clock times would be linked shifted by the UTC offset).
| Name | Type | Description | Default |
|---|---|---|---|
events | DataFrame | OCEL events table with ocel:eid (and ocel:timestamp, optionally ts_shape:start_timestamp, for temporal linking). | required |
intervals | DataFrame | Object table with oid, type and, for temporal linking, start / end (uuid / source_uuid for scope). | required |
qualifiers | Mapping[str, str] | None | Maps an object type to the relation qualifier to stamp (e.g. {"batch": "processed_in"}); unmapped types get <NA>. | None |
key_columns | Sequence[str] | None | Event columns whose value is an object id. Integral floats (an integer id column with gaps) match "1", not "1.0". | None |
contain | bool | Link by temporal overlap. | True |
scope | Scope | None | {signal_uuid: scope_label} for id and event source signals; links only within one label. | None |
fanout_threshold | int | None | Without scope, warn when an event links to more than this many objects of one type present at the same time (default 1); None disables the check. | FANOUT_THRESHOLD |
| Type | Description |
|---|---|
DataFrame | The OCEL 2.0 relations table. |
| Type | Description |
|---|---|
ColumnNotFoundError | When events lacks ocel:eid (or ocel:timestamp with contain) or intervals lacks oid / type. |