Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

[Feature] New Edge Attribute: AttributeFromNode #95

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Keep it human-readable, your future self will thank you!

### Added

- feat: Add `AttributeFromSourceNode` and `AttributeFromTargetNode` edge attribute to copy attribute from source or target node. Set `node_attr_name` in the config to specify which attribute to copy from the source | target node (#94)

# Changed
- feat: Support for providing lon/lat coordinates from a text file (loaded with numpy loadtxt method) to build the graph `TextNodes` (#93)
- feat: Build 2D graphs with `Voronoi` in case `SphericalVoronoi` does not work well/is an overkill (LAM). Set `flat=true` in the nodes attributes to compute area weight using Voronoi with a qhull options preventing the empty region creation (#93) 
- feat: Support for defining nodes from lat& lon NumPy arrays (#98)
Expand Down
48 changes: 47 additions & 1 deletion docs/graphs/edge_attributes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Edges - Attributes
####################

There are 2 main edge attributes implemented in the `anemoi-graphs`
There are few edge attributes implemented in the `anemoi-graphs`
package:

*************
Expand Down Expand Up @@ -44,3 +44,49 @@ latitude and longitude coordinates of the source and target nodes.
attributes:
edge_length:
_target_: anemoi.graphs.edges.attributes.EdgeDirection

*********************
Copy link
Member

@JPXKQX JPXKQX Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would group all new documentation under the same header "Attribute from Node" (or "Attribute from Source/Target Node"), so we get the left sidebar with the 3 types of attributes: Edge direction, edge length, attribute from node. Now:
Screenshot 2024-12-19 at 09 08 05
I would drop the 1st and 3rd yaml examples, and also the subheaders. If you think it needs some explanation, you can also add a comment after a "#" in the yaml example.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done ✅

Attribute from Node
*********************

Attributes can also be copied from nodes to edges. This is done using
the `AttributeFromNode` base class, with specialized versions for source
and target nodes.

From Source
===========

This attribute copies a specific property of the source node to the
edge. Example usage for copying the cutout mask from nodes to edges in
the encoder:

.. code:: yaml

edges:
# Encoder
- source_name: data
target_name: hidden
edge_builders: ...
attributes:
cutout: # Assigned name to the edge attribute, can be different than node_attr_name
_target_: anemoi.graphs.edges.attributes.AttributeFromSourceNode
node_attr_name: cutout

From Target
===========

This attribute copies a specific property of the target node to the
edge. Example usage for copying the coutout mask from nodes to edges in
the decoder:

.. code:: yaml

edges:
# Decoder
- source_name: hidden
target_name: data
edge_builders: ...
attributes:
cutout: # Assigned name to the edge attribute, can be different than node_attr_name
_target_: anemoi.graphs.edges.attributes.AttributeFromTargetNode
node_attr_name: cutout
85 changes: 82 additions & 3 deletions src/anemoi/graphs/edges/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
class BaseEdgeAttribute(ABC, NormaliserMixin):
"""Base class for edge attributes."""

def __init__(self, norm: str | None = None) -> None:
def __init__(self, norm: str | None = None, dtype: str = "float32") -> None:
self.norm = norm
self.dtype = dtype

@abstractmethod
def get_raw_values(self, graph: HeteroData, source_name: str, target_name: str, *args, **kwargs) -> np.ndarray: ...
Expand All @@ -35,9 +36,9 @@ def post_process(self, values: np.ndarray) -> torch.Tensor:
if values.ndim == 1:
values = values[:, np.newaxis]

normed_values = self.normalise(values)
norm_values = self.normalise(values)

return torch.tensor(normed_values, dtype=torch.float32)
return torch.tensor(norm_values.astype(self.dtype))

def compute(self, graph: HeteroData, edges_name: tuple[str, str, str], *args, **kwargs) -> torch.Tensor:
"""Compute the edge attributes."""
Expand Down Expand Up @@ -155,3 +156,81 @@ def post_process(self, values: np.ndarray) -> torch.Tensor:
values = 1 - values

return values


class BooleanBaseEdgeAttribute(BaseEdgeAttribute, ABC):
"""Base class for boolean edge attributes."""

def __init__(self) -> None:
super().__init__(norm=None, dtype="bool")


class AttributeFromNode(BooleanBaseEdgeAttribute, ABC):
"""
Base class for Attribute from Node.

Copy an attribute of either the source or target node to the edge.
Accesses source/target node attribute and propagates it to the edge.
Used for example to identify if an encoder edge originates from a LAM or global node.

Attributes
----------
node_attr_name : str
Name of the node attribute to propagate.

Methods
-------
get_node_name(source_name, target_name)
Return the name of the node to copy.

get_raw_values(graph, source_name, target_name)
Computes the edge attribute from the source or target node attribute.

"""

def __init__(self, node_attr_name: str) -> None:
super().__init__()
self.node_attr_name = node_attr_name
self.idx = None

@abstractmethod
def get_node_name(self, source_name: str, target_name: str): ...

def get_raw_values(self, graph: HeteroData, source_name: str, target_name: str) -> np.ndarray:

node_name = self.get_node_name(source_name, target_name)

edge_index = graph[(source_name, "to", target_name)].edge_index
try:
return graph[node_name][self.node_attr_name].numpy()[edge_index[self.idx]]

except AttributeError:
raise AttributeError(
f"{self.__class__.__name__} failed because the attribute '{self.node_attr_name}' is not defined for the nodes."
)


class AttributeFromSourceNode(AttributeFromNode):
"""
Copy an attribute of the source node to the edge.
"""

def __init__(self, node_attr_name: str) -> None:
super().__init__(node_attr_name)
self.idx = 0

def get_node_name(self, source_name: str, target_name: str):
return source_name


class AttributeFromTargetNode(AttributeFromNode):
"""
Copy an attribute of the target node to the edge.
"""

def __init__(self, node_attr_name: str) -> None:
super().__init__(node_attr_name)
self.idx = 1

def get_node_name(self, source_name: str, target_name: str):
return target_name
Loading