Skip to content

Commit 771a489

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 35ce5b0 commit 771a489

3 files changed

Lines changed: 82 additions & 85 deletions

File tree

pypsa_eur_cli/pypsa_eur/cli_helpers.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import hashlib
2-
import json
32
import time
43
from datetime import datetime
54
from pathlib import Path
@@ -13,27 +12,27 @@ def generate_job_id(configfile: Path | None = None, name: str | None = None) ->
1312
if name:
1413
# If name is provided via CLI, use it directly
1514
return name
16-
15+
1716
if configfile and configfile.exists():
1817
# Load the configuration file
1918
with open(configfile) as f:
2019
config_data = yaml.safe_load(f)
21-
20+
2221
# Extract the original run name from the config
2322
original_run_name = config_data.get("run", {}).get("name", "pypsa_eur")
2423
else:
2524
original_run_name = "pypsa_eur"
26-
25+
2726
# Generate timestamp-based unique ID
2827
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
29-
28+
3029
# Create a short hash for uniqueness
3130
hash_input = f"{original_run_name}{timestamp}{time.time()}"
3231
short_hash = hashlib.sha256(hash_input.encode()).hexdigest()[:6]
33-
32+
3433
# Combine everything
3534
final_job_id = f"{timestamp}_{original_run_name}_{short_hash}"
36-
35+
3736
return final_job_id
3837

3938

@@ -76,4 +75,4 @@ def recursive_merge(d1: dict, d2: dict) -> dict:
7675
recursive_merge(d1[key], value) # Recursively merge dictionaries
7776
else:
7877
d1[key] = value # Overwrite or add the value
79-
return d1
78+
return d1

pypsa_eur_cli/pypsa_eur/main.py

Lines changed: 65 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
import os
21
import subprocess
32
import tempfile
43
from pathlib import Path
5-
from typing import Optional
64

75
import typer
86
import yaml
7+
from tqdm import tqdm
8+
99
from pypsa_eur.cli_helpers import (
1010
check_in_project_root,
1111
comma_separated_list,
1212
generate_job_id,
1313
recursive_merge,
1414
)
1515
from pypsa_eur.progress_tracker import ProgressTracker
16-
from snakemake.utils import validate
17-
from tqdm import tqdm
1816

