def __init__(self, graph_name, fitness_val, number_of_runs): self.graph = utils.string_to_graph(graph_name) self.graph = nx.relabel_nodes(self.graph, lambda x: (x, 1)) self.status = 'ongoing' self.time_step = 0 self.fitness_val = fitness_val self.number_of_runs = number_of_runs self.number_of_nodes = len(self.graph.nodes())
def sim_upload_graphs(list_of_graphs): q = mp.Queue() q_lock = mp.Lock() v = mp.Value('i', 0) v_lock = mp.Lock() for graph_name in list_of_graphs: v.value = 0 return_dict = mp.Manager().dict() graph = utils.string_to_graph(graph_name) for i in range(graph.number_of_nodes()): q.put(i) processes = [mp.Process(name="thread %s"%x, target=thread_node_sim_upload_general, args=(graph_name, q_lock, q, v_lock, v, return_dict)) for x in range(utils.NUMBER_OF_THREADS)] for process in processes: process.start() while not q.empty(): pass for process in processes: process.join() # upload to targets database bson_object = {} bson_object['graph_name'] = graph_name bson_object['adj_matrix'] = pickle.dumps(nx.adjacency_matrix(graph)) bson_object['rep_dim'] = '?' bson_object['f_val'] = utils.FITNESS bson_object['num_runs'] = utils.NUMBER_OF_RUNS bson_object['num_nodes'] = graph.number_of_nodes() bson_object['num_edges'] = graph.number_of_edges() bson_object['node_vectors'] = dict(return_dict) utils.TARGETS.insert_one(bson_object)
def thread_embed(q_lock, q, v_lock, v): print("%s started" % mp.current_process().name) while not q.empty(): with q_lock: graph_name = q.get() try: csv_filename = graph_name + '.csv' node_emb_filename = graph_name + '.nemb' g = utils.string_to_graph(graph_name) if not nx.is_connected(g): raise utils.GraphDisconnectedError() edgelist_path = utils.I_DATAPATH + csv_filename nx.write_edgelist(g, edgelist_path, data=False) node_emb_path = utils.N_DATAPATH + node_emb_filename command = [ 'python2.7', '../node2vec/src/main.py', '--input', edgelist_path, '--output', node_emb_path, '--dimensions', str(utils.REP_DIMENSIONS) ] subprocess.call(command) except utils.GraphDisconnectedError: print("Graph is disconnected") with v_lock: v.value += 1 print("Graph %s is processed" % v.value) print("%s finished" % mp.current_process().name)
sys.path.append('../graphwave/') from GraphWave.graphwave import graphwave from GraphWave.shapes import build_graph # from GraphWave import * # from GraphWave import shapes, graphwave, heat_diffusion, characteristic_functions # from GraphWave.shapes import build_graph,shapes # from GraphWave.heat_diffusion import * # from GraphWave.utils.graph_tools import * # from GraphWave.utils.utils import * # from GraphWave.characteristic_functions import * np.random.seed(123) for graph_name in utils.get_connected_builtins(utils.get_builtins_medium()): graph = utils.string_to_graph(graph_name) try: chi, heat_print, taus = graphwave(graph, 'automatic', verbose=False) for node_index, feature_vec in enumerate(chi): feature_vec = list(feature_vec) utils.GRAPHWAVE_DATA_FINAL.update_one({"graph_name": graph_name}, { "$set": { "node_vectors.%s.feature_vec" % node_index: feature_vec, "rep_dim": len(feature_vec) } }, upsert=True)
def thread_embed_upload(r,s,q_lock, q, v_lock, v): print("%s started" % mp.current_process().name) while not q.empty(): with q_lock: graph_name = q.get() try: csv_filename = graph_name + 'r=%s_s=%s'%(r,s) + '.csv' node_emb_filename = graph_name + 'r=%s_s=%s'%(r,s)'.nemb' g = utils.string_to_graph(graph_name) if not nx.is_connected(g): raise utils.GraphDisconnectedError() edgelist_path = utils.I_DATAPATH + csv_filename nx.write_edgelist(g, edgelist_path, data=False) node_emb_path = utils.N_DATAPATH + node_emb_filename command = ['python2.7', '../node2vec/src/main.py', '--input', edgelist_path, '--output', node_emb_path, '--dimensions', str(utils.REP_DIMENSIONS), '--window-size', str(20), '--q', str(r), '--p', str(s)] subprocess.call(command) #handle uploading part now rs_map = {(1,8): '0125', (1,4): '025', (1,2): '05', (1,1): '1', (2,1): '2', (4,1): '4', (8,1): '8'} collection = pymongo.MongoClient(utils.MONGO_URI)['moran']['NODE_qp_'+rs_map[(r,s)]] bson_object = {} bson_object['num_nodes'] = g.number_of_nodes() bson_object['num_edges'] = g.number_of_edges() bson_object['adj_mat'] = pickle.dumps(nx.adjacency_matrix(g)) bson_object['num_runs'] = utils.NUMBER_OF_RUNS bson_object['f_val'] = utils.FITNESS with open(node_emb_path) as csvfile: node_reader = csv.reader(csvfile, delimiter = ' ') _, rep_dim = next(node_reader) bson_object['rep_dim'] = int(rep_dim) node_vectors = {} for node_vec in node_reader: node_vectors[node_vec[0]] = {'feature_vec': list(map(lambda x: float(x), node_vec[1:])), 'target_vec': {}} bson_object['node_vectors'] = node_vectors collection.insert_one(bson_object) print("Done with uploading %s!" % graph_name) except utils.GraphDisconnectedError: print("Graph is disconnected") with v_lock: v.value += 1 print("Graph %s is processed" % v.value) print("%s finished" % mp.current_process().name)
def thread_sim_upload(q_lock, q, v_lock, v): while not q.empty(): with q_lock: file_name = q.get() #file_name is path to node_emb graph_name = file_name.split("/")[-1] graph_name = graph_name.split(".nemb")[0] adj_mat = nx.adjacency_matrix(utils.string_to_graph(graph_name)) pickled_adj_mat = pickle.dumps(adj_mat) # results['0': {'p_success':, 'f_time', 'classification': }] results = ndsmoran.MoranNodeSimulation(graph_name, utils.FITNESS, utils.NUMBER_OF_RUNS).node_run() list_of_nodes = [] for node, target_vec in results.items(): node_row = {"node": node, **target_vec} list_of_nodes.append(node_row) df = pd.DataFrame(list_of_nodes) # check this line below df.to_csv('data/node_targets/%s.nvec' % graph_name, sep=' ',index=False) # write nodetargetvecs to csv CHECK # pickle and store adjacency CHECK # make sure we keep node order consistent with open(file_name) as csvfile: node_reader = csv.reader(csvfile, delimiter = ' ') num_nodes, rep_dim = next(node_reader) num_nodes = int(num_nodes) num_edges = len(utils.string_to_graph(graph_name).edges()) rep_dim = int(rep_dim) node_vectors = {} for node_vec in node_reader: node_vectors[node_vec[0]] = {'feature_vec': list(map(lambda x: float(x), node_vec[1:])), 'target_vec': results[node_vec[0]]} utils.NODE_DATA_FINAL.update_one({"graph_name": graph_name}, {"$set": {"node_vectors": node_vectors, "rep_dim": rep_dim, "num_nodes": num_nodes, 'num_edges': num_edges, 'f_val': utils.FITNESS, 'num_runs': utils.NUMBER_OF_RUNS, "adj_matrix": pickled_adj_mat}},upsert=True) with v_lock: v.value += 1 print("Graph %s is uploaded" % v.value) print("%s finished" % mp.current_process().name)