Skip to content

Commit

Permalink
Revise test structure
Browse files Browse the repository at this point in the history
  • Loading branch information
uvchik committed Dec 18, 2020
1 parent c5a4e1f commit a93985a
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 167 deletions.
19 changes: 10 additions & 9 deletions examples/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,12 +714,13 @@ def main_deflex(path, name=None):
main.model_scenario(scenario)


logger.define_logging()
my_path = "/path/to/store/example/files"
download_example_scenarios(my_path)
# main_deflex(my_path, name="de02")
my_mcp = fetch_mcp(my_path)
show_relation(my_mcp, name="deflex_2014_de02")
compare_different_mcp(my_mcp)
compare_emission_types(my_path, name="deflex_2014_de02")
show_transmission(my_path, name="de21_transmission-losses")
if __name__ == "__main__":
logger.define_logging()
my_path = "/path/to/store/example/files"
download_example_scenarios(my_path)
# main_deflex(my_path, name="de02")
my_mcp = fetch_mcp(my_path)
show_relation(my_mcp, name="deflex_2014_de02")
compare_different_mcp(my_mcp)
compare_emission_types(my_path, name="deflex_2014_de02")
show_transmission(my_path, name="de21_transmission-losses")
4 changes: 2 additions & 2 deletions src/deflex/geometries.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def deflex_regions(rmap=None, rtype="polygons"):
Examples
--------
>>> my_regions=deflex_regions('de17')
>>> len(regions)
>>> len(my_regions)
17
>>> my_regions.geometry.iloc[0].geom_type
'MultiPolygon'
Expand Down Expand Up @@ -87,7 +87,7 @@ def deflex_power_lines(rmap=None, rtype="lines"):
>>> my_lines=deflex_power_lines('de17')
>>> my_lines.geometry.iloc[0].geom_type
'LineString'
>>> len(lines)
>>> len(my_lines)
31
>>> deflex_power_lines('de02').index[0]
'DE01-DE02'
Expand Down
4 changes: 2 additions & 2 deletions src/deflex/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ def batch_model_scenario(path, named=True, file_type=None, ignore_errors=True):
Examples
--------
>>> fn = os.path.join(os.path.dirname(__file__), os.pardir,
>>> fn = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
... "tests", "data", "deflex_test_scenario.xls")
>>> r = batch_model_scenario(fn) # doctest: +ELLIPSIS
>>> r = batch_model_scenario(fn, ignore_errors=False) # doctest: +ELLIPSIS
Welcome to the CBC MILP ...
>>> r.name
'deflex_test_scenario.xls'
Expand Down
35 changes: 15 additions & 20 deletions src/deflex/scenario_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@
from collections import namedtuple

import pandas as pd

from deflex import config as cfg
from deflex import (
geometries,
scenario_tools,
transmission,
)

from scenario_builder import (
demand,
powerplants,
storages,
mobility,
feedin,
commodity,
)
from deflex import geometries
from deflex import scenario_tools
from deflex import transmission

try:
from scenario_builder import commodity
from scenario_builder import demand
from scenario_builder import feedin
from scenario_builder import mobility
from scenario_builder import powerplants
from scenario_builder import storages
except ModuleNotFoundError:
scenario_builer = None


def scenario_decentralised_heat():
Expand Down Expand Up @@ -185,12 +185,7 @@ def clean_time_series(table_collection):