1917
app = typer.Typer(
2018
help="PyPSA-Eur CLI: Command Line Interface for European Power System Analysis",
@@ -24,65 +22,65 @@
2422

2523
@app.command(name="run")
2624
def run(
27-
name: str = typer.Option(
28-
None,
29-
"--name",
30-
"-n",
31-
help="Name for this run (will be used as job ID).",
32-
),
33-
configfile: Path = typer.Option(
34-
None,
35-
"--configfile",
36-
"-c",
37-
help="Path to configuration file (default: config/config.default.yaml).",
38-
),
39-
verbose: bool = typer.Option(
40-
False,
41-
"--verbose",
42-
"-v",
43-
help="Show live Snakemake output during workflow execution.",
44-
),
45-
cores: int = typer.Option(
46-
None,
47-
"--cores",
48-
help="Number of cores to use for Snakemake. If not specified, all available cores will be used.",
49-
),
50-
snapshots: str = typer.Option(
51-
None,
52-
"--snapshots",
53-
"-s",
54-
help="Override snapshots configuration (format: 'start,end' e.g., '2013-01-01,2014-01-01').",
55-
),
56-
planning_horizons: str = typer.Option(
57-
None,
58-
"--planning-horizons",
59-
help="Override planning horizons as comma-separated list (e.g., 2030,2040,2050).",
60-
),
61-
clusters: str = typer.Option(
62-
None,
63-
"--clusters",
64-
help="Override network clusters as comma-separated list (e.g., 39,128,256).",
65-
),
66-
opts: str = typer.Option(
67-
None,
68-
"--opts",
69-
help="Override opts configuration as comma-separated list.",
70-
),
71-
sector_opts: str = typer.Option(
72-
None,
73-
"--sector-opts",
74-
help="Override sector_opts configuration as comma-separated list.",
75-
),
76-
foresight: str = typer.Option(
77-
None,
78-
"--foresight",
79-
help="Override foresight configuration (overnight, myopic, or perfect).",
80-
),
81-
countries: str = typer.Option(
82-
None,
83-
"--countries",
84-
help="Override countries configuration as comma-separated list (e.g., DE,FR,ES).",
85-
),
25+
name: str = typer.Option(
26+
None,
27+
"--name",
28+
"-n",
29+
help="Name for this run (will be used as job ID).",
30+
),
31+
configfile: Path = typer.Option(
32+
None,
33+
"--configfile",
34+
"-c",
35+
help="Path to configuration file (default: config/config.default.yaml).",
36+
),
37+
verbose: bool = typer.Option(
38+
False,
39+
"--verbose",
40+
"-v",
41+
help="Show live Snakemake output during workflow execution.",
42+
),
43+
cores: int = typer.Option(
44+
None,
45+
"--cores",
46+
help="Number of cores to use for Snakemake. If not specified, all available cores will be used.",
47+
),
48+
snapshots: str = typer.Option(
49+
None,
50+
"--snapshots",
51+
"-s",
52+
help="Override snapshots configuration (format: 'start,end' e.g., '2013-01-01,2014-01-01').",
53+
),
54+
planning_horizons: str = typer.Option(
55+
None,
56+
"--planning-horizons",
57+
help="Override planning horizons as comma-separated list (e.g., 2030,2040,2050).",
58+
),
59+
clusters: str = typer.Option(
60+
None,
61+
"--clusters",
62+
help="Override network clusters as comma-separated list (e.g., 39,128,256).",
63+
),
64+
opts: str = typer.Option(
65+
None,
66+
"--opts",
67+
help="Override opts configuration as comma-separated list.",
68+
),
69+
sector_opts: str = typer.Option(
70+
None,
71+
"--sector-opts",
72+
help="Override sector_opts configuration as comma-separated list.",
73+
),
74+
foresight: str = typer.Option(
75+
None,
76+
"--foresight",
77+
help="Override foresight configuration (overnight, myopic, or perfect).",
78+
),
79+
countries: str = typer.Option(
80+
None,
81+
"--countries",
82+
help="Override countries configuration as comma-separated list (e.g., DE,FR,ES).",
83+
),
8684
):
8785
"""
8886
Run the PyPSA-Eur workflow with specified configuration.
@@ -106,11 +104,9 @@ def run(
106104
"retrieve_ship_raster": "Retrieving shipping raster data",
107105
"retrieve_databundle": "Retrieving data bundle",
108106
"retrieve_egrigcs3_pm_production": "Retrieving EGRIGCS3 power production data",
109-
110107
# Shape and geography building
111108
"build_shapes": "Building geographical shapes",
112109
"build_ship_raster": "Building shipping density raster",
113-
114110
# Network building and preparation
115111
"base_network": "Building base network structure",
116112
"build_osm_network": "Building network from OpenStreetMap data",
@@ -120,15 +116,13 @@ def run(
120116
"cluster_network": "Clustering network nodes",
121117
"add_transmission_projects_and_dlr": "Adding transmission projects and dynamic line rating",
122118
"build_transmission_projects": "Building transmission projects",
123-
124119
# Electricity system components
125120
"build_electricity_demand": "Building electricity demand time series",
126121
"build_electricity_demand_base": "Building base electricity demand",
127122
"build_powerplants": "Building power plant database",
128123
"build_line_rating": "Building dynamic line ratings",
129124
"add_electricity": "Adding electrical parameters",
130125
"prepare_network": "Preparing network for optimization",
131-
132126
# Renewable energy
133127
"build_renewable_profiles": "Building renewable generation profiles",
134128
"determine_availability_matrix": "Determining renewable availability matrix",
@@ -137,13 +131,11 @@ def run(
137131
"build_temperature_profiles": "Building temperature profiles",
138132
"build_cop_profiles": "Building heat pump COP profiles",
139133
"build_solar_thermal_profiles": "Building solar thermal profiles",
140-
141134
# Solving and optimization
142135
"solve_network": "Solving network optimization",
143136
"solve_operations_network": "Solving operational dispatch",
144137
"solve_elec_networks": "Solving electricity network",
145138
"solve_sector_network": "Solving sector-coupled network",
146-
147139
# Sector coupling
148140
"prepare_sector_network": "Preparing sector-coupled network",
149141
"build_industrial_demand": "Building industrial demand",
@@ -153,13 +145,11 @@ def run(
153145
"build_industry_sector": "Building industry sector model",
154146
"build_heating_sector": "Building heating sector model",
155147
"build_transport_sector": "Building transport sector model",
156-
157148
# Post-processing and validation
158149
"make_summary": "Creating results summary",
159150
"plot_network": "Plotting network",
160151
"plot_summary": "Plotting summary statistics",
161152
"validate_network": "Validating network consistency",
162-
163153
# Other rules
164154
"copy_config": "Copying configuration",
165155
"build_cutout": "Building weather cutout",
@@ -221,7 +211,9 @@ def run(
221211
config_data.setdefault("costs", {})["year"] = ph_list[0]
222212

223213
if clusters:
224-
config_data.setdefault("scenario", {})["clusters"] = comma_separated_list(clusters)
214+
config_data.setdefault("scenario", {})["clusters"] = comma_separated_list(
215+
clusters
216+
)
225217

226218
if opts:
227219
config_data.setdefault("scenario", {})["opts"] = comma_separated_list(opts)
@@ -444,4 +436,4 @@ def callback():
444436

445437

446438
if __name__ == "__main__":
447-
app()
439+
app()

pypsa_eur_cli/pypsa_eur/progress_tracker.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import re
22
import sys
3-
from pathlib import Path
43

54
from colorama import Fore, Style
65
from tqdm import tqdm
@@ -117,7 +116,12 @@ def parse_line(self, line):
117116
if "rule " in line and ":" in line:
118117
parts = line.split()
119118
for i, part in enumerate(parts):
120-
if part in ["rule", "localrule", "checkpoint", "localcheckpoint"] and i + 1 < len(parts):
119+
if part in [
120+
"rule",
121+
"localrule",
122+
"checkpoint",
123+
"localcheckpoint",
124+
] and i + 1 < len(parts):
121125
rule_name = parts[i + 1].strip(":")
122126
statement_identifier = f"{rule_name}_detected"
123127
if statement_identifier not in self.printed_statements:
@@ -150,7 +154,9 @@ def parse_line(self, line):
150154

151155
if statement_identifier not in self.printed_statements:
152156
# Print wildcard details as additional info
153-
tqdm.write(f" {Fore.YELLOW}└─ with {wildcards_str}{Style.RESET_ALL}")
157+
tqdm.write(
158+
f" {Fore.YELLOW}└─ with {wildcards_str}{Style.RESET_ALL}"
159+
)
154160
self.printed_statements.add(statement_identifier)
155161

156162
return True
@@ -171,4 +177,4 @@ def clean_up(self):
171177

172178
def __repr__(self):
173179
"""Return a string representation of the progress tracker state."""
174-
return f"ProgressTracker()"
180+
return "ProgressTracker()"

0 commit comments

Comments
 (0)