Skip to content

Commit

Permalink
Add test for build powerplants (#1432)
Browse files Browse the repository at this point in the history
* code: add unit test for add_custom_powerplants

* remove unnecessary file

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* code: add test parametrization

* code: pre-commit fix

* code: unit test for build_powerplants.py, modify Makefile and test.yaml

* code: include requested comments to PR

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* code: new reformatting

---------

Co-authored-by: Fabrizio Finozzi <fabriziofinozzi@Mac.homenet.telecomitalia.it>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 22, 2024
1 parent 98d387c commit 17628e9
Show file tree
Hide file tree
Showing 9 changed files with 340 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ jobs:
run: |
make test
- name: Run unit tests
run: |
make unit-test
- name: Upload artifacts
if: always()
uses: actions/upload-artifact@v4
Expand Down
4 changes: 4 additions & 0 deletions .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ Files: doc/data.csv
Copyright: 2019-2024 The PyPSA-Eur Authors
License: CC-BY-4.0

Files: test/test_data/*
Copyright: 2019-2024 The PyPSA-Eur Authors
License: CC-BY-4.0

Files: doc/configtables/*
Copyright: 2019-2024 The PyPSA-Eur Authors
License: CC-BY-4.0
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# SPDX-License-Identifier: CC0-1.0

.PHONY: _conda_check install install-pinned-linux install-pinned-windows install-pinned-macos test clean-tests reset
.PHONY: _conda_check install install-pinned-linux install-pinned-windows install-pinned-macos test checks clean-tests reset

# Helper: Check if conda or mamba is installed and set CONDA_OR_MAMBA variable
_conda_check:
Expand Down Expand Up @@ -44,6 +44,9 @@ test:
snakemake --configfile config/test/config.scenarios.yaml --rerun-triggers=mtime -n
echo "All tests completed successfully."

unit-test:
pytest test

# Cleans all output files from tests
clean-tests:
snakemake solve_elec_networks --configfile config/test/config.electricity.yaml --rerun-triggers=mtime --delete-all-output
Expand Down
4 changes: 3 additions & 1 deletion scripts/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: : 2017-2023 The PyPSA-Eur Authors
# SPDX-FileCopyrightText: : 2017-2024 The PyPSA-Eur Authors
#
# SPDX-License-Identifier: MIT

# coding: utf-8
4 changes: 4 additions & 0 deletions test/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: : 2017-2024 The PyPSA-Eur Authors
#
# SPDX-License-Identifier: MIT
30 changes: 30 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: : 2017-2024 The PyPSA-Eur Authors
#
# SPDX-License-Identifier: MIT

# coding: utf-8

import pathlib

import pypsa
import pytest
import yaml


@pytest.fixture(scope="function")
def scigrid_network():
return pypsa.examples.scigrid_de(from_master=True)


@pytest.fixture(scope="function")
def ac_dc_network():
return pypsa.examples.ac_dc_meshed(from_master=True)


@pytest.fixture(scope="function")
def config():
path_config = pathlib.Path(pathlib.Path.cwd(), "config", "config.default.yaml")
with open(path_config, "r") as file:
config_dict = yaml.safe_load(file)
return config_dict
152 changes: 152 additions & 0 deletions test/test_build_powerplants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: : 2017-2024 The PyPSA-Eur Authors
#
# SPDX-License-Identifier: MIT

# coding: utf-8
"""
Tests the functionalities of scripts/build_powerplants.py.
"""

import pathlib
import sys

import numpy as np
import pandas as pd
import pytest

sys.path.append("./scripts")

from build_powerplants import (
add_custom_powerplants,
replace_natural_gas_fueltype,
replace_natural_gas_technology,
)

path_cwd = pathlib.Path.cwd()


@pytest.mark.parametrize(
"query_value,expected",
[(False, (131, 18)), (True, (137, 18))],
)
def test_add_custom_powerplants(config, query_value, expected):
"""
Verify what returned by add_custom_powerplants.
"""
config_dict = config
config_dict["electricity"]["custom_powerplants"] = query_value
custom_powerplants_path = pathlib.Path(
path_cwd, "test", "test_data", "custom_powerplants_DE.csv"
)
ppl_path = pathlib.Path(path_cwd, "test", "test_data", "powerplants_DE.csv")
ppl_df = pd.read_csv(ppl_path)
ppl_final = add_custom_powerplants(
ppl_df,
custom_powerplants_path,
config_dict["electricity"]["custom_powerplants"],
)
assert ppl_df.shape == (131, 18)
assert ppl_final.shape == expected


def test_replace_natural_gas_technology():
"""
Verify what returned by replace_natural_gas_technology.
"""
input_df = pd.DataFrame(
{
"Name": [
"plant_hydro",
"plant_ng_1",
"plant_ng_2",
"plant_ng_3",
"plant_ng_4",
],
"Fueltype": [
"Hydro",
"Natural Gas",
"Natural Gas",
"Natural Gas",
"Natural Gas",
],
"Technology": [
"Run-Of-River",
"Steam Turbine",
"Combustion Engine",
"Not Found",
np.nan,
],
}
)

reference_df = pd.DataFrame(
{
"Name": [
"plant_hydro",
"plant_ng_1",
"plant_ng_2",
"plant_ng_3",
"plant_ng_4",
],
"Fueltype": [
"Hydro",
"Natural Gas",
"Natural Gas",
"Natural Gas",
"Natural Gas",
],
"Technology": ["Run-Of-River", "CCGT", "OCGT", "CCGT", "CCGT"],
}
)
modified_df = input_df.assign(Technology=replace_natural_gas_technology)
comparison_df = modified_df.compare(reference_df)
assert comparison_df.empty


def test_replace_natural_gas_fueltype():
"""
Verify what returned by replace_natural_gas_fueltype.
"""
input_df = pd.DataFrame(
{
"Name": [
"plant_hydro",
"plant_ng_1",
"plant_ng_2",
],
"Fueltype": [
"Hydro",
"Gas",
"Natural",
],
"Technology": [
"Run-Of-River",
"CCGT",
"OCGT",
],
}
)

reference_df = pd.DataFrame(
{
"Name": [
"plant_hydro",
"plant_ng_1",
"plant_ng_2",
],
"Fueltype": [
"Hydro",
"Natural Gas",
"Natural Gas",
],
"Technology": [
"Run-Of-River",
"CCGT",
"OCGT",
],
}
)
modified_df = input_df.assign(Fueltype=replace_natural_gas_fueltype)
comparison_df = modified_df.compare(reference_df)
assert comparison_df.empty
7 changes: 7 additions & 0 deletions test/test_data/custom_powerplants_DE.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Name,Fueltype,Technology,Set,Country,Capacity,Efficiency,DateIn,DateRetrofit,DateOut,lat,lon,Duration,Volume_Mm3,DamHeight_m,StorageCapacity_MWh,EIC,projectID
Eichenkofen,Hydro,Run-Of-River,PP,DE,0.05,,1955.5420560747664,1955.5420560747664,2055.542056074766,48.345217,11.921901,,0.0,0.0,0.0,{},{'JRC': {'JRC-H3173'}}
Isenkraftwerk Generator,Hydro,Run-Of-River,PP,DE,3.15,,1955.5420560747664,1955.5420560747664,2055.542056074766,48.26,12.634,,0.0,0.0,0.0,{},"{'JRC': {'JRC-H2724', 'JRC-H2725', 'JRC-H2719', 'JRC-H2723', 'JRC-H2727', 'JRC-H2720', 'JRC-H2721', 'JRC-H2726', 'JRC-H2722'}}"
Wasserkraftwerk Dietldorf,Hydro,Run-Of-River,PP,DE,0.108,,1955.5420560747664,1955.5420560747664,2055.542056074766,49.205998,11.943661,,0.0,0.0,0.0,{},{'JRC': {'JRC-H2963'}}
Bmkw Konigs Wusterhausen,Bioenergy,Steam Turbine,PP,DE,17.1,0.38,2003.0,2003.0,2023.0,52.30479,13.636029999999998,,0.0,0.0,0.0,{},{'OPSD': {'BNA0549'}}
Datteln,Hard Coal,Steam Turbine,CHP,DE,1014.1306522194916,,1964.0,1964.0,2038.0,51.6293,7.3307,,0.0,0.0,0.0,{},"{'BEYONDCOAL': {'BEYOND-DE-501'}, 'GEM': {'G100000102255', 'G100000102253', 'G100000102254', 'G100000102252'}, 'GEO': {'GEO-44321'}}"
Irsching,Natural Gas,CCGT,PP,DE,1822.0,0.5867942430703624,2010.0,2011.0,2051.0,48.767,11.58,,0.0,0.0,0.0,{'11WD2IRSC000179N'},"{'ENTSOE': {'11WD2IRSC000179N'}, 'GEM': {'L100000400188'}, 'OPSD': {'BNA0995', 'BNA0994'}, 'GEO': {'GEO-44306'}}"
Loading

0 comments on commit 17628e9

Please sign in to comment.