seapopym.core package

Submodules

seapopym.core.kernel module

Define the Kernels used in the model.

class seapopym.core.kernel.Kernel(kernel_unit: Iterable[type[KernelUnit]], chunk: dict[str, int])

Bases: object

The Kernel class is used to define a kernel that can be applied to the model state. It contains a list of KernelUnit that will be applied in order.

run(state: SeapopymState) SeapopymState

Run all kernel_unit in the kernel in order.

template(state: SeapopymState) SeapopymState

Generate an empty Dataset that represent the state of the model at the end of execution. Usefull for size estimation.

class seapopym.core.kernel.KernelUnit(name: str, template: Template, function: Callable[[SeapopymState], xr.Dataset], to_remove_from_state: list[str] | None = None)

Bases: object

The KernelUnit class is used to define a kernel function that can be applied to the model state. It contains the function, its template, and the arguments to be passed to the function.

function: Callable[[SeapopymState], xr.Dataset]
name: str
run(state: SeapopymState) SeapopymState | SeapopymForcing

Execute the kernel function on the model state and return the results as Dataset.

template: Template
to_remove_from_state: list[str] | None = None
seapopym.core.kernel.kernel_factory(class_name: str, kernel_unit: list[type[KernelUnit]]) Kernel

Create a custom kernel class with the specified name and functions.

Parameters

class_namestr

The name to assign to the custom kernel class.

kernel_unitlist of str

A list of KernelUnit names to be used in the kernel. These KernelUnits must be registered in the kernel_unit_registry. Be aware that the order of the list matters, as the kernel units will be applied in the order they are listed.

Returns

Kernel

A dynamically created kernel class with the specified name and kernel units.

Notes

The returned class inherits from Kernel and is initialized with the provided functions and a chunk dictionary.

seapopym.core.kernel.kernel_unit_factory(name: str, function: Callable[[SeapopymState], xr.Dataset], template: Iterable[type[TemplateUnit]], to_remove_from_state: list[str] | None = None) type[KernelUnit]

Create a custom kernel unit class with the specified name and function.

Parameters

namestr

The name to assign to the custom kernel unit class.

functionCallable

The function to be used in the kernel unit. It should accept a SeapopymState and return a Dataset.

templatelist of TemplateUnit

A list of TemplateUnit classes to be used in the kernel unit. These TemplateUnits must be registered in the template_unit_registry. Be aware that the order of the list matters, as the template units will be applied in the order they are listed.

to_remove_from_statelist of str, optional

A list of variable names to be removed from the state after the kernel unit has been executed. If not provided, no variables will be removed.

Returns

KernelUnit

A dynamically created kernel unit class with the specified name and function.

Notes

The returned class inherits from KernelUnit and is initialized with the provided function and a chunk dictionary.

seapopym.core.template module

Functions used to generalize the usage of map_blocks function in the model.

Notes

This module is used to generate a template for a new variable that can be used in a xarray.map_blocks function. The template is based on the state of the model and the dimensions of the new variable. The template can be chunked if needed.

### xarray documentation:

> If none of the variables in obj is backed by dask arrays, calling this function is equivalent to calling > func(obj, *args, **kwargs).

class seapopym.core.template.BaseTemplate

Bases: ABC

Method generated by attrs for class BaseTemplate.

abstractmethod generate(state: SeapopymState) SeapopymForcing | SeapopymState

Generate an empty xr.DataArray/Dataset.

class seapopym.core.template.Template(*, template_unit: Iterable[TemplateUnit])

Bases: BaseTemplate

Method generated by attrs for class Template.

generate(state: SeapopymState) SeapopymState

Generate an empty xr.DataArray/Dataset.

template_unit: Iterable[TemplateUnit]
class seapopym.core.template.TemplateUnit(*, name: ForcingName, attrs: ForcingAttrs, dims: Iterable[SeapopymDims | SeapopymForcing], chunks: dict[str, int] | None = None, dtype: type | None = None)

Bases: BaseTemplate

Method generated by attrs for class TemplateUnit.

attrs: ForcingAttrs
chunks: dict[str, int] | None
dims: Iterable[SeapopymDims | SeapopymForcing]
dtype: type | None
generate(state: SeapopymState) SeapopymForcing

Generate an empty xr.DataArray/Dataset.

name: ForcingName
seapopym.core.template.template_unit_factory(name: ForcingName, attributs: ForcingAttrs, dims: Iterable[SeapopymDims | SeapopymForcing], dtype: type | None = None) type[BaseTemplate]

Module contents

This module provides the core functionality for the seapopym package.

  • Kernel: Manages functions used throughout simulations.

  • Template: Defines the structure for parallel and distributed computations.

For defining new model behaviors, refer to the seapopym.model and seapopym.function modules.