cellmap_flow.models.registry

The model types cellmap-flow knows, and how a model config is built from strings.

Every ModelConfig subclass is a model type: the built-in ones in models/configs (imported from models_config) and any that a plugin defines. The model CLIs (cellmap_flow infer, cellmap_flow serve, cellmap_flow yaml), YAML configs, the dashboard’s model form and the finetune CLI’s model entries all look types up here, so a plugin type is a type everywhere.

Nothing is cached. Plugins are loaded when cellmap_flow is imported, and a plugin or a test can define a type at any time, so each call walks the subclasses as they are at that moment.

Strings become constructor arguments in three ways, kept apart on purpose because each reproduces what its caller has always done:

  • coerce_cli_args: click’s strings. "8,8,8" for a tuple becomes (8, 8, 8), ints.

  • coerce_form_params: the dashboard form’s strings. JSON, or "8,8,8" becomes (8.0, 8.0, 8.0), floats; instantiate_model_config builds the form’s model with them.

  • build_model: YAML values, after YAML_ALIASES and the voxel-size shorthands, then coerce_cli_args; build_models for a YAML’s whole models.

A bad YAML entry or type name is a config.yaml.ConfigError.

A model entry’s env (the environment its server runs in, see models.envs) is not a constructor argument: build_model and instantiate_model_config check it and set it on the built config, and the dashboard’s form offers it beside each type’s arguments. Left out, the type’s default_env applies (ModelConfig.effective_env).

Importing this module imports neither torch, flask, huggingface_hub nor cellmap_flow.globals. The model config classes, which bring numpy and funlib (most of a second), are imported only when a function needs them. describe_types reads constructor signatures only, so it never imports bioimageio, dacapo or cellmap_models either.

Attributes

logger

YAML_ALIASES

ENV_KEY

Functions

cli_name_of(→ str)

The name cls is registered under: its own cli_name, else one from its class name.

model_types(→ Dict[str, type])

Every model type, by the name the CLIs and YAML type: use.

model_type(→ type)

The class registered as name, matched as a YAML type: is.

model_classes(→ Dict[str, type])

Every model type by class name ("ScriptModelConfig"), plugins included.

required_params(→ List[str])

The constructor arguments a user must give: no default, and not name or scale.

build_model(entry, name)

A model config from a YAML model entry; name is its key in the YAML.

build_models(→ list)

The model configs for a YAML's models: build_model for each entry.

coerce_cli_args(→ Dict[str, Any])

kwargs from the command line, as constructor arguments for cls.

click_options(→ List[dict])

The click.option arguments for each of cls's constructor arguments.

print_available_models([cli_command_name])

Print every model type and its constructor arguments, for list-models.

coerce_form_params(→ Dict[str, Any])

The model form's values, as constructor arguments for cls.

instantiate_model_config(→ Any)

The model config the dashboard's model form describes.

parameter_info(→ Dict[str, Any])

Each constructor argument of cls: name, required, description, type and default.

describe_types(→ Dict[str, Dict[str, Any]])

What the dashboard's model form offers, by class name.

Module Contents

cellmap_flow.models.registry.logger
cellmap_flow.models.registry.YAML_ALIASES
cellmap_flow.models.registry.ENV_KEY = 'env'
cellmap_flow.models.registry.cli_name_of(cls: type) → str

The name cls is registered under: its own cli_name, else one from its class name.

Only a cli_name the class sets itself counts. A plugin’s class MyScript(ScriptModelConfig) inherits cli_name = "script"; taking that as its name would either replace the script type or lose the name clash and leave MyScript without a type, and its command would ask the server for a plain ScriptModelConfig. It is “myscript”.

cellmap_flow.models.registry.model_types() → Dict[str, type]

Every model type, by the name the CLIs and YAML type: use.

Plugins included, subclasses of subclasses too. When two classes claim a name the first one found keeps it, and the other is left out with a warning.

cellmap_flow.models.registry.model_type(name: str) → type

The class registered as name, matched as a YAML type: is.

Raises:

ConfigError – no type has that name; the message lists the valid ones.

cellmap_flow.models.registry.model_classes() → Dict[str, type]

Every model type by class name ("ScriptModelConfig"), plugins included.

cellmap_flow.models.registry.required_params(cls) → List[str]

The constructor arguments a user must give: no default, and not name or scale.

cellmap_flow.models.registry.build_model(entry: Dict[str, Any], name: str)

A model config from a YAML model entry; name is its key in the YAML.

Raises:

ConfigError – the entry does not describe a model that can be built.

cellmap_flow.models.registry.build_models(model_entries) → list

The model configs for a YAML’s models: build_model for each entry.

model_entries maps each model’s name to its entry, or is a list of entries that each have a name:

models:
  my_model_1:
    type: cellmap
    checkpoint_path: /path/to/checkpoint
  my_model_2:
    type: dacapo
    run_name: my_run
    iteration: 50000
Raises:

ConfigError – an entry does not describe a model that can be built.

cellmap_flow.models.registry.coerce_cli_args(cls, kwargs: Dict[str, Any]) → Dict[str, Any]

kwargs from the command line, as constructor arguments for cls.

Only list and tuple arguments given as strings are converted (from comma-separated values); None values and names cls does not take are dropped, and everything else is passed through as it is.

cellmap_flow.models.registry.click_options(cls, reserved_short) → List[dict]

The click.option arguments for each of cls’s constructor arguments.

In signature order, which is also the order short flags are handed out in: an argument gets its first letter unless an earlier argument has it already (script -s is --script-path, and --scale has no short flag). reserved_short holds the command’s own short flags (-d and so on), which no argument gets.

cellmap_flow.models.registry.print_available_models(cli_command_name: str = 'cellmap_flow')

Print every model type and its constructor arguments, for list-models.

cli_command_name is the command the closing hint names.

cellmap_flow.models.registry.coerce_form_params(cls, params: Dict[str, Any]) → Dict[str, Any]

The model form’s values, as constructor arguments for cls.

Raises:

ValueError – a required argument is missing or empty.

cellmap_flow.models.registry.instantiate_model_config(class_name: str, params: Dict[str, Any]) → Any

The model config the dashboard’s model form describes.

class_name is a model type’s class name ("ScriptModelConfig"), plugins’ included, and params the form’s values (coerce_form_params).

Raises:

ValueError – the class is unknown, or the values do not build one.

cellmap_flow.models.registry.parameter_info(cls) → Dict[str, Any]

Each constructor argument of cls: name, required, description, type and default.

cellmap_flow.models.registry.describe_types(classes=None) → Dict[str, Dict[str, Any]]

What the dashboard’s model form offers, by class name.

classes (class name -> class) defaults to model_classes().