ts_shape.loader.metadata.metadata_json_loader
Classes
MetadataJsonLoader(json_data: Any, *, strict: bool = True)
Load metadata JSON of shape
{
"uuid": {"0": "...", "1": "...", ...},
"label": {"0": "...", "1": "...", ...},
"config": {"0": {...}, "1": {...}, ...}
}
into a pandas DataFrame with flattened config columns.
Functions
| Name | Description |
|---|
from_file | Create a loader from a JSON file on disk. |
from_str | Create a loader from a JSON string. |
from_flat_frame | Create a loader from a flat datapoint table. |
from_workbook | Create a loader from a datapoint workbook (.xlsx). |
delivering | Datapoints that are enabled and not archived. |
units | uuid -> unit text of the datapoints that name a unit. |
scaling | scaling_factor and scaling_offset per uuid (1 and 0 if unset). |
to_df | Return the underlying DataFrame. |
head | Convenience wrapper for DataFrame.head. |
get_by_uuid | Retrieve a row by UUID as a dictionary. |
get_by_label | Retrieve the row matching a label as a dictionary. |
get_all_by_label | Every row matching a label, as dictionaries (with uuid). |
duplicate_labels | Labels used by more than one datapoint. |
join_with | Join the metadata DataFrame with another DataFrame on the 'uuid' index. |
filter_by_uuid | Filter rows by a set or sequence of UUIDs. |
filter_by_label | Filter rows by a set or sequence of labels. |
list_uuids | Return a list of UUIDs present in the metadata index. |
list_labels | Return all non-null labels. |
Parameters
| Name | Type | Description | Default |
|---|
json_data | Any | One of three shapes: a dict of columns with index maps ({"uuid": {"0": ...}, "label": {...}, "config": {...}}), a dict of columns with lists ({"uuid": [...], ...}), or a list of records ([{"uuid": ..., "label": ..., "config": {...}}]). | required |
strict | bool | If True, enforce presence of required keys, a label on every record and unique UUIDs, and reject empty input. For columnar input the columns must also line up: lists of equal length, index maps with the same keys. With False, a record missing from label or config gets an empty label / config, and one missing from uuid is dropped. | True |
Raises
| Type | Description |
|---|
ValueError | For columnar input without config, missing or duplicate UUIDs, a record without label (strict), columns that do not line up (strict), or empty input (strict). |
TypeError | For an unsupported structure (strict). |
from_file(filepath: str, *, strict: bool = True) -> Self
Create a loader from a JSON file on disk.
Parameters
| Name | Type | Description | Default |
|---|
filepath | str | Path to the JSON file. | required |
strict | bool | Validation behavior; when True, enforces required fields and unique UUIDs. | True |
Returns
| Type | Description |
|---|
Self | MetadataJsonLoader instance. |
from_str(json_str: str, *, strict: bool = True) -> Self
Create a loader from a JSON string.
Parameters
| Name | Type | Description | Default |
|---|
json_str | str | Raw JSON content as a string. | required |
strict | bool | Validation behavior; when True, enforces required fields and unique UUIDs. | True |
Returns
| Type | Description |
|---|
Self | MetadataJsonLoader instance. |
from_flat_frame(frame: pd.DataFrame, *, strict: bool = False) -> Self
Create a loader from a flat datapoint table.
The table has uuid, label and the configuration as
config.<key> (or config.<key>[type]) columns -- the layout of
a plant's datapoint workbook. The config.* columns are nested
back into config; other columns (is_enabled,
scaling_factor ...) stay at the top level.
Parameters
| Name | Type | Description | Default |
|---|
frame | DataFrame | The flat table. | required |
strict | bool | Validation behavior (see the constructor); default False, as workbooks often lack a label on some rows. | False |
Returns
| Type | Description |
|---|
Self | MetadataJsonLoader instance. |
from_workbook(
filepath: str, *, sheet: int | str = 0, strict: bool = False
) -> Self
Create a loader from a datapoint workbook (.xlsx).
Reads the sheet with the standard library only
(:func:ts_shape.loader.metadata.workbook.read_workbook) and nests
its config.* columns (see :meth:from_flat_frame).
Parameters
| Name | Type | Description | Default |
|---|
filepath | str | Path to the workbook. | required |
sheet | int | str | Sheet position (0 = first) or name. | 0 |
strict | bool | Validation behavior (see the constructor). | False |
Returns
| Type | Description |
|---|
Self | MetadataJsonLoader instance. |
delivering() -> pd.DataFrame
Datapoints that are enabled and not archived.
A workbook's is_enabled alone does not tell which datapoints
deliver data: an archived datapoint can still be enabled. Missing
flags count as enabled / not archived.
Returns
| Type | Description |
|---|
DataFrame | The rows of :meth:to_df that are enabled and not archived. |
uuid -> unit text of the datapoints that name a unit.
scaling() -> pd.DataFrame
scaling_factor and scaling_offset per uuid (1 and 0 if unset).
to_df(copy: bool = True) -> pd.DataFrame
Return the underlying DataFrame.
Parameters
| Name | Type | Description | Default |
|---|
copy | bool | When True, returns a copy; otherwise returns a view/reference. | True |
Returns
| Type | Description |
|---|
DataFrame | pandas DataFrame indexed by 'uuid'. |
head(n: int = 5) -> pd.DataFrame
Convenience wrapper for DataFrame.head.
Parameters
| Name | Type | Description | Default |
|---|
n | int | Number of rows to return. | 5 |
Returns
| Type | Description |
|---|
DataFrame | Top n rows of the metadata DataFrame. |
get_by_uuid(uuid: str) -> dict[str, Any] | None
Retrieve a row by UUID as a dictionary.
UUIDs are compared as text, so get_by_uuid(1) finds "1".
Parameters
| Name | Type | Description | Default |
|---|
uuid | str | UUID key (index) to look up. | required |
Returns
| Type | Description |
|---|
dict[str, Any] | None | Row as a dict, or None if not present. |
get_by_label(label: str) -> dict[str, Any] | None
Retrieve the row matching a label as a dictionary.
Parameters
| Name | Type | Description | Default |
|---|
label | str | Label value to search for. | required |
Returns
| Type | Description |
|---|
dict[str, Any] | None | Row as a dict, or None if not found. |
Raises
| Type | Description |
|---|
ValueError | When several datapoints carry the label (a label alone does not identify a signal then); the message lists their uuids. Use :meth:get_all_by_label or :meth:get_by_uuid. |
get_all_by_label(label: str) -> list[dict[str, Any]]
Every row matching a label, as dictionaries (with uuid).
Parameters
| Name | Type | Description | Default |
|---|
label | str | Label value to search for. | required |
Returns
| Type | Description |
|---|
list[dict[str, Any]] | One dict per matching datapoint; empty when none matches. |
duplicate_labels() -> pd.DataFrame
Labels used by more than one datapoint.
Returns
| Type | Description |
|---|
DataFrame | DataFrame with label, count and uuids (a list), sorted |
DataFrame | by label; empty when every label is unique. |
join_with(other_df: pd.DataFrame, how: str = 'inner') -> pd.DataFrame
Join the metadata DataFrame with another DataFrame on the 'uuid' index.
Parameters
| Name | Type | Description | Default |
|---|
other_df | DataFrame | DataFrame to join with (must be indexed compatibly). | required |
how | str | Join strategy (e.g., 'inner', 'left', 'outer'). | 'inner' |
Returns
| Type | Description |
|---|
DataFrame | Joined pandas DataFrame. |
filter_by_uuid(uuids: Iterable[str]) -> pd.DataFrame
Filter rows by a set or sequence of UUIDs.
Parameters
| Name | Type | Description | Default |
|---|
uuids | Iterable[str] | Iterable of UUID strings to retain. | required |
Returns
| Type | Description |
|---|
DataFrame | Filtered DataFrame. |
filter_by_label(labels: Iterable[str]) -> pd.DataFrame
Filter rows by a set or sequence of labels.
Parameters
| Name | Type | Description | Default |
|---|
labels | Iterable[str] | Iterable of label strings to retain. | required |
Returns
| Type | Description |
|---|
DataFrame | Filtered DataFrame. |
list_uuids() -> list[str]
Return a list of UUIDs present in the metadata index.
Returns
| Type | Description |
|---|
list[str] | List of UUID strings. |
list_labels() -> list[str]
Return all non-null labels.
Returns
| Type | Description |
|---|
list[str] | List of label strings. |