Upload data/evaluation/evaluate.py with huggingface_hub
Browse files- data/evaluation/evaluate.py +118 -0
data/evaluation/evaluate.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Evaluation script for CAMELYON17.
|
3 |
+
"""
|
4 |
+
|
5 |
+
import sklearn.metrics
|
6 |
+
import pandas as ps
|
7 |
+
|
8 |
+
import argparse
|
9 |
+
|
10 |
+
#----------------------------------------------------------------------------------------------------
|
11 |
+
|
12 |
+
def calculate_kappa(reference, submission):
|
13 |
+
"""
|
14 |
+
Calculate inter-annotator agreement with quadratic weighted Kappa.
|
15 |
+
|
16 |
+
Args:
|
17 |
+
reference (pandas.DataFrame): List of labels assigned by the organizers.
|
18 |
+
submission (pandas.DataFrame): List of labels assigned by participant.
|
19 |
+
|
20 |
+
Returns:
|
21 |
+
float: Kappa score.
|
22 |
+
|
23 |
+
Raises:
|
24 |
+
ValueError: Unknown stage in reference.
|
25 |
+
ValueError: Patient missing from submission.
|
26 |
+
ValueError: Unknown stage in submission.
|
27 |
+
"""
|
28 |
+
|
29 |
+
# The accepted stages are pN0, pN0(i+), pN1mi, pN1, pN2 as described on the website. During parsing all strings converted to lowercase.
|
30 |
+
#
|
31 |
+
stage_list = ['pn0', 'pn0(i+)', 'pn1mi', 'pn1', 'pn2']
|
32 |
+
|
33 |
+
# Extract the patient pN stages from the tables for evaluation.
|
34 |
+
#
|
35 |
+
reference_map = {df_row['patient']: df_row['stage'].lower() for _, df_row in reference.iterrows() if df_row['patient'].lower().endswith('.zip')}
|
36 |
+
submission_map = {df_row['patient']: df_row['stage'].lower() for _, df_row in submission.iterrows() if df_row['patient'].lower().endswith('.zip')}
|
37 |
+
|
38 |
+
# Reorganize data into lists with the same patient order and check consistency.
|
39 |
+
#
|
40 |
+
reference_stage_list = []
|
41 |
+
submission_stage_list = []
|
42 |
+
for patient_id, reference_stage in reference_map.items():
|
43 |
+
# Check consistency: all stages must be from the official stage list and there must be a submission for each patient in the ground truth.
|
44 |
+
#
|
45 |
+
submission_stage = submission_map[patient_id].lower()
|
46 |
+
|
47 |
+
if reference_stage not in stage_list:
|
48 |
+
raise ValueError('Unknown stage in reference: \'{stage}\''.format(stage=reference_stage))
|
49 |
+
if patient_id not in submission_map:
|
50 |
+
raise ValueError('Patient missing from submission: \'{patient}\''.format(patient=patient_id))
|
51 |
+
if submission_stage not in stage_list:
|
52 |
+
raise ValueError('Unknown stage in submission: \'{stage}\''.format(stage=submission_map[patient_id]))
|
53 |
+
|
54 |
+
# Add the pair to the lists.
|
55 |
+
#
|
56 |
+
reference_stage_list.append(reference_stage)
|
57 |
+
submission_stage_list.append(submission_stage)
|
58 |
+
|
59 |
+
# Return the Kappa score.
|
60 |
+
#
|
61 |
+
return sklearn.metrics.cohen_kappa_score(y1=reference_stage_list, y2=submission_stage_list, labels=stage_list, weights='quadratic')
|
62 |
+
|
63 |
+
#----------------------------------------------------------------------------------------------------
|
64 |
+
|
65 |
+
def collect_arguments():
|
66 |
+
"""
|
67 |
+
Collect command line arguments.
|
68 |
+
|
69 |
+
Returns:
|
70 |
+
(str, str): The parsed reference and submission CSV file paths.
|
71 |
+
"""
|
72 |
+
|
73 |
+
# Configure argument parser.
|
74 |
+
#
|
75 |
+
argument_parser = argparse.ArgumentParser(description='Calculate inter-annotator agreement.')
|
76 |
+
|
77 |
+
argument_parser.add_argument('-r', '--reference', required=True, type=str, help='reference CSV path')
|
78 |
+
argument_parser.add_argument('-s', '--submission', required=True, type=str, help='submission CSV path')
|
79 |
+
|
80 |
+
# Parse arguments.
|
81 |
+
#
|
82 |
+
arguments = vars(argument_parser.parse_args())
|
83 |
+
|
84 |
+
# Collect arguments.
|
85 |
+
#
|
86 |
+
parsed_reference_path = arguments['reference']
|
87 |
+
parsed_submission_path = arguments['submission']
|
88 |
+
|
89 |
+
# Print parsed parameters.
|
90 |
+
#
|
91 |
+
print(argument_parser.description)
|
92 |
+
print('Reference: {path}'.format(path=parsed_reference_path))
|
93 |
+
print('Submission: {path}'.format(path=parsed_submission_path))
|
94 |
+
|
95 |
+
return parsed_reference_path, parsed_submission_path
|
96 |
+
|
97 |
+
#----------------------------------------------------------------------------------------------------
|
98 |
+
|
99 |
+
|
100 |
+
if __name__ == '__main__':
|
101 |
+
|
102 |
+
# Parse parameters.
|
103 |
+
#
|
104 |
+
reference_path, submission_path = collect_arguments()
|
105 |
+
|
106 |
+
# Load tables to Pandas data frames.
|
107 |
+
#
|
108 |
+
reference_df = ps.read_csv(reference_path)
|
109 |
+
submission_df = ps.read_csv(submission_path)
|
110 |
+
|
111 |
+
# Calculate kappa score.
|
112 |
+
#
|
113 |
+
try:
|
114 |
+
kappa_score = calculate_kappa(reference=reference_df, submission=submission_df)
|
115 |
+
except Exception as exception:
|
116 |
+
print(exception)
|
117 |
+
else:
|
118 |
+
print('Score: {score}'.format(score=kappa_score))
|