Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyError in EnergySystem.flows() after dump/restore #742

Closed
jnnr opened this issue Jan 25, 2021 · 11 comments
Closed

KeyError in EnergySystem.flows() after dump/restore #742

jnnr opened this issue Jan 25, 2021 · 11 comments

Comments

@jnnr
Copy link
Member

jnnr commented Jan 25, 2021

Describe the bug
There is a KeyError in EnergySystem's flows method when called on a EnergySystem that has been dumped and restored: https://github.com/oemof/oemof.network/blob/96648132045837e58b008f114eae88ed66154007/src/oemof/network/energy_system.py#L176-L181

I found it when using oemof.solph.processing.parameter_as_dict. Getting the parameters of an EnergySystem is a useful step, e.g. in postprocessing.

The respective dictionary (see log below) contains a Bus with the proper name, but does not seem to totally fit. Maybe the EnergySystem is in an inconsistent state after deserialization? Any ideas @oemof-developer, @oemof/oemof-solph?

To Reproduce
Steps to reproduce the behavior:

  1. Go to e.g. the basic example https://github.com/oemof/oemof-examples/blob/master/oemof_examples/oemof.solph/v0.4.x/basic_example/basic_example.py
  2. insert this line before energysystem.dump() to see that it works
    energysystem.flows()
  3. insert the line after energysystem.restore() to see it fail
  4. Get an error traceback like this:

Traceback (most recent call last):
File "basic_example.py", line 226, in
solph.processing.parameter_as_dict(energysystem)
File "oemof-solph/src/oemof/solph/processing.py", line 378, in parameter_as_dict
flow_data = __separate_attrs(system, True, exclude_none)
File "oemof-solph/src/oemof/solph/processing.py", line 343, in __separate_attrs
components = system.flows() if get_flows else system.nodes
File ".virtualenvs/oemof-solph/lib/python3.6/site-packages/oemof/network/energy_system.py", line 179, in flows
for source in self.nodes
File ".virtualenvs/oemof-solph/lib/python3.6/site-packages/oemof/network/energy_system.py", line 180, in
for target in source.outputs
File ".virtualenvs/oemof-solph/lib/python3.6/collections/init.py", line 991, in getitem
raise KeyError(key)
KeyError: "<oemof.solph.network.Bus: 'natural_gas'>"

Expected behavior
The method should work after restoring. In particular, it should be possible to use parameter_as_dict after restoring.

Desktop (please complete the following information):

  • OS: Xubuntu
  • Python version 3.6.9
@jnnr jnnr changed the title Key error in processing.parameter_as_dict when passing restored EnergySystem KeyError in processing.parameter_as_dict when passing restored EnergySystem Jan 25, 2021
@henhuy
Copy link
Contributor

henhuy commented Jan 25, 2021

What do you use for dumping and restoring?

@jnnr
Copy link
Member Author

jnnr commented Jan 25, 2021

energysystem.dump() and energysystem.restore(), like in the basic example.

@henhuy
Copy link
Contributor

henhuy commented Jan 26, 2021

Scheint als würden die outputs nicht korrekt restored, da folgende Zeile den Fehler wirft:
https://github.com/oemof/oemof.network/blob/dev/src/oemof/network/energy_system.py#L178

Denke, dass hat nichts mit der Funktion parameter_as_dict zu tun, eher mit dem dump/restore process...?!

@jnnr
Copy link
Member Author

jnnr commented Jan 26, 2021

Scheint als würden die outputs nicht korrekt restored, da folgende Zeile den Fehler wirft:
https://github.com/oemof/oemof.network/blob/dev/src/oemof/network/energy_system.py#L178

Denke, dass hat nichts mit der Funktion parameter_as_dict zu tun, eher mit dem dump/restore process...?!

You are right, the KeyError is in the flows() method. So, it is actually a bug in the EnergySystem class of oemof.network. Does it make sense to open an issue there?

@henhuy
Copy link
Contributor

henhuy commented Jan 26, 2021

sorry for en/de mixup...
Maybe rename issue and assign to some related people?

@jnnr jnnr changed the title KeyError in processing.parameter_as_dict when passing restored EnergySystem KeyError in EnergySystem.flows() after dump/restore Jan 26, 2021
@sschlueters
Copy link

Hi there, I think I also stumbled upon this problem: oemof/oemof-network#10 (comment)

@henhuy
Copy link
Contributor

henhuy commented Jan 26, 2021

This answers your question @jnnr - we can simply close this issue...

Duplicate of oemof/oemof-network#10

@henhuy henhuy closed this as completed Jan 26, 2021
@jnnr
Copy link
Member Author

jnnr commented Jan 26, 2021

Ok, if you say so. I just updated the title. But it is the same problem.

@henhuy
Copy link
Contributor

henhuy commented Jan 26, 2021

Maybe you could ask if there is any progress?

@Bachibouzouk
Copy link
Contributor

Bachibouzouk commented Feb 1, 2021

According to __getitem__() from the collection module the exception is raised only if

if key in self.data:

is False

Apparently the dict used for oemof outputs inherits from collection.UserDict (so I guess source.outputs.data would display something worth looking into?). Interestingly networks.Outputs overrides __delitem__, __setitem__ and __init__ of collection.UserDict but not __getitem__. Maybe the solution is to look there?

Sorry I don't have time to look into it further right now, I hope this might help to lead to finding the bug :)

@jnnr
Copy link
Member Author

jnnr commented Feb 1, 2021

According to __getitem__() from the collection module the exception is raised only if

if key in self.data:

is False

Thanks for your feedback. That's right, the key in self.data gives False and causes the error. As I understand, it calls the __contains__ method of the dict, which also returns False. However, key in list(self.data) returns True.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants