### Install Pre-Commit Hooks
Source: https://anemoi.readthedocs.io/en/latest/contributing/environment.html
Install the pre-commit framework to manage and run code quality hooks automatically.
```bash
pre-commit install
```
--------------------------------
### Install testing dependencies
Source: https://anemoi.readthedocs.io/en/latest/_sources/contributing/testing.rst.txt
Install the required dependencies for running tests in the current environment.
```bash
pip install -e .[test]
```
--------------------------------
### Module docstring example
Source: https://anemoi.readthedocs.io/en/latest/_sources/contributing/documentation.rst.txt
Example of a module-level docstring following the NumPy style.
```python
"""
Module for building and managing reduced Gaussian grid nodes.
This module provides functionality to create and manipulate nodes based on
ECMWF's reduced Gaussian grid system, supporting both original and octahedral
grid types.
"""
```
--------------------------------
### Install Project Dependencies
Source: https://anemoi.readthedocs.io/en/latest/_sources/contributing/environment.rst.txt
Install all project dependencies or only development dependencies using pip. Use '-e' for editable installs.
```bash
pip install -e .
```
```bash
pip install -e '.[dev]'
```
--------------------------------
### Install Pre-Commit Hooks
Source: https://anemoi.readthedocs.io/en/latest/_sources/contributing/environment.rst.txt
Install the pre-commit framework to manage and run code quality hooks automatically before each commit.
```bash
pre-commit install
```
--------------------------------
### Example build output
Source: https://anemoi.readthedocs.io/en/latest/_sources/contributing/documentation.rst.txt
Sample terminal output indicating a successful Sphinx build process.
```bash
Running Sphinx v8.1.3
loading translations [en]... done
[...]
writing additional pages... search done
dumping search index in English (code: en)... done
dumping object inventory... done
build succeeded, 1 warning.
The HTML pages are in _build/html.
```
--------------------------------
### Install Documentation Dependencies
Source: https://anemoi.readthedocs.io/en/latest/contributing/documentation.html
Install the necessary dependencies to build the Anemoi documentation locally. This command should be run from the root of the Anemoi repository.
```bash
pip install '.[docs]'
```
--------------------------------
### Install Pandoc on macOS
Source: https://anemoi.readthedocs.io/en/latest/contributing/environment.html
Install the pandoc utility using Homebrew, required for building documentation on macOS.
```bash
brew install pandoc
```
--------------------------------
### Install Project Dependencies
Source: https://anemoi.readthedocs.io/en/latest/contributing/environment.html
Install all project dependencies, including development dependencies, using pip in an editable mode.
```bash
# For all dependencies
pip install -e .
# For development dependencies
pip install -e '.[dev]'
```
--------------------------------
### Install Anemoi package via pip
Source: https://anemoi.readthedocs.io/en/latest/_sources/getting-started/installing.rst.txt
Use this command to install specific Anemoi packages from PyPI.
```bash
pip install anemoi-{package}
```
--------------------------------
### Example Module Docstring
Source: https://anemoi.readthedocs.io/en/latest/contributing/documentation.html
An example of a module docstring following the NumPy style, explaining the module's purpose and functionality. This should be placed at the beginning of each Python module.
```python
"""
Module for building and managing reduced Gaussian grid nodes.
This module provides functionality to create and manipulate nodes based on
ECMWF's reduced Gaussian grid system, supporting both original and octahedral
grid types.
"""
```
--------------------------------
### Pytest Fixture Example
Source: https://anemoi.readthedocs.io/en/latest/_sources/contributing/testing.rst.txt
Demonstrates how to define and use a pytest fixture for setting up test data. Fixtures are useful for providing common test data or objects to multiple tests.
```python
@pytest.fixture
def sample_dataset():
# Create and return a sample dataset
pass
def test_data_loading(sample_dataset):
# Use the sample_dataset fixture in your test
pass
```
--------------------------------
### Define a Function with Docstrings
Source: https://anemoi.readthedocs.io/en/latest/contributing/documentation.html
Functions require clear docstrings including parameters, return types, and usage examples.
```python
def get_coordinates(self) -> torch.Tensor:
"""Get the coordinates of the nodes.
Returns
-------
torch.Tensor
A tensor of shape (num_nodes, 2) containing the latitude and longitude
coordinates in radians.
Examples
--------
>>> nodes = ReducedGaussianGridNodes("O640", "data")
>>> coords = nodes.get_coordinates()
>>> print(coords.shape)
torch.Size([6599680, 2])
"""
```
--------------------------------
### Function Docstrings
Source: https://anemoi.readthedocs.io/en/latest/contributing/documentation.html
Function docstrings should clearly define parameters, return values, and provide illustrative examples.
```APIDOC
## Function Docstrings
Functions should have clear docstrings with parameters, returns, and examples:
```python
def get_coordinates(self) -> torch.Tensor:
"""Get the coordinates of the nodes.
Returns
-------
torch.Tensor
A tensor of shape (num_nodes, 2) containing the latitude and longitude
coordinates in radians.
Examples
--------
>>> nodes = ReducedGaussianGridNodes("O640", "data")
>>> coords = nodes.get_coordinates()
>>> print(coords.shape)
torch.Size([6599680, 2])
"""
```
```
--------------------------------
### Define test structure
Source: https://anemoi.readthedocs.io/en/latest/_sources/contributing/testing.rst.txt
Example of a standard pytest structure for testing features, including normal inputs and error handling.
```python
import pytest
from anemoi.training import SomeFeature
def test_some_feature_normal_input():
feature = SomeFeature()
result = feature.process(normal_input)
assert result == expected_output
def test_some_feature_edge_case():
feature = SomeFeature()
with pytest.raises(ValueError):
feature.process(invalid_input)
```
--------------------------------
### Define Pytest Fixtures
Source: https://anemoi.readthedocs.io/en/latest/contributing/testing.html
Utilize fixtures to manage setup for common test data or objects required by multiple tests.
```python
@pytest.fixture
def sample_dataset():
# Create and return a sample dataset
pass
def test_data_loading(sample_dataset):
# Use the sample_dataset fixture in your test
pass
```
--------------------------------
### Marking Long-Running Tests in Pytest
Source: https://anemoi.readthedocs.io/en/latest/_sources/contributing/testing.rst.txt
Example of marking a test function with `@pytest.mark.slow`. This allows slow-running integration tests to be selectively run using a flag like `--slow`.
```python
@pytest.mark.slow
def test_long():
pass
```
--------------------------------
### Serve documentation locally
Source: https://anemoi.readthedocs.io/en/latest/_sources/contributing/documentation.rst.txt
Serve the built documentation files using a local Python HTTP server.
```bash
cd docs/_build/html
python -m http.server
```
--------------------------------
### Build Documentation Locally
Source: https://anemoi.readthedocs.io/en/latest/contributing/documentation.html
Navigate to the docs directory and run the make html command to build the documentation locally. The generated HTML files will be located in the docs/_build/html directory.
```bash
cd docs
make html
```
--------------------------------
### View Locally Built Documentation
Source: https://anemoi.readthedocs.io/en/latest/contributing/documentation.html
Open the index.html file in your web browser to view the locally built documentation. Alternatively, you can serve the documentation using Python's http.server module.
```bash
open docs/_build/html/index.html # macOS
xdg-open docs/_build/html/index.html # Linux
start docs/_build/html/index.html # Windows
```
```bash
cd docs/_build/html
python -m http.server
```
--------------------------------
### View local documentation
Source: https://anemoi.readthedocs.io/en/latest/_sources/contributing/documentation.rst.txt
Commands to open the generated HTML documentation in a web browser based on the operating system.
```bash
open docs/_build/html/index.html # macOS
xdg-open docs/_build/html/index.html # Linux
start docs/_build/html/index.html # Windows
```
--------------------------------
### Private Methods
Source: https://anemoi.readthedocs.io/en/latest/contributing/documentation.html
Even private methods should include basic documentation, including potential exceptions.
```APIDOC
## Private Methods
Even private methods should have basic documentation:
```python
def _validate_grid(self) -> None:
"""Validate the grid identifier format.
Raises
------
ValueError
If grid identifier doesn't match expected format
"""
```
```
--------------------------------
### Run Pre-Commit Hooks on All Files
Source: https://anemoi.readthedocs.io/en/latest/contributing/environment.html
Execute all configured pre-commit hooks on every file in the repository to ensure code consistency.
```bash
pre-commit run --all-files
```
--------------------------------
### Navigate to Package Directory
Source: https://anemoi.readthedocs.io/en/latest/contributing/environment.html
Change directory to the specific package within the anemoi-core or anemoi-{package} structure.
```bash
cd anemoi-core/{package}
```
```bash
cd anemoi-{package}
```
--------------------------------
### Run integration tests
Source: https://anemoi.readthedocs.io/en/latest/contributing/testing.html
Execute integration tests with the --slow flag enabled.
```bash
pytest training/tests/integration/ --slow
```
--------------------------------
### Class Docstrings
Source: https://anemoi.readthedocs.io/en/latest/contributing/documentation.html
Documentation for classes should include a brief description, attributes, methods, and any relevant notes.
```APIDOC
## Class Docstrings
Classes should have detailed docstrings following this format:
```python
class ReducedGaussianGridNodes:
"""Nodes from a reduced gaussian grid.
A gaussian grid is a latitude/longitude grid. The spacing of the latitudes
is not regular. However, the spacing of the lines of latitude is
symmetrical about the Equator.
Attributes
----------
grid : str
The reduced gaussian grid identifier (e.g., 'O640')
name : str
Unique identifier for the nodes in the graph
Methods
-------
get_coordinates()
Get the lat-lon coordinates of the nodes.
register_nodes(graph, name)
Register the nodes in the graph.
Notes
-----
The grid identifier format follows ECMWF conventions:
- 'N' prefix for original reduced Gaussian grid
- 'O' prefix for octahedral reduced Gaussian grid
- Number indicates latitude lines between pole and equator
For example, 'O640' represents an octahedral grid with 640
latitude lines between pole and equator.
"""
```
```
--------------------------------
### Define a Property with Docstrings
Source: https://anemoi.readthedocs.io/en/latest/contributing/documentation.html
Properties should have concise docstrings describing their purpose.
```python
@property
def num_nodes(self) -> int:
"""Number of nodes in the grid."""
return len(self.coordinates)
```
--------------------------------
### Property Docstrings
Source: https://anemoi.readthedocs.io/en/latest/contributing/documentation.html
Properties require concise yet informative docstrings.
```APIDOC
## Property Docstrings
Properties should have concise but clear docstrings:
```python
@property
def num_nodes(self) -> int:
"""Number of nodes in the grid."""
return len(self.coordinates)
```
```
--------------------------------
### Docstring Best Practices
Source: https://anemoi.readthedocs.io/en/latest/contributing/documentation.html
General guidelines for writing effective docstrings, including referencing other documentation and handling exceptions.
```APIDOC
"""
Process nodes in the graph.
See Also
--------
:ref:`graphs-post-processor` : Documentation about post-processing nodes
`PyG Documentation `_ : External docs
anemoi.graphs.nodes.TriNodes : Reference to another class
"""
```
--------------------------------
### Run unit tests
Source: https://anemoi.readthedocs.io/en/latest/contributing/testing.html
Execute the unit test suite using pytest.
```bash
pytest
```
--------------------------------
### Define a Private Method with Docstrings
Source: https://anemoi.readthedocs.io/en/latest/contributing/documentation.html
Private methods require basic documentation, including any exceptions that may be raised.
```python
def _validate_grid(self) -> None:
"""Validate the grid identifier format.
Raises
------
ValueError
If grid identifier doesn't match expected format
"""
```
--------------------------------
### Reference Documentation Links
Source: https://anemoi.readthedocs.io/en/latest/contributing/documentation.html
Use specific syntax to reference internal sections or external documentation.
```python
"""
Process nodes in the graph.
See Also
--------
:ref:`graphs-post-processor` : Documentation about post-processing nodes
`PyG Documentation `_ : External docs
anemoi.graphs.nodes.TriNodes : Reference to another class
"""
```
--------------------------------
### Run tests with pytest
Source: https://anemoi.readthedocs.io/en/latest/_sources/contributing/testing.rst.txt
Commands for executing different subsets of the test suite.
```bash
pytest
```
```bash
pytest tests/unit/test_specific_feature.py
```
```bash
pytest training/tests/integration/ --slow
```
--------------------------------
### Define a Class with Docstrings
Source: https://anemoi.readthedocs.io/en/latest/contributing/documentation.html
Classes should include detailed docstrings covering attributes, methods, and notes following the specified format.
```python
class ReducedGaussianGridNodes:
"""Nodes from a reduced gaussian grid.
A gaussian grid is a latitude/longitude grid. The spacing of the latitudes
is not regular. However, the spacing of the lines of latitude is
symmetrical about the Equator.
Attributes
----------
grid : str
The reduced gaussian grid identifier (e.g., 'O640')
name : str
Unique identifier for the nodes in the graph
Methods
-------
get_coordinates()
Get the lat-lon coordinates of the nodes.
register_nodes(graph, name)
Register the nodes in the graph.
Notes
-----
The grid identifier format follows ECMWF conventions:
- 'N' prefix for original reduced Gaussian grid
- 'O' prefix for octahedral reduced Gaussian grid
- Number indicates latitude lines between pole and equator
For example, 'O640' represents an octahedral grid with 640
latitude lines between pole and equator.
"""
```
--------------------------------
### Running Pytest with Coverage
Source: https://anemoi.readthedocs.io/en/latest/_sources/contributing/testing.rst.txt
Command to run pytest and measure code coverage using pytest-cov. Aim for at least 80% coverage for new features.
```bash
pytest --cov=anemoi_training
```
--------------------------------
### Type Hints
Source: https://anemoi.readthedocs.io/en/latest/contributing/documentation.html
Combining docstrings with type hints enhances code clarity and aids in error detection.
```APIDOC
## Type Hints
Always combine docstrings with type hints for better code clarity and catch potential errors:
```python
def register_nodes(
self, graph: HeteroData, attrs_config: dict[str, dict] | None = None
) -> HeteroData:
"""Register nodes in the graph with optional attributes.
Parameters
----------
graph : HeteroData
The graph to add nodes to
attrs_config : dict[str, dict] | None
Configuration for node attributes
Returns
-------
HeteroData
The updated graph with new nodes
"""
```
```
--------------------------------
### Mocking External API Calls with pytest-mock
Source: https://anemoi.readthedocs.io/en/latest/_sources/contributing/testing.rst.txt
Shows how to mock external dependencies like API calls using pytest-mock. This is useful for isolating the code under test and ensuring predictable behavior.
```python
def test_api_call(mocker):
mock_response = mocker.Mock()
mock_response.json.return_value = {"data": "mocked"}
mocker.patch("requests.get", return_value=mock_response)
result = my_api_function()
assert result == "mocked"
```
--------------------------------
### Verify Git Remote Configuration
Source: https://anemoi.readthedocs.io/en/latest/contributing/environment.html
Check the current Git remote configuration to ensure the upstream push URL is set correctly.
```bash
git remote -v
```
--------------------------------
### Run specific test file
Source: https://anemoi.readthedocs.io/en/latest/contributing/testing.html
Execute tests contained within a specific file.
```bash
pytest tests/unit/test_specific_feature.py
```
--------------------------------
### Define a Function with Type Hints
Source: https://anemoi.readthedocs.io/en/latest/contributing/documentation.html
Combine docstrings with type hints to improve code clarity and error detection.
```python
def register_nodes(
self, graph: HeteroData, attrs_config: dict[str, dict] | None = None
) -> HeteroData:
"""Register nodes in the graph with optional attributes.
Parameters
----------
graph : HeteroData
The graph to add nodes to
attrs_config : dict[str, dict] | None
Configuration for node attributes
Returns
-------
HeteroData
The updated graph with new nodes
"""
```
--------------------------------
### Measure Test Coverage
Source: https://anemoi.readthedocs.io/en/latest/contributing/testing.html
Execute pytest with the coverage plugin to generate reports for the specified package.
```bash
pytest --cov=anemoi_training
```
--------------------------------
### Mock External Dependencies
Source: https://anemoi.readthedocs.io/en/latest/contributing/testing.html
Use pytest-mock or unittest.mock to patch external calls and control return values during testing.
```python
def test_api_call(mocker):
mock_response = mocker.Mock()
mock_response.json.return_value = {"data": "mocked"}
mocker.patch("requests.get", return_value=mock_response)
result = my_api_function()
assert result == "mocked"
```
--------------------------------
### Mark Long-Running Tests
Source: https://anemoi.readthedocs.io/en/latest/contributing/testing.html
Apply the slow marker to integration tests to allow selective execution using the --slow flag.
```python
@pytest.mark.slow
def test_long():
pass
```
--------------------------------
### Parametrize Pytest Tests
Source: https://anemoi.readthedocs.io/en/latest/contributing/testing.html
Use the parametrize decorator to execute a single test function with multiple input and expected output pairs.
```python
@pytest.mark.parametrize(
"input,expected",
[
(2, 4),
(3, 9),
(4, 16),
],
)
def test_square(input, expected):
assert square(input) == expected
```
--------------------------------
### Prevent Accidental Pushes to Upstream
Source: https://anemoi.readthedocs.io/en/latest/contributing/environment.html
Configure the upstream remote to prevent accidental pushes. This command sets the push URL to 'no_push'.
```bash
git remote set-url --push upstream no_push
```
--------------------------------
### Prevent Upstream Pushes with Git
Source: https://anemoi.readthedocs.io/en/latest/_sources/contributing/environment.rst.txt
Configure the upstream remote's push URL to 'no_push' to prevent accidental commits to the original repository. Verify the configuration using 'git remote -v'.
```bash
git remote set-url --push upstream no_push
```
```bash
git remote -v
```
```bash
origin https://github.com/your-username/repository.git (fetch)
origin https://github.com/your-username/repository.git (push)
upstream https://github.com/original-owner/repository.git (fetch)
upstream no_push (push)
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.