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_data_and_valid_unit(
interpretation, unnormalized_unit_with_target_unit, unit_timeseries):
unnormalized_unit, normalized_unit, mult = \
unnormalized_unit_with_target_unit
et = EnergyTrace(interpretation=interpretation, data=unit_timeseries,
unit=unnormalized_unit)
assert et.interpretation == interpretation
assert et.unit == normalized_unit
np.testing.assert_allclose(
et.data.value.iloc[0], (unit_timeseries.value * mult).iloc[0],
rtol=1e-3, atol=1e-3)
assert not et.data.estimated.iloc[0]
assert not et.placeholder
def test_data_but_no_unit(interpretation):
with pytest.raises(ValueError):
EnergyTrace(interpretation=interpretation, data=pd.DataFrame())
def test_min_contiguous_months(input_df):
min_contiguous_months = 9
model = HourlyDayOfWeekModel(min_contiguous_months=min_contiguous_months)
with pytest.raises(model_exceptions.DataSufficiencyException) as sufficiency_exception:
model.fit(input_df)
def test_TemperatureSensitivityModel_with_heating():
initial_params = {
"base_daily_consumption": 0,
"heating_slope": 0,
"heating_balance_temperature": 55,
}
param_bounds = {
"base_daily_consumption": [0,100],
"heating_slope": [0,100],
"heating_balance_temperature": [50,60],
}
model = AverageDailyTemperatureSensitivityModel(heating=True, cooling=False, initial_params=initial_params, param_bounds=param_bounds)
params = model.param_type([1,60,1])
observed_temps = np.array([[i] for i in range(50,70)])
usages = model.transform(observed_temps,params)
assert_allclose(usages[8:13],[3,2,1,1,1], rtol=1e-2, atol=1e-2)
opt_params = model.fit(observed_temps, usages)
assert_allclose(params.to_list(), opt_params.to_list(), rtol=1e-2, atol=1e-2)
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")