Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions pypsa_validation_processing/configs/mapping.default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@

Final Energy [by Carrier]|Electricity: Final_Energy_by_Carrier__Electricity
Final Energy [by Sector]|Transportation: Final_Energy_by_Sector__Transportation
Final Energy [by Sector]|Agriculture: Final_Energy_by_Sector__Agriculture
42 changes: 42 additions & 0 deletions pypsa_validation_processing/statistics_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,45 @@ def Final_Energy_by_Sector__Transportation(
.sum()
)
return res


def Final_Energy_by_Sector__Agriculture(n: pypsa.Network) -> pd.Series:
"""Extract agriculture-sector final energy from a PyPSA Network.

Returns the total energy consumed by the transportation sector (excluding
transmission / distribution losses) across the pypsa-network.

Parameters
----------
n : pypsa.Network
PyPSA network to process.

Returns
-------
pd.Series
Pandas Series with Multiindex of ``country`` and ``unit``

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._
"""
carriers = [
"agriculture electricity",
"agriculture heat",
"agriculture machinery electric",
"agriculture machinery oil",
]
res = (
n.statistics.energy_balance(
carrier=carriers,
groupby=["carrier", "unit", "country"],
components="Load",
direction="withdrawal", # for positive values
)
.groupby(["country", "unit"])
.sum()
)
return res
47 changes: 47 additions & 0 deletions tests/test_statistics_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from pypsa_validation_processing.statistics_functions import (
Final_Energy_by_Carrier__Electricity,
Final_Energy_by_Sector__Agriculture,
Final_Energy_by_Sector__Transportation,
)

Expand Down Expand Up @@ -103,3 +104,49 @@ def test_multiple_networks(self, mock_network_collection: MockNetworkCollection)
assert isinstance(result.index, pd.MultiIndex)
assert result.index.names == ["country", "unit"]
assert len(result) > 0


# ---------------------------------------------------------------------------
# Tests for Final_Energy_by_Sector__Agriculture
# ---------------------------------------------------------------------------


class TestFinalEnergyBySectorAgriculture:
"""Test suite for Final_Energy_by_Sector__Agriculture function."""

def test_returns_series(self, mock_network: MockPyPSANetwork):
"""Test that the function returns a pandas Series."""
result = Final_Energy_by_Sector__Agriculture(mock_network)
assert isinstance(result, pd.Series)

def test_has_country_and_unit_multiindex(self, mock_network: MockPyPSANetwork):
"""Test that result has MultiIndex with country and unit levels."""
result = Final_Energy_by_Sector__Agriculture(mock_network)
assert isinstance(result.index, pd.MultiIndex)
assert result.index.names == ["country", "unit"]

def test_not_empty(self, mock_network: MockPyPSANetwork):
"""Test that result is not empty."""
result = Final_Energy_by_Sector__Agriculture(mock_network)
assert len(result) > 0

def test_numeric_values(self, mock_network: MockPyPSANetwork):
"""Test that result values are numeric."""
result = Final_Energy_by_Sector__Agriculture(mock_network)
assert result.dtype in [float, int] or pd.api.types.is_numeric_dtype(
result.dtype
)

def test_contains_austria(self, mock_network: MockPyPSANetwork):
"""Test that result contains Austria (AT) data."""
result = Final_Energy_by_Sector__Agriculture(mock_network)
assert "AT" in result.index.get_level_values("country")

def test_multiple_networks(self, mock_network_collection: MockNetworkCollection):
"""Test processing multiple networks from collection."""
for network in mock_network_collection:
result = Final_Energy_by_Sector__Agriculture(network)
assert isinstance(result, pd.Series)
assert isinstance(result.index, pd.MultiIndex)
assert result.index.names == ["country", "unit"]
assert len(result) > 0
Loading