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

Deprecate get_dims and set_dims! #798

Merged
merged 12 commits into from
Jan 13, 2021
Merged
28 changes: 12 additions & 16 deletions src/HDF5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,7 @@ end
Base.isempty(dspace::Union{Dataspace,Dataset,Attribute}) = length(dspace) == 0

"""
isnull(dspace::Union{Dataspace, Dataset, Attribute})
isnull(dspace::Union{HDF5.Dataspace, HDF5.Dataset, HDF5.Attribute})

Determines whether the given object has no size (consistent with the `H5S_NULL` dataspace).

Expand All @@ -1107,12 +1107,19 @@ function isnull(obj::Union{Dataspace,Dataset,Attribute})
return ret
end

function get_dims(dspace::Dataspace)
"""
HDF5.get_simple_extent_dims(obj::Union{HDF5.Dataspace, HDF5.Dataset,HDF5.Attribute}) -> dims, maxdims

Get the array dimensions from a dataset or attribute and return a tuple of dims and maxdims.
"""
function get_simple_extent_dims(obj::Union{Dataspace,Dataset,Attribute})
dspace = obj isa Dataspace ? checkvalid(obj) : dataspace(obj)
h5_dims, h5_maxdims = h5s_get_simple_extent_dims(dspace)
# reverse dimensions since hdf5 uses C-style order
N = length(h5_dims)
dims = ntuple(i -> @inbounds(Int(h5_dims[N-i+1])), N)
maxdims = ntuple(i -> @inbounds(h5_maxdims[N-i+1]) % Int, N) # allows max_dims to be specified as -1 without triggering an overflow
obj isa Dataspace || close(dspace)
return dims, maxdims
end

Expand All @@ -1123,25 +1130,14 @@ function get_regular_hyperslab(dspace::Dataspace)
return rev(start), rev(stride), rev(count), rev(block)
end

"""
HDF5.get_dims(obj::Union{HDF5.Dataset, HDF5.Attribute})

Get the array dimensions from a dataset or attribute and return a tuple of dims and maxdims.
"""
function get_dims(dset::Union{Dataset,Attribute})
dspace = dataspace(dset)
ret = get_dims(dspace)
close(dspace)
return ret
end

"""
set_dims!(dset::HDF5.Dataset, new_dims::Dims)
HDF5.set_extent!(dset::HDF5.Dataset, new_dims::Dims)

Change the current dimensions of a dataset to `new_dims`, limited by
`max_dims = get_dims(dset)[2]`. Reduction is possible and leads to loss of truncated data.
`max_dims = get_simple_extent_dims(dset)[2]`. Reduction is possible and leads to loss of truncated data.
"""
set_dims!(dset::Dataset, new_dims::Dims) = h5d_set_extent(checkvalid(dset), hsize_t[reverse(new_dims)...])
set_extent!(dset::Dataset, new_dims::Dims) = h5d_set_extent(checkvalid(dset), hsize_t[reverse(new_dims)...])
musm marked this conversation as resolved.
Show resolved Hide resolved

"""
start_swmr_write(h5::HDF5.File)
Expand Down
3 changes: 3 additions & 0 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ function exists end
### Changed in PR#776
@deprecate create_dataset(parent::Union{File,Group}, path::AbstractString, dtype::Datatype, dspace::Dataspace,
lcpl::Properties, dcpl::Properties, dapl::Properties, dxpl::Properties) HDF5.Dataset(HDF5.h5d_create(parent, path, dtype, dspace, lcpl, dcpl, dapl), HDF5.file(parent), dxpl) false

@deprecate get_dims(dspace::Union{Dataspace,Dataset,Attribute}) get_simple_extent_dims(dspace) false
musm marked this conversation as resolved.
Show resolved Hide resolved
@deprecate set_dims!(dspace::Union{Dataspace,Dataset,Attribute}) set_extent!(dset) false
2 changes: 1 addition & 1 deletion src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function Base.show(io::IO, dspace::Dataspace)
return
end
# otherwise type == H5S_SIMPLE
sz, maxsz = get_dims(dspace)
sz, maxsz = get_simple_extent_dims(dspace)
sel = h5s_get_select_type(dspace)
if sel == H5S_SEL_HYPERSLABS && h5s_is_regular_hyperslab(dspace)
start, stride, count, _ = get_regular_hyperslab(dspace)
Expand Down
14 changes: 7 additions & 7 deletions test/dataspace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ using Test
@test !HDF5.isnull(ds_zerosz)
@test !HDF5.isnull(ds_vector)

@test HDF5.get_dims(ds_null) === ((), ())
@test HDF5.get_dims(ds_scalar) === ((), ())
@test HDF5.get_dims(ds_zerosz) === ((0,), (0,))
@test HDF5.get_dims(ds_vector) === ((5,), (5,))
@test HDF5.get_dims(ds_matrix) === ((5, 7), (5, 7))
@test HDF5.get_dims(ds_maxdim) === ((5, 7), (20, 20))
@test HDF5.get_dims(ds_unlim) === ((1,), (-1,))
@test HDF5.get_simple_extent_dims(ds_null) === ((), ())
@test HDF5.get_simple_extent_dims(ds_scalar) === ((), ())
@test HDF5.get_simple_extent_dims(ds_zerosz) === ((0,), (0,))
@test HDF5.get_simple_extent_dims(ds_vector) === ((5,), (5,))
@test HDF5.get_simple_extent_dims(ds_matrix) === ((5, 7), (5, 7))
@test HDF5.get_simple_extent_dims(ds_maxdim) === ((5, 7), (20, 20))
@test HDF5.get_simple_extent_dims(ds_unlim) === ((1,), (-1,))

