In [ ]:
import common
reload(common)

In [ ]:
from datetime import datetime
#science
import numpy as np
from scipy.sparse import coo_matrix
%matplotlib inline
import matplotlib as mpl
mpl.rc('figure', figsize=(40, 32))
import matplotlib.pyplot as plt
import pandas as pd
pd.set_option('display.max_rows', 200)
from ipywidgets import StaticInteract, RangeWidget

In [ ]:
def single_plot(subject, write_figure=False):
    conn, processed_seed_list, N = common.single_process(common.sc_dir(subject))
    fig, ax = plt.subplots()
    cax = ax.matshow(conn, interpolation='nearest')
    cax.set_cmap('hot')
    caxes = cax.get_axes()

    caxes.set_xticks(range(N))
    caxes.set_yticks(range(N))

    caxes.set_xticklabels(processed_seed_list, rotation=90)
    caxes.set_yticklabels(processed_seed_list, rotation=0)

    caxes.set_xlabel('Target ROI', fontsize=20)
    caxes.set_ylabel('Seed ROI', fontsize=20)

    cbar = fig.colorbar(cax)
    cbar.set_label('% of streamlines from seed to target', rotation=-90, fontsize=20)

    title_text = ax.set_title('Structural Connectivity with Freesurfer Labels & ProbtrackX2 ({})'.format(subject), fontsize=26)
    title_text.set_position((.5, 1.10))
    
    if write_figure:
        fig_date = datetime.now().strftime('%Y-%m-%d-%H%M')
        fig_name = os.path.join(wd, 'results/adjacency_{}.png'.format(fig_date))
        fig.savefig(fig_name, dpi=150)
        print("Wrote {}".format(fig_name))
    return fig


def plot_by_index(i, plot_kwargs=None):
    subject = common.subject_list[i]
    if not plot_kwargs:
        plot_kwargs = {}
    return single_plot(subject, **plot_kwargs)

In [ ]:
for i, subject in enumerate(common.subject_list):
    fig = plot_by_index(i)
    plt.show()

Graph Analysis


In [ ]:
import networkx as nx
G = nx.from_numpy_matrix(conn)
for n, name in zip(G.nodes_iter(), processed_seed_list):
    G.node[n]['name'] = name

In [ ]:
G = nx.MultiDiGraph()
for i, name in enumerate(processed_seed_list):
    G.add_node(i, attr_dict={'name': name})
for i, row in enumerate(conn):
    for j, val in enumerate(row):
        if i != j:
            if val > 1:
                G.add_edge(i, j, weight=val)

In [ ]:
name_to_deg = {G.node[i]['name']: val for i, val in G.degree().iteritems()}
sorted(name_to_deg, key=lambda x: name_to_deg[x], reverse=True )

In [ ]: