Skip to content

config_io

tit.config_io

JSON serialisation for config dataclasses.

Provides helpers to serialise typed config dataclasses (with Enum fields, nested dataclasses, and union-typed ROI/electrode specs) to JSON files and read them back.

Public API

serialize_config Convert a config dataclass to a JSON-serialisable dict (with project_dir injection). write_config_json Serialise a config dataclass to a temporary JSON file. read_config_json Read a JSON config file and return the parsed dict.

Examples

from tit.config_io import write_config_json, read_config_json path = write_config_json(my_flex_config, prefix="flex") data = read_config_json(path)

See Also

tit.opt.config : FlexConfig and ExConfig dataclasses. tit.sim.config : Montage dataclass.

serialize_config

serialize_config(config: Any) -> dict[str, Any]

Convert a config dataclass to a JSON-serialisable dict.

Handles Enum fields (via .value), nested dataclasses (recursed), union-typed ROI / electrode specs (injects a _type discriminator), and None values (preserved as JSON null).

Also injects project_dir from the active :class:~tit.paths.PathManager so that subprocess entry points can initialise their own singleton.

Parameters

config : dataclass instance Any config dataclass (e.g., FlexConfig, ExConfig).

Returns

dict JSON-serialisable dictionary representation of config.

See Also

write_config_json : Serialise and write to a temp file in one step. read_config_json : Read a JSON config back into a dict.

Source code in tit/config_io.py
def serialize_config(config: Any) -> dict[str, Any]:
    """Convert a config dataclass to a JSON-serialisable dict.

    Handles Enum fields (via ``.value``), nested dataclasses (recursed),
    union-typed ROI / electrode specs (injects a ``_type`` discriminator),
    and *None* values (preserved as JSON ``null``).

    Also injects ``project_dir`` from the active :class:`~tit.paths.PathManager`
    so that subprocess entry points can initialise their own singleton.

    Parameters
    ----------
    config : dataclass instance
        Any config dataclass (e.g., ``FlexConfig``, ``ExConfig``).

    Returns
    -------
    dict
        JSON-serialisable dictionary representation of *config*.

    See Also
    --------
    write_config_json : Serialise and write to a temp file in one step.
    read_config_json : Read a JSON config back into a dict.
    """
    data = _serialize(config)
    # Inject project_dir for subprocess entry points
    from tit.paths import get_path_manager

    data["project_dir"] = get_path_manager().project_dir
    return data

write_config_json

write_config_json(config: Any, prefix: str = 'config') -> str

Serialise a config dataclass to a temporary JSON file.

Parameters

config : dataclass instance Config object to serialise. prefix : str, optional Filename prefix for the temp file. Default is "config".

Returns

str Absolute path to the created JSON file.

See Also

serialize_config : Convert to dict without writing to disk. read_config_json : Read a JSON config file.

Source code in tit/config_io.py
def write_config_json(config: Any, prefix: str = "config") -> str:
    """Serialise a config dataclass to a temporary JSON file.

    Parameters
    ----------
    config : dataclass instance
        Config object to serialise.
    prefix : str, optional
        Filename prefix for the temp file.  Default is ``"config"``.

    Returns
    -------
    str
        Absolute path to the created JSON file.

    See Also
    --------
    serialize_config : Convert to dict without writing to disk.
    read_config_json : Read a JSON config file.
    """
    data = serialize_config(config)
    fd, path = tempfile.mkstemp(prefix=f"{prefix}_", suffix=".json")
    with os.fdopen(fd, "w") as f:
        json.dump(data, f, indent=2)
    return path

read_config_json

read_config_json(path: str) -> dict[str, Any]

Read a JSON config file and return the parsed dict.

Parameters

path : str Path to the JSON file.

Returns

dict Parsed JSON contents.

See Also

write_config_json : Create a config JSON file from a dataclass.

Source code in tit/config_io.py
def read_config_json(path: str) -> dict[str, Any]:
    """Read a JSON config file and return the parsed dict.

    Parameters
    ----------
    path : str
        Path to the JSON file.

    Returns
    -------
    dict
        Parsed JSON contents.

    See Also
    --------
    write_config_json : Create a config JSON file from a dataclass.
    """
    with open(path) as f:
        return json.load(f)