Skip to content

Commit

Permalink
irradiance rescale test for value error
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-springer committed Apr 30, 2024
1 parent 4c84bdc commit f1378d9
Showing 1 changed file with 38 additions and 25 deletions.
63 changes: 38 additions & 25 deletions rdtools/test/irradiance_rescale_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,28 @@

@pytest.fixture
def simple_irradiance():
times = pd.date_range('2019-06-01 12:00', freq='15T', periods=5)
times = pd.date_range("2019-06-01 12:00", freq="15T", periods=5)
time_series = pd.Series([1, 2, 3, 4, 5], index=times, dtype=float)
return time_series


@pytest.mark.parametrize("method", ['iterative', 'single_opt'])
@pytest.mark.parametrize("method", ["iterative", "single_opt", "error"])
def test_rescale(method, simple_irradiance):
# test basic functionality
modeled = simple_irradiance
measured = 1.05 * simple_irradiance
rescaled = irradiance_rescale(measured, modeled, method=method)
expected = measured
assert_series_equal(rescaled, expected, check_exact=False)
if method == "error":
pytest.raises(
ValueError,
irradiance_rescale,
simple_irradiance,
simple_irradiance * 1.05,
method=method,
)
else:
modeled = simple_irradiance
measured = 1.05 * simple_irradiance
rescaled = irradiance_rescale(measured, modeled, method=method)
expected = measured
assert_series_equal(rescaled, expected, check_exact=False)


def test_max_iterations(simple_irradiance):
Expand All @@ -31,38 +40,42 @@ def test_max_iterations(simple_irradiance):
modeled.iloc[4] *= 0.8

with pytest.raises(ConvergenceError):
_ = irradiance_rescale(measured, modeled, method='iterative',
max_iterations=2)
_ = irradiance_rescale(measured, modeled, method="iterative", max_iterations=2)

_ = irradiance_rescale(measured, modeled, method='iterative',
max_iterations=10)
_ = irradiance_rescale(measured, modeled, method="iterative", max_iterations=10)


def test_max_iterations_zero(simple_irradiance):
# zero is sort of a special case, test it separately

# test series already close enough
true_factor = 1.0 + 1e-8
rescaled = irradiance_rescale(simple_irradiance,
simple_irradiance * true_factor,
max_iterations=0,
method='iterative')
rescaled = irradiance_rescale(
simple_irradiance,
simple_irradiance * true_factor,
max_iterations=0,
method="iterative",
)
assert_series_equal(rescaled, simple_irradiance, check_exact=False)

# tighten threshold so that it isn't already close enough
with pytest.raises(ConvergenceError):
_ = irradiance_rescale(simple_irradiance,
simple_irradiance * true_factor,
max_iterations=0,
convergence_threshold=1e-9,
method='iterative')
_ = irradiance_rescale(
simple_irradiance,
simple_irradiance * true_factor,
max_iterations=0,
convergence_threshold=1e-9,
method="iterative",
)


def test_convergence_threshold(simple_irradiance):
# can't converge if threshold is negative
with pytest.raises(ConvergenceError):
_ = irradiance_rescale(simple_irradiance,
simple_irradiance * 1.05,
max_iterations=5, # reduced count for speed
convergence_threshold=-1,
method='iterative')
_ = irradiance_rescale(
simple_irradiance,
simple_irradiance * 1.05,
max_iterations=5, # reduced count for speed
convergence_threshold=-1,
method="iterative",
)

0 comments on commit f1378d9

Please sign in to comment.