# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import scipy.sparse as sparse
import pandas as pd
from itertools import count

# ## Visualize an adjacency matrix

graph = nx.Graph()

# +
graph.clear()

node_list = [1,2,3,4]
graph.add_nodes_from(node_list)

graph.add_edges_from([(1,2), (2,3), (3,4), (4,1)])
graph.add_edge(1,1)
graph.add_edge(2,4)
# -

nx.draw(graph, with_labels=True)
plt.show()

for i in node_list:
    print('node', i, 'adjacencies: ', graph.adj[i])

# +
adj_img = sparse.csr_matrix.todense(nx.adjacency_matrix(graph))

plt.imshow(adj_img, cmap='Greys')
plt.colorbar()
plt.show()
# -

# So because an adjacency matrix is just an image, we can attempt to use machine learning to classify the content it describes.

# ## Munge 3980

edges = pd.read_csv('../dat/facebook/3980.edges', sep=' ', skiprows=0, header=None, names=['node1','node2'])
edges.head()

nodes = pd.read_csv('../dat/facebook/3980.feat', sep=' ', skiprows=0, header=None, names=range(43))
nodes.drop(range(1,18), axis=1, inplace=True)
nodes.drop(range(19,43), axis=1, inplace=True)
nodes.head()

# +
graph.clear()

graph.add_node(3980, gender=0)

for i in range(nodes.shape[0]):
    graph.add_node(nodes[0].iloc[i], gender=nodes[18].iloc[i])

for i in range(edges.shape[0]):
    graph.add_edge(edges['node1'].iloc[i], edges['node2'].iloc[i])

for i in list(graph.nodes):
    graph.add_edge(3980, i)

# +
plt.figure(figsize=(15,10))
pos = nx.spring_layout(graph)

groups = set(nx.get_node_attributes(graph, 'gender').values())
mapping = dict(zip(sorted(groups),count()))
nodes = graph.nodes()
colors = [mapping[graph.node[n]['gender']] for n in nodes]

nx.draw_networkx_labels(graph, pos, font_size=8)

nc = nx.draw_networkx_nodes(graph, pos, nodelist=nodes, node_color=colors, with_labels=True,
                            node_size=100, cmap='plasma_r')
ec = nx.draw_networkx_edges(graph, pos, alpha=0.2)

plt.colorbar(nc)
plt.axis('off')
plt.show()

# +
adj_img = sparse.csr_matrix.todense(nx.adjacency_matrix(graph))

plt.figure(figsize=(8,8))
plt.imshow(adj_img, cmap='Greys')
plt.colorbar()
plt.show()
# -

adj_img.shape

# ## Munge 1912

edges = pd.read_csv('../dat/facebook/1912.edges', sep=' ', skiprows=0, header=None, names=['node1','node2'])
edges.head()

# Which columns represent gender?

# +
nodes = pd.read_csv('../dat/facebook/1912.feat', sep=' ', skiprows=0, header=None, names=range(481))

plt.figure(figsize=(15,5))
plt.plot(nodes[261])
plt.plot(nodes[260])
plt.show()

# +
diff = np.zeros(nodes.shape[0])
diff.fill(2)

for i in range(nodes.shape[0]):
    if nodes[260].iloc[i] == nodes[261].iloc[i]:
        diff[i] = 1
    else:
        diff[i] = 0
        
plt.plot(diff)
plt.show()
# -

np.nonzero(diff)

# Okay, so it's columns 260 and 261 which are the gender coordinates.

nodes = pd.read_csv('../dat/facebook/1912.feat', sep=' ', skiprows=0, header=None, names=range(481))
nodes.drop(range(1,260), axis=1, inplace=True)
nodes.drop(range(261,481), axis=1, inplace=True)
nodes.head()

# +
graph.clear()

graph.add_node(1912, gender=0)

for i in range(nodes.shape[0]):
    graph.add_node(nodes[0].iloc[i], gender=nodes[260].iloc[i])

for i in range(edges.shape[0]):
    graph.add_edge(edges['node1'].iloc[i], edges['node2'].iloc[i])
    
for i in list(graph.nodes):
    graph.add_edge(1912, i)

# +
# %%time

plt.figure(figsize=(15,10))
pos = nx.spring_layout(graph)

groups = set(nx.get_node_attributes(graph, 'gender').values())
mapping = dict(zip(sorted(groups),count()))
noodes = graph.nodes()
colors = [mapping[graph.node[n]['gender']] for n in noodes]

nx.draw_networkx_labels(graph, pos, font_size=8)

nc = nx.draw_networkx_nodes(graph, pos, nodelist=noodes, node_color=colors, with_labels=True,
                            node_size=100, cmap='plasma_r')
ec = nx.draw_networkx_edges(graph, pos, alpha=0.2)

plt.colorbar(nc)
plt.axis('off')
plt.show()

# +
adj_img = sparse.csr_matrix.todense(nx.adjacency_matrix(graph))

plt.figure(figsize=(8,8))
plt.imshow(adj_img, cmap='Greys')
plt.colorbar()
plt.show()
# -

