Skip to content

Commit 0580bd7

Browse files
committed
Atomize tests with a fixture
* Upgrade docstring
1 parent 7489f6e commit 0580bd7

File tree

2 files changed

+134
-76
lines changed

2 files changed

+134
-76
lines changed

pvlib/spectrum/mismatch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ def martin_ruiz_spectral_modifier(clearness_index, airmass_absolute,
256256
----------
257257
clearness_index : numeric
258258
Clearness index of the sky.
259-
Should be obtained through :py:func:`~pvlib.irradiance.clearness_index`
260259
261260
airmass_absolute : numeric
262261
Absolute airmass. Give attention to algorithm used (``kasten1966`` is
@@ -327,6 +326,7 @@ def martin_ruiz_spectral_modifier(clearness_index, airmass_absolute,
327326
pvlib.atmosphere.get_absolute_airmass
328327
pvlib.atmosphere.first_solar_spectral_correction
329328
"""
329+
# Note tests for this function are prefixed with test_martin_ruiz_mm_*
330330

331331
irrad_components = ('direct', 'sky_diffuse', 'ground_diffuse')
332332
# Fitting parameters directly from [1]_

pvlib/tests/test_spectrum.py

Lines changed: 133 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -174,115 +174,173 @@ def test_calc_spectral_mismatch_field(spectrl2_data):
174174
assert_allclose(mm, expected, rtol=1e-6)
175175

176176

177-
def test_martin_ruiz_spectral_modifier():
178-
# tests with only cell_type given
179-
# test with scalar values
180-
clearness_index = 0.82
181-
airmass_absolute = 1.2
182-
# Expected values: Direct | Sky diffuse | Ground diffuse
183-
# Do not change order in any 'expected' values list
184-
expected = (1.00197741, 0.71632057, 0.94757498)
185-
186-
result = \
187-
spectrum.martin_ruiz_spectral_modifier(clearness_index,
188-
airmass_absolute,
189-
cell_type='monosi')
190-
assert_approx_equal(result['direct'], expected[0])
191-
assert_approx_equal(result['sky_diffuse'], expected[1])
192-
assert_approx_equal(result['ground_diffuse'], expected[2])
193-
194-
# test NaN in, NaN out
195-
clearness_index = 0.82
196-
airmass_absolute = np.nan
177+
@pytest.fixture
178+
def martin_ruiz_mismatch_data():
179+
# Data to run tests of martin_ruiz_spectral_modifier
180+
kwargs = {
181+
'clearness_index': [0.56, 0.612, 0.664, 0.716, 0.768, 0.82],
182+
'airmass_absolute': [2, 1.8, 1.6, 1.4, 1.2, 1],
183+
'monosi_expected': {
184+
'dir': [1.09149, 1.07275, 1.05432, 1.03622, 1.01842, 1.00093],
185+
'sky': [0.88636, 0.85009, 0.81530, 0.78194, 0.74994, 0.71925],
186+
'gnd': [1.02011, 1.00465, 0.98943, 0.97444, 0.95967, 0.94513]},
187+
'polysi_expected': {
188+
'dir': [1.09166, 1.07280, 1.05427, 1.03606, 1.01816, 1.00058],
189+
'sky': [0.89443, 0.85553, 0.81832, 0.78273, 0.74868, 0.71612],
190+
'gnd': [1.02638, 1.00888, 0.99168, 0.97476, 0.95814, 0.94180]},
191+
'asi_expected': {
192+
'dir': [1.07066, 1.05643, 1.04238, 1.02852, 1.01485, 1.00136],
193+
'sky': [0.94889, 0.91699, 0.88616, 0.85637, 0.82758, 0.79976],
194+
'gnd': [1.03801, 1.02259, 1.00740, 0.99243, 0.97769, 0.96316]},
195+
'monosi_model_params_dict': {
196+
'direct': {'c': 1.029, 'a': -3.13e-1, 'b': 5.24e-3},
197+
'sky_diffuse': {'c': 0.764, 'a': -8.82e-1, 'b': -2.04e-2},
198+
'ground_diffuse': {'c': 0.970, 'a': -2.44e-1, 'b': 1.29e-2}},
199+
'monosi_custom_params_df': pd.DataFrame({
200+
'direct': [1.029, -0.313, 0.00524],
201+
'diffuse_sky': [0.764, -0.882, -0.0204]},
202+
index=('c', 'a', 'b'))
203+
}
204+
return kwargs
205+
206+
207+
def test_martin_ruiz_mm_scalar(martin_ruiz_mismatch_data):
208+
# test scalar input ; only cell_type given
209+
clearness_index = martin_ruiz_mismatch_data['clearness_index'][0]
210+
airmass_absolute = martin_ruiz_mismatch_data['airmass_absolute'][0]
211+
result = spectrum.martin_ruiz_spectral_modifier(clearness_index,
212+
airmass_absolute,
213+
cell_type='asi')
214+
215+
assert_approx_equal(result['direct'],
216+
martin_ruiz_mismatch_data['asi_expected']['dir'][0],
217+
significant=5)
218+
assert_approx_equal(result['sky_diffuse'],
219+
martin_ruiz_mismatch_data['asi_expected']['sky'][0],
220+
significant=5)
221+
assert_approx_equal(result['ground_diffuse'],
222+
martin_ruiz_mismatch_data['asi_expected']['gnd'][0],
223+
significant=5)
224+
225+
226+
def test_martin_ruiz_mm_series(martin_ruiz_mismatch_data):
227+
# test with Series input ; only cell_type given
228+
clearness_index = pd.Series(martin_ruiz_mismatch_data['clearness_index'])
229+
airmass_absolute = pd.Series(martin_ruiz_mismatch_data['airmass_absolute'])
230+
expected = {
231+
'dir': pd.Series(martin_ruiz_mismatch_data['polysi_expected']['dir']),
232+
'sky': pd.Series(martin_ruiz_mismatch_data['polysi_expected']['sky']),
233+
'gnd': pd.Series(martin_ruiz_mismatch_data['polysi_expected']['gnd'])}
234+
235+
result = spectrum.martin_ruiz_spectral_modifier(clearness_index,
236+
airmass_absolute,
237+
cell_type='polysi')
238+
assert_series_equal(result['direct'], expected['dir'], atol=1e-5)
239+
assert_series_equal(result['sky_diffuse'], expected['sky'], atol=1e-5)
240+
assert_series_equal(result['ground_diffuse'], expected['gnd'], atol=1e-5)
241+
242+
243+
def test_martin_ruiz_mm_nans(martin_ruiz_mismatch_data):
244+
# test NaN in, NaN out ; only cell_type given
245+
clearness_index = pd.Series(martin_ruiz_mismatch_data['clearness_index'])
246+
airmass_absolute = pd.Series(martin_ruiz_mismatch_data['airmass_absolute'])
247+
airmass_absolute[:5] = np.nan
248+
197249
result = spectrum.martin_ruiz_spectral_modifier(clearness_index,
198250
airmass_absolute,
199251
cell_type='monosi')
200-
assert result.isna().all()
201-
202-
# test with Series input
203-
clearness_index = pd.Series([0.56, 0.67, 0.80])
204-
airmass_absolute = pd.Series([1.6, 1.4, 1.2])
205-
expected = (
206-
pd.Series([1.088928, 1.050989, 1.008082]), # Direct
207-
pd.Series([0.901327, 0.816901, 0.726754]), # Sky diffuse
208-
pd.Series([1.019917, 0.986947, 0.949899])) # Ground diffuse
209-
210-
result = \
211-
spectrum.martin_ruiz_spectral_modifier(clearness_index,
212-
airmass_absolute,
213-
cell_type='polysi')
214-
assert_series_equal(result['direct'], expected[0], atol=1e-5)
215-
assert_series_equal(result['sky_diffuse'], expected[1], atol=1e-5)
216-
assert_series_equal(result['ground_diffuse'], expected[2], atol=1e-5)
252+
assert np.isnan(result['direct'][:5]).all()
253+
assert not np.isnan(result['direct'][5:]).any()
254+
assert np.isnan(result['sky_diffuse'][:5]).all()
255+
assert not np.isnan(result['sky_diffuse'][5:]).any()
256+
assert np.isnan(result['ground_diffuse'][:5]).all()
257+
assert not np.isnan(result['ground_diffuse'][5:]).any()
258+
259+
260+
def test_martin_ruiz_mm_model_dict(martin_ruiz_mismatch_data):
261+
# test results when giving 'model_parameters' as dict
262+
# test custom quantity of components and its names can be given
263+
clearness_index = pd.Series(martin_ruiz_mismatch_data['clearness_index'])
264+
airmass_absolute = pd.Series(martin_ruiz_mismatch_data['airmass_absolute'])
265+
expected = {
266+
'dir': pd.Series(martin_ruiz_mismatch_data['monosi_expected']['dir']),
267+
'sky': pd.Series(martin_ruiz_mismatch_data['monosi_expected']['sky']),
268+
'gnd': pd.Series(martin_ruiz_mismatch_data['monosi_expected']['gnd'])}
269+
model_parameters = martin_ruiz_mismatch_data['monosi_model_params_dict']
270+
271+
result = spectrum.martin_ruiz_spectral_modifier(
272+
clearness_index,
273+
airmass_absolute,
274+
model_parameters=model_parameters)
275+
assert_allclose(result['direct'], expected['dir'], atol=1e-5)
276+
assert_allclose(result['sky_diffuse'], expected['sky'], atol=1e-5)
277+
assert_allclose(result['ground_diffuse'], expected['gnd'], atol=1e-5)
217278

279+
280+
def test_martin_ruiz_mm_model_df(martin_ruiz_mismatch_data):
218281
# test results when giving 'model_parameters' as DataFrame
219282
# test custom quantity of components and its names can be given
220-
clearness_index = np.array([0.56, 0.612, 0.664, 0.716, 0.768, 0.82])
221-
airmass_absolute = np.array([2, 1.8, 1.6, 1.4, 1.2, 1])
222-
model_parameters = pd.DataFrame({ # monosi values
223-
'direct': [1.029, -0.313, 0.00524],
224-
'diffuse_sky': [0.764, -0.882, -0.0204]},
225-
index=('c', 'a', 'b'))
226-
expected = ( # Direct / Sky diffuse / Ground diffuse
227-
np.array([1.09149, 1.07274, 1.05432, 1.03621, 1.01841, 1.00092]),
228-
np.array([0.88636, 0.85009, 0.81530, 0.78193, 0.74993, 0.71924]))
283+
clearness_index = np.array(martin_ruiz_mismatch_data['clearness_index'])
284+
airmass_absolute = np.array(martin_ruiz_mismatch_data['airmass_absolute'])
285+
model_parameters = martin_ruiz_mismatch_data['monosi_custom_params_df']
286+
expected = {
287+
'dir': np.array(martin_ruiz_mismatch_data['monosi_expected']['dir']),
288+
'sky': np.array(martin_ruiz_mismatch_data['monosi_expected']['sky'])}
229289

230290
result = spectrum.martin_ruiz_spectral_modifier(
231291
clearness_index,
232292
airmass_absolute,
233293
model_parameters=model_parameters)
234-
assert_allclose(result['direct'], expected[0], atol=1e-5)
235-
assert_allclose(result['diffuse_sky'], expected[1], atol=1e-5)
294+
assert_allclose(result['direct'], expected['dir'], atol=1e-5)
295+
assert_allclose(result['diffuse_sky'], expected['sky'], atol=1e-5)
296+
236297

298+
def test_martin_ruiz_mm_userwarning(martin_ruiz_mismatch_data):
237299
# test warning is raised with both 'cell_type' and 'model_parameters'
238-
# test results when giving 'model_parameters' as dict
239-
clearness_index = np.array([0.56, 0.612, 0.664, 0.716, 0.768, 0.82])
240-
airmass_absolute = np.array([2, 1.8, 1.6, 1.4, 1.2, 1])
241-
model_parameters = { # Using 'monosi' values
242-
'direct': {'c': 1.029, 'a': -3.13e-1, 'b': 5.24e-3},
243-
'sky_diffuse': {'c': 0.764, 'a': -8.82e-1, 'b': -2.04e-2},
244-
'ground_diffuse': {'c': 0.970, 'a': -2.44e-1, 'b': 1.29e-2}}
245-
expected = ( # Direct / Sky diffuse / Ground diffuse
246-
np.array([1.09149, 1.07274, 1.05432, 1.03621, 1.01841, 1.00092]),
247-
np.array([0.88636, 0.85009, 0.81530, 0.78193, 0.74993, 0.71924]),
248-
np.array([1.02011, 1.00465, 0.98943, 0.97443, 0.95967, 0.94513]))
300+
clearness_index = pd.Series(martin_ruiz_mismatch_data['clearness_index'])
301+
airmass_absolute = pd.Series(martin_ruiz_mismatch_data['airmass_absolute'])
302+
model_parameters = martin_ruiz_mismatch_data['monosi_model_params_dict']
249303

250304
with pytest.warns(UserWarning,
251305
match='Both "cell_type" and "model_parameters" given! '
252306
'Using provided "model_parameters".'):
253-
result = spectrum.martin_ruiz_spectral_modifier(
307+
_ = spectrum.martin_ruiz_spectral_modifier(
254308
clearness_index,
255309
airmass_absolute,
256310
cell_type='asi',
257311
model_parameters=model_parameters)
258-
assert_allclose(result['direct'], expected[0], atol=1e-5)
259-
assert_allclose(result['sky_diffuse'], expected[1], atol=1e-5)
260-
assert_allclose(result['ground_diffuse'], expected[2], atol=1e-5)
261312

262313

263-
def test_martin_ruiz_spectral_modifier_errors():
264-
# mock values to run errors
265-
clearness_index = 0.75
266-
airmass_absolute = 1.6
267-
# test exception raised when cell_type does not exist in algorithm
314+
def test_martin_ruiz_mm_error_notimplemented(martin_ruiz_mismatch_data):
315+
# test exception is raised when cell_type does not exist in algorithm
316+
clearness_index = np.array(martin_ruiz_mismatch_data['clearness_index'])
317+
airmass_absolute = np.array(martin_ruiz_mismatch_data['airmass_absolute'])
318+
268319
with pytest.raises(NotImplementedError,
269320
match='Cell type parameters not defined in algorithm!'):
270321
_ = spectrum.martin_ruiz_spectral_modifier(clearness_index,
271322
airmass_absolute,
272323
cell_type='')
273-
# test exception raised when missing cell_type and model_parameters
324+
325+
326+
def test_martin_ruiz_mm_error_missing_params(martin_ruiz_mismatch_data):
327+
# test exception is raised when missing cell_type and model_parameters
328+
clearness_index = np.array(martin_ruiz_mismatch_data['clearness_index'])
329+
airmass_absolute = np.array(martin_ruiz_mismatch_data['airmass_absolute'])
330+
274331
with pytest.raises(TypeError,
275332
match='You must pass at least "cell_type" '
276333
'or "model_parameters" as arguments!'):
277334
_ = spectrum.martin_ruiz_spectral_modifier(clearness_index,
278335
airmass_absolute)
279-
# test for error in params keys
280-
clearness_index = 0.74
281-
airmass_absolute = 1.5
336+
337+
338+
def test_martin_ruiz_mm_error_model_keys(martin_ruiz_mismatch_data):
339+
# test exception is raised when in params keys
340+
clearness_index = np.array(martin_ruiz_mismatch_data['clearness_index'])
341+
airmass_absolute = np.array(martin_ruiz_mismatch_data['airmass_absolute'])
282342
model_parameters = {
283-
'direct': {'c': 1.029, 'a': -3.13e-1, 'b': 5.24e-3},
284-
'sky_diffuse': {'c': 0.764, 'a': -8.82e-1, 'b': -2.04e-2},
285-
'ground_diffuse': {'z': 0.970, 'x': -2.44e-1, 'y': 1.29e-2}}
343+
'component_example': {'z': 0.970, 'x': -2.44e-1, 'y': 1.29e-2}}
286344
with pytest.raises(ValueError,
287345
match="You must specify model parameters with keys "
288346
"'a','b','c' for each irradiation component."):

0 commit comments

Comments
 (0)