RelatedTables#

class sdm.relational.RelatedTables(tables: Mapping[str, T], relationships: Collection[Relationship | Mapping[str, str | Sequence[str]]], task_links: Collection[TaskLink | Mapping[str, str | Sequence[str]]])#

Bases: DeviceMixin, Generic[T]

Task-specific related tables attached to model inputs.

RelatedTables store the relational context provided to a model for a particular task table. It may contain a sampled subset of a larger RelationalData. The task_links describe how rows in the model input match to rows in the related tables.

from sdm import RelatedTables, TableTensor

data = RelatedTables(
    tables={
        "users": TableTensor.from_columns(
            {"user_id": [0, 1]},
            stypes={"user_id": "id"},
        ),
        "orders": TableTensor.from_columns(
            {
                "user_id": [0, 1],
                "item_id": [10, 11],
            },
            stypes={
                "user_id": "id",
                "item_id": "id",
            },
        ),
        "items": TableTensor.from_columns(
            {"item_id": [10, 11]},
            stypes={"item_id": "id"},
        ),
    },
    relationships=[
        # Foreign key from orders to users:
        dict(left_table="orders", left_column="user_id", right_table="users", right_column="user_id"),
        # Foreign key from orders to items:
        dict(left_table="orders", left_column="item_id", right_table="items", right_column="item_id"),
    ],
    task_links=[
        # Foreign key in the task table to users:
        dict(task_column="ENTITY", table="users", table_column="user_id")
    ],
)
Parameters:
  • tables (Mapping[str, T]) – Related tables keyed by table name.

  • relationships (tuple[Relationship, ...]) – Join relationships among tables.

  • task_links (tuple[TaskLink, ...]) – Links from task columns to related tables.

property schema: RelatedTablesSchema#

The schema of this related context.

is_same_schema(other: RelatedTables) → bool#

Whether other has the same schema layout.

Parameters:

other (RelatedTables) – The object to compare against.

Return type:

bool

select_tables(tables: Iterable[str]) → Self#

Return related tables containing only tables.

Parameters:

tables (Iterable[str]) – The table names to select.

Return type:

Self

replace_tables(tables: Mapping[str, T]) → Self#

Return related tables with replaced table data.

Parameters:

tables (Mapping[str, T]) – Related tables keyed by table name.

Return type:

Self

to_graphviz(*, hide_columns: bool = False, **kwargs: Any) → graphviz.Graph#

Return a task visualization of the relational schema.

Parameters:
  • hide_columns (bool) – Whether to hide column name descriptions.

  • **kwargs (Any) – Additional keyword arguments passed to graphviz.Graph.

Return type:

graphviz.Graph