| import numpy as np |
| from rdkit.ML.Scoring.Scoring import CalcBEDROC, CalcAUC, CalcEnrichment |
| from sklearn.metrics import roc_curve |
|
|
| def re_new(y_true, y_score, ratio): |
| fp = 0 |
| tp = 0 |
| p = sum(y_true) |
| n = len(y_true) - p |
| num = ratio * n |
| sort_index = np.argsort(y_score)[::-1] |
| for i in range(len(sort_index)): |
| index = sort_index[i] |
| if y_true[index] == 1: |
| tp += 1 |
| else: |
| fp += 1 |
| if fp >= num: |
| break |
| return (tp * n) / (p * fp) |
|
|
|
|
| def calc_re(y_true, y_score, ratio_list): |
| fpr, tpr, thresholds = roc_curve(y_true, y_score, pos_label=1) |
| |
| res = {} |
| res2 = {} |
| total_active_compounds = sum(y_true) |
| total_compounds = len(y_true) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| for ratio in ratio_list: |
| res2[str(ratio)] = re_new(y_true, y_score, ratio) |
|
|
| |
| |
| return res2 |
|
|
|
|
| def cal_metrics(y_score, y_true, alpha=80.5): |
| """ |
| Calculate BEDROC score. |
| |
| Parameters: |
| - y_true: true binary labels (0 or 1) |
| - y_score: predicted scores or probabilities |
| - alpha: parameter controlling the degree of early retrieval emphasis |
| |
| Returns: |
| - BEDROC score |
| """ |
|
|
| |
| scores = np.expand_dims(y_score, axis=1) |
| y_true = np.expand_dims(y_true, axis=1) |
| scores = np.concatenate((scores, y_true), axis=1) |
| |
| scores = scores[scores[:, 0].argsort()[::-1]] |
| bedroc = CalcBEDROC(scores, 1, 80.5) |
| count = 0 |
| |
| index = np.argsort(y_score)[::-1] |
| for i in range(int(len(index) * 0.005)): |
| if y_true[index[i]] == 1: |
| count += 1 |
| auc = CalcAUC(scores, 1) |
| ef_list = CalcEnrichment(scores, 1, [0.005, 0.01, 0.05]) |
| return { |
| "BEDROC": bedroc, |
| "AUC": auc, |
| "EF0.5": ef_list[0], |
| "EF1": ef_list[1], |
| "EF5": ef_list[2] |
| } |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |