Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add loss value metric based on optimal performance definition #66

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
9 changes: 9 additions & 0 deletions asreviewcontrib/insights/algorithms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
from sklearn import metrics


def _recall_values(labels, x_absolute=False, y_absolute=False):
Expand All @@ -19,6 +20,14 @@ def _recall_values(labels, x_absolute=False, y_absolute=False):
return x.tolist(), y.tolist()


def _loss_value(labels):
positive_doc_ratio = sum(labels) / len(labels)
triangle_before_perfect_recall = positive_doc_ratio * 0.5
aera_under_recall_curve = metrics.auc(*_recall_values(labels))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this not something like y*np.diff(x)?

In that case, we don't need the dependency (which is installed via asreview, but yeah).


return 1 - (triangle_before_perfect_recall + aera_under_recall_curve)


def _wss_values(labels, x_absolute=False, y_absolute=False):
n_docs = len(labels)
n_pos_docs = sum(labels)
Expand Down
19 changes: 19 additions & 0 deletions asreviewcontrib/insights/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from asreviewcontrib.insights.algorithms import _erf_values
from asreviewcontrib.insights.algorithms import _fn_values
from asreviewcontrib.insights.algorithms import _fp_values
from asreviewcontrib.insights.algorithms import _loss_value
from asreviewcontrib.insights.algorithms import _recall_values
from asreviewcontrib.insights.algorithms import _tn_values
from asreviewcontrib.insights.algorithms import _tp_values
Expand Down Expand Up @@ -169,6 +170,24 @@ def _tnr(labels, intercept, x_absolute=False):

return _slice_metric(x, y, intercept)

def loss(state_obj, priors=False):
"""
Computes a loss value that represents how far the recall curve is from
perfect recall.

The function calculates a value based on the area over the recall curve and
under the perfect recall (i.e., an impossible area for recall values).

Returns:
float: The loss value representing the distance from perfect recall.
"""
labels = _pad_simulation_labels(state_obj, priors=priors)

return _loss(labels)

def _loss(labels):
return _loss_value(labels)

Comment on lines +188 to +190
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the added value?


def get_metrics(
state_obj,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ classifiers = [
"Programming Language :: Python :: 3.11"
]
license = {text = "Apache-2.0"}
dependencies = ["numpy", "matplotlib", "asreview>=1,<2"]
dependencies = ["numpy", "matplotlib", "asreview>=1,<2", "scikit-learn"]
dynamic = ["version"]
requires-python = ">=3.7"

Expand Down