Skip to content

Commit

Permalink
Merge pull request #191 from oemof/fix_issue_190
Browse files Browse the repository at this point in the history
Fix issue #190
  • Loading branch information
uvchik authored Aug 12, 2016
2 parents 0e7a57d + 7cb564c commit 912b86a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 49 deletions.
32 changes: 16 additions & 16 deletions examples/example_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,22 @@ def check_nosetests():

test_results = {
'objective': 2947725.249402091,
('b_el', 'input', 'pp_chp', 'val'): 11161.357450000065,
('b_el', 'input', 'pp_coal', 'val'): 33723.047672110595,
('b_el', 'input', 'pp_gas', 'val'): 30412.377779000046,
('b_el', 'input', 'pp_lig', 'val'): 22066.451080999268,
('b_el', 'input', 'pp_oil', 'val'): 2.2872599999999998,
('b_el', 'input', 'pv', 'val'): 7796.8431880300122,
('b_el', 'input', 'wind', 'val'): 28009.549502999955,
('b_el', 'output', 'demand_el', 'val'): 132243.7904593189,
('b_el', 'output', 'excess', 'val'): 928.12139200000013,
('b_th', 'input', 'pp_chp', 'val'): 14881.810039999958,
('b_th', 'output', 'demand_th', 'val'): 14881.80983624002,
('coal', 'output', 'pp_coal', 'val'): 86469.394787298472,
('gas', 'output', 'pp_chp', 'val'): 37204.525720000034,
('gas', 'output', 'pp_gas', 'val'): 60824.751778000136,
('lignite', 'output', 'pp_lig', 'val'): 53820.634704001102,
('oil', 'output', 'pp_oil', 'val'): 8.1687949999999994}
('b_el', 'to_bus', 'pp_chp', 'val'): 11161.357450000065,
('b_el', 'to_bus', 'pp_coal', 'val'): 33723.047672110595,
('b_el', 'to_bus', 'pp_gas', 'val'): 30412.377779000046,
('b_el', 'to_bus', 'pp_lig', 'val'): 22066.451080999268,
('b_el', 'to_bus', 'pp_oil', 'val'): 2.2872599999999998,
('b_el', 'to_bus', 'pv', 'val'): 7796.8431880300122,
('b_el', 'to_bus', 'wind', 'val'): 28009.549502999955,
('b_el', 'from_bus', 'demand_el', 'val'): 132243.7904593189,
('b_el', 'from_bus', 'excess', 'val'): 928.12139200000013,
('b_th', 'to_bus', 'pp_chp', 'val'): 14881.810039999958,
('b_th', 'from_bus', 'demand_th', 'val'): 14881.80983624002,
('coal', 'from_bus', 'pp_coal', 'val'): 86469.394787298472,
('gas', 'from_bus', 'pp_chp', 'val'): 37204.525720000034,
('gas', 'from_bus', 'pp_gas', 'val'): 60824.751778000136,
('lignite', 'from_bus', 'pp_lig', 'val'): 53820.634704001102,
('oil', 'from_bus', 'pp_oil', 'val'): 8.1687949999999994}

