Skip to content

Model run

ModelRun puts a model configuration together with a run period and an output directory, and generates, runs and postprocesses the model.

Model run implementation for ROMPY.

This module provides the ModelRun class which is the main entry point for running models with ROMPY.

logger module-attribute

logger = get_logger(__name__)

CONFIG_TYPES module-attribute

CONFIG_TYPES = load_entry_points('rompy.config')

ModelRun pydantic-model

ModelRun(**data: Any)

Bases: RompyBaseModel

A model run.

It is intented to be model agnostic. It deals primarily with how the model is to be run, i.e. the period of the run and where the output is going. The actual configuration of the run is provided by the config object.

Further explanation is given in the rompy.core.Baseconfig docstring.

Config:

  • protected_namespaces: ()
  • extra: forbid

Fields:

model_type pydantic-field

model_type: Literal['modelrun'] = 'modelrun'

The model type.

run_id pydantic-field

run_id: str = 'run_id'

The run id

period pydantic-field

period: TimeRange = TimeRange(
    start=datetime(2020, 2, 21, 4),
    end=datetime(2020, 2, 24, 4),
    interval="15M",
)

The time period to run the model

output_dir pydantic-field

output_dir: Path = './simulations'

The output directory

config pydantic-field

The configuration object

delete_existing pydantic-field

delete_existing: bool = False

Delete existing output directory

run_id_subdir pydantic-field

run_id_subdir: bool = True

Use run_id subdirectory in the output directory

staging_dir property

staging_dir

The directory where the model is staged for execution

Returns:

Name Type Description
staging_dir str

generate

generate() -> str

Generate the model input files

Returns:

Name Type Description
staging_dir str

zip

zip() -> str

Zip the input files for the model run

This function zips the input files for the model run and returns the name of the zip file. It also cleans up the staging directory leaving only the settings.json file that can be used to reproduce the run.

Returns:

Name Type Description
zip_fn str

run

run(
    backend: BackendConfig,
    workspace_dir: Optional[str] = None,
) -> bool

Run the model using the specified backend configuration.

This method uses Pydantic configuration objects that provide type safety and validation for all backend parameters.

Parameters:

Name Type Description Default
backend BackendConfig

Pydantic configuration object (LocalConfig, DockerConfig, etc.)

required
workspace_dir Optional[str]

Path to generated workspace directory (optional)

None

Returns:

Type Description
bool

True if execution was successful, False otherwise

Raises:

Type Description
TypeError

If backend is not a BackendConfig instance

Examples:

from rompy.backends import LocalConfig, DockerConfig

Local execution

model.run(LocalConfig(timeout=3600, command="python run.py"))

Docker execution

model.run(DockerConfig(image="swan:latest", cpu=4, memory="2g"))

postprocess

postprocess(
    processor: BasePostprocessorConfig, **kwargs: Any
) -> Dict[str, Any]

Postprocess the model outputs using the specified processor configuration.

This method uses the provided configuration to instantiate and execute the appropriate postprocessor. The processor type is determined by the configuration object.

Parameters:

Name Type Description Default
processor BasePostprocessorConfig

Configuration object for the postprocessor to use

required
**kwargs Any

Additional processor-specific parameters (override config values)

{}

Returns:

Type Description
Dict[str, Any]

Dictionary with results from the postprocessing

Raises:

Type Description
TypeError

If processor is not a BasePostprocessorConfig instance

pipeline

pipeline(
    pipeline_backend: str = "local", **kwargs: Any
) -> Dict[str, Any]

Run the complete model pipeline (generate, run, postprocess) using the specified pipeline backend.

This method executes the entire model workflow from input generation through running the model to postprocessing outputs. It uses entry points to load and execute the appropriate pipeline backend from the rompy.pipeline entry point group.

Built-in pipeline backends: - "local": Execute the complete pipeline locally using the existing ModelRun methods

Parameters:

Name Type Description Default
pipeline_backend str

Name of the pipeline backend to use (default: "local")

'local'
**kwargs Any

Additional backend-specific parameters. Common parameters include: - backend_config: BackendConfig instance for the run stage (for local pipeline) - processor: ProcessorConfig instance for postprocessing (for local pipeline) - run_kwargs: Additional parameters for the run stage - process_kwargs: Additional parameters for postprocessing

{}

Returns:

Type Description
Dict[str, Any]

Dictionary with results from the pipeline execution

Raises:

Type Description
ValueError

If the specified pipeline backend is not available

check_for_typos classmethod

check_for_typos(data: Any) -> Any

Suggest the closest field name for an unknown input key.

A field can be given by its name or, like in pydantic, by its alias.

dump_inputs_dict

dump_inputs_dict() -> dict

Return the original inputs as a dictionary.

dump_inputs_json

dump_inputs_json() -> str

Return the original inputs as a JSON string.