How to add new node attributes to an already created stellargraph graph?

83 Views Asked by At

I have a graph

StellarGraph: Undirected multigraph
 Nodes: 4, Edges: 10

 Node types:
  default: [4]
    Features: float32 vector, length 2
    Edge types: default-default->default

 Edge types:
    default-default->default: [10]
        Weights: all 1 (default)
        Features: float32 vector, length 2
StellarGraph: Undirected multigraph
 Nodes: 4, Edges: 10

I want to add a new node attributes from an array new_feature=[2,3,5,6] (i.e 2 for node 1, 3 for node 2 and so on). How to add it to already exiting stellargraph graph?

Is there a direct function like in networkx?

1

There are 1 best solutions below

0
On BEST ANSWER

I think it's not recommended but you can modify your Graph manually:

Input info:

>>> print(G.info())

StellarGraph: Undirected multigraph
 Nodes: 4, Edges: 10

 Node types:
  default: [4]
    Features: float32 vector, length 2  # <- 2 features
    Edge types: default-default->default

 Edge types:
    default-default->default: [10]
        Weights: all 1 (default)
        Features: none

Modify the default type:

new_feature = np.array([2, 3, 5, 6]).reshape(-1, 1)
G._nodes._features['default'] = np.hstack([G._nodes._features['default'], new_feature])

Output info:

>>> print(G.info())

StellarGraph: Undirected multigraph
 Nodes: 4, Edges: 10

 Node types:
  default: [4]
    Features: float64 vector, length 3  # <- 3 features now
    Edge types: default-default->default

 Edge types:
    default-default->default: [10]
        Weights: all 1 (default)
        Features: none

>>> G.node_features()
array([[ 1.        , -0.2       ,  2.        ],
       [ 2.        ,  0.30000001,  3.        ],
       [ 3.        ,  0.        ,  5.        ],
       [ 4.        , -0.5       ,  6.        ]])