Skip to content

add statistics-function for variable Final Energy [by Sector]|Agriculture#18

Open
maxnutz wants to merge 2 commits intomainfrom
13-new-variable-statistics-final-energy-by-sectoragriculture
Open

add statistics-function for variable Final Energy [by Sector]|Agriculture#18
maxnutz wants to merge 2 commits intomainfrom
13-new-variable-statistics-final-energy-by-sectoragriculture

Conversation

@maxnutz
Copy link
Copy Markdown
Owner

@maxnutz maxnutz commented Mar 26, 2026

  • workflow processes
  • output is constant over all investment periods
  • tests succeed
  • latest version of master is merged into feature branch

Agriculture includes carriers ['agriculture electricity','agriculture heat','agriculture machinery', 'agriculture machinery oil'] evaluated on Load-Components.

The carriers could be easily identified with

carriers = list(n.loads[n.loads.index.str.contains("agriculture")].carrier.unique())

but as other functions are also based on hard-coded carrier names, it seems safer to also keep it static.

Summary by Sourcery

Add support for computing and exposing final energy consumption for the agriculture sector.

New Features:

  • Introduce a statistics function to calculate agriculture-sector final energy from PyPSA networks.
  • Expose the agriculture-sector final energy metric through the default mapping configuration.

Tests:

  • Add a dedicated test suite validating the agriculture final energy statistics function for single and multiple networks.

@maxnutz maxnutz linked an issue Mar 26, 2026 that may be closed by this pull request
7 tasks
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Mar 26, 2026

Reviewer's Guide

Adds a new statistics function to compute final energy consumption for the agriculture sector, wires it into the mapping configuration, and introduces a focused test suite mirroring the existing transportation-sector tests.

Flow diagram for Final_Energy_by_Sector__Agriculture statistics function

flowchart TD
    Start([Start Final_Energy_by_Sector__Agriculture])
    InputN["Input n: pypsa.Network"]
    DefineCarriers["Define carriers = [agriculture electricity, agriculture heat, agriculture machinery electric, agriculture machinery oil]"]
    CallStats["Call n.statistics.energy_balance with\ncarrier=carriers\ngroupby=[carrier, unit, country]\ncomponents=Load\ndirection=withdrawal"]
    GroupSum["Group result by [country, unit]\nand sum over all carriers"]
    ReturnRes(["Return aggregated pd.Series res"]) 

    Start --> InputN --> DefineCarriers --> CallStats --> GroupSum --> ReturnRes
Loading

File-Level Changes

Change Details Files
Introduce Final_Energy_by_Sector__Agriculture statistics function for agriculture-sector final energy calculation.
  • Define Final_Energy_by_Sector__Agriculture taking a pypsa.Network and returning a pandas Series with MultiIndex ['country','unit'].
  • Specify a static list of agriculture-related carriers and call n.statistics.energy_balance filtered by those carriers, grouped by ['carrier','unit','country'], component 'Load', and direction 'withdrawal'.
  • Aggregate the energy balance by ['country','unit'] and return the summed result.
pypsa_validation_processing/statistics_functions.py
Register the new agriculture-sector statistics function in the default mapping configuration.
  • Add a mapping entry for 'Final Energy [by Sector]
Agriculture' that points to Final_Energy_by_Sector__Agriculture.
Add tests covering the new Final_Energy_by_Sector__Agriculture function.
  • Create TestFinalEnergyBySectorAgriculture test class mirroring the transportation tests for a single mock network and a mock network collection.
  • Assert that the function returns a non-empty pandas Series with MultiIndex ['country','unit'], numeric values, and that it includes country 'AT'.
  • Verify behavior across multiple networks by iterating over a MockNetworkCollection and checking the shape and types of the results.
tests/test_statistics_functions.py

Assessment against linked issues

