Skip to content

Commit 6f7fcee

Browse files
authored
Fix and add pre-commit checks (#66)
* add config * first run * Fix and add pre-commit
1 parent 281c32a commit 6f7fcee

File tree

57 files changed

+252
-417
lines changed

Some content is hidden

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

57 files changed

+252
-417
lines changed

.github/workflows/test.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
- name: Setup cache keys
6161
run: |
6262
echo "WEEK=$(date +'%Y%U')" >> $GITHUB_ENV # data and cutouts
63-
63+
6464
- uses: actions/cache@v4
6565
with:
6666
path: |
@@ -86,7 +86,7 @@ jobs:
8686
run: |
8787
conda env update -n pypsa-de -f ${{ env.env_file }}
8888
echo "Run conda list" && conda list
89-
89+
9090
- name: Setup ixmp4 access
9191
run: |
9292
mkdir -p ~/.local/share/ixmp4/
@@ -158,7 +158,7 @@ jobs:
158158
else
159159
echo "pinned=false" >> $GITHUB_ENV
160160
fi
161-
161+
162162
- name: Setup cache keys
163163
if: env.pinned == 'false'
164164
run: |
@@ -218,4 +218,4 @@ jobs:
218218
logs
219219
.snakemake/log
220220
results
221-
retention-days: 3
221+
retention-days: 3

.pre-commit-config.yaml

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,53 @@
11
ci:
22
autoupdate_schedule: monthly
33

4+
exclude: "^LICENSES"
5+
46
repos:
57
- repo: https://github.com/pre-commit/pre-commit-hooks
68
rev: v5.0.0
79
hooks:
10+
- id: check-merge-conflict
811
- id: check-added-large-files
9-
args: ['--maxkb=2000']
12+
args: ["--maxkb=2000"]
13+
14+
# Run ruff to lint and format
15+
- repo: https://github.com/astral-sh/ruff-pre-commit
16+
# Ruff version.
17+
rev: v0.11.2
18+
hooks:
19+
# Run the linter.
20+
- id: ruff
21+
args: [--fix]
22+
# Run the formatter.
23+
- id: ruff-format
24+
25+
# Find common spelling mistakes in comments and docstrings
26+
- repo: https://github.com/codespell-project/codespell
27+
rev: v2.4.1
28+
hooks:
29+
- id: codespell
30+
args: ['--ignore-regex="(\b[A-Z]+\b)"', '--ignore-words-list=fom,appartment,bage,ore,setis,tabacco,berfore,vor,pris,GuD,Dezember,Juni,Juli,Produktion,WorstCase'] # Ignore capital case words, e.g. country codes
31+
types_or: [python, rst, markdown]
32+
files: ^(scripts|doc)/
33+
34+
# Remove output from Jupyter notebooks
35+
- repo: https://github.com/aflc/pre-commit-jupyter
36+
rev: v1.2.1
37+
hooks:
38+
- id: jupyter-notebook-cleanup
39+
args: ['--remove-kernel-metadata']
40+
41+
# YAML formatting
42+
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
43+
rev: v2.14.0
44+
hooks:
45+
- id: pretty-format-yaml
46+
exclude: pinned\.yaml$
47+
args: [--autofix, --indent, "2", --preserve-quotes]
48+
49+
# Format Snakemake rule / workflow files
50+
- repo: https://github.com/snakemake/snakefmt
51+
rev: v0.11.0
52+
hooks:
53+
- id: snakefmt

Snakefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,9 @@ rule modify_prenetwork:
332332
scale_capacity=config_provider("scale_capacity"),
333333
input:
334334
costs_modifications="ariadne-data/costs_{planning_horizons}-modifications.csv",
335-
network=resources("networks/base_s_{clusters}_{opts}_{sector_opts}_{planning_horizons}_brownfield.nc"),
335+
network=resources(
336+
"networks/base_s_{clusters}_{opts}_{sector_opts}_{planning_horizons}_brownfield.nc"
337+
),
336338
wkn=lambda w: (
337339
resources("wasserstoff_kernnetz_base_s_{clusters}.csv")
338340
if config_provider("wasserstoff_kernnetz", "enable")(w)

config/test/config.dach.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@ electricity:
5454
solving:
5555
solver:
5656
name: highs
57-
options: highs-simplex
57+
options: highs-simplex

scripts/_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def get_scenarios(run):
4747
fn = Path(scenario_config["file"])
4848
if fn.exists():
4949
scenarios = yaml.safe_load(fn.read_text())
50-
if scenarios == None:
50+
if scenarios is None:
5151
print(
5252
"WARNING! Scenario management enabled but scenarios file appears to be empty."
5353
)

scripts/add_brownfield.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@
1212
import pypsa
1313
import xarray as xr
1414
from add_existing_baseyear import add_build_year_to_new_assets
15-
from pypsa.clustering.spatial import normed_or_uniform
1615

1716
from scripts._helpers import (
1817
configure_logging,
1918
get_snapshots,
2019
set_scenario_config,
2120
update_config_from_wildcards,
2221
)
23-
from add_existing_baseyear import add_build_year_to_new_assets
2422

2523
logger = logging.getLogger(__name__)
2624
idx = pd.IndexSlice

scripts/add_existing_baseyear.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
"""
88

99
import logging
10-
import os
11-
import sys
1210
from types import SimpleNamespace
1311

1412
import country_converter as coco
@@ -25,9 +23,8 @@
2523
update_config_from_wildcards,
2624
)
2725
from scripts.add_electricity import sanitize_carriers
28-
from scripts.definitions.heat_sector import HeatSector
26+
from scripts.build_powerplants import add_custom_powerplants
2927
from scripts.definitions.heat_system import HeatSystem
30-
from scripts.definitions.heat_system_type import HeatSystemType
3128
from scripts.prepare_sector_network import (
3229
cluster_heat_buses,
3330
define_spatial,
@@ -39,8 +36,6 @@
3936
idx = pd.IndexSlice
4037
spatial = SimpleNamespace()
4138

42-
from scripts.build_powerplants import add_custom_powerplants
43-
4439

4540
def add_build_year_to_new_assets(n: pypsa.Network, baseyear: int) -> None:
4641
"""
@@ -500,8 +495,8 @@ def add_chp_plants(n, grouping_years, costs, baseyear):
500495

501496
# drop assets which are already phased out / decommissioned
502497
# drop hydro, waste and oil fueltypes for CHP
503-
limit = np.max(grouping_years)
504-
drop_fueltypes = ["Hydro", "Other", "Waste", "nicht biogener Abfall"]
498+
limit = np.max(grouping_years) # noqa
499+
drop_fueltypes = ["Hydro", "Other", "Waste", "nicht biogener Abfall"] # noqa
505500
chp = ppl.query(
506501
"Set == 'CHP' and (DateOut >= @baseyear or DateOut != DateOut) and (DateIn <= @limit or DateIn != DateIn) and Fueltype not in @drop_fueltypes"
507502
).copy()
@@ -516,7 +511,9 @@ def add_chp_plants(n, grouping_years, costs, baseyear):
516511
chp["lifetime"] = (chp.DateOut - chp["grouping_year"] + 1).fillna(
517512
snakemake.params.costs["fill_values"]["lifetime"]
518513
)
519-
chp = chp.loc[chp.grouping_year + chp.lifetime > baseyear] # in add_brownfield this is build_year + lifetime <= baseyear
514+
chp = chp.loc[
515+
chp.grouping_year + chp.lifetime > baseyear
516+
] # in add_brownfield this is build_year + lifetime <= baseyear
520517

521518
# check if the CHPs were read in from MaStR for Germany
522519
if "Capacity_thermal" in chp.columns:
@@ -1054,7 +1051,6 @@ def add_heating_capacities_installed_before_baseyear(
10541051
)
10551052

10561053
# delete links with capacities below threshold
1057-
threshold = snakemake.params.existing_capacities["threshold_capacity"]
10581054
n.remove(
10591055
"Link",
10601056
[

scripts/build_daily_heat_demand.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
import geopandas as gpd
2121
import numpy as np
2222
import xarray as xr
23-
from scripts._helpers import configure_logging, get_snapshots, set_scenario_config
2423
from dask.distributed import Client, LocalCluster
2524

25+
from scripts._helpers import configure_logging, get_snapshots, set_scenario_config
26+
2627
logger = logging.getLogger(__name__)
2728

2829

scripts/build_hac_features.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99

1010
import atlite
1111
import geopandas as gpd
12-
from scripts._helpers import configure_logging, get_snapshots, set_scenario_config
1312
from atlite.aggregate import aggregate_matrix
1413
from dask.distributed import Client
1514

15+
from scripts._helpers import configure_logging, get_snapshots, set_scenario_config
16+
1617
logger = logging.getLogger(__name__)
1718

1819
if __name__ == "__main__":

scripts/build_hourly_heat_demand.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414

1515
import pandas as pd
1616
import xarray as xr
17+
1718
from scripts._helpers import (
1819
configure_logging,
1920
generate_periodic_profiles,
2021
get_snapshots,
22+
mock_snakemake,
2123
set_scenario_config,
2224
)
2325

0 commit comments

Comments
 (0)