-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtest_iam.py
More file actions
581 lines (442 loc) · 19.1 KB
/
test_iam.py
File metadata and controls
581 lines (442 loc) · 19.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
"""
Created on Wed Oct 2 10:14:16 2019
@author: cwhanse
"""
import numpy as np
import pandas as pd
import pytest
from .conftest import assert_series_equal
from numpy.testing import assert_allclose
from pvlib import iam as _iam
def test_ashrae():
thetas = np.array([-90., -67.5, -45., -22.5, 0., 22.5, 45., 67.5, 89., 90.,
np.nan])
expected = np.array([0, 0.9193437, 0.97928932, 0.99588039, 1., 0.99588039,
0.97928932, 0.9193437, 0, 0, np.nan])
iam = _iam.ashrae(thetas, .05)
assert_allclose(iam, expected, equal_nan=True)
iam_series = _iam.ashrae(pd.Series(thetas))
assert_series_equal(iam_series, pd.Series(expected))
def test_ashrae_scalar():
thetas = -45.
iam = _iam.ashrae(thetas, .05)
expected = 0.97928932
assert_allclose(iam, expected, equal_nan=True)
thetas = np.nan
iam = _iam.ashrae(thetas, .05)
expected = np.nan
assert_allclose(iam, expected, equal_nan=True)
def test_physical():
aoi = np.array([-90., -67.5, -45., -22.5, 0., 22.5, 45., 67.5, 90.,
np.nan])
expected = np.array([0, 0.8893998, 0.98797788, 0.99926198, 1, 0.99926198,
0.98797788, 0.8893998, 0, np.nan])
iam = _iam.physical(aoi, 1.526, 0.002, 4)
assert_allclose(iam, expected, atol=1e-7, equal_nan=True)
# GitHub issue 397
aoi = pd.Series(aoi)
iam = _iam.physical(aoi, 1.526, 0.002, 4)
expected = pd.Series(expected)
assert_series_equal(iam, expected)
def test_physical_n1_L0():
aoi = np.array([0, 22.5, 45, 67.5, 90, 100, np.nan])
expected = np.array([1, 1, 1, 1, 0, 0, np.nan])
iam = _iam.physical(aoi, n=1, L=0)
assert_allclose(iam, expected, equal_nan=True)
aoi = pd.Series(aoi)
expected = pd.Series(expected)
iam = _iam.physical(aoi, n=1, L=0)
assert_series_equal(iam, expected)
def test_physical_ar():
aoi = np.array([0, 22.5, 45, 67.5, 90, 100, np.nan])
expected = np.array([1, 0.99944171, 0.9917463, 0.91506158, 0, 0, np.nan])
iam = _iam.physical(aoi, n_ar=1.29)
assert_allclose(iam, expected, atol=1e-7, equal_nan=True)
def test_physical_noar():
aoi = np.array([0, 22.5, 45, 67.5, 90, 100, np.nan])
expected = _iam.physical(aoi)
iam0 = _iam.physical(aoi, n_ar=1)
iam1 = _iam.physical(aoi, n_ar=1.526)
assert_allclose(iam0, expected, equal_nan=True)
assert_allclose(iam1, expected, equal_nan=True)
def test_physical_scalar():
aoi = -45.
iam = _iam.physical(aoi, 1.526, 0.002, 4)
expected = 0.98797788
assert_allclose(iam, expected, equal_nan=True)
aoi = np.nan
iam = _iam.physical(aoi, 1.526, 0.002, 4)
expected = np.nan
assert_allclose(iam, expected, equal_nan=True)
def test_martin_ruiz():
aoi = 45.
a_r = 0.16
expected = 0.98986965
# will fail if default values change
iam = _iam.martin_ruiz(aoi)
assert_allclose(iam, expected)
# will fail if parameter names change
iam = _iam.martin_ruiz(aoi=aoi, a_r=a_r)
assert_allclose(iam, expected)
a_r = 0.18
aoi = [-100, -60, 0, 60, 100, np.nan, np.inf]
expected = [0.0, 0.9414631, 1.0, 0.9414631, 0.0, np.nan, 0.0]
# check out of range of inputs as list
iam = _iam.martin_ruiz(aoi, a_r)
assert_allclose(iam, expected, equal_nan=True)
# check out of range of inputs as array
iam = _iam.martin_ruiz(np.array(aoi), a_r)
assert_allclose(iam, expected, equal_nan=True)
# check out of range of inputs as Series
aoi = pd.Series(aoi)
expected = pd.Series(expected)
iam = _iam.martin_ruiz(aoi, a_r)
assert_series_equal(iam, expected)
def test_martin_ruiz_exception():
with pytest.raises(ValueError):
_iam.martin_ruiz(0.0, a_r=0.0)
def test_martin_ruiz_diffuse():
surface_tilt = 30.
a_r = 0.16
expected = (0.9549735, 0.7944426)
# will fail if default values change
iam = _iam.martin_ruiz_diffuse(surface_tilt)
assert_allclose(iam, expected)
# will fail if parameter names change
iam = _iam.martin_ruiz_diffuse(surface_tilt=surface_tilt, a_r=a_r)
assert_allclose(iam, expected)
a_r = 0.18
surface_tilt = [0, 30, 90, 120, 180, np.nan, np.inf]
expected_sky = [0.9407678, 0.9452250, 0.9407678, 0.9055541, 0.0000000,
np.nan, np.nan]
expected_gnd = [0.0000000, 0.7610849, 0.9407678, 0.9483508, 0.9407678,
np.nan, np.nan]
# check various inputs as list
iam = _iam.martin_ruiz_diffuse(surface_tilt, a_r)
assert_allclose(iam[0], expected_sky, atol=1e-7, equal_nan=True)
assert_allclose(iam[1], expected_gnd, atol=1e-7, equal_nan=True)
# check various inputs as array
iam = _iam.martin_ruiz_diffuse(np.array(surface_tilt), a_r)
assert_allclose(iam[0], expected_sky, atol=1e-7, equal_nan=True)
assert_allclose(iam[1], expected_gnd, atol=1e-7, equal_nan=True)
# check various inputs as Series
surface_tilt = pd.Series(surface_tilt)
expected_sky = pd.Series(expected_sky, name='iam_sky')
expected_gnd = pd.Series(expected_gnd, name='iam_ground')
iam = _iam.martin_ruiz_diffuse(surface_tilt, a_r)
assert_series_equal(iam[0], expected_sky)
assert_series_equal(iam[1], expected_gnd)
def test_iam_interp():
aoi_meas = [0.0, 45.0, 65.0, 75.0]
iam_meas = [1.0, 0.9, 0.8, 0.6]
# simple default linear method
aoi = 55.0
expected = 0.85
iam = _iam.interp(aoi, aoi_meas, iam_meas)
assert_allclose(iam, expected)
# simple non-default method
aoi = 55.0
expected = 0.8878062
iam = _iam.interp(aoi, aoi_meas, iam_meas, method='cubic')
assert_allclose(iam, expected)
# check with all reference values
aoi = aoi_meas
expected = iam_meas
iam = _iam.interp(aoi, aoi_meas, iam_meas)
assert_allclose(iam, expected)
# check normalization and Series
aoi = pd.Series(aoi)
expected = pd.Series(expected)
iam_mult = np.multiply(0.9, iam_meas)
iam = _iam.interp(aoi, aoi_meas, iam_mult, normalize=True)
assert_series_equal(iam, expected)
# check beyond reference values
aoi = [-45, 0, 45, 85, 90, 95, 100, 105, 110]
expected = [0.9, 1.0, 0.9, 0.4, 0.3, 0.2, 0.1, 0.0, 0.0]
iam = _iam.interp(aoi, aoi_meas, iam_meas)
assert_allclose(iam, expected)
# check exception clause
with pytest.raises(ValueError):
_iam.interp(0.0, [0], [1])
# check exception clause
with pytest.raises(ValueError):
_iam.interp(0.0, [0, 90], [1, -1])
# check linear after updating interp1d
theta_ref = np.array([0, 60, 90])
iam_ref = np.array([1.0, 0.8, 0.0])
aoi = np.array([0, 30, 60])
iam = _iam.interp(
aoi, theta_ref, iam_ref,
method="linear", normalize=False)
expected = np.array([1.0, 0.9, 0.8])
np.testing.assert_allclose(iam, expected)
# check quadratic
theta_ref = np.array([0, 30, 60, 90])
iam_ref = 1.0 - 1e-4 * theta_ref**2
aoi = np.array([15, 45, 75])
iam = _iam.interp(
aoi,
theta_ref,
iam_ref,
method="quadratic",
normalize=False
)
expected = 1.0 - 1e-4 * aoi**2
np.testing.assert_allclose(iam, expected, rtol=1e-12)
@pytest.mark.parametrize('aoi,expected', [
(45, 0.9975036250000002),
(np.array([[-30, 30, 100, np.nan]]),
np.array([[0, 1.007572, 0, np.nan]])),
(pd.Series([80]), pd.Series([0.597472]))
])
def test_sapm(sapm_module_params, aoi, expected):
out = _iam.sapm(aoi, sapm_module_params)
if isinstance(aoi, pd.Series):
assert_series_equal(out, expected, check_less_precise=4)
else:
assert_allclose(out, expected, atol=1e-4)
def test_sapm_limits():
module_parameters = {'B0': 5, 'B1': 0, 'B2': 0, 'B3': 0, 'B4': 0, 'B5': 0}
assert _iam.sapm(1, module_parameters) == 5
module_parameters = {'B0': 5, 'B1': 0, 'B2': 0, 'B3': 0, 'B4': 0, 'B5': 0}
assert _iam.sapm(1, module_parameters, upper=1) == 1
module_parameters = {'B0': -5, 'B1': 0, 'B2': 0, 'B3': 0, 'B4': 0, 'B5': 0}
assert _iam.sapm(1, module_parameters) == 0
def test_marion_diffuse_model(mocker):
# 1: return values are correct
# 2: the underlying models are called appropriately
ashrae_expected = {
'sky': 0.9596085829811408,
'horizon': 0.8329070417832541,
'ground': 0.719823559106309
}
physical_expected = {
'sky': 0.9539178294437575,
'horizon': 0.7652650139134007,
'ground': 0.6387140117795903
}
ashrae_spy = mocker.spy(_iam, 'ashrae')
physical_spy = mocker.spy(_iam, 'physical')
ashrae_actual = _iam.marion_diffuse('ashrae', 20)
assert ashrae_spy.call_count == 3 # one call for each of the 3 regions
assert physical_spy.call_count == 0
physical_actual = _iam.marion_diffuse('physical', 20)
assert ashrae_spy.call_count == 3
assert physical_spy.call_count == 3
for k, v in ashrae_expected.items():
assert_allclose(ashrae_actual[k], v)
for k, v in physical_expected.items():
assert_allclose(physical_actual[k], v)
def test_marion_diffuse_kwargs():
# kwargs get passed to underlying model
expected = {
'sky': 0.967489994422575,
'horizon': 0.8647842827418412,
'ground': 0.7700443455928433
}
actual = _iam.marion_diffuse('ashrae', 20, b=0.04)
for k, v in expected.items():
assert_allclose(actual[k], v)
def test_marion_diffuse_invalid():
with pytest.raises(ValueError):
_iam.marion_diffuse('not_a_model', 20)
@pytest.mark.parametrize('region,N,expected', [
('sky', 180, 0.9596085829811408),
('horizon', 1800, 0.8329070417832541),
('ground', 180, 0.719823559106309)
])
def test_marion_integrate_scalar(region, N, expected):
actual = _iam.marion_integrate(_iam.ashrae, 20, region, N)
assert_allclose(actual, expected)
with np.errstate(invalid='ignore'):
actual = _iam.marion_integrate(_iam.ashrae, np.nan, region, N)
expected = np.nan
assert_allclose(actual, expected)
@pytest.mark.parametrize('region,N,expected', [
('sky', 180, [0.9523611991069362, 0.9596085829811408, 0.9619811198105501]),
('horizon', 1800, [0.0, 0.8329070417832541, 0.8987287652347437]),
('ground', 180, [0.0, 0.719823559106309, 0.8186360238536674])
])
def test_marion_integrate_list(region, N, expected):
actual = _iam.marion_integrate(_iam.ashrae, [0, 20, 30], region, N)
assert_allclose(actual, expected)
with np.errstate(invalid='ignore'):
actual = _iam.marion_integrate(_iam.ashrae, [0, 20, np.nan], region, N)
assert_allclose(actual, [expected[0], expected[1], np.nan])
@pytest.mark.parametrize('region,N,expected', [
('sky', 180, [0.9523611991069362, 0.9596085829811408, 0.9619811198105501]),
('horizon', 1800, [0.0, 0.8329070417832541, 0.8987287652347437]),
('ground', 180, [0.0, 0.719823559106309, 0.8186360238536674])
])
def test_marion_integrate_series(region, N, expected):
idx = pd.date_range('2019-01-01', periods=3, freq='h')
tilt = pd.Series([0, 20, 30], index=idx)
expected = pd.Series(expected, index=idx)
actual = _iam.marion_integrate(_iam.ashrae, tilt, region, N)
assert_series_equal(actual, expected)
tilt.iloc[1] = np.nan
expected.iloc[1] = np.nan
with np.errstate(invalid='ignore'):
actual = _iam.marion_integrate(_iam.ashrae, tilt, region, N)
assert_allclose(actual, expected)
def test_marion_integrate_ground_flat():
iam = _iam.marion_integrate(_iam.ashrae, 0, 'horizon', 1800)
assert_allclose(iam, 0)
def test_marion_integrate_invalid():
# check for invalid region string. this actually gets checked twice,
# with the difference being whether `num` is specified or not.
with pytest.raises(ValueError):
_iam.marion_integrate(_iam.ashrae, 0, 'bad')
with pytest.raises(ValueError):
_iam.marion_integrate(_iam.ashrae, 0, 'bad', 180)
def test_schlick():
idx = pd.date_range('2019-01-01', freq='h', periods=9)
aoi = pd.Series([-180, -135, -90, -45, 0, 45, 90, 135, 180], idx)
expected = pd.Series([0, 0, 0, 0.99784451, 1.0, 0.99784451, 0, 0, 0], idx)
# scalars
for aoi_scalar, expected_scalar in zip(aoi, expected):
actual = _iam.schlick(aoi_scalar)
assert_allclose(expected_scalar, actual)
# numpy arrays
actual = _iam.schlick(aoi.values)
assert_allclose(expected.values, actual)
# pandas Series
actual = _iam.schlick(aoi)
assert_series_equal(expected, actual)
def test_schlick_diffuse():
surface_tilt = np.array([0, 20, 70, 90])
# expected values calculated with marion_integrate and schlick
expected_sky = np.array([0.95238092, 0.96249934, 0.96228167, 0.95238094])
expected_ground = np.array([0, 0.62693858, 0.93218737, 0.95238094])
# numpy arrays
actual_sky, actual_ground = _iam.schlick_diffuse(surface_tilt)
assert_allclose(expected_sky, actual_sky)
assert_allclose(expected_ground, actual_ground, rtol=1e-6)
# scalars
for i in range(len(surface_tilt)):
actual_sky, actual_ground = _iam.schlick_diffuse(surface_tilt[i])
assert_allclose(expected_sky[i], actual_sky)
assert_allclose(expected_ground[i], actual_ground, rtol=1e-6)
# pandas Series
idx = pd.date_range('2019-01-01', freq='h', periods=len(surface_tilt))
actual_sky, actual_ground = _iam.schlick_diffuse(pd.Series(surface_tilt,
idx))
assert_series_equal(pd.Series(expected_sky, idx), actual_sky)
assert_series_equal(pd.Series(expected_ground, idx), actual_ground,
rtol=1e-6)
@pytest.mark.parametrize('source,source_params,target,expected', [
('physical', {'n': 1.5, 'K': 4.5, 'L': 0.004}, 'martin_ruiz',
{'a_r': 0.174037}),
('physical', {'n': 1.5, 'K': 4.5, 'L': 0.004}, 'ashrae',
{'b': 0.042896}),
('ashrae', {'b': 0.15}, 'physical',
{'n': 0.991457, 'K': 4, 'L': 0.037813}),
('ashrae', {'b': 0.15}, 'martin_ruiz', {'a_r': 0.302390}),
('martin_ruiz', {'a_r': 0.15}, 'physical',
{'n': 1.240190, 'K': 4, 'L': 0.002791055}),
('martin_ruiz', {'a_r': 0.15}, 'ashrae', {'b': 0.025458})])
def test_convert(source, source_params, target, expected):
target_params = _iam.convert(source, source_params, target)
exp = [expected[k] for k in expected]
tar = [target_params[k] for k in expected]
assert_allclose(exp, tar, rtol=1e-05)
@pytest.mark.parametrize('source,source_params', [
('ashrae', {'b': 0.15}),
('ashrae', {'b': 0.05}),
('martin_ruiz', {'a_r': 0.15})])
def test_convert_recover(source, source_params):
# convert isn't set up to handle both source and target = 'physical'
target_params = _iam.convert(source, source_params, source, xtol=1e-7)
exp = [source_params[k] for k in source_params]
tar = [target_params[k] for k in source_params]
assert_allclose(exp, tar, rtol=1e-05)
def test_convert_ashrae_physical_no_fix_n():
# convert ashrae to physical, without fixing n
source_params = {'b': 0.15}
target_params = _iam.convert('ashrae', source_params, 'physical',
fix_n=False)
expected = {'n': 0.989019, 'K': 4, 'L': 0.037382}
exp = [expected[k] for k in expected]
tar = [target_params[k] for k in expected]
assert_allclose(exp, tar, rtol=1e-05)
def test_convert_reverse_order_in_physical():
source_params = {'a_r': 0.25}
target_params = _iam.convert('martin_ruiz', source_params, 'physical')
expected = {'n': 1.691398, 'K': 4, 'L': 0.071633}
exp = [expected[k] for k in expected]
tar = [target_params[k] for k in expected]
assert_allclose(exp, tar, rtol=1e-05)
def test_convert_xtol():
source_params = {'b': 0.15}
target_params = _iam.convert('ashrae', source_params, 'physical',
xtol=1e-8)
expected = {'n': 0.9914568914, 'K': 4, 'L': 0.0378126985}
exp = [expected[k] for k in expected]
tar = [target_params[k] for k in expected]
assert_allclose(exp, tar, rtol=1e-6)
def test_convert_custom_weight_func():
aoi = np.linspace(0, 90, 91)
# convert physical to martin_ruiz, using custom weight function
source_params = {'n': 1.5, 'K': 4.5, 'L': 0.004}
source_iam = _iam.physical(aoi, **source_params)
# define custom weight function that takes in other arguments
def scaled_weight(aoi):
return 2. * aoi
# expected value calculated from computing residual function over
# a range of inputs, and taking minimum of these values
expected_min_res = 16.39724
actual_dict = _iam.convert('physical', source_params, 'martin_ruiz',
weight=scaled_weight)
actual_min_res = _iam._residual(aoi, source_iam, _iam.martin_ruiz,
[actual_dict['a_r']], scaled_weight)
assert np.isclose(expected_min_res, actual_min_res, atol=1e-06)
def test_convert_model_not_implemented():
with pytest.raises(NotImplementedError, match='model has not been'):
_iam.convert('ashrae', {'b': 0.1}, 'foo')
def test_convert_wrong_model_parameters():
with pytest.raises(ValueError, match='model was expecting'):
_iam.convert('ashrae', {'B': 0.1}, 'physical')
def test_convert__minimize_fails():
# to make scipy.optimize.minimize fail, we'll pass in a nonsense
# weight function that only outputs nans
def nan_weight(aoi):
return np.nan
with pytest.raises(RuntimeError, match='Optimizer exited unsuccessfully'):
_iam.convert('ashrae', {'b': 0.1}, 'physical', weight=nan_weight)
def test_fit():
aoi = np.linspace(0, 90, 5)
perturb = np.array([1.2, 1.01, 0.95, 1, 0.98])
perturbed_iam = _iam.martin_ruiz(aoi, a_r=0.14) * perturb
expected_a_r = 0.14
actual_dict = _iam.fit(aoi, perturbed_iam, 'martin_ruiz')
actual_a_r = actual_dict['a_r']
assert np.isclose(expected_a_r, actual_a_r, atol=1e-04)
def test_fit_custom_weight_func():
# define custom weight function that takes in other arguments
def scaled_weight(aoi):
return 2. * aoi
aoi = np.linspace(0, 90, 5)
perturb = np.array([1.2, 1.01, 0.95, 1, 0.98])
perturbed_iam = _iam.martin_ruiz(aoi, a_r=0.14) * perturb
expected_a_r = 0.14
actual_dict = _iam.fit(aoi, perturbed_iam, 'martin_ruiz',
weight=scaled_weight)
actual_a_r = actual_dict['a_r']
assert np.isclose(expected_a_r, actual_a_r, atol=1e-04)
def test_fit_model_not_implemented():
with pytest.raises(NotImplementedError, match='model has not been'):
_iam.fit(np.array([0, 10]), np.array([1, 0.99]), 'foo')
def test_fit__minimize_fails():
# to make scipy.optimize.minimize fail, we'll pass in a nonsense
# weight function that only outputs nans
def nan_weight(aoi):
return np.nan
with pytest.raises(RuntimeError, match='Optimizer exited unsuccessfully'):
_iam.fit(np.array([0, 10]), np.array([1, 0.99]), 'physical',
weight=nan_weight)
def test__residual_zero_outside_range():
# check that _residual annihilates any weights that come from aoi
# outside of interval [0, 90] (this is important for `iam.fit`, when
# the `measured_aoi` contains angles outside this range
residual = _iam._residual(101, _iam.ashrae(101), _iam.martin_ruiz, [0.16])
assert residual == 0.0