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

Dump and restore not working properly / strange KeyError #10

Open
sschlueters opened this issue Nov 13, 2020 · 6 comments
Open

Dump and restore not working properly / strange KeyError #10

sschlueters opened this issue Nov 13, 2020 · 6 comments
Assignees
Labels
bug Something isn't working

Comments

@sschlueters
Copy link

I stumbled upon a strange error. The EnergySystem.flows method doesn't work, when the energy system was dumped and restored. Here is a minimal example:

from oemof.solph import EnergySystem, Bus, Flow, Source

es_orig = EnergySystem()

bus = Bus(label="electricity")
source = Source(label="grid", outputs={bus: Flow(variable_costs=1)})

es_orig.add(bus, source)

print(es_orig.flows())

print("Now we dump and restore...")
es_orig.dump(dpath='.', filename='test.oemof')

es_restored = EnergySystem()
es_restored.restore(dpath='.', filename='test.oemof')

print(es_restored.flows())

I get as an output:

{("<oemof.solph.network.Source: 'grid'>", "<oemof.solph.network.Bus: 'electricity'>"): '<oemof.solph.network.Flow: EdgeLabel(input="<oemof.solph.network.Source: \'grid\'>", output="<oemof.solph.network.Bus: \'electricity\'>")>'}
Now we dump and restore...
Traceback (most recent call last):
  File ".\debug.py", line 25, in <module>
    print(es_restored.flows())
  File "D:\bin\anaconda3\envs\oemof\lib\site-packages\oemof\network\energy_system.py", line 179, in flows
    for source in self.nodes
  File "D:\bin\anaconda3\envs\oemof\lib\site-packages\oemof\network\energy_system.py", line 180, in <dictcomp>
    for target in source.outputs
  File "D:\bin\anaconda3\envs\oemof\lib\collections\__init__.py", line 1027, in __getitem__
    raise KeyError(key)
KeyError: "<oemof.solph.network.Bus: 'electricity'>"

Even

grid_node, = [node for node in es_restored.nodes if node.label == 'grid']
key, = [k for k in grid_node.outputs.keys()]
print(grid_node.outputs[key])

fails after dumping and restoring the energy system.

@sschlueters
Copy link
Author

sschlueters commented Jan 26, 2021

I don't know how to solve this issue, but @jnnr seemed to have similar intentions while discovering this bug: analysis of the oemof results. In the end I worked my way around this problem and wrote some functions which work for me. Maybe they are useful for others: https://gist.github.com/sschlueters/4af0f5f035ea3f38f6050e818aba776c

Edit: The functions expect the results as given by results = oemof.solph.processing.results(model)

@jnnr
Copy link
Member

jnnr commented Jan 26, 2021

I could fix the KeyError by replacing

(source, target): source.outputs[target]

with

(source, target): source.outputs.get(target)

in the flows() method, which is said to be a usual solution in case of KeyErrors. Still not 100% sure why this works.

@jnnr
Copy link
Member

jnnr commented Jan 26, 2021

I could fix the KeyError by replacing

Ok, that was trivial - too quick. .get returns None if the key is not found, so this is not really a fix.

@jnnr
Copy link
Member

jnnr commented Jan 26, 2021

The funny thing is that the key seems to be present. I checked and found that the hash is correct.

@jnnr
Copy link
Member

jnnr commented Feb 11, 2021

I made some further observations in oemof/oemof-solph#742 (comment), but I am stuck again. Any ideas, @oemof/oemof-core?

@p-snft
Copy link
Member

p-snft commented Dec 19, 2023

I found a related issue. I think it's because Nodes have a hash function that is depending on the label. The label, however, seems always set (in time) when restoring a model.

https://github.com/oemof/oemof-network/pull/43/files#diff-7cfcaf50c32fb4054e7474030d8097d8987c25da20fcc93b00c32c591a087ac4R81

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants