graphorge.gnn_base_model.data.graph_data.GraphData

class GraphData(n_dim, nodes_coords)[source]

Bases: object

Graph Neural Network graph data.

_n_dim

Number of spatial dimensions.

Type:

int

_nodes_coords

Coordinates of nodes stored as a numpy.ndarray(2d) with shape (n_nodes, n_dim). Coordinates of i-th node are stored in nodes_coords[i, :].

Type:

numpy.ndarray(2d)

_n_node

Number of nodes.

Type:

int

_n_edge

Number of edges.

Type:

int

_node_features_matrix

Nodes input features matrix stored as a numpy.ndarray(2d) of shape (n_nodes, n_features).

Type:

numpy.ndarray(2d)

_edge_features_matrix

Edges input features matrix stored as a numpy.ndarray(2d) of shape (n_edges, n_features).

Type:

numpy.ndarray(2d)

_global_features_matrix

Global input features matrix stored as a numpy.ndarray(2d) of shape (1, n_features).

Type:

numpy.ndarray(2d)

_node_targets_matrix

Nodes targets matrix stored as a numpy.ndarray(2d) of shape (n_nodes, n_targets).

Type:

numpy.ndarray(2d)

_edge_targets_matrix

Edges targets matrix stored as a numpy.ndarray(2d) of shape (n_nodes, n_targets).

Type:

numpy.ndarray(2d)

_global_targets_matrix

Global targets matrix stored as a numpy.ndarray(2d) of shape (1, n_targets).

Type:

numpy.ndarray(2d)

_edges_indexes

Edges indexes matrix stored as numpy.ndarray[int](2d) with shape (n_edges, 2), where the i-th edge is stored in edges_indexes[i, :] as (start_node_index, end_node_index).

Type:

numpy.ndarray(2d)

_metadata

A dictionary containing any metadata information.

Type:

dict

get_torch_data_object(self)[source]

Get PyG homogeneous graph data object.

get_nodes_coords(self)[source]

Get graph node coordinates.

get_n_node(self)[source]

Get graph number of nodes.

get_n_edge(self)[source]

Get graph number of edges.

set_graph_edges_indexes(self, connect_radius=None, edges_indexes_mesh=None, is_unique=True)[source]

Set graph edges indexes.

get_graph_edges_indexes(self)[source]

Get graph edges indexes.

set_node_features_matrix(self, node_features_matrix)[source]

Set nodes input features matrix.

get_node_features_matrix(self)[source]

Set nodes input features matrix.

set_edge_features_matrix(self, edge_features_matrix)[source]

Set edges input features matrix.

get_edge_features_matrix(self)[source]

Get edges input features matrix.

set_global_features_matrix(self, global_features_matrix)[source]

Set global input features matrix.

get_global_features_matrix(self)[source]

Get global input features matrix.

set_node_targets_matrix(self, node_targets_matrix)[source]

Set node targets matrix.

get_node_targets_matrix(self)[source]

Get node targets matrix.

set_edge_targets_matrix(self, edge_targets_matrix)[source]

Set edge targets matrix.

get_edge_targets_matrix(self)[source]

Get edge targets matrix.

set_global_targets_matrix(self, global_targets_matrix)[source]

Set global targets matrix.

get_global_targets_matrix(self)[source]

Get global targets matrix.

set_metadata(self, metadata)[source]

Set metadata information.

add_metadata(self, key, value)[source]

Add metadata information.

get_metadata(self)[source]

Get metadata information.

plot_graph(self, is_show_plot=False, is_save_plot=False, save_directory=None, plot_name=None, is_overwrite_file=False)[source]

Generate plot of graph.

_get_edges_from_local_radius(nodes_coords, connect_radius)[source]

Get edges between nodes that are within a given connectivity radius.

get_undirected_unique_edges(edges_indexes)[source]

Get set of undirected unique edges indexes.

_check_edges_indexes_matrix(edges_indexes)[source]

Check if given edges indexes matrix is valid.

Constructor.

