Skip to content

Commit

Permalink
Code cleanup for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
craigmaloney committed Sep 15, 2020
1 parent a40d36f commit 6d6ad4f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 28 deletions.
22 changes: 21 additions & 1 deletion tests/test_core_single_stage.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import pytest
import warnings
import numpy as np
from numpy.testing import assert_allclose
from numpy import isnan
import pandas as pd

from datetime import datetime

from thermostat.core import __pandas_warnings, percent_savings
from thermostat.importers import from_csv
from thermostat.util.testing import get_data_path

Expand All @@ -30,12 +32,30 @@


def test_rhu_formatting(thermostat_type_1):

assert('rhu1_less05F' == thermostat_type_1._format_rhu('rhu1', -np.inf, 5, None))
assert('rhu1_greater55F' == thermostat_type_1._format_rhu('rhu1', 55, np.inf, None))
assert('rhu1_30F_to_45F' == thermostat_type_1._format_rhu('rhu1', 30, 45, None))
assert('rhu2_05F_to_10F_aux_duty_cycle' == thermostat_type_1._format_rhu('rhu2', 5, 10, 'aux_duty_cycle'))

def test_pandas_warnings(thermostat_type_1):
with pytest.warns(Warning):
__pandas_warnings('0.21.0')

with pytest.warns(Warning):
__pandas_warnings('1.2.0')

with pytest.warns(None) as pytest_warnings:
__pandas_warnings('0.25.3')
assert not pytest_warnings

assert __pandas_warnings(None) is None


def test_perecent_saings(thermostat_type_1):
avoided = pd.Series([4, 5,6])
baseline = pd.Series([0, 0, 0])
result = percent_savings(avoided, baseline, 'thermostat_test')
assert result == np.inf


def test_zero_days_warning(thermostat_zero_days):
Expand Down
56 changes: 29 additions & 27 deletions thermostat/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,6 @@

warnings.simplefilter('module', Warning)

try:
pd_version = pd.__version__.split('.')
pd_major = int(pd_version.pop(0))
pd_minor = int(pd_version.pop(0))
if pd_major == 0 and pd_minor == 21:
warnings.warn(
"WARNING: Pandas version 0.21.x has known issues and is not supported. "
"Please either downgrade to Pandas 0.20.3 or upgrade to the latest Pandas version.")
# Pandas 1.x causes issues. Need to warn about this at the moment.
if pd_major >= 1:
warnings.warn(
"WARNING: Pandas version 1.x has changed significantly, and causes "
"issues with this software. We are working on supporting Pandas 1.x in "
"a future release.")

except TypeError:
pass # Documentation mocks out pd, so ignore if not present.

# Ignore divide-by-zero errors
np.seterr(divide='ignore', invalid='ignore')

Expand All @@ -66,20 +48,40 @@
pd.set_option('mode.chained_assignment', None)


def __pandas_warnings(pandas_version):
''' Helper to warn about versions of Pandas that aren't supported yet or have issues '''
try:
pd_version = pandas_version.split('.')
pd_major = int(pd_version.pop(0))
pd_minor = int(pd_version.pop(0))
if pd_major == 0 and pd_minor == 21:
warnings.warn(
"WARNING: Pandas version 0.21.x has known issues and is not supported. "
"Please either downgrade to Pandas 0.20.3 or upgrade to the latest Pandas version.")
# Pandas 1.x causes issues. Need to warn about this at the moment.
if pd_major >= 1:
warnings.warn(
"WARNING: Pandas version 1.x has changed significantly, and causes "
"issues with this software. We are working on supporting Pandas 1.x in "
"a future release.")

except Exception:
# If we can't figure out the version string then don't worry about it for now
return None


try:
__pandas_warnings(pd.__version__)
except TypeError:
pass # Documentation mocks out pd, so ignore if not present.


def avoided(baseline, observed):
return baseline - observed


def percent_savings(avoided, baseline, thermostat_id):
try:
savings = (avoided.mean() / baseline.mean()) * 100.0
except ZeroDivisionError:
logger.debug(
'percent_savings divided by zero: %s / %s '
'for thermostat_id %s ' % (
avoided.mean(), baseline.mean(),
thermostat_id))
savings = np.nan
savings = np.divide(avoided.mean(), baseline.mean()) * 100.0
return savings


Expand Down

0 comments on commit 6d6ad4f

Please sign in to comment.