def test_to_numpy_recarray(self): G=nx.Graph() G.add_edge(1,2,weight=7.0,cost=5) A=nx.to_numpy_recarray(G,dtype=[('weight',float),('cost',int)]) assert_equal(sorted(A.dtype.names),['cost','weight']) assert_equal(A.weight[0,1],7.0) assert_equal(A.weight[0,0],0.0) assert_equal(A.cost[0,1],5) assert_equal(A.cost[0,0],0)
def test_to_numpy_recarray(self): G = nx.Graph() G.add_edge(1, 2, weight=7.0, cost=5) A = nx.to_numpy_recarray(G, dtype=[('weight', float), ('cost', int)]) assert_equal(sorted(A.dtype.names), ['cost', 'weight']) assert_equal(A.weight[0, 1], 7.0) assert_equal(A.weight[0, 0], 0.0) assert_equal(A.cost[0, 1], 5) assert_equal(A.cost[0, 0], 0)
def test_to_numpy_recarray(self): G = nx.Graph() G.add_edge(1, 2, weight=7.0, cost=5) A = nx.to_numpy_recarray(G, dtype=[("weight", float), ("cost", int)]) assert sorted(A.dtype.names) == ["cost", "weight"] assert A.weight[0, 1] == 7.0 assert A.weight[0, 0] == 0.0 assert A.cost[0, 1] == 5 assert A.cost[0, 0] == 0
def test_to_numpy_recarray(recarray_test_graph): A = nx.to_numpy_recarray(recarray_test_graph, dtype=[("weight", float), ("cost", int)]) assert sorted(A.dtype.names) == ["cost", "weight"] assert A.weight[0, 1] == 7.0 assert A.weight[0, 0] == 0.0 assert A.cost[0, 1] == 5 assert A.cost[0, 0] == 0 with pytest.raises(AttributeError, match="has no attribute"): A.color[0, 1]
def _get_matdict(self): if not self.loaded: self.load() import networkx as nx matdict = {} g = self._relabel_to_int(self.graph) # grab keys from the first edge, discarding id dl = self._get_edge_values() # create numpy matrix for each key using recarray matrec = nx.to_numpy_recarray(g, dtype=zip(dl, [float]*len(dl)) ) for k in dl: matdict[k] = matrec[k] return matdict
def nx_to_weighted_array(self, G): labels=G.nodes() matrix=nx.to_numpy_recarray(G) distances=numpy.zeros((len(G),len(G))) i=0 for x in matrix: j=0 for y in x: distances[i][j]=y[0] distances[j][i]=y[0] if i==j: distances[i][j]=0 j+=1 i+=1 return distances
def test_to_numpy_recarray_bad_nodelist(recarray_nodelist_test_graph, nodelist, errmsg): with pytest.raises(nx.NetworkXError, match=errmsg): A = nx.to_numpy_recarray(recarray_nodelist_test_graph, nodelist=nodelist)
def test_to_numpy_recarray_nodelist(recarray_nodelist_test_graph): A = nx.to_numpy_recarray(recarray_nodelist_test_graph, nodelist=[0, 1]) np.testing.assert_array_equal(A.weight, np.array([[0, 1], [1, 0]]))
def test_to_numpy_recarray_directed(recarray_test_graph): G = recarray_test_graph.to_directed() G.remove_edge(2, 1) A = nx.to_numpy_recarray(G, dtype=[("weight", float), ("cost", int)]) np.testing.assert_array_equal(A.weight, np.array([[0, 7.0], [0, 0]])) np.testing.assert_array_equal(A.cost, np.array([[0, 5], [0, 0]]))
def __init__( self, num_nodes=None, # Number of nodes num_items=None, # Number of items item_distribution='uniform', # Different item distributions # 1. uniform # 2. direct # 3. inverse # 4. ego # 5. custom initial_item_distribution=None, # Initial item distribution for custom ego_graph_radius=1, # Radius of the ego graph current_time=1, # Total time G=None): # Networkx DiGraph such that: # 1. Each node has an attribute 'num_items' # 2. Each edge has an attribute 'weight' self.num_nodes = num_nodes self.num_items = num_items self.current_time = current_time self.item_distribution = item_distribution self.ego_graph_radius = ego_graph_radius # self.initial_transition_matrix = np.array([[ 0., 1., 0. ], # [ 0.21128204, 0., 0.78871796], # [ 0., 1., 0. ]] # self.initial_item_distribution = {0: 1, 1:1, 2:1} # self.G = nx.from_numpy_matrix(self.initial_transition_matrix, create_using=nx.DiGraph()) # nx.set_node_attributes(self.G, 'num_items', self.initial_item_distribution) if G is None: # No networkx graph provided while True: try: self.initial_transition_matrix = self.initialize_transition_matrix( ) except: continue break self.G = nx.from_numpy_matrix(self.initial_transition_matrix, create_using=nx.DiGraph()) self.initial_item_distribution = self.initialize_item_distribution( initial_item_distribution) nx.set_node_attributes(self.G, 'num_items', self.initial_item_distribution) else: self.G = G self.initial_transition_matrix = nx.to_numpy_recarray( self.G, dtype=[('weight', float)]).weight # Test that resultant transition matrix is row stochastic absdiff = np.abs( self.initial_transition_matrix.sum(axis=1)) - np.ones( (self.num_nodes)) assert absdiff.max() <= 10 * np.spacing( np.float64(1)), "Not row stochastic" # self.initial_item_distribution = nx.get_node_attributes(self.G, 'num_items') self.initial_item_distribution = self.initialize_item_distribution( initial_item_distribution) nx.set_node_attributes(self.G, 'num_items', self.initial_item_distribution)