Reference

deflex.analyses module

Analyses of deflex.

SPDX-FileCopyrightText: 2016-2020 Uwe Krien <krien@uni-bremen.de>

SPDX-License-Identifier: MIT

deflex.analyses.check_comparision_of_merit_order(path)[source]

Comparison of two different ways to calculate the merit order.

  1. Calculate the merit order from scenario
  2. Calculate the merit order from the results

The resulting tables are not exactly the same because they will have some additional columns. The following columns should be the same.

“capacity”, “efficiency”, “fuel_emission”, “fuel”, “costs_total”, “capacity_cum”

Parameters:path (str) – Full path of results file.

Examples

>>> from deflex import results
>>> name = "de02_no_heat_reg_merit"
>>> my_path = results.fetch_example_results(name)
>>> check_comparision_of_merit_order(my_path)
Check passed! Both merit order DataFrame tables are the same.
deflex.analyses.get_flow_results(result)[source]

Extract values from the flows and calculate key values.

Parameters:result (dict) – A deflex results dictionary.
Returns:
Return type:pandas.DataFrame
deflex.analyses.merit_order_from_results(result)[source]

Create a merit order from deflex results.

Parameters:result (dict) – A deflex results dictionary.
Returns:
Return type:pandas.DataFrame

Examples

>>> from deflex import results
>>> fn = results.fetch_example_results("de02_no_heat_reg_merit")
>>> my_results = results.restore_results(fn)
>>> a = merit_order_from_results(my_results)
deflex.analyses.merit_order_from_scenario(path, with_downtime=True, with_co2_price=True)[source]

Create a merit order from a deflex scenario.

TODO: Check transmission. TODO: Add volatile sources as an optional feature TODO: Check chp. Warn if chp are present. Add chp as an option TODO: Or warn if no chp are present, installed capacity might be too high

Parameters:
  • path (str) – Path of the directory where the csv files of the scenario are located.
  • with_downtime (bool) – Use down time factor to reduce the installed capacity.
  • with_co2_price (bool) – Consider the CO2 price to calculate the merit order.
Returns:

Return type:

pandas.DataFrame

Examples

>>> import os
>>> my_path = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
...                        "tests", "data", "deflex_2014_de02_test_csv")
>>> mo1 = merit_order_from_scenario(my_path)
>>> round(mo1.capacity_cum.iloc[-1], 4)
71.9878
>>> round(mo1.capacity.sum(), 1)
71987.8
>>> round(mo1.loc[("DE01", "natural gas"), "costs_total"], 2)
59.93
>>> mo2 = merit_order_from_scenario(my_path, with_downtime=False)
>>> int(round(mo2.capacity.sum(), 0))
84225
>>> mo3 = merit_order_from_scenario(my_path, with_co2_price=False)
>>> round(mo3.loc[("DE01", "natural gas"), "costs_total"], 2)
52.87

deflex.config module

deflex.geometries module

Reegis geometry tools.

SPDX-FileCopyrightText: 2016-2019 Uwe Krien <krien@uni-bremen.de>

SPDX-License-Identifier: MIT

deflex.geometries.deflex_power_lines(rmap=None, rtype='lines')[source]
Parameters:
  • rmap (str) – Name of the deflex powerline map.
  • rtype (str) – Type of the deflex powerline map (‘lines’, ‘labels’).

Examples

>>> my_lines=deflex_power_lines('de17')
>>> my_lines.geometry.iloc[0].geom_type
'LineString'
>>> len(my_lines)
31
>>> deflex_power_lines('de02').index[0]
'DE01-DE02'
>>> cfg.tmp_set('init', 'map', 'de21')
>>> deflex_power_lines().name
'de21'
deflex.geometries.deflex_regions(rmap=None, rtype='polygons')[source]
Parameters:
  • rmap (str) – Name of the deflex map.
  • rtype (str) – Type of the deflex map (‘polygon’, ‘labels’).
Returns:

Return type:

GeoDataFrame

Examples

>>> my_regions=deflex_regions('de17')
>>> len(my_regions)
17
>>> my_regions.geometry.iloc[0].geom_type
'MultiPolygon'
>>> l=deflex_regions('de21', 'labels').loc['DE04', 'geometry']
>>> l.geom_type
'Point'
>>> l.x
13.2
>>> l.y
51.1
>>> cfg.tmp_set('init', 'map', 'de22')
>>> deflex_regions().name
'de22'
>>> list(deflex_regions('de02').index)
['DE01', 'DE02']
deflex.geometries.divide_off_and_onshore(regions)[source]

