Skip to content

Commit

Permalink
overlaps, pixi workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
scottyhq committed Sep 25, 2024
1 parent fb5893e commit 091ade5
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/pixi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Pixi

on:
workflow_dispatch:
pull_request:
push:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: prefix-dev/[email protected]
with:
environments: dev
cache: true
cache-write:
${{ github.event_name == 'push' && github.ref_name == 'main' }}

- name: Run Pytest
env:
MAXAR_API_KEY: ${{ secrets.MAXAR_API_KEY}}
run: |
pixi run postinstall
pixi run networktest
64 changes: 64 additions & 0 deletions src/coincident/overlaps/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""
Functions for refining search results based on spatial and temporal overlaps
"""

from __future__ import annotations

import geopandas as _gpd
import pandas as _pd # noqa: ICN001


def subset_by_maximum_duration(
gf: _gpd.GeoDataFrame,
max_duration: int = 60, # days
) -> _gpd.GeoDataFrame:
"""
Subset a GeoDataFrame by a maximum duration.
Parameters
----------
gf : _gpd.GeoDataFrame
The input GeoDataFrame containing a 'duration' column.
max_duration : int, optional
The maximum duration in days to filter the GeoDataFrame. Default is 60 days.
Returns
-------
_gpd.GeoDataFrame
A GeoDataFrame filtered to include only rows where the 'duration' is less than or equal to the specified maximum duration.
"""
max_duration = _pd.Timedelta(max_duration)
# NOTE: keep indexes?
return gf.loc[gf.duration <= max_duration, :].reset_index()


def subset_by_temporal_overlap(
gf: _gpd.GeoDataFrame,
start_datetime: _pd.Timestamp,
end_datetime: _pd.Timestamp,
temporal_buffer: int = 14,
) -> _gpd.GeoDataFrame:
"""
Subset a DataFrame based on temporal overlap.
Parameters:
gf (DataFrame): The input DataFrame with a datetime column.
start_datetime (Timestamp): The start datetime for the overlap.
end_datetime (Timestamp): The end datetime for the overlap.
temporal_buffer (int): The buffer to apply to the start and end datetimes.
Returns:
DataFrame: A subset of the input DataFrame with rows that overlap temporally.
"""
temporal_buffer = _pd.Timedelta(temporal_buffer)
# Apply temporal buffer to collection start and end dates
buffered_start = gf.start_datetime - temporal_buffer
buffered_end = gf.end_datetime + temporal_buffer

results_intervals = _gpd.pd.IntervalIndex.from_arrays(
buffered_start, buffered_end, closed="both"
)
search_interval = _gpd.pd.Interval(start_datetime, end_datetime, closed="both")
keep = results_intervals.overlaps(search_interval)

return gf.loc[keep, :].reset_index()

0 comments on commit 091ade5

Please sign in to comment.