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

make tslearn an optional dependency #19

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/workflows/run-pytest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
python -m pip install --upgrade pip
- name: Setup aglio
run: |
python -m pip install -e .[dev]
python -m pip install -e .[dev,full]
- name: Test with pytest
run: pytest --cov=./ --cov-report=xml:coverage/coverage.xml

Expand Down
18 changes: 18 additions & 0 deletions aglio/_utilities/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ def has_yt(self):
self._has_yt = False
return self._has_yt

_has_tslearn = None

@property
def has_tslearn(self):
if self._has_tslearn is None:
try:
import tslearn # noqa: F401

self._has_tslearn = True
except ImportError:
self._has_tslearn = False
return self._has_tslearn

def requires(self, module_name, func):
def wrapper(*args, **kwargs):
att_name = f"has_{module_name}"
Expand All @@ -37,3 +50,8 @@ def wrapper(*args, **kwargs):


dependency_checker = DependencyChecker()


class TimeSeriesKMeansDummy:
def __init__(self, *args, **kwargs):
raise ImportError("This functionality requires tslearn, please install.")
6 changes: 5 additions & 1 deletion aglio/seismology/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import numpy as np
from dask import compute, delayed
from geopandas import GeoDataFrame, points_from_xy, sjoin
from tslearn.clustering import TimeSeriesKMeans

from aglio.mapping import BoundingPolies, default_crs
from aglio.point_data import _gpd_df_from_lat_lon

try:
from tslearn.clustering import TimeSeriesKMeans
except ImportError:
from aglio._utilities.dependencies import TimeSeriesKMeansDummy as TimeSeriesKMeans


class ProfileCollection:
def __init__(self, profiles, depth, x, y, crs=default_crs):
Expand Down
10 changes: 8 additions & 2 deletions aglio/tests/test_dependencies.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import pytest

from aglio._utilities.dependencies import dependency_checker
from aglio._utilities.dependencies import TimeSeriesKMeansDummy, dependency_checker


def test_attributes():
assert dependency_checker.has_yt is True
assert dependency_checker.has_yt
assert dependency_checker.has_cartopy is False
assert dependency_checker.has_tslearn


def test_decorator():
Expand All @@ -18,3 +19,8 @@ def temp_func(x, y=2):
missing_wrapped = dependency_checker.requires("not_a_module", temp_func)
with pytest.raises(ImportError, match="This method requires not_a_module"):
_ = missing_wrapped(2)


def test_tslearn_dummy():
with pytest.raises(ImportError, match="This functionality requires tslearn"):
_ = TimeSeriesKMeansDummy()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ dependencies=["netcdf4",
"shapely>=2.0",
"xarray",
"scikit-learn",
"tslearn",
"dask",
]

Expand All @@ -44,6 +43,7 @@ dev = [
]
full = [
"yt>4.1",
"tslearn",
]
extra = [
"yt>4.1",
Expand Down