Skip to content

Commit

Permalink
Merge pull request blaylockbk#319 from rafa-guedes/optional_dependencies
Browse files Browse the repository at this point in the history
Optional dependencies
  • Loading branch information
blaylockbk authored Jun 2, 2024
2 parents f97375b + 4117fae commit 42f2a5b
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 26 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/tests-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,19 @@ jobs:
if: runner.os == 'Linux'
run: |
pip3 install --requirement=requirements-test.txt
pip3 install --editable=.
pip3 install --editable='.[extras]'
- name: Install project (macOS)
if: runner.os == 'macOS'
run: |
pip3 install --break-system-packages --requirement=requirements-test.txt
pip3 install --break-system-packages --editable=.
pip3 install --break-system-packages --editable='.[extras]'
- name: Install project (Windows)
if: runner.os == 'Windows'
run: |
pip3 install --requirement=requirements-test.txt
pip3 install --editable=.
pip3 install --editable='.[extras]'
- name: Run tests
env:
Expand Down
11 changes: 9 additions & 2 deletions docs/user_guide/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Herbie is published on PyPI and you can install it with pip, _but_ it requires s

- Python 3.9+
- cURL
- [cfgrib](https://github.com/ecmwf/cfgrib), which requires **_eccodes_** (most easily installed with conda)
- _Optional:_ wgrib2
- _Optional:_ [Carpenter Workshop](https://github.com/blaylockbk/Carpenter_Workshop)

Expand All @@ -29,7 +28,15 @@ When those are installed within your environment, _then_ you can install Herbie
pip install herbie-data
```

or
To install the full functionality in the library which includes
[xarray accessors](https://github.com/blaylockbk/Herbie/blob/main/herbie/accessors.py) for plotting and data manipulation please install the "extras" dependencies:

```bash
# Install last published version
pip install 'herbie-data[extras]'
```

The code can also be installed directly from github.

```bash
# Install current main branch
Expand Down
52 changes: 46 additions & 6 deletions herbie/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,15 @@
import warnings
from pathlib import Path

import cartopy.crs as ccrs
import metpy # * Needed for metpy accessor # noqa: F401
import numpy as np
import pandas as pd
import pygrib
import shapely
import xarray as xr
from pyproj import CRS
from shapely.geometry import MultiPoint, Point, Polygon
from sklearn.neighbors import BallTree

import herbie


_level_units = dict(
adiabaticCondensation="adiabatic condensation",
atmosphere="atmosphere",
Expand Down Expand Up @@ -124,6 +120,14 @@ def crs(self):
An xarray.Dataset from a GRIB2 file opened by the cfgrib engine.
"""
try:
import metpy
except ModuleNotFoundError:
raise ModuleNotFoundError(
"metpy is an 'extra' requirement, please use "
"`pip install 'herbie-data[extras]'` for the full functionality."
)

ds = self._obj

# Get variables that have dimensions
Expand All @@ -137,6 +141,15 @@ def crs(self):
@functools.cached_property
def polygon(self):
"""Get a polygon of the domain boundary."""
try:
import cartopy.crs as ccrs
from shapely.geometry import Polygon
except ModuleNotFoundError:
raise ModuleNotFoundError(
"cartopy is an 'extra' requirements, please use "
"`pip install 'herbie-data[extras]'` for the full functionality."
)

ds = self._obj

LON = ds.longitude.data
Expand Down Expand Up @@ -243,6 +256,13 @@ def pick_points(
dimension.
>>> dsp = dsp.swap_dims({"point": "point_stid"})
"""
try:
from sklearn.neighbors import BallTree
except ModuleNotFoundError:
raise ModuleNotFoundError(
"scikit-learn is an 'extra' requirement, please use "
"`pip install 'herbie-data[extras]'` for the full functionality."
)

def plant_tree(save_pickle=None):
"""Grow a new BallTree object from seedling."""
Expand Down Expand Up @@ -476,6 +496,16 @@ def nearest_points(self, points, names=None, verbose=True):
- Or possibly scipy BallTree method.
"""
try:
import cartopy.crs as ccrs
import shapely
from shapely.geometry import MultiPoint, Point
except ModuleNotFoundError:
raise ModuleNotFoundError(
"cartopy is an 'extra' requirements, please use "
"`pip install 'herbie-data[extras]'` for the full functionality."
)

warnings.warn(
"The accessor `ds.herbie.nearest_points` is deprecated in "
"favor of the `ds.herbie.pick_points` which uses the "
Expand Down Expand Up @@ -566,7 +596,17 @@ def plot(self, ax=None, common_features_kw={}, vars=None, **kwargs):
"""
# From Carpenter_Workshop:
# https://github.com/blaylockbk/Carpenter_Workshop
import matplotlib.pyplot as plt

try:
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import shapely
from shapely.geometry import MultiPoint, Point, Polygon
except ModuleNotFoundError:
raise ModuleNotFoundError(
"cartopy is an 'extra' requirements, please use "
"`pip install 'herbie-data[extras]'` for the full functionality."
)

try:
from paint.radar import cm_reflectivity
Expand Down
10 changes: 8 additions & 2 deletions herbie/hrrr_zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
"""

import datetime

import cartopy.crs as ccrs
import pandas as pd
import s3fs
import xarray as xr
Expand All @@ -26,6 +24,14 @@ def load_dataset(urls):
and latitude/longitude. We also promote the "time" attribute to a coordinate
so that combining the datasets for each hour will work later on.
"""
try:
import cartopy.crs as ccrs
except ModuleNotFoundError:
raise ModuleNotFoundError(
"cartopy is an 'extra' requirement, please use "
"`pip install 'herbie-data[extras]'` for the full functionality."
)

projection = ccrs.LambertConformal(
central_longitude=262.5,
central_latitude=38.5,
Expand Down
23 changes: 16 additions & 7 deletions herbie/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def print_rich(H):
f"[rgb(41, 130, 13)]F{H.fxx:02d}[/] "
f"┊ [#ff9900 italic]source={H.grib_source}[/]"
)
except:
except (ImportError, ModuleNotFoundError):
print("rich is not working/installed")


Expand All @@ -119,9 +119,13 @@ def print_rich(H):

def HerbieLogo(white_line=False):
"""Logo of Herbie The Love Bug"""
import matplotlib.patheffects as path_effects
import matplotlib.pyplot as plt

try:
import matplotlib.pyplot as plt
except ModuleNotFoundError:
raise ModuleNotFoundError(
"matplotlib is an 'extra' requirement, please use "
"`pip install 'herbie-data[extras]'` for the full functionality."
)
plt.figure(figsize=[5, 5], facecolor=hc.tan)

plt.axis([-10, 10, -10, 10])
Expand Down Expand Up @@ -171,9 +175,14 @@ def HerbieLogo2(white_line=False, text_color="tan", text_stroke="black"):
>>> ax = HerbieLogo2(text_color='tan')
>>> plt.savefig('Herbie_transparent_tan.svg', bbox_inches="tight", transparent=True)
"""
import matplotlib.patheffects as path_effects
import matplotlib.pyplot as plt

try:
import matplotlib.pyplot as plt
import matplotlib.patheffects as path_effects
except ModuleNotFoundError:
raise ModuleNotFoundError(
"matplotlib is an 'extra' requirement, please use "
"`pip install 'herbie-data[extras]'` for the full functionality."
)
plt.figure(figsize=[5, 3], facecolor=hc.tan)

plt.axis([1.5, 20, -10, 10])
Expand Down
6 changes: 2 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@ keywords = [
"HRRR",
]
dependencies = [
"cartopy",
"cfgrib",
"matplotlib",
"metpy",
"numpy",
"pandas",
"pygrib",
"scikit-learn",
"requests",
"toml",
"xarray",
]
Expand All @@ -51,6 +48,7 @@ dynamic = ["version"]
"Bug Tracker" = "https://github.com/blaylockbk/Herbie/issues"

[project.optional-dependencies]
extras = ["cartopy", "metpy", "scikit-learn"]
docs = [
"autodocsumm",
"ipython",
Expand Down
2 changes: 0 additions & 2 deletions tests/test_accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
"""

from herbie import Herbie
from shapely.geometry import MultiPoint
import pandas as pd


def test_crs():
Expand Down

0 comments on commit 42f2a5b

Please sign in to comment.