Skip to content

Commit

Permalink
upgrade code to pyhon 3.8 and xarray 2022.03 (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathause authored Jan 3, 2024
1 parent e30aa89 commit af83255
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 35 deletions.
9 changes: 2 additions & 7 deletions mesmer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
analyze the results.
"""

from importlib.metadata import version as _get_version

from . import calibrate_mesmer, core, create_emulations, io, stats, testing, utils
from .core import _data as data
from .core import geospatial, grid, mask, volc, weighted
Expand All @@ -32,13 +34,6 @@
"weighted",
]

try:
from importlib.metadata import version as _get_version
except ImportError:
# importlib.metadata not available in python 3.7
import pkg_resources

_get_version = lambda pkg: pkg_resources.get_distribution(pkg).version

try:
__version__ = _get_version("mesmer-emulator")
Expand Down
10 changes: 2 additions & 8 deletions mesmer/stats/_auto_regression.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import numpy as np
import pandas as pd
import xarray as xr
from packaging.version import Version

from mesmer.core.utils import _check_dataarray_form, _check_dataset_form

Expand Down Expand Up @@ -36,23 +35,18 @@ def _select_ar_order_scen_ens(*objs, dim, ens_dim, maxlag, ic="bic"):
then over all scenarios.
"""

if Version(xr.__version__) >= Version("2022.03.0"):
method = "method"
else:
method = "interpolation"

ar_order_scen = list()
for obj in objs:
res = select_ar_order(obj, dim=dim, maxlag=maxlag, ic=ic)

if ens_dim in res.dims:
res = res.quantile(dim=ens_dim, q=0.5, **{method: "nearest"})
res = res.quantile(dim=ens_dim, q=0.5, method="nearest")

ar_order_scen.append(res)

ar_order_scen = xr.concat(ar_order_scen, dim="scen")

ar_order = ar_order_scen.quantile(0.5, dim="scen", **{method: "nearest"})
ar_order = ar_order_scen.quantile(0.5, dim="scen", method="nearest")

if not np.isnan(ar_order).any():
ar_order = ar_order.astype(int)
Expand Down
33 changes: 13 additions & 20 deletions tests/unit/test_smoothing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import pandas as pd
import pytest
import xarray as xr
from packaging.version import Version
from statsmodels.nonparametric.smoothers_lowess import lowess

import mesmer
Expand Down Expand Up @@ -35,15 +34,12 @@ def test_lowess_errors():
with pytest.raises(TypeError, match="Cannot convert coords"):
mesmer.stats.lowess(data.to_dataset(), "time", frac=0.5)

# TODO: remove check once we drop python 3.7
if Version(xr.__version__) >= Version("21.0"):

# cftime datetime
time = xr.date_range("2000-01-01", periods=30, calendar="noleap")
data = data.assign_coords(time=time)
# cftime datetime
time = xr.date_range("2000-01-01", periods=30, calendar="noleap")
data = data.assign_coords(time=time)

with pytest.raises(TypeError, match="Cannot convert coords"):
mesmer.stats.lowess(data.to_dataset(), "time", frac=0.5)
with pytest.raises(TypeError, match="Cannot convert coords"):
mesmer.stats.lowess(data.to_dataset(), "time", frac=0.5)


@pytest.mark.parametrize("it", [0, 3])
Expand Down Expand Up @@ -171,19 +167,16 @@ def test_lowess_2D_combine_dim():

xr.testing.assert_allclose(expected, result)

# TODO: remove check once we drop python 3.7
if Version(xr.__version__) >= Version("21.0"):

# cftime datetime
time = xr.date_range("2000-01-01", periods=30, calendar="noleap")
data = data.assign_coords(time=time)
# cftime datetime
time = xr.date_range("2000-01-01", periods=30, calendar="noleap")
data = data.assign_coords(time=time)

result = mesmer.stats.lowess(
data, "time", combine_dim="cells", frac=0.3, use_coords=False
)
expected = expected.assign_coords(time=time)
result = mesmer.stats.lowess(
data, "time", combine_dim="cells", frac=0.3, use_coords=False
)
expected = expected.assign_coords(time=time)

xr.testing.assert_allclose(expected, result)
xr.testing.assert_allclose(expected, result)


def test_lowess_2D_combine_dim_it():
Expand Down

0 comments on commit af83255

Please sign in to comment.