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 from datasets import load_dataset os.makedirs("experiments/analysis/correlation", exist_ok=True) prefix = "after" with open("data/data_processed.new.test.jsonl") as f: data = [json.loads(line) for line in f] with open("data/data_processed.new.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())