Skip to content

Commit

Permalink
test similar kw
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaqz committed Nov 4, 2024
1 parent c7ecc89 commit 78bf7b9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
48 changes: 27 additions & 21 deletions src/array/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Base.Array{T}(x::UndefInitializer, dims::DimTuple; kw...) where T = Array{T}(x,
function Base.NamedTuple(A1::AbstractDimArray, As::AbstractDimArray...)
arrays = (A1, As...)
keys = map(Symbol name, arrays)
NamedTuple{keys}(arrays)
return NamedTuple{keys}(arrays)
end

# undef constructor for all AbstractDimArray
Expand All @@ -141,26 +141,30 @@ Base.read(A::AbstractDimArray) = A

# Need to cover a few type signatures to avoid ambiguity with base
function Base.similar(A::AbstractDimArray;
dims=dims(A), refdims=refdims(A), name=_noname(A), metadata=metadata(A)
data=similar(parent(A)),
dims=dims(A), refdims=refdims(A), name=_noname(A), metadata=NoMetadata(), kw...
)
rebuild(A; data=similar(parent(A)), dims, refdims, name, metadata)
rebuild(A; dims=format(dims, data), refdims, name, metadata, kw...)
end
function Base.similar(A::AbstractDimArray, ::Type{T};
dims=dims(A), refdims=refdims(A), name=_noname(A), metadata=metadata(A)
data=similar(parent(A), T),
dims=dims(A), refdims=refdims(A), name=_noname(A), metadata=NoMetadata(), kw...
) where T
rebuild(A; data=similar(parent(A), T), dims, refdims, name, metadata)
rebuild(A; data, dims=format(dims, data), refdims, name, metadata, kw...)
end

# We avoid calling `parent` for AbstractBasicDimArray as we don't know what it is/if there is one
function Base.similar(A::AbstractBasicDimArray{T,N};
dims=dims(A), refdims=refdims(A), name=_noname(A), metadata=metadata(A)
data=similar(Array{T,N}, size(A)),
dims=dims(A), refdims=refdims(A), name=_noname(A), metadata=NoMetadata(), kw...
) where {T,N}
rebuild(A; data=similar(Array{T,N}, size(A)), dims, refdims, name, metadata)
dimconstructor(dims)(data, dims; refdims, name, metadata, kw...)
end
function Base.similar(A::AbstractBasicDimArray{<:Any,N}, ::Type{T};
dims=dims(A), refdims=refdims(A), name=_noname(A), metadata=metadata(A)
data=similar(Array{T,N}, size(A)),
dims=dims(A), refdims=refdims(A), name=_noname(A), metadata=NoMetadata(), kw...
) where {T,N}
rebuild(A; data=similar(Array{T,N}, size(A)), dims, refdims, name, metadata)
dimconstructor(dims)(data, dims; refdims, name, metadata, kw...)
end
# We can't resize the dims or add missing dims, so return the unwraped Array type?
# An alternative would be to fill missing dims with `Anon`, and keep existing
Expand Down Expand Up @@ -241,31 +245,30 @@ end
function _similar(A::AbstractArray, T::Type, shape::Tuple; kw...)
data = similar(parent(A), T, map(_parent_range, shape))
shape isa Tuple{Vararg{Dimensions.DimUnitRange}} || return data
C = dimconstructor(dims(shape); kw...)
return C(data, dims(shape))
C = dimconstructor(dims(shape))
return C(data, dims(shape); kw...)
end
function _similar(::Type{T}, shape::Tuple; kw...) where {T<:AbstractArray}
data = similar(T, map(_parent_range, shape))
shape isa Tuple{Vararg{Dimensions.DimUnitRange}} || return data
C = dimconstructor(dims(shape); kw...)
return C(data, dims(shape))
C = dimconstructor(dims(shape))
return C(data, dims(shape); kw...)
end
_parent_range(r::Dimensions.DimUnitRange) = parent(r)
_parent_range(r) = r

# With Dimensions we can return an `AbstractDimArray`
Base.similar(A::AbstractBasicDimArray, D::DimTuple; kw...) = Base.similar(A, eltype(A), D; kw...)
Base.similar(A::AbstractBasicDimArray, D::Dimension...; kw...) = Base.similar(A, eltype(A), D; kw...)
fuBase.similar(A::AbstractBasicDimArray, ::Type{T}, D::Dimension...; kw...) where T =
Base.similar(A::AbstractBasicDimArray, ::Type{T}, D::Dimension...; kw...) where T =
Base.similar(A, T, D; kw...)
function Base.similar(A::AbstractDimArray, ::Type{T}, D::DimTuple;
refdims=(), name=_noname(A), metadata=NoMetadata()
refdims=(), name=_noname(A), metadata=NoMetadata(), kw...
) where T
rebuild(A; data=similar(parent(A), T, size(dims)), dims=D, refdims, metadata, name)
rebuild(A; data=similar(parent(A), T, size(dims)), dims=D, refdims, metadata, name, kw...)
end
function Base.similar(A::AbstractDimArray, ::Type{T}, D::Tuple{}
refdims=(), name=_noname(A), metadata==NoMetadata()
function Base.similar(A::AbstractDimArray, ::Type{T}, D::Tuple{};
refdims=(), name=_noname(A), metadata=NoMetadata(), kw...
) where T
rebuild(A; data=similar(parent(A), T, ()), dims=D, refdims, metadata, name)
rebuild(A; data=similar(parent(A), T, ()), dims=D, refdims, metadata, name, kw...)
end

# Keep the same type in `similar`
Expand All @@ -275,6 +278,9 @@ _noname(::NoName) = NoName()
_noname(::Symbol) = Symbol("")
_noname(name::Name) = name # Keep the name so the type doesn't change

_parent_range(r::Dimensions.DimUnitRange) = parent(r)
_parent_range(r) = r

for func in (:copy, :one, :oneunit, :zero)
@eval begin
(Base.$func)(A::AbstractDimArray; kw...) = rebuild(A; data=($func)(parent(A)), kw...)
Expand Down
16 changes: 16 additions & 0 deletions test/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ end
@test metadata(da_sim) === metadata(da)
end

@testset "similar with keywords" begin
for x in (da, DimPoints(da))
md = Dict(:new_meta => "b")
rd = (format(Ti(1:1)),)
da_named = similar(x; name=:new_name, metadata=md, refdims=rd)
@test name(da_named) === :new_name
@test metadata(da_named) === md
@test refdims(da_named) === rd
da_float_named = similar(x, Float64; name=:new_name, metadata=md, refdims=rd)
@test eltype(da_float_named) === Float64
@test name(da_float_named) === :new_name
@test metadata(da_float_named) === md
@test refdims(da_float_named) === rd
end
end

@testset "similar with a type" begin
da_float = @inferred similar(da, Float64)
@test eltype(da_float) == Float64
Expand Down

0 comments on commit 78bf7b9

Please sign in to comment.