def run_mini_pipeline(): atlas = datasets.fetch_atlas_msdl() atlas_img = atlas['maps'] labels = pd.read_csv(atlas['labels'])['name'] masker = NiftiMapsMasker(maps_img=atlas_img, standardize=True, memory='/tmp/nilearn', verbose=0) data = datasets.fetch_adhd(number_subjects) figures_folder = '../figures/' count=0 for func_file, confound_file in zip(data.func, data.confounds): # fit the data to the atlas mask, regress out confounds time_series = masker.fit_transform(func_file, confounds=confound_file) correlation = np.corrcoef(time_series.T) #plotting starts here plt.figure(figsize=(10, 10)) plt.imshow(correlation, interpolation="nearest") x_ticks = plt.xticks(range(len(labels)), labels, rotation=90) y_ticks = plt.yticks(range(len(labels)), labels) corr_file = figures_folder+'subject_number_' + str(count) + '_correlation.pdf' plt.savefig(corr_file) atlas_region_coords = [plotting.find_xyz_cut_coords(img) for img in image.iter_img(atlas_img)] threshold = 0.6 plotting.plot_connectome(correlation, atlas_region_coords, edge_threshold=threshold) connectome_file = figures_folder+'subject_number_' + str(count) + '_connectome.pdf' plt.savefig(connectome_file) #graph setup #binarize correlation matrix correlation[correlation<threshold] = 0 correlation[correlation != 0] = 1 graph = nx.from_numpy_matrix(correlation) partition=louvain.best_partition(graph) values = [partition.get(node) for node in graph.nodes()] plt.figure() nx.draw_spring(graph, cmap = plt.get_cmap('jet'), node_color = values, node_size=30, with_labels=True) graph_file = figures_folder+'subject_number_' + str(count) + '_community.pdf' plt.savefig(graph_file) count += 1 plt.close('all')
def vk_test(): user_id = 1 #graph = vk.get_friends_to_friends_graph(user_id) #vk.get_friends_to_friends_file(user_id, file_path="./data/friends_my.txt") graph = rg.read_from_file("./data/friends_my_without_single.txt") print(graph.number_of_edges()) print(graph.number_of_nodes()) print(graph.number_of_selfloops()) pa = louvain.best_partition(graph) sg.print_graph_communities(pa) sg.show_graph_communities(graph, pa)
def louvain_test(graph): ''' :param graph: networkx.Graph :return: None ''' print("Louvain has been started started") start_time = time.time() pa = louvain.best_partition(graph) print("time : ", time.time() - start_time) modular = louvain.modularity(pa, graph) print('Louvain modularity: ', modular) # sg.print_graph_communities(pa) sg.write_graph_communities(pa, "./data/vk_louvain_source.txt") print('\n')
def analyse_community(graph, save_name): partition = louvain.best_partition(graph) dendro = louvain.generate_dendrogram(graph) print 'Number of communities: ' + str(len(set(partition.values()))) logging.info('Number of communities: ' + str(len(set(partition.values())))) com_node_map = {} for community in set(partition.values()): com_node_map[community] = len([nodes for nodes in partition.keys() if partition[nodes] == community]) for k, v in com_node_map.items(): print 'Community ' + str(k+1) + ' has ' + str(v) + ' nodes' logging.info('Community ' + str(k+1) + ' has ' + str(v) + ' nodes') nx.set_node_attributes(graph, 'community', partition) no_pe_com_map_list = nodes_per_community(dendro) community_size_distribution(no_pe_com_map_list, save_name) drawNetwork(graph, save_name)
import sys sys.path.append("louvain") import louvain sys.path.append("infomap") import infomap import matplotlib.pyplot as plt import matplotlib.colors as colors import logging global_frame_list = [] dispatch = { 'infomap_partition': infomap.infomap(graph) 'louvain_partition': louvain.best_partition(graph) 'infomap_dendro': infomap.iteration_loop(graph) 'louvain_dendro': louvain.generate_dendrogram(graph) } def run_mini_pipeline(): datadir = 'data' for root, dirs, filenames in os.walk(datadir): for file in filenames: print 'computing subject ' + str(filenames.index(file)) logging.info('computing subject ' + str(filenames.index(file))) file_handle = os.path.join(root, file) df = pd.read_csv(file_handle, sep='\t') np_ts_matrix=df.as_matrix() correlation_matrix = np.corrcoef(np_ts_matrix.T)
# For some reason the type of this is still a multigraph, so louvain aborts. # Sledge hammer time und_G1 = nx.Graph(data=list(set(und_G0.edges()))) # Might as well just do the large connected component while were at it. components = nx.connected_components(und_G1) und_G = nx.subgraph(und_G1,components[0]) ####################################################################### ## ## F i n d i n g C o m m u n i t i e s ## ####################################################################### #partition = community.best_partition(und_G) partition = louvain.best_partition(und_G) ####################################################################### ## ## D r a w i n g ## ####################################################################### colors = get_partition_coloring(und_G,partition) pos = nx.spring_layout(und_G) #count = 0. nx.draw_networkx_nodes(und_G, pos, und_G.nodes(), node_size = 80, node_color = colors) nx.draw_networkx_edges(und_G,pos, alpha=0.5) plt.show()
def run_mini_pipeline(): atlas = datasets.fetch_atlas_msdl() atlas_img = atlas['maps'] labels = pd.read_csv(atlas['labels'])['name'] masker = NiftiMapsMasker(maps_img=atlas_img, standardize=True, memory='/tmp/nilearn', verbose=0) data = datasets.fetch_adhd(number_subjects) figures_folder = '../figures/' count = 0 for func_file, confound_file in zip(data.func, data.confounds): # fit the data to the atlas mask, regress out confounds time_series = masker.fit_transform(func_file, confounds=confound_file) correlation = np.corrcoef(time_series.T) #plotting starts here plt.figure(figsize=(10, 10)) plt.imshow(correlation, interpolation="nearest") x_ticks = plt.xticks(range(len(labels)), labels, rotation=90) y_ticks = plt.yticks(range(len(labels)), labels) corr_file = figures_folder + 'subject_number_' + str( count) + '_correlation.pdf' plt.savefig(corr_file) atlas_region_coords = [ plotting.find_xyz_cut_coords(img) for img in image.iter_img(atlas_img) ] threshold = 0.6 plotting.plot_connectome(correlation, atlas_region_coords, edge_threshold=threshold) connectome_file = figures_folder + 'subject_number_' + str( count) + '_connectome.pdf' plt.savefig(connectome_file) #graph setup #binarize correlation matrix correlation[correlation < threshold] = 0 correlation[correlation != 0] = 1 graph = nx.from_numpy_matrix(correlation) partition = louvain.best_partition(graph) values = [partition.get(node) for node in graph.nodes()] plt.figure() nx.draw_spring(graph, cmap=plt.get_cmap('jet'), node_color=values, node_size=30, with_labels=True) graph_file = figures_folder + 'subject_number_' + str( count) + '_community.pdf' plt.savefig(graph_file) count += 1 plt.close('all')