Skip to content

Commit

Permalink
Events when the values are outside of the expected range
Browse files Browse the repository at this point in the history
  • Loading branch information
micah-prime committed Oct 19, 2023
1 parent d993918 commit c7bb529
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 2 deletions.
28 changes: 28 additions & 0 deletions metevents/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,31 @@ def find(self, min_len=5, slope_thresh=0.0):
# only keep events that are longer than what is configured
if len(event.data) >= min_len:
self._events.append(event)


class ExtremeValueEvent(BaseEvents):

def find(self, expected_max=600.0, expected_min=0.0):
"""
Find events where the values are outside of the expected range
Args:
expected_max: Maximum expected value in the data
expected_min: minimum expected value in the data
"""
# Indices where data is outside of expected range
ind = (self.data > expected_max) | (self.data < expected_min)

# Group the nan data events
groups, _ = self.group_condition_by_time(ind)
# sort the group list
group_list = sorted(list(groups.items()))

# Build the list of events
for event_id, curr_group in group_list:
curr_start = curr_group.min()
curr_stop = curr_group.max()
event = BaseTimePeriod(self.data.loc[curr_start:curr_stop])
# store the events
self._events.append(event)
65 changes: 63 additions & 2 deletions tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from pandas import DatetimeIndex

from metevents.events import (
StormEvents, SpikeValleyEvent, DataGapEvent, FlatLineEvent
StormEvents, SpikeValleyEvent, DataGapEvent, FlatLineEvent,
ExtremeValueEvent
)


Expand Down Expand Up @@ -229,7 +230,7 @@ def flat_series(self):
data = np.array(range(100)).astype("float32")
index = [datetime(2023, 1, 1) + timedelta(days=i) for i in
range(len(data))]
# Set nans that we will drop
# Set flatlines
data[10:18] = 10.0
data[40:48] = 40.0
# not long enough to flag
Expand Down Expand Up @@ -278,3 +279,63 @@ def test_stop_dates(self, found_events, idx, stop_date):
def test_start_duration(self, found_events, idx, duration):
event = found_events.events[idx]
assert event.duration == pd.to_timedelta(duration)


class TestExtremeValueEvent:

@pytest.fixture(scope="class")
def series(self):
data = np.array(range(100)).astype("float32")
index = [datetime(2023, 1, 1) + timedelta(days=i) for i in
range(len(data))]
# Set extreme values
data[10:15] = 700.0
data[40:48] = -1.0
data[50:54] = 601.0
series = pd.Series(data, index=DatetimeIndex(index, freq='D'))
return series

@pytest.fixture(scope="class")
def events(self, series):
yield ExtremeValueEvent(series)

@pytest.fixture(scope="class")
def found_events(self, events):
events.find(expected_max=600.0, expected_min=0.0)
yield events

def test_number_of_events(self, found_events):
assert found_events.N == 3

@pytest.mark.parametrize(
"idx, start_date", [
(0, "2023-01-11"),
(1, "2023-02-10"),
(2, "2023-02-20"),
]
)
def test_start_dates(self, found_events, idx, start_date):
event = found_events.events[idx]
assert event.start == pd.to_datetime(start_date)

@pytest.mark.parametrize(
"idx, stop_date", [
(0, "2023-01-15"),
(1, "2023-02-17"),
(2, "2023-02-23"),
]
)
def test_stop_dates(self, found_events, idx, stop_date):
event = found_events.events[idx]
assert event.stop == pd.to_datetime(stop_date)

@pytest.mark.parametrize(
"idx, duration", [
(0, "4 days"),
(1, "7 days"),
(2, "3 days"),
]
)
def test_start_duration(self, found_events, idx, duration):
event = found_events.events[idx]
assert event.duration == pd.to_timedelta(duration)

0 comments on commit c7bb529

Please sign in to comment.