8000 more asv tests for solar position, fix fuentes asv bug by wholmgren · Pull Request #1059 · pvlib/pvlib-python · GitHub
[go: up one dir, main page]

Skip to content

more asv tests for solar position, fix fuentes asv bug #1059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions benchmarks/asv.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
// Note: these don't have a minimum in setup.py
"pytables": "3.6.1",
"ephem": "3.7.6.0",
"numba": "0.36.1",
},
// latest versions available
{
Expand All @@ -130,6 +131,7 @@
"scipy": "",
"pytables": "",
"ephem": "",
"numba": ""
},
],
"exclude": [
Expand Down
61 changes: 51 additions & 10 deletions benchmarks/benchmarks/solarposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
ASV benchmarks for solarposition.py
"""

import datetime
import pandas as pd
import pvlib
from pvlib import solarposition
Expand All @@ -16,28 +17,68 @@


class SolarPosition:
params = [1, 10, 100] # number of days
param_names = ['ndays']

def setup(self):
def setup(self, ndays):
self.times = pd.date_range(start='20180601', freq='1min',
periods=14400)
periods=1440*ndays)
self.times_localized = self.times.tz_localize('Etc/GMT+7')
self.lat = 35.1
self.lon = -106.6
self.times_daily = pd.date_range(
start='20180601', freq='24h', periods=ndays, tz='Etc/GMT+7')

# GH 512
def time_ephemeris(self):
def time_ephemeris(self, ndays):
solarposition.ephemeris(self.times, self.lat, self.lon)

# GH 512
def time_ephemeris_localized(self):
def time_ephemeris_localized(self, ndays):
solarposition.ephemeris(self.times_localized, self.lat, self.lon)

def time_spa_python(self):
solarposition.spa_python(self.times_localized[::5], self.lat, self.lon)
def time_spa_python(self, ndays):
solarposition.spa_python(self.times_localized, self.lat, self.lon)

def time_pyephem(self, ndays):
solarposition.pyephem(self.times_localized, self.lat, self.lon)

def time_sun_rise_set_transit_spa(self, ndays):
sun_rise_set_transit_spa(self.times_daily, self.lat, self.lon)

def time_sun_rise_set_transit_spa(self):
sun_rise_set_transit_spa(self.times_localized[::30],
self.lat, self.lon)
def time_sun_rise_set_transit_ephem(self, ndays):
solarposition.sun_rise_set_transit_ephem(
self.times_daily, self.lat, self.lon)

def time_nrel_earthsun_distance(self):
def time_sun_rise_set_transit_geometric_full_comparison(self, ndays):
dayofyear = self.times_daily.dayofyear
declination = solarposition.declination_spencer71(dayofyear)
equation_of_time = solarposition.equation_of_time_spencer71(dayofyear)
solarposition.sun_rise_set_transit_geometric(
self.times_daily, self.lat, self.lon, declination,
equation_of_time)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what to think about running more than one function in a single benchmark. It would be a little awkward/repetitive to run benchmark each step of a sequence individually, but it seems like the rule of thumb "only test one thing at a time" for unit tests ought to apply to benchmarks as well.

That said, any benchmark is better than no benchmark!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also consider calculating dayofyear in the setup function because it's external

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I agree. In this case I wanted a more direct comparison to the other sun_rise_set_transit functions, so I put all that in the test. The dayofyear calculation is part of a fair comparison. But I also see the argument to test each of them individually and add them up. For now, I renamed the existing function to make it clear it's for comparison.


def time_nrel_earthsun_distance(self, ndays):
solarposition.nrel_earthsun_distance(self.times_localized)


class SolarPositionCalcTime:

def setup(self):
# test calc_time for finding times at which sun is 3 degrees
# above the horizon.
# Tucson 2020-09-14 sunrise at 6:08 AM MST, 13:08 UTC
# according to google.
self.start = datetime.datetime(2020, 9, 14, 12)
self.end = datetime.datetime(2020, 9, 14, 15)
self.value = 0.05235987755982988
self.lat = 32.2
self.lon = -110.9
self.attribute = 'alt'

def time_calc_time(self):
# datetime.datetime(2020, 9, 14, 13, 24, 13, 861913, tzinfo=<UTC>)
solarposition.calc_time(
self.start, self.end, self.lat, self.lon, self.attribute,
self.value
)
46 changes: 46 additions & 0 deletions benchmarks/benchmarks/solarposition_numba.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""ASV benchmarks for solarposition.py using numba.

We use a separate module so that we can control the pvlib import process
using an environment variable. This will force pvlib to compile the numba
code during setup.

Try to keep relevant sections in sync with benchmarks/solarposition.py
"""

from pkg_resources import parse_version
import pandas as pd

import os
os.environ['PVLIB_USE_NUMBA'] = '1'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the environment var might not be needed now that you added numba to the env specs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With setting the environment variable I see this:

(pvlib38ci) 8:22:16 holmgren@holmgren-mbp.local benchmarks solposasv ? asv run --bench SolarPosition --show-stderr
· Creating environments
· Discovering benchmarks
· Running 22 total benchmarks (1 commits * 2 environments * 11 benchmarks)
[  0.00%] · For pvlib-python commit 30d784e7 <master>:
[  0.00%] ·· Building for conda-py3.6-ephem3.7.6.0-numba-numpy1.12.0-pandas0.22.0-pytables3.6.1-scipy1.2.0
[  0.00%] ·· Benchmarking conda-py3.6-ephem3.7.6.0-numba-numpy1.12.0-pandas0.22.0-pytables3.6.1-scipy1.2.0
[  2.27%] ··· Running (solarposition.SolarPosition.time_calc_time--)...........
[ 27.27%] ··· solarposition.SolarPosition.time_calc_time                                                       363±3μs
[ 29.55%] ··· solarposition.SolarPosition.time_ephemeris                                                    22.6±0.5ms
[ 31.82%] ··· solarposition.SolarPosition.time_ephemeris_localized                                          22.4±0.2ms
[ 34.09%] ··· solarposition.SolarPosition.time_nrel_earthsun_distance                                         26.2±1ms
[ 36.36%] ··· solarposition.SolarPosition.time_pyephem                                                         416±2ms
[ 38.64%] ··· solarposition.SolarPosition.time_spa_python                                                      134±4ms
[ 40.91%] ··· solarposition.SolarPosition.time_sun_rise_set_transit_ephem                                   53.5±0.6ms
[ 43.18%] ··· solarposition.SolarPosition.time_sun_rise_set_transit_geometric                              4.38±0.03ms
[ 45.45%] ··· solarposition.SolarPosition.time_sun_rise_set_transit_spa                                     22.1±0.3ms
[ 47.73%] ··· solarposition_numba.SolarPositionNumba.time_spa_python                                          28.1±1ms
[ 50.00%] ··· solarposition_numba.SolarPositionNumba.time_sun_rise_set_transit_spa                          6.90±0.1ms

Without setting the environment variable (comment out the line), I see this:

(pvlib38ci) 8:20:05 holmgren@holmgren-mbp.local benchmarks solposasv ? asv run --bench SolarPosition --show-stderr
· Creating environments
· Discovering benchmarks
· Running 22 total benchmarks (1 commits * 2 environments * 11 benchmarks)
[  0.00%] · For pvlib-python commit 30d784e7 <master>:
[  0.00%] ·· Building for conda-py3.6-ephem3.7.6.0-numba-numpy1.12.0-pandas0.22.0-pytables3.6.1-scipy1.2.0
[  0.00%] ·· Benchmarking conda-py3.6-ephem3.7.6.0-numba-numpy1.12.0-pandas0.22.0-pytables3.6.1-scipy1.2.0
[  2.27%] ··· Running (solarposition.SolarPosition.time_calc_time--)...........
[ 27.27%] ··· solarposition.SolarPosition.time_calc_time                                                      378±20μs
[ 29.55%] ··· solarposition.SolarPosition.time_ephemeris                                                      23.8±1ms
[ 31.82%] ··· solarposition.SolarPosition.time_ephemeris_localized                                          22.7±0.5ms
[ 34.09%] ··· solarposition.SolarPosition.time_nrel_earthsun_distance                                         38.2±7ms
[ 36.36%] ··· solarposition.SolarPosition.time_pyephem                                                        447±10ms
[ 38.64%] ··· solarposition.SolarPosition.time_spa_python                                                      138±5ms
[ 40.91%] ··· solarposition.SolarPosition.time_sun_rise_set_transit_ephem                                   53.1±0.5ms
[ 43.18%] ··· solarposition.SolarPosition.time_sun_rise_set_transit_geometric                               4.56±0.2ms
[ 45.45%] ··· solarposition.SolarPosition.time_sun_rise_set_transit_spa                                     22.3±0.7ms
[ 47.73%] ··· solarposition_numba.SolarPositionNumba.time_spa_python                                          29.3±2ms
[ 47.73%] ···· /Users/holmgren/git_repos/pvlib-python/benchmarks/env/4b3141485b42c1a3d28c30cd810d4559/lib/python3.6/site-packages/pvlib/solarposition.py:266: UserWarning: Reloading spa to use numba
               warnings.warn('Reloading spa to use numba')

[ 50.00%] ··· solarposition_numba.SolarPositionNumba.time_sun_rise_set_transit_spa                          6.81±0.2ms
[ 50.00%] ···· /Users/holmgren/git_repos/pvlib-python/benchmarks/env/4b3141485b42c1a3d28c30cd810d4559/lib/python3.6/site-packages/pvlib/solarposition.py:266: UserWarning: Reloading spa to use numba
               warnings.warn('Reloading spa to use numba')



import pvlib # NOQA: E402
from pvlib import solarposition # NOQA: E402


if parse_version(pvlib.__version__) >= parse_version('0.6.1'):
sun_rise_set_transit_spa = solarposition.sun_rise_set_transit_spa
else:
sun_rise_set_transit_spa = solarposition.get_sun_rise_set_transit


class SolarPositionNumba:
params = [1, 10, 100] # number of days
param_names = ['ndays']

def setup(self, ndays):
self.times = pd.date_range(start='20180601', freq='1min',
periods=1440*ndays)
self.times_localized = self.times.tz_localize('Etc/GMT+7')
self.lat = 35.1
self.lon = -106.6
self.times_daily = pd.date_range(
start='20180601', freq='24h', periods=ndays, tz='Etc/GMT+7')

def time_spa_python(self, ndays):
solarposition.spa_python(
self.times_localized, self.lat, self.lon, how='numba')

def time_sun_rise_set_transit_spa(self, ndays):
sun_rise_set_transit_spa(
self.times_daily, self.lat, self.lon, how='numba')
4 changes: 2 additions & 2 deletions benchmarks/benchmarks/temperature.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def set_weather_data(obj):
periods=14400)
obj.poa = pd.Series(1000, index=obj.times)
obj.tamb = pd.Series(20, index=obj.times)
obj.windspeed = pd.Series(2, index=obj.times)
obj.wind_speed = pd.Series(2, index=obj.times)


class SAPM:
Expand All @@ -35,7 +35,7 @@ def sapm_cell_wrapper(poa_global, temp_air, wind_speed):

def time_sapm_cell(self):
# use version-appropriate wrapper
self.sapm_cell_wrapper(self.poa, self.tamb, self.windspeed)
self.sapm_cell_wrapper(self.poa, self.tamb, self.wind_speed)


class Fuentes:
Expand Down
2 changes: 1 addition & 1 deletion docs/sphinx/source/whatsnew/v0.8.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Bug fixes
Testing
~~~~~~~
* Add airspeed velocity performance testing configuration and a few benchmarks.
(:issue:`419`, :pull:`1049`)
(:issue:`419`, :pull:`1049`, :pull:`1059`)

Documentation
~~~~~~~~~~~~~
Expand Down
133 changes: 93 additions & 40 deletions docs/tutorials/solarposition.ipynb

Large diffs are not rendered by default.

0