Skip to content

Commit 10cd8f2

Browse files
alexandermorgancwhanse
authored andcommitted
refactor bifacial merge, improve merge tests (pvlib#747)
* Simplify merge method. Simplify merge method by using list and dictionary comprehensions instead of nested for loops. This also avoids the need to check if some of the elements of the reports arguments are None, and the need to check if reports contains more than one report. This should also be a small performance improvement. This change also adds a bit more detail to the docstring for the merge method. The creation of a new empty report is also modified to no longer use an intermediary list of keys. * Update what's new doc. * Update whatsnew file syntax. * Rename whatsnew file to change format to rst. * Fix linting errors. * Remove pandas and numpy version reqs * Update v0.7.1.rst * Revert "Update v0.7.1.rst" This reverts commit d131657. * Revert "Remove pandas and numpy version reqs" This reverts commit 84d7189. * Reword elements in merge() comprehension. * Fix handling of None values in merge. If one of the values in the reports argument that merge() takes is a None, this is now properly handled by being dropped. * Remove unnecessary checks in merge(). Returns merge method to its original state with two nested for loops, but without the checks for the number of dictionaries in the reports argument, or the check that each element in reports is a dictionary. Reports are only created in the build method so a test was made to ensure that build always returns a dictionary instead. * Add tests for merge() and build(). * Fix lint errors. * Correct import mistake. Previous commits I made did not import the PVFactorsReportBuilder class correctly. * Reinsert type check in merge loop. * Revert "Reinsert type check in merge loop." This reverts commit eeb8053. * Fix test_build_1 test. * Update what's new file. Add bullet point about added tests for bifacial.py methods. * Fix merge conflicts in what's new file. * Revert "Fix merge conflicts in what's new file." This reverts commit 74c8eba. * Revert "Update what's new file." This reverts commit 4447e6c. * Revert "Rename whatsnew file to change format to rst." This reverts commit 93d0559. * Revert "Update whatsnew file syntax." This reverts commit c969994. * Revert "Update what's new doc." This reverts commit 55d8f08. * Merge what's new updates from upstream. * Update what's new file.
1 parent e3635c2 commit 10cd8f2

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

docs/sphinx/source/whatsnew/v0.7.0.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This is a major release that drops support for Python 2 and Python 3.4. We
77
recommend all users of v0.6.3 upgrade to this release after checking API
88
compatibility notes.
99

10-
**Python 2.7 support ended on June 1, 2019**. (:issue:`501`)
10+
**Python 2.7 support ended on June 1, 2019.** (:issue:`501`)
1111
**Minimum numpy version is now 1.10.4. Minimum pandas version is now 0.18.1.**
1212

1313
Bug fixes
@@ -18,10 +18,12 @@ Bug fixes
1818
Testing
1919
~~~~~~~
2020
* Added 30 minutes to timestamps in `test_psm3.csv` to match change in NSRDB (:issue:`733`)
21+
* Added tests for methods in bifacial.py.
2122

2223

2324
Contributors
2425
~~~~~~~~~~~~
2526
* Mark Campanellli (:ghuser:`markcampanelli`)
2627
* Will Holmgren (:ghuser:`wholmgren`)
2728
* Oscar Dowson (:ghuser:`odow`)
29+
* Alexander Morgan (:ghuser:`alexandermorgan`)

pvlib/bifacial.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ def build(report, pvarray):
159159
back surface of center pvrow (index=1)"""
160160
# Initialize the report as a dictionary
161161
if report is None:
162-
list_keys = ['total_inc_back', 'total_inc_front']
163-
report = {key: [] for key in list_keys}
162+
report = {'total_inc_back': [], 'total_inc_front': []}
164163
# Add elements to the report
165164
if pvarray is not None:
166165
pvrow = pvarray.pvrows[1] # use center pvrow
@@ -177,13 +176,12 @@ def build(report, pvarray):
177176

178177
@staticmethod
179178
def merge(reports):
180-
"""Works for dictionary reports"""
179+
"""Works for dictionary reports. Merges the reports list of
180+
dictionaries in a single dictionary. The list of the first
181+
dictionary are extended by those of all subsequent lists."""
181182
report = reports[0]
182-
# Merge only if more than 1 report
183-
if len(reports) > 1:
184-
keys_report = list(reports[0].keys())
185-
for other_report in reports[1:]:
186-
if other_report is not None:
187-
for key in keys_report:
188-
report[key] += other_report[key]
183+
keys_report = list(report.keys())
184+
for other_report in reports[1:]: # loop won't run if len(reports) < 2
185+
for key in keys_report:
186+
report[key] += other_report[key]
189187
return report

pvlib/test/test_bifacial.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pandas as pd
2+
import numpy as np
23
from datetime import datetime
3-
from pvlib.bifacial import pvfactors_timeseries
4+
from pvlib.bifacial import pvfactors_timeseries, PVFactorsReportBuilder
45
from conftest import requires_pvfactors
56
import pytest
67

@@ -107,3 +108,43 @@ def test_pvfactors_timeseries_pandas_inputs(run_parallel_calculations):
107108

108109
pd.testing.assert_series_equal(ipoa_front, expected_ipoa_front)
109110
pd.testing.assert_series_equal(ipoa_back, expected_ipoa_back)
111+
112+
113+
def test_build_1():
114+
"""Test that build correctly instantiates a dictionary, when passed a Nones
115+
for the report and pvarray arguments.
116+
"""
117+
report = None
118+
pvarray = None
119+
expected = {'total_inc_back': [np.nan], 'total_inc_front': [np.nan]}
120+
assert expected == PVFactorsReportBuilder.build(report, pvarray)
121+
122+
123+
def test_merge_1():
124+
"""Test that merge correctly returns the first element of the reports
125+
argument when there is only dictionary in reports.
126+
"""
127+
test_dict = {'total_inc_back': [1, 2, 3], 'total_inc_front': [4, 5, 6]}
128+
reports = [test_dict]
129+
assert test_dict == PVFactorsReportBuilder.merge(reports)
130+
131+
132+
def test_merge_2():
133+
"""Test that merge correctly combines two dictionary reports.
134+
"""
135+
test_dict_1 = {'total_inc_back': [1, 2], 'total_inc_front': [4, 5]}
136+
test_dict_2 = {'total_inc_back': [3], 'total_inc_front': [6]}
137+
expected = {'total_inc_back': [1, 2, 3], 'total_inc_front': [4, 5, 6]}
138+
reports = [test_dict_1, test_dict_2]
139+
assert expected == PVFactorsReportBuilder.merge(reports)
140+
141+
142+
def test_merge_3():
143+
"""Test that merge correctly combines three dictionary reports.
144+
"""
145+
test_dict_1 = {'total_inc_back': [1], 'total_inc_front': [4]}
146+
test_dict_2 = {'total_inc_back': [2], 'total_inc_front': [5]}
147+
test_dict_3 = {'total_inc_back': [3], 'total_inc_front': [6]}
148+
expected = {'total_inc_back': [1, 2, 3], 'total_inc_front': [4, 5, 6]}
149+
reports = [test_dict_1, test_dict_2, test_dict_3]
150+
assert expected == PVFactorsReportBuilder.merge(reports)

0 commit comments

Comments
 (0)