From 094ced2ba1d75dd4af10ce1c201e6c92259c5af2 Mon Sep 17 00:00:00 2001 From: Qiusheng Wu Date: Thu, 30 Jan 2025 00:14:56 -0500 Subject: [PATCH] Make richdem optional (#56) * Make richdem optional * Make import optinal * Remove pkg_resources * Skip macOS testing --- .github/workflows/macos.yml | 18 +++++++++--------- .pre-commit-config.yaml | 1 - lidar/example.py | 4 ++-- lidar/filling.py | 8 +++++++- lidar/filtering.py | 9 +++++++-- lidar/gui.py | 12 +++++++++--- lidar/mounts.py | 9 +++++++-- lidar/slicing.py | 8 +++++++- requirements.txt | 2 +- tests/test_lidar.py | 14 +++++++------- 10 files changed, 56 insertions(+), 29 deletions(-) diff --git a/.github/workflows/macos.yml b/.github/workflows/macos.yml index d1d6739..16e763e 100644 --- a/.github/workflows/macos.yml +++ b/.github/workflows/macos.yml @@ -38,15 +38,15 @@ jobs: restore-keys: | ${{ runner.os }}-pip- - - name: Testing conda - run: | - conda info - conda list - - - name: Install GDAL - run: | - conda install -c conda-forge mamba --yes - mamba install -c conda-forge gdal pyproj richdem lidar --yes + # - name: Testing conda + # run: | + # conda info + # conda list + + # - name: Install GDAL + # run: | + # conda install -c conda-forge mamba --yes + # mamba install -c conda-forge gdal pyproj richdem lidar --yes # pip install -U whitebox # - name: Test GDAL installation diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4717bd5..af9a576 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,7 +15,6 @@ repos: rev: 24.10.0 hooks: - id: black-jupyter - language_version: python3.11 # - repo: https://github.com/codespell-project/codespell # rev: v2.2.6 diff --git a/lidar/example.py b/lidar/example.py index 6a3b65b..3282580 100644 --- a/lidar/example.py +++ b/lidar/example.py @@ -1,13 +1,13 @@ import os -import pkg_resources import richdem as rd from filtering import MedianFilter from filling import ExtractSinks from slicing import DelineateDepressions +import importlib.resources as resources # identify the sample data directory of the package package_name = "lidar" -data_dir = pkg_resources.resource_filename(package_name, "data/") +data_dir = resources.files(package_name) / "data" # use the sample dem. Change it to your own dem if needed in_dem = os.path.join(data_dir, "dem.tif") diff --git a/lidar/filling.py b/lidar/filling.py index e2b5871..8e62ced 100644 --- a/lidar/filling.py +++ b/lidar/filling.py @@ -6,13 +6,19 @@ import time import whitebox import numpy as np -import richdem as rd from scipy import ndimage from skimage import measure from osgeo import gdal, ogr, osr from .common import * from .filtering import * +try: + import richdem as rd +except ImportError: + print( + "richdem is not installed. Please install it with `pip install richdem` or `conda install richdem -c conda-forge`." + ) + class Depression: """The class for storing depression info.""" diff --git a/lidar/filtering.py b/lidar/filtering.py index 231e397..b5dc3a9 100644 --- a/lidar/filtering.py +++ b/lidar/filtering.py @@ -3,12 +3,17 @@ """ import os -import pkg_resources -import richdem as rd from scipy import ndimage import numpy as np import time +try: + import richdem as rd +except ImportError: + print( + "richdem is not installed. Please install it with `pip install richdem` or `conda install richdem -c conda-forge`." + ) + def np2rdarray(in_array, no_data, projection, geotransform): """Converts an numpy array to rdarray. diff --git a/lidar/gui.py b/lidar/gui.py index 2a54d65..655b4a1 100644 --- a/lidar/gui.py +++ b/lidar/gui.py @@ -1,11 +1,17 @@ import os -import pkg_resources -import richdem as rd +import importlib.resources as resources from .filtering import MedianFilter, MeanFilter, GaussianFilter from .filling import ExtractSinks from .slicing import DelineateDepressions from .mounts import DelineateMounts +try: + import richdem as rd +except ImportError: + print( + "richdem is not installed. Please install it with `pip install richdem` or `conda install richdem -c conda-forge`." + ) + def gui(): """An interactive Graphical User Interface (GUI) for the lidar package.""" @@ -19,7 +25,7 @@ def gui(): # identify the sample data directory of the package package_name = "lidar" - data_dir = pkg_resources.resource_filename(package_name, "data/") + data_dir = resources.files(package_name) / "data" # use the sample dem. Change it to your own dem if needed in_dem = os.path.join(data_dir, "dem.tif") diff --git a/lidar/mounts.py b/lidar/mounts.py index a6bcc36..7acd78e 100644 --- a/lidar/mounts.py +++ b/lidar/mounts.py @@ -3,13 +3,18 @@ """ import os -import pkg_resources -import richdem as rd import numpy as np import lidar from .filling import ExtractSinks from .slicing import DelineateDepressions +try: + import richdem as rd +except ImportError: + print( + "richdem is not installed. Please install it with `pip install richdem` or `conda install richdem -c conda-forge`." + ) + def get_min_max_nodata(dem): """Gets the minimum, maximum, and no_data value of a numpy array. diff --git a/lidar/slicing.py b/lidar/slicing.py index 0633f2a..a7e7ba1 100644 --- a/lidar/slicing.py +++ b/lidar/slicing.py @@ -7,11 +7,17 @@ import time import shutil import numpy as np -import richdem as rd from scipy import ndimage from skimage import measure from osgeo import gdal, ogr, osr +try: + import richdem as rd +except ImportError: + print( + "richdem is not installed. Please install it with `pip install richdem` or `conda install richdem -c conda-forge`." + ) + class Depression: """The class for storing depression info.""" diff --git a/requirements.txt b/requirements.txt index 899d928..8c98e5c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,7 +5,7 @@ matplotlib numpy pandas pyshp -richdem +# richdem scikit-image scipy whitebox diff --git a/tests/test_lidar.py b/tests/test_lidar.py index 935ab60..9e9a88e 100644 --- a/tests/test_lidar.py +++ b/tests/test_lidar.py @@ -11,7 +11,7 @@ from lidar import cli import os -import pkg_resources +import importlib.resources as resources import richdem as rd from scipy import ndimage import numpy as np @@ -35,7 +35,7 @@ def test_mean_filter(self): # identify the sample data directory of the package package_name = "lidar" - data_dir = pkg_resources.resource_filename(package_name, "data/") + data_dir = resources.files(package_name) / "data" print("Sample data directory: {}".format(data_dir)) # use the sample dem. Change it to your own dem if needed @@ -54,7 +54,7 @@ def test_median_filter(self): # identify the sample data directory of the package package_name = "lidar" - data_dir = pkg_resources.resource_filename(package_name, "data/") + data_dir = resources.files(package_name) / "data" print("Sample data directory: {}".format(data_dir)) # use the sample dem. Change it to your own dem if needed @@ -73,7 +73,7 @@ def test_gaussian_filter(self): # identify the sample data directory of the package package_name = "lidar" - data_dir = pkg_resources.resource_filename(package_name, "data/") + data_dir = resources.files(package_name) / "data" print("Sample data directory: {}".format(data_dir)) # use the sample dem. Change it to your own dem if needed @@ -92,7 +92,7 @@ def test_sink_filling(self): # identify the sample data directory of the package package_name = "lidar" - data_dir = pkg_resources.resource_filename(package_name, "data/") + data_dir = resources.files(package_name) / "data" print("Sample data directory: {}".format(data_dir)) # use the sample dem. Change it to your own dem if needed @@ -115,7 +115,7 @@ def test_slicing(self): # set input files # identify the sample data directory of the package package_name = "lidar" - data_dir = pkg_resources.resource_filename(package_name, "data/") + data_dir = resources.files(package_name) / "data" # in_dem = os.path.join(data_dir, "dem.tif") in_sink = os.path.join(data_dir, "sink.tif") # parameters for level set method @@ -144,7 +144,7 @@ def test_mounts(self): # identify the sample data directory of the package package_name = "lidar" - data_dir = pkg_resources.resource_filename(package_name, "data/") + data_dir = resources.files(package_name) / "data" # use the sample dem. Change it to your own dem if needed in_dem = os.path.join(data_dir, "dsm.tif")