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

Allow create_dataset to take a Type and Dataspace, Fix #1084 #1086

Merged
merged 4 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/datasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ create_dataset(
dspace_dims::Int...;
pv...
) = create_dataset(checkvalid(parent), path, datatype(dtype), dataspace(dspace_dims); pv...)
create_dataset(
parent::Union{File,Group},
path::Union{AbstractString,Nothing},
dtype::Type,
dspace::Dataspace;
pv...
) = create_dataset(checkvalid(parent), path, datatype(dtype), dspace; pv...)

# Get the datatype of a dataset
datatype(dset::Dataset) = Datatype(API.h5d_get_type(checkvalid(dset)), file(dset))
Expand Down
4 changes: 4 additions & 0 deletions src/dataspaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,17 @@ dataspace(n::Nothing) = Dataspace(API.h5s_create(API.H5S_NULL))
# for giving sizes explicitly
"""
dataspace(dims::Tuple; max_dims::Tuple=dims)
dataspace(dims::Tuple, max_dims::Tuple)

Construct a simple `Dataspace` for the given dimensions `dims`. The maximum
dimensions `maxdims` specifies the maximum possible size: `-1` can be used to
indicate unlimited dimensions.
"""
dataspace(sz::Dims{N}; max_dims::Union{Dims{N},Tuple{}}=()) where {N} =
_dataspace(sz, max_dims)
dataspace(sz::Dims{N}, max_dims::Union{Dims{N},Tuple{}}) where {N} =
_dataspace(sz, max_dims)
dataspace(dims::Tuple{Dims{N},Dims{N}}) where {N} = _dataspace(first(dims), last(dims))
Copy link
Member

Choose a reason for hiding this comment

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

should we document these additional methods? These were added for additional flexibility?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think that we just implemented what wa already documented per #1084, but yes the documentation should reflect the new methods.

Copy link
Member

Choose a reason for hiding this comment

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

I see the fix for adding the methods for create_dataset as per documentation, I was just curious on the new dataspace methods.

dataspace(sz1::Int, sz2::Int, sz3::Int...; max_dims::Union{Dims,Tuple{}}=()) =
_dataspace(tuple(sz1, sz2, sz3...), max_dims)

Expand Down
61 changes: 61 additions & 0 deletions test/create_dataset.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using HDF5
using Test

"""
Test Set create_dataset

Test the combination of arguments to create_dataset.
"""

@testset "create_dataset" begin
mktemp() do fn, io
h5open(fn, "w") do h5f
h5g = create_group(h5f, "test_group")

# Arguments
# Test file and group
parents = (h5f, h5g)
# Test anonymous dattaset, String, and SubString
names = (nothing, "test_dataset", @view("test_dataset"[1:4]))
# Test primitive, HDF5.Datatype, non-primitive, non-primitive HDF5.Datatype
types = (UInt8, datatype(UInt8), Complex{Float32}, datatype(Complex{Float32}))
# Test Tuple, HDF5.Dataspace, two tuples (extendible), extendible HDF5.Dataspace
spaces = (
(3, 4),
dataspace((16, 16)),
((4, 4), (8, 8)),
dataspace((16, 16); max_dims=(32, 32))
)
# TODO: test keywords

# Create argument cross product
p = Iterators.product(parents, names, types, spaces)

for (parent, name, type, space) in p
try
# create a chunked dataset since contiguous datasets are not extendible
ds = create_dataset(parent, name, type, space; chunk=(2, 2))
@test datatype(ds) == datatype(type)
@test dataspace(ds) == dataspace(space)
@test isvalid(ds)
close(ds)
if !isnothing(name)
# if it is not an anonymous dataset, try to open it
ds2 = open_dataset(parent, name)
@test isvalid(ds2)
close(ds2)
delete_object(parent, name)
end
catch err
throw(ArgumentError("""
Error occured with (
$parent :: $(typeof(parent)),
$name :: $(typeof(name)),
$type :: $(typeof(type)),
$space :: $(typeof(space)))
"""))
end
end
end
end
end
4 changes: 4 additions & 0 deletions test/dataspace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ using Test
@test dataspace((5,)) == ds_vector
@test dataspace((5, 7)) == ds_matrix != ds_maxdim
@test dataspace((5, 7); max_dims=(20, 20)) == ds_maxdim != ds_matrix
@test dataspace((5, 7), (20, 20)) == ds_maxdim
@test dataspace(((5, 7), (20, 20))) == ds_maxdim
@test dataspace((1,); max_dims=(-1,)) == ds_unlim
@test dataspace((1,), (-1,)) == ds_unlim
@test dataspace(((1,), (-1,))) == ds_unlim
# for ≥ 2 numbers, same as single tuple argument
@test dataspace(5, 7) == ds_matrix
@test dataspace(5, 7, 1) == dataspace((5, 7, 1))
Expand Down
15 changes: 11 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ using HDF5
using Test
using Pkg
filter_path = joinpath(dirname(pathof(HDF5)), "..", "filters")
Pkg.develop(PackageSpec(; path=joinpath(filter_path, "H5Zblosc")))
Pkg.develop(PackageSpec(; path=joinpath(filter_path, "H5Zbzip2")))
Pkg.develop(PackageSpec(; path=joinpath(filter_path, "H5Zlz4")))
Pkg.develop(PackageSpec(; path=joinpath(filter_path, "H5Zzstd")))
if !Base.BinaryPlatforms.CPUID.test_cpu_feature(Base.BinaryPlatforms.CPUID.JL_X86_avx2)
Pkg.add(PackageSpec(; name="Blosc_jll", version=v"1.21.2+0"))
end
Pkg.develop([
PackageSpec(; path=joinpath(filter_path, "H5Zblosc")),
PackageSpec(; path=joinpath(filter_path, "H5Zbzip2")),
PackageSpec(; path=joinpath(filter_path, "H5Zlz4")),
PackageSpec(; path=joinpath(filter_path, "H5Zzstd")),
])
@static if VERSION >= v"1.6"
Pkg.develop(PackageSpec(; path=joinpath(filter_path, "H5Zbitshuffle")))
end
Expand All @@ -29,6 +34,8 @@ end
@testset "HDF5.jl" begin
@debug "plain"
include("plain.jl")
@debug "create_dataset"
include("create_dataset.jl")
@debug "strings"
include("strings.jl")
@debug "api"
Expand Down