File size: 2,826 Bytes
d38bce3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import seaborn as sns; sns.set()
import matplotlib.pyplot as plt
from tqdm import tqdm
import os
import numpy as np
import scipy.sparse as sp

def degree_dist(clean_adj, perturbed_adj, savename='degree_dist.pdf'):
    """Plot degree distributnio on clean and perturbed graphs.

    Parameters
    ----------
    clean_adj: sp.csr_matrix
        adjancecy matrix of the clean graph
    perturbed_adj: sp.csr_matrix
        adjancecy matrix of the perturbed graph
    savename: str
        filename to be saved

    Returns
    -------
    None

    """
    clean_degree = clean_adj.sum(1)
    perturbed_degree = perturbed_adj.sum(1)
    fig, ax1 = plt.subplots()
    sns.distplot(clean_degree, label='Clean Graph', norm_hist=False, ax=ax1)
    sns.distplot(perturbed_degree, label='Perturbed Graph', norm_hist=False, ax=ax1)
    ax1.grid(False)
    plt.legend(prop={'size':18})
    plt.ylabel('Density Distribution', fontsize=18)
    plt.xlabel('Node degree', fontsize=18)
    plt.xticks(fontsize=14)
    plt.yticks(fontsize=14)
    # plt.title(f'Feature difference of adjacency after {attack}-attack')
    if not os.path.exists('figures/'):
       os.mkdir('figures')
    plt.savefig('figures/%s' % savename, bbox_inches='tight')
    plt.show()

def feature_diff(clean_adj, perturbed_adj, features, savename='feature_diff.pdf'):
    """Plot feature difference on clean and perturbed graphs.

    Parameters
    ----------
    clean_adj: sp.csr_matrix
        adjancecy matrix of the clean graph
    perturbed_adj: sp.csr_matrix
        adjancecy matrix of the perturbed graph
    features: sp.csr_matrix or np.array
        node features
    savename: str
        filename to be saved

    Returns
    -------
    None
    """

    fig, ax1 = plt.subplots()
    sns.distplot(_get_diff(clean_adj, features), label='Normal Edges', norm_hist=True, ax=ax1)
    delta_adj = perturbed_adj - clean_adj
    delta_adj[delta_adj < 0] = 0
    sns.distplot(_get_diff(delta_adj, features), label='Adversarial Edges', norm_hist=True, ax=ax1)
    ax1.grid(False)
    plt.legend(prop={'size':18})
    plt.ylabel('Density Distribution', fontsize=18)
    plt.xlabel('Feature Difference Between Connected Nodes', fontsize=18)
    plt.xticks(fontsize=14)
    plt.yticks(fontsize=14)
    if not os.path.exists('figures/'):
       os.mkdir('figures')
    plt.savefig('figures/%s' % savename, bbox_inches='tight')
    plt.show()


def _get_diff(adj, features):
    isSparse = sp.issparse(features)
    edges = np.array(adj.nonzero()).T
    row_degree = adj.sum(0).tolist()[0]
    diff = []
    for edge in tqdm(edges):
        n1 = edge[0]
        n2 = edge[1]
        if n1 > n2:
            continue
        d = np.sum((features[n1]/np.sqrt(row_degree[n1]) - features[n2]/np.sqrt(row_degree[n2])).power(2))
        diff.append(d)
    return diff