Skip to content

Commit 8451499

Browse files
committed
update plotting tests to be relative value, update ordering of module import in plotting.py, per Copilot review.
1 parent 0f53385 commit 8451499

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

rdtools/plotting.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import plotly.express as px
66
import numpy as np
77
import warnings
8+
import datetime
89

910

1011
def degradation_summary_plots(yoy_rd, yoy_ci, yoy_info, normalized_yield,
@@ -473,8 +474,6 @@ def degradation_timeseries_plot(yoy_info, rolling_days=365, include_ci=True, lab
473474
matplotlib.figure.Figure
474475
'''
475476

476-
import datetime
477-
478477
def _bootstrap(x, percentile, reps):
479478
# stolen from degradation_year_on_year
480479
n1 = len(x)

rdtools/test/plotting_test.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -252,20 +252,45 @@ def test_availability_summary_plots_empty(availability_analysis_object):
252252
def test_degradation_timeseries_plot(degradation_info):
253253
power, yoy_rd, yoy_ci, yoy_info = degradation_info
254254

255-
# test defaults
256-
result = degradation_timeseries_plot(yoy_info)
257-
assert_isinstance(result, plt.Figure)
258-
assert (result.get_axes()[0].get_xlim()[0] == 17685.55)
259-
# test other label options
260-
result = degradation_timeseries_plot(yoy_info=yoy_info, include_ci=False,
261-
label='center', fig=result)
262-
assert_isinstance(result, plt.Figure)
263-
assert (result.get_axes()[0].get_xlim()[0] == 17304.4)
264-
result = degradation_timeseries_plot(yoy_info=yoy_info, include_ci=False, label='left')
265-
assert_isinstance(result, plt.Figure)
266-
assert (result.get_axes()[0].get_xlim()[0] == 17130.5)
267-
result = degradation_timeseries_plot(yoy_info=yoy_info, include_ci=False, label=None)
268-
assert_isinstance(result, plt.Figure)
255+
# test defaults (label='right')
256+
result_right = degradation_timeseries_plot(yoy_info)
257+
assert_isinstance(result_right, plt.Figure)
258+
xlim_right = result_right.get_axes()[0].get_xlim()[0]
259+
260+
# test label='center'
261+
result_center = degradation_timeseries_plot(yoy_info=yoy_info, include_ci=False,
262+
label='center', fig=result_right)
263+
assert_isinstance(result_center, plt.Figure)
264+
xlim_center = result_center.get_axes()[0].get_xlim()[0]
265+
266+
# test label='left'
267+
result_left = degradation_timeseries_plot(yoy_info=yoy_info, include_ci=False, label='left')
268+
assert_isinstance(result_left, plt.Figure)
269+
xlim_left = result_left.get_axes()[0].get_xlim()[0]
270+
271+
# test label=None (should default to 'right')
272+
result_none = degradation_timeseries_plot(yoy_info=yoy_info, include_ci=False, label=None)
273+
assert_isinstance(result_none, plt.Figure)
274+
xlim_none = result_none.get_axes()[0].get_xlim()[0]
275+
276+
# Check that the xlim values are offset as expected
277+
# right > center > left (since offset_days increases)
278+
assert xlim_right > xlim_center > xlim_left
279+
assert xlim_right == xlim_none # label=None defaults to 'right'
280+
281+
# The expected difference from right to left is 548 days (1.5 yrs), allow 5% tolerance
282+
expected_diff = 548
283+
actual_diff = (xlim_right - xlim_left)
284+
tolerance = expected_diff * 0.05
285+
assert abs(actual_diff - expected_diff) <= tolerance, \
286+
f"difference of right-left xlim {actual_diff} not within 5% of 1.5 yrs."
287+
288+
# The expected difference from right to center is 365 days, allow 5% tolerance
289+
expected_diff2 = 365
290+
actual_diff2 = (xlim_right - xlim_center)
291+
tolerance2 = expected_diff2 * 0.05
292+
assert abs(actual_diff2 - expected_diff2) <= tolerance2, \
293+
f"difference of right-center xlim {actual_diff2} not within 5% of 1 yr."
269294

270295
with pytest.raises(KeyError):
271296
degradation_timeseries_plot({'a': 1}, include_ci=False)

0 commit comments

Comments
 (0)