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

Enable configfile specification for mock_snakemake #1135

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions doc/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ E.g. if a new rule becomes available describe how to use it `make test` and in o

* Include a dedicated cutout for Europe in bundle_config.yaml `PR #1125 <https://github.com/pypsa-meets-earth/pypsa-earth/pull/1125>`_

* Enable configfile specification for mock_snakemake `PR #1135 <https://github.com/pypsa-meets-earth/pypsa-earth/pull/1135>`_

PyPSA-Earth 0.4.1
=================

Expand Down
20 changes: 17 additions & 3 deletions scripts/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,9 @@ def get_aggregation_strategies(aggregation_strategies):
return bus_strategies, generator_strategies


def mock_snakemake(rulename, root_dir=None, submodule_dir=None, **wildcards):
def mock_snakemake(
rulename, root_dir=None, submodule_dir=None, configfile=None, **wildcards
):
"""
This function is expected to be executed from the "scripts"-directory of "
the snakemake project. It returns a snakemake.script.Snakemake object,
Expand All @@ -534,6 +536,8 @@ def mock_snakemake(rulename, root_dir=None, submodule_dir=None, **wildcards):
----------
rulename: str
name of the rule for which the snakemake object should be generated
configfile: str
path to config file to be used in mock_snakemake
wildcards:
keyword arguments fixing the wildcards. Only necessary if wildcards are
needed.
Expand Down Expand Up @@ -566,9 +570,19 @@ def mock_snakemake(rulename, root_dir=None, submodule_dir=None, **wildcards):
if os.path.exists(p):
snakefile = p
break

if isinstance(configfile, str):
with open(configfile, "r") as file:
configfile = yaml.safe_load(file)
else:
Copy link
Member

Choose a reason for hiding this comment

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

Rather than else, maybe "elif not isinstance(configfile, dict)" or drop the if case?
By default it is none, so the else case may not be necessary but a dictionary may be provided as well as input.
What do you think?

Almost ready to merge :D

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@davide-f, thanks for the feedback. I have initially meant to omit else case, because default if None. But then I thought what if the user gives something other than str, it can cause error. Therefore I have added additional else with None. Similar thing I have noticed in PyPSA-Eur. Maybe it is better then drop else case?

Copy link
Member

Choose a reason for hiding this comment

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

@davide-f, thanks for the feedback. I have initially meant to omit else case, because default if None. But then I thought what if the user gives something other than str, it can cause error. Therefore I have added additional else with None. Similar thing I have noticed in PyPSA-Eur.

Let's drop it :) that function is not used by the normal user.
If provided a wrong input, probably it is safe for the code to through an error rather than silently fix it.
Great contribution!

Copy link
Collaborator Author

@yerbol-akhmetov yerbol-akhmetov Oct 13, 2024

Choose a reason for hiding this comment

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

@davide-f, ok, thanks, understood.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I have dropped else condition, while keeping if, so to make sure, config is read only if the path is given as string.

configfile = None

workflow = sm.Workflow(
snakefile, overwrite_configfiles=[], rerun_triggers=[]
) # overwrite_config=config
snakefile,
overwrite_configfiles=[],
rerun_triggers=[],
overwrite_config=configfile,
)
workflow.include(snakefile)
workflow.global_resources = {}
try:
Expand Down
Loading