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

Attempt to implement dimension_separator #150

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
13 changes: 7 additions & 6 deletions src/Storage/Storage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,18 @@ function subkeys end
Deletes the given key from the store.
"""

citostring(i::CartesianIndex) = join(reverse((i - oneunit(i)).I), '.')
citostring(::CartesianIndex{0}) = "0"
citostring(i::CartesianIndex, sep::Char='.') = join(reverse((i - oneunit(i)).I), sep)
citostring(::CartesianIndex{0}, _::Char) = "0"
citostring(i::CartesianIndex, s::AbstractStore, p) = citostring(i, only(getmetadata(s, p, true).dimension_separator))
_concatpath(p,s) = isempty(p) ? s : rstrip(p,'/') * '/' * s

Base.getindex(s::AbstractStore, p, i::CartesianIndex) = s[p, citostring(i)]
Base.getindex(s::AbstractStore, p, i::CartesianIndex) = s[p, citostring(i, s, p)]
Base.getindex(s::AbstractStore, p, i) = s[_concatpath(p,i)]
Base.delete!(s::AbstractStore, p, i::CartesianIndex) = delete!(s, p, citostring(i))
Base.delete!(s::AbstractStore, p, i::CartesianIndex) = delete!(s, p, citostring(i, s, p))
Base.delete!(s::AbstractStore, p, i) = delete!(s, _concatpath(p,i))
Base.haskey(s::AbstractStore, k) = isinitialized(s,k)
Base.setindex!(s::AbstractStore,v,p,i) = setindex!(s,v,_concatpath(p,i))
Base.setindex!(s::AbstractStore,v,p,i::CartesianIndex) = s[p, citostring(i)]=v
Base.setindex!(s::AbstractStore,v,p,i::CartesianIndex) = s[p, citostring(i, s, p)]=v


maybecopy(x) = copy(x)
Expand All @@ -82,7 +83,7 @@ end
is_zgroup(s::AbstractStore, p) = isinitialized(s,_concatpath(p,".zgroup"))
is_zarray(s::AbstractStore, p) = isinitialized(s,_concatpath(p,".zarray"))

isinitialized(s::AbstractStore, p, i::CartesianIndex)=isinitialized(s,p,citostring(i))
isinitialized(s::AbstractStore, p, i::CartesianIndex)=isinitialized(s, p, citostring(i, s, p))
isinitialized(s::AbstractStore, p, i) = isinitialized(s,_concatpath(p,i))
isinitialized(s::AbstractStore, i) = s[i] !== nothing

Expand Down
1 change: 1 addition & 0 deletions src/Storage/s3store.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function Base.getindex(s::S3Store, i::String)
return s3_get(s.aws,s.bucket,i,raw=true,retry=false)
catch e
if e isa AWSS3.AWS.AWSException && e.code == "NoSuchKey"
@info "getindex(::S3Store, $i)" s.aws s.bucket i
return nothing
else
throw(e)
Expand Down
17 changes: 16 additions & 1 deletion src/ZArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,24 @@ function zcreate(::Type{T},storage::AbstractStore,
fill_value=nothing,
fill_as_missing=false,
compressor=BloscCompressor(),
filters = filterfromtype(T),
filters = filterfromtype(T),
dimension_separator='.',
attrs=Dict(),
writeable=true,
) where T

if compressor isa AbstractString
if haskey(compressortypes, String(compressor))
compressor = compressortypes[compressor]()
else
throw(UnknownCompressorException(compressor))
end
end

if dimension_separator isa AbstractString
# Convert AbstractString to Char
dimension_separator = only(dimension_separator)
end

length(dims) == length(chunks) || throw(DimensionMismatch("Dims must have the same length as chunks"))
N = length(dims)
Expand All @@ -347,6 +361,7 @@ function zcreate(::Type{T},storage::AbstractStore,
fill_value,
'C',
filters,
dimension_separator,
)

isemptysub(storage,path) || error("$storage $path is not empty")
Expand Down
16 changes: 11 additions & 5 deletions src/metadata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,15 @@ struct Metadata{T, N, C, F}
fill_value::Union{T, Nothing}
order::Char
filters::F # not yet supported
function Metadata{T2, N, C, F}(zarr_format, shape, chunks, dtype, compressor,fill_value, order, filters) where {T2,N,C,F}
dimension_separator::Char
function Metadata{T2, N, C, F}(zarr_format, shape, chunks, dtype, compressor,fill_value, order, filters, dimension_separator) where {T2,N,C,F}
#We currently only support version
zarr_format == 2 || throw(ArgumentError("Zarr.jl currently only support v2 of the protocol"))
#Do some sanity checks to make sure we have a sane array
any(<(0), shape) && throw(ArgumentError("Size must be positive"))
any(<(1), chunks) && throw(ArgumentError("Chunk size must be >= 1 along each dimension"))
order === 'C' || throw(ArgumentError("Currently only 'C' storage order is supported"))
new{T2, N, C, F}(zarr_format, Base.RefValue{NTuple{N,Int}}(shape), chunks, dtype, compressor,fill_value, order, filters)
new{T2, N, C, F}(zarr_format, Base.RefValue{NTuple{N,Int}}(shape), chunks, dtype, compressor,fill_value, order, filters, dimension_separator)
end
end

Expand All @@ -152,7 +153,8 @@ function ==(m1::Metadata, m2::Metadata)
m1.compressor == m2.compressor &&
m1.fill_value == m2.fill_value &&
m1.order == m2.order &&
m1.filters == m2.filters
m1.filters == m2.filters &&
m1.dimension_separator == m2.dimension_separator
end


Expand All @@ -163,6 +165,7 @@ function Metadata(A::AbstractArray{T, N}, chunks::NTuple{N, Int};
fill_value::Union{T, Nothing}=nothing,
order::Char='C',
filters::Nothing=nothing,
dimension_separator::Char='.',
fill_as_missing = false,
) where {T, N, C}
T2 = (fill_value === nothing || !fill_as_missing) ? T : Union{T,Missing}
Expand All @@ -174,7 +177,8 @@ function Metadata(A::AbstractArray{T, N}, chunks::NTuple{N, Int};
compressor,
fill_value,
order,
filters
filters,
dimension_separator,
)
end

Expand Down Expand Up @@ -207,6 +211,7 @@ function Metadata(d::AbstractDict, fill_as_missing)
fv,
first(d["order"]),
filters,
only(get(d, "dimension_separator", '.')),
)
end

Expand All @@ -220,7 +225,8 @@ function JSON.lower(md::Metadata)
"compressor" => md.compressor,
"fill_value" => fill_value_encoding(md.fill_value),
"order" => md.order,
"filters" => md.filters
"filters" => md.filters,
"dimension_separator" => md.dimension_separator,
)
end

Expand Down
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ end
"zarr_format" => 2,
"chunks" => [3, 2],
"fill_value" => nothing,
"compressor" => nothing)
"compressor" => nothing,
"dimension_separator" => ".",
)
# call gc to avoid unlink: operation not permitted (EPERM) on Windows
# might be because files are left open
# from https://github.com/JuliaLang/julia/blob/f6344d32d3ebb307e2b54a77e042559f42d2ebf6/stdlib/SharedArrays/test/runtests.jl#L146
Expand Down
Loading