utilities.py 4.94 KB
Newer Older
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import numpy as np
import pandas as pd
import sklearn.metrics as metrics
import matplotlib.pyplot as plt

"""
Calculate and plot ROC curve.
Input:
    y_test: Real labels.
    preds: Predictions.
    edge: Edge to study.
    extension: Extension to the figure file.
Output:
    False positive and true positive rate and the area under the curve.
"""
def plot_roc(y_test, preds, edge, extension=''):
    fpr, tpr, threshold = metrics.roc_curve(y_test, preds)
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
18 19
    roc_auc = metrics.auc(fpr, tpr)

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
20
    label = " ".join(edge) + " " + 'AUC = %0.2f' % roc_auc
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
21
    fileName = 'metrics/aucroc' + extension + '.svg'
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
22
    random = [[0, 1], [0, 1], 'r--']
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
23

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    plotAndSaveFig(title='ROC Curve', x=fpr, y=tpr, label=label, path=fileName,
                   loc='lower right', xlim=[0, 1], ylim=[0, 1], xlabel='False Positive Rate',
                   ylabel='True Positive Rate', random=random)

    print("AUC:", roc_auc)
    return fpr, tpr, roc_auc


"""
Calculate and plot PR curve.
Input:
    y_test: Real labels.
    preds: Predictions.
    edge: Edge to study.
    extension: Extension to the figure file.
Output:
    Recall, precision and the area under the curve.
"""
def plot_prc(y_true, y_pred, edge, extension=''):
    precision, recall, trhesholds = metrics.precision_recall_curve(y_true, y_pred)
    average_precision = metrics.average_precision_score(y_true, y_pred)
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
45

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
46
    label = " ".join(edge) + " " + 'RPC = %0.2f' % average_precision
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
47 48
    fileName = 'metrics/prc' + extension + '.svg'

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
49 50
    plotAndSaveFig(title='Precision-Recall Curve', x=recall, y=precision, label=label, path=fileName,
                   loc='lower right', xlim=[0, 1], ylim=[0, 1], xlabel='Recall', ylabel='Precision')
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
51

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
52 53 54
    print("PRC:", average_precision)
    return recall, precision, average_precision

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
55

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
56 57 58 59 60 61 62 63 64 65 66 67 68
"""
Plot distribution of predictions of the true and random edges. Plots the distribution function and histogram.
Input:
    extension: Extension to the figure file.
"""
def plot_dist(extension=''):
    preds = [pd.read_csv('results/dis_dru_the_5013_table.csv'), pd.read_csv('results/dis_dru_the_5013R_table.csv')]
    df = pd.DataFrame(data={'RepoDB': preds[0]['pred'], 'Random': preds[1]['pred']})

    ax = df.plot.hist(bins=50, alpha=0.5)
    ax.set_xticks(np.arange(0, 1.1, 0.1))
    ax.set_title('RepoDB & Random Prediction Histogram')
    ax.set_xlabel('Prediction Score')
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
69
    ax.figure.savefig('results/histogram' + extension + '.svg', format='svg', dpi=1200)
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
70
    ax.figure.clf()
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
71

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
72 73 74
    bx = df.plot.kde()
    bx.set_title('RepoDB & Random Prediction')
    bx.set_xlabel('Prediction Score')
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
75
    bx.figure.savefig('results/distribution' + extension + '.svg', format='svg', dpi=1200)
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
76
    bx.set_xlim([0, 1])
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
77
    bx.figure.savefig('results/distribution01' + extension + '.svg', format='svg', dpi=1200)
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
78 79
    bx.figure.clf()

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
80

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
"""
It plots and saves the figure sent.
Input:
    title: Title of the plot.
    x: X axis.
    y: Y axis.
    label: Label of the plot.
    path: Path to save the figure.
    loc: Location of the legend.
    xlim: Limit of the X axis.
    ylim: Limit of the Y axis.
    xlabel: Label of the X axis.
    ylabel: Label of the Y axis.
    random: Random selector.
"""
def plotAndSaveFig(title, x, y, label, path, loc=None, xlim=None, ylim=None, xlabel=None, ylabel=None, random=None):
    plt.title(title)
    plt.plot(x, y, label=label)
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
99

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
100 101
    if loc is not None:
        plt.legend(loc=loc)
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
102

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
103 104
    if xlim is not None:
        plt.xlim(xlim)
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
105

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
106 107
    if ylim is not None:
        plt.ylim(ylim)
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
108

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
109 110
    if xlabel is not None:
        plt.xlabel(xlabel)
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
111

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
112 113
    if ylabel is not None:
        plt.ylabel(ylabel)
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
114

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
115 116
    if random is not None:
        plt.plot(random[0], random[1], random[2])
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
117

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
118 119 120 121
    plt.show()
    plt.savefig(path, format='svg', dpi=1200)
    plt.clf()

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
122

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
"""
It plots and saves together the figures sent.
Input:
    title: Title of the plot.
    x: X axis.
    y: Y axis.
    label: Label of the plot.
    path: Path to save the figure.
    loc: Location of the legend.
    xlim: Limit of the X axis.
    ylim: Limit of the Y axis.
    xlabel: Label of the X axis.
    ylabel: Label of the Y axis.
    random: Random selector.
"""
def plotTogether(title, x, y, label, path, loc=None, xlim=None, ylim=None, xlabel=None, ylabel=None, random=None):
    figH, axs = plt.subplots(2, figsize=(6, 10))
    figV, axs2 = plt.subplots(1, 2, figsize=(12, 4))
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
141

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
142 143 144
    for i, elem in enumerate(axs):
        elem.plot(x[i], y[i], label=label[i])
        elem.set_title(title[i])
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
145

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
146 147
        if loc[i] is not None:
            elem.legend(loc=loc[i])
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
148

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
149 150
        if random[i] is not None:
            elem.plot(random[i][0], random[i][1], random[i][2])
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
151

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
152 153
        if xlim[i] is not None:
            elem.set_xlim(xlim[i])
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
154

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
155 156
        if ylim[i] is not None:
            elem.set_ylim(ylim[i])
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
157

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
158 159
        if xlabel[i] is not None:
            elem.set_xlabel(xlabel)
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
160

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
161 162 163 164 165
        if ylabel[i] is not None:
            elem.set_ylabel(ylabel)

    axs2[0] = axs[0]
    axs2[1] = axs[1]
ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
166

ADRIAN  AYUSO MUNOZ's avatar
ADRIAN AYUSO MUNOZ committed
167 168
    figH.savefig(path[0], format='svg', dpi=1200)
    figV.savefig(path[1], format='svg', dpi=1200)