neural_lam.utils.graph#
Loading and zero-indexing of mesh/grid graph tensors.
Attributes#
Functions#
|
Compute the total grid input dimensionality of a graph step predictor. |
|
Load a graph and register its tensors on |
|
Load all tensors representing the graph from graph_dir_path. |
|
Make both sender and receiver indices of edge_index start at 0. |
|
Zero-index the g2m (grid-to-mesh) edge index, or undo this operation. |
|
Zero-index the m2g (mesh-to-grid) edge index, or undo this operation. |
Module Contents#
- neural_lam.utils.graph.compute_grid_input_dim(datastore: neural_lam.datastore.BaseDatastore, num_past_forcing_steps: int, num_future_forcing_steps: int) int[source]#
Compute the total grid input dimensionality of a graph step predictor.
The grid input concatenates the two previous states, the grid static features and the windowed forcing (past + current + future forcing steps).
- Parameters:
datastore (BaseDatastore) – Datastore providing the number of state, static and forcing variables.
num_past_forcing_steps (int) – Number of past forcing steps included in the input window.
num_future_forcing_steps (int) – Number of future forcing steps included in the input window.
- Returns:
Total grid input dimensionality.
- Return type:
- neural_lam.utils.graph.load_and_register_graph(module: torch.nn.Module, datastore: neural_lam.datastore.BaseDatastore, graph_name: str, mesh_node_features_scaling: float) bool[source]#
Load a graph and register its tensors on
module.Loads the graph
graph_namefrom the datastore’s graph directory viaload_graph(), then registers each tensor as a non-persistent buffer and each non-tensor (e.g.BufferList) as a plain attribute onmodule.- Parameters:
module (torch.nn.Module) – Module to register the graph tensors and attributes on, in place.
datastore (BaseDatastore) – Datastore whose
root_pathholds thegraphdirectory.graph_name (str) – Name of the graph directory (under
<root>/graph) to load.mesh_node_features_scaling (float) – Scalar used to normalize mesh node coordinate features for graphs in the current on-disk format; forwarded to
load_graph().
- Returns:
Whether the loaded graph is hierarchical.
- Return type:
- neural_lam.utils.graph.load_graph(graph_dir_path: str | pathlib.Path, mesh_node_features_scaling: float, device: str = 'cpu') tuple[bool, dict[str, Any]][source]#
Load all tensors representing the graph from graph_dir_path.
Needs the following files for all graphs: - m2m_edge_index.pt - g2m_edge_index.pt - m2g_edge_index.pt - m2m_features.pt - g2m_features.pt - m2g_features.pt - mesh_features.pt
And in addition for hierarchical graphs: - mesh_up_edge_index.pt - mesh_down_edge_index.pt - mesh_up_features.pt - mesh_down_features.pt
- Parameters:
- Returns:
hierarchical (bool) – Whether the graph is hierarchical.
graph (dict) – Dictionary containing the graph tensors, with keys as follows: - g2m_edge_index - m2g_edge_index - m2m_edge_index - mesh_up_edge_index - mesh_down_edge_index - g2m_features - m2g_features - m2m_features - mesh_up_features - mesh_down_features - mesh_static_features
- neural_lam.utils.graph.zero_index_edge_index(edge_index: torch.Tensor) torch.Tensor[source]#
Make both sender and receiver indices of edge_index start at 0.
- Parameters:
edge_index (torch.Tensor) – Edge index tensor of shape (2, num_edges).
- Returns:
Edge index tensor with indices starting at 0.
- Return type:
- neural_lam.utils.graph.zero_index_g2m(g2m_edge_index: torch.Tensor, mesh_static_features: list[torch.Tensor], mesh_first: bool, restore: bool = False) torch.Tensor[source]#
Zero-index the g2m (grid-to-mesh) edge index, or undo this operation.
Special handling is needed since not all mesh nodes may be present.
- Parameters:
g2m_edge_index (torch.Tensor) – Edge index tensor of shape (2, num_edges).
mesh_static_features (list of torch.Tensor) – Mesh node feature tensors.
mesh_first (bool) – If True, mesh nodes are indexed before grid nodes.
restore (bool) – If True, undo zero-indexing (restore original indices).
- Returns:
Edge index tensor with zero-based or restored indices.
- Return type:
- neural_lam.utils.graph.zero_index_m2g(m2g_edge_index: torch.Tensor, mesh_static_features: list[torch.Tensor], mesh_first: bool, restore: bool = False) torch.Tensor[source]#
Zero-index the m2g (mesh-to-grid) edge index, or undo this operation.
Special handling is needed since not all mesh nodes may be present.
- Parameters:
m2g_edge_index (torch.Tensor) – Edge index tensor of shape (2, num_edges).
mesh_static_features (list of torch.Tensor) – Mesh node feature tensors.
mesh_first (bool) – If True, mesh nodes are indexed before grid nodes.
restore (bool) – If True, undo zero-indexing (restore original indices).
- Returns:
Edge index tensor with zero-based or restored indices.
- Return type:
- neural_lam.utils.graph.LEGACY_GRAPH_SPEC_VERSION = 'legacy'#