def weight_label_generators(gen_dict,G,ascending=False): weighted_generators={}; for key in gen_dict: weighted_generators[key]=[]; edge_weights=nx.get_edge_attributes(G,'weight').values(); edge_weights=list(set(edge_weights)); if ascending==False: edge_weights=sorted(edge_weights, reverse=True); max_weight=edge_weights[0]; else: edge_weights=sorted(edge_weights); max_weight=edge_weights[-1]; for key in gen_dict: for rank_cycle in gen_dict[key]: b=int(float(rank_cycle.start)); d=int(float(rank_cycle.end)); comp=rank_cycle.composition; dim=rank_cycle.dim; if ascending==True: weighted_generators[key].append(ho.Cycle(dim,comp,edge_weights[b],edge_weights[d])); else: weighted_generators[key].append(ho.Cycle(dim,comp,edge_weights[d],edge_weights[b])); return weighted_generators;
def read_generators_scaff(mat, G, type_analysis, output_folder, name_output_holes, index_graph=None): """ Given parameters to read output file of holes/javaplex and the initial graph from where we have computed homology (after "bin" the graph, return bined graph (input to holes) and dictionary of cycles for dim 0 and 1 obtained from holes. Input: - mat (read correlation matrix) - G: binned network and converted to distance network - output_folder,name_output_holes: path to be able to load results obtained from holes - type_analysis: by subject or averaged - index_graph: if we are in type_analysis by subject we need to indicate in which index we are Output: - G (networkx graph) - wgen1 (dict) as keys dim 0 and dim 1 in homology that contains lists of Holes.classes.cycle.Cycle, contains output holes """ print 'type_analysis ', type_analysis if (type_analysis == 'by_subject' and index_graph != None): # print '1' file_gen = output_folder + 'gen/generators_' + name_output_holes + '_%i_.pck' % index_graph if (type_analysis == 'by_subject' and index_graph == None): # print '2' file_gen = output_folder + 'gen/generators_' + name_output_holes + '_.pck' if (type_analysis == 'averaged'): # print '3' file_gen = output_folder + 'gen/generators_' + name_output_holes + '_.pck' gen1 = pk.load(open(file_gen)) wgen1 = ho.weight_label_generators(gen1, G, ascending=True) return (G, wgen1)
def ProcessFile(filename): print("Processing '%s'" % (filename)) file_title = os.path.splitext(os.path.basename(filename))[0] matrix = np.loadtxt(filename, delimiter=",") G = nx.Graph() for i in range(0,matrix.shape[0]): for j in range(i,matrix.shape[1]): G.add_edge(i,j,weight=matrix[i][j]) #Once the network has been loaded, one needs to produce the filtration of the #network. This can be done in different ways. The simplest is to rank the #edges in descending order and use their rank as the indices for the sequence #of simplicial complex. Holes contains a few options for different #filtrations (ascending, descending, metrical..). Below we consider the one #with descending weights described in #[http://www.plosone.org/article/info%3Adoi%2F10.1371%2Fjournal.pone.0066506]: fil = ho.filtrations.dense_graph_weight_clique_rank_filtration(G,2) #Once the filtration has been created, it needs to be saved and passed to #jython, so that javaplex can receive the data and process it. This requires #calling a subprocess and feeding it the right file. clique_dictionary_file = './output/'+file_title+'_filtration.pck' pk.dump(fil,open(clique_dictionary_file,'w')) hom_dim = 2 # max homology group calculated dataset_tag = 'test_IO2009' output_dir = './output/'; gen_file = './output/gen/generators_test_'+file_title+'_.pck' if not os.path.isfile(gen_file): ho.persistent_homology_calculation(clique_dictionary_file, hom_dim, dataset_tag, output_dir, jython_call='C:\\jython2.5.4rc1\\jython.bat', m1=2048, m2=2048) gen = pk.load(open(gen_file)) #The properties of each generator can be listed easily: gen[1][0].summary() #One can also simply produce the barcodes for the network then: ho.barcode_creator(gen[0]) plt.title(r'Barcode for $H_0$, '+file_title) plt.savefig('Barcode0_2006.png') ho.barcode_creator(gen[1]) plt.title(r'Barcode for $H_1$, '+file_title) plt.savefig('Barcode1_2006.png') ho.barcode_creator(gen[2]) plt.title(r'Barcode for $H_2$, '+file_title) plt.savefig('Barcode2_2006.png')
def calculate_persistent_homology(edge_dir, output_dir, hom_dim=1): """ :param edge_dir: :param output_dir: :param hom_dim: :return: """ import time t1 = time.time() fls = os.listdir(edge_dir) kv_files = [(i, i.split('_')[-1].split('.')[0]) for i in fls] edge_paths = [edge_dir + '/{0}'.format(i[0]) for i in sorted(kv_files, key=lambda line: int(line[1]))] for ep in edge_paths: dataset_tag = ep.split('/_')[-1].split('.txt')[0] clique_save_file = os.path.join(output_dir, 'clique_dictionaries/_{}.pkl'.format(dataset_tag)) fil_file = weight_rank_filter(ep, clique_save_file) ho.persistent_homology_calculation(fil_file, hom_dim, dataset_tag, output_dir + '/', m1=512, m2=2048) dt = time.time() - t1 print("Total time taken is: {}".format(dt))
original_graph= #name of the edgelist file dir= #output directory path dataset_tag= #name tag IR_weight_cutoff= #optional cutoff weight else: if len(sys.argv)>=3: dir=str(sys.argv[1]); #insert final slash dataset_tag=str(sys.argv[2]); #without underlines else: print('Input is required as:\n 1) name of output directory \n 2) name tag for output files'); sys.exit(); if not os.path.exists(dir): os.makedirs(dir) sys.path.append('../') import Holes as ho import pickle as pk generators_dict=pk.load(open(dir+'gen/generators_'+dataset_tag+'_.pck')) for key in generators_dict: if len(generators_dict[key])>0: print key ho.barcode_creator(generators_dict[key]); plt.show() ho.complete_persistence_diagram(generators_dict[key])
Clique_dictionary[str(list(subclique))]=[]; Clique_dictionary[str(list(subclique))].append(str(1)); Clique_dictionary[str(list(subclique))].append(str(1)) # output of the filtration file in a hopefully matlab compliant form fname=dir+dataset_tag+'_random%d_clique_filtration.pck' %i filtration_file=open(fname,'w'); pk.dump(Clique_dictionary,filtration_file); filtration_file.close(); del filtration_file; imp.reload(ho); max_homology_dimension=k; try: ho.persistent_homology_calculation(fname,k,dataset_tag,dir) except OSError, e: print "Execution failed for file:"+str(fname); generators_dict=pk.load(open(dir+'gen/generators_' +dataset_tag+'_.pck', 'rb')) gname=dir+'gen/generators_'+dataset_tag+'_random%d.pck' %i gfile=open(gname,'w') pk.dump(generators_dict,gfile) betti_num=pk.load(open(dir+'gen/betti_'+dataset_tag+'_.pck', 'rb')) for j in betti_num.strip('{}').split(','): d = 0 for k in j.split(':'): if d == 0:
import string for h in range(dimension + 1): Generator_dictionary[h] = [] list_gen = list(annotated_intervals.getGeneratorsAtDimension(h)) # if save_generators==True: # gen_details_file=open(gendir+'details_generators_'+str(h)+'_'+str(stringie)+'.pck','w'); # pickle.dump(list_gen,gen_details_file); list_intervals = list(annotated_intervals.getIntervalsAtDimension(h)) for n, key in enumerate(list_gen): test = str(list_intervals[n]).split(',') test[0] = test[0].strip(' [') test[1] = test[1].strip(' )') if test[1] == 'infinity': test[1] = str(max_filtration_value) line = str(key) line = line.translate(string.maketrans('', ''), '-[]') line = re.sub('[,+ ]', ' ', line) line = line.split() tempcycle = Holes.Cycle(h, list2simplexes(line, h), test[0], test[1]) Generator_dictionary[h].append(tempcycle) del tempcycle for cycle in Generator_dictionary[h]: cycle.summary() filename = os.path.join(gendir, 'generators_' + str(stringie) + '.pck') generator_dict_file = open(filename, 'w') pickle.dump(Generator_dictionary, generator_dict_file) print 'Generator dictionary dumped to ' + filename, generator_dict_file.close()
#!/usr/bin/env python import os, sys import networkx as nx sys.path.append('../../../') import Holes as ho import pickle as pk import numpy as np import matplotlib.pyplot as plt gen_file = './output/gen/generators_test_IO2007_.pck' if not os.path.isfile(gen_file): os.environ["JAVA_OPTS"]="-Xms2048m -Xmx2048m" ho.persistent_homology_calculation(clique_dictionary_file, hom_dim, dataset_tag, output_dir, jython_call='C:\\jython2.5.4rc1\\jython.bat', m1=2048, m2=2048) gen = pk.load(open(gen_file)) #The properties of each generator can be listed easily: gen[1][0].summary() #One can also simply produce the barcodes for the network then: ho.barcode_creator(gen[0]) plt.title(r'Barcode for $H_0$, IO2007') plt.savefig('Barcode0.png') ho.barcode_creator(gen[1]) plt.title(r'Barcode for $H_1$, IO2007') plt.savefig('Barcode1.png')