Parameters:
  • n_dim (int) – Number of spatial dimensions.

  • nodes_coords (numpy.ndarray(2d)) – Coordinates of nodes stored as a numpy.ndarray(2d) with shape (n_nodes, n_dim). Coordinates of i-th node are stored in nodes_coords[i, :].

List of Public Methods

add_metadata

Add metadata information.

extract_data_torch_data_object

Extract data from PyG homogeneous graph data object.

get_edge_features_matrix

Get edges input features matrix.

get_edge_targets_matrix

Get edge targets matrix.

get_edges_indexes_mesh

Convert set of mesh connected nodes to edges indexes matrix.

get_global_features_matrix

Get global input features matrix.

get_global_targets_matrix

Get global targets matrix.

get_graph_edges_indexes

Get graph edges indexes.

get_metadata

Get metadata information.

get_n_edge

Get graph number of edges.

get_n_node

Get graph number of nodes.

get_node_features_matrix

Set nodes input features matrix.

get_node_targets_matrix

Get node targets matrix.

get_nodes_coords

Get graph node coordinates.

get_torch_data_object

Get PyG homogeneous graph data object.

get_undirected_unique_edges

Get set of undirected unique edges indexes.

move_time_dimension_axis

Move time dimension axis in temporal data array.

plot_graph

Generate plot of graph.

set_edge_features_matrix

Set edges input features matrix.

set_edge_targets_matrix

Set edge targets matrix.

set_global_features_matrix

Set global input features matrix.

set_global_targets_matrix

Set global targets matrix.

set_graph_edges_indexes

Set graph edges indexes and number of edges.

set_metadata

Set metadata information.

set_node_features_matrix

Set nodes input features matrix.

set_node_targets_matrix

Set node targets matrix.

Methods

__init__(n_dim, nodes_coords)[source]

Constructor.

Parameters:
  • n_dim (int) – Number of spatial dimensions.

  • nodes_coords (numpy.ndarray(2d)) – Coordinates of nodes stored as a numpy.ndarray(2d) with shape (n_nodes, n_dim). Coordinates of i-th node are stored in nodes_coords[i, :].

static _check_edges_indexes_matrix(edges_indexes)[source]

Check if given edges indexes matrix is valid.

Parameters:

edges_indexes (numpy.ndarray(2d)) – Edges indexes matrix stored as numpy.ndarray[int](2d) with shape (n_edges, 2), where the i-th edge is stored in edges_indexes[i, :] as (start_node_index, end_node_index).

static _get_edges_from_local_radius(nodes_coords, connect_radius)[source]

Get edges between nodes that are within a given connectivity radius.

Parameters:
  • nodes_coords (numpy.ndarray(2d)) – Coordinates of nodes stored as a numpy.ndarray(2d) with shape (n_nodes, n_dim). Coordinates of i-th node are stored in nodes_coords[i, :].

  • connect_radius (float) – Connectivity radius that sets the maximum distance between two nodes that leads to an edge.

Returns:

edges_indexes – Edges indexes matrix stored as numpy.ndarray[int](2d) with shape (num_edges, 2), where the i-th edge is stored in edges_indexes[i, :] as (start_node_index, end_node_index).

Return type:

numpy.ndarray(2d)

add_metadata(key, value)[source]

Add metadata information.

Parameters:
  • key (str) – Key of metadata information.

  • value (Any) – Value of metadata information.

static extract_data_torch_data_object(pyg_graph, attributes)[source]

Extract data from PyG homogeneous graph data object.

Parameters:
  • pyg_graph (torch_geometric.data.Data) – PyG data object describing a homogeneous graph.

  • attributes (tuple) – Attributes to be extracted from PyG data object. Available attributes coincide with the attributes of GraphData.

Returns:

attributes_data – Extracted attributes data sorted according with provided attributes sequence.

Return type:

tuple

get_edge_features_matrix()[source]

Get edges input features matrix.

Returns:

edge_features_matrix – Edges input features matrix stored as a numpy.ndarray(2d) of shape (n_edges, n_features).

