Skip to content

model_description

Module use to build BMZ model description.

create_model_description(config, name, general_description, authors, inputs, outputs, weights_path, torch_version, careamics_version, config_path, env_path, channel_names=None, data_description=None) #

Create model description.

Parameters:

Name Type Description Default
config Configuration

CAREamics configuration.

required
name str

Name of the model.

required
general_description str

General description of the model.

required
authors List[Author]

Authors of the model.

required
inputs Union[Path, str]

Path to input .npy file.

required
outputs Union[Path, str]

Path to output .npy file.

required
weights_path Union[Path, str]

Path to model weights.

required
torch_version str

Pytorch version.

required
careamics_version str

CAREamics version.

required
config_path Union[Path, str]

Path to model configuration.

required
env_path Union[Path, str]

Path to environment file.

required
channel_names Optional[List[str]]

Channel names, by default None.

None
data_description Optional[str]

Description of the data, by default None.

None

Returns:

Type Description
ModelDescr

Model description.

Source code in src/careamics/model_io/bioimage/model_description.py
def create_model_description(
    config: Configuration,
    name: str,
    general_description: str,
    authors: List[Author],
    inputs: Union[Path, str],
    outputs: Union[Path, str],
    weights_path: Union[Path, str],
    torch_version: str,
    careamics_version: str,
    config_path: Union[Path, str],
    env_path: Union[Path, str],
    channel_names: Optional[List[str]] = None,
    data_description: Optional[str] = None,
) -> ModelDescr:
    """Create model description.

    Parameters
    ----------
    config : Configuration
        CAREamics configuration.
    name : str
        Name of the model.
    general_description : str
        General description of the model.
    authors : List[Author]
        Authors of the model.
    inputs : Union[Path, str]
        Path to input .npy file.
    outputs : Union[Path, str]
        Path to output .npy file.
    weights_path : Union[Path, str]
        Path to model weights.
    torch_version : str
        Pytorch version.
    careamics_version : str
        CAREamics version.
    config_path : Union[Path, str]
        Path to model configuration.
    env_path : Union[Path, str]
        Path to environment file.
    channel_names : Optional[List[str]], optional
        Channel names, by default None.
    data_description : Optional[str], optional
        Description of the data, by default None.

    Returns
    -------
    ModelDescr
        Model description.
    """
    # documentation
    doc = readme_factory(
        config,
        careamics_version=careamics_version,
        data_description=data_description,
    )

    # inputs, outputs
    input_descr, output_descr = _create_inputs_ouputs(
        input_array=np.load(inputs),
        output_array=np.load(outputs),
        data_config=config.data_config,
        input_path=inputs,
        output_path=outputs,
        channel_names=channel_names,
    )

    # weights description
    architecture_descr = ArchitectureFromLibraryDescr(
        import_from="careamics.models.unet",
        callable=f"{config.algorithm_config.model.architecture}",
        kwargs=config.algorithm_config.model.model_dump(),
    )

    weights_descr = WeightsDescr(
        pytorch_state_dict=PytorchStateDictWeightsDescr(
            source=weights_path,
            architecture=architecture_descr,
            pytorch_version=Version(torch_version),
            dependencies=EnvironmentFileDescr(source=env_path),
        ),
    )

    # overall model description
    model = ModelDescr(
        name=name,
        authors=authors,
        description=general_description,
        documentation=doc,
        inputs=[input_descr],
        outputs=[output_descr],
        tags=config.get_algorithm_keywords(),
        links=[
            "https://github.com/CAREamics/careamics",
            "https://careamics.github.io/latest/",
        ],
        license="BSD-3-Clause",
        version="0.1.0",
        weights=weights_descr,
        attachments=[FileDescr(source=config_path)],
        cite=config.get_algorithm_citations(),
        config={  # conversion from float32 to float64 creates small differences...
            "bioimageio": {
                "test_kwargs": {
                    "pytorch_state_dict": {
                        "decimals": 0,  # ...so we relax the constraints on the decimals
                    }
                }
            }
        },
    )

    return model

extract_model_path(model_desc) #

Return the relative path to the weights and configuration files.

Parameters:

Name Type Description Default
model_desc ModelDescr

Model description.

required

Returns:

Type Description
Tuple[Path, Path]

Weights and configuration paths.

Source code in src/careamics/model_io/bioimage/model_description.py
def extract_model_path(model_desc: ModelDescr) -> Tuple[Path, Path]:
    """Return the relative path to the weights and configuration files.

    Parameters
    ----------
    model_desc : ModelDescr
        Model description.

    Returns
    -------
    Tuple[Path, Path]
        Weights and configuration paths.
    """
    weights_path = model_desc.weights.pytorch_state_dict.source.path

    if len(model_desc.attachments) == 1:
        config_path = model_desc.attachments[0].source.path
    else:
        for file in model_desc.attachments:
            if file.source.path.suffix == ".yml":
                config_path = file.source.path
                break

        if config_path is None:
            raise ValueError("Configuration file not found.")

    return weights_path, config_path