# ---
# 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
# ---

# <br />
# <h1>GRAPH EXPLORER</h1>
#
# <h2>By: <NAME>, <NAME>, and <NAME></h2>
# <h3>JHU EN.601.223(13)<br />
# Introduction to Connectomics<br />
# Dr. <NAME><br />
# Intersession 2018</h3>
#
# <hr />

# # Getting Data
#
# First, we want to grab some graphs and subject covariates from a web-accessible url.  We've given this to you on google drive rather than having you set up aws s3 credentials in the interest of saving time. The original data is hosted at m2g.io
#
# Below, you will be getting the following dataset:
#
# | Property | Value |
# |:--------:|:-----:|
# | Dataset  | SWU4  |
# | N-Subjects  | 454   |
# | Scans-per-subjects | 2 |
# | Atlases | Desikan, CPAC200 |
# | Desikan Nodes | 70 |
# | CPAC200 Nodes | 200 |
#
# The covariates you have are: `SUBID, SESSION, AGE_AT_SCAN_1, SEX, RESTING_STATE_INSTRUCTION, TIME_OF_DAY, SEASON, SATIETY, LMP`. There are other columns in the `.csv` file (downloaded in the next step) but they are populated with a `#` meaning that the value was not recorded.
#
# There are several other atlases available - you can change which one you use 
# Running the cell below will get you the data. **Please note, you only have to run these two cells once!!!**

# ## Loading Graphs + Covariates
# Run the following cells of code to load the graphs into your computer, as well as the covariates.

# +
#CELL 1

# !pip install networkx==1.9 #networkx broke backwards compatibility with these graph files
# !pip install sklearn
import sklearn.ensemble
import numpy as np
import networkx as nx
import scipy as sp
import matplotlib.pyplot as plt
import os
import csv


from collections import OrderedDict

# +
#CELL 2
# Initializing dataset names
dataset_names = 'SWU4'

basepath = 'data'

# change which atlas you use, here!

atlas = 'desikan' # 'desikan' # or 'CPAC200', or 'Talairach'
dir_names = basepath + '/' + dataset_names + '/' + atlas
#basepath = "/"
#dir_names = basepath
print(dir_names)
fs = OrderedDict()
fs[dataset_names] = [root + "/" + fl for root, dirs, files in os.walk(dir_names)
                     for fl in files if fl.endswith(".gpickle")]

ps = "data/SWU4/SWU4.csv"

print("Datasets: " + ", ".join([fkey + " (" + str(len(fs[fkey])) + ")"
                                for fkey in fs]))
print("Total Subjects: %d" % (sum([len(fs[key]) for key in fs])))


# +
#CELL 3
def loadGraphs(filenames, verb=False):
    """
    Given a list of files, returns a dictionary of graphs

    Required parameters:
        filenames:
            - List of filenames for graphs
    Optional parameters:
        verb:
            - Toggles verbose output statements
    """
    #  Initializes empty dictionary
    gstruct = OrderedDict()
    for idx, files in enumerate(filenames):
        if verb:
            print("Loading: " + files)
        #  Adds graphs to dictionary with key being filename
        fname = os.path.basename(files)
        gstruct[fname] = nx.read_gpickle(files)
    return gstruct

def constructGraphDict(names, fs, verb=False):
    """
    Given a set of files and a directory to put things, loads graphs.

    Required parameters:
        names:
            - List of names of the datasets
        fs:
            - Dictionary of lists of files in each dataset
    Optional parameters:
        verb:
            - Toggles verbose output statements
    """
    #  Loads graphs into memory for all datasets
    graphs = OrderedDict()
    if verb:
        print("Loading Dataset: " + names)
    # The key for the dictionary of graphs is the dataset name
    graphs[names] = loadGraphs(fs[names], verb=verb)
    return graphs


# -

#CELL 4
graphs = constructGraphDict(dataset_names, fs, verb=False)

# +
#CELL 5
import csv
# This gets age and sex, respecitvely.
tmp = csv.reader(open(ps,newline='')) # this is the whole phenotype file
pheno = OrderedDict()
triple = [[t[0].strip(), t[2], int(t[3] == '2')] for t in tmp
          if t[3] != '#' and t[2] != '#'][1:]  # female=1->0, male=2->1

for idx, trip in enumerate(triple):
    pheno[trip[0]] = trip[1:]

# +
#CELL 6
k = list(graphs['SWU4'].keys())
g = list(graphs['SWU4'].values())
k = list(key[6:11] for key in k)
k = k[0::2]
g1 = g[0::2]
g2 = g[1::2]
d = dict(zip(k,g1))


#Create vectors of labels
age = list()
sex = list()
for x in pheno:
    if x in d:
        age.append(pheno[x][0])
        sex.append(pheno[x][1])
        
