-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pre-commit configuration for linting and formatting (#6)
- Added linting and formatting using pre-commits - Trigger pre-commit on main-branch push - Change line length to 80 - Fixing existing formatting issues - Added section about development with pre-commits --------- Co-authored-by: joeloskarsson <joel.oskarsson@liu.se>
- Loading branch information
1 parent
c14b6b4
commit 474bad9
Showing
23 changed files
with
2,318 additions
and
1,122 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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
name: Run pre-commit in blueprint | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
blueprint-pre-commit: | ||
runs-on: ubuntu-latest | ||
defaults: | ||
run: | ||
shell: bash -l {0} | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Set up Python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: 3.11.7 | ||
- name: Install pre-commit hooks | ||
run: | | ||
pip install -r requirements.txt | ||
- name: Run pre-commit hooks | ||
run: | | ||
pre-commit run --all-files |
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 |
---|---|---|
|
@@ -72,4 +72,3 @@ tags | |
|
||
# Coc configuration directory | ||
.vim | ||
|
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,51 @@ | ||
repos: | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v4.5.0 | ||
hooks: | ||
- id: check-ast | ||
- id: check-case-conflict | ||
- id: check-docstring-first | ||
- id: check-symlinks | ||
- id: check-toml | ||
- id: check-yaml | ||
- id: debug-statements | ||
- id: end-of-file-fixer | ||
- id: trailing-whitespace | ||
- repo: local | ||
hooks: | ||
- id: codespell | ||
name: codespell | ||
description: Check for spelling errors | ||
language: system | ||
entry: codespell | ||
- repo: local | ||
hooks: | ||
- id: black | ||
name: black | ||
description: Format Python code | ||
language: system | ||
entry: black | ||
types_or: [python, pyi] | ||
- repo: local | ||
hooks: | ||
- id: isort | ||
name: isort | ||
description: Group and sort Python imports | ||
language: system | ||
entry: isort | ||
types_or: [python, pyi, cython] | ||
- repo: local | ||
hooks: | ||
- id: flake8 | ||
name: flake8 | ||
description: Check Python code for correctness, consistency and adherence to best practices | ||
language: system | ||
entry: flake8 --max-line-length=80 --ignore=E203,F811,I002,W503 | ||
types: [python] | ||
- repo: local | ||
hooks: | ||
- id: pylint | ||
name: pylint | ||
entry: pylint -rn -sn | ||
language: system | ||
types: [python] |
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,42 +1,59 @@ | ||
# Standard library | ||
import os | ||
from tqdm import tqdm | ||
from argparse import ArgumentParser | ||
|
||
# Third-party | ||
import numpy as np | ||
import torch | ||
|
||
|
||
def main(): | ||
parser = ArgumentParser(description='Training arguments') | ||
parser.add_argument('--dataset', type=str, default="meps_example", | ||
help='Dataset to compute weights for (default: meps_example)') | ||
""" | ||
Pre-compute all static features related to the grid nodes | ||
""" | ||
parser = ArgumentParser(description="Training arguments") | ||
parser.add_argument( | ||
"--dataset", | ||
type=str, | ||
default="meps_example", | ||
help="Dataset to compute weights for (default: meps_example)", | ||
) | ||
args = parser.parse_args() | ||
|
||
static_dir_path = os.path.join("data", args.dataset, "static") | ||
|
||
# -- Static grid node features -- | ||
grid_xy = torch.tensor(np.load(os.path.join(static_dir_path, "nwp_xy.npy") | ||
)) # (2, N_x, N_y) | ||
grid_xy = grid_xy.flatten(1,2).T # (N_grid, 2) | ||
grid_xy = torch.tensor( | ||
np.load(os.path.join(static_dir_path, "nwp_xy.npy")) | ||
) # (2, N_x, N_y) | ||
grid_xy = grid_xy.flatten(1, 2).T # (N_grid, 2) | ||
pos_max = torch.max(torch.abs(grid_xy)) | ||
grid_xy = grid_xy / pos_max # Divide by maximum coordinate | ||
|
||
geopotential = torch.tensor(np.load(os.path.join(static_dir_path, | ||
"surface_geopotential.npy"))) # (N_x, N_y) | ||
geopotential = geopotential.flatten(0,1).unsqueeze(1) # (N_grid,1) | ||
geopotential = torch.tensor( | ||
np.load(os.path.join(static_dir_path, "surface_geopotential.npy")) | ||
) # (N_x, N_y) | ||
geopotential = geopotential.flatten(0, 1).unsqueeze(1) # (N_grid,1) | ||
gp_min = torch.min(geopotential) | ||
gp_max = torch.max(geopotential) | ||
# Rescale geopotential to [0,1] | ||
geopotential = (geopotential - gp_min)/(gp_max - gp_min) # (N_grid, 1) | ||
geopotential = (geopotential - gp_min) / (gp_max - gp_min) # (N_grid, 1) | ||
|
||
grid_border_mask = torch.tensor(np.load(os.path.join(static_dir_path, | ||
"border_mask.npy")), dtype=torch.int64) # (N_x, N_y) | ||
grid_border_mask = grid_border_mask.flatten(0, 1).to( | ||
torch.float).unsqueeze(1) # (N_grid, 1) | ||
grid_border_mask = torch.tensor( | ||
np.load(os.path.join(static_dir_path, "border_mask.npy")), | ||
dtype=torch.int64, | ||
) # (N_x, N_y) | ||
grid_border_mask = ( | ||
grid_border_mask.flatten(0, 1).to(torch.float).unsqueeze(1) | ||
) # (N_grid, 1) | ||
|
||
# Concatenate grid features | ||
grid_features = torch.cat((grid_xy, geopotential, grid_border_mask), | ||
dim=1) # (N_grid, 4) | ||
grid_features = torch.cat( | ||
(grid_xy, geopotential, grid_border_mask), dim=1 | ||
) # (N_grid, 4) | ||
|
||
torch.save(grid_features, os.path.join(static_dir_path, "grid_features.pt")) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.