Skip to content

Commit

Permalink
Merge branch 'master' into am/pin-gee
Browse files Browse the repository at this point in the history
  • Loading branch information
atmorling authored Feb 3, 2025
2 parents d86c436 + c7a7a9a commit 1108d62
Show file tree
Hide file tree
Showing 22 changed files with 178 additions and 113 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ jobs:
- name: Create channel
run: mkdir -p /tmp/ecoscope/release/artifacts
- name: Build prefix release
uses: prefix-dev/[email protected].26
uses: prefix-dev/[email protected].29
with:
recipe-path: publish/recipes/release/ecoscope.yaml
build-args: --output-dir /tmp/ecoscope/release/artifacts --channel https://prefix.dev/ecoscope-workflows --channel conda-forge
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- uses: actions/checkout@v4

- name: Setup Micromamba
uses: mamba-org/[email protected].3
uses: mamba-org/[email protected].4
with:
environment-file: environment.yml
cache-environment: true
Expand Down
2 changes: 1 addition & 1 deletion doc/source/notebooks/01. IO/EarthRanger_IO.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"outputs": [],
"source": [
"%pip install \\\n",
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].17' &> /dev/null"
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].19' &> /dev/null"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion doc/source/notebooks/01. IO/GEE_IO.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"outputs": [],
"source": [
"%pip install \\\n",
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].17' &> /dev/null"
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].19' &> /dev/null"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion doc/source/notebooks/01. IO/Landscape Dynamics Data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"outputs": [],
"source": [
"%pip install \\\n",
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].17' &> /dev/null"
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].19' &> /dev/null"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"outputs": [],
"source": [
"%pip install \\\n",
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].17' &> /dev/null"
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].19' &> /dev/null"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"ECOSCOPE_RAW = \"https://raw.githubusercontent.com/wildlife-dynamics/ecoscope/master\"\n",
"\n",
"%pip install \\\n",
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].17' &> /dev/null"
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].19' &> /dev/null"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"ECOSCOPE_RAW = \"https://raw.githubusercontent.com/wildlife-dynamics/ecoscope/master\"\n",
"\n",
"%pip install \\\n",
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].17' &> /dev/null"
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].19' &> /dev/null"
]
},
{
Expand All @@ -49,6 +49,7 @@
"import ecoscope\n",
"from ecoscope.analysis.UD import calculate_etd_range\n",
"from ecoscope.analysis.percentile import get_percentile_area\n",
"from ecoscope.io.raster import RasterData\n",
"\n",
"ecoscope.init()"
]
Expand Down Expand Up @@ -255,7 +256,12 @@
"outputs": [],
"source": [
"percentiles = pd.concat(\n",
" [get_percentile_area(percentile_levels=[50, 99.9, 100.0], raster_path=v, subject_id=k) for k, v in etd.items()]\n",
" [\n",
" get_percentile_area(\n",
" percentile_levels=[50, 99.9, 100.0], raster_data=RasterData.from_raster_file(v), subject_id=k\n",
" )\n",
" for k, v in etd.items()\n",
" ]\n",
").reset_index(drop=True)\n",
"\n",
"percentiles"
Expand Down Expand Up @@ -301,7 +307,7 @@
"source": [
"salif = get_percentile_area(\n",
" percentile_levels=[50, 60, 70, 80, 90, 99.9],\n",
" raster_path=etd.at[\"Salif Keita\"],\n",
" raster_data=RasterData.from_raster_file(etd.at[\"Salif Keita\"]),\n",
" subject_id=\"Salif Keita\",\n",
")"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"ECOSCOPE_RAW = \"https://raw.githubusercontent.com/wildlife-dynamics/ecoscope/master\"\n",
"\n",
"%pip install \\\n",
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].17' &> /dev/null"
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].19' &> /dev/null"
]
},
{
Expand Down
5 changes: 3 additions & 2 deletions doc/source/notebooks/04. EcoMap & EcoPlot/EcoMap.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"outputs": [],
"source": [
"%pip install \\\n",
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].17' &> /dev/null"
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].19' &> /dev/null"
]
},
{
Expand Down Expand Up @@ -496,9 +496,10 @@
"metadata": {},
"outputs": [],
"source": [
"raster_data = ecoscope.io.raster.RasterData.from_raster_file(etd.at[\"Salif Keita\"])\n",
"percentile_areas = get_percentile_area(\n",
" percentile_levels=[50, 60, 70, 80, 90, 99.9],\n",
" raster_path=etd.at[\"Salif Keita\"],\n",
" raster_data=raster_data,\n",
" subject_id=\"Salif Keita\",\n",
").to_crs(4326)\n",
"\n",
Expand Down
2 changes: 1 addition & 1 deletion doc/source/notebooks/04. EcoMap & EcoPlot/EcoPlot.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"outputs": [],
"source": [
"%pip install \\\n",
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].17' &> /dev/null"
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].19' &> /dev/null"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"ECOSCOPE_RAW = \"https://raw.githubusercontent.com/wildlife-dynamics/ecoscope/master\"\n",
"\n",
"%pip install \\\n",
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].17' &> /dev/null"
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].19' &> /dev/null"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"ECOSCOPE_RAW = \"https://raw.githubusercontent.com/wildlife-dynamics/ecoscope/master\"\n",
"\n",
"%pip install \\\n",
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].17' &> /dev/null"
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].19' &> /dev/null"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"ECOSCOPE_RAW = \"https://raw.githubusercontent.com/wildlife-dynamics/ecoscope/master\"\n",
"\n",
"%pip install \\\n",
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].17' &> /dev/null"
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].19' &> /dev/null"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"ECOSCOPE_RAW = \"https://raw.githubusercontent.com/wildlife-dynamics/ecoscope/master\"\n",
"\n",
"%pip install \\\n",
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].17' &> /dev/null"
" 'ecoscope[analysis,mapping,plotting] @ git+https://github.com/wildlife-dynamics/[email protected].19' &> /dev/null"
]
},
{
Expand Down
47 changes: 35 additions & 12 deletions ecoscope/analysis/UD/etd_range.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import logging
import math
import os
import typing
from dataclasses import dataclass

import numpy as np

from ecoscope.base import Trajectory
from ecoscope.io import raster

try:
import numba as nb
import scipy
from sklearn import neighbors
from scipy.optimize import minimize
from scipy.stats import weibull_min
from sklearn import neighbors
except ModuleNotFoundError:
raise ModuleNotFoundError(
'Missing optional dependencies required by this module. \
Please run pip install ecoscope["analysis"]'
)

logger = logging.getLogger(__name__)


@nb.cfunc("double(intc, CPointer(double))")
def __etd__(_, a):
Expand Down Expand Up @@ -84,13 +88,14 @@ class Weibull3Parameter(WeibullPDF):

def calculate_etd_range(
trajectory_gdf: Trajectory,
output_path: typing.Union[str, bytes, os.PathLike],
output_path: typing.Union[str, bytes, os.PathLike, None] = None,
max_speed_kmhr: float = 0.0,
max_speed_percentage: float = 0.9999,
raster_profile: raster.RasterProfile = None,
expansion_factor: float = 1.3,
weibull_pdf: typing.Union[Weibull2Parameter, Weibull3Parameter] = Weibull2Parameter(),
) -> None:
grid_threshold: int = 100,
) -> raster.RasterData:
"""
The ETDRange class provides a trajectory-based, nonparametric approach to estimate the utilization distribution (UD)
of an animal, using model parameters derived directly from the movement behaviour of the species.
Expand All @@ -100,12 +105,13 @@ def calculate_etd_range(
Parameters
----------
trajectory_gdf : geopandas.GeoDataFrame
output_path : str or PathLike
output_path : str or PathLike or None
max_speed_kmhr : float
max_speed_percentage : 0.999
raster_profile : raster.RasterProfile
expansion_factor : float
weibull_pdf : Weibull2Parameter or Weibull3Parameter
grid_threshold: int
Returns
-------
Expand Down Expand Up @@ -169,8 +175,20 @@ def calculate_etd_range(
grid_centroids[1, 0] = y_max - raster_profile.pixel_size * 0.5

centroids_coords = np.dot(grid_centroids, np.mgrid[1:2, :num_columns, :num_rows].T.reshape(-1, 3, 1))
centroids_coords = centroids_coords.squeeze().T

if centroids_coords.size < grid_threshold:
logger.warning(
f"Centroid size {centroids_coords.size} is too small to calculate density. "
f"The threshold value is {grid_threshold}. "
"Check if there’s a data issue or decrease pixel size"
)
return raster.RasterData(data=np.array([]), crs=raster_profile.crs, transform=raster_profile.transform)

if centroids_coords.ndim != 2:
centroids_coords = centroids_coords.reshape(1, -1)

tr = neighbors.KDTree(centroids_coords.squeeze().T)
tr = neighbors.KDTree(centroids_coords)

del centroids_coords

Expand Down Expand Up @@ -206,12 +224,17 @@ def calculate_etd_range(
# Normalize the grid values
raster_ndarray = raster_ndarray / raster_ndarray.sum()

# Set the null data values
raster_ndarray[raster_ndarray == 0] = raster_profile.nodata_value
ndarray = raster_ndarray.reshape(num_rows, num_columns)

# write raster_ndarray to GeoTIFF file.
raster.RasterPy.write(
ndarray=raster_ndarray.reshape(num_rows, num_columns),
fp=output_path,
**raster_profile,
)
if output_path:
# Set the null data values
raster_ndarray[np.isnan(raster_ndarray) | (raster_ndarray == 0)] = raster_profile.nodata_value

raster.RasterPy.write(
ndarray,
fp=output_path,
**raster_profile,
)

return raster.RasterData(data=ndarray.astype("float32"), crs=raster_profile.crs, transform=raster_profile.transform)
Loading

0 comments on commit 1108d62

Please sign in to comment.