Sort regions into onshore and offshore regions. A namedtuple with two list of regions ids will be returned. Fetch the onshore and offshore attribute of the named tuple to get the list.

Parameters:regions (GeoDataFrame) – A region set with the region id in the index.
Returns:
Return type:named tuple

Examples

>>> reg=deflex_regions('de02')
>>> divide_off_and_onshore(reg).onshore
['DE01']
>>> reg=deflex_regions('de21')
>>> divide_off_and_onshore(reg).offshore
['DE19', 'DE20', 'DE21']

deflex.main module

Main script.

SPDX-FileCopyrightText: 2016-2019 Uwe Krien <krien@uni-bremen.de>

SPDX-License-Identifier: MIT

deflex.main.batch_model_scenario(path, named=True, file_type=None, ignore_errors=True)[source]

Model a single scenario in batch mode. By default errors will be ignored and returned together with the traceback.

Parameters:
  • path (str) – A valid deflex scenario.
  • file_type (str or None) – Type of the input data. Valid values are ‘csv’, ‘excel’, None. If the input is non the path schould end on ‘csv’, ‘.xls’, ‘.xlsx’.
  • named (bool) – If True a named tuple with the following fields will be returned
  • ignore_errors (bool) – Set True to stop the script if an error occurs for debugging. By default errors are ignored and returned.
Returns:

Return type:

namedtuple

Examples

>>> fn = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
...                   "tests", "data", "deflex_test_scenario.xls")
>>> r = batch_model_scenario(fn, ignore_errors=False)  # doctest: +ELLIPSIS
Welcome to the CBC MILP ...
>>> r.name
'deflex_test_scenario.xls'
>>> result_file = r.result_file
>>> os.path.basename(result_file)
'deflex_test_scenario_alpha.esys'
>>> r.trace
>>> r.return_value.year > 2019
True
>>> fn = os.path.join("wrong_file.xls")
>>> r = batch_model_scenario(fn)
>>> r.name
'wrong_file.xls'
>>> repr(r.return_value)
"FileNotFoundError(2, 'No such file or directory')"
>>> r.result_file
>>> r.trace  # doctest: +ELLIPSIS
'Traceback (most recent call last):...
>>> os.remove(result_file)
deflex.main.fetch_scenarios_from_dir(path, csv=True, xls=False)[source]

Search for files with an excel extension or directories ending with ‘_csv’.

By now it is not possible to distinguish between valid deflex scenarios and other excel files or directories ending with ‘csv’. Therefore, the given directory should only contain valid scenarios.

The function will not search recursively.

Parameters:
  • path (str) – Directory with valid deflex scenarios.
  • csv (bool) – Search for csv directories.
  • xls (bool) – Search for xls files.
Returns:

list

Return type:

Scenarios found in the given directory.

Examples

>>> test_data = os.path.join(os.path.dirname(__file__), os.pardir,
...                          os.pardir, "tests", "data")
>>> my_csv = fetch_scenarios_from_dir(test_data)
>>> len(my_csv)
2
>>> os.path.basename(my_csv[0])
'deflex_2014_de02_test_csv'
>>> my_excel = fetch_scenarios_from_dir(test_data, csv=False, xls=True)
>>> len(my_excel)
3
>>> os.path.basename(my_excel[0])
'deflex_2013_de02_test.xls'
>>> len(fetch_scenarios_from_dir(test_data, xls=True))
5
deflex.main.load_scenario(path, file_type=None)[source]

Create a deflex scenario object from file.

Parameters:
  • path (str) – A valid deflex scenario file.
  • file_type (str or None) – Type of the input data. Valid values are ‘csv’, ‘excel’, None. If the input is non the path should end on ‘csv’, ‘.xls’, ‘.xlsx’ to allow auto-detection.
Returns:

Return type:

deflex.DeflexScenario

Examples

>>> fn = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
...                   "tests", "data", "deflex_test_scenario.xls")
>>> s = load_scenario(fn, file_type="excel")
>>> type(s)
<class 'deflex.scenario_tools.DeflexScenario'>
>>> int(s.table_collection["volatile_source"]["capacity"]["DE02", "wind"])
517
>>> type(load_scenario(fn))
<class 'deflex.scenario_tools.DeflexScenario'>
>>> load_scenario(fn, file_type="csv")  # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
 ...
NotADirectoryError: [Errno 20] Not a directory:
deflex.main.model_multi_scenarios(scenarios, cpu_fraction=0.2, log_file=None)[source]
Parameters:
  • scenarios (iterable) – Multiple scenarios to be modelled in parallel.
  • cpu_fraction (float) – Fraction of available cpu cores to use for the parallel modelling. A resulting dezimal number of cores will be rounded up to an integer.
  • log_file (str) – Filename to store the log file.

