gafusion / omas

Ordered Multidimensional Array Structure
http://gafusion.github.io/omas
MIT License
30 stars 14 forks source link

Wildcards Produce a Key Error #203

Closed torrinba closed 2 years ago

torrinba commented 2 years ago

The use of : as a wild card for indices in an ods result in a KeyError. This should not be expected according to @smithsp.

Example: ods[':.container.data']

orso82 commented 2 years ago

@bechtt could you please provide an actual example?

smithsp commented 2 years ago

There is an example piece of code that should work

# access data across an array of structures via data slicing
data = ods['equilibrium.time_slice.:.time']
assert numpy.all(data == numpy.array([1000.0, 2000.0, 3000.0, 4000.0, 5000.0]))

from https://gafusion.github.io/omas/auto_examples/showcase_paths.html#sphx-glr-auto-examples-showcase-paths-py .

I agree with @orso82 to provide the code that didn't work.

torrinba commented 2 years ago

After further testing I determined that the KeyError only appears when the wildcard is multiple levels deep in the ods hierarchy. So there is no error in the example @smithsp provided, but there is one with this:

import numpy
from omas import *

ods = ODS()
with omas_environment(ods, dynamic_path_creation=True):
    ods['equilibrium.code.parameters.time_slice.0.in1.itime'] = 1000.0
    ods['equilibrium.code.parameters.time_slice.1.in1.itime'] = 2000.0
    ods['equilibrium.code.parameters.time_slice.2.in1.itime'] = 3000.0
    ods['equilibrium.code.parameters.time_slice.3.in1.itime'] = 4000.0

print(ods['equilibrium.code.parameters.time_slice.:.in1.itime'])
orso82 commented 2 years ago

The issue has likely to do that you are working with code parameters, which is a Python class of its own. It's not an ODS, and as such it does not have all of the capabilities that ODSs have.

In the IMAS standard, code parameters are stored as XML (*blah*), and OMAS makes things nicer by allowing you to manipulate those parameters as if they were nested structures in the ODS. All of the nested structures are represented as dictionaries and not list of dictionaries, which is probably also why the : wildcard does not work.

Here is an example using code parameters in OMAS: https://gafusion.github.io/omas/auto_examples/parse_codeparameters.html#sphx-glr-auto-examples-parse-codeparameters-py

torrinba commented 2 years ago

Thanks for the explanation @orso82! That sounds like a better explanation of what is happening. Is there any plans for adding ODS capabilities like this to the code parameters class? I've been able to make due with loops instead of wildcards, but it isn't as clean.

orso82 commented 2 years ago

It's a bit tricky, since code parameters are setup to be structs-of-structs (ie. dicts-of-dicts) and not arrays-of-structs (ie. list of dicts). This is not impossible, but it would take some work. For the time being, I'd suggest keep working with loops.

smithsp commented 2 years ago

@bechtt Thanks for finding a problem case. Sorry it didn't work out. @orso82 Thanks for the clarification.