Skip to content

Commit c03b09a

Browse files
authored
Merge pull request pik-piam#68 from beijingzyl/testupdate
Test update
2 parents c9a48bd + 64b4d93 commit c03b09a

File tree

68 files changed

+3012
-40207
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+3012
-40207
lines changed

.github/workflows/run_tests.yml

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,42 @@ jobs:
1515
runs-on: ubuntu-latest # Set the environment to Ubuntu
1616

1717
steps:
18-
# Step 1: Checkout the repository code
18+
# Step 1: Checkout the repository
1919
- uses: actions/checkout@v4
20-
# Step 2: Set up Conda
21-
- name: Set up Conda
20+
21+
# Step 2: Install Miniforge (no env creation here)
22+
- name: Setup Miniforge
2223
uses: conda-incubator/setup-miniconda@v3
2324
with:
24-
miniconda-version: 'latest' # Use the latest version of Miniconda
25-
environment-file: workflow/envs/environment.yaml # Point to your conda environment file (if exists)
25+
miniforge-version: latest
26+
installation-dir: /home/runner/miniforge3
27+
use-mamba: true
2628
auto-activate-base: false
27-
activate-environment: pypsa-china
28-
# /3 cache the Conda environment (for faster CI)
29-
- name: Cache Conda environment
30-
uses: actions/cache@v4
29+
channels: conda-forge,bioconda
30+
channel-priority: strict
31+
pkgs-dirs: /home/runner/.conda/pkgs
32+
33+
# Step 3: Restore full Conda environment cache AFTER setup
34+
- name: Restore Conda env cache
35+
id: env-cache
36+
uses: actions/cache@v4
3137
with:
32-
path: ~/miniconda3
33-
key: ${{ runner.os }}-conda-${{ hashFiles('workflow/envs/environment.yaml') }}
34-
restore-keys: |
35-
${{ runner.os }}-conda-
36-
# 4/ Run tests using pytest
37-
- name: Run pytest
38+
path: |
39+
/home/runner/miniforge3/envs/pypsa-china
40+
/home/runner/.conda/pkgs
41+
/home/runner/.cache/mamba
42+
key: ${{ runner.os }}-conda-env-${{ hashFiles('workflow/envs/environment.yaml') }}
43+
44+
# Step 4: Create env only if cache miss
45+
- name: Create env
46+
if: steps.env-cache.outputs.cache-hit != 'true'
3847
shell: bash -el {0}
3948
run: |
40-
conda install pytest
41-
pytest -v -s # Run pytest (make sure your tests are discovered and run)
49+
conda config --add envs_dirs /home/runner/miniforge3/envs
50+
mamba env create -n pypsa-china -f workflow/envs/environment.yaml
4251
52+
# Step 5: Run tests
53+
- name: Run pytest
54+
shell: bash -el {0}
55+
run: |
56+
conda run -n pypsa-china pytest -v -s

.pre-commit-config.yaml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
repos:
2-
- repo: https://github.com/psf/black
3-
rev: 22.3.0
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
# Ruff version.
4+
rev: v0.12.10
45
hooks:
5-
- id: black
6-
args: [--line-length=100]
7-
language_version: python3.12
6+
# Run the linter.
7+
- id: ruff-check
8+
args: [ --fix ]
9+
# Run the formatter.
10+
- id: ruff-format
11+
812
- repo: https://github.com/pre-commit/pre-commit-hooks
913
rev: v5.0.0
1014
hooks:
1115
- id: check-yaml
1216
- id: check-case-conflict
1317
- id: end-of-file-fixer
1418
- id: trailing-whitespace
19+
- repo: https://github.com/kynan/nbstripout
20+
rev: 0.8.1
21+
hooks:
22+
- id: nbstripout

.ruff.toml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
line-length = 100
2+
target-version = "py312"
3+
4+
# Exclude notebook files from linting
5+
exclude = [
6+
"**/*.ipynb",
7+
"workflow/notebooks/**",
8+
]
9+
10+
[lint]
11+
select = [
12+
"F", # pyflakes
13+
"E", # pycodestyle: Error
14+
"W", # pycodestyle: Warning
15+
"I", # isort
16+
"D", # pydocstyle
17+
"UP", # pyupgrade
18+
"TID", # flake8-tidy-imports
19+
]
20+
ignore = [
21+
"ANN401", # Dynamically typed expressions are forbidden
22+
"E712", # comparison to False should be 'if cond is False:' or 'if not cond:'
23+
"E741", # ambiguous variable names
24+
"D203", # 1 blank line required before class docstring
25+
"D212", # Multi-line docstring summary should start at the second line
26+
"D401", # First line should be in imperative mood
27+
]
28+
29+
[lint.per-file-ignores]
30+
# Ignore docstring and line length issues in all Python files
31+
32+
"**/*.py" = [
33+
'E501', # line too long
34+
'D107', # Missing docstring in __init__
35+
'D200', # One-line docstring should fit on one line with quotes
36+
'D202', # No blank lines allowed after function docstring
37+
'D205', # 1 blank line required between summary line and description
38+
'D400', # First line should end with a period
39+
'D404', # First word of the docstring should not be "This"
40+
'D413', # Missing blank line after last section
41+
'D415', # First line should end with a period, question mark, or exclamation point
42+
'D417', # Missing argument descriptions in the docstring
43+
"W291", # trailing whitespace
44+
]
45+
46+
[format]
47+
quote-style = "double"
48+
indent-style = "space"
49+
50+
[lint.pydocstyle]
51+
convention = "google"
52+
53+
[lint.isort]
54+
combine-as-imports = true
55+
known-first-party = ["workflow"]

Remind-PyPSA-coupling

Lines changed: 0 additions & 1 deletion
This file was deleted.

config/pik_hpc_profile/status-check.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# https://github.com/LUMC/slurm-cluster-status/blob/master/README.md
33
import argparse
44
import subprocess
5-
import time
65

76
STATE_MAP = {
87
"BOOT_FAIL": "failed",
@@ -20,25 +19,24 @@
2019
"RESIZING": "running",
2120
"SUSPENDED": "running",
2221
"TIMEOUT": "failed",
23-
"UNKNOWN": "running"
22+
"UNKNOWN": "running",
2423
}
2524

2625

2726
def fetch_status(batch_id):
28-
"""fetch the status for the batch id"""
29-
sacct_args = ["sacct", "-j", batch_id, "-o", "State", "--parsable2",
30-
"--noheader"]
27+
"""Fetch the status for the batch id"""
28+
sacct_args = ["sacct", "-j", batch_id, "-o", "State", "--parsable2", "--noheader"]
3129

3230
try:
3331
output = subprocess.check_output(sacct_args).decode("utf-8").strip()
3432
except Exception:
3533
# If sacct fails for whatever reason, assume its temporary and return 'running'
36-
output = 'UNKNOWN'
34+
output = "UNKNOWN"
3735

3836
# Sometimes, sacct returns nothing, in which case we assume it is temporary
3937
# and return 'running'
4038
if not output:
41-
output = 'UNKNOWN'
39+
output = "UNKNOWN"
4240

4341
# The first output is the state of the overall job
4442
# See
@@ -55,8 +53,9 @@ def fetch_status(batch_id):
5553
try:
5654
return STATE_MAP[job_status]
5755
except KeyError:
58-
raise NotImplementedError(f"Encountered unknown status '{job_status}' "
59-
f"when parsing output:\n'{output}'")
56+
raise NotImplementedError(
57+
f"Encountered unknown status '{job_status}' when parsing output:\n'{output}'"
58+
)
6059

6160

6261
if __name__ == "__main__":
@@ -65,4 +64,4 @@ def fetch_status(batch_id):
6564
args = parser.parse_args()
6665

6766
status = fetch_status(args.batch_id)
68-
print(status)
67+
print(status)

docs/autogen_ref_pages.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from pathlib import Path
2+
23
import mkdocs_gen_files
34

45
# Set the root and scripts directory

tests/conftest.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,15 @@
44
"""
55

66
import pathlib
7-
import yaml
8-
import pytest
7+
from hashlib import sha256 as hash256
98
from os import PathLike
10-
import matplotlib
11-
import shutil
129

13-
from hashlib import sha256 as hash256
14-
from os import remove
10+
import matplotlib
11+
import pytest
12+
import yaml
1513

1614
# from filelock import BaseFileLock
17-
18-
from constants import TESTS_RUNNAME, TESTS_CUTOUT
19-
from _helpers import PathManager
15+
from constants import TESTS_CUTOUT, TESTS_RUNNAME
2016

2117
DEFAULT_CONFIG = pathlib.Path(pathlib.Path.cwd(), "config", "default_config.yaml")
2218
TECH_CONFIG = pathlib.Path(pathlib.Path.cwd(), "config", "technology_config.yaml")
@@ -29,7 +25,7 @@ def set_matplotlib_backend():
2925

3026

3127
def load_config(config_path: PathLike) -> dict:
32-
"""load a config file
28+
"""Load a config file
3329
Args:
3430
config_path (PathLike): the path to the config file
3531
@@ -43,17 +39,21 @@ def load_config(config_path: PathLike) -> dict:
4339

4440
@pytest.fixture(scope="module")
4541
def make_snakemake_test_config(tmp_path_factory) -> dict:
46-
"""make a test config for snamekemake based on the default config
42+
"""Make a test config for snamekemake based on the default config
4743
Example:
4844
conf_dict = make_snamkemake_test_config({"scenario":{"planning_horizons":2030}})
45+
4946
Returns:
5047
dict: the test config
5148
"""
5249

5350
def make(
54-
time_res=24, plan_year=2040, start_d="01-02 00:00", end_d="01-02 23:00", **kwargs
51+
time_res=24,
52+
plan_year=2040,
53+
start_d="01-02 00:00",
54+
end_d="01-02 23:00",
55+
**kwargs,
5556
) -> dict:
56-
5757
base_config = load_config(DEFAULT_CONFIG)
5858
# base_config.update(load_config(TECH_CONFIG))
5959
base_config.update(kwargs)
@@ -76,7 +76,6 @@ def make(
7676
test_config["run"]["name"] = TESTS_RUNNAME
7777
test_config["run"]["is_test"] = True
7878
test_config["paths"]["costs_dir"] = None
79-
8079

8180
# mock the atlite cutout config
8281
test_config["atlite"]["cutout_name"] = TESTS_CUTOUT
@@ -94,7 +93,6 @@ def make_test_config_file(make_snakemake_test_config, tmpdir_factory, request):
9493
"""Fixture to save a temp config file for testing, return its path,
9594
and clean up after module.
9695
"""
97-
9896
# Get parameters passed via pytest.mark.parametrize
9997
time_res = request.param.get("time_res", 24)
10098
plan_year = request.param.get("plan_year", 2040)

0 commit comments

Comments
 (0)