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

Tests: Timeaverage Unit tests #64

Merged
merged 25 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f5e49df
test: tests for get_time_method
pgierz Nov 13, 2024
31df783
Merge branch 'main' into test/timeaverage
pgierz Nov 13, 2024
33a9abb
wip: more tests for timeaverage module
pgierz Nov 13, 2024
3188c56
wip: more tests for timeaverage module
pgierz Nov 13, 2024
50d99ae
fix: bad empty method for validate
pgierz Nov 13, 2024
2996078
ci: separates out tests
pgierz Nov 13, 2024
57819b8
test: init before process in integration tests
pgierz Nov 13, 2024
fb6cc7a
ci: pytest filepath incorrect
pgierz Nov 13, 2024
280310f
ci: test the test
pgierz Nov 13, 2024
e29ed6f
fix: typo
pgierz Nov 13, 2024
99270f3
fix: argh
pgierz Nov 13, 2024
e48436c
test: more explicit checks for open mfdataset
pgierz Nov 13, 2024
1f37555
test: more verbosity
pgierz Nov 13, 2024
78e3f30
test: tests for timespans
pgierz Nov 13, 2024
a2115e3
test: more wip for timeaverage
pgierz Nov 13, 2024
ddc85fc
fixed _frequency_from_approx_interval
siligam Nov 13, 2024
fbeecbd
raises ValueError if 'time' is missing in DataArray
siligam Nov 14, 2024
b43b8bc
fixed a bug in input argument type in timeaverage._get_time_method. U…
siligam Nov 14, 2024
d67389d
changed TypeError to ValueError
siligam Nov 26, 2024
065c789
Merge branch 'main' into test/timeaverage
pgierz Nov 26, 2024
74d3356
style: fixup from github auto-merge
pgierz Nov 26, 2024
818af21
fix(timeaverage): check if time dimension is empty
pgierz Nov 26, 2024
edf7764
style: isort
pgierz Nov 26, 2024
f03e5be
fix/test(timeaverage): check against ValueError when there are no chu…
pgierz Nov 26, 2024
c000f42
fixed file_timespan
siligam Nov 26, 2024
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
11 changes: 9 additions & 2 deletions .github/workflows/CI-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,16 @@ jobs:
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
- name: Test if data will work (Meta-Test)
run: |
pytest -v --cov
# -s turns on printing to screen, even for passing tests
pytest -vvv --cov -s tests/meta/*.py
- name: Test with pytest (Unit)
run: |
pytest -vvv --cov tests/unit/*.py
- name: Test with pytest (Integration)
run: |
pytest -vvv --cov tests/integration/*.py
- name: Test with doctest
run: |
PYTHONPATH=src pytest -v --doctest-modules src/
1 change: 1 addition & 0 deletions src/pymorize/cmorizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ def _post_init_inherit_rules(self):
rule.set(rule_attr, rule_value)

def validate(self):
"""Performs validation on files if they are suitable for use with the pipeline requirements"""
# Sanity Checks:
# :PS: @PG the following functions are not defined yet
# self._check_rules_for_table()
Expand Down
25 changes: 10 additions & 15 deletions src/pymorize/timeaverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@

import pandas as pd
import xarray as xr

import numpy as np
import functools
from .logging import logger


Expand Down Expand Up @@ -135,23 +136,19 @@ def _frequency_from_approx_interval(interval: str):
("year", lambda x: f"{x}YE", 365),
("month", lambda x: f"{x}ME", 30),
("day", lambda x: f"{x}D", 1),
("hour", lambda x: f"{x}H", 24),
("minute", lambda x: f"{x}min", 24 * 60),
("second", lambda x: f"{x}s", 24 * 60 * 60),
("millisecond", lambda x: f"{x}ms", 24 * 60 * 60 * 1000),
("hour", lambda x: f"{x}H", 1 / 24),
("minute", lambda x: f"{x}min", 1.0 / (24 * 60)),
("second", lambda x: f"{x}s", 1.0 / (24 * 60 * 60)),
("millisecond", lambda x: f"{x}ms", 1.0 / (24 * 60 * 60 * 1000)),
]
try:
interval = float(interval)
except ValueError:
return interval
to_divide = {"decade", "year", "month", "day"}
isclose = functools.partial(np.isclose, rtol=1e-3)
for name, func, val in notation:
if name in to_divide:
value = interval // val
else:
value = interval * val
if value >= 1:
value = round(value)
if (interval >= val) or isclose(interval, val):
value = round(interval / val)
value = "" if value == 1 else value
return func(value)

Expand Down Expand Up @@ -313,7 +310,5 @@ def compute_average(da: xr.DataArray, rule):
time: mean
time: mean grid_longitude: mean
time: point
""".strip().split(
"\n"
)
""".strip().split("\n")
"""list: cell_methods to ignore when calculating time averages"""
13 changes: 8 additions & 5 deletions tests/integration/test_basic_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
from pymorize.cmorizer import CMORizer
from pymorize.logging import logger

def test_init(test_config):
logger.info(f"Processing {test_config}")
with open(test_config, "r") as f:
cfg = yaml.safe_load(f)
CMORizer.from_dict(cfg)
# If we get this far, it was possible to construct
# the object, so this test passes:
assert True

@pytest.mark.skipif(
shutil.which("sbatch") is None, reason="sbatch is not available on this host"
Expand All @@ -22,8 +30,3 @@ def test_process(test_config):
cmorizer.process()


def test_init(test_config):
logger.info(f"Processing {test_config}")
with open(test_config, "r") as f:
cfg = yaml.safe_load(f)
cmorizer = CMORizer.from_dict(cfg)
15 changes: 15 additions & 0 deletions tests/integration/test_fesom_2p6_pimesh_esm_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@
from pymorize.logging import logger


def test_init(fesom_2p6_pimesh_esm_tools_config, fesom_2p6_pimesh_esm_tools_data):
logger.info(f"Processing {fesom_2p6_pimesh_esm_tools_config}")
with open(fesom_2p6_pimesh_esm_tools_config, "r") as f:
cfg = yaml.safe_load(f)
for rule in cfg["rules"]:
for input in rule["inputs"]:
input["path"] = input["path"].replace(
"REPLACE_ME", str(fesom_2p6_pimesh_esm_tools_data)
)
CMORizer.from_dict(cfg)
# If we get this far, it was possible to construct
# the object, so this test passes:
assert True


def test_process(fesom_2p6_pimesh_esm_tools_config, fesom_2p6_pimesh_esm_tools_data):
logger.info(f"Processing {fesom_2p6_pimesh_esm_tools_config}")
with open(fesom_2p6_pimesh_esm_tools_config, "r") as f:
Expand Down
Empty file added tests/meta/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions tests/meta/test_xarray_open_mfdataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import xarray as xr


def test_open_fesom_2p6_pimesh_esm_tools(fesom_2p6_pimesh_esm_tools_data):
matching_files = [
f
for f in (fesom_2p6_pimesh_esm_tools_data / "outdata/fesom/").iterdir()
if f.name.startswith("temp.fesom")
]
assert len(matching_files) > 0
print(matching_files)
ds = xr.open_mfdataset(matching_files)
print(ds)
Loading
Loading