from operator import itemgetter

import numpy as np
from scipy.special import gamma
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()

from sgp import GGPrnd, BSgraphrnd, GGPgraphrnd, GGPmixtureGraphrnd
from sgp.GraphUtil import compute_growth_rate, degree_distribution, degree_one_nodes

%matplotlib inline

"""
Explanation: Simulating a mixture of sparse + dense network from a mixture of GGPs
End of explanation
"""


s_alpha = 20
s_tau = 1
s_sigma = 0.5

d_alpha = 100
d_tau = 1
d_sigma = -0.2

G, D, w, w_rem, params, sparse_nodes, dense_nodes = GGPmixtureGraphrnd(s_alpha, s_sigma, s_tau, d_alpha, d_sigma, d_tau)

"""
Explanation: Set up hyper-params for the mixture graph
Expected weight from dense-GGP is -d_sigma / d_tau
End of explanation
"""


n = G.shape[0]
n_s = len(sparse_nodes); n_d = len(dense_nodes)
print('sparse nodes', n_s)
print('dense nodes', n_d)

plt.figure(figsize=(12, 12))
ax = sns.heatmap(G.toarray(), square=True, xticklabels=False, yticklabels=False)
ax.set_xticks([n_s])
ax.set_xticklabels(['Sparse|Dense'])
ax.set_yticks([n_d])
ax.set_yticklabels(['Dense|Sparse'], rotation='vertical')
ax.hlines(n_d, xmin=0, xmax=n, linestyles='dashed', color='blue')
ax.vlines(n_s, ymin=0, ymax=n, linestyles='dashed', color='blue')

"""
Explanation: How many sparse & dense nodes in the graph?
End of explanation
"""


g_rate = compute_growth_rate(G)
plt.plot(g_rate)
plt.title('Growth rate of the graph')
plt.xlabel('# of nodes')
plt.ylabel('# of edges')

"""
Explanation: The sparse nodes are sorted by their weights.
Compute the growth rate of random graph
End of explanation
"""


d, d_list = degree_distribution(G)
plt.loglog(d_list[1:])
plt.title('Node degree distribution (in+outdegree)')
plt.xlabel('Degree of node')
plt.ylabel('# of nodes')

print('Number of nodes with degree one', degree_one_nodes(G))

"""
Explanation: Compute degree distribution of random graph
End of explanation
"""