def create_basic_scenario(
year,
rmap=None,
path=None,
csv_dir=None,
xls_name=None,
only_out=None,
year, rmap=None, path=None, csv_dir=None, xls_name=None, only_out=None,
):
"""
Create a basic scenario for a given year and region-set.
Expand Down
2 changes: 1 addition & 1 deletion tests/test_scenario_demand.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import os
from nose.tools import eq_, with_setup
from deflex import config as cfg, basic_scenario, geometries
from deflex import config as cfg, geometries
from reegis.tools import download_file


Expand Down
3 changes: 1 addition & 2 deletions tests/test_scenario_feedin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

import os
from shutil import copyfile
from deflex import config as cfg, basic_scenario, geometries
from reegis.tools import download_file
from deflex import config as cfg, geometries


class TestFeedin:
Expand Down
146 changes: 73 additions & 73 deletions tests/test_scenario_powerplant_and_chp.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,78 +11,78 @@
__license__ = "MIT"

from nose.tools import eq_, assert_raises_regexp
from deflex import basic_scenario, geometries, powerplants, config as cfg
from deflex import geometries, powerplants, config as cfg


class TestScenarioPowerplantsAndCHP:
@classmethod
def setup_class(cls):
cls.regions = geometries.deflex_regions(rmap="de21")
cls.pp = basic_scenario.scenario_powerplants(
dict(), cls.regions, 2014, "de21",
)

def test_01_deflex_power_plants_by_year(self):
pp = powerplants.get_deflex_pp_by_year(
self.regions, 2014, "de21", overwrite_capacity=True
)
eq_(int(pp["capacity"].sum()), 181489)

def test_scenario_pp(self):
eq_(
float(
self.pp["volatile_source"].loc[("DE03", "wind"), "capacity"]
),
3052.8,
)
eq_(
float(self.pp["transformer"].loc[("DE03", "lignite"), "capacity"]),
1135.6,
)

def test_scenario_transmission(self):
cfg.tmp_set("init", "map", "de22")
lines = basic_scenario.scenario_transmission(
self.pp, self.regions, "de22"
)
line = "DE07-DE05"
eq_(int(lines.loc[line, ("electrical", "capacity")]), 1978)
eq_(int(lines.loc[line, ("electrical", "distance")]), 199)
eq_(float(lines.loc[line, ("electrical", "efficiency")]), 0.9)
cfg.tmp_set("basic", "copperplate", True)
lines = basic_scenario.scenario_transmission(
self.pp, self.regions, "de22"
)
cfg.tmp_set("basic", "copperplate", False)
eq_(
float(lines.loc["DE07-DE05", ("electrical", "capacity")]),
float("inf"),
)
eq_(str(lines.loc["DE07-DE05", ("electrical", "distance")]), "nan")
eq_(float(lines.loc["DE07-DE05", ("electrical", "efficiency")]), 1.0)

def test_scenario_transmisson_error(self):
old_value = cfg.get("transmission", "general_efficiency")
cfg.tmp_set("transmission", "general_efficiency", "None")
msg = "The calculation of the efficiency by distance is not yet"
with assert_raises_regexp(NotImplementedError, msg):
basic_scenario.scenario_transmission(self.pp, self.regions, "de22")
cfg.tmp_set("transmission", "general_efficiency", old_value)

def test_scenario_commodity_sources(self):
src = basic_scenario.scenario_commodity_sources(2013)
eq_(round(src.loc[("DE", "hard coal"), "costs"], 2), 12.53)
eq_(round(src.loc[("DE", "natural gas"), "emission"], 2), 201.0)

def test_chp(self):
eq_(
int(self.pp["transformer"].loc[("DE01", "hard coal"), "capacity"]),
1291,
)
tables = basic_scenario.scenario_chp(
self.pp, self.regions, 2014, "de21"
)
transf = tables["transformer"]
chp = tables["chp_hp"]
eq_(int(transf.loc[("DE01", "hard coal"), "capacity"]), 623)
eq_(int(chp.loc[("DE01", "hard coal"), "capacity_elec_chp"]), 667)
# class TestScenarioPowerplantsAndCHP:
# @classmethod
# def setup_class(cls):
# cls.regions = geometries.deflex_regions(rmap="de21")
# cls.pp = basic_scenario.scenario_powerplants(
# dict(), cls.regions, 2014, "de21",
# )
#
# def test_01_deflex_power_plants_by_year(self):
# pp = powerplants.get_deflex_pp_by_year(
# self.regions, 2014, "de21", overwrite_capacity=True
# )
# eq_(int(pp["capacity"].sum()), 181489)
#
# def test_scenario_pp(self):
# eq_(
# float(
# self.pp["volatile_source"].loc[("DE03", "wind"), "capacity"]
# ),
# 3052.8,
# )
# eq_(
# float(self.pp["transformer"].loc[("DE03", "lignite"), "capacity"]),
# 1135.6,
# )
#
# def test_scenario_transmission(self):
# cfg.tmp_set("init", "map", "de22")
# lines = basic_scenario.scenario_transmission(
# self.pp, self.regions, "de22"
# )
# line = "DE07-DE05"
# eq_(int(lines.loc[line, ("electrical", "capacity")]), 1978)
# eq_(int(lines.loc[line, ("electrical", "distance")]), 199)
# eq_(float(lines.loc[line, ("electrical", "efficiency")]), 0.9)
# cfg.tmp_set("basic", "copperplate", True)
# lines = basic_scenario.scenario_transmission(
# self.pp, self.regions, "de22"
# )
# cfg.tmp_set("basic", "copperplate", False)
# eq_(
# float(lines.loc["DE07-DE05", ("electrical", "capacity")]),
# float("inf"),
# )
# eq_(str(lines.loc["DE07-DE05", ("electrical", "distance")]), "nan")
# eq_(float(lines.loc["DE07-DE05", ("electrical", "efficiency")]), 1.0)
#
# def test_scenario_transmisson_error(self):
# old_value = cfg.get("transmission", "general_efficiency")
# cfg.tmp_set("transmission", "general_efficiency", "None")
# msg = "The calculation of the efficiency by distance is not yet"
# with assert_raises_regexp(NotImplementedError, msg):
# basic_scenario.scenario_transmission(self.pp, self.regions, "de22")
# cfg.tmp_set("transmission", "general_efficiency", old_value)
#
# def test_scenario_commodity_sources(self):
# src = basic_scenario.scenario_commodity_sources(2013)
# eq_(round(src.loc[("DE", "hard coal"), "costs"], 2), 12.53)
# eq_(round(src.loc[("DE", "natural gas"), "emission"], 2), 201.0)
#
# def test_chp(self):
# eq_(
# int(self.pp["transformer"].loc[("DE01", "hard coal"), "capacity"]),
# 1291,
# )
# tables = basic_scenario.scenario_chp(
# self.pp, self.regions, 2014, "de21"
# )
# transf = tables["transformer"]
# chp = tables["chp_hp"]
# eq_(int(transf.loc[("DE01", "hard coal"), "capacity"]), 623)
# eq_(int(chp.loc[("DE01", "hard coal"), "capacity_elec_chp"]), 667)
112 changes: 55 additions & 57 deletions tests/test_scenario_z1_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,62 @@
__license__ = "MIT"

import os
from nose.tools import eq_
from unittest.mock import MagicMock
import pandas as pd
from deflex import basic_scenario, geometries, demand
from reegis import energy_balance
from deflex import scenario_builder, geometries, demand


def test_scenario_creation():
data = {}
for name in ["volatile_series", "demand_series"]:
fn = os.path.join(
os.path.dirname(__file__),
"data",
"deflex_2014_de21_test_csv",
name + ".csv",
)
data[name] = pd.read_csv(fn, index_col=[0], header=[0, 1])

name = "heat_demand_deflex"
fn = os.path.join(os.path.dirname(__file__), "data", name + ".csv")
data[name] = pd.read_csv(fn, index_col=[0], header=[0, 1])

name = "transformer_balance"
fn = os.path.join(os.path.dirname(__file__), "data", name + ".csv")
data[name] = pd.read_csv(fn, index_col=[0, 1, 2], header=[0])

basic_scenario.scenario_feedin = MagicMock(
return_value=data["volatile_series"]
)
basic_scenario.scenario_demand = MagicMock(
return_value=data["demand_series"]
)
energy_balance.get_transformation_balance_by_region = MagicMock(
return_value=data["transformer_balance"]
)
demand.get_heat_profiles_deflex = MagicMock(
return_value=data["heat_demand_deflex"]
)
regions = geometries.deflex_regions(rmap="de21")
table_collection = basic_scenario.create_scenario(regions, 2014, "de21")
eq_(
sorted(list(table_collection.keys())),
sorted(
[
"meta",
"storages",
"transformer",
"volatile_source",
"chp_hp",
"transmission",
"decentralised_heat",
"commodity_source",
"volatile_series",
"demand_series",
"mobility_energy_content",
"mobility_mileage",
"mobility_spec_demand",
]
),
)
eq_(len(list(table_collection.keys())), 13)
# def test_scenario_creation():
# data = {}
# for name in ["volatile_series", "demand_series"]:
# fn = os.path.join(
# os.path.dirname(__file__),
# "data",
# "deflex_2014_de21_test_csv",
# name + ".csv",
# )
# data[name] = pd.read_csv(fn, index_col=[0], header=[0, 1])
#
# name = "heat_demand_deflex"
# fn = os.path.join(os.path.dirname(__file__), "data", name + ".csv")
# data[name] = pd.read_csv(fn, index_col=[0], header=[0, 1])
#
# name = "transformer_balance"
# fn = os.path.join(os.path.dirname(__file__), "data", name + ".csv")
# data[name] = pd.read_csv(fn, index_col=[0, 1, 2], header=[0])
#
# basic_scenario.scenario_feedin = MagicMock(
# return_value=data["volatile_series"]
# )
# basic_scenario.scenario_demand = MagicMock(
# return_value=data["demand_series"]
# )
# energy_balance.get_transformation_balance_by_region = MagicMock(
# return_value=data["transformer_balance"]
# )
# demand.get_heat_profiles_deflex = MagicMock(
# return_value=data["heat_demand_deflex"]
# )
# regions = geometries.deflex_regions(rmap="de21")
# table_collection = basic_scenario.create_scenario(regions, 2014, "de21")
# eq_(
# sorted(list(table_collection.keys())),
# sorted(
# [
# "meta",
# "storages",
# "transformer",
# "volatile_source",
# "chp_hp",
# "transmission",
# "decentralised_heat",
# "commodity_source",
# "volatile_series",
# "demand_series",
# "mobility_energy_content",
# "mobility_mileage",
# "mobility_spec_demand",
# ]
# ),
# )
# eq_(len(list(table_collection.keys())), 13)
Loading

0 comments on commit a93985a

Please sign in to comment.