Skip to content

clabe.data_mapper

DataMapper

Bases: Service, ABC, Generic[TMapTo]

Abstract base class for data mappers.

Defines the interface for mapping data from various sources to specific target formats or schemas.

mapped property

mapped: TMapTo

Retrieves the mapped data object.

Returns:

Type Description
TMapTo

The mapped data object

Raises:

Type Description
ValueError

If the data has not been mapped yet

run

run() -> TMapTo

Executes the data mapping process.

Returns:

Type Description
TMapTo

The mapped data object

Source code in src/clabe/data_mapper/_base.py
@runnable
@final
def run(self) -> TMapTo:
    """
    Executes the data mapping process.

    Returns:
        The mapped data object
    """
    self._mapped = self.map()
    return self._mapped

map abstractmethod

map() -> TMapTo

Maps data to the target schema or format.

Returns:

Type Description
TMapTo

The mapped data object

Source code in src/clabe/data_mapper/_base.py
@abc.abstractmethod
def map(self) -> TMapTo:
    """
    Maps data to the target schema or format.

    Returns:
        The mapped data object
    """

is_mapped

is_mapped() -> bool

Checks if the data has been successfully mapped.

Returns:

Type Description
bool

True if the data is mapped, False otherwise

Source code in src/clabe/data_mapper/_base.py
def is_mapped(self) -> bool:
    """
    Checks if the data has been successfully mapped.

    Returns:
        True if the data is mapped, False otherwise
    """
    return self._mapped is not None

get_cameras

get_cameras(
    rig_instance: Rig,
    exclude_without_video_writer: bool = True,
) -> dict[str, CameraTypes]

Retrieves cameras from a rig instance.

Extracts camera information from camera controllers within the rig model, optionally filtering based on video writer availability.

Parameters:

Name Type Description Default
rig_instance Rig

The rig model instance containing camera controllers

required
exclude_without_video_writer bool

If True, exclude cameras without a video writer

True

Returns:

Type Description
dict[str, CameraTypes]

A dictionary mapping camera names to their types

Source code in src/clabe/data_mapper/helpers.py
def get_cameras(rig_instance: Rig, exclude_without_video_writer: bool = True) -> dict[str, CameraTypes]:
    """
    Retrieves cameras from a rig instance.

    Extracts camera information from camera controllers within the rig model,
    optionally filtering based on video writer availability.

    Args:
        rig_instance: The rig model instance containing camera controllers
        exclude_without_video_writer: If True, exclude cameras without a video writer

    Returns:
        A dictionary mapping camera names to their types
    """
    cameras: dict[str, CameraTypes] = {}
    camera_controllers = [x[1] for x in get_fields_of_type(rig_instance, CameraController)]

    for controller in camera_controllers:
        if exclude_without_video_writer:
            these_cameras = {k: v for k, v in controller.cameras.items() if v.video_writer is not None}
        else:
            these_cameras = controller.cameras
        cameras.update(these_cameras)
    return cameras

snapshot_bonsai_environment

snapshot_bonsai_environment(
    config_file: PathLike = Path("./bonsai/bonsai.config"),
) -> dict[str, str]

Captures a snapshot of the Bonsai environment from a configuration file.

Parses the Bonsai configuration file to extract information about installed packages and their versions.

Parameters:

Name Type Description Default
config_file PathLike

Path to the Bonsai configuration file

Path('./bonsai/bonsai.config')

Returns:

Type Description
dict[str, str]

A dictionary of package IDs and their versions

Source code in src/clabe/data_mapper/helpers.py
def snapshot_bonsai_environment(
    config_file: os.PathLike = Path("./bonsai/bonsai.config"),
) -> dict[str, str]:
    """
    Captures a snapshot of the Bonsai environment from a configuration file.

    Parses the Bonsai configuration file to extract information about installed
    packages and their versions.

    Args:
        config_file: Path to the Bonsai configuration file

    Returns:
        A dictionary of package IDs and their versions
    """
    tree = ET.parse(Path(config_file))
    root = tree.getroot()
    packages = root.findall("Packages/Package")
    return {leaf.attrib["id"]: leaf.attrib["version"] for leaf in packages}

snapshot_python_environment

snapshot_python_environment() -> dict[str, str]

Captures a snapshot of the current Python environment.

Creates a record of all currently installed Python packages and their versions, useful for reproducibility and debugging purposes. Inside a run, these are the distributions the run's manifest (run.json) recorded, so the mapped metadata and the manifest agree.

Returns:

Type Description
dict[str, str]

A dictionary of package names and their versions

Source code in src/clabe/data_mapper/helpers.py
def snapshot_python_environment() -> dict[str, str]:
    """
    Captures a snapshot of the current Python environment.

    Creates a record of all currently installed Python packages and their versions,
    useful for reproducibility and debugging purposes. Inside a run, these are the distributions the run's
    manifest (``run.json``) recorded, so the mapped metadata and the manifest agree.

    Returns:
        A dictionary of package names and their versions
    """
    manifest = _current_manifest()
    if manifest is not None and manifest.environment is not None:
        return dict(manifest.environment.distributions)
    return {dist.name: dist.version for dist in metadata.distributions()}