Skip to main content
Version: Next

ts_shape.eventlog.query

Genealogy (traceability) queries and coherent sub-logs of an :class:EventLog.

The object-to-object table o2o holds directed edges ocel:oid -> ocel:oid_2; ts-shape's part_of edges point from the child to the parent (a serial is part_of its batch, a batch part_of its material lot). On that graph:

  • :func:where_used follows the edges backwards, from a parent to everything it went into: where_used(batch) returns the batch's serials, the handling units those serials were packed into, and so on.
  • :func:where_from follows them forwards, from a child to everything it came from: where_from(serial) returns its batch, the batch's material, ...
  • :func:lineage returns both as one frame with a signed depth (negative: where the object came from, positive: where it was used, 0: the object itself).

Each traversal is a breadth-first search over an adjacency built once per call (int codes, CSR arrays); a level is expanded with array operations, so 100k edges take milliseconds. Every object is reported at its shortest distance from the start; cycles terminate because an object is visited once.

o2o carries no validity time in ts-shape. A start / end window therefore restricts the walk to objects present in the window, from the lifecycle rows of object_changes (active -> released); an object without lifecycle rows (presence unknown, e.g. one imported from another system) counts as present. Extra o2o columns, such as a validity time a user-supplied table carries, are passed through on each row.

Functions
NameDescription
where_usedEverything an object went into: its children, their children, ...
where_fromEverything an object came from: its parents, their parents, ...
lineageBoth directions of an object's genealogy as one frame.

where_used​

functionsource
where_used(
log: EventLog,
oid: str,
*,
depth: int | None = None,
start: object = None,
end: object = None,
qualifiers: str | Iterable[str] | None = None
) -> pd.DataFrame

Everything an object went into: its children, their children, ...

Follows o2o edges from ocel:oid_2 back to ocel:oid: for part_of (child -> parent) where_used(batch) returns the batch's serials, then the handling units the serials are part of, and so on.

Parameters
NameTypeDescriptionDefault
logEventLogThe event log.required
oidstrThe object to start from (an ocel:oid).required
depthint | NoneMaximum number of steps; None follows the graph to the end.None
startobjectWindow start (inclusive); naive times are UTC. With start / end only objects present in start, end) (lifecycle rows of object_changes) are reached; objects without lifecycle rows count as present.None
end[objectWindow end (exclusive).None
qualifiersstr | Iterable[str] | NoneThe o2o qualifiers to follow; None follows every qualifier except the symmetric co_occurs.None
Returns
TypeDescription
DataFrameOne row per edge into a newly reached object, sorted by depth,
DataFrameoid, via: oid, object_type (from objects; NA when
DataFramethe oid is only in o2o), qualifier (of the edge), depth
DataFrame(steps from oid, 1 = direct), via (the object one step closer
DataFrameto the start), then any extra o2o columns of the edge. An object
DataFramereached over two edges at its shortest depth (two parents) has two
DataFramerows; the start object is not included.
Raises
TypeDescription
ObjectNotFoundErrorWhen oid is neither in objects nor in o2o (a KeyError).
ValueErrorFor a negative depth or an empty window.
Example
>>> import pandas as pd
>>> from ts_shape.eventlog import EventLog, where_used
>>> o2o = pd.DataFrame({"ocel:oid": ["S1", "S2", "HU1"],
... "ocel:oid_2": ["B1", "B1", "S1"],
... "ocel:qualifier": "part_of"})
>>> where_used(EventLog(o2o=o2o), "B1")
oid object_type qualifier depth via
0 S1 <NA> part_of 1 B1
1 S2 <NA> part_of 1 B1
2 HU1 <NA> part_of 2 S1

where_from​

functionsource
where_from(
log: EventLog,
oid: str,
*,
depth: int | None = None,
start: object = None,
end: object = None,
qualifiers: str | Iterable[str] | None = None
) -> pd.DataFrame

Everything an object came from: its parents, their parents, ...

Follows o2o edges from ocel:oid to ocel:oid_2: for part_of (child -> parent) where_from(serial) returns the serial's batch (and handling unit), the batch's material, and so on.

Parameters
NameTypeDescriptionDefault
logEventLogThe event log.required
oidstrThe object to start from (an ocel:oid).required
depthint | NoneMaximum number of steps; None follows the graph to the end.None
startobjectWindow start (inclusive), as in :func:where_used.None
endobjectWindow end (exclusive).None
qualifiersstr | Iterable[str] | NoneThe o2o qualifiers to follow; None follows every qualifier except the symmetric co_occurs.None
Returns
TypeDescription
DataFrameThe columns of :func:where_used; via is the object one step
DataFramecloser to the start (the child of oid's row).
Raises
TypeDescription
ObjectNotFoundErrorWhen oid is neither in objects nor in o2o (a KeyError).
ValueErrorFor a negative depth or an empty window.

lineage​

functionsource
lineage(
log: EventLog,
oid: str,
*,
depth: int | None = None,
start: object = None,
end: object = None,
qualifiers: str | Iterable[str] | None = None
) -> pd.DataFrame

Both directions of an object's genealogy as one frame.

Parameters
NameTypeDescriptionDefault
logEventLogThe event log.required
oidstrThe object to start from (an ocel:oid).required
depthint | NoneMaximum number of steps in each direction; None: no limit.None
startobjectWindow start (inclusive), as in :func:where_used.None
endobjectWindow end (exclusive).None
qualifiersstr | Iterable[str] | NoneThe o2o qualifiers to follow; None follows every qualifier except the symmetric co_occurs.None
Returns
NameTypeDescription
DataFrameThe columns of :func:where_used with a signed depth: the
DataFramefunc:where_from rows with negative depth (-1 = direct parent),
DataFramea row for oid itself at 0 (qualifier and via NA), and
theDataFramefunc:where_used rows with positive depth. Sorted by depth,
DataFrameoid, via. In a cycle an object can be both an ancestor and a
DataFramedescendant and then appears on both sides.
Raises
TypeDescription
ObjectNotFoundErrorWhen oid is neither in objects nor in o2o (a KeyError).
ValueErrorFor a negative depth or an empty window.