Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: modify copy commit processing rule #237

Merged
merged 13 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions Snakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from os.path import exists
from shutil import copyfile, move
from scripts.helpers import get_last_commit_message

from snakemake.remote.HTTP import RemoteProvider as HTTPRemoteProvider

Expand All @@ -19,6 +20,9 @@ SDIR = config["summary_dir"] + config["run"]
RDIR = config["results_dir"] + config["run"]
CDIR = config["costs_dir"]

config.update({"git_commit": get_last_commit_message(".")})
config.update({"submodule_commit": get_last_commit_message("./pypsa-earth")})

CUTOUTS_PATH = (
"cutouts/cutout-2013-era5-tutorial.nc"
if config["tutorial"]
Expand Down Expand Up @@ -411,15 +415,6 @@ rule copy_config:
"scripts/copy_config.py"


rule copy_commit:
output:
SDIR + "/commit_info.txt",
shell:
"""
git log -n 1 --pretty=format:"Commit: %H%nAuthor: %an <%ae>%nDate: %ad%nMessage: %s" > {output}
"""


rule solve_network:
input:
overrides="data/override_component_attrs",
Expand All @@ -429,7 +424,6 @@ rule solve_network:
+ "/prenetworks/elec_s{simpl}_{clusters}_ec_l{ll}_{opts}_{sopts}_{planning_horizons}_{discountrate}_{demand}_{h2export}export.nc",
costs=CDIR + "costs_{planning_horizons}.csv",
configs=SDIR + "/configs/config.yaml", # included to trigger copy_config rule
commit=SDIR + "/commit_info.txt",
output:
RDIR
+ "/postnetworks/elec_s{simpl}_{clusters}_ec_l{ll}_{opts}_{sopts}_{planning_horizons}_{discountrate}_{demand}_{h2export}export.nc",
Expand Down
25 changes: 25 additions & 0 deletions scripts/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import os
import shutil
import subprocess
import zipfile
from pathlib import Path

Expand Down Expand Up @@ -565,3 +566,27 @@ def locate_bus(
return gdf_co[gdf_co.geometry == min(gdf_co.geometry, key=(point.distance))][
col
].item() # looks for closest one shape=node


def get_last_commit_message(path):
"""
Function to get the last PyPSA-Earth Git commit message
Returns
-------
result : string
"""
_logger = logging.getLogger(__name__)
try:
last_commit_message = (
subprocess.check_output(
["git", "log", "-n", "1", "--pretty=format:%H %s"],
cwd=path,
stderr=subprocess.STDOUT,
)
.decode()
.strip()
)
return last_commit_message
except subprocess.CalledProcessError as e:
_logger.warning(f"Error executing Git: {e}")
return None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the documentation, it seems that the cwd option overrides the cwd.

It may be a good idea to create a backup of cwd and restore it after the execution in an error-proven way.

To do this, for example, we may:

  1. before the try, initialize the last_commit_message to None and store cwd in a local variable
  2. in the try/except, drop the return statements
  3. after the try/except, restore the original cwd path
  4. return the last_commit_message

This may be worth trying to check if it solves the problem.
It may be good to reflect these changes into pypsa-earth as well, once validated.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @davide-f ,
thanks for the solution approach. Sadly it seems not to work.
Do you have any other ideas? Im pretty much empty with solution ideas.

5 changes: 3 additions & 2 deletions scripts/solve_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,11 +481,11 @@ def add_existing(n):
snakemake = mock_snakemake(
"solve_network",
simpl="",
clusters="12",
clusters="4",
ll="c1.0",
opts="Co2L",
planning_horizons="2030",
sopts="24H",
sopts="144H",
discountrate=0.071,
demand="DF",
h2export="120",
Expand Down Expand Up @@ -539,6 +539,7 @@ def add_existing(n):
solver_logfile=snakemake.log.solver,
)

n.meta = dict(snakemake.config, **dict(wildcards=dict(snakemake.wildcards)))
n.export_to_netcdf(snakemake.output[0])

logger.info("Maximum memory usage: {}".format(mem.mem_usage))
Loading