Skip to content

Commit

Permalink
make datashader optional dep
Browse files Browse the repository at this point in the history
  • Loading branch information
DirkEilander committed Dec 11, 2024
1 parent 4cb09ce commit 5154f11
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 77 deletions.
111 changes: 56 additions & 55 deletions .github/workflows/tests_dev.yml
Original file line number Diff line number Diff line change
@@ -1,62 +1,63 @@
name: Tests with HydroMT dev
# Uncomment when we move to HydroMT v1
# name: Tests with HydroMT dev

on:
# trigger weekly on monday at 00:00 UTC
schedule:
- cron: '0 0 * * 1'
push:
branches: [main]
paths:
- .github/workflows/tests_dev.yml
- tests/*
- hydromt_sfincs/*
- pyproject.toml
pull_request:
branches: [main]
paths:
- .github/workflows/tests_dev.yml
- tests/*
- hydromt_sfincs/*
- pyproject.toml
# on:
# # trigger weekly on monday at 00:00 UTC
# schedule:
# - cron: '0 0 * * 1'
# push:
# branches: [main]
# paths:
# - .github/workflows/tests_dev.yml
# - tests/*
# - hydromt_sfincs/*
# - pyproject.toml
# pull_request:
# branches: [main]
# paths:
# - .github/workflows/tests_dev.yml
# - tests/*
# - hydromt_sfincs/*
# - pyproject.toml

jobs:
build:
defaults:
run:
shell: bash -l {0}
strategy:
fail-fast: false
runs-on: ubuntu-latest
timeout-minutes: 30
concurrency:
group: ${{ github.workflow }}-${{ matrix.python-version }}-${{ github.ref }}
cancel-in-progress: true
steps:
# jobs:
# build:
# defaults:
# run:
# shell: bash -l {0}
# strategy:
# fail-fast: false
# runs-on: ubuntu-latest
# timeout-minutes: 30
# concurrency:
# group: ${{ github.workflow }}-${{ matrix.python-version }}-${{ github.ref }}
# cancel-in-progress: true
# steps:

- uses: actions/checkout@v3
# - uses: actions/checkout@v3

- uses: actions/setup-python@v5
id: pip
with:
# caching, see https://github.com/actions/setup-python/blob/main/docs/advanced-usage.md#caching-packages
cache: 'pip'
python-version: '3.9' # 3.9 is not supported by last released version of hydromt
cache-dependency-path: pyproject.toml
# - uses: actions/setup-python@v5
# id: pip
# with:
# # caching, see https://github.com/actions/setup-python/blob/main/docs/advanced-usage.md#caching-packages
# cache: 'pip'
# python-version: '3.9' # 3.9 is not supported by last released version of hydromt
# cache-dependency-path: pyproject.toml

# true if cache-hit occurred on the primary key
- name: Cache hit
run: echo '${{ steps.pip.outputs.cache-hit }}'
# # true if cache-hit occurred on the primary key
# - name: Cache hit
# run: echo '${{ steps.pip.outputs.cache-hit }}'

# build environment with pip
- name: Install hydromt-sfincs
run: |
pip install --upgrade pip
pip install .[test,examples]
pip install git+https://github.com/Deltares/hydromt.git
pip list
# # build environment with pip
# - name: Install hydromt-sfincs
# run: |
# pip install --upgrade pip
# pip install .[test,examples]
# pip install git+https://github.com/Deltares/hydromt.git
# pip list

# run test
- name: Test
run: |
export NUMBA_DISABLE_JIT=1
python -m pytest --verbose
# # run test
# - name: Test
# run: |
# export NUMBA_DISABLE_JIT=1
# python -m pytest --verbose
40 changes: 20 additions & 20 deletions hydromt_sfincs/quadtree.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
import logging
import os
import time
import warnings
from pathlib import Path
from typing import List, Union
from typing import Union

import numpy as np
from matplotlib import path
from pyproj import CRS, Transformer

np.warnings = warnings

import geopandas as gpd
import pandas as pd
import shapely

try:
import xugrid as xu
except ImportError:
raise ImportError("xugrid is not installed. Please install it first.")
import xarray as xr
import xugrid as xu
import shapely

# optional dependency
try:
import datashader as ds
from datashader import Canvas
import datashader.transfer_functions as tf
from datashader.utils import export_image

HAS_DATASHADER = True
except ImportError:
raise ImportError("datashader is not installed. Please install it first.")
HAS_DATASHADER = False


from hydromt_sfincs.subgrid import SubgridTableQuadtree

Expand All @@ -39,9 +34,9 @@ def __init__(self, logger=logger):
self.nr_refinement_levels = 1
self.version = 0

self.data = None
self.data = None # placeholder for xugrid object
self.subgrid = SubgridTableQuadtree()
self.df = None
self.df = None # placeholder for pandas dataframe for datashader

@property
def crs(self):
Expand Down Expand Up @@ -133,6 +128,10 @@ def write(self, file_name: Union[str, Path] = "sfincs.nc", version: int = 0):
ds.to_netcdf(file_name)

def map_overlay(self, file_name, xlim=None, ylim=None, color="black", width=800):
# check if datashader is available
if not HAS_DATASHADER:
logger.warning("Datashader is not available. Please install datashader.")
return False
if self.data is None:
# No grid (yet)
return False
Expand All @@ -149,7 +148,7 @@ def map_overlay(self, file_name, xlim=None, ylim=None, color="black", width=800)
ylim = [yl0, yl1]
ratio = (ylim[1] - ylim[0]) / (xlim[1] - xlim[0])
height = int(width * ratio)
cvs = ds.Canvas(
cvs = Canvas(
x_range=xlim, y_range=ylim, plot_height=height, plot_width=width
)
agg = cvs.line(self.df, x=["x1", "x2"], y=["y1", "y2"], axis=1)
Expand All @@ -161,19 +160,20 @@ def map_overlay(self, file_name, xlim=None, ylim=None, color="black", width=800)
name = os.path.splitext(name)[0]
export_image(img, name, export_path=path)
return True
except Exception:
except Exception as e:
logger.warning("Failed to create map overlay. Error: %s" % e)
return False

def snap_to_grid(self, polyline, max_snap_distance=1.0):
if len(polyline) == 0:
return gpd.GeoDataFrame()
geom_list = []
for iline, line in polyline.iterrows():
for _, line in polyline.iterrows():
geom = line["geometry"]
if geom.geom_type == "LineString":
geom_list.append(geom)
gdf = gpd.GeoDataFrame({"geometry": geom_list})
snapped_uds, snapped_gdf = xu.snap_to_grid(
_, snapped_gdf = xu.snap_to_grid(
gdf, self.data.grid, max_snap_distance=max_snap_distance
)
snapped_gdf = snapped_gdf.set_crs(self.crs)
Expand Down
6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ authors = [
]
dependencies = [
"affine",
"datashader",
"geopandas>1.0",
"hydromt>=0.10, <0.11",
"numba",
Expand All @@ -25,7 +24,7 @@ dependencies = [
"scipy",
"shapely",
"xarray",
"xugrid",
"xugrid>=0.12, <1.0",
]
requires-python = ">=3.9" # fix tests to support older versions
readme = "README.rst"
Expand Down Expand Up @@ -61,6 +60,9 @@ examples = [
"matplotlib",
"pillow",
]
quadtree = [
"datashader",
]

full = ["hydromt_sfincs[test, doc, examples]"]

Expand Down

0 comments on commit 5154f11

Please sign in to comment.