Skip to content

Commit f7dadbb

Browse files
committed
remove array inputs
1 parent aa3b2ac commit f7dadbb

File tree

2 files changed

+19
-55
lines changed

2 files changed

+19
-55
lines changed

pvlib/clearsky.py

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ def _clear_sample_index(clear_windows, samples_per_window, align, H):
848848
return clear_samples
849849

850850

851-
def detect_clearsky(measured, clearsky, times=None, window_length=10,
851+
def detect_clearsky(measured, clearsky, window_length=10,
852852
mean_diff=75, max_diff=75,
853853
lower_line_length=-5, upper_line_length=10,
854854
var_diff=0.005, slope_dev=8, max_iterations=20,
@@ -873,13 +873,10 @@ def detect_clearsky(measured, clearsky, times=None, window_length=10,
873873
874874
Parameters
875875
----------
876-
measured : array or Series
876+
measured : Series
877877
Time series of measured GHI. [W/m2]
878-
clearsky : array or Series
878+
clearsky : Series
879879
Time series of the expected clearsky GHI. [W/m2]
880-
times : DatetimeIndex or None, default None.
881-
Times of measured and clearsky values. If None the index of measured
882-
will be used.
883880
window_length : int, default 10
884881
Length of sliding time window in minutes. Must be greater than 2
885882
periods.
@@ -910,9 +907,9 @@ def detect_clearsky(measured, clearsky, times=None, window_length=10,
910907
911908
Returns
912909
-------
913-
clear_samples : array or Series
914-
Boolean array or Series of whether or not the given time is
915-
clear. Return type is the same as the input type.
910+
clear_samples : Series
911+
Series of boolean indicating whether or not the given time is
912+
clear.
916913
917914
components : OrderedDict, optional
918915
Dict of arrays of whether or not the given time window is clear
@@ -925,8 +922,6 @@ def detect_clearsky(measured, clearsky, times=None, window_length=10,
925922
926923
Raises
927924
------
928-
ValueError
929-
If measured is not a Series and times is not provided
930925
NotImplementedError
931926
If timestamps are not equally spaced
932927
@@ -954,45 +949,25 @@ def detect_clearsky(measured, clearsky, times=None, window_length=10,
954949
* uses centered windows (Matlab function uses left-aligned windows)
955950
"""
956951

957-
if times is None:
958-
try:
959-
times = measured.index
960-
except AttributeError:
961-
raise ValueError("times is required when measured is not a Series")
962-
963-
# be polite about returning the same type as was input
964-
ispandas = isinstance(measured, pd.Series)
965-
966-
# for internal use, need a Series
967-
if not ispandas:
968-
meas = pd.Series(measured, index=times)
969-
else:
970-
meas = measured
971-
972-
if not isinstance(clearsky, pd.Series):
973-
clear = pd.Series(clearsky, index=times)
974-
else:
975-
clear = clearsky
976-
977-
sample_interval, samples_per_window = _get_sample_intervals(times,
952+
sample_interval, samples_per_window = _get_sample_intervals(measured.index,
978953
window_length)
979954

980955
# generate matrix of integers for creating windows with indexing
981956
H = hankel(np.arange(samples_per_window),
982-
np.arange(samples_per_window-1, len(times)))
957+
np.arange(samples_per_window-1, len(measured.index)))
983958

984959
# calculate measurement statistics
985960
meas_mean, meas_max, meas_line_length, meas_slope_nstd, meas_slope \
986-
= _calc_stats(meas, samples_per_window, sample_interval, 'center')
961+
= _calc_stats(measured, samples_per_window, sample_interval, 'center')
987962

988963
# calculate clear sky statistics
989964
clear_mean, clear_max, _, _, clear_slope \
990-
= _calc_stats(clear, samples_per_window, sample_interval, 'center')
965+
= _calc_stats(clearsky, samples_per_window, sample_interval, 'center')
991966

992967
alpha = 1
993968
for iteration in range(max_iterations):
994969
_, _, clear_line_length, _, _ = _calc_stats(
995-
alpha * clear, samples_per_window, sample_interval, 'center')
970+
alpha * clearsky, samples_per_window, sample_interval, 'center')
996971

997972
line_diff = meas_line_length - clear_line_length
998973

@@ -1002,24 +977,26 @@ def detect_clearsky(measured, clearsky, times=None, window_length=10,
1002977
c3 = (line_diff > lower_line_length) & (line_diff < upper_line_length)
1003978
c4 = meas_slope_nstd < var_diff
1004979
# need to reduce window by 1 since slope is differenced already
1005-
c5 = _calc_c5(meas, alpha * clear, samples_per_window,
980+
c5 = _calc_c5(measured, alpha * clearsky, samples_per_window,
1006981
'center', slope_dev)
1007982
c6 = (clear_mean != 0) & ~np.isnan(clear_mean)
1008983
clear_windows = c1 & c2 & c3 & c4 & c5 & c6
1009984

1010985
# create array to return
1011-
clear_samples = np.full_like(meas, False, dtype='bool')
986+
clear_samples = np.full_like(measured, False, dtype='bool')
1012987
# find the samples contained in any window classified as clear
1013988
idx = _clear_sample_index(clear_windows, samples_per_window, 'center',
1014989
H)
1015990
clear_samples[idx] = True
1016991

1017992
# find a new alpha
1018993
previous_alpha = alpha
1019-
clear_meas = meas[clear_samples]
1020-
clear_clear = clear[clear_samples]
994+
clear_meas = measured[clear_samples]
995+
clear_clear = clearsky[clear_samples]
996+
1021997
def rmse(alpha):
1022998
return np.sqrt(np.mean((clear_meas - alpha*clear_clear)**2))
999+
10231000
alpha = minimize_scalar(rmse).x
10241001
if round(alpha*10000) == round(previous_alpha*10000):
10251002
break
@@ -1028,10 +1005,6 @@ def rmse(alpha):
10281005
warnings.warn('failed to converge after %s iterations'
10291006
% max_iterations, RuntimeWarning)
10301007

1031-
# be polite about returning the same type as was input
1032-
if ispandas:
1033-
clear_samples = pd.Series(clear_samples, index=times)
1034-
10351008
if return_components:
10361009
components = OrderedDict()
10371010
components['mean_diff'] = c1

pvlib/tests/test_clearsky.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -579,8 +579,8 @@ def test_detect_clearsky_iterations(detect_clearsky_data):
579579
with pytest.warns(RuntimeWarning):
580580
clear_samples = clearsky.detect_clearsky(
581581
expected['GHI'], cs['ghi']*alpha, max_iterations=1)
582-
assert (clear_samples[:'2012-04-01 10:41:00'] == True).all()
583-
assert (clear_samples['2012-04-01 10:42:00':] == False).all()
582+
assert (clear_samples[:'2012-04-01 10:41:00'] is True).all()
583+
assert (clear_samples['2012-04-01 10:42:00':] is False).all()
584584
clear_samples = clearsky.detect_clearsky(
585585
expected['GHI'], cs['ghi']*alpha, max_iterations=20)
586586
assert_series_equal(expected['Clear or not'], clear_samples,
@@ -606,15 +606,6 @@ def test_detect_clearsky_window(detect_clearsky_data):
606606
check_dtype=False, check_names=False)
607607

608608

609-
def test_detect_clearsky_arrays(detect_clearsky_data):
610-
expected, cs = detect_clearsky_data
611-
clear_samples = clearsky.detect_clearsky(
612-
expected['GHI'].values, cs['ghi'].values, times=cs.index,
613-
window_length=10)
614-
assert isinstance(clear_samples, np.ndarray)
615-
assert (clear_samples == expected['Clear or not'].values).all()
616-
617-
618609
def test_detect_clearsky_irregular_times(detect_clearsky_data):
619610
expected, cs = detect_clearsky_data
620611
times = cs.index.values.copy()

0 commit comments

Comments
 (0)