Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes colocation setup to use only model kwargs. Adds validator, with test #1184

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyaerocom/colocation_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ def _instantiate_gridded_reader(self, what):
reader_class = self._get_gridded_reader_class(what=what)
if what == "model" and reader_class in self.MODELS_WITH_KWARGS:
reader = reader_class(
data_id=data_id, data_dir=data_dir, **self.colocation_setup.model_read_kwargs
data_id=data_id, data_dir=data_dir, **self.colocation_setup.model_kwargs
)
else:
reader = reader_class(data_id=data_id, data_dir=data_dir)
Expand Down
28 changes: 18 additions & 10 deletions pyaerocom/colocation_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,7 @@ class ColocationSetup(BaseModel):
active, only single year analysis are supported (i.e. provide int to
:attr:`start` to specify the year and leave :attr:`stop` empty).
model_kwargs: dict
Key word arguments to be given to the model reader class's read_var function
model_read_kwargs: dict
Key word arguments to be given to the model reader class's init function
Key word arguments to be given to the model reader class's read_var and init function
gridded_reader_id : dict
BETA: dictionary specifying which gridded reader is supposed to be used
for model (and gridded obs) reading. Note: this is a workaround
Expand Down Expand Up @@ -390,15 +388,11 @@ def validate_basedirs(cls, v):
model_to_stp: bool = False

model_ts_type_read: str | dict | None = None
model_read_aux: dict[
str, dict[Literal["vars_required", "fun"], list[str] | Callable]
] | None = {}
model_read_aux: (
dict[str, dict[Literal["vars_required", "fun"], list[str] | Callable]] | None
) = {}
model_use_climatology: bool = False

model_kwargs: dict = {}
# model_read_kwargs are arguments that are sent to the model reader
model_read_kwargs: dict = {}

gridded_reader_id: dict[str, str] = {"model": "ReadGridded", "obs": "ReadGridded"}

flex_ts_type: bool = True
Expand All @@ -423,6 +417,20 @@ def validate_basedirs(cls, v):
keep_data: bool = True
add_meta: dict | None = {}

model_kwargs: dict = {}

@field_validator("model_kwargs")
@classmethod
def validate_kwargs(cls, v):
forbidden = [
"vert_which",
"ts_type",
] # Forbidden key names which is not found in colocation_setup, or has another name there
dulte marked this conversation as resolved.
Show resolved Hide resolved
for key in v:
if key in dir(cls) or key in forbidden:
raise ValueError(f"Key {key} not allowed in model_kwargs")
return v

# Override __init__ to allow for positional arguments
def __init__(
self,
Expand Down
13 changes: 13 additions & 0 deletions tests/test_colocation_setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pathlib import Path

import pytest
from pydantic import ValidationError

from pyaerocom import const
from pyaerocom.colocation_setup import ColocationSetup
Expand Down Expand Up @@ -65,3 +66,15 @@ def test_ColocationSetup(stp: ColocationSetup, should_be: dict):
assert Path(val) == Path(stp_dict["basedir_coldata"])
else:
assert val == stp_dict[key], key


def test_ColocationSetup_model_kwargs_validationerror() -> None:
stp_dict = default_setup

with pytest.raises(ValidationError):
stp_dict["model_kwargs"] = "not a dict"
stp = ColocationSetup(**stp_dict)

with pytest.raises(ValidationError):
stp_dict["model_kwargs"] = {"emep_vars": {}, "ts_type": "daily"}
stp = ColocationSetup(**stp_dict)