# should use g1 for now - g2 is the retest data for each subject        
# -

# ## ASSIGNMENT:  
# (Code above used to get data in the correct format.  Below is a simple example test string with kind of silly features)

# +
#CELL 7
#Combine features, separate training and test data

import skimage.feature as skif

X = []
for i in range(len(g1)):
    featvec = [] 
    
    #DEGREE CENTRALITY
    degree_c = nx.degree_centrality(g1[i])
    dlist = list(degree_c.values())
    degree_c = np.array(dlist)
    for ss in degree_c:
        featvec.append(ss)
        
    #ADJACENCY SPECTRUM
    adj_spec = nx.adjacency_spectrum(g1[i])
    for a in adj_spec:
        featvec.append(np.real(a))     
    
    #NUMBER OF EDGES
    featvec.append(nx.number_of_edges(g1[i]))
    
    #NUMBER OF NODES
    featvec.append(nx.number_of_nodes(g1[i]))

    #DENSITY
    featvec.append(nx.density(g1[i]))
    
    np.shape(featvec)
    X.append(featvec)

# +
#CELL 8
X_train = X[0:100]
Y_train = sex[0:100]

X_test = X[100:200]
Y_test = sex[100:200]

from sklearn.naive_bayes import GaussianNB

accuracy = []

for ii in range(10): #performance will change over time
    
    # WE ARE USING NAIVE BAYES CLASSIFIER! NOT RANDOM FOREST
    # BONUS POINTS FOR CLASSIFICATION TECHNIQUE BEYOND RANDOM FOREST
    clf = GaussianNB()
    
    clf.fit(X_train, Y_train)
    acc = (clf.predict(X_test) == Y_test)
    accval = (float(np.sum(acc))/float(len(Y_test)))
    accuracy.append(accval)
    print('Accuracy:',accval)

print('Overall Accuracy:',str(np.mean(accuracy)))
# -

# <!-- SCRAPS
#   
#     
#     #
#     #logmatrix = np.log10(np.sum(matrix,0) + 1)
#     #logmatrix = np.ravel(logmatrix)
#         
#     #for ii in logmatrix:
#     #    featvec.append(ii)
#     
#         #clstr = nx.clustering(g1[i], "min")
#     #clstr = list(clstr.values())
#     #clstr = np.array(clstr)
#     #for cc in clstr:
#     #    featvec.append(cc)
#     
#         
#     #matrix = nx.to_numpy_matrix(g1[i], nodelist=sorted(g1[i].nodes())) #this is how you go to a matrix
#
#     
#     #featvec.append(nx.degree_assortativity_coefficient(g1[i]))
#     
#     
#     
#     #featvec.append(nx.edge_connectivity(g1[i]))
#     
#     
#     #print(matrix)
#     
#     #xsum = np.asarray(np.sum(matrix))
#     #featvec.append(xsum) 
#     
# # plot a graph
# import matplotlib.pyplot as plt
# # %matplotlib inline
#
# final_matrix = np.zeros((70,70))
# for i in g1:
# # convert to numpy
#     matrix = nx.to_numpy_matrix(i, nodelist=sorted(i.nodes())) #this is how you go to a matrix
#     final_matrix = final_matrix + matrix
# final_matrix = final_matrix/len(g1)
#     
# plt.imshow(np.log10(final_matrix+1))
# plt.colorbar()
# plt.title('Mean Connectome')
# plt.show()
#     
#     
#     
#     
#     
#     -->

# +
#CELL 9
# plot a graph
import matplotlib.pyplot as plt
# %matplotlib inline
male_matrix = np.zeros((70,70))
female_matrix = np.zeros((70,70))
mcount = 0
fcount = 0
for i in range(0,len(sex)):
# convert to numpy
    if sex[i] == 1:
        matrix = nx.to_numpy_matrix(g1[i], nodelist=sorted(g1[i].nodes())) #this is how you go to a matrix
        male_matrix = male_matrix + matrix
        mcount = mcount + 1
    else:
        matrix = nx.to_numpy_matrix(g1[i], nodelist=sorted(g1[i].nodes())) #this is how you go to a matrix
        female_matrix = female_matrix + matrix
        fcount = fcount + 1
male_matrix = male_matrix/mcount
female_matrix = female_matrix/fcount
#print(male_matrix)

    
plt.imshow(np.log10(male_matrix+1))
plt.colorbar()
plt.title('Male Connectome')
plt.show()
# -

#CELL 10
plt.imshow(np.log10(female_matrix+1))
plt.colorbar()
plt.title('Female Connectome')
plt.show()
#print(female_matrix)



StarcoderdataJupyterScriptsDedupFiltered