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

fix logic to extract parameters from file names in the contour subfolder #1510

Merged
merged 3 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
30 changes: 17 additions & 13 deletions pyaerocom/aeroval/experiment_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
TypeValidator,
sort_dict_by_name,
)
from pyaerocom.aeroval import EvalSetup
from pyaerocom.aeroval.collections import ObsCollection
from pyaerocom.aeroval.glob_defaults import (
VariableInfo,
Expand All @@ -28,7 +29,6 @@
)
from pyaerocom.aeroval.json_utils import round_floats
from pyaerocom.aeroval.modelentry import ModelEntry
from pyaerocom.aeroval import EvalSetup
from pyaerocom.aeroval.varinfo_web import VarinfoWeb
from pyaerocom.colocation.colocated_data import ColocatedData
from pyaerocom.exceptions import EntryNotAvailable, VariableDefinitionError
Expand Down Expand Up @@ -339,18 +339,22 @@ def _info_from_contour_dir_file(file: pathlib.PosixPath):
str
Time period
"""
spl = os.path.basename(file.name).split(file.suffix)[0].split("_")

if len(spl) == 3: # png, webp
name = spl[0]
var_name = spl[1]
per = spl[2]
return (name, var_name, per)
elif len(spl) == 2: # geojson
name = spl[1]
var_name = spl[0]
per = None
return (name, var_name, per)
suffix = file.suffix
spl = os.path.basename(file.name).split(suffix)[0].split("_")

if len(spl) == 3:
if suffix == ".png" or suffix == ".webp":
name = spl[0]
var_name = spl[1]
per = spl[2]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not appear to match the template string in aerovaldb, according to which the order should be var_name, name, period. If this gives the right behaviour, and doesn't work with the aerovaldb something weird is going on.

return (name, var_name, per)
elif suffix == ".geojson":
name = spl[1]
var_name = spl[0]
per = spl[2]
return (name, var_name, per)
else:
raise NotImplementedError(f"{suffix} file format not supported")
else:
raise ValueError(f"invalid contour filename: {file}")

Expand Down
25 changes: 18 additions & 7 deletions tests/aeroval/test_experiment_output.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
from __future__ import annotations

import pathlib
from pathlib import Path

import aerovaldb
import pytest

from pyaerocom import const
from pyaerocom.aeroval import ExperimentProcessor
from pyaerocom.aeroval import EvalSetup, ExperimentProcessor
from pyaerocom.aeroval.experiment_output import ExperimentOutput, ProjectOutput
from pyaerocom.aeroval.json_utils import read_json, write_json
from pyaerocom.aeroval import EvalSetup
import pathlib

BASEDIR_DEFAULT = Path(const.OUTPUTDIR) / "aeroval" / "data"

Expand Down Expand Up @@ -174,20 +173,32 @@ def test_ExperimentOutput__info_from_map_file_error(filename: str):
)


def test_ExperimentOutput__info_from_contour_dir_file():
file = pathlib.PosixPath("path/to/name_vertical_period.txt")
def test_ExperimentOutput__info_from_contour_dir_file_webp():
file = pathlib.PosixPath("path/to/name_vertical_period.webp")
output = ExperimentOutput._info_from_contour_dir_file(file)

assert output == ("name", "vertical", "period")


def test_ExperimentOutput__info_from_contour_dir_file_geojson():
file = pathlib.PosixPath("path/to/var_model_period.geojson")
output = ExperimentOutput._info_from_contour_dir_file(file)
assert output == ("model", "var", "period")


def test_ExperimentOutput__info_from_contour_dir_file_error():
file = pathlib.PosixPath("path/to/obs_vertical_model_period.txt")
file = pathlib.PosixPath("path/to/obs_vertical_model_period.geojson")
with pytest.raises(ValueError) as e:
ExperimentOutput._info_from_contour_dir_file(file)
assert "invalid contour filename" in str(e.value)


def test_ExperimentOutput__info_from_contour_dir_file_error_extension():
file = pathlib.PosixPath("path/to/name_vertical_period.txt")
with pytest.raises(NotImplementedError) as e:
ExperimentOutput._info_from_contour_dir_file(file)
assert ".txt file format not supported" in str(e.value)


def test_ExperimentOutput__results_summary_EMPTY(dummy_expout: ExperimentOutput):
assert dummy_expout._results_summary() == dict(obs=[], ovar=[], vc=[], mod=[], mvar=[], per=[])

Expand Down
Loading