From 71b6756a6b72928b61b7e22b3127021b0ccf8753 Mon Sep 17 00:00:00 2001 From: echedey-ls <80125792+echedey-ls@users.noreply.github.com> Date: Fri, 12 Apr 2024 17:32:38 +0200 Subject: [PATCH 1/5] Update test_singlediode.py --- pvlib/tests/test_singlediode.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pvlib/tests/test_singlediode.py b/pvlib/tests/test_singlediode.py index 899f158415..52a21a8fd5 100644 --- a/pvlib/tests/test_singlediode.py +++ b/pvlib/tests/test_singlediode.py @@ -10,6 +10,7 @@ bishop88, bishop88_i_from_v, bishop88_v_from_i) from pvlib._deprecation import pvlibDeprecationWarning import pytest +from numpy.testing import assert_approx_equal from .conftest import DATA_DIR POA = 888 @@ -168,16 +169,15 @@ def test_singlediode_precision(method, precise_iv_curves): def test_singlediode_lambert_negative_voc(): - # Those values result in a negative v_oc out of `_lambertw_v_from_i` x = np.array([0., 1.480501e-11, 0.178, 8000., 1.797559]) outs = pvsystem.singlediode(*x, method='lambertw') - assert outs['v_oc'] == 0 + assert_approx_equal(outs['v_oc'], 0) # Testing for an array x = np.array([x, x]).T outs = pvsystem.singlediode(*x, method='lambertw') - assert np.array_equal(outs['v_oc'], [0, 0]) + assert_approx_equal(outs['v_oc'], [0, 0]) @pytest.mark.parametrize('method', ['lambertw']) From 61764fd227788212c346023bb84784b2ccedb84a Mon Sep 17 00:00:00 2001 From: echedey-ls <80125792+echedey-ls@users.noreply.github.com> Date: Fri, 12 Apr 2024 17:39:33 +0200 Subject: [PATCH 2/5] Linter :wrench: --- pvlib/tests/test_singlediode.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pvlib/tests/test_singlediode.py b/pvlib/tests/test_singlediode.py index 52a21a8fd5..b8aedc6714 100644 --- a/pvlib/tests/test_singlediode.py +++ b/pvlib/tests/test_singlediode.py @@ -175,7 +175,7 @@ def test_singlediode_lambert_negative_voc(): assert_approx_equal(outs['v_oc'], 0) # Testing for an array - x = np.array([x, x]).T + x = np.array([x, x]).T outs = pvsystem.singlediode(*x, method='lambertw') assert_approx_equal(outs['v_oc'], [0, 0]) From d2fa3a90ffe29a09904f5f4acf393e47cd881d2b Mon Sep 17 00:00:00 2001 From: echedey-ls <80125792+echedey-ls@users.noreply.github.com> Date: Fri, 12 Apr 2024 17:50:28 +0200 Subject: [PATCH 3/5] Update test_singlediode.py --- pvlib/tests/test_singlediode.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pvlib/tests/test_singlediode.py b/pvlib/tests/test_singlediode.py index b8aedc6714..37908c5ca6 100644 --- a/pvlib/tests/test_singlediode.py +++ b/pvlib/tests/test_singlediode.py @@ -10,7 +10,7 @@ bishop88, bishop88_i_from_v, bishop88_v_from_i) from pvlib._deprecation import pvlibDeprecationWarning import pytest -from numpy.testing import assert_approx_equal +from numpy.testing import assert_allclose from .conftest import DATA_DIR POA = 888 @@ -172,12 +172,12 @@ def test_singlediode_lambert_negative_voc(): # Those values result in a negative v_oc out of `_lambertw_v_from_i` x = np.array([0., 1.480501e-11, 0.178, 8000., 1.797559]) outs = pvsystem.singlediode(*x, method='lambertw') - assert_approx_equal(outs['v_oc'], 0) + assert_allclose(outs['v_oc'], 0) # Testing for an array x = np.array([x, x]).T outs = pvsystem.singlediode(*x, method='lambertw') - assert_approx_equal(outs['v_oc'], [0, 0]) + assert_allclose(outs['v_oc'].values, [0, 0]) @pytest.mark.parametrize('method', ['lambertw']) From cc32413c05d360e0491c2fb01b13055880968fe3 Mon Sep 17 00:00:00 2001 From: echedey-ls <80125792+echedey-ls@users.noreply.github.com> Date: Fri, 12 Apr 2024 21:00:58 +0200 Subject: [PATCH 4/5] Change approach completely. Now we are mocking. :construction_worker: --- pvlib/tests/test_singlediode.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/pvlib/tests/test_singlediode.py b/pvlib/tests/test_singlediode.py index 37908c5ca6..f66c1f8381 100644 --- a/pvlib/tests/test_singlediode.py +++ b/pvlib/tests/test_singlediode.py @@ -10,7 +10,7 @@ bishop88, bishop88_i_from_v, bishop88_v_from_i) from pvlib._deprecation import pvlibDeprecationWarning import pytest -from numpy.testing import assert_allclose +from numpy.testing import assert_array_equal from .conftest import DATA_DIR POA = 888 @@ -168,16 +168,24 @@ def test_singlediode_precision(method, precise_iv_curves): assert np.allclose(pc['i_xx'], outs['i_xx'], atol=1e-6, rtol=0) -def test_singlediode_lambert_negative_voc(): - # Those values result in a negative v_oc out of `_lambertw_v_from_i` - x = np.array([0., 1.480501e-11, 0.178, 8000., 1.797559]) - outs = pvsystem.singlediode(*x, method='lambertw') - assert_allclose(outs['v_oc'], 0) +def test_singlediode_lambert_negative_voc(mocker): + """Tests approximation to zero of v_oc when it is negative and small. + See singlediode.py:_lambertw > comment 'Set small elements <0 in v_oc to 0' + """ + # Next values should result in a negative v_oc out of `_lambertw_v_from_i` + # however, we can't ensure that the output belongs to (-1e-12, 0), so we + # mock it. It depends on the platform and Python distro. See issue #2000. + patcher = mocker.patch("pvlib.singlediode._lambertw_v_from_i") + x = np.array([0.0, 1.480501e-11, 0.178, 8000.0, 1.797559]) + patcher.return_value=-9.999e-13 + outs = pvsystem.singlediode(*x, method="lambertw") + assert outs["v_oc"] == 0 # Testing for an array + patcher.return_value=np.array([-9.999e-13, -1.001e-13]) x = np.array([x, x]).T - outs = pvsystem.singlediode(*x, method='lambertw') - assert_allclose(outs['v_oc'].values, [0, 0]) + outs = pvsystem.singlediode(*x, method="lambertw") + assert_array_equal(outs['v_oc'], [0, 0]) @pytest.mark.parametrize('method', ['lambertw']) From af7fbf9c5ae673a9beaf75135837a8d76f40076f Mon Sep 17 00:00:00 2001 From: echedey-ls <80125792+echedey-ls@users.noreply.github.com> Date: Fri, 12 Apr 2024 21:01:37 +0200 Subject: [PATCH 5/5] Thanks ruff for the formatting :green_heart: --- pvlib/tests/test_singlediode.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pvlib/tests/test_singlediode.py b/pvlib/tests/test_singlediode.py index f66c1f8381..54442cde7e 100644 --- a/pvlib/tests/test_singlediode.py +++ b/pvlib/tests/test_singlediode.py @@ -177,15 +177,15 @@ def test_singlediode_lambert_negative_voc(mocker): # mock it. It depends on the platform and Python distro. See issue #2000. patcher = mocker.patch("pvlib.singlediode._lambertw_v_from_i") x = np.array([0.0, 1.480501e-11, 0.178, 8000.0, 1.797559]) - patcher.return_value=-9.999e-13 + patcher.return_value = -9.999e-13 outs = pvsystem.singlediode(*x, method="lambertw") assert outs["v_oc"] == 0 # Testing for an array - patcher.return_value=np.array([-9.999e-13, -1.001e-13]) + patcher.return_value = np.array([-9.999e-13, -1.001e-13]) x = np.array([x, x]).T outs = pvsystem.singlediode(*x, method="lambertw") - assert_array_equal(outs['v_oc'], [0, 0]) + assert_array_equal(outs["v_oc"], [0, 0]) @pytest.mark.parametrize('method', ['lambertw'])