adj_img.shape

# ## Build Ftr Vects

oneD = np.ravel(adj_img)

# +
traindat = np.zeros((nodes.shape[0], oneD.shape[0] + 1))

for i in range(nodes.shape[0]):
    traindat[i, 0] = i
    traindat[i, 1:] = oneD

# +
trainlbl = np.zeros(nodes.shape[0])

for i in range(nodes.shape[0]):
    trainlbl[i] = nodes[260].iloc[i]
# -

# ## Random Forest clf

from sklearn.ensemble import RandomForestClassifier as rfc
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import roc_curve, auc
from scipy import interp

# +
# %%time

plt.figure(figsize=(10,10))


cv = StratifiedKFold(n_splits=10)
clf = rfc(n_estimators=50, n_jobs=-1)

tprs = []
aucs = []
mean_fpr = np.linspace(0, 1, 100)

i = 0
for train, test in cv.split(traindat, trainlbl):
    probas_ = clf.fit(traindat[train], trainlbl[train]).predict_proba(traindat[test])

    fpr, tpr, thresholds = roc_curve(trainlbl[test], probas_[:, 1])
    tprs.append(interp(mean_fpr, fpr, tpr))
    tprs[-1][0] = 0.0
    roc_auc = auc(fpr, tpr)
    aucs.append(roc_auc)
    plt.plot(fpr, tpr, lw=1, alpha=0.3,
             label='ROC fold %d (AUC = %0.2f)' % (i, roc_auc))

    i += 1
    
plt.plot([0, 1], [0, 1], linestyle='--', lw=2, color='r',
         label='Coin Flip (50% chance)', alpha=.8)

mean_tpr = np.mean(tprs, axis=0)
mean_tpr[-1] = 1.0
mean_auc = auc(mean_fpr, mean_tpr)
std_auc = np.std(aucs)
plt.plot(mean_fpr, mean_tpr, color='b',
         label=r'Mean ROC (AUC = %0.2f $\pm$ %0.2f)' % (mean_auc, std_auc),
         lw=2, alpha=.8)

std_tpr = np.std(tprs, axis=0)
tprs_upper = np.minimum(mean_tpr + std_tpr, 1)
tprs_lower = np.maximum(mean_tpr - std_tpr, 0)
plt.fill_between(mean_fpr, tprs_lower, tprs_upper, color='grey', alpha=.2,
                 label=r'$\pm$ 1 std. dev.')

plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic example')
plt.legend(loc="lower right")
plt.show()
# -

# ## Linear NN

import tensorflow as tf

trainlbl_new = np.expand_dims(trainlbl, axis=1)

trainlbl_new.shape

# +
# placeholders for I/O
inp = tf.placeholder('float', [None, 571537], name='inp')
otp = tf.placeholder('float', [None, 1], name='otp')

# build weight matrices
w1 = tf.Variable(tf.random_normal([571537,1000], stddev=0.01), name='w1')
w2 = tf.Variable(tf.random_normal([1000,500], stddev=0.01), name='w2')
w3 = tf.Variable(tf.random_normal([500,50], stddev=0.01), name='w3')
w4 = tf.Variable(tf.random_normal([50,1], stddev=0.01), name='w4')

# weight matrix histogram summaries
tf.summary.histogram('w1_summ',w1)
tf.summary.histogram('w2_summ',w2)
tf.summary.histogram('w3_summ',w3)
tf.summary.histogram('w4_summ',w4)

# build model
def model(inp, w1, w2, w3, w4):
	with tf.name_scope('layer1'):
		l1 = tf.nn.relu(tf.matmul(inp, w1))
	with tf.name_scope('layer2'):
		l2 = tf.nn.relu(tf.matmul(l1, w2))
	with tf.name_scope('layer3'):
		l3 = tf.nn.relu(tf.matmul(l2, w3))
	with tf.name_scope('layer4'):
		return tf.matmul(l3, w4)

mod = model(inp, w1, w2, w3, w4)

# build loss function
with tf.name_scope('loss'):
	loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=mod, labels=otp))
	train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(loss)
	tf.summary.scalar('loss', loss)

# validate
with tf.name_scope('validation'):
	correct = tf.equal(tf.argmax(otp, 1), tf.argmax(mod, 1))
	val_op  = tf.reduce_mean(tf.cast(correct, 'float'))
	tf.summary.scalar('accuracy', val_op)

with tf.Session() as sess:
	# write out log
	writer = tf.summary.FileWriter('../dat/logs/mlp', sess.graph)
	merged = tf.summary.merge_all()

	# init
	tf.global_variables_initializer().run()

	# train
	for i in range(10):
		sess.run(train_op, feed_dict={inp: traindat, otp: trainlbl_new})

		summary, acc = sess.run([merged, val_op],
			feed_dict={inp: testdat, otp: testlbl})
		writer.add_summary(summary, i)

		print(i, acc)
# -

# ## CNN

# +
#import tensorflow as tf
# -



StarcoderdataJupyterScriptsDedupFiltered