This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
145 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -187,4 +187,4 @@ _build/ | |
_version.py | ||
*.code-workspace | ||
|
||
/config* | ||
/config* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,10 @@ | ||
earth_radius = 6371.0 # km | ||
# (C) Copyright 2023 European Centre for Medium-Range Weather Forecasts. | ||
# This software is licensed under the terms of the Apache Licence Version 2.0 | ||
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
# In applying this licence, ECMWF does not waive the privileges and immunities | ||
# granted to it by virtue of its status as an intergovernmental organisation | ||
# nor does it submit to any jurisdiction. | ||
|
||
from ._version import __version__ | ||
|
||
earth_radius = 6371.0 # km |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from anemoi.graphs.create import GraphCreator | ||
|
||
from . import Command | ||
|
||
|
||
class Create(Command): | ||
"""Create a graph.""" | ||
|
||
internal = True | ||
timestamp = True | ||
|
||
def add_arguments(self, command_parser): | ||
command_parser.add_argument( | ||
"--overwrite", | ||
action="store_true", | ||
help="Overwrite existing files. This will delete the target graph if it already exists.", | ||
) | ||
command_parser.add_argument("config", help="Configuration yaml file defining the recipe to create the graph.") | ||
command_parser.add_argument("path", help="Path to store the created graph.") | ||
|
||
def run(self, args): | ||
kwargs = vars(args) | ||
|
||
c = GraphCreator(**kwargs) | ||
c.create() | ||
|
||
|
||
command = Create |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import os | ||
import torch | ||
from anemoi.utils.config import DotDict | ||
from hydra.utils import instantiate | ||
from torch_geometric.data import HeteroData | ||
|
||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def generate_graph(graph_config: DotDict) -> HeteroData: | ||
graph = HeteroData() | ||
|
||
for name, nodes_cfg in graph_config.nodes.items(): | ||
graph = instantiate(nodes_cfg.node_type).transform(graph, name, nodes_cfg.get("attributes", {})) | ||
|
||
for edges_cfg in graph_config.edges: | ||
graph = instantiate(edges_cfg.edge_type, **edges_cfg.nodes).transform(graph, edges_cfg.get("attributes", {})) | ||
|
||
return graph | ||
|
||
|
||
class GraphCreator: | ||
def __init__( | ||
self, | ||
path, | ||
config=None, | ||
cache=None, | ||
print=print, | ||
overwrite=False, | ||
**kwargs, | ||
): | ||
self.path = path # Output path | ||
self.config = config | ||
self.cache = cache | ||
self.print = print | ||
self.overwrite = overwrite | ||
|
||
def init(self): | ||
assert os.path.exists(self.config), f"Path {self.config} does not exist." | ||
|
||
if self._path_readable() and not self.overwrite: | ||
raise Exception(f"{self.path} already exists. Use overwrite=True to overwrite.") | ||
|
||
def load(self) -> HeteroData: | ||
config = DotDict.from_file(self.config) | ||
graph = generate_graph(config) | ||
return graph | ||
|
||
def save(self, graph: HeteroData) -> None: | ||
if not os.path.exists(self.path) or self.overwrite: | ||
torch.save(graph, self.path) | ||
self.print(f"Graph saved at {self.path}.") | ||
|
||
def create(self): | ||
self.init() | ||
graph = self.load() | ||
self.save(graph) | ||
|
||
def _path_readable(self) -> bool: | ||
import torch | ||
|
||
try: | ||
torch.load(self.path, "r") | ||
return True | ||
except FileNotFoundError: | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import numpy as np | ||
import logging | ||
|
||
import numpy as np | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters