From b871702f56ce976cf100f17cade2cc3271625b22 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Sun, 14 Aug 2022 17:27:00 -0400 Subject: [PATCH] :adhesive_bandage: Raise ModuleNotFoundError when xbatcher not installed (#37) Ensure that a helpful ModuleNotFoundError is raised when attempting to use XbatcherSlicer without xbatcher being installed. * :adhesive_bandage: Raise ModuleNotFoundError when xbatcher not installed Left out on adding this if-statement in #22. So here's the patch! * :pencil2: Fix type hints for xbatcher and datashader source_datapipe Need to use the full name xarray.DataArray instead of xr.DataArray to have the intersphinx link work. --- zen3geo/datapipes/datashader.py | 2 +- zen3geo/datapipes/xbatcher.py | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/zen3geo/datapipes/datashader.py b/zen3geo/datapipes/datashader.py index b28def5..8376178 100644 --- a/zen3geo/datapipes/datashader.py +++ b/zen3geo/datapipes/datashader.py @@ -251,7 +251,7 @@ class XarrayCanvasIterDataPipe(IterDataPipe[Union[xr.DataArray, xr.Dataset]]): Parameters ---------- - source_datapipe : IterDataPipe[xr.DataArray] + source_datapipe : IterDataPipe[xarrray.DataArray] A DataPipe that contains :py:class:`xarray.DataArray` or :py:class:`xarray.Dataset` objects. These data objects need to have both a ``.rio.x_dim`` and ``.rio.y_dim`` attribute, which is present diff --git a/zen3geo/datapipes/xbatcher.py b/zen3geo/datapipes/xbatcher.py index a6d1345..b8fa485 100644 --- a/zen3geo/datapipes/xbatcher.py +++ b/zen3geo/datapipes/xbatcher.py @@ -22,7 +22,7 @@ class XbatcherSlicerIterDataPipe(IterDataPipe[Union[xr.DataArray, xr.Dataset]]): Parameters ---------- - source_datapipe : IterDataPipe[xr.DataArray] + source_datapipe : IterDataPipe[xarray.DataArray] A DataPipe that contains :py:class:`xarray.DataArray` or :py:class:`xarray.Dataset` objects. @@ -85,6 +85,12 @@ def __init__( input_dims: Dict[Hashable, int], **kwargs: Optional[Dict[str, Any]] ) -> None: + if xbatcher is None: + raise ModuleNotFoundError( + "Package `xbatcher` is required to be installed to use this datapipe. " + "Please use `pip install xbatcher` " + "to install the package" + ) self.source_datapipe: IterDataPipe[ Union[xr.DataArray, xr.Dataset] ] = source_datapipe @@ -105,5 +111,5 @@ def __iter__(self) -> Iterator[Union[xr.DataArray, xr.Dataset]]: ): yield chip - # def __len__(self) -> int: - # return len(self.source_datapipe) + # def __len__(self) -> int: + # return len(self.source_datapipe)