Skip to content

Commit

Permalink
Silence warnings in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
p-snft committed Aug 15, 2024
1 parent 2e0e251 commit d51d94e
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 82 deletions.
128 changes: 74 additions & 54 deletions tests/test_components/test_offset_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import pytest
from oemof.tools.debugging import ExperimentalFeatureWarning

from oemof import solph
from oemof.solph._plumbing import sequence
Expand Down Expand Up @@ -104,58 +105,67 @@ def check_results(
def add_OffsetConverter(
es, reference_bus, nominal_value, minimal_value, eta_at_nom, eta_at_min
):
# Use of experimental API to access nodes by label.
# Can be removed with future release of network.
with warnings.catch_warnings(
action="ignore", category=ExperimentalFeatureWarning
):
oc_inputs = {
b: solph.Flow()
for label, b in es.node.items()
if "bus input" in label
}
oc_outputs = {
b: solph.Flow()
for label, b in es.node.items()
if "bus output" in label
}

if reference_bus in oc_outputs:
f = oc_outputs[reference_bus]
get_slope_and_offset = slope_offset_from_nonconvex_output
fix = [0] + np.linspace(minimal_value, nominal_value, 9).tolist()
else:
f = oc_inputs[reference_bus]
get_slope_and_offset = slope_offset_from_nonconvex_input
fix = [0] + np.linspace(
minimal_value * eta_at_min[es.node["bus output 0"]],
nominal_value * eta_at_nom[es.node["bus output 0"]],
9,
).tolist()

fix_flow = es.flows()[es.node["bus output 0"], es.node["sink 0"]]
fix_flow.fix = fix
fix_flow.nominal_value = 1

slopes = {}
offsets = {}

for bus in list(oc_inputs) + list(oc_outputs):
if bus == reference_bus:
continue
slope, offset = get_slope_and_offset(
1,
minimal_value / nominal_value,
eta_at_nom[bus],
eta_at_min[bus],
)
slopes[bus] = slope
offsets[bus] = offset

oc_inputs = {
b: solph.Flow() for label, b in es.node.items() if "bus input" in label
}
oc_outputs = {
b: solph.Flow()
for label, b in es.node.items()
if "bus output" in label
}
f.nonconvex = solph.NonConvex()
f.nominal_value = nominal_value
f.min = sequence(minimal_value / nominal_value)

if reference_bus in oc_outputs:
f = oc_outputs[reference_bus]
get_slope_and_offset = slope_offset_from_nonconvex_output
fix = [0] + np.linspace(minimal_value, nominal_value, 9).tolist()
else:
f = oc_inputs[reference_bus]
get_slope_and_offset = slope_offset_from_nonconvex_input
fix = [0] + np.linspace(
minimal_value * eta_at_min[es.node["bus output 0"]],
nominal_value * eta_at_nom[es.node["bus output 0"]],
9,
).tolist()

fix_flow = es.flows()[es.node["bus output 0"], es.node["sink 0"]]
fix_flow.fix = fix
fix_flow.nominal_value = 1

slopes = {}
offsets = {}

for bus in list(oc_inputs) + list(oc_outputs):
if bus == reference_bus:
continue
slope, offset = get_slope_and_offset(
1, minimal_value / nominal_value, eta_at_nom[bus], eta_at_min[bus]
oc = solph.components.OffsetConverter(
label="offset converter",
inputs=oc_inputs,
outputs=oc_outputs,
conversion_factors=slopes,
normed_offsets=offsets,
)
slopes[bus] = slope
offsets[bus] = offset

f.nonconvex = solph.NonConvex()
f.nominal_value = nominal_value
f.min = sequence(minimal_value / nominal_value)

oc = solph.components.OffsetConverter(
label="offset converter",
inputs=oc_inputs,
outputs=oc_outputs,
conversion_factors=slopes,
normed_offsets=offsets,
)

es.add(oc)
es.add(oc)


def test_custom_properties():
Expand Down Expand Up @@ -476,8 +486,13 @@ def test_two_OffsetConverters_with_and_without_investment():

es.add(oc)

fix_flow = es.flows()[es.node["bus output 0"], es.node["sink 0"]]
fix_flow.fix = [v * 2 for v in fix_flow.fix]
# Use of experimental API to access nodes by label.
# Can be removed with future release of network.
with warnings.catch_warnings(
action="ignore", category=ExperimentalFeatureWarning
):
fix_flow = es.flows()[es.node["bus output 0"], es.node["sink 0"]]
fix_flow.fix = [v * 2 for v in fix_flow.fix]
# if the model solves it is feasible
_ = solve_and_extract_results(es)

Expand All @@ -491,10 +506,15 @@ def test_OffsetConverter_05x_compatibility():
nominal_value = 10
minimal_value = 3

fix = [0] + np.linspace(minimal_value, nominal_value, 9).tolist()
fix_flow = es.flows()[es.node["bus output 0"], es.node["sink 0"]]
fix_flow.fix = fix
fix_flow.nominal_value = 1
# Use of experimental API to access nodes by label.
# Can be removed with future release of network.
with warnings.catch_warnings(
action="ignore", category=ExperimentalFeatureWarning
):
fix = [0] + np.linspace(minimal_value, nominal_value, 9).tolist()
fix_flow = es.flows()[es.node["bus output 0"], es.node["sink 0"]]
fix_flow.fix = fix
fix_flow.nominal_value = 1

eta_at_nom = 0.7
eta_at_min = 0.5
Expand Down
28 changes: 17 additions & 11 deletions tests/test_components/test_sink.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
import warnings

import pytest
from oemof.tools.debugging import ExperimentalFeatureWarning

from oemof import solph

Expand All @@ -22,18 +24,22 @@ def test_multi_input_sink():
es.add(
solph.components.Source(f"source {i}", outputs={b: solph.Flow()})
)

es.add(
solph.components.Sink(
inputs={
es.node[f"bus input {i}"]: solph.Flow(
nominal_value=1,
variable_costs=costs,
)
for i in range(num_in)
}
# Use of experimental API to access nodes by label.
# Can be removed with future release of network.
with warnings.catch_warnings(
action="ignore", category=ExperimentalFeatureWarning
):
es.add(
solph.components.Sink(
inputs={
es.node[f"bus input {i}"]: solph.Flow(
nominal_value=1,
variable_costs=costs,
)
for i in range(num_in)
}
)
)
)

model = solph.Model(es)
model.solve("cbc")
Expand Down
27 changes: 17 additions & 10 deletions tests/test_components/test_source.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
import warnings

import pytest
from oemof.tools.debugging import ExperimentalFeatureWarning

from oemof import solph

Expand All @@ -21,17 +23,22 @@ def test_multi_output_source():
es.add(b)
es.add(solph.components.Sink(f"source {i}", inputs={b: solph.Flow()}))

es.add(
solph.components.Source(
outputs={
es.node[f"bus input {i}"]: solph.Flow(
nominal_value=1,
variable_costs=costs,
)
for i in range(num_out)
}
# Use of experimental API to access nodes by label.
# Can be removed with future release of network.
with warnings.catch_warnings(
action="ignore", category=ExperimentalFeatureWarning
):
es.add(
solph.components.Source(
outputs={
es.node[f"bus input {i}"]: solph.Flow(
nominal_value=1,
variable_costs=costs,
)
for i in range(num_out)
}
)
)
)

model = solph.Model(es)
model.solve("cbc")
Expand Down
17 changes: 10 additions & 7 deletions tests/test_solph_network_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import warnings

import pytest
from oemof.tools.debugging import SuspiciousUsageWarning

from oemof import solph

Expand Down Expand Up @@ -60,20 +61,22 @@ def test_sequence_conversion_factor_from_list_wrong_length(self):
with pytest.raises(IndexError):
self.a = transf.conversion_factors[self.bus][6]

@pytest.mark.filterwarnings("ignore:Attribute <outputs>:UserWarning")
def test_converter_missing_output_create_empty_dict(self):
trfr = solph.components.Converter(inputs={})
assert trfr.outputs == {}
with pytest.warns(SuspiciousUsageWarning):
trfr = solph.components.Converter(inputs={})
assert trfr.outputs == {}

@pytest.mark.filterwarnings("ignore:Attribute <inputs>:UserWarning")
def test_converter_missing_input_create_empty_dict(self):
trfr = solph.components.Converter(outputs={})
assert trfr.inputs == {}
with pytest.warns(SuspiciousUsageWarning):
trfr = solph.components.Converter(outputs={})
assert trfr.inputs == {}


def test_transformer_wrapper():
with pytest.warns(FutureWarning):
solph.components.Transformer()
# no inputs/outputs
with pytest.warns(SuspiciousUsageWarning):
solph.components.Transformer()


def test_offset_transformer_wrapper():
Expand Down

0 comments on commit d51d94e

Please sign in to comment.