-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
51a7fa3
commit 9baccc8
Showing
4 changed files
with
194 additions
and
66 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import pandas as pd | ||
|
||
|
||
def coerce_schema( | ||
annotations: pd.DataFrame, task_column: str, worker_column: str | ||
) -> pd.DataFrame: | ||
all_columns = annotations.reset_index().columns | ||
required = [task_column, worker_column] | ||
|
||
if missing := set(required) - set(all_columns): | ||
raise ValueError( | ||
f"Annotations should have {task_column} and" | ||
f" {worker_column} as columns or index levels, missing {missing}." | ||
) | ||
|
||
if set(all_columns) == set(required): | ||
raise ValueError("Annotations should have at least one label column") | ||
|
||
annotations = annotations.reset_index().set_index(required)[ | ||
[column for column in annotations.columns if column not in required] | ||
] | ||
|
||
return annotations |
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
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import pandas as pd | ||
import pytest | ||
|
||
|
||
@pytest.mark.ut | ||
@pytest.mark.parametrize( | ||
["annotations", "task_column", "worker_column", "expected_result"], | ||
[ | ||
( | ||
pd.DataFrame( | ||
{ | ||
"task": ["q1", "q1"], | ||
"worker": ["v1", "v2"], | ||
"a": [1, 0], | ||
"b": [0, 1], | ||
} | ||
).set_index(["task", "worker"]), | ||
"task", | ||
"worker", | ||
pd.DataFrame( | ||
{ | ||
"task": ["q1", "q1"], | ||
"worker": ["v1", "v2"], | ||
"a": [1, 0], | ||
"b": [0, 1], | ||
} | ||
).set_index(["task", "worker"]), | ||
), | ||
( | ||
pd.DataFrame( | ||
{ | ||
"question": ["q1", "q1"], | ||
"voter": ["v1", "v2"], | ||
"a": [1, 0], | ||
"b": [0, 1], | ||
} | ||
), | ||
"question", | ||
"voter", | ||
pd.DataFrame( | ||
{ | ||
"question": ["q1", "q1"], | ||
"voter": ["v1", "v2"], | ||
"a": [1, 0], | ||
"b": [0, 1], | ||
} | ||
).set_index(["question", "voter"]), | ||
), | ||
( | ||
pd.DataFrame( | ||
{ | ||
"question": ["q1", "q1"], | ||
"voter": ["v1", "v2"], | ||
"a": [1, 0], | ||
"b": [0, 1], | ||
} | ||
).set_index("question"), | ||
"question", | ||
"voter", | ||
pd.DataFrame( | ||
{ | ||
"question": ["q1", "q1"], | ||
"voter": ["v1", "v2"], | ||
"a": [1, 0], | ||
"b": [0, 1], | ||
} | ||
).set_index(["question", "voter"]), | ||
), | ||
( | ||
pd.DataFrame( | ||
{ | ||
"extra_index_level": ["l1", "l2"], | ||
"question": ["q1", "q1"], | ||
"voter": ["v1", "v2"], | ||
"a": [1, 0], | ||
"b": [0, 1], | ||
} | ||
).set_index(["extra_index_level", "question"]), | ||
"question", | ||
"voter", | ||
pd.DataFrame( | ||
{ | ||
"question": ["q1", "q1"], | ||
"voter": ["v1", "v2"], | ||
"a": [1, 0], | ||
"b": [0, 1], | ||
} | ||
).set_index(["question", "voter"]), | ||
), | ||
], | ||
) | ||
def test_coerce_schema( | ||
annotations: pd.DataFrame, | ||
task_column: str, | ||
worker_column: str, | ||
expected_result: pd.DataFrame, | ||
): | ||
# Given | ||
from hakeem.core.utils.coerce import coerce_schema | ||
|
||
# When | ||
result = coerce_schema(annotations, task_column, worker_column) | ||
|
||
# Then | ||
pd.testing.assert_frame_equal(expected_result, result) |