|
18 | 18 | import numpy as np
|
19 | 19 | import torch
|
20 | 20 | from anemoi.datasets import open_dataset
|
| 21 | +from scipy.spatial import ConvexHull |
21 | 22 | from scipy.spatial import SphericalVoronoi
|
| 23 | +from scipy.spatial import Voronoi |
22 | 24 | from torch_geometric.data import HeteroData
|
23 | 25 | from torch_geometric.data.storage import NodeStorage
|
24 | 26 |
|
@@ -101,6 +103,68 @@ def get_raw_values(self, nodes: NodeStorage, **kwargs) -> np.ndarray:
|
101 | 103 | class AreaWeights(BaseNodeAttribute):
|
102 | 104 | """Implements the area of the nodes as the weights.
|
103 | 105 |
|
| 106 | + Attributes |
| 107 | + ---------- |
| 108 | + flat: bool |
| 109 | + If True, the area is computed in 2D, otherwise in 3D. |
| 110 | + **other: Any |
| 111 | + Additional keyword arguments, see PlanarAreaWeights and SphericalAreaWeights |
| 112 | + for details. |
| 113 | +
|
| 114 | + Methods |
| 115 | + ------- |
| 116 | + compute(self, graph, nodes_name) |
| 117 | + Compute the area attributes for each node. |
| 118 | + """ |
| 119 | + |
| 120 | + def __new__(cls, flat: bool = False, **kwargs): |
| 121 | + logging.warning( |
| 122 | + "Creating %s with flat=%s and kwargs=%s. In a future release, AreaWeights will be deprecated: please use directly PlanarAreaWeights or SphericalAreaWeights.", |
| 123 | + cls.__name__, |
| 124 | + flat, |
| 125 | + kwargs, |
| 126 | + ) |
| 127 | + if flat: |
| 128 | + return PlanarAreaWeights(**kwargs) |
| 129 | + return SphericalAreaWeights(**kwargs) |
| 130 | + |
| 131 | + |
| 132 | +class PlanarAreaWeights(BaseNodeAttribute): |
| 133 | + """Implements the 2D area of the nodes as the weights. |
| 134 | +
|
| 135 | + Attributes |
| 136 | + ---------- |
| 137 | + norm : str |
| 138 | + Normalisation of the weights. |
| 139 | +
|
| 140 | + Methods |
| 141 | + ------- |
| 142 | + compute(self, graph, nodes_name) |
| 143 | + Compute the area attributes for each node. |
| 144 | + """ |
| 145 | + |
| 146 | + def __init__( |
| 147 | + self, |
| 148 | + norm: str | None = None, |
| 149 | + dtype: str = "float32", |
| 150 | + ) -> None: |
| 151 | + super().__init__(norm, dtype) |
| 152 | + |
| 153 | + def get_raw_values(self, nodes: NodeStorage, **kwargs) -> np.ndarray: |
| 154 | + latitudes, longitudes = nodes.x[:, 0], nodes.x[:, 1] |
| 155 | + points = np.stack([latitudes, longitudes], -1) |
| 156 | + v = Voronoi(points, qhull_options="QJ Pp") |
| 157 | + areas = [] |
| 158 | + for r in v.regions: |
| 159 | + area = ConvexHull(v.vertices[r, :]).volume |
| 160 | + areas.append(area) |
| 161 | + result = np.asarray(areas) |
| 162 | + return result |
| 163 | + |
| 164 | + |
| 165 | +class SphericalAreaWeights(BaseNodeAttribute): |
| 166 | + """Implements the 3D area of the nodes as the weights. |
| 167 | +
|
104 | 168 | Attributes
|
105 | 169 | ----------
|
106 | 170 | norm : str
|
@@ -148,8 +212,9 @@ def get_raw_values(self, nodes: NodeStorage, **kwargs) -> np.ndarray:
|
148 | 212 | np.ndarray
|
149 | 213 | Attributes.
|
150 | 214 | """
|
151 |
| - points = latlon_rad_to_cartesian(nodes.x) |
152 |
| - sv = SphericalVoronoi(points.cpu().numpy(), self.radius, self.centre) |
| 215 | + latitudes, longitudes = nodes.x[:, 0], nodes.x[:, 1] |
| 216 | + points = latlon_rad_to_cartesian((np.asarray(latitudes), np.asarray(longitudes))) |
| 217 | + sv = SphericalVoronoi(points, self.radius, self.centre) |
153 | 218 | mask = np.array([bool(i) for i in sv.regions])
|
154 | 219 | sv.regions = [region for region in sv.regions if region]
|
155 | 220 | # compute the area weight without empty regions
|
|
0 commit comments