-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutils.py
55 lines (47 loc) · 1.77 KB
/
utils.py
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
import json
import csv
import numpy as np
import scipy.stats
from scipy.optimize import curve_fit
from sklearn.metrics import mean_squared_error
def load_csv_as_dict_list(file_path):
dict_list = []
with open(file_path, mode='r', encoding='utf-8') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
dict_list.append(row)
return dict_list
def load_json_as_dict_list(file_path):
with open(file_path, mode='r', encoding='utf-8') as json_file:
data = json.load(json_file)
return data
def load_data(file_path, file_type):
if file_type == 'csv':
return load_csv_as_dict_list(file_path)
elif file_type == 'json':
return load_json_as_dict_list(file_path)
else:
raise ValueError("Unsupported file type. Please use 'csv' or 'json'.")
def logistic_func(X, bayta1, bayta2, bayta3, bayta4):
# 4-parameter logistic function
logisticPart = 1 + np.exp(np.negative(np.divide(X - bayta3, np.abs(bayta4))))
yhat = bayta2 + np.divide(bayta1 - bayta2, logisticPart)
return yhat
def compute_metrics(y_pred, y):
'''
compute metrics btw predictions & labels
'''
# compute SRCC & KRCC
SRCC = scipy.stats.spearmanr(y, y_pred)[0]
try:
KRCC = scipy.stats.kendalltau(y, y_pred)[0]
except:
KRCC = scipy.stats.kendalltau(y, y_pred, method='asymptotic')[0]
# logistic regression btw y_pred & y
beta_init = [np.max(y), np.min(y), np.mean(y_pred), np.std(y_pred)]
popt, _ = curve_fit(logistic_func, y_pred, y, p0=beta_init, maxfev=int(1e8))
y_pred_logistic = logistic_func(y_pred, *popt)
# compute PLCC RMSE
PLCC = scipy.stats.pearsonr(y, y_pred_logistic)[0]
RMSE = np.sqrt(mean_squared_error(y, y_pred))
return SRCC, KRCC, PLCC, RMSE