File size: 2,163 Bytes
1bb8d13 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import json
import os
from itertools import permutations, chain
from string import ascii_letters
from statistics import mean
import numpy as np
import pandas as pd
os.makedirs("experiments/analysis/correlation", exist_ok=True)
prefix = "before"
with open("data/data_processed.test.jsonl") as f:
data = [json.loads(line) for line in f]
with open("data/data_processed.validation.jsonl") as f:
data += [json.loads(line) for line in f]
tmp = {}
for i in data:
if i['relation_type'] not in tmp:
tmp[i['relation_type']] = i['scores_all']
else:
tmp[i['relation_type']] = i['scores_all'] + tmp[i['relation_type']]
num_annotators = len(list(tmp.values())[0][0])
for r, scores in tmp.items():
corr_matrix = np.ones((num_annotators, num_annotators)) * 100
for a, b in permutations(range(num_annotators), 2):
score_a = [s[a] for s in scores]
score_b = [s[b] for s in scores]
corr_matrix[a][b] = pd.DataFrame([score_a, score_b]).T.corr("spearman").values[0][1] * 100
corr_df = pd.DataFrame(corr_matrix, columns=[ascii_letters[i].upper() for i in range(num_annotators)],
index=[ascii_letters[i].upper() for i in range(num_annotators)])
corr_df['Others'] = [pd.DataFrame([
[s[a] for s in scores],
[mean(_s for _n, _s in enumerate(s) if _n != a) for s in scores]
]).T.corr("spearman").values[0][1] * 100 for a in range(num_annotators)]
corr_df = corr_df.T
corr_df['Avg'] = corr_df.mean(1)
corr_df = corr_df.T
corr_df.to_csv(f"experiments/analysis/correlation/{prefix}.{r.replace(' ', '_').replace('/', '-')}.csv")
print(r)
print(corr_df.round(0).astype(int).to_latex())
print()
df = None
for r, scores in tmp.items():
if df is None:
df = pd.read_csv(f"experiments/analysis/correlation/{prefix}.{r.replace(' ', '_').replace('/', '-')}.csv", index_col=0)
else:
df += pd.read_csv(f"experiments/analysis/correlation/{prefix}.{r.replace(' ', '_').replace('/', '-')}.csv", index_col=0)
df = df/5
df = df.T
df.pop("Avg")
df['Avg'] = df.mean(1)
df = df.T
print("ALL")
print(df.round(0).astype(int).to_latex())
|