Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions orion/evaluation/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ def _point_partition(expected, observed, start=None, end=None):
expected = set(expected)
observed = set(observed)

edge_start = min(expected.union(observed))
if start is not None:
edge_start = start
edge_start = start
if edge_start is None:
edge_start = min(expected.union(observed))

edge_end = max(expected.union(observed))
if end is not None:
edge_end = end
edge_end = end
if edge_end is None:
edge_end = max(expected.union(observed))

length = int(edge_end) - int(edge_start) + 1

Expand Down Expand Up @@ -62,6 +62,13 @@ def _ws(x, y, z, w):
if not isinstance(observed, list):
observed = list(observed['timestamp'])

if not expected and not observed and (start is None or end is None):
# Without any anomalies there is no range to partition, and none was
# supplied through ``data``/``start``/``end``. There are no true or
# false positives and no false negatives, but the number of true
# negatives is unknown.
return None, 0, 0, 0

return _ws(expected, observed, start, end)


Expand Down
33 changes: 33 additions & 0 deletions tests/unit/evaluation/test_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,36 @@ def test_point_f1_score_nan():
observed = pd.DataFrame({"timestamp": [4, 5]})
returned = point_f1_score(expected, observed)
assert np.isnan(returned)


def test_point_confusion_matrix_empty():
empty = pd.DataFrame({"timestamp": []})

returned = point_confusion_matrix(empty, empty)

# There is no range to partition, so the number of true negatives is
# unknown, but there are no positives and no false negatives.
assert returned == (None, 0, 0, 0)


def test_point_confusion_matrix_empty_with_range():
empty = pd.DataFrame({"timestamp": []})

returned = point_confusion_matrix(empty, empty, start=0, end=9)

# Every point in the range is correctly considered normal.
np.testing.assert_array_equal(np.array(returned), np.array((10, 0, 0, 0)))


def test_point_scores_empty():
empty = pd.DataFrame({"timestamp": []})

assert np.isnan(point_precision(empty, empty))
assert np.isnan(point_recall(empty, empty))
assert np.isnan(point_f1_score(empty, empty))

# Accuracy needs the true negatives, which are unknown without a range.
with pytest.raises(ValueError):
point_accuracy(empty, empty)

assert point_accuracy(empty, empty, start=0, end=9) == 1.0