Wildcards Produce a Key Error
Closed this issue · 7 comments
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']
@bechtt could you please provide an actual example?
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]))
I agree with @orso82 to provide the code that didn't work.
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'])
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
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.
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.