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

add support for CartesianRange indexing #442

Closed
wants to merge 4 commits into from
Closed
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
21 changes: 21 additions & 0 deletions src/HDF5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,12 @@ function h5read(filename, name::String, indices::Tuple{Vararg{Union{Range{Int},I
dat
end

function h5read{N}(filename, name::String, cr::CartesianRange{CartesianIndex{N}})
# transfer to unit range
ur = map((x,y)->x:y, cr.start.I, cr.stop.I)
Copy link
Member

Choose a reason for hiding this comment

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

Better to use first(cr) and last(cr) than cr.start.I and cr.stop.I; in 0.7, the representation of CartesianRange has changed. Indeed, in 0.7 this can just be ur = cr.indices.

h5read(filename, name, ur)
end

function h5writeattr(filename, name::String, data::Dict)
fid = h5open(filename, "r+")
try
Expand Down Expand Up @@ -845,6 +851,12 @@ root(obj::Union{HDF5Group, HDF5Dataset}) = g_open(file(obj), "/")
getindex(parent::Union{HDF5File, HDF5Group}, path::String) = o_open(parent, path)
getindex(dset::HDF5Dataset, name::String) = a_open(dset, name)
getindex(x::HDF5Attributes, name::String) = a_open(x.parent, name)
# getindex for cartesian range
function getindex{N}(dset::HDF5Dataset, cr::CartesianRange{CartesianIndex{N}})
# transfer to unit range
ur = map((x,y)->x:y, cr.start.I, cr.stop.I)
dset[ur...]
end

# Path manipulation
function joinpathh5(a::String, b::String)
Expand Down Expand Up @@ -958,6 +970,15 @@ function setindex!(p::HDF5Properties, val, name::String)
funcset(p, val...)
return p
end

# cartesian range indexing
function setindex!{N}(dset::HDF5Dataset, val, cr::CartesianRange{CartesianIndex{N}})
# transfer to unit range
ur = map((x,y)->x:y, cr.start.I, cr.stop.I)
@show ur
Copy link
Member

Choose a reason for hiding this comment

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

why @show ur here ?

dset[ur...] = val
end

# Create a dataset with properties: obj[path, prop1, set1, ...] = val
function setindex!(parent::Union{HDF5File, HDF5Group}, val, path::String, prop1::String, val1, pv...)
if !iseven(length(pv))
Expand Down
20 changes: 20 additions & 0 deletions test/cartesian_range.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using HDF5
using Base.Test
using Compat

@testset "cartesian_range" begin

filename = "$(tempname()).h5"

h5write(filename, "main", Array(1:10))
@test h5read(filename, "main", CartesianRange((3:5,))) == [3:5;]
f = h5open(filename,"r+")
dset = f["main"]
# setindex of cartesian range
dset[CartesianRange((3:5,))] = [4:6;]
@test dset[CartesianRange((3:5,))] == [4:6;]
Copy link
Contributor

Choose a reason for hiding this comment

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

The tests should cover a case with more than one dimension. Also, why is the correct answer not [3:5;] as you would get with a normal range?

Copy link
Member

Choose a reason for hiding this comment

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

because on line 14 he rewrites elements 3:5 to 4:6 (which is testing setindex!)


close(f)
rm(filename)

end # end of testset
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using HDF5
using Base.Test

include("plain.jl")
include("cartesian_range.jl")
include("readremote.jl")
include("extend_test.jl")
include("gc.jl")
Expand Down