Skip to content
Snippets Groups Projects
Commit 2ac2b607 authored by Yoann Schneider's avatar Yoann Schneider :tennis:
Browse files

Some small refactoring

parent 25a70324
No related branches found
No related tags found
1 merge request!20Some small refactoring
Pipeline #104003 passed
......@@ -3,48 +3,57 @@ import pytest
from nerval import evaluate
fake_annot_entity_count = {"All": 3, "DAT": 1, "LOC": 1, "PER": 1}
fake_predict_entity_count = {"All": 3, "DAT": 1, "***": 1, "PER": 1}
fake_matches = {"All": 1, "PER": 1, "LOC": 0, "DAT": 0}
expected_scores = {
"***": {
"P": 0.0,
"R": None,
"F1": None,
"predicted": 1,
"matched": 0,
"Support": None,
},
"DAT": {"P": 0.0, "R": 0.0, "F1": 0, "predicted": 1, "matched": 0, "Support": 1},
"All": {
"P": 0.3333333333333333,
"R": 0.3333333333333333,
"F1": 0.3333333333333333,
"predicted": 3,
"matched": 1,
"Support": 3,
},
"PER": {"P": 1.0, "R": 1.0, "F1": 1.0, "predicted": 1, "matched": 1, "Support": 1},
"LOC": {
"P": None,
"R": 0.0,
"F1": None,
"predicted": None,
"matched": 0,
"Support": 1,
},
}
@pytest.mark.parametrize(
"test_input, expected",
"annot,predict,matches",
[
(
(fake_annot_entity_count, fake_predict_entity_count, fake_matches),
expected_scores,
{"All": 3, "DAT": 1, "LOC": 1, "PER": 1},
{"All": 3, "DAT": 1, "***": 1, "PER": 1},
{"All": 1, "PER": 1, "LOC": 0, "DAT": 0},
)
],
)
def test_compute_scores(test_input, expected):
assert evaluate.compute_scores(*test_input) == expected
def test_compute_scores(annot, predict, matches):
assert evaluate.compute_scores(annot, predict, matches) == {
"***": {
"P": 0.0,
"R": None,
"F1": None,
"predicted": 1,
"matched": 0,
"Support": None,
},
"DAT": {
"P": 0.0,
"R": 0.0,
"F1": 0,
"predicted": 1,
"matched": 0,
"Support": 1,
},
"All": {
"P": 0.3333333333333333,
"R": 0.3333333333333333,
"F1": 0.3333333333333333,
"predicted": 3,
"matched": 1,
"Support": 3,
},
"PER": {
"P": 1.0,
"R": 1.0,
"F1": 1.0,
"predicted": 1,
"matched": 1,
"Support": 1,
},
"LOC": {
"P": None,
"R": 0.0,
"F1": None,
"predicted": None,
"matched": 0,
"Support": 1,
},
}
# -*- coding: utf-8 -*-
from pathlib import Path
import pytest
from nerval import evaluate
NO_EXIST_BIO = "no_exist.bio"
EMPTY_BIO = "tests/test_empty.bio"
BAD_BIO = "tests/test_bad.bio"
FAKE_ANNOT_BIO = "tests/test_annot.bio"
FAKE_PREDICT_BIO = "tests/test_predict.bio"
BIOESLU_BIO = "tests/bioeslu.bio"
END_OF_FILE_BIO = "tests/end_of_file.bio"
expected_parsed_annot = {
"entity_count": {"All": 3, "DAT": 1, "LOC": 1, "PER": 1},
"labels": [
......@@ -179,22 +172,22 @@ expected_parsed_end_of_file = {
@pytest.mark.parametrize(
"test_input, expected",
[
(FAKE_ANNOT_BIO, expected_parsed_annot),
(FAKE_PREDICT_BIO, expected_parsed_predict),
(EMPTY_BIO, None),
(BIOESLU_BIO, expected_parsed_annot),
(END_OF_FILE_BIO, expected_parsed_end_of_file),
(pytest.lazy_fixture("fake_annot_bio"), expected_parsed_annot),
(pytest.lazy_fixture("fake_predict_bio"), expected_parsed_predict),
(pytest.lazy_fixture("empty_bio"), None),
(pytest.lazy_fixture("bioeslu_bio"), expected_parsed_annot),
(pytest.lazy_fixture("end_of_file_bio"), expected_parsed_end_of_file),
],
)
def test_parse_bio(test_input, expected):
assert evaluate.parse_bio(test_input) == expected
def test_parse_bio_bad_input():
def test_parse_bio_bad_input(bad_bio):
with pytest.raises(Exception):
evaluate.parse_bio(BAD_BIO)
evaluate.parse_bio(bad_bio)
def test_parse_bio_no_input():
with pytest.raises(AssertionError):
evaluate.parse_bio(NO_EXIST_BIO)
evaluate.parse_bio(Path("not_a_bio"))
......@@ -3,88 +3,111 @@ import pytest
from nerval import evaluate
THRESHOLD = 0.30
FAKE_ANNOT_BIO = "tests/test_annot.bio"
FAKE_PREDICT_BIO = "tests/test_predict.bio"
EMPTY_BIO = "tests/test_empty.bio"
FAKE_BIO_NESTED = "tests/test_nested.bio"
BIO_FOLDER = "test_folder"
CSV_FILE = "test_mapping_file.csv"
expected_scores_nested = {
"All": {
"P": 1.0,
"R": 1.0,
"F1": 1.0,
"predicted": 3,
"matched": 3,
"Support": 3,
},
"PER": {"P": 1.0, "R": 1.0, "F1": 1.0, "predicted": 1, "matched": 1, "Support": 1},
"LOC": {
"P": 1.0,
"R": 1.0,
"F1": 1.0,
"predicted": 2,
"matched": 2,
"Support": 2,
},
}
expected_scores = {
"***": {
"P": 0.0,
"R": None,
"F1": None,
"predicted": 1,
"matched": 0,
"Support": None,
},
"DAT": {"P": 0.0, "R": 0.0, "F1": 0, "predicted": 1, "matched": 0, "Support": 1},
"All": {
"P": 0.3333333333333333,
"R": 0.3333333333333333,
"F1": 0.3333333333333333,
"predicted": 3,
"matched": 1,
"Support": 3,
},
"PER": {"P": 1.0, "R": 1.0, "F1": 1.0, "predicted": 1, "matched": 1, "Support": 1},
"LOC": {
"P": None,
"R": 0.0,
"F1": None,
"predicted": None,
"matched": 0,
"Support": 1,
},
}
@pytest.mark.parametrize(
"test_input, expected",
[
((FAKE_ANNOT_BIO, FAKE_PREDICT_BIO, THRESHOLD, True), expected_scores),
((FAKE_BIO_NESTED, FAKE_BIO_NESTED, THRESHOLD, True), expected_scores_nested),
],
"annotation, prediction, expected",
(
(
pytest.lazy_fixture("fake_annot_bio"),
pytest.lazy_fixture("fake_predict_bio"),
{
"***": {
"P": 0.0,
"R": None,
"F1": None,
"predicted": 1,
"matched": 0,
"Support": None,
},
"DAT": {
"P": 0.0,
"R": 0.0,
"F1": 0,
"predicted": 1,
"matched": 0,
"Support": 1,
},
"All": {
"P": 0.3333333333333333,
"R": 0.3333333333333333,
"F1": 0.3333333333333333,
"predicted": 3,
"matched": 1,
"Support": 3,
},
"PER": {
"P": 1.0,
"R": 1.0,
"F1": 1.0,
"predicted": 1,
"matched": 1,
"Support": 1,
},
"LOC": {
"P": None,
"R": 0.0,
"F1": None,
"predicted": None,
"matched": 0,
"Support": 1,
},
},
),
(
pytest.lazy_fixture("nested_bio"),
pytest.lazy_fixture("nested_bio"),
{
"All": {
"P": 1.0,
"R": 1.0,
"F1": 1.0,
"predicted": 3,
"matched": 3,
"Support": 3,
},
"PER": {
"P": 1.0,
"R": 1.0,
"F1": 1.0,
"predicted": 1,
"matched": 1,
"Support": 1,
},
"LOC": {
"P": 1.0,
"R": 1.0,
"F1": 1.0,
"predicted": 2,
"matched": 2,
"Support": 2,
},
},
),
),
)
def test_run(test_input, expected):
# print(evaluate.run(*test_input))
assert evaluate.run(*test_input) == expected
def test_run(annotation, prediction, expected):
assert (
evaluate.run(
annotation=annotation,
prediction=prediction,
threshold=0.3,
verbose=False,
)
== expected
)
def test_run_empty_bio():
def test_run_empty_bio(empty_bio):
with pytest.raises(Exception):
evaluate.run(EMPTY_BIO, EMPTY_BIO, THRESHOLD)
evaluate.run(empty_bio, empty_bio, 0.3)
def test_run_empty_entry():
with pytest.raises(TypeError):
evaluate.run(None, None, THRESHOLD)
evaluate.run(None, None, 0.3)
def test_run_multiple():
@pytest.mark.parametrize("threshold", ([0.3]))
def test_run_multiple(csv_file, folder_bio, threshold):
with pytest.raises(Exception):
evaluate.run_multiple(CSV_FILE, BIO_FOLDER, THRESHOLD)
evaluate.run_multiple(csv_file, folder_bio, threshold)
[tox]
envlist = nerval
skipsdist=True
[testenv:nerval]
[testenv]
commands =
pytest {posargs}
deps =
pytest
pytest-lazy-fixture
-rrequirements.txt
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment