Skip to content

Configuration

Source

Pydantic CAREamics configuration.

Configuration

Bases: BaseModel

CAREamics configuration.

The configuration defines all parameters used to build and train a CAREamics model. These parameters are validated to ensure that they are compatible with each other.

It contains three sub-configurations:

  • AlgorithmModel: configuration for the algorithm training, which includes the architecture, loss function, optimizer, and other hyperparameters.
  • DataModel: configuration for the dataloader, which includes the type of data, transformations, mean/std and other parameters.
  • TrainingModel: configuration for the training, which includes the number of epochs or the callbacks.

Attributes:

  • experiment_name (str) –

    Name of the experiment, used when saving logs and checkpoints.

  • algorithm (AlgorithmModel) –

    Algorithm configuration.

  • data (DataModel) –

    Data configuration.

  • training (TrainingModel) –

    Training configuration.

Methods:

  • set_3D

    Switch configuration between 2D and 3D.

  • model_dump

    exclude_defaults: bool = False, exclude_none: bool = True, **kwargs: Dict ) -> Dict Export configuration to a dictionary.

Raises:

  • ValueError

    Configuration parameter type validation errors.

  • ValueError

    If the experiment name contains invalid characters or is empty.

  • ValueError

    If the algorithm is 3D but there is not "Z" in the data axes, or 2D algorithm with "Z" in data axes.

  • ValueError

    Algorithm, data or training validation errors.

Notes

We provide convenience methods to create standards configurations, for instance:

from careamics.compat.config import create_n2v_configuration config = create_n2v_configuration( ... experiment_name="n2v_experiment", ... data_type="array", ... axes="YX", ... patch_size=[64, 64], ... batch_size=32, ... )

The configuration can be exported to a dictionary using the model_dump method:

config_dict = config.model_dump()

Configurations can also be exported or imported from yaml files:

from careamics.compat.config.utils.configuration_io import save_configuration from careamics.compat.config.utils.configuration_io import load_configuration path_to_config = save_configuration(config, my_path / "config.yml") other_config = load_configuration(path_to_config)

Examples:

Minimum example:

>>> from careamics.compat.config import Configuration
>>> config_dict = {
...         "experiment_name": "N2V_experiment",
...         "algorithm_config": {
...             "algorithm": "n2v",
...             "loss": "n2v",
...             "model": {
...                 "architecture": "UNet",
...             },
...         },
...         "training_config": {},
...         "data_config": {
...             "data_type": "tiff",
...             "patch_size": [64, 64],
...             "axes": "SYX",
...         },
...     }
>>> config = Configuration(**config_dict)

algorithm_config = Field(discriminator='algorithm') class-attribute instance-attribute

Algorithm configuration, holding all parameters required to configure the model.

data_config instance-attribute

Data configuration, holding all parameters required to configure the training data loader.

experiment_name instance-attribute

Name of the experiment, used to name logs and checkpoints.

training_config instance-attribute

Training configuration, holding all parameters required to configure the training process.

version = '0.1.0' class-attribute instance-attribute

CAREamics configuration version.

__str__()

Pretty string reprensenting the configuration.

Returns:

  • str

    Pretty string.

get_algorithm_citations()

Return a list of citation entries of the current algorithm.

This is used to generate the model description for the BioImage Model Zoo.

Returns:

  • List[CiteEntry]

    List of citation entries.

get_algorithm_description()

Return a description of the algorithm.

This method is used to generate the README of the BioImage Model Zoo export.

Returns:

  • str

    Description of the algorithm.

get_algorithm_friendly_name()

Get the algorithm name.

Returns:

  • str

    Algorithm name.

get_algorithm_keywords()

Get algorithm keywords.

Returns:

get_algorithm_references()

Get the algorithm references.

This is used to generate the README of the BioImage Model Zoo export.

Returns:

  • str

    Algorithm references.

get_safe_experiment_name()

Return the experiment name safe for use in paths and filenames.

Spaces are replaced with underscores to avoid issues with folder creation and checkpoint naming.

Returns:

  • str

    Experiment name with spaces replaced with underscores.

model_dump(**kwargs)

Override model_dump method in order to set default values.

As opposed to the parent model_dump method, this method sets exclude none by default.

Parameters:

  • **kwargs (Any, default: {} ) –

    Additional arguments to pass to the parent model_dump method.

Returns:

  • dict

    Dictionary containing the model parameters.

no_symbol(name) classmethod

Validate experiment name.

A valid experiment name is a non-empty string with only contains letters, numbers, underscores, dashes and spaces.

Parameters:

  • name (str) –

    Name to validate.

Returns:

  • str

    Validated name.

Raises:

  • ValueError

    If the name is empty or contains invalid characters.

set_3D(is_3D, axes, patch_size)

Set 3D flag and axes.

Parameters:

  • is_3D (bool) –

    Whether the algorithm is 3D or not.

  • axes (str) –

    Axes of the data.

  • patch_size (list[int]) –

    Patch size.

validate_3D()

Change algorithm dimensions to match data.axes.

Returns:

  • Self

    Validated configuration.

validate_n2v_mask_pixel_perc()

Validate that there will always be at least one blind-spot pixel in every patch.

The probability of creating a blind-spot pixel is a function of the chosen masked pixel percentage and patch size.

Returns:

  • Self

    Validated configuration.

Raises:

  • ValueError

    If the probability of masking a pixel within a patch is less than 1 for the chosen masked pixel percentage and patch size.