# Can create new copies
ds_tmp = copy(ds_maxdim)
Expand Down
30 changes: 15 additions & 15 deletions test/extend_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ fn = tempname()
fid = h5open(fn, "w")
g = create_group(fid, "shoe")
d = create_dataset(g, "foo", datatype(Float64), ((10, 20), (100, 200)), chunk=(1, 1))
#println("d is size current $(map(int,HDF5.get_dims(d)[1])) max $(map(int,HDF5.get_dims(d)[2]))")
dims, max_dims = HDF5.get_dims(d)
#println("d is size current $(map(int,HDF5.get_simple_extent_dims(d)[1])) max $(map(int,HDF5.get_simple_extent_dims(d)[2]))")
dims, max_dims = HDF5.get_simple_extent_dims(d)
@test dims == (UInt64(10), UInt64(20))
@test max_dims == (UInt64(100), UInt64(200))
HDF5.set_dims!(d, (100, 150))
dims, max_dims = HDF5.get_dims(d)
HDF5.set_extent!(d, (100, 150))
dims, max_dims = HDF5.get_simple_extent_dims(d)
@test dims == (UInt64(100), UInt64(150))
@test max_dims == (UInt64(100), UInt64(200))
d[1, 1:5] = [1.1231, 1.313, 5.123, 2.231, 4.1231]
HDF5.set_dims!(d, (1, 5))
HDF5.set_extent!(d, (1, 5))
@test size(d) == (1, 5)

# Indexing returns correct array dimensions
Expand All @@ -39,18 +39,18 @@ HDF5.set_dims!(d, (1, 5))
# Test Array constructor
Array(d) == [1.1231 1.313 5.123 2.231 4.1231]

#println("d is size current $(map(int,HDF5.get_dims(d)[1])) max $(map(int,HDF5.get_dims(d)[2]))")
#println("d is size current $(map(int,HDF5.get_simple_extent_dims(d)[1])) max $(map(int,HDF5.get_simple_extent_dims(d)[2]))")
b = create_dataset(fid, "b", Int, ((1000,), (-1,)), chunk=(100,)) #-1 is equivalent to typemax(hsize_t) as far as I can tell
#println("b is size current $(map(int,HDF5.get_dims(b)[1])) max $(map(int,HDF5.get_dims(b)[2]))")
#println("b is size current $(map(int,HDF5.get_simple_extent_dims(b)[1])) max $(map(int,HDF5.get_simple_extent_dims(b)[2]))")
b[1:200] = ones(200)
dims, max_dims = HDF5.get_dims(b)
dims, max_dims = HDF5.get_simple_extent_dims(b)
@test dims == (UInt64(1000),)
@test max_dims == (HDF5.H5S_UNLIMITED % Int,)
HDF5.set_dims!(b, (10000,))
dims, max_dims = HDF5.get_dims(b)
HDF5.set_extent!(b, (10000,))
dims, max_dims = HDF5.get_simple_extent_dims(b)
@test dims == (UInt64(10000),)
@test max_dims == (HDF5.H5S_UNLIMITED % Int,)
#println("b is size current $(map(int,HDF5.get_dims(b)[1])) max $(map(int,HDF5.get_dims(b)[2]))")
#println("b is size current $(map(int,HDF5.get_simple_extent_dims(b)[1])) max $(map(int,HDF5.get_simple_extent_dims(b)[2]))")
# b[:] = [1:10000] # gave error no method lastindex(HDF5.Dataset{PlainHDF5File},),
# so I defined lastindex(dset::HDF5.Dataset) = length(dset), and exported lastindex
# but that didn't fix the error, despite the lastindex function working
Expand All @@ -62,17 +62,17 @@ close(fid)

fid = h5open(fn, "r")
d_again = fid["shoe/foo"]
dims, max_dims = HDF5.get_dims(d_again)
dims, max_dims = HDF5.get_simple_extent_dims(d_again)
@test dims == (UInt64(1), UInt64(5))
@test max_dims == (UInt64(100), UInt64(200))
@test (sum(d_again[1, 1:5]) - sum([1.1231, 1.313, 5.123, 2.231, 4.1231])) == 0
#println("d is size current $(map(int,HDF5.get_dims(re_d)[1])) max $(map(int,HDF5.get_dims(re_d)[2]))")
#println("d is size current $(map(int,HDF5.get_simple_extent_dims(re_d)[1])) max $(map(int,HDF5.get_simple_extent_dims(re_d)[2]))")
@test fid["b"][1:10000] == [1:10000;]
b_again = fid["b"]
dims, max_dims = HDF5.get_dims(b_again)
dims, max_dims = HDF5.get_simple_extent_dims(b_again)
@test dims == (UInt64(10000),)
@test max_dims == (HDF5.H5S_UNLIMITED % Int,)
#println("b is size current $(map(int,HDF5.get_dims(b)[1])) max $(map(int,HDF5.get_dims(b)[2]))")
#println("b is size current $(map(int,HDF5.get_simple_extent_dims(b)[1])) max $(map(int,HDF5.get_simple_extent_dims(b)[2]))")

close(fid)
rm(fn)
Expand Down
2 changes: 1 addition & 1 deletion test/swmr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ end
function dataset_write(d, ch_written, ch_read)
for i = 1:10
@assert take!(ch_read) == true
HDF5.set_dims!(d, (i*10,))
HDF5.set_extent!(d, (i*10,))
inds::UnitRange{Int} = (1:10) .+ (i - 1) * 10
d[inds] = inds
flush(d) # flush the dataset
Expand Down