Skip to content

Commit 2c39f75

Browse files
committed
code review
* doi backticks * cell_type --> module_type * spectrum.martin_ruiz_spectral_modifer --> spectrum.martin_ruiz * change hastattr __iter__ to np.isscalar * behaviour: providing module_type and model_parameters raises an error instead of a warning
1 parent 1c2dd73 commit 2c39f75

File tree

4 files changed

+69
-78
lines changed

4 files changed

+69
-78
lines changed

docs/sphinx/source/reference/effects_on_pv_system_output.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ Spectrum
5252
spectrum.get_example_spectral_response
5353
spectrum.get_am15g
5454
spectrum.calc_spectral_mismatch_field
55-
spectrum.martin_ruiz_spectral_modifier
55+
spectrum.martin_ruiz

pvlib/spectrum/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from pvlib.spectrum.spectrl2 import spectrl2 # noqa: F401
22
from pvlib.spectrum.mismatch import (get_example_spectral_response, get_am15g,
33
calc_spectral_mismatch_field,
4-
martin_ruiz_spectral_modifier)
4+
martin_ruiz)

pvlib/spectrum/mismatch.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import pandas as pd
88
from scipy.interpolate import interp1d
99
import os
10-
from warnings import warn
1110

1211

1312
def get_example_spectral_response(wavelength=None):
@@ -238,8 +237,8 @@ def integrate(e):
238237
return smm
239238

240239

