pypsa-eur icon indicating copy to clipboard operation
pypsa-eur copied to clipboard

define extra_funcitonality inside solve_network to capture snakemake object

Open lindnemi opened this issue 5 months ago • 2 comments

At the moment the solve_network function can not be imported from other scripts, because the snakemake object required by extra_functionality is a global variable.

This PR defines the extra_functionality within solve_network, and adds the snakemake object as an additional argument to solve_network, s.t. the snakemake object can be captured from the scope of solve_network instead of the global scope. This allows imports of the solve_network function from other scripts.

For an application see: https://github.com/PyPSA/pypsa-de/pull/103

Before completing the PR i'd like to hear some opinions @fneum @lkstrp

Closes # (if applicable).

Changes proposed in this Pull Request

Checklist

  • [ ] I tested my contribution locally and it works as intended.
  • [ ] Code and workflow changes are sufficiently documented.
  • [ ] Changed dependencies are added to envs/environment.yaml.
  • [ ] Changes in configuration options are added in config/config.default.yaml.
  • [ ] Changes in configuration options are documented in doc/configtables/*.csv.
  • [ ] Sources of newly added data are documented in doc/data_sources.rst.
  • [ ] A release note doc/release_notes.rst is added.

lindnemi avatar Jul 31 '25 10:07 lindnemi

Or you make an explicit argument to extra_functionality:

def extra_functionality(
    n: pypsa.Network, snapshots: pd.DatetimeIndex, planning_horizons: str | None = None, snakemake: Snakemake
) -> None:

and then pass it to n.optimize with:

n.optimize(..., extra_functionality=partial(extra_functionality, snakemake=snakemake))

coroa avatar Jul 31 '25 10:07 coroa

I like the partial suggestion

While we are at it: If snakemake is passed to solve_network should we get rid of all the other keywords as well?

lindnemi avatar Jul 31 '25 13:07 lindnemi