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_no_data_no_placeholder(interpretation):
with pytest.raises(ValueError):
EnergyTrace(interpretation=interpretation)
def test_data_and_placeholder(interpretation):
with pytest.raises(ValueError):
EnergyTrace(interpretation=interpretation, data=pd.DataFrame(),
placeholder=True)
def test_bad_column_name_data(interpretation, unit):
data = {"energy": [1, np.nan], "estimated": [False, False]}
columns = ["energy", "estimated"]
index = pd.date_range('2000-01-01', periods=2, freq='D')
df = pd.DataFrame(data, index=index, columns=columns)
with pytest.raises(ValueError):
EnergyTrace(interpretation=interpretation, data=df, unit=unit)
def test_has_trace_id_and_interval():
et = EnergyTrace(
interpretation='ELECTRICITY_CONSUMPTION_SUPPLIED',
placeholder=True
)
assert et.trace_id is None
assert et.interval is None
et = EnergyTrace(
interpretation='ELECTRICITY_CONSUMPTION_SUPPLIED',
placeholder=True,
trace_id='ABC',
interval='daily'
)
assert et.trace_id == 'ABC'
assert et.interval == 'daily'
assert 'ABC' in str(et)
"value": [1, 1, 1, 1, np.nan] + [1,]*13,
"estimated": [False, False, True, False, False] + [False,]*13
}
columns = ["value", "estimated"]
index = [
datetime(2011, 1, 1, tzinfo=pytz.UTC),
datetime(2011, 2, 1, tzinfo=pytz.UTC),
datetime(2011, 3, 2, tzinfo=pytz.UTC),
datetime(2011, 4, 3, tzinfo=pytz.UTC),
datetime(2011, 4, 29, tzinfo=pytz.UTC),
] + [
datetime(2011, 6, 1, tzinfo=pytz.UTC) + timedelta(days=30*i)
for i in range(13)
]
df = pd.DataFrame(data, index=index, columns=columns)
return EnergyTrace("ELECTRICITY_CONSUMPTION_SUPPLIED", df, unit="KWH")
def test_non_timeseries_data(interpretation, unit):
data = {"value": [1, np.nan], "estimated": [False, False]}
columns = ["value", "estimated"]
df = pd.DataFrame(data, columns=columns)
with pytest.raises(ValueError):
EnergyTrace(interpretation=interpretation, data=df, unit=unit)
def daily_trace():
data = {
"value": np.tile(1, (365,)),
"estimated": np.tile(False, (365,)),
}
columns = ["value", "estimated"]
index = pd.date_range('2000-01-01', periods=365, freq='D', tz=pytz.UTC)
df = pd.DataFrame(data, index=index, columns=columns)
return EnergyTrace("ELECTRICITY_CONSUMPTION_SUPPLIED", df, unit="KWH")
# Get all consumption records, group by fuel type.
for flow_direction, records in self._get_consumption_record_groups():
if len(records) > 0:
fuel_type_records = defaultdict(list)
for record in records:
fuel_type_records[record["fuel_type"]].append(record)
# Wrap records in EnergyTrace objects, by fuel type.
for fuel_type, records in fuel_type_records.items():
if fuel_type is None:
fuel_type = service_kind_default
selector = (fuel_type, flow_direction)
interpretation = INTERPRETATION_MAPPING[selector]
yield EnergyTrace(
interpretation,
records=records,
unit=records[0]["unit_name"],
serializer=ArbitrarySerializer())