Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_recent_reading_meter():
recent_record = {"start": datetime.now(pytz.utc) - timedelta(days=390),
"end": datetime.now(pytz.utc) - timedelta(days=360), "value": 0}
old_record = {"start": datetime(2012,1,1,tzinfo=pytz.utc),
"end": datetime(2012,2,1,tzinfo=pytz.utc), "value": 0}
no_cd = ConsumptionData([],
"electricity", "kWh", record_type="arbitrary")
old_cd = ConsumptionData([old_record],
"electricity", "kWh", record_type="arbitrary")
recent_cd = ConsumptionData([recent_record],
"electricity", "kWh", record_type="arbitrary")
mixed_cd = ConsumptionData([recent_record,old_record],
"electricity", "kWh", record_type="arbitrary")
meter = RecentReadingMeter()
assert meter.evaluate_raw(consumption_data=no_cd)["n_days"] == np.inf
assert meter.evaluate_raw(consumption_data=old_cd)["n_days"] == 31
assert meter.evaluate_raw(consumption_data=recent_cd)["n_days"] == 30
assert meter.evaluate_raw(consumption_data=mixed_cd)["n_days"] == 30
def test_consumption_data_interval_start_15min(
records_interval_start_15min, fuel_type, unit_name):
cd = ConsumptionData(records_interval_start_15min,
fuel_type, unit_name, freq="15T")
assert cd.freq_timedelta == timedelta(seconds=60*15)
assert_allclose(cd.data.values,[1,2,3,4,5,6], rtol=RTOL, atol=ATOL)
assert cd.data.index[0] == datetime(2015,1,1)
assert cd.pulse_value is None
def test_consumption_data_interval_start_daily_all_freq_D(
records_interval_start_daily_all, fuel_type, unit_name):
cd = ConsumptionData(records_interval_start_daily_all,
fuel_type, unit_name, freq="D")
assert cd.freq_timedelta == timedelta(days=1)
assert_allclose(cd.data.values,[1,2,3,4,5], rtol=RTOL, atol=ATOL)
assert cd.data.index[0] == datetime(2015,1,1, tzinfo=pytz.UTC)
assert_allclose(cd.estimated,[True,False,False,False,False], rtol=RTOL, atol=ATOL)
def test_consumption_data_interval_end_daily_all_freq_2D(
records_interval_end_daily_all, fuel_type, unit_name):
cd = ConsumptionData(records_interval_end_daily_all,
fuel_type, unit_name, freq="2D")
assert cd.freq_timedelta == timedelta(days=2)
assert_allclose(cd.data.values,[1,3,5], rtol=RTOL, atol=ATOL)
assert cd.data.index[0] == datetime(2014,12,30)
def consumption_data_blank():
return ConsumptionData([], "electricity", "kWh", "arbitrary_start")
def consumption_data_kWh_arbitrary():
records = [{"start": datetime(2015,1,i+1, tzinfo=pytz.UTC), "end": datetime(2015,1,i+2, tzinfo=pytz.UTC),
"value": 1} for i in range(10)]
return ConsumptionData(records, "electricity", "kWh", record_type="arbitrary")
def test_consumption_data_arbitrary_end(records_arbitrary_end,
fuel_type, unit_name, record_type_arbitrary_end):
cd = ConsumptionData(records_arbitrary_end,
fuel_type, unit_name, record_type=record_type_arbitrary_end)
assert_allclose(cd.data.values,[0,0,0,1,0,np.nan],
rtol=RTOL, atol=ATOL)
assert cd.data.index[0] == datetime(2014,2,2)
assert cd.data.index[5] == datetime(2015,2,1)
assert cd.freq is None
assert cd.freq_timedelta is None
assert cd.pulse_value is None
generated_records = cd.records(record_type=record_type_arbitrary_end)
assert len(generated_records) == 6
assert generated_records[0] == {"end": datetime(2014,2,2),"value": np.nan, "estimated": False}
assert generated_records[5] == {"end": datetime(2015,2,1),"value":0, "estimated": False}
def test_unit_conversion():
records = [{
"start": datetime(2015, 1, i+1),
"end": datetime(2015, 1, i+2),
"value": 1
} for i in range(3)]
cd_Wh = ConsumptionData(records, "electricity", "Wh", record_type="arbitrary")
cd_kWh = ConsumptionData(records, "electricity", "kWh", record_type="arbitrary")
cd_kwh = ConsumptionData(records, "electricity", "kwh", record_type="arbitrary")
cd_therm = ConsumptionData(records, "electricity", "therm", record_type="arbitrary")
cd_therms = ConsumptionData(records, "electricity", "therms", record_type="arbitrary")
assert_allclose(cd_Wh.data.values, [0.001, 0.001, 0.001, np.nan])
assert_allclose(cd_kWh.data.values, [1, 1, 1, np.nan])
assert_allclose(cd_kwh.data.values, [1, 1, 1, np.nan])
assert_allclose(cd_therm.data.values, [1, 1, 1, np.nan])
assert_allclose(cd_therms.data.values, [1, 1, 1, np.nan])
assert cd_Wh.unit_name == "kWh"
assert cd_kWh.unit_name == "kWh"
assert cd_kwh.unit_name == "kWh"
assert cd_therm.unit_name == "therm"
assert cd_therms.unit_name == "therm"
baseline_data = baseline_consumption_data.data
reporting_data = reporting_consumption_data.data
periods = baseline_consumption_data.periods()
records = []
for bl_data, rp_data, period in zip(baseline_data, reporting_data,
periods):
if period in reporting_period:
val = rp_data
else:
val = bl_data
record = {"start": period.start, "end": period.end, "value": val}
records.append(record)
cd = ConsumptionData(records, fuel_type, consumption_unit_name,
record_type="arbitrary")
return cd, estimated_annualized_savings, baseline_params, \
reporting_params
"""
rng = pd.date_range('2011-01-01', periods=2, freq=self.freq)
max_period = rng[1] - rng[0]
index_series = pd.Series(consumption_data.data.index.tz_convert(pytz.UTC))
n = 5
if index_series.shape[0] > n:
timedeltas = (index_series - index_series.shift()).iloc[1:(n + 1)]
for timedelta in timedeltas:
if timedelta > max_period:
return { "consumption_resampled": consumption_data }
consumption_resampled = ConsumptionData([],
consumption_data.fuel_type, consumption_data.unit_name,
record_type="arbitrary")
consumption_resampled.data = consumption_data.data.resample(self.freq).sum()
return { "consumption_resampled": consumption_resampled }