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

Unit test base network #1438

Merged
merged 28 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
046fd3a
code: add unit tests for base_network.py
finozzifa Nov 28, 2024
8169205
Merge branch 'master' of https://github.com/PyPSA/pypsa-eur into unit…
finozzifa Nov 28, 2024
b7cc742
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 28, 2024
e618ec6
code: pre-commit checks
finozzifa Nov 28, 2024
b505fd1
Merge branch 'unit_test_base_network' of https://github.com/finozzifa…
finozzifa Nov 28, 2024
f7b5a84
code: add further unit tests base_network
finozzifa Nov 29, 2024
2371cc3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 29, 2024
edb4ecb
code: new test
finozzifa Nov 29, 2024
6a1538c
Merge branch 'unit_test_base_network' of https://github.com/finozzifa…
finozzifa Nov 29, 2024
1a5e08b
Merge branch 'master' of https://github.com/PyPSA/pypsa-eur into unit…
finozzifa Dec 2, 2024
dff048b
code: add unit test for _load_buses and sketch the one for _load_conv…
finozzifa Dec 2, 2024
df9d774
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 2, 2024
4111708
code: pre-commit
finozzifa Dec 2, 2024
244b7bd
Merge branch 'unit_test_base_network' of https://github.com/finozzifa…
finozzifa Dec 2, 2024
2dc6b9a
code: fix issue with test_load_buses
finozzifa Dec 2, 2024
bdcffae
code: unit test for _load_converters_from_osm
finozzifa Dec 2, 2024
151214a
data: add italy_shape.geojson
finozzifa Dec 3, 2024
a943f53
code: unit test for _load_converters_from_eg and _load_transformers
finozzifa Dec 3, 2024
429fee8
code: add unit test for _load_links_from_osm and _load_links_from_eg
finozzifa Dec 3, 2024
2f4ec22
code: add unit test for _load_lines
finozzifa Dec 3, 2024
b9104f4
Merge branch 'master' of https://github.com/PyPSA/pypsa-eur into unit…
finozzifa Dec 3, 2024
37da69a
code: add unit test for _reconnect_crimea
finozzifa Dec 3, 2024
a3df7e4
code: add unit test for _set_electrical_parameters_lines_eg
finozzifa Dec 4, 2024
3d1b67e
code: add new unit tests
finozzifa Dec 4, 2024
14ad7ba
Merge branch 'master' of https://github.com/PyPSA/pypsa-eur into unit…
finozzifa Dec 12, 2024
ea5a9a6
code: fixing unlink usage
finozzifa Dec 12, 2024
b85dbff
code: config fixture - scope from function to session
finozzifa Dec 12, 2024
4265e29
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 12, 2024
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: 1 addition & 1 deletion scripts/base_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
from packaging.version import Version, parse
from scipy.sparse import csgraph
from scipy.spatial import KDTree
from shapely.geometry import LineString, Point
from shapely.geometry import Point

PD_GE_2_2 = parse(pd.__version__) >= Version("2.2")

Expand Down
89 changes: 88 additions & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import pathlib

import pandas as pd
import pypsa
import pytest
import yaml
Expand All @@ -22,9 +23,95 @@ def ac_dc_network():
return pypsa.examples.ac_dc_meshed(from_master=True)


@pytest.fixture(scope="function")
@pytest.fixture(scope="session")
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


@pytest.fixture(scope="function")
Copy link
Member

Choose a reason for hiding this comment

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

Do you have any best practices for choosing scopes? I think the function scope works well for DataFrames because they initialise quickly. But in PyPSA I've been playing around with the scope of test networks because they are sometimes modified and sometimes not in the tests, which adds up if function is used as scope.

Copy link
Member

Choose a reason for hiding this comment

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

And same goes for the two network fixtures you created here already

Copy link
Collaborator Author

@finozzifa finozzifa Dec 12, 2024

Choose a reason for hiding this comment

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

Hey @lkstrp,

indeed great question!

I usually used function being the default one and (most likely) the safest one.

However, now that I think about it, the config fixture can be initialized only once per test session. Changing the scope for this fixture actually leads to a speed up of the test execution (almost 30%). You can find the results below. The scope session and package behave equally well.

This is of course not very relevant now, but it will become once we add more and more tests.

@pytest.fixture(scope="function")
def config():

hyperfine pytest test

Time (mean ± σ):      2.223 s ±  0.108 s    [User: 1.949 s, System: 0.251 s]
Range (min … max):    2.106 s …  2.459 s    10 runs

@pytest.fixture(scope="session")
def config():

hyperfine pytest test

Time (mean ± σ):      1.568 s ±  0.004 s    [User: 1.333 s, System: 0.228 s]
Range (min … max):    1.563 s …  1.573 s    10 runs

@pytest.fixture(scope="package")
def config():

hyperfine pytest test

Time (mean ± σ):      1.570 s ±  0.003 s    [User: 1.334 s, System: 0.229 s]
Range (min … max):    1.567 s …  1.574 s    10 runs

def buses_dataframe():
return pd.DataFrame(
{
"bus_id": [5231, 5232],
"voltage": [380.0, 380.0],
"dc": ["f", "f"],
"symbol": ["Substation", "Substation"],
"under_construction": ["f", "f"],
"x": [6.8884, 6.8894],
"y": [45.6783, 45.6793],
"country": ["IT", "IT"],
"geometry": ["POINT (6.8884 45.6783)", "POINT (6.8894 45.6793)"],
},
index=[5090, 5091],
)


@pytest.fixture(scope="function")
def converters_dataframe():
return pd.DataFrame(
{
"converter_id": "convert_5231_5232",
"bus0": 5231,
"bus1": 5232,
"voltage": 380.0,
"geometry": "'LINESTRING(6.8884 45.6783 ",
"": "6.8894 45.6793)'",
},
index=[0],
)


@pytest.fixture(scope="function")
def lines_dataframe():
return pd.DataFrame(
{
"line_id": "line_5231_5232",
"bus0": 5231,
"bus1": 5232,
"voltage": 380.0,
"circuits": 1.0,
"length": 1000.0,
"underground": "t",
"under_construction": "f",
"geometry": "'LINESTRING(6.8884 45.6783 ",
"": "6.8894 45.6793)'",
},
index=[0],
)


@pytest.fixture(scope="function")
def links_dataframe():
return pd.DataFrame(
{
"link_id": "link_5231_5232",
"bus0": 5231,
"bus1": 5232,
"voltage": 380.0,
"p_nom": 600.0,
"length": 1000.0,
"underground": "t",
"under_construction": "f",
"geometry": "'LINESTRING(6.8884 45.6783 ",
"": "6.8894 45.6793)'",
},
index=[0],
)


@pytest.fixture(scope="function")
def transformers_dataframe():
return pd.DataFrame(
{
"transformer_id": "transf_5231_5232",
"bus0": 5231,
"bus1": 5232,
"voltage": 380.0,
"geometry": "'LINESTRING(6.8884 45.6783 ",
"": "6.8894 45.6793)'",
},
index=[0],
)
Loading
Loading