def build_A_tables_for_lanes(sumo_network, lanes=None): """Returns dict of dataframes for different lane adjacency matrices""" if lanes is None: lanes = sumo_network.lanes_with_detectors() A_dfs = {} A_dfs['A_downstream'] = nx.to_pandas_adjacency( sumo_network.graph, nodelist=lanes).astype('bool') A_dfs['A_upstream'] = nx.to_pandas_adjacency(sumo_network.graph.reverse(), nodelist=lanes).astype('bool') A_dfs['A_neighbors'] = nx.to_pandas_adjacency( sumo_network.get_lane_graph_for_neighboring_lanes( include_self_adjacency=False), nodelist=lanes).astype('bool') A_dfs['A_turn_movements'] = nx.to_pandas_adjacency( sumo_network.get_lane_graph_for_turn_movements(), nodelist=lanes).astype('bool') A_dfs['A_through_movements'] = nx.to_pandas_adjacency( sumo_network.get_lane_graph_for_thru_movements(), nodelist=lanes).astype('bool') return A_dfs
def compare_interactions_df(G, node_a=str, node_b=str, method="ratio"): import networkx as nx import pandas as pd import numpy as np Gs = split_graph(G) interaction_dict_i = dict() for node in G.nodes(): ratio_dict = dict() for interaction in Gs: if method == "ratio": df = nx.to_pandas_adjacency(Gs[interaction], weight="log_score") a = df[node_a][node] b = df[node_b][node] ratio = a - b # calculate the ratio of interaction scores (using log values log(a)-log(b) = log(a/b)) # This bypass divition with 0 issues as in a/b, b might be 0 if method == "difference": df = nx.to_pandas_adjacency(Gs[interaction], weight="score") a = df[node_a][node] b = df[node_b][node] ratio = a - b # Calulate the difference in interaction score if method == "specificity": df = nx.to_pandas_adjacency(Gs[interaction], weight="specificity") a = df[node_a][node] b = df[node_b][node] ratio = a - b # Calulate the difference in specificity ratio_dict[interaction] = ratio interaction_dict_i[node] = ratio_dict df_i = pd.DataFrame(interaction_dict_i) interaction_dict_o = dict() for node in G.nodes(): ratio_dict = dict() for interaction in Gs: if method == "ratio": df = nx.to_pandas_adjacency(Gs[interaction], weight="log_score") a = df[node][node_a] b = df[node][node_b] ratio = a - b # calculate the ratio of interaction scores (using log values log(a)-log(b) = log(a/b)) # This bypass divition with 0 issues as in a/b, b might be 0 if method == "difference": df = nx.to_pandas_adjacency(Gs[interaction], weight="score") a = df[node][node_a] b = df[node][node_b] ratio = a - b # Calulate the difference in interaction score ratio_dict[interaction] = ratio interaction_dict_o[node] = ratio_dict df_o = pd.DataFrame(interaction_dict_o) return df_i, df_o
def test_normalize_graph(self): matrix = pd.DataFrame([[0,4,2,0,0],[1,0,2,0,0],[1,0,0,0,0], [0,0,1,0,5],[0,0,0,5,0]]) graph = nx.from_pandas_adjacency(matrix, create_using= nx.DiGraph) print(matrix) print(nx.to_pandas_adjacency(graph)) calculate_total_node_volume(graph) normalize_directional_graph(graph) print(nx.to_pandas_adjacency(graph))
def load_everything( graph_type, version="2019-09-18-v2", return_keys=None, return_df=False, return_ids=False, ): """Function to load an adjacency matrix and optionally return some associated metadata Parameters ---------- graph_type : str Which version of the graph to load version : str, optional Date/version descriptor for which dataset to use, by default "2019-09-18-v2" return_df : bool, optional Whether to return a Pandas DataFrame representation of the adjacency, by default False return_ids : bool, optional Whether to return the cell ids (skeleton ids), by default False Returns ------- [type] [description] Raises ------ ValueError [description] """ graph = load_networkx(graph_type, version=version) nx_ids = np.array(list(graph.nodes()), dtype=int) df_adj = nx.to_pandas_adjacency(graph) df_ids = df_adj.index.values.astype(int) if not np.array_equal(nx_ids, df_ids): raise ValueError( "Networkx indexing is inconsistent with Pandas adjacency") adj = nx.to_pandas_adjacency(graph).values outs = [adj] if return_keys is not None: if not isinstance(return_keys, list): return_keys = [return_keys] for k in return_keys: labels = meta_to_array(graph, k) outs.append(labels) if return_df: outs.append(df_adj) if return_ids: outs.append(df_ids) if len(outs) > 1: outs = tuple(outs) return outs else: return adj
def getGraphFromFile(file_path): file = open(file_path, "r") graph = nx.Graph() content = file.read() # dictionary with coordinates of nodes coords = {} cities = [] # # turninig on options to see all information in DataFrame #pd.set_option('display.max_rows', None) #pd.set_option('display.max_columns', None) # extract node section node_section = content[content.find("NODES"):content.find("LINK")] # extract nodes only node_section = node_section[node_section.find("(") + 1:node_section.rfind(")")] # split into single nodes nodes = node_section.split('\n')[1:-1] for node in nodes: # get list with name on 0 index and two coordinates node = node.replace('(', '').replace(')', '').split(' ')[2:-1] node.pop(1) cities.append(node[0]) coords[node[0]] = {"x": node[1], "y": node[2]} graph.add_node(node[0]) # extract link section link_section = content[content.find("LINKS"):content.find("DEMAND")] link_section = link_section[link_section.find("(") + 1:link_section.rfind(")")] links = link_section.split('\n')[1:-1] #print(links) for link in links: link = link[link.find("(") + 1:link.find(")")].split(' ')[1:-1] lon1 = float(coords[link[0]]["x"]) # lon lat1 = float(coords[link[0]]["y"]) # lat lon2 = float(coords[link[1]]["x"]) lat2 = float(coords[link[1]]["y"]) edge_real_distance = geodesic((lat1, lon1), (lat2, lon2)).kilometers graph.add_edge(link[0], link[1]) graph[link[0]][link[1]]['weight'] = edge_real_distance graph[link[0]][link[1]]['pheromone'] = 1 # # distance dataframe df = nx.to_pandas_adjacency(graph, weight='weight', nonedge=np.inf) # beginnine pheromone amount for each city in dataframe pheromone = nx.to_pandas_adjacency(graph, weight='pheromone', nonedge=0) # # beginning eta amount for each citi in dataframe eta = 1 / df return graph, df, pheromone, eta, cities
def test_from_adjacency(self): nodelist = [1, 2] dftrue = pd.DataFrame([[1, 1], [1, 0]], dtype=int, index=nodelist, columns=nodelist) G = nx.Graph([(1, 1), (1, 2)]) df = nx.to_pandas_adjacency(G, dtype=int) pd.testing.assert_frame_equal(df, dftrue)
def prunedDF(dataFrame, saveAsGraph, file=''): if isinstance(dataFrame, pd.DataFrame): graph = nx.from_pandas_adjacency(dataFrame) graph.remove_nodes_from(list(nx.isolates(graph))) if saveAsGraph: saveGraph(graph, file) return nx.to_pandas_adjacency(graph)
def PPICompute(graph,proteinNodes,trueP,falseP): #print('PPI - starting subgraph') subPROTEIN = graph.subgraph(proteinNodes) # did the PPI filter out some proteins? #subPROTEIN = subPROTEIN.subgraph( set(subPROTEIN.nodes) - set(nx.isolates(subPROTEIN)) ) trues = set() neighborSet = set() #print('PPI - making neighborSet') for protein in trueP: if protein in set(proteinNodes): trues.add(protein) neighborSet = neighborSet | set(nx.all_neighbors(subPROTEIN, protein)) nodesList = trues | neighborSet finalGraph = subPROTEIN.subgraph(nodesList) #print('PPI - making adj subgraph') # this is slow adjIt = nx.to_pandas_adjacency(finalGraph,weight='combined_score') # this does our PPI computations with the values we want #print('PPI - matrix and loop') RR = completePPI(finalGraph,trues,nodesList,adjIt) #print('PPI - done with these computations') return RR
def calculatePhi(graph, k): path = 'graphs/' file = [graph] edgelist = pd.read_csv(path + file[0], delimiter=' ', skiprows=1, header=None) G = nx.from_pandas_edgelist(edgelist, source=0, target=1, create_using=nx.Graph()) adj_matr = nx.to_pandas_adjacency(G, dtype=np.float64) laplacian = sparse.csgraph.laplacian(adj_matr.values, normed=True) laplacian = normalize(laplacian) eigenvalues, eigenvectors = np.linalg.eig(laplacian) kmeans = KMeans(n_clusters=k, max_iter=3000).fit(eigenvectors.astype(np.float64)) cut_edges = 0 for i in range(edgelist.shape[0]): if (kmeans.labels_[edgelist[0][i]] != kmeans.labels_[edgelist[1][i]]): cut_edges += 1 counter = collections.Counter(kmeans.labels_) smallest = math.inf for key, value in counter.items(): if (value < smallest): smallest = value phi = cut_edges / smallest return phi
def get_adjacency(self, *, weight_col=None, norm_type=None): """ Creates edge graph in the matrix format. Row indeces are event_col values, from which the transition occured, and columns are events, to which the transition occured. The values are weights of the edges defined with weight_col and norm_type parameters. Parameters ---------- weight_col: str (optional, default=None) Aggregation column for transitions weighting. To calculate weights as number of transion events use None. To calculate number of unique users passed through given transition 'user_id'. For any other aggreagtion, like number of sessions, pass the column name. norm_type: {None, 'full', 'node'} (optional, default=None) Type of normalization. If None return raw number of transtions or other selected aggregation column. 'full' - normalized over entire dataset. 'node' weight for edge A --> B normalized over user in A Returns ------- Dataframe with number of columns and rows equal to unique number of event_col values. Return type ----------- pd.DataFrame """ agg = self.get_edgelist(weight_col=weight_col, norm_type=norm_type) graph = nx.DiGraph() graph.add_weighted_edges_from(agg.values) return nx.to_pandas_adjacency(graph)
def step(self): # collect knowledge, attitude, and belief valence data # loop through each agent and only store agent info if necessary for i in range(len(self.all_agents)): a = self.all_agents[i] # for sensitivity analysis, only collect knowledge information # collect knowledge flows if ((a.timeKnowledge == a.currentStep - 1) and (np.isnan(a.receivedKnowledge) == False)) or ( (a.timeKnowledge == 0) and (a.currentStep == 0)): kn = { 'CurrentStep': a.currentStep, 'MyAgentID': a.agent_number, 'OtherAgentID': a.receivedKnowledge, 'TimeKnowledgeReceived': a.timeKnowledge, 'HasKnowledge': a.hasKnowledge, 'Model': self.cognitivePhase, 'NumInitialKnowledgeAgents': self.numRandomKnowledgeAgents, 'NumberAgentsSeeded': self.numSeeds, 'SuccessProbability': self.probSuccess, 'RateOfLogisticCurve': self.a, 'SensitivityAnalysis': self.sensitivityAnalysis, 'Subgraph': self.subgraph } kn = pd.DataFrame(data=kn, index=np.arange(0, 1)) self.knowledge_table = self.knowledge_table.append(kn) if self.sensitivityAnalysis == False: # collect attitude values if agent's attitude has changed if ((a.timeNewAttitude == a.currentStep - 1) or (a.currentStep == 0)): at = { 'CurrentStep': a.currentStep, 'TimeNewAttitude': a.timeNewAttitude, 'MyAgentID': a.agent_number, 'Attitude': a.attitude } at = pd.DataFrame(data=at, index=np.arange(0, 1)) self.attitude_table = self.attitude_table.append(at) # collect contact network, social network, and interaction times if self.step_number % 1440 == 0: print("current step: ", self.step_number) if self.networkPhase != 1: # create table with social network #sn=nx.to_pandas_dataframe(self.socialNetwork) sn = nx.to_pandas_adjacency(self.socialNetwork) sn['Step'] = self.step_number sn['contactWeight'] = self.beta1 sn['attWeight'] = self.beta2 sn['roleWeight'] = self.beta3 self.social_table = self.social_table.append(sn) self.step_number = self.step_number + 1 self.schedule.step()
def BuildAM(par, t, data_type): k = par["k"] path = par["path"] os.chdir(path) if(t == "cg"): G = nx.complete_graph(10) am = nx.to_pandas_adjacency(G) elif(t == "half"): am = pd.read_table("C:\\Users\\Administrator\\Desktop\\causal_compare\\data_e\\inter_" + str(k) + ".txt", header=None) am = CreatPesudoAM(am) elif(t == "mmhc"): am = pd.read_csv("C:\\Users\\Administrator\\Desktop\\causal_compare\\beem_e\\" + par["i"] + "_" + par["j"] + "\\mmhc\\"+ data_type+ "_adjacent_matrix_" + str(k) + ".csv", index_col=0) elif(t == "sparcc"): p = pd.read_table("C:\\Users\\Administrator\\Desktop\\causal_compare\\data_e\\" + par["i"] + "_" + par["j"] + "\\SparCC\\p_" + str(k) + ".txt", index_col=0) am = pd.DataFrame(np.zeros([p.shape[0], p.shape[0]])) am.columns = ["sp" + str(i) for i in range(p.shape[0])] am.index = ["sp" + str(i) for i in range(p.shape[0])] for s in range(p.shape[0]): for t in range(p.shape[1]): if(p.ix[s, t] <= 0.05): am.ix[s, t] = 1 elif(t == "true"): am = pd.read_table("C:\\Users\\Administrator\\Desktop\\causal_compare\\data_e\\inter_" + str(k) + ".txt", header=None) label = ["sp" + str(i) for i in range(len(am))] am.index = label; am.columns = label #am = pd.DataFrame(am, index=label, columns=label) return am
def test_roundtrip(self): # edgelist Gtrue = nx.Graph([(1, 1), (1, 2)]) df = nx.to_pandas_edgelist(Gtrue) G = nx.from_pandas_edgelist(df) assert_graphs_equal(Gtrue, G) # adjacency Gtrue = nx.Graph(({ 1: { 1: { 'weight': 1 }, 2: { 'weight': 1 } }, 2: { 1: { 'weight': 1 } } })) df = nx.to_pandas_adjacency(Gtrue, dtype=int) G = nx.from_pandas_adjacency(df) assert_graphs_equal(Gtrue, G)
def Crear_Grafos(nombre, size, int_distancia): G = nx.Graph() nodes = [] for i in range(size): nodes.append(i) print(nodes) for i in nodes: idx = nodes.index(i) + 1 print(idx) for j in nodes[idx:len(nodes)]: if rnd.randint(0, 80) == 1: G.add_edge(i, j, distancia=rnd.randint(1, int_distancia)) print(G.edges) df = pd.DataFrame() df = nx.to_pandas_adjacency(G, dtype=int, weight='distancia') df.to_csv(nombre + ".csv", index=None, header=None) plt.figure(figsize=(15, 15)) position = nx.spring_layout(G, scale=5, iterations=200) nx.draw_networkx_nodes(G, position, node_size=50, node_color="#de8919", node_shape="<") nx.draw_networkx_edges(G, position, width=0.5, edge_color='black') plt.axis("off") plt.savefig(nombre + ".png", bbox_inches='tight') plt.savefig(nombre + ".eps", bbox_inches='tight') plt.show(G)
def convert_to_csv(graph, c_obj, c_nodes, output_path=config.path, adj_mat=False): if adj_mat: df_matrix = nx.to_pandas_adjacency(graph, dtype=np.uint8) df_matrix.to_csv(output_path + 'Adj_matrix_bbike_map.csv', index=False) adj_list = list(nx.generate_adjlist(graph)) df_list = pd.DataFrame(adj_list, columns=['row']) df_list = pd.DataFrame(df_list.row.str.split(' ').tolist()) df_list = df_list.rename(columns={0: 'Source'}) df_list.to_csv(output_path + 'Adj_list_bbike_map.csv', index=False) df_nodes = pd.DataFrame(dict(graph.nodes(data=True))).T df_nodes.to_csv(output_path + 'All.csv', index=False) to_dict_nodes = df_nodes.loc[df_nodes['osmid'].isin(c_nodes)] to_dict_nodes.to_csv(output_path + 'Nodes_bbike_map.csv', index=False) to_dict_objects = df_nodes.loc[df_nodes['osmid'].isin(c_obj)] to_dict_objects.to_csv(output_path + 'Objects_bbike_map.csv', index=False) adj_list = pd.read_csv(output_path + 'Adj_list_bbike_map.csv') df_graph = df_nodes.merge(adj_list, left_on='osmid', right_on='Source') adj = df_graph.iloc[:, 6:].to_numpy() adj = [node[~pd.isnull(node)] for node in adj] df_graph['adj'] = adj df_graph = df_graph[['osmid', 'x', 'y', 'weight', 'adj']].set_index('osmid') with open(output_path + 'Chel_simp.pickle', 'wb') as f: pickle.dump(graph, f) return df_graph
def from_json( self, jsonstr, func=nf.MJ_func, # default mechanism for all nodes SpN=2): self.graph = self.node_lut = self.tpm = None jdict = json.loads(jsonstr) edges = jdict.get('edges', []) i, j = zip(*edges) n_list = sorted(set(i + j)) nodes = [Node(**nd) for nd in jdict.get('nodes', [])] for n in nodes: n.func = nf.funcLUT[n.func] self.graph = nx.DiGraph(edges) self.node_lut = dict((n.label, n) for n in nodes) S = nx.DiGraph(jdict.get('tpm', [])) #self.tpm = sbs2sbn(nx.to_pandas_adjacency(S)) s2s_df = nx.to_pandas_adjacency(S) df = pd.DataFrame(index=s2s_df.index, columns=self.node_lut.keys()) for istate in s2s_df.index: for outstate in s2s_df.columns: if s2s_df.loc[istate, outstate] == 0: continue ns = nodes_state(outstate, self.node_lut.keys()) print(f'DBG: istate={istate} ns={ns}') df.loc[istate] = list(ns.values()) self.tpm = df
def graph_contrast_heatmap(G, H): fig, ax = plt.subplots(figsize=(10, 10)) # same network, return empty plot if G.edges == H.edges: return plt # grab set of nodes from all graphs nodelist = set() for g in [ G, H, ]: for node in g.nodes: nodelist.add(node) g = nx.to_pandas_adjacency(G, nodelist=nodelist) h = nx.to_pandas_adjacency(H, nodelist=nodelist) h.replace(1, 2, inplace=True) a = g + h # drop zeros from resulting dataframe a = a.loc[(a != 0).any(1)] a = a.loc[:, (a != 0).any(axis=0)] ax = seaborn.heatmap(a, cbar=False, square=True, linewidths=1.5, cmap=['white', 'red', 'blue', 'purple']) ax.set_xticklabels(labels=list(a.columns), rotation=45, rotation_mode="anchor", ha="right", fontsize=7) ax.set_yticklabels(labels=list(a.index), fontsize=7) plt.xlabel("target") plt.ylabel("source") fig.tight_layout() return plt
def wrapper(self, *args, **kwargs): before = nx.to_pandas_adjacency(self._dg) func(self, *args, **kwargs) after = nx.to_pandas_adjacency(self._dg) idx_union = before.index.union(after.index) before = before.reindex(idx_union) after = after.reindex(idx_union) diff = after.subtract(before) # Find symmetric difference. This is what I would use to do CRUD on relational db. df = diff.unstack().to_frame() df.index = df.index.rename(['child', 'parent']) df.columns = ['diff'] df = df[df['diff'] != 0] df['changed'] = df['diff'].map(lambda x: 'Added' if x > 0 else 'Removed') print('Symmetric diff:\n', df)
def na(): pr_sorted = sorted(pr.items(), key=operator.itemgetter(1), reverse=True) # test graph edges = {"to": [0, 1, 2, 2], "from": [2, 0, 1, 1]} df = pd.DataFrame(edges) G = nx.from_pandas_edgelist(df, "to", "from", create_using=nx.MultiDiGraph) print(nx.to_pandas_adjacency(G))
def build_adjacency_matrix_from_graph(graph, attribute_name, all_nodes=None, distribution=False): """ Building a adjacency matrix for a condensed graph. Args: graph: the condensed graph that is described by the produced adjacency matrix. attribute_name: the name of the attribute to use as names in the matrix all_nodes: list of nodes to be considered in the adjacency matrix, should be according to the requested attribute. distribution: if false all the values are {0,1}, else the number number of adjacent nodes normalized. Returns: the adjacency matrix. """ AdjacencyGraphBuilder._validate_attribute_is_unique( graph, attribute_name) graph = graph.copy() # The condensed nodes attributes. node_attribute_value = nx.get_node_attributes(graph, attribute_name) # The index / columns of the adjacency matrix will be returned with the attribute value. rename_dict = {k: v.value for k, v in node_attribute_value.items()} existing_nodes = rename_dict.values() if all_nodes is not None: for node in set(all_nodes) - set(existing_nodes): node_name = 'dummy {}'.format(node) graph.add_node(node_name, **{attribute_name: node}) rename_dict[node_name] = node # The adjacency matrix of the condensed graph. condensed_graph_df = nx.to_pandas_adjacency(graph) # Renaming the index/columns to the appropriate name. condensed_graph_df = condensed_graph_df.rename(columns=rename_dict, index=rename_dict) # Condensing the matrix. condensed_graph_df = condensed_graph_df.groupby( condensed_graph_df.index).sum().T condensed_graph_df = condensed_graph_df.groupby( condensed_graph_df.index).sum() # If a distribution is desired then the number of adjacent nodes is normalized if distribution: condensed_graph_df = (condensed_graph_df / condensed_graph_df.sum()).fillna(0) # if not it is updated to {0/1} else: condensed_graph_df = (condensed_graph_df / condensed_graph_df).fillna(0) return condensed_graph_df
def test_to_pandas_adjacency_with_nodelist(): G = nx.complete_graph(5) nodelist = [1, 4] expected = pd.DataFrame([[0, 1], [1, 0]], dtype=int, index=nodelist, columns=nodelist) pd.testing.assert_frame_equal( expected, nx.to_pandas_adjacency(G, nodelist, dtype=int))
def test_from_adjacency(self): nodelist = [1, 2] dftrue = pd.DataFrame([[1, 1], [1, 0]], dtype=int, index=nodelist, columns=nodelist) G = nx.Graph([(1, 1), (1, 2)]) df = nx.to_pandas_adjacency(G, dtype=int) pd.testing.assert_frame_equal(df, dftrue) # deprecated df = nx.to_pandas_dataframe(G, dtype=int) pd.testing.assert_frame_equal(df, dftrue)
def test_from_adjacency_named(self): # example from issue #3105 data = {"A": {"A": 0, "B": 0, "C": 0}, "B": {"A": 1, "B": 0, "C": 0}, "C": {"A": 0, "B": 1, "C": 0}} dftrue = pd.DataFrame(data) df = dftrue[["A", "C", "B"]] G = nx.from_pandas_adjacency(df, create_using=nx.DiGraph()) df = nx.to_pandas_adjacency(G, dtype=int) pd.testing.assert_frame_equal(df, dftrue)
def clip_to_table(self, clip, nodelist=None): """ This method recieves a networkx graph and return its dataframe """ if nodelist == None: df_nodelist = sorted(clip.nodes()) Table_clip = nx.to_pandas_adjacency(clip, nodelist=df_nodelist) return Table_clip
def test_roundtrip(self): # edgelist Gtrue = nx.Graph([(1, 1), (1, 2)]) df = nx.to_pandas_edgelist(Gtrue) G = nx.from_pandas_edgelist(df) assert_graphs_equal(Gtrue, G) # adjacency Gtrue = nx.Graph(({1: {1: {'weight': 1}, 2: {'weight': 1}}, 2: {1: {'weight': 1}}})) df = nx.to_pandas_adjacency(Gtrue, dtype=int) G = nx.from_pandas_adjacency(df) assert_graphs_equal(Gtrue, G)
def load_adjacency(graph_type="G", nodelist=None, output="numpy", path=None, version=None): g = load_networkx(graph_type=graph_type, path=path, version=version) if output == "numpy": adj = nx.to_numpy_array(g, nodelist=nodelist) elif output == "pandas": adj = nx.to_pandas_adjacency(g, nodelist=nodelist) return adj
def GenerateGraph(nameToSave,Smin,Smax,max_weight,numberOfGraphs): for h in range(numberOfGraphs): G=nx.Graph() size=rdm.randint(Smin,Smax) for i in range(size): for j in range(i, size): if rdm.randint(0,int(size/20))==0: G.add_edge(i,j, weight=rdm.randint(1,max_weight)) df = pd.DataFrame() df = nx.to_pandas_adjacency(G, dtype=int, weight='weight') df.to_csv(nameToSave+str(h)+".csv")
def occurence(data,document): #生成共现矩阵 empty1=[];empty2=[];empty3=[] for a in data: for b in data: count = 0 for x in document: if [a in i for i in x].count(True) >0 and [b in i for i in x].count(True) >0: count += 1 empty1.append(a);empty2.append(b);empty3.append(count) df=pd.DataFrame({'from':empty1,'to':empty2,'weight':empty3}) G=nx.from_pandas_edgelist(df, 'from', 'to', 'weight') return (nx.to_pandas_adjacency(G, dtype=int))
def test_roundtrip(self, graph): # edgelist Gtrue = graph([(1, 1), (1, 2)]) df = nx.to_pandas_edgelist(Gtrue) G = nx.from_pandas_edgelist(df, create_using=graph) assert_graphs_equal(Gtrue, G) # adjacency adj = {1: {1: {"weight": 1}, 2: {"weight": 1}}, 2: {1: {"weight": 1}}} Gtrue = graph(adj) df = nx.to_pandas_adjacency(Gtrue, dtype=int) G = nx.from_pandas_adjacency(df, create_using=graph) assert_graphs_equal(Gtrue, G)
def make_directed_graph(nodes, edges, normalize=True): #nodes,edges = make_traded_volume_matrix(input_path, cutoff) graph = nx.DiGraph() #Rows are the active agent graph.add_nodes_from(nodes) graph.add_weighted_edges_from(edges) if normalize == True: adjacency_df = nx.to_pandas_adjacency(graph) graph = nx.from_pandas_adjacency( adjacency_df.div(adjacency_df.sum(axis=1), axis=0).fillna( 0)) # We can end up dividing with zero so we need to fill that return graph
def networks(corr_list, indexname, stockyear, show_networks=False): """ Creates the correlation network from correlation matrices. :param corr_list: List of correlation matrices from incremented data :param indexname: Name of index being referenced (labeling purposes) :param stockyear: Start year of data being used (labeling purposes) :param show_networks: Indicates whether to display the correlation networks The pseudocode goes as follows: for each correlation matrix: links = pd.DataFrame(var1, var2, value) links(var1,var2) = stock1, stock2 links(value) = correlation links_filtered = links[correlation above predetermined threshold] G = graph(edges from links_filtered) if show_networks: display network :Returns: none Saves adjacency matrices to external files (for later use) Optionally displays the graphs using networkx """ for i in range(len(corr_list)): # creates the filtered links for each correlation set, threshold at 0.5 somewhat arbitrarily corr_df = corr_list[i] links = corr_df.stack().reset_index() links.columns = ['var1', 'var2', 'value'] mean = np.mean(links['value']) std = np.std(links['value']) links_filtered = links.loc[(links['value'] > mean - std) & (links['var1'] != links['var2'])] G = nx.from_pandas_edgelist(links_filtered, 'var1', 'var2') G_df = nx.to_pandas_adjacency(G) G_df.to_csv('Graphs/{}_{}/adjacency_matrix/range_{}.csv'.format( indexname, stockyear, i)) if show_networks: plt.figure(i) nx.draw_spring(G, with_labels=True, node_color='orange', node_size=400, edge_color='black', linewidths=1, font_size=9) if show_networks: plt.show()
def graph_contrast_report(G, H): # same network, return empty plot if G.edges == H.edges: return "same graph" # grab set of nodes from all graphs nodelist = set() for g in [ G, H, ]: for node in g.nodes: nodelist.add(node) g = nx.to_pandas_adjacency(G, nodelist=nodelist) h = nx.to_pandas_adjacency(H, nodelist=nodelist) h.replace(1, 2, inplace=True) a = g + h # drop zeros from resulting dataframe a = a.loc[(a != 0).any(1)] a = a.loc[:, (a != 0).any(axis=0)] no_change = 0 deletion = 0 insertion = 0 purple = 0 for r in a.to_numpy(): row = list(r) no_change += row.count(0) deletion += row.count(1) insertion += row.count(2) purple += row.count(3) return (no_change, deletion, insertion, purple)