This repository has been archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor runner * Add fleiss kappa * Refactor None handling
- Loading branch information
Showing
4 changed files
with
83 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,41 @@ | ||
from typing import List, Any | ||
from typing import List, Any, cast | ||
|
||
from sklearn.metrics import confusion_matrix | ||
from sklearn.preprocessing import LabelEncoder | ||
from statsmodels.stats.inter_rater import cohens_kappa | ||
from statsmodels.stats.inter_rater import cohens_kappa, fleiss_kappa, aggregate_raters | ||
|
||
|
||
def kappa(*predictions: List[Any]) -> float: | ||
def kappa(predictions: List[List[Any]]) -> float: | ||
if len(predictions) < 2: | ||
raise ValueError | ||
if len(predictions) > 2: | ||
raise NotImplementedError | ||
|
||
# TODO: We only support cohens_kappa for now | ||
assert len(predictions) == 2 | ||
|
||
if len(predictions[0]) == 0 or len(predictions[1]) == 0: | ||
if any(len(ps) == 0 for ps in predictions): | ||
raise ValueError | ||
|
||
# TODO: hacky none-handling | ||
if isinstance(predictions[0][0], str): | ||
# TODO: workaround for None | ||
a = ["" if p is None else p for p in predictions[0]] | ||
b = ["" if p is None else p for p in predictions[1]] | ||
for ps in predictions: | ||
ps = ["" if p is None else p for p in ps] | ||
|
||
le = LabelEncoder() | ||
le.fit(list(set(a + b))) | ||
le.fit(list(set([p for ps in predictions for p in ps]))) | ||
|
||
a, b = le.transform(a), le.transform(b) | ||
predictions = [le.transform(p) for p in predictions] | ||
else: | ||
# TODO: workaround for None | ||
a = [-1 if p is None else p for p in predictions[0]] | ||
b = [-1 if p is None else p for p in predictions[1]] | ||
predictions = [[-1 if p is None else p for p in ps] for ps in predictions] | ||
|
||
if len(predictions) == 2: | ||
return _cohens_kappa(predictions[0], predictions[1]) | ||
return _fleiss_kappa(predictions) | ||
|
||
|
||
def _cohens_kappa(a: List[Any], b: List[Any]) -> float: | ||
return cohens_kappa( | ||
table=confusion_matrix(a, b), | ||
return_results=False, | ||
) | ||
|
||
|
||
return cohens_kappa(table=confusion_matrix(a, b), return_results=False) | ||
def _fleiss_kappa(predictions: List[List[Any]]) -> float: | ||
input = list(zip(*predictions)) # transpose | ||
table, _ = aggregate_raters(input) | ||
return fleiss_kappa(table) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters