Skip to content

Commit

Permalink
Bug fix make sure to use clean object for each test
Browse files Browse the repository at this point in the history
  • Loading branch information
birgits committed Mar 8, 2023
1 parent c1ac2be commit 18d754f
Showing 1 changed file with 54 additions and 53 deletions.
107 changes: 54 additions & 53 deletions tests/network/test_heat.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
class TestHeatPump:
@classmethod
def setup_class(cls):
# set up data used in tests
cls.heatpump = HeatPump()

cls.timeindex = pd.date_range("1/1/2011 12:00", periods=2, freq="H")
cls.cop = pd.DataFrame(
Expand Down Expand Up @@ -66,16 +64,16 @@ def setup_egon_heat_pump_data(self):
def test_set_cop(self):

# ################### test with dataframe ###################
self.edisgo = EDisGo(ding0_grid=pytest.ding0_test_network_path)
heat_pump = HeatPump()
cop = pd.DataFrame(
data={
"hp3": [5.0, 6.0],
},
index=self.timeindex,
)
self.heatpump.set_cop(self.edisgo, cop)
heat_pump.set_cop(None, cop)
pd.testing.assert_frame_equal(
self.heatpump.cop_df,
heat_pump.cop_df,
cop,
check_freq=False,
)
Expand All @@ -88,9 +86,9 @@ def test_set_cop(self):
},
index=self.timeindex,
)
self.heatpump.set_cop(self.edisgo, cop)
heat_pump.set_cop(None, cop)
pd.testing.assert_frame_equal(
self.heatpump.cop_df,
heat_pump.cop_df,
cop,
check_freq=False,
)
Expand Down Expand Up @@ -135,37 +133,37 @@ def test_set_cop_oedb(self, caplog):
edisgo_object.topology.loads_df.index[0:1]
)
with caplog.at_level(logging.WARNING):
self.heatpump.set_cop(
edisgo_object.heat_pump.set_cop(
edisgo_object,
"oedb",
engine=pytest.engine,
heat_pump_names=heat_pump_names,
)
assert "There are heat pumps with no weather cell ID." in caplog.text
assert self.heatpump.cop_df.shape == (8760, 4)
assert edisgo_object.heat_pump.cop_df.shape == (8760, 4)

# test with empty list for heat_pump_names
self.heatpump.set_cop(
edisgo_object.heat_pump.set_cop(
edisgo_object,
"oedb",
engine=pytest.engine,
heat_pump_names=[],
)
assert self.heatpump.cop_df.shape == (8760, 4)
assert edisgo_object.heat_pump.cop_df.shape == (8760, 4)

def test_set_heat_demand(self):
# test with dataframe
self.edisgo = EDisGo(ding0_grid=pytest.ding0_test_network_path)
heat_pump = HeatPump()
heat_demand = pd.DataFrame(
data={
"hp3": [1.0, 2.0],
"hp4": [1.0, 2.0],
},
index=self.timeindex,
)
self.heatpump.set_heat_demand(self.edisgo, heat_demand)
heat_pump.set_heat_demand(None, heat_demand)
pd.testing.assert_frame_equal(
self.heatpump.heat_demand_df,
heat_pump.heat_demand_df,
heat_demand,
check_freq=False,
)
Expand All @@ -178,10 +176,10 @@ def test_set_heat_demand(self):
},
index=self.timeindex,
)
self.heatpump.set_heat_demand(self.edisgo, heat_demand)
heat_pump.set_heat_demand(None, heat_demand)
heat_demand["hp4"] = pd.Series([1.0, 2.0], index=self.timeindex)
pd.testing.assert_frame_equal(
self.heatpump.heat_demand_df[sorted(self.heatpump.heat_demand_df)],
heat_pump.heat_demand_df[sorted(heat_pump.heat_demand_df)],
heat_demand[sorted(heat_demand.columns)],
check_freq=False,
)
Expand All @@ -198,75 +196,77 @@ def test_set_heat_demand_oedb(self):
)