Issue Objective Addressed Explanation
#13 Add a new statistics function Final_Energy_by_Sector__Agriculture to statistics_functions.py, following existing naming/structural conventions and including a comprehensive docstring.
#13 Add the mapping from `Final Energy [by Sector] AgriculturetoFinal_Energy_by_Sector__Agriculture` in the default mapping configuration file.
#13 Add a testing routine for Final_Energy_by_Sector__Agriculture under tests/ in line with the existing testing structure.

Possibly linked issues

  • #0: PR adds Final_Energy_by_Sector__Agriculture, its config mapping, and tests, directly implementing the issue’s requested feature.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 2 issues, and left some high level feedback:

  • The docstring for Final_Energy_by_Sector__Agriculture still mentions "transportation sector" in the Returns description, which is misleading for this function and should be updated to refer to the agriculture sector.
  • In Final_Energy_by_Sector__Agriculture the notes mention that agriculture machinery oil is also a carrier of Links and Buses, but the actual call uses components="Load"; consider either expanding the components argument or adjusting the note so it matches the implemented behavior.
  • The test_numeric_values assertion can be simplified by directly using pd.api.types.is_numeric_dtype(result.dtype) instead of combining it with explicit checks against float and int, which are redundant for pandas dtypes.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The docstring for `Final_Energy_by_Sector__Agriculture` still mentions "transportation sector" in the Returns description, which is misleading for this function and should be updated to refer to the agriculture sector.
- In `Final_Energy_by_Sector__Agriculture` the notes mention that `agriculture machinery oil` is also a carrier of Links and Buses, but the actual call uses `components="Load"`; consider either expanding the components argument or adjusting the note so it matches the implemented behavior.
- The `test_numeric_values` assertion can be simplified by directly using `pd.api.types.is_numeric_dtype(result.dtype)` instead of combining it with explicit checks against `float` and `int`, which are redundant for pandas dtypes.

## Individual Comments

### Comment 1
<location path="pypsa_validation_processing/statistics_functions.py" line_range="132-133" />
<code_context>
+
+    Notes
+    -----
+    Includes carriers ['agriculture electricity','agriculture heat','agriculture machinery electric',
+    'agriculture machinery oil'] executed on Load-Components. agriculture machinery oil is also carrier
+    of Links and Buses, as Demand is assued fixed. _Timeseries of Agriculture demand are assumed
+    to be constant in PyPSA-EUR._
</code_context>
<issue_to_address>
**question:** Docstring mentions additional components for 'agriculture machinery oil' but implementation only uses Load.

Since `n.statistics.energy_balance` is called with `components="Load"`, this metric excludes Link/Bus usage of `agriculture machinery oil`. Please either adjust the wording about Links/Buses to avoid implying they are included here, or explicitly note that only load-side demand is captured by this statistic.
</issue_to_address>

### Comment 2
<location path="pypsa_validation_processing/statistics_functions.py" line_range="130-135" />
<code_context>
+    Notes
+    -----
+    Includes carriers ['agriculture electricity','agriculture heat','agriculture machinery electric',
+    'agriculture machinery oil'] executed on Load-Components. agriculture machinery oil is also carrier
+    of Links and Buses, as Demand is assued fixed. _Timeseries of Agriculture demand are assumed
+    to be constant in PyPSA-EUR._
+    """
</code_context>
<issue_to_address>
**nitpick (typo):** Clean up spelling and capitalization in the docstring for clarity.

Specifically: change "assued" → "assumed", "Timeseries" → "time series", and capitalize "agriculture machinery oil" at the start of the sentence for consistency.

```suggestion
    Notes
    -----
    Includes carriers ['agriculture electricity','agriculture heat','agriculture machinery electric',
    'agriculture machinery oil'] executed on Load-Components. Agriculture machinery oil is also carrier
    of Links and Buses, as Demand is assumed fixed. _Time series of Agriculture demand are assumed
    to be constant in PyPSA-EUR._
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@maxnutz maxnutz changed the title add statistics-function and mapping; include test class for function add statistics-function for variable Final Energy [by Sector]|Agriculture Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New Variable Statistics: Final Energy [by Sector]|Agriculture

1 participant