Examples

>>> fn1 = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
...                    "tests", "data", "deflex_test_scenario.xls")
>>> fn2 = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
...                    "tests", "data", "deflex_test_scenario_broken.xls")
>>> my_log_file = os.path.join(os.path.dirname(__file__), os.pardir,
...                            os.pardir, "tests", "data",
...                            "my_log_file.csv")
>>> my_scenarios = [fn1, fn2]
>>> model_multi_scenarios(my_scenarios, log_file=my_log_file)
>>> my_log = pd.read_csv(my_log_file, index_col=[0])
>>> good = my_log.loc["deflex_test_scenario.xls"]
>>> rv = good["return_value"]
>>> datetime.strptime(rv, "%Y-%m-%d %H:%M:%S.%f").year > 2019
True
>>> good["trace"]
nan
>>> os.path.basename(good["result_file"])
'deflex_test_scenario_alpha.esys'
>>> broken = my_log.loc["deflex_test_scenario_broken.xls"]
>>> broken["return_value"].replace("'", "")  # doctest: +ELLIPSIS
'ValueError(Missing time series for geothermal (capacity: 31.4) in DE01...
>>> broken["trace"]  # doctest: +ELLIPSIS
'Traceback (most recent call last)...
>>> broken["result_file"]
nan
>>> os.remove(my_log_file)
>>> os.remove(good["result_file"])
deflex.main.model_scenario(path=None, file_type=None, result_path=None)[source]

Compute a deflex scenario.

Parameters:
  • path (str or None) – File or directory with a valid deflex scenario. If no path is given an energy system (es) has to be passed.
  • file_type (str or None) – Type of the input data. Valid values are ‘csv’, ‘excel’, None. If the input is non the path schould end on ‘csv’, ‘.xls’, ‘.xlsx’.
  • result_path (str or None) – Path to store the output file. If None the results will be stored along with the scenarios.

Examples

>>> fn = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
...                   "tests", "data", "deflex_test_scenario.xls")
>>> r = model_scenario(fn, file_type="excel")  # doctest: +ELLIPSIS
Welcome to the CBC MILP ...
>>> rf = os.path.join(os.path.dirname(fn), "results_cbc",
...                   "deflex_test_scenario_alpha.esys")
>>> os.remove(rf)
deflex.main.plot_scenario(path, file_type=None, image_file=None)[source]

Plot the graph of an energy system. If no filename is given the plot will be shown on the screen but not writen to an image file

Parameters:
  • path (str) – A valid deflex scenario file.
  • file_type (str or None) – Type of the input data. Valid values are ‘csv’, ‘excel’, None. If the input is none the path should end on ‘csv’, ‘.xls’, ‘.xlsx’ to allow auto detection.
  • image_file (str) – The image file with a valid suffix (e.g. png, pdf, svg).
Returns:

TODO

Return type:

Keep this test? It does not work without graphviz-dev and python3-dev

Examples

>>> fn = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
...      "tests", "data", "deflex_test_scenario.xls")
>>> fn_img = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir,
...                       "tests", "data", "test_es.graphml")
>>> plot_scenario(fn, "excel", fn_img)
>>> os.path.isfile(fn_img)
True
>>> os.remove(fn_img)
>>> os.path.isfile(fn_img)
False
deflex.main.stopwatch()[source]

Track the running time.

deflex.nodes module

deflex.postprocessing module

deflex.scenario module

deflex.scenario_creator module

deflex.tools module

Processing a list of power plants in Germany.

SPDX-FileCopyrightText: 2016-2019 Uwe Krien <krien@uni-bremen.de>

SPDX-License-Identifier: MIT

deflex.tools.download(fn, url)[source]

deflex.transmission module

Processing a list of power plants in Germany.

SPDX-FileCopyrightText: 2016-2019 Uwe Krien <krien@uni-bremen.de>

SPDX-License-Identifier: MIT

deflex.transmission.add_reverse_direction(df)[source]

Duplicate all entries of a DataFrame with a reverse index. The index must contain a dash between two sub-strings.

deflex.transmission.get_electrical_transmission_default(rmap=None, power_lines=None, both_directions=False)[source]

Creates a default set of transmission capacities, distance and efficiency. The map of the lines must exist in the geometries directory. The default values are infinity for the capacity, nan for the distance and 1 for the efficiency.

