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

Tuple support #1060

Merged
merged 6 commits into from
May 18, 2023
Merged

Tuple support #1060

merged 6 commits into from
May 18, 2023

Conversation

lxvm
Copy link
Contributor

@lxvm lxvm commented Mar 25, 2023

Since there is already support for reading/writing compound types, I wanted to try adding support for Tuples as well, since sometimes saving tuple data is convenient, or compound types have tuple data (e.g. SArray). This pr writes tuples to compound types with member names corresponding to the indices of each element, i.e. var"1", var"2", var"3",.... I understand that there is no native HDF5 type corresponding to a tuple, however this pr is non-breaking and since tuples will be read back as named tuples, which inherit the majority of behavior from tuples, this only provides additional features. Please advise.

I also made the following changes:

  • allow dset[i] = e for e a compound type by loosening method signatures
  • add a method for datatype(::Type{T}) so that create_dataset(parent, path, T, dataspace) works for compound types T

I also prepared a follow-up pr showing that we can save StaticArrays as compound types (https://github.com/lxvm/HDF5.jl/tree/pr_sarray), although one method would change in a breaking way.

@@ -64,14 +64,15 @@ end

# These will use finalizers. Close them eagerly to avoid issues.
datatype(::T) where {T} = Datatype(hdf5_type_id(T), true)
datatype(T::Type) = Datatype(hdf5_type_id(T), true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we actually want this.

Suggested change
datatype(T::Type) = Datatype(hdf5_type_id(T), true)
datatype(::Type{T}) where T = Datatype(hdf5_type_id(T), true)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree that your change fixes several issues

datatype(x::AbstractArray{T}) where {T} = Datatype(hdf5_type_id(T), true)

hdf5_type_id(::Type{T}) where {T} = hdf5_type_id(T, Val(isstructtype(T)))
function hdf5_type_id(::Type{T}, isstruct::Val{true}) where {T}
dtype = API.h5t_create(API.H5T_COMPOUND, sizeof(T))
for (idx, fn) in enumerate(fieldnames(T))
ftype = fieldtype(T, idx)
API.h5t_insert(dtype, fn, fieldoffset(T, idx), hdf5_type_id(ftype))
API.h5t_insert(dtype, Symbol(fn), fieldoffset(T, idx), hdf5_type_id(ftype))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this change do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the line that adds support for tuples to be saved as hdf5 compound data types. The field names of tuples are integers whereas other structs have field names that are symbols, which is why this conversion is necessary.

@mkitti mkitti added this to the 0.16.5 milestone Apr 27, 2023
@mkitti
Copy link
Member

mkitti commented Apr 28, 2023

I'm going to try to get this formatted and merged soon to do a release.

@mkitti mkitti merged commit e369d1f into JuliaIO:master May 18, 2023
15 checks passed
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 this pull request may close these issues.

None yet

2 participants