Skip to content

Commit

Permalink
Merge pull request #58 from magnusuMET/bugfix/endtime
Browse files Browse the repository at this point in the history
Use last element as end time
  • Loading branch information
magnusuMET authored Nov 1, 2024
2 parents 3ecece0 + d14a17f commit 923faeb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/pyaro/timeseries/Filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,16 +579,15 @@ def init_kwargs(self):
}

def _index_from_include_exclude(self, times1, times2, includes, excludes):
idx = times1.astype("bool")
if len(includes) == 0:
idx[:] = True
idx = np.repeat(True, len(times1))
else:
idx[:] = False
idx = np.repeat(False, len(times1))
for start, end in includes:
idx |= (start <= times1) & (times2 <= end)
idx |= (np.datetime64(start) <= times1) & (times2 <= np.datetime64(end))

for start, end in excludes:
idx &= (times1 < start) | (end < times2)
idx &= (times1 < np.datetime64(start)) | (np.datetime64(end) < times2)

return idx

Expand All @@ -614,7 +613,7 @@ def envelope(self) -> tuple[datetime, datetime]:
end = datetime.min
for s, e in self._start_include + self._startend_include + self._end_include:
start = min(start, s)
end = max(end, s)
end = max(end, e)
if end < start:
raise TimeBoundsException(
f"TimeBoundsEnvelope end < start: {end} < {start}"
Expand Down
18 changes: 18 additions & 0 deletions tests/test_timefilter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from datetime import datetime

import numpy as np

from pyaro.timeseries.Filter import TimeBoundsFilter


def test_timemax():
bounds = TimeBoundsFilter(start_include=[("2023-01-01 00:00:00", "2024-01-01 00:00:00")])

envelope = bounds.envelope()
assert envelope[0] == datetime.fromisoformat("2023-01-01 00:00:00")
assert envelope[1] == datetime.fromisoformat("2024-01-01 00:00:00")

dt_start = np.arange(np.datetime64("2023-01-30"), np.datetime64("2023-03-10"), np.timedelta64(1, "D"))
dt_end = dt_start + np.timedelta64(1, "h")
idx = bounds.contains(dt_start, dt_end)
assert len(idx) == len(dt_start)

0 comments on commit 923faeb

Please sign in to comment.