Skip to content
Snippets Groups Projects
Commit 525c1a9e authored by Manon Blanco's avatar Manon Blanco
Browse files

Comply with ruff's PT rule

parent 674d7e14
No related branches found
No related tags found
1 merge request!33Comply with ruff's PT rule
Pipeline #150431 passed
...@@ -374,7 +374,7 @@ def run_multiple(file_csv: Path, folder: Path, threshold: int, verbose: bool): ...@@ -374,7 +374,7 @@ def run_multiple(file_csv: Path, folder: Path, threshold: int, verbose: bool):
recall += scores["All"]["R"] recall += scores["All"]["R"]
f1 += scores["All"]["F1"] f1 += scores["All"]["F1"]
else: else:
raise Exception(f"No file found for files {annot}, {predict}") raise Exception(f"No file found for files {row[0]}, {row[1]}")
if count: if count:
logger.info("Average score on all corpus") logger.info("Average score on all corpus")
table = PrettyTable() table = PrettyTable()
......
...@@ -35,4 +35,6 @@ select = [ ...@@ -35,4 +35,6 @@ select = [
"ARG", "ARG",
# flake8-use-pathlib # flake8-use-pathlib
"PTH", "PTH",
# flake8-pytest-style
"PT",
] ]
...@@ -5,46 +5,46 @@ import pytest ...@@ -5,46 +5,46 @@ import pytest
FIXTURES = Path(__file__).parent / "fixtures" FIXTURES = Path(__file__).parent / "fixtures"
@pytest.fixture @pytest.fixture()
def fake_annot_bio(): def fake_annot_bio():
return FIXTURES / "test_annot.bio" return FIXTURES / "test_annot.bio"
@pytest.fixture @pytest.fixture()
def fake_predict_bio(): def fake_predict_bio():
return FIXTURES / "test_predict.bio" return FIXTURES / "test_predict.bio"
@pytest.fixture @pytest.fixture()
def empty_bio(): def empty_bio():
return FIXTURES / "test_empty.bio" return FIXTURES / "test_empty.bio"
@pytest.fixture @pytest.fixture()
def bad_bio(): def bad_bio():
return FIXTURES / "test_bad.bio" return FIXTURES / "test_bad.bio"
@pytest.fixture @pytest.fixture()
def bioeslu_bio(): def bioeslu_bio():
return FIXTURES / "bioeslu.bio" return FIXTURES / "bioeslu.bio"
@pytest.fixture @pytest.fixture()
def end_of_file_bio(): def end_of_file_bio():
return FIXTURES / "end_of_file.bio" return FIXTURES / "end_of_file.bio"
@pytest.fixture @pytest.fixture()
def nested_bio(): def nested_bio():
return FIXTURES / "test_nested.bio" return FIXTURES / "test_nested.bio"
@pytest.fixture @pytest.fixture()
def folder_bio(): def folder_bio():
return Path("test_folder") return FIXTURES
@pytest.fixture() @pytest.fixture()
def csv_file(): def csv_file():
return Path("test_mapping_file.csv") return FIXTURES / "test_mapping_file.csv"
File moved
...@@ -3,7 +3,7 @@ import pytest ...@@ -3,7 +3,7 @@ import pytest
@pytest.mark.parametrize( @pytest.mark.parametrize(
"query,target", ("query", "target"),
[ [
( (
"Gérard de Nerval was born in Paris in 1808 .", "Gérard de Nerval was born in Paris in 1808 .",
......
...@@ -202,7 +202,7 @@ fake_predict_tags_bk_boundary_2 = [ ...@@ -202,7 +202,7 @@ fake_predict_tags_bk_boundary_2 = [
@pytest.mark.parametrize( @pytest.mark.parametrize(
"test_input, expected", ("test_input", "expected"),
[ [
( (
( (
......
...@@ -4,7 +4,7 @@ from nerval import evaluate ...@@ -4,7 +4,7 @@ from nerval import evaluate
@pytest.mark.parametrize( @pytest.mark.parametrize(
"annot,predict,matches", ("annot", "predict", "matches"),
[ [
( (
{"All": 3, "DAT": 1, "LOC": 1, "PER": 1}, {"All": 3, "DAT": 1, "LOC": 1, "PER": 1},
......
Césaire B-PER
Alphonse I-PER
Garon I-PER
marraine O
Adeline B-PER
Dionne I-PER
, O
soussignés O
Lecture O
faite O
Adéline O
Dionne O
Arsène O
Côté O
Arpin O
R O
Le O
onze B-DAT
aout I-DAT
mil I-DAT
neuf I-DAT
cent I-DAT
un I-DAT
nous O
prêtre O
soussigné O
avons O
baptisé O
Marie B-PER
Luce I-PER
Louise I-PER
, O
née O
la B-DAT
veille I-DAT
, O
fille O
légitime O
de O
Carmel B-PER
Côté I-PER
, O
cordonnier B-OCC
, O
pré O
- O
sent O
, O
déclarant O
ne O
savoir O
signer O
, O
et O
de O
Eugé B-PER
nie I-PER
Fréchette I-PER
, O
de O
cette B-LOC
paroisse I-LOC
. O
Parrain O
Napoléon B-PER
Fréchette I-PER
, O
marraine O
Adeline B-PER
Tremblay I-PER
, O
soussignés O
, O
de O
Ste B-LOC
Luce I-LOC
, O
Lec O
- O
ture O
faite O
. O
Césaire B-PER
Alphonse O
Garon B-PER
marraine O
Adeline B-PER
Dionne I-PER
, O
soussignés O
Lecture O
faite O
Adéline O
Dionne O
Arsène O
Côté O
Arpin O
R O
Le O
onze B-DAT
aout I-DAT
mil I-DAT
neuf I-DAT
cent I-DAT
un O
nous O
pretre O
soussigné O
avons O
baptisé O
Marie B-PER
Luce I-PER
Louise I-PER
, O
née O
la B-DAT
veille I-DAT
, O
fille O
légitime O
de O
Carmel B-PER
Côté I-PER
, O
cordonnier B-OCC
, O
pré O
- O
sent O
, O
déclarant O
ne O
savoir O
signer O
, O
et O
de O
Eugé B-PER
nie I-PER
Fréchette I-PER
, O
de O
cette B-LOC
paroisse I-LOC
. O
Parrain O
Napoléon B-PER
Fréchette I-PER
, O
marraine O
Adéline B-PER
Tremblay I-PER
, O
sousignés O
, O
de O
St B-LOC
. I-LOC
Luce O
, O
Lec O
ture O
faite O
John B-PER
Ronald I-PER
Reuel I-PER
Tolkien I-PER
was O
born O
on O
three B-DAT
January I-DAT
eighteen I-DAT
ninety I-DAT
- I-DAT
two I-DAT
in O
Bloemfontein B-LOC
in O
the O
Orange B-LOC
Free I-LOC
State I-LOC
, O
to O
Arthur B-PER
Reuel I-PER
Tolkien I-PER
, O
an O
English O
bank B-OCC
manager I-OCC
, O
and O
his O
wife O
Mabel B-PER
, O
née O
Suffield B-PER
. O
John B-PER
Ronald I-PER
Reuel I-PER
Tolkien I-PER
was O
born O
on O
three B-DAT
January I-DAT
eighteen I-DAT
ninety I-DAT
- I-DAT
two I-DAT
in O
Bloemfontein B-LOC
in O
the O
Orange B-LOC
Free I-LOC
State I-LOC
, O
to O
Arthur B-PER
Reuel I-PER
Tolkien I-PER
, O
an O
English O
bank B-OCC
manager I-OCC
, O
and O
his O
wife O
Mabel B-PER
, O
née O
Suffield B-PER
. O
...@@ -97,7 +97,7 @@ expected_predict_tags_aligned = [ ...@@ -97,7 +97,7 @@ expected_predict_tags_aligned = [
@pytest.mark.parametrize( @pytest.mark.parametrize(
"test_input, expected", ("test_input", "expected"),
[ [
( (
(fake_annot_original, fake_annot_aligned, fake_annot_tags_original), (fake_annot_original, fake_annot_aligned, fake_annot_tags_original),
......
import re
import pytest import pytest
from nerval import evaluate from nerval import evaluate
...@@ -168,7 +170,7 @@ expected_parsed_end_of_file = { ...@@ -168,7 +170,7 @@ expected_parsed_end_of_file = {
@pytest.mark.parametrize( @pytest.mark.parametrize(
"test_input, expected", ("test_input", "expected"),
[ [
(pytest.lazy_fixture("fake_annot_bio"), expected_parsed_annot), (pytest.lazy_fixture("fake_annot_bio"), expected_parsed_annot),
(pytest.lazy_fixture("fake_predict_bio"), expected_parsed_predict), (pytest.lazy_fixture("fake_predict_bio"), expected_parsed_predict),
...@@ -183,18 +185,21 @@ def test_parse_bio(test_input, expected): ...@@ -183,18 +185,21 @@ def test_parse_bio(test_input, expected):
def test_parse_bio_bad_input(bad_bio): def test_parse_bio_bad_input(bad_bio):
with pytest.raises(Exception): lines = bad_bio.read_text().strip().splitlines()
evaluate.parse_bio(bad_bio) with pytest.raises(
Exception, match=re.escape("The file is not in BIO format: check line 1 (file)")
):
evaluate.parse_bio(lines)
@pytest.mark.parametrize( @pytest.mark.parametrize(
"line, word, label", ("line", "word", "label"),
( [
("Hi B-ORG", "Hi", "B-ORG"), ("Hi B-ORG", "Hi", "B-ORG"),
("Hi B-Org or maybe not org", "Hi", "B-Org or maybe not org"), ("Hi B-Org or maybe not org", "Hi", "B-Org or maybe not org"),
("1258 B-Date et Lieu", "1258", "B-Date et Lieu"), ("1258 B-Date et Lieu", "1258", "B-Date et Lieu"),
("Devoti B-Sous-titre", "Devoti", "B-Sous-titre"), ("Devoti B-Sous-titre", "Devoti", "B-Sous-titre"),
), ],
) )
def test_parse_line(line, word, label): def test_parse_line(line, word, label):
assert parse_line(index=0, line=line) == (word, label) assert parse_line(index=0, line=line) == (word, label)
...@@ -202,21 +207,24 @@ def test_parse_line(line, word, label): ...@@ -202,21 +207,24 @@ def test_parse_line(line, word, label):
@pytest.mark.parametrize( @pytest.mark.parametrize(
"line", "line",
(("HiB-ORG"), ("HiB-ORG or maybe not")), [("HiB-ORG"), ("HiB-ORG or maybe not")],
) )
def test_parse_line_crash(line): def test_parse_line_crash(line):
with pytest.raises(Exception): with pytest.raises(
Exception,
match=re.escape(f"The file is not in BIO format: check line 0 ({line})"),
):
parse_line(index=0, line=line) parse_line(index=0, line=line)
@pytest.mark.parametrize( @pytest.mark.parametrize(
"label, expected_type", ("label", "expected_type"),
( [
("B-ORG", "ORG"), ("B-ORG", "ORG"),
("B-Date et Lieu", "Date et Lieu"), ("B-Date et Lieu", "Date et Lieu"),
("I-Date et Lieu", "Date et Lieu"), ("I-Date et Lieu", "Date et Lieu"),
("B-Sous-titre", "Sous-titre"), ("B-Sous-titre", "Sous-titre"),
), ],
) )
def test_get_type_label(label, expected_type): def test_get_type_label(label, expected_type):
assert get_type_label(label) == expected_type assert get_type_label(label) == expected_type
import re
from pathlib import Path
import pytest import pytest
from nerval import evaluate from nerval import evaluate
@pytest.mark.parametrize( @pytest.mark.parametrize(
"annotation, prediction, expected", ("annotation", "prediction", "expected"),
( [
( (
pytest.lazy_fixture("fake_annot_bio"), pytest.lazy_fixture("fake_annot_bio"),
pytest.lazy_fixture("fake_predict_bio"), pytest.lazy_fixture("fake_predict_bio"),
...@@ -82,7 +85,7 @@ from nerval import evaluate ...@@ -82,7 +85,7 @@ from nerval import evaluate
}, },
}, },
), ),
), ],
) )
def test_run(annotation, prediction, expected): def test_run(annotation, prediction, expected):
assert ( assert (
...@@ -97,16 +100,22 @@ def test_run(annotation, prediction, expected): ...@@ -97,16 +100,22 @@ def test_run(annotation, prediction, expected):
def test_run_empty_bio(empty_bio): def test_run_empty_bio(empty_bio):
with pytest.raises(Exception): with pytest.raises(
evaluate.run(empty_bio, empty_bio, 0.3) Exception, match="No content found in annotation or prediction files."
):
evaluate.run(empty_bio, empty_bio, 0.3, False)
def test_run_empty_entry(): def test_run_empty_entry():
with pytest.raises(TypeError): with pytest.raises(
evaluate.run(None, None, 0.3) AssertionError,
match=re.escape("Error: Input file invalid.bio does not exist"),
):
evaluate.run(Path("invalid.bio"), Path("invalid.bio"), 0.3, False)
@pytest.mark.parametrize("threshold", ([0.3])) def test_run_multiple(csv_file, folder_bio):
def test_run_multiple(csv_file, folder_bio, threshold): with pytest.raises(
with pytest.raises(Exception): Exception, match="No file found for files demo_annot.bio, demo_predict.bio"
evaluate.run_multiple(csv_file, folder_bio, threshold) ):
evaluate.run_multiple(csv_file, folder_bio, 0.3, False)
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