Return type:

numpy.ndarray(2d)

get_edge_targets_matrix()[source]

Get edge targets matrix.

Returns:

edge_targets_matrix – Edges targets matrix stored as a numpy.ndarray(2d) of shape (n_edges, n_targets).

Return type:

numpy.ndarray(2d)

static get_edges_indexes_mesh(connected_nodes)[source]

Convert set of mesh connected nodes to edges indexes matrix.

It is assumed that nodes are labeled from 1 to n_nodes, such that node 1 and node n_nodes are associated with indexes 0 and n_nodes-1, respectively.

Parameters:

connected_nodes (tuple[tuple(2)]) – A set containing all pairs of nodes that are connected by any relevant mesh representation (e.g., finite element mesh). Each connection is stored a single time as a tuple(node[int], node[int]) and is independent of the corresponding nodes storage order.

Returns:

edges_indexes_mesh – Edges stemming from any relevant mesh representation (e.g., finite element mesh) and that should be accounted for. Edges indexes matrix stored as numpy.ndarray[int](2d) with shape (n_edges, 2), where the i-th edge is stored in edges_indexes[i, :] as (start_node_index, end_node_index).

Return type:

numpy.ndarray(2d), default=None

get_global_features_matrix()[source]

Get global input features matrix.

Returns:

global_features_matrix – Global input features matrix stored as a numpy.ndarray(2d) of shape (1, n_features).

Return type:

numpy.ndarray(2d)

get_global_targets_matrix()[source]

Get global targets matrix.

Returns:

global_targets_matrix – Global targets matrix stored as a numpy.ndarray(2d) of shape (1, n_targets).

Return type:

numpy.ndarray(2d)

get_graph_edges_indexes()[source]

Get graph edges indexes.

Returns:

edges_indexes – Edges indexes matrix stored as numpy.ndarray[int](2d) with shape (num_edges, 2), where the i-th edge is stored in edges_indexes[i, :] as (start_node_index, end_node_index).

Return type:

numpy.ndarray(2d)

get_metadata()[source]

Get metadata information.

Returns:

metadata – Metadata information stored as a dictionary.

Return type:

dict

get_n_edge()[source]

Get graph number of edges.

Returns:

n_edge – Number of edges.

Return type:

int

get_n_node()[source]

Get graph number of nodes.

Returns:

n_node – Number of nodes.

Return type:

int

get_node_features_matrix()[source]

Set nodes input features matrix.

Returns:

node_features_matrix – Nodes input features matrix stored as a numpy.ndarray(2d) of shape (n_nodes, n_features).

Return type:

numpy.ndarray(2d)

get_node_targets_matrix()[source]

Get node targets matrix.

Returns:

node_targets_matrix – Nodes targets matrix stored as a numpy.ndarray(2d) of shape (n_nodes, n_targets).

Return type:

numpy.ndarray(2d)

get_nodes_coords()[source]

Get graph node coordinates.

Returns:

nodes_coords – Coordinates of nodes stored as a numpy.ndarray(2d) with shape (n_nodes, n_dim). Coordinates of i-th node are stored in nodes_coords[i, :].

Return type:

numpy.ndarray(2d), default=None

get_torch_data_object()[source]

Get PyG homogeneous graph data object.

Returns:

pyg_graph – PyG data object describing a homogeneous graph.

Return type:

torch_geometric.data.Data

static get_undirected_unique_edges(edges_indexes)[source]

Get set of undirected unique edges indexes.

This function processes the given matrix of edges indexes and transforms all edges into undirected edges. In addition, it also removes any duplicated edges.

Parameters:

edges_indexes (numpy.ndarray(2d)) – Edges indexes matrix stored as numpy.ndarray[int](2d) with shape (n_edges, 2), where the i-th edge is stored in edges_indexes[i, :] as (start_node_index, end_node_index).

Returns:

edges_indexes – Edges indexes matrix stored as numpy.ndarray[int](2d) with shape (n_edges, 2), where the i-th edge is stored in edges_indexes[i, :] as (start_node_index, end_node_index).

