Source code for vivyd.data.source

from __future__ import annotations

from .container import Container
from .signal import Signal
from .value import Value

from dataclasses import dataclass
from typing import Any
from zarr.storage import ZipStore


[docs] @dataclass(kw_only=True) class Source(Container): """ A class representing a data source, which corresponds to any source of data/information. A source can contain signals and values, which are the actual data. For example, a source could represent a specific sensor or measurement device. Parameters ---------- name : str The name of the source. info : dict[str, Any] Additional metadata about the source, such as experimental conditions, measurement techniques, etc. This is a flexible field that can hold any relevant information that does not fit into the predefined fields. """
[docs] def add(self, *obj: Signal | Value) -> Source: """ Add one or more signals or values to the source. Parameters ---------- *obj : Signal | Value One or more signals or values to add to the source. Returns ------- Source The source with the added contents. """ for o in obj: super().add(o.name, o) return self
@staticmethod def _from_dict(data: dict[str, Any], store: ZipStore) -> Source: new = Source( name = data["name"], info = data["info"] ) for obj in data["contents"]: match obj["type"]: case "Signal": new.add(Signal._from_dict(obj, store)) case "Value": new.add(Value._from_dict(obj)) case _: raise ValueError(f"Unsupported type '{obj['type']}' found in source '{new.name}'. Supported types are 'Signal' and 'Value'.") return new