Skip to content

Commit f976f61

Browse files
committed
fix pytest.raise context, add test_v_from_i
* add all i_from_v and v_from_i args to checklist * remove enumerate, not used * add some comments to explain why we do it * combine assignment of size, shape initial values in one line * use *args in calls to i_from_v, etc. since we have it * don't need to check if size<=1 before updating size or shape, since we let np.vectorize handle broadcasting and raising ValueError
1 parent 446fa9e commit f976f61

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

pvlib/pvsystem.py

+22-26
Original file line numberDiff line numberDiff line change
@@ -2100,35 +2100,35 @@ def v_from_i(resistance_shunt, resistance_series, nNsVth, current,
21002100
# wrap it so it returns nan
21012101
v_from_i_fun = singlediode_methods.returns_nan()(v_from_i_fun)
21022102
# find the right size and shape for returns
2103-
args = (current, photocurrent, resistance_shunt)
2104-
size = 0
2105-
shape = None
2106-
for n, arg in enumerate(args):
2103+
args = (current, photocurrent, saturation_current,
2104+
resistance_series, resistance_shunt, nNsVth)
2105+
size, shape = 0, None # 0 or None both mean scalar
2106+
for arg in args:
21072107
try:
2108-
this_shape = arg.shape
2108+
this_shape = arg.shape # try to get shape
21092109
except AttributeError:
21102110
this_shape = None
21112111
try:
2112-
this_size = len(arg)
2112+
this_size = len(arg) # try to get the size
21132113
except TypeError:
21142114
this_size = 0
21152115
else:
2116-
this_size = sum(this_shape)
2116+
this_size = sum(this_shape) # calc size from shape
21172117
if shape is None:
2118-
shape = this_shape
2119-
if this_size > size and size <= 1:
2118+
shape = this_shape # set the shape if None
2119+
# update size and shape
2120+
if this_size > size:
21202121
size = this_size
21212122
if this_shape is not None:
21222123
shape = this_shape
21232124
if size <= 1:
2124-
V = v_from_i_fun(current, photocurrent, saturation_current,
2125-
resistance_series, resistance_shunt, nNsVth)
2125+
V = v_from_i_fun(*args)
21262126
if shape is not None:
21272127
V = np.tile(V, shape)
21282128
else:
2129+
# np.vectorize handles broadcasting, raises ValueError
21292130
vecfun = np.vectorize(v_from_i_fun)
2130-
V = vecfun(current, photocurrent, saturation_current,
2131-
resistance_series, resistance_shunt, nNsVth)
2131+
V = vecfun(*args)
21322132
if np.isnan(V).any() and size <= 1:
21332133
V = np.repeat(V, size)
21342134
if shape is not None:
@@ -2270,35 +2270,31 @@ def i_from_v(resistance_shunt, resistance_series, nNsVth, voltage,
22702270
# find the right size and shape for returns
22712271
args = (voltage, photocurrent, saturation_current, resistance_series,
22722272
resistance_shunt, nNsVth)
2273-
size = 0
2274-
shape = None
2275-
for n, arg in enumerate(args):
2273+
size, shape = 0, None # 0 or None both mean scalar
2274+
for arg in args:
22762275
try:
2277-
this_shape = arg.shape
2276+
this_shape = arg.shape # try to get shape
22782277
except AttributeError:
22792278
this_shape = None
22802279
try:
2281-
this_size = len(arg)
2280+
this_size = len(arg) # try to get the size
22822281
except TypeError:
22832282
this_size = 0
22842283
else:
2285-
this_size = sum(this_shape)
2284+
this_size = sum(this_shape) # calc size from shape
22862285
if shape is None:
2287-
shape = this_shape
2288-
if this_size > size and size <= 1:
2286+
shape = this_shape # set the shape if None
2287+
# update size and shape
2288+
if this_size > size:
22892289
size = this_size
22902290
if this_shape is not None:
22912291
shape = this_shape
2292-
else:
2293-
msg = ('Argument: "%s" is different size from other arguments'
2294-
' (%d > %d). All arguments must be the same size or'
2295-
' scalar.') % (arg, this_size, size)
2296-
raise ValueError(msg)
22972292
if size <= 1:
22982293
I = i_from_v_fun(*args)
22992294
if shape is not None:
23002295
I = np.tile(I, shape)
23012296
else:
2297+
# np.vectorize handles broadcasting, raises ValueError
23022298
vecfun = np.vectorize(i_from_v_fun)
23032299
I = vecfun(*args)
23042300
if np.isnan(I).any() and size <= 1:

pvlib/test/test_pvsystem.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -626,9 +626,15 @@ def test_PVSystem_i_from_v():
626626

627627

628628
@requires_scipy
629-
@pytest.raises(ValueError)
630629
def test_i_from_v_size():
631-
pvsystem.i_from_v(20, [0.1] * 2, 0.5, [7.5] * 3, 6.0e-7, 7.0)
630+
with pytest.raises(ValueError):
631+
pvsystem.i_from_v(20, [0.1] * 2, 0.5, [7.5] * 3, 6.0e-7, 7.0)
632+
633+
634+
@requires_scipy
635+
def test_v_from_i_size():
636+
with pytest.raises(ValueError):
637+
pvsystem.v_from_i(20, [0.1] * 2, 0.5, [3.0] * 3, 6.0e-7, 7.0)
632638

633639

634640
@requires_scipy

0 commit comments

Comments
 (0)