check(test_results, least_costs_run, testdict['least_costs'], results)
# *********** end of simple least cost example *******************************
Expand Down
2 changes: 1 addition & 1 deletion examples/solph/simple_least_costs/simple_least_costs.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def plot_results(energysystem):
# create multiindex dataframe with result values
esplot = output.DataFramePlot(energy_system=energysystem)
# select input results of electrical bus (i.e. power delivered by plants)
esplot.slice_unstacked(bus_label="b_el", type="input",
esplot.slice_unstacked(bus_label="b_el", type="to_bus",
date_from='2012-01-01 00:00:00',
date_to='2012-01-07 00:00:00')
# set colorlist for esplot
Expand Down
6 changes: 3 additions & 3 deletions examples/solph/storage_optimization/storage_invest.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def get_result_dict(energysystem):
storage = energysystem.groups['storage']
myresults = outputlib.DataFramePlot(energy_system=energysystem)

pp_gas = myresults.slice_by(obj_label='pp_gas', type='input',
pp_gas = myresults.slice_by(obj_label='pp_gas', type='to_bus',
date_from='2012-01-01 00:00:00',
date_to='2012-12-31 23:00:00')

Expand Down Expand Up @@ -182,7 +182,7 @@ def create_plots(energysystem):

# Plotting the input flows of the electricity bus for January
myplot = outputlib.DataFramePlot(energy_system=energysystem)
myplot.slice_unstacked(bus_label="electricity", type="input",
myplot.slice_unstacked(bus_label="electricity", type="to_bus",
date_from="2012-01-01 00:00:00",
date_to="2012-01-31 00:00:00")
colorlist = myplot.color_from_dict(cdict)
Expand All @@ -193,7 +193,7 @@ def create_plots(energysystem):
myplot.set_datetime_ticks(date_format='%d-%m-%Y', tick_distance=24*7)

# Plotting the output flows of the electricity bus for January
myplot.slice_unstacked(bus_label="electricity", type="output")
myplot.slice_unstacked(bus_label="electricity", type="from_bus")
myplot.plot(title="Year 2016", colormap='Spectral', linewidth=2)
myplot.ax.legend(loc='upper right')
myplot.ax.set_ylabel('Power in MW')
Expand Down
51 changes: 22 additions & 29 deletions oemof/outputlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pandas as pd
try:
import matplotlib.pyplot as plt
except:
except ImportError:
logging.warning('Matplotlib does not work.')


Expand Down Expand Up @@ -41,34 +41,20 @@ class ResultsDataFrame(pd.DataFrame):
http://pandas.pydata.org/pandas-docs/stable/advanced.html
"""


# results[source][target]
# if source == target:
# if isinstance(source, 'Bus'):
# if isinstance(target.key(), str):
# # duals
# pass
# else:
# storage_level
# else:
# # component logic
# pass

def __init__(self, **kwargs):
# default values if not arguments are passed
es = kwargs.get('energy_system')

rows_list = []
for k, v in es.results.items():
if ('Bus' in str(k.__class__)):
if 'Bus' in str(k.__class__):
for kk, vv in v.items():
row = {}
row = dict()
row['bus_label'] = k.label
if k is kk:
row['type'] = 'other'
else:
row['type'] = 'output'
row['type'] = 'from_bus'
if k is kk:
row['obj_label'] = 'duals'
elif isinstance(kk, str):
Expand All @@ -82,9 +68,9 @@ def __init__(self, **kwargs):
if k in v.keys():
# self ref. components (results[component][component])
for kk, vv in v.items():
if(k is kk):
if k is kk:
# self ref. comp. (results[component][component])
row = {}
row = dict()
row['bus_label'] = list(k.outputs.keys())[0].label
row['type'] = 'other'
row['obj_label'] = k.label
Expand All @@ -93,19 +79,19 @@ def __init__(self, **kwargs):
rows_list.append(row)
else:
# bus inputs (only self ref. components)
row = {}
row = dict()
row['bus_label'] = list(k.outputs.keys())[0].label
row['type'] = 'input'
row['type'] = 'to_bus'
row['obj_label'] = k.label
row['datetime'] = es.time_idx
row['val'] = v.get(list(k.outputs.keys())[0])
rows_list.append(row)
else:
for kk, vv in v.items():
# bus inputs (results[component][bus])
row = {}
row = dict()
row['bus_label'] = kk.label
row['type'] = 'input'
row['type'] = 'to_bus'
row['obj_label'] = k.label
row['datetime'] = es.time_idx
row['val'] = vv
Expand Down Expand Up @@ -133,7 +119,7 @@ def slice_by(self, **kwargs):
Parameters
----------
bus_label : string
type : string (input/output/other)
type : string (to_bus/from_bus/other)
obj_label: string
date_from : string
Start date selection e.g. "2016-01-01 00:00:00". If not set, the
Expand Down Expand Up @@ -172,6 +158,8 @@ def slice_unstacked(self, unstacklevel='obj_label',
----------
unstacklevel : string (default: 'obj_label')
Level to unstack the subset of the DataFrame.
formatted : boolean
missing...
"""
subset = self.slice_by(**kwargs)
subset = subset.unstack(level=unstacklevel)
Expand Down Expand Up @@ -364,8 +352,8 @@ def plot(self, **kwargs):
self.ax = self.subset.plot(**kwargs)
return self

def io_plot(self, bus_label, cdict, line_kwa={}, lineorder=None, bar_kwa={},
barorder=None, **kwargs):
def io_plot(self, bus_label, cdict, line_kwa=None, lineorder=None,
bar_kwa=None, barorder=None, **kwargs):
r""" Plotting a combined bar and line plot to see the fitting of in-
and outcomming flows of a bus balance.
Expand Down Expand Up @@ -398,20 +386,25 @@ def io_plot(self, bus_label, cdict, line_kwa={}, lineorder=None, bar_kwa={},
"""
self.ax = kwargs.get('ax', self.ax)

if bar_kwa is None:
bar_kwa = dict()
if line_kwa is None:
line_kwa = dict()

if self.ax is None:
fig = plt.figure()
self.ax = fig.add_subplot(1, 1, 1)

# Create a bar plot for all input flows
self.slice_unstacked(bus_label=bus_label, type='input', **kwargs)
self.slice_unstacked(bus_label=bus_label, type='to_bus', **kwargs)
if barorder is not None:
self.rearrange_subset(barorder)
self.subset.plot(kind='bar', linewidth=0, stacked=True, width=1,
ax=self.ax, color=self.color_from_dict(cdict),
**bar_kwa)

# Create a line plot for all output flows
self.slice_unstacked(bus_label=bus_label, type='output', **kwargs)
self.slice_unstacked(bus_label=bus_label, type='from_bus', **kwargs)
if lineorder is not None:
self.rearrange_subset(lineorder)
# The following changes are made to have the bottom line on top layer
Expand Down

0 comments on commit 912b86a

Please sign in to comment.