def test_sim_anneal_simple(): """Very simple simulated_annealing test with a small network""" # nnod, nmod, av_degree, btwn_frac = 24, 3, 4, 0 g = mod.random_modular_graph(nnod, nmod, av_degree, btwn_frac) #Compute the # of nodes per module ## nnod_mod = nnod/nmod #Make a "correct" partition for the graph ## ppart = mod.perfect_partition(nmod,nnod_mod) temperature = 10 temp_scaling = 0.95 tmin = 1 graph_out, graph_dict = mod.simulated_annealing(g, temperature=temperature, temp_scaling=temp_scaling, tmin=tmin, extra_info=True, debug=True) # Ensure that there are no empty modules util.assert_no_empty_modules(graph_out.index)
def run_sim_store_as_dict(g): """runs one pass of simulated annealing, stores results in a nice dict""" mod_out=dict() mod_raw_out=mod.simulated_annealing(g) mod_out['q']=mod_raw_out[0].modularity() mod_out['part']=mod_raw_out[0].index_as_node_names() return mod_out
def run_sim_store_as_dict(g): """runs one pass of simulated annealing, stores results in a nice dict""" mod_out = dict() mod_raw_out = mod.simulated_annealing(g) mod_out['q'] = mod_raw_out[0].modularity() mod_out['part'] = mod_raw_out[0].index_as_node_names() return mod_out
def calc_sa_modularity(mat, cost): """ Use brainx to generate binary matrix theshold based on cost use simulated annealing to find communities Parameters ---------- mat : numpy matrix symmetric adjacency matrix, should not contain nan cost: float cost used to binarize adjacency matrix Returns ------- part : GraphPartition part.index provides access to found communities true_cost : float the actual cost associated with thresholded adj matrix """ mask, real_cost = util.threshold_adjacency_matrix(mat, cost) true_cost = util.find_true_cost(mask) graph = nx.from_numpy_matrix(mask) part, mod = modularity.simulated_annealing(graph) return part, true_cost
def SA(): """ Test the simulated annealing script""" #nnod_mod, av_degrees, nmods #networks = [ [4, [2, 3], [2, 4, 6]]]#, #networks = [ [8, [4, 6], [4, 6, 8]]] #networks = [[40, [20], [2]]] networks = [[32, [16], [4]]] #networks = [[64, [12], [6]]] btwn_fracs = [0] temperature = 10 temp_scaling = 0.9995 tmin = 1e-4 nochange_ratio_min = 0.01 #keep time for nnod_mod, av_degrees, nmods in networks: for nmod in nmods: nnod = nnod_mod * nmod for av_degree in av_degrees: for btwn_frac in btwn_fracs: t1 = time.clock() g = mod.random_modular_graph(nnod, nmod, av_degree, btwn_frac) #Compute the # of nodes per module nnod_mod = nnod / nmod #Make a "correct" partition for the graph ppart = mod.perfect_partition(nmod, nnod_mod) graph_out, energy_array, rej_array, temp_array = mod.simulated_annealing( g, temperature=temperature, temp_scaling=temp_scaling, tmin=tmin, nochange_ratio_min=nochange_ratio_min) print "perfect partition", ppart print "SA partition", graph_out.index t2 = time.clock() print 'Elapsed time: ', float(t2 - t1) / 60, 'minutes' print 'partition similarity: ', mod.mutual_information( ppart, graph_out.index) return graph_out, g, energy_array, rej_array, ppart, temp_array
def SA(): """ Test the simulated annealing script""" # nnod_mod, av_degrees, nmods # networks = [ [4, [2, 3], [2, 4, 6]]]#, # networks = [ [8, [4, 6], [4, 6, 8]]] # networks = [[40, [20], [2]]] networks = [[32, [16], [4]]] # networks = [[64, [12], [6]]] btwn_fracs = [0] temperature = 10 temp_scaling = 0.9995 tmin = 1e-4 nochange_ratio_min = 0.01 # keep time for nnod_mod, av_degrees, nmods in networks: for nmod in nmods: nnod = nnod_mod * nmod for av_degree in av_degrees: for btwn_frac in btwn_fracs: t1 = time.clock() g = mod.random_modular_graph(nnod, nmod, av_degree, btwn_frac) # Compute the # of nodes per module nnod_mod = nnod / nmod # Make a "correct" partition for the graph ppart = mod.perfect_partition(nmod, nnod_mod) graph_out, energy_array, rej_array, temp_array = mod.simulated_annealing( g, temperature=temperature, temp_scaling=temp_scaling, tmin=tmin, nochange_ratio_min=nochange_ratio_min, ) print "perfect partition", ppart print "SA partition", graph_out.index t2 = time.clock() print "Elapsed time: ", float(t2 - t1) / 60, "minutes" print "partition similarity: ", mod.mutual_information(ppart, graph_out.index) return graph_out, g, energy_array, rej_array, ppart, temp_array
def test_sim_anneal_simple(): """Very simple simulated_annealing test with a small network""" # nnod, nmod, av_degree, btwn_frac = 24, 3, 4, 0 g = mod.random_modular_graph(nnod, nmod, av_degree, btwn_frac) #Compute the # of nodes per module ## nnod_mod = nnod/nmod #Make a "correct" partition for the graph ## ppart = mod.perfect_partition(nmod,nnod_mod) temperature = 10 temp_scaling = 0.95 tmin=1 graph_out, graph_dict = mod.simulated_annealing(g, temperature = temperature, temp_scaling = temp_scaling, tmin=tmin, extra_info = True, debug=True) # Ensure that there are no empty modules util.assert_no_empty_modules(graph_out.index)
def calc_modularity(inmat, ideal_cost = 0.1): """ Once we have a thresholded matrix, we can use simulated annealing to calculate the modularity of the graph, datgrap.index shows us the components that make up the distinct modules in the network (note ids start at 0, aal count from 1) """ inmat = np.load(inmat) G = nx.Graph(weighted=False) G = nx.from_numpy_matrix(inmat, G) ## fixed config parameters for simulated annealing temperature = 0.1 temp_scaling = 0.9995 tmin = 1e-4 ideal_cost = 0.1 (datgraph, modularity_value) = md.simulated_annealing(G, temperature = temperature, temp_scaling = temp_scaling, tmin = tmin, extra_info = False, debug = False) print modularity_value return datgraph, modularity_value
def danon_benchmark(): """This test comes from Danon et al 2005. It will create the line plot of Mututal Information vs. betweenness fraction to assess the performance of the simulated annealing algorithm.""" networks = [[32, [16], [6]]] btwn_fracs = [float(i)/100 for i in range(0,80,3)] temperature = 0.1 temp_scaling = 0.9995 tmin=1e-4 num_reps = range(1) mi_arr=np.empty((len(btwn_fracs),len(num_reps))) #keep time for rep in num_reps: t1 = time.clock() for nnod_mod, av_degrees, nmods in networks: for nmod in nmods: nnod = nnod_mod*nmod for av_degree in av_degrees: x_mod = [] for ix,btwn_frac in enumerate(btwn_fracs): print 'btwn_frac: ',btwn_frac g = mod.random_modular_graph(nnod, nmod, av_degree,btwn_frac) #Compute the # of nodes per module nnod_mod = nnod/nmod #Make a "correct" partition for the graph ppart = mod.perfect_partition(nmod,nnod_mod) graph_out, graph_dict =mod.simulated_annealing(g, temperature = temperature,temp_scaling = temp_scaling, tmin=tmin, extra_info = True) #print "SA partition",graph_out.index mi = mod.mutual_information(ppart,graph_out.index) t2 = time.clock() print 'Elapsed time: ', (float(t2-t1)/60), ' minutes' print 'partition similarity: ',mi mi_arr[ix,rep] = mi ## plot_partition(g,graph_out.index,'mi: '+ str(mi),'danon_test_6mod'+str(btwn_frac)+'_graph.png') x_mod.append(betweenness_to_modularity(g,ppart)) ## mi_arr_avg = np.mean(mi_arr,1) ## plt.figure() ## plt.plot(btwn_fracs,mi_arr_avg) ## plt.xlabel('Betweenness fraction') ## plt.ylabel('Mutual information') ## plt.savefig('danon_test_6mod/danontest_btwn.png') ## plt.figure() ## plt.plot(x_mod,mi_arr_avg) ## plt.xlabel('Modularity') ## plt.ylabel('Mutual information') ## plt.savefig('danon_test_6mod/danontest_mod.png') #plt.figure() #plt.plot(graph_dict['energy'], label = 'energy') #plt.plot(graph_dict['temperature'], label = 'temperature') #plt.xlabel('Iteration') return mi_arr
def danon_benchmark(): """This test comes from Danon et al 2005. It will create the line plot of Mututal Information vs. betweenness fraction to assess the performance of the simulated annealing algorithm.""" networks = [[32, [16], [6]]] btwn_fracs = [float(i) / 100 for i in range(0, 80, 3)] temperature = 0.1 temp_scaling = 0.9995 tmin = 1e-4 num_reps = range(1) mi_arr = np.empty((len(btwn_fracs), len(num_reps))) #keep time for rep in num_reps: t1 = time.clock() for nnod_mod, av_degrees, nmods in networks: for nmod in nmods: nnod = nnod_mod * nmod for av_degree in av_degrees: x_mod = [] for ix, btwn_frac in enumerate(btwn_fracs): print 'btwn_frac: ', btwn_frac g = mod.random_modular_graph(nnod, nmod, av_degree, btwn_frac) #Compute the # of nodes per module nnod_mod = nnod / nmod #Make a "correct" partition for the graph ppart = mod.perfect_partition(nmod, nnod_mod) graph_out, graph_dict = mod.simulated_annealing( g, temperature=temperature, temp_scaling=temp_scaling, tmin=tmin, extra_info=True) #print "SA partition",graph_out.index mi = mod.mutual_information(ppart, graph_out.index) t2 = time.clock() print 'Elapsed time: ', (float(t2 - t1) / 60), ' minutes' print 'partition similarity: ', mi mi_arr[ix, rep] = mi ## plot_partition(g,graph_out.index,'mi: '+ str(mi),'danon_test_6mod'+str(btwn_frac)+'_graph.png') x_mod.append(betweenness_to_modularity(g, ppart)) ## mi_arr_avg = np.mean(mi_arr,1) ## plt.figure() ## plt.plot(btwn_fracs,mi_arr_avg) ## plt.xlabel('Betweenness fraction') ## plt.ylabel('Mutual information') ## plt.savefig('danon_test_6mod/danontest_btwn.png') ## plt.figure() ## plt.plot(x_mod,mi_arr_avg) ## plt.xlabel('Modularity') ## plt.ylabel('Mutual information') ## plt.savefig('danon_test_6mod/danontest_mod.png') #plt.figure() #plt.plot(graph_dict['energy'], label = 'energy') #plt.plot(graph_dict['temperature'], label = 'temperature') #plt.xlabel('Iteration') return mi_arr