Skip to content
Snippets Groups Projects
Commit 917410f5 authored by Eva Bardou's avatar Eva Bardou :frog: Committed by Yoann Schneider
Browse files

Use csv.DictReader rather than csv.reader

parent d506b249
No related branches found
No related tags found
1 merge request!38Use csv.DictReader rather than csv.reader
Pipeline #150443 passed
Annotation,Prediction
demo_annot.bio,demo_predict.bio demo_annot.bio,demo_predict.bio
toy_test_annot.bio,toy_test_predict.bio toy_test_annot.bio,toy_test_predict.bio
\ No newline at end of file
import csv
import logging import logging
from csv import reader
from pathlib import Path from pathlib import Path
from typing import List from typing import List
...@@ -19,6 +19,10 @@ from nerval.utils import print_result_compact, print_results ...@@ -19,6 +19,10 @@ from nerval.utils import print_result_compact, print_results
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
ANNO_COLUMN = "Annotation"
PRED_COLUMN = "Prediction"
CSV_HEADER = [ANNO_COLUMN, PRED_COLUMN]
def compute_matches( def compute_matches(
annotation: str, annotation: str,
...@@ -346,7 +350,10 @@ def run_multiple(file_csv: Path, folder: Path, threshold: int, verbose: bool): ...@@ -346,7 +350,10 @@ def run_multiple(file_csv: Path, folder: Path, threshold: int, verbose: bool):
"""Run the program for multiple files (correlation indicated in the csv file)""" """Run the program for multiple files (correlation indicated in the csv file)"""
# Read the csv in a list # Read the csv in a list
with file_csv.open() as read_obj: with file_csv.open() as read_obj:
csv_reader = reader(read_obj) csv_reader = csv.DictReader(read_obj)
assert (
csv_reader.fieldnames == CSV_HEADER
), f'Columns in the CSV mapping should be: {",".join(CSV_HEADER)}'
list_cor = list(csv_reader) list_cor = list(csv_reader)
if not folder.is_dir(): if not folder.is_dir():
...@@ -363,14 +370,16 @@ def run_multiple(file_csv: Path, folder: Path, threshold: int, verbose: bool): ...@@ -363,14 +370,16 @@ def run_multiple(file_csv: Path, folder: Path, threshold: int, verbose: bool):
predict = None predict = None
for file in list_bio_file: for file in list_bio_file:
if row[0] == file.name: if row[ANNO_COLUMN] == file.name:
annot = file annot = file
for file in list_bio_file: for file in list_bio_file:
if row[1] == file.name: if row[PRED_COLUMN] == file.name:
predict = file predict = file
if not (annot and predict): if not (annot and predict):
raise Exception(f"No file found for files {row[0]}, {row[1]}") raise Exception(
f"No file found for files {row[ANNO_COLUMN]}, {row[PRED_COLUMN]}"
)
count += 1 count += 1
scores = run(annot, predict, threshold, verbose) scores = run(annot, predict, threshold, verbose)
......
...@@ -45,6 +45,11 @@ def folder_bio(): ...@@ -45,6 +45,11 @@ def folder_bio():
return FIXTURES return FIXTURES
@pytest.fixture()
def csv_file_error():
return FIXTURES / "test_mapping_file_error.csv"
@pytest.fixture() @pytest.fixture()
def csv_file(): def csv_file():
return FIXTURES / "test_mapping_file.csv" return FIXTURES / "test_mapping_file.csv"
Annotation,Prediction
demo_annot.bio,demo_predict.bio demo_annot.bio,demo_predict.bio
toy_test_annot.bio,toy_test_predict.bio toy_test_annot.bio,toy_test_predict.bio
\ No newline at end of file
Anno,Pred
demo_annot.bio,demo_predict.bio
toy_test_annot.bio,toy_test_predict.bio
\ No newline at end of file
...@@ -114,6 +114,13 @@ def test_run_empty_entry(): ...@@ -114,6 +114,13 @@ def test_run_empty_entry():
evaluate.run(Path("invalid.bio"), Path("invalid.bio"), 0.3, False) evaluate.run(Path("invalid.bio"), Path("invalid.bio"), 0.3, False)
def test_run_invalid_header(csv_file_error, folder_bio):
with pytest.raises(
Exception, match="Columns in the CSV mapping should be: Annotation,Prediction"
):
evaluate.run_multiple(csv_file_error, folder_bio, 0.3, False)
def test_run_multiple(csv_file, folder_bio): def test_run_multiple(csv_file, folder_bio):
with pytest.raises( with pytest.raises(
Exception, match="No file found for files demo_annot.bio, demo_predict.bio" Exception, match="No file found for files demo_annot.bio, demo_predict.bio"
......
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