241-
def martin_ruiz_spectral_modifier(clearness_index, airmass_absolute,
242-
cell_type=None, model_parameters=None):
240+
def martin_ruiz(clearness_index, airmass_absolute, module_type=None,
241+
model_parameters=None):
243242
r"""
244243
Calculate spectral mismatch modifiers for POA direct, sky diffuse and
245244
ground diffuse irradiances using the clearness index and the absolute
@@ -261,7 +260,7 @@ def martin_ruiz_spectral_modifier(clearness_index, airmass_absolute,
261260
recommended for default parameters of ``monosi``, ``polysi`` and
262261
``asi``, see [1]_).
263262
264-
cell_type : string, optional
263+
module_type : string, optional
265264
Specifies material of the cell in order to infer model parameters.
266265
Allowed types are ``monosi``, ``polysi`` and ``asi``, either lower or
267266
upper case. If not specified, ``model_parameters`` must be provided.
@@ -272,7 +271,7 @@ def martin_ruiz_spectral_modifier(clearness_index, airmass_absolute,
272271
.. code-block:: python
273272
274273
# Using a dict
275-
# Return keys are the same as specifying 'cell_type'
274+
# Return keys are the same as specifying 'module_type'
276275
model_parameters = {
277276
'poa_direct': {'c': c1, 'a': a1, 'b': b1},
278277
'poa_sky_diffuse': {'c': c2, 'a': a2, 'b': b2},
@@ -305,9 +304,9 @@ def martin_ruiz_spectral_modifier(clearness_index, airmass_absolute,
305304
ValueError
306305
If ``model_parameters`` is not suitable. See examples given above.
307306
TypeError
308-
If neither ``cell_type`` nor ``model_parameters`` are given.
307+
If neither ``module_type`` nor ``model_parameters`` are given.
309308
NotImplementedError
310-
If ``cell_type`` is not found in internal table of parameters.
309+
If ``module_type`` is not found in internal table of parameters.
311310
312311
Notes
313312
-----
@@ -322,7 +321,7 @@ def martin_ruiz_spectral_modifier(clearness_index, airmass_absolute,
322321
----------
323322
.. [1] Martín, N. and Ruiz, J.M. (1999), A new method for the spectral
324323
characterisation of PV modules. Prog. Photovolt: Res. Appl., 7: 299-310.
325-
:doi:10.1002/(SICI)1099-159X(199907/08)7:4<299::AID-PIP260>3.0.CO;2-0
324+
:doi:`10.1002/(SICI)1099-159X(199907/08)7:4<299::AID-PIP260>3.0.CO;2-0`
326325
327326
See Also
328327
--------
@@ -346,39 +345,39 @@ def martin_ruiz_spectral_modifier(clearness_index, airmass_absolute,
346345
])
347346

348347
# Argument validation and choose components and model parameters
349-
if cell_type is not None and model_parameters is None:
348+
if module_type is not None and model_parameters is None:
350349
# Infer parameters from cell material
351-
cell_type_lower = cell_type.lower()
352-
if cell_type_lower in MARTIN_RUIZ_PARAMS.index:
353-
_params = MARTIN_RUIZ_PARAMS.loc[cell_type_lower]
350+
module_type_lower = module_type.lower()
351+
if module_type_lower in MARTIN_RUIZ_PARAMS.index:
352+
_params = MARTIN_RUIZ_PARAMS.loc[module_type_lower]
354353
else:
355354
raise NotImplementedError('Cell type parameters not defined in '
356355
'algorithm. Allowed types are '
357356
f'{tuple(MARTIN_RUIZ_PARAMS.index)}')
358-
elif cell_type is None and model_parameters is None:
359-
raise TypeError('You must pass at least "cell_type" '
360-
'or "model_parameters" as arguments.')
361-
elif model_parameters is not None: # Use user-defined model parameters
357+
elif model_parameters is not None and module_type is None:
358+
# Use user-defined model parameters
362359
# Validate 'model_parameters' sub-dicts keys
363360
if any([{'a', 'b', 'c'} != set(model_parameters[component].keys())
364361
for component in model_parameters.keys()]):
365362
raise ValueError("You must specify model parameters with keys "
366363
"'a','b','c' for each irradiation component.")
367-
368364
_params = model_parameters
369-
if cell_type is not None:
370-
warn('Both "cell_type" and "model_parameters" given. '
371-
'Using provided "model_parameters".')
365+
elif module_type is None and model_parameters is None:
366+
raise TypeError('You must pass at least "module_type" '
367+
'or "model_parameters" as arguments.')
368+
elif model_parameters is not None and module_type is not None:
369+
raise TypeError('Cannot resolve input: must supply only one of '
370+
'"module_type" or "model_parameters"')
371+
372+
if np.isscalar(clearness_index) and np.isscalar(airmass_absolute):
373+
modifiers = dict(zip(IRRAD_COMPONENTS, (np.nan,)*3))
374+
else:
375+
modifiers = pd.DataFrame(columns=IRRAD_COMPONENTS)
372376

373377
# Compute difference here to avoid recalculating inside loop
374378
kt_delta = clearness_index - 0.74
375379
am_delta = airmass_absolute - 1.5
376380

377-
if hasattr(kt_delta, '__iter__') or hasattr(am_delta, '__iter__'):
378-
modifiers = pd.DataFrame(columns=IRRAD_COMPONENTS)
379-
else:
380-
modifiers = dict(zip(IRRAD_COMPONENTS, (np.nan,)*3))
381-
382381
# Calculate mismatch modifier for each irradiation
383382
for irrad_type in IRRAD_COMPONENTS:
384383
# Skip irradiations not specified in 'model_params'

pvlib/tests/test_spectrum.py

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def test_calc_spectral_mismatch_field(spectrl2_data):
176176

177177
@pytest.fixture
178178
def martin_ruiz_mismatch_data():
179-
# Data to run tests of martin_ruiz_spectral_modifier
179+
# Data to run tests of spectrum.martin_ruiz
180180
kwargs = {
181181
'clearness_index': [0.56, 0.612, 0.664, 0.716, 0.768, 0.82],
182182
'airmass_absolute': [2, 1.8, 1.6, 1.4, 1.2, 1],
@@ -205,12 +205,12 @@ def martin_ruiz_mismatch_data():
205205

206206

207207
def test_martin_ruiz_mm_scalar(martin_ruiz_mismatch_data):
208-
# test scalar input ; only cell_type given
208+
# test scalar input ; only module_type given
209209
clearness_index = martin_ruiz_mismatch_data['clearness_index'][0]
210210
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')
211+
result = spectrum.martin_ruiz(clearness_index,
212+
airmass_absolute,
213+
module_type='asi')
214214

215215
assert_approx_equal(result['poa_direct'],
216216
martin_ruiz_mismatch_data['asi_expected']['dir'][0],
@@ -224,31 +224,29 @@ def test_martin_ruiz_mm_scalar(martin_ruiz_mismatch_data):
224224

225225

226226
def test_martin_ruiz_mm_series(martin_ruiz_mismatch_data):
227-
# test with Series input ; only cell_type given
227+
# test with Series input ; only module_type given
228228
clearness_index = pd.Series(martin_ruiz_mismatch_data['clearness_index'])
229229
airmass_absolute = pd.Series(martin_ruiz_mismatch_data['airmass_absolute'])
230230
expected = pd.DataFrame(data={
231231
'dir': pd.Series(martin_ruiz_mismatch_data['polysi_expected']['dir']),
232232
'sky': pd.Series(martin_ruiz_mismatch_data['polysi_expected']['sky']),
233233
'gnd': pd.Series(martin_ruiz_mismatch_data['polysi_expected']['gnd'])})
234234

235-
result = spectrum.martin_ruiz_spectral_modifier(clearness_index,
236-
airmass_absolute,
237-
cell_type='polysi')
235+
result = spectrum.martin_ruiz(clearness_index, airmass_absolute,
236+
module_type='polysi')
238237
assert_allclose(result['poa_direct'], expected['dir'], atol=1e-5)
239238
assert_allclose(result['poa_sky_diffuse'], expected['sky'], atol=1e-5)
240239
assert_allclose(result['poa_ground_diffuse'], expected['gnd'], atol=1e-5)
241240

242241

243242
def test_martin_ruiz_mm_nans(martin_ruiz_mismatch_data):
244-
# test NaN in, NaN out ; only cell_type given
243+
# test NaN in, NaN out ; only module_type given
245244
clearness_index = pd.Series(martin_ruiz_mismatch_data['clearness_index'])
246245
airmass_absolute = pd.Series(martin_ruiz_mismatch_data['airmass_absolute'])
247246
airmass_absolute[:5] = np.nan
248247

249-
result = spectrum.martin_ruiz_spectral_modifier(clearness_index,
250-
airmass_absolute,
251-
cell_type='monosi')
248+
result = spectrum.martin_ruiz(clearness_index, airmass_absolute,
249+
module_type='monosi')
252250
assert np.isnan(result['poa_direct'][:5]).all()
253251
assert not np.isnan(result['poa_direct'][5:]).any()
254252
assert np.isnan(result['poa_sky_diffuse'][:5]).all()
@@ -268,7 +266,7 @@ def test_martin_ruiz_mm_model_dict(martin_ruiz_mismatch_data):
268266
'gnd': pd.Series(martin_ruiz_mismatch_data['monosi_expected']['gnd'])})
269267
model_parameters = martin_ruiz_mismatch_data['monosi_model_params_dict']
270268

271-
result = spectrum.martin_ruiz_spectral_modifier(
269+
result = spectrum.martin_ruiz(
272270
clearness_index,
273271
airmass_absolute,
274272
model_parameters=model_parameters)
@@ -287,7 +285,7 @@ def test_martin_ruiz_mm_model_df(martin_ruiz_mismatch_data):
287285
'dir': np.array(martin_ruiz_mismatch_data['monosi_expected']['dir']),
288286
'sky': np.array(martin_ruiz_mismatch_data['monosi_expected']['sky'])})
289287

290-
result = spectrum.martin_ruiz_spectral_modifier(
288+
result = spectrum.martin_ruiz(
291289
clearness_index,
292290
airmass_absolute,
293291
model_parameters=model_parameters)
@@ -296,44 +294,15 @@ def test_martin_ruiz_mm_model_df(martin_ruiz_mismatch_data):
296294
assert result['poa_ground_diffuse'].isna().all()
297295

298296

299-
def test_martin_ruiz_mm_userwarning(martin_ruiz_mismatch_data):
300-
# test warning is raised with both 'cell_type' and 'model_parameters'
301-
clearness_index = pd.Series(martin_ruiz_mismatch_data['clearness_index'])
302-
airmass_absolute = pd.Series(martin_ruiz_mismatch_data['airmass_absolute'])
303-
model_parameters = martin_ruiz_mismatch_data['monosi_model_params_dict']
304-
305-
with pytest.warns(UserWarning,
306-
match='Both "cell_type" and "model_parameters" given. '
307-
'Using provided "model_parameters".'):
308-
_ = spectrum.martin_ruiz_spectral_modifier(
309-
clearness_index,
310-
airmass_absolute,
311-
cell_type='asi',
312-
model_parameters=model_parameters)
313-
314-
315297
def test_martin_ruiz_mm_error_notimplemented(martin_ruiz_mismatch_data):
316-
# test exception is raised when cell_type does not exist in algorithm
298+
# test exception is raised when module_type does not exist in algorithm
317299
clearness_index = np.array(martin_ruiz_mismatch_data['clearness_index'])
318300
airmass_absolute = np.array(martin_ruiz_mismatch_data['airmass_absolute'])
319301

320302
with pytest.raises(NotImplementedError,
321303
match='Cell type parameters not defined in algorithm.'):
322-
_ = spectrum.martin_ruiz_spectral_modifier(clearness_index,
323-
airmass_absolute,
324-
cell_type='')
325-
326-
327-
def test_martin_ruiz_mm_error_missing_params(martin_ruiz_mismatch_data):
328-
# test exception is raised when missing cell_type and model_parameters
329-
clearness_index = np.array(martin_ruiz_mismatch_data['clearness_index'])
330-
airmass_absolute = np.array(martin_ruiz_mismatch_data['airmass_absolute'])
331-
332-
with pytest.raises(TypeError,
333-
match='You must pass at least "cell_type" '
334-
'or "model_parameters" as arguments.'):
335-
_ = spectrum.martin_ruiz_spectral_modifier(clearness_index,
336-
airmass_absolute)
304+
_ = spectrum.martin_ruiz(clearness_index, airmass_absolute,
305+
module_type='')
337306

338307

339308
def test_martin_ruiz_mm_error_model_keys(martin_ruiz_mismatch_data):
@@ -345,7 +314,30 @@ def test_martin_ruiz_mm_error_model_keys(martin_ruiz_mismatch_data):
345314
with pytest.raises(ValueError,
346315
match="You must specify model parameters with keys "
347316
"'a','b','c' for each irradiation component."):
348-
_ = spectrum.martin_ruiz_spectral_modifier(
349-
clearness_index,
350-
airmass_absolute,
351-
model_parameters=model_parameters)
317+
_ = spectrum.martin_ruiz(clearness_index, airmass_absolute,
318+
model_parameters=model_parameters)
319+
320+
321+
def test_martin_ruiz_mm_error_missing_params(martin_ruiz_mismatch_data):
322+
# test exception is raised when missing module_type and model_parameters
323+
clearness_index = np.array(martin_ruiz_mismatch_data['clearness_index'])
324+
airmass_absolute = np.array(martin_ruiz_mismatch_data['airmass_absolute'])
325+
326+
with pytest.raises(TypeError,
327+
match='You must pass at least "module_type" '
328+
'or "model_parameters" as arguments.'):
329+
_ = spectrum.martin_ruiz(clearness_index, airmass_absolute)
330+
331+
332+
def test_martin_ruiz_mm_error_too_many_arguments(martin_ruiz_mismatch_data):
333+
# test warning is raised with both 'module_type' and 'model_parameters'
334+
clearness_index = pd.Series(martin_ruiz_mismatch_data['clearness_index'])
335+
airmass_absolute = pd.Series(martin_ruiz_mismatch_data['airmass_absolute'])
336+
model_parameters = martin_ruiz_mismatch_data['monosi_model_params_dict']
337+
338+
with pytest.raises(TypeError,
339+
match='Cannot resolve input: must supply only one of '
340+
'"module_type" or "model_parameters"'):
341+
_ = spectrum.martin_ruiz(clearness_index, airmass_absolute,
342+
module_type='asi',
343+
model_parameters=model_parameters)

0 commit comments

Comments
 (0)