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

With this new structure, how do I read the attribute value itself? #579

Closed
denfc opened this issue Jul 29, 2024 · 9 comments · Fixed by #581
Closed

With this new structure, how do I read the attribute value itself? #579

denfc opened this issue Jul 29, 2024 · 9 comments · Fixed by #581

Comments

@denfc
Copy link

denfc commented Jul 29, 2024

My problem is not an "issue" as such, but my ignorance. Before the latest update, I loaded the attributes (JLD2.load_attributes(oneFile["Header"])), searched for the first element of the pair to get what I wanted and took its second element to obtain the value.

Now the pairs appear to be gone (or am I no longer looking in the right place?). I can get to the "ReadAttribute", but have not figured out how to get the value, e.g., of "Time" in "Header", out of it:

`
using JLD2
jfile = jldopen("xxx.hdf5")
h = JLD2.get_dataset(jfile, "Header")
h2 = h.attributes # OrderedCollections.OrderedDict{String, Any}
h2S = h2["Time"] # type is JLD2.ReadAttribute

`
Sorry to bother you.

-- dfc

@JonasIsensee
Copy link
Collaborator

JonasIsensee commented Jul 29, 2024

Hi, no worries. I somewhat intentionally have not yet documented the new api since I might still change it soonish.

You're very welcome to try it and give feedback. (Ask please feel free to ask as many questions as needed)

If you already have a Dataset object, you can use JLD2.attributes(dset)

Otherwise JLD2.load_attributes(g, datasetname) should still work. If it doesn't, I'll fix it. However, all of that API may potentially change in a future release. (Not in a way that breaks old data files, just possibly API functions)

@denfc
Copy link
Author

denfc commented Jul 29, 2024

Thanks for the rapid response!

I can use JLD2.load_attributes or JLD2.attributes obtain the array index, but I'll be reading (many) standard hdf5 files that I haven't written, and I'm nervous that something might change in them, and I won't have the value that I think I have. With the pairs I looped through, with a "findfirst", until I found what I wanted, and then I took its value.

I can always switch back to the HDF5 package in the interim, but I really like using JLD2.

Thanks again for the info. I work pretty much by myself, so I go down many wrong paths, and a little direction is always greatly appreciated.

-- Denis

@JonasIsensee
Copy link
Collaborator

JonasIsensee commented Jul 29, 2024

Findfirst should still be ok.

You got an OrderedDict. The entries are guaranteed to be in the correct order und iteration or e.g. (keys(dict) values(dict)

@denfc
Copy link
Author

denfc commented Jul 29, 2024

The values are "ReadAttribute". Can I get the actual number easily? For example,

`
julia> h2["Time"]
JLD2.ReadAttribute(:Time, JLD2.ReadDataspace(0xff, 0x00, 6000), JLD2.FloatingPointDatatype(0x11, 0x20, 0x3f, 0x00, 0x00000008, 0x0000, 0x0040, 0x34, 0x0b, 0x00, 0x34, 0x000003ff), 6000)

`

The actual number for "Time" is 0.33300950325867074.

@JonasIsensee
Copy link
Collaborator

Ah, damn.

For now, use:
JLD2.read_attr_data(f::JLDFile, attr::ReadAttribute)

I'll fix it, next time I'm at a computer...

@denfc
Copy link
Author

denfc commented Jul 29, 2024

Ahh! Great, thanks. I had searched through the "read" functions but either missed that one or applied it incorrectly. Thanks again for the fast assistance! I can now continue to confront my ignorance of the non-Julia issues for which I'm using Julia. Many, many thanks.

@JonasIsensee
Copy link
Collaborator

JonasIsensee commented Jul 30, 2024

Using the dev branch should make JLD2.attributes(dset::Dataset) and JLD2.load_attributes(::JLDFile, ::String) return an OrderedDict with keys and the actual values in the order they are found in the file.

@denfc
Copy link
Author

denfc commented Jul 30, 2024

Maybe not yet? Yesterday's fix still works, so I'm fine, but I get the same result now with the dev version and the non-dev version.

With the dev: [033835bb] JLD2 v0.4.51 ~/.julia/dev/JLD2

using JLD2
jfile = jldopen("xxx.hdf5")
h = JLD2.get_dataset(jfile, "Header")
oDict = JLD2.load_attributes(jfile, "Header")
o = JLD2.attributes(h)
println(o == oDict)
println(typeof(o))
close(jfile)

┌ Warning: This file was not written with JLD2. Some things may not work.
└ @ JLD2 ~/.julia/packages/JLD2/bfPAo/src/JLD2.jl:283
true
Vector{Any}

@denfc
Copy link
Author

denfc commented Aug 26, 2024 via email

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

Successfully merging a pull request may close this issue.

2 participants