Return type:

numpy.ndarray(2d)

static move_time_dimension_axis(array, source_dim, dest_dim)[source]

Move time dimension axis in temporal data array.

Parameters:
  • array (np.ndarray) – Temporal data array.

  • source_dim (int) – Original index of time dimension.

  • dest_dim (int) – Destination index of time dimension.

Returns:

array – Reshaped temporal data array.

Return type:

np.ndarray

plot_graph(is_show_plot=False, is_save_plot=False, save_directory=None, plot_name=None, is_overwrite_file=False)[source]

Generate plot of graph.

Parameters:
  • is_show_plot (bool, default=False) – Display plot of graph if True.

  • is_save_plot (bool, default=False) – Save plot of graph. Plot is only saved if save_directory is provided and exists.

  • save_directory (str, default=None) – Directory where plot of graph is stored.

  • plot_name (str, default=None) – Filename of graph plot.

  • is_overwrite_file (bool, default=False) – Overwrite plot of graph if True, otherwise generate generate non-existent file path by extending the original file path with an integer.

set_edge_features_matrix(edge_features_matrix)[source]

Set edges input features matrix.

Parameters:

edge_features_matrix (numpy.ndarray(2d)) – Edges input features matrix stored as a numpy.ndarray(2d) of shape (n_edges, n_features).

set_edge_targets_matrix(edge_targets_matrix)[source]

Set edge targets matrix.

Parameters:

edge_targets_matrix (numpy.ndarray(2d)) – Edges targets matrix stored as a numpy.ndarray(2d) of shape (n_edges, n_targets).

set_global_features_matrix(global_features_matrix)[source]

Set global input features matrix.

Parameters:

global_features_matrix (numpy.ndarray(2d)) – Global input features matrix stored as a numpy.ndarray(2d) of shape (1, n_features).

set_global_targets_matrix(global_targets_matrix)[source]

Set global targets matrix.

Parameters:

global_targets_matrix (numpy.ndarray(2d)) – Global targets matrix stored as a numpy.ndarray(2d) of shape (1, n_targets).

set_graph_edges_indexes(connect_radius=None, edges_indexes_mesh=None, is_unique=True)[source]

Set graph edges indexes and number of edges.

Sets existing edges input features and targets matrices to None.

Parameters:
  • connect_radius (float, default=None) – Connectivity radius that sets the maximum distance between two nodes that leads to an edge. If None, then no edges are generated from distance-based search.

  • edges_indexes_mesh (numpy.ndarray(2d), default=None) – Edges stemming from any relevant mesh representation (e.g., finite element mesh) and that should be accounted for. Edges indexes matrix stored as numpy.ndarray[int](2d) with shape (n_edges, 2), where the i-th edge is stored in edges_indexes[i, :] as (start_node_index, end_node_index).

  • is_unique (bool, default=True) – Remove any existent duplicated edges if True. Resulting unique edges are sorted by ascending order of the corresponding indexes, according to ‘numpy.unique’ output. If False, edges are kept unmodified.

set_metadata(metadata)[source]

Set metadata information.

Parameters:

metadata (dict) – Any metadata information stored as a dictionary.

Note

Metadata information is useful to store any additional information that is not related to the graph data itself but that is relevant for further processing or analysis, e,g., sample id, time step, etc.

metadata is a dictionary that won’t be moved to the GPU when using .to(device). Avoid retrieving information from it inside your a model.

set_node_features_matrix(node_features_matrix)[source]

Set nodes input features matrix.

Parameters:

node_features_matrix ({numpy.ndarray(2d), None}) – Nodes input features matrix stored as a numpy.ndarray(2d) of shape (n_nodes, n_features).

set_node_targets_matrix(node_targets_matrix)[source]

Set node targets matrix.

Parameters:

node_targets_matrix (numpy.ndarray(2d)) – Nodes targets matrix stored as a numpy.ndarray(2d) of shape (n_nodes, n_targets).