# ################# test with no timeindex to get year from #############
self.heatpump.set_heat_demand(
edisgo_object.heat_pump.set_heat_demand(
edisgo_object,
"oedb",
engine=pytest.engine,
scenario="eGon2035",
)
assert self.heatpump.heat_demand_df.shape == (8760, 3)
assert self.heatpump.heat_demand_df.index[0].year == 2035
assert edisgo_object.heat_pump.heat_demand_df.shape == (8760, 3)
assert edisgo_object.heat_pump.heat_demand_df.index[0].year == 2035

# ###### test with timeindex to get year from and invalid heat pump name #####
# reset heat_demand_df
self.heatpump.heat_demand_df = pd.DataFrame()
edisgo_object.heat_pump.heat_demand_df = pd.DataFrame()
edisgo_object.set_timeindex(
pd.date_range("1/1/2011 12:00", periods=2, freq="H")
)
self.heatpump.set_heat_demand(
edisgo_object.heat_pump.set_heat_demand(
edisgo_object,
"oedb",
engine=pytest.engine,
scenario="eGon2035",
heat_pump_names=["HP_442081", "HP_dummy"],
)
assert self.heatpump.heat_demand_df.shape == (8760, 1)
assert self.heatpump.heat_demand_df.index[0].year == 2011
assert edisgo_object.heat_pump.heat_demand_df.shape == (8760, 1)
assert edisgo_object.heat_pump.heat_demand_df.index[0].year == 2011

# ###### test with empty list for heat pump names #####
self.heatpump.set_heat_demand(
edisgo_object.heat_pump.set_heat_demand(
edisgo_object,
"oedb",
engine=pytest.engine,
scenario="eGon2035",
heat_pump_names=[],
)
assert self.heatpump.heat_demand_df.shape == (8760, 1)
assert self.heatpump.heat_demand_df.index[0].year == 2011
assert edisgo_object.heat_pump.heat_demand_df.shape == (8760, 1)
assert edisgo_object.heat_pump.heat_demand_df.index[0].year == 2011

def test_reduce_memory(self):

self.heatpump.cop_df = self.cop
self.heatpump.heat_demand_df = self.heat_demand
heatpump = HeatPump()
heatpump.cop_df = self.cop
heatpump.heat_demand_df = self.heat_demand

# check with default value
assert (self.heatpump.cop_df.dtypes == "float64").all()
assert (self.heatpump.heat_demand_df.dtypes == "float64").all()
assert (heatpump.cop_df.dtypes == "float64").all()
assert (heatpump.heat_demand_df.dtypes == "float64").all()

self.heatpump.reduce_memory()
heatpump.reduce_memory()

assert (self.heatpump.cop_df.dtypes == "float32").all()
assert (self.heatpump.heat_demand_df.dtypes == "float32").all()
assert (heatpump.cop_df.dtypes == "float32").all()
assert (heatpump.heat_demand_df.dtypes == "float32").all()

# check arguments
self.heatpump.reduce_memory(to_type="float16", attr_to_reduce=["cop_df"])
heatpump.reduce_memory(to_type="float16", attr_to_reduce=["cop_df"])

assert (self.heatpump.cop_df.dtypes == "float16").all()
assert (self.heatpump.heat_demand_df.dtypes == "float32").all()
assert (heatpump.cop_df.dtypes == "float16").all()
assert (heatpump.heat_demand_df.dtypes == "float32").all()

# check with empty dataframes
self.heatpump.heat_demand_df = pd.DataFrame()
self.heatpump.reduce_memory()
heatpump.heat_demand_df = pd.DataFrame()
heatpump.reduce_memory()

def test_to_csv(self):

self.heatpump.cop_df = self.cop
self.heatpump.heat_demand_df = self.heat_demand
self.heatpump.thermal_storage_units_df = self.tes
heatpump = HeatPump()
heatpump.cop_df = self.cop
heatpump.heat_demand_df = self.heat_demand
heatpump.thermal_storage_units_df = self.tes

# test with default values
save_dir = os.path.join(os.getcwd(), "heat_pump_csv")
self.heatpump.to_csv(save_dir)
heatpump.to_csv(save_dir)

files_in_dir = os.listdir(save_dir)
assert len(files_in_dir) == 3
Expand All @@ -277,41 +277,42 @@ def test_to_csv(self):
shutil.rmtree(save_dir)

# test with reduce memory True, to_type = float16
self.heatpump.to_csv(save_dir, reduce_memory=True, to_type="float16")
heatpump.to_csv(save_dir, reduce_memory=True, to_type="float16")

assert (self.heatpump.cop_df.dtypes == "float16").all()
assert (heatpump.cop_df.dtypes == "float16").all()
files_in_dir = os.listdir(save_dir)
assert len(files_in_dir) == 3

shutil.rmtree(save_dir, ignore_errors=True)

def test_from_csv(self):

self.heatpump.cop_df = self.cop
self.heatpump.heat_demand_df = self.heat_demand
self.heatpump.thermal_storage_units_df = self.tes
heatpump = HeatPump()
heatpump.cop_df = self.cop
heatpump.heat_demand_df = self.heat_demand
heatpump.thermal_storage_units_df = self.tes

# write to csv
save_dir = os.path.join(os.getcwd(), "heat_pump_csv")
self.heatpump.to_csv(save_dir)
heatpump.to_csv(save_dir)

# reset HeatPump object
self.heatpump = HeatPump()
heatpump = HeatPump()

self.heatpump.from_csv(save_dir)
heatpump.from_csv(save_dir)

pd.testing.assert_frame_equal(
self.heatpump.cop_df,
heatpump.cop_df,
self.cop,
check_freq=False,
)
pd.testing.assert_frame_equal(
self.heatpump.heat_demand_df,
heatpump.heat_demand_df,
self.heat_demand,
check_freq=False,
)
pd.testing.assert_frame_equal(
self.heatpump.thermal_storage_units_df,
heatpump.thermal_storage_units_df,
self.tes,
)

Expand Down

0 comments on commit 18d754f

Please sign in to comment.