Parameters:
  • rmap (str) – The name of the transmission line map, that is part of deflex.
  • power_lines (iterable[str]) – A list of names of transmission lines. All name must contain a dash between the id of the regions (FromRegion-ToRegion).
  • both_directions (bool) – If True any line will be replicated in the reverse direction.
Returns:

Transmission capacity, distance and efficiency between regions

Return type:

pd.DataFrame

Examples

>>> df=get_electrical_transmission_default('de21')
>>> df.loc['DE10-DE12', 'capacity']
inf
>>> df.loc['DE10-DE12', 'distance']
nan
>>> df.loc['DE10-DE12', 'efficiency']
1.0
>>> len(df)
39
>>> len(get_electrical_transmission_default('de22'))
40
>>> len(get_electrical_transmission_default('de17'))
31
>>> len(get_electrical_transmission_default('de02'))
1
>>> my_lines=['reg1-reg2', 'reg2-reg3']
>>> df=get_electrical_transmission_default(power_lines=my_lines)
>>> df.loc['reg1-reg2', 'capacity']
inf
>>> df=get_electrical_transmission_default(power_lines=my_lines,
...                                          both_directions=True)
>>> df.loc['reg2-reg1', 'capacity']
inf
deflex.transmission.get_electrical_transmission_renpass(both_directions=False)[source]

Prepare the transmission capacity and distance between de21 regions from the renpass database. The original table of the reegis database is transferred to a csv file, which is part of the reegis package. As renpass is deprecated it will not change in the future. The index uses the format ‘region1-region2’. The distance is taken from centroid to centroid. By default every region pair exists only once. It is possible to get an entry in both directions if the parameter both_directions is set True.

The capacity calculation is taken from the description of the renpass package [1]. The data is taken from the renpass database [2].

This function is only valid for the original renpass region set.

Parameters:both_directions (bool) – If True any line will be replicated in the reverse direction.
Returns:Transmission capacity and distance between regions
Return type:pd.DataFrame

References

[1]Wiese, Frauke (2015). „Renewable Energy Pathways Simulation System – Open Source as an approach to meet challenges in energy modeling“. Diss. University of Flensburg. URL : https://www.reiner-lemoine-stiftung.de/pdf/dissertationen/Dissertation_Frauke_Wiese.pdf. (page 49)
[2]Wiese, F.: Renpass - Renewable Energy Pathways Simulation System, https://github.com/fraukewiese/renpass

Examples

>>> translines=get_electrical_transmission_renpass()
>>> int(translines.loc['DE11-DE17', 'capacity'])
2506
>>> int(translines.loc['DE18-DE17', 'distance'])
119
>>> translines.loc['DE08-DE06']
capacity    7519.040402
distance     257.000000
Name: DE08-DE06, dtype: float64
>>> translines=get_electrical_transmission_renpass(both_directions=True)
>>> int(translines.loc['DE11-DE17', 'capacity'])
2506
>>> int(translines.loc['DE17-DE11', 'capacity'])
2506
deflex.transmission.get_grid_capacity(grid, plus, minus)[source]

Read the grid capacity from a given region pair from the renpass db.

deflex.transmission.scenario_transmission(table_collection, regions, name)[source]

Get power plants for the scenario year

Examples

>>> my_regions=geometries.deflex_regions(rmap="de21")  # doctest: +SKIP
>>> pp=scenario_powerplants(dict(), my_regions, 2014, "de21"
...     )  # doctest: +SKIP
>>> lines=scenario_transmission(pp, my_regions, "de21")  # doctest: +SKIP
>>> int(lines.loc["DE07-DE05", ("electrical", "capacity")]
...     )  # doctest: +SKIP
1978
>>> int(lines.loc["DE07-DE05", ("electrical", "distance")]
...     )  # doctest: +SKIP
199
>>> float(lines.loc["DE07-DE05", ("electrical", "efficiency")]
...     )  # doctest: +SKIP
0.9
>>> cfg.tmp_set("basic", "copperplate", "True")
>>> lines=scenario_transmission(pp, regions, "de21"
...     )  # doctest: +SKIP
>>> cfg.tmp_set("basic", "copperplate", "False")
>>> float(lines.loc["DE07-DE05", ("electrical", "capacity")]
...     )  # doctest: +SKIP
inf
>>> float(lines.loc["DE07-DE05", ("electrical", "distance")]
...     )  # doctest: +SKIP
nan
>>> float(lines.loc["DE07-DE05", ("electrical", "efficiency")]
...     )  # doctest: +SKIP
1.0

Module contents