Skip to main content
Version: 0.0.98

ts_shape.loader.metadata.metadata_json_loader

Classes
NameDescription
MetadataJsonLoaderLoad metadata JSON of shape:

MetadataJsonLoader​

classsource
MetadataJsonLoader(json_data: Any, *, strict: bool = True)

into a pandas DataFrame with flattened config columns.

Functions
NameDescription
from_fileCreate a loader from a JSON file on disk.
from_strCreate a loader from a JSON string.
from_flat_frameCreate a loader from a flat datapoint table.
from_workbookCreate a loader from a datapoint workbook (.xlsx).
deliveringDatapoints that are enabled and not archived.
unitsuuid -> unit text of the datapoints that name a unit.
scalingscaling_factor and scaling_offset per uuid (1 and 0 if unset).
to_dfReturn the underlying DataFrame.
headConvenience wrapper for DataFrame.head.
get_by_uuidRetrieve a row by UUID as a dictionary.
get_by_labelRetrieve the row matching a label as a dictionary.
get_all_by_labelEvery row matching a label, as dictionaries (with uuid).
duplicate_labelsLabels used by more than one datapoint.
join_withJoin the metadata DataFrame with another DataFrame on the 'uuid' index.
filter_by_uuidFilter rows by a set or sequence of UUIDs.
filter_by_labelFilter rows by a set or sequence of labels.
list_uuidsReturn a list of UUIDs present in the metadata index.
list_labelsReturn all non-null labels.
Parameters
NameTypeDescriptionDefault
json_dataAnyOne 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
strictboolIf 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
TypeDescription
ValueErrorFor columnar input without config, missing or duplicate UUIDs, a record without label (strict), columns that do not line up (strict), or empty input (strict).
TypeErrorFor an unsupported structure (strict).

from_file​

classmethodsource
from_file(filepath: str, *, strict: bool = True) -> Self

Create a loader from a JSON file on disk.

Parameters
NameTypeDescriptionDefault
filepathstrPath to the JSON file.required
strictboolValidation behavior; when True, enforces required fields and unique UUIDs.True
Returns
TypeDescription
SelfMetadataJsonLoader instance.

from_str​

classmethodsource
from_str(json_str: str, *, strict: bool = True) -> Self

Create a loader from a JSON string.

Parameters
NameTypeDescriptionDefault
json_strstrRaw JSON content as a string.required
strictboolValidation behavior; when True, enforces required fields and unique UUIDs.True
Returns
TypeDescription
SelfMetadataJsonLoader instance.

from_flat_frame​

classmethodsource
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
NameTypeDescriptionDefault
frameDataFrameThe flat table.required
strictboolValidation behavior (see the constructor); default False, as workbooks often lack a label on some rows.False
Returns
TypeDescription
SelfMetadataJsonLoader instance.

from_workbook​

classmethodsource
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
NameTypeDescriptionDefault
filepathstrPath to the workbook.required
sheetint | strSheet position (0 = first) or name.0
strictboolValidation behavior (see the constructor).False
Returns
TypeDescription
SelfMetadataJsonLoader instance.

delivering​

methodsource
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
TypeDescription
DataFrameThe rows of :meth:to_df that are enabled and not archived.

units​

methodsource
units() -> pd.Series

uuid -> unit text of the datapoints that name a unit.

scaling​

methodsource
scaling() -> pd.DataFrame

scaling_factor and scaling_offset per uuid (1 and 0 if unset).

to_df​

methodsource
to_df(copy: bool = True) -> pd.DataFrame

Return the underlying DataFrame.

Parameters
NameTypeDescriptionDefault
copyboolWhen True, returns a copy; otherwise returns a view/reference.True
Returns
TypeDescription
DataFramepandas DataFrame indexed by 'uuid'.

head​

methodsource
head(n: int = 5) -> pd.DataFrame

Convenience wrapper for DataFrame.head.

Parameters
NameTypeDescriptionDefault
nintNumber of rows to return.5
Returns
TypeDescription
DataFrameTop n rows of the metadata DataFrame.

get_by_uuid​

methodsource
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
NameTypeDescriptionDefault
uuidstrUUID key (index) to look up.required
Returns
TypeDescription
dict[str, Any] | NoneRow as a dict, or None if not present.

get_by_label​

methodsource
get_by_label(label: str) -> dict[str, Any] | None

Retrieve the row matching a label as a dictionary.

Parameters
NameTypeDescriptionDefault
labelstrLabel value to search for.required
Returns
TypeDescription
dict[str, Any] | NoneRow as a dict, or None if not found.
Raises
TypeDescription
ValueErrorWhen 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​

methodsource
get_all_by_label(label: str) -> list[dict[str, Any]]

Every row matching a label, as dictionaries (with uuid).

Parameters
NameTypeDescriptionDefault
labelstrLabel value to search for.required
Returns
TypeDescription
list[dict[str, Any]]One dict per matching datapoint; empty when none matches.

duplicate_labels​

methodsource
duplicate_labels() -> pd.DataFrame

Labels used by more than one datapoint.

Returns
TypeDescription
DataFrameDataFrame with label, count and uuids (a list), sorted
DataFrameby label; empty when every label is unique.

join_with​

methodsource
join_with(other_df: pd.DataFrame, how: str = 'inner') -> pd.DataFrame

Join the metadata DataFrame with another DataFrame on the 'uuid' index.

Parameters
NameTypeDescriptionDefault
other_dfDataFrameDataFrame to join with (must be indexed compatibly).required
howstrJoin strategy (e.g., 'inner', 'left', 'outer').'inner'
Returns
TypeDescription
DataFrameJoined pandas DataFrame.

filter_by_uuid​

methodsource
filter_by_uuid(uuids: Iterable[str]) -> pd.DataFrame

Filter rows by a set or sequence of UUIDs.

Parameters
NameTypeDescriptionDefault
uuidsIterable[str]Iterable of UUID strings to retain.required
Returns
TypeDescription
DataFrameFiltered DataFrame.

filter_by_label​

methodsource
filter_by_label(labels: Iterable[str]) -> pd.DataFrame

Filter rows by a set or sequence of labels.

Parameters
NameTypeDescriptionDefault
labelsIterable[str]Iterable of label strings to retain.required
Returns
TypeDescription
DataFrameFiltered DataFrame.

list_uuids​

methodsource
list_uuids() -> list[str]

Return a list of UUIDs present in the metadata index.

Returns
TypeDescription
list[str]List of UUID strings.

list_labels​

methodsource
list_labels() -> list[str]

Return all non-null labels.

Returns
TypeDescription
list[str]List of label strings.