rafaqz / DimensionalData.jl

Named dimensions and indexing for julia arrays and other data
https://rafaqz.github.io/DimensionalData.jl/stable/
MIT License
271 stars 38 forks source link

Add similar for mixed axis types #470

Closed sethaxen closed 1 year ago

sethaxen commented 1 year ago

This is a (particularly ugly) fix for #464 based on https://github.com/rafaqz/DimensionalData.jl/issues/464#issuecomment-1437256934 and https://github.com/rafaqz/DimensionalData.jl/issues/464#issuecomment-1437285282. It allows DimUnitRange to be mixed with Integer and/or Base.OneTo axes, so long as a DimUnitRange appears within the first 4 axes, which should cover the most common cases. Fixes #464

@mcabbot I ended up dispatching also on Integer types for axes because otherwise we need to also implement overloads to hit similar(::AbstractArray, ::Tuple) for all of these combinations, which seemed unnecessary.

~Still need to exhaustively test~, but now this works:

julia> da = DimArray(randn(5, 2, 10), (X(1:5), Y(1:2), Z(1:10)));

julia> similar(parent(da), axes(da)) isa DimArray
true

julia> similar(parent(da), (Base.OneTo(10), axes(da)...)) isa Array
true

julia> similar(parent(da), (axes(da)..., Base.OneTo(10))) isa Array
true

julia> slices = eachslice(da; dims=X);

julia> da_slices = rebuild(da; data=slices, dims=dims(axes(slices)));

julia> stack(da_slices) isa DimArray
true
codecov-commenter commented 1 year ago

Codecov Report

Merging #470 (ddcbaf5) into main (be957d9) will increase coverage by 0.06%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #470      +/-   ##
==========================================
+ Coverage   89.64%   89.70%   +0.06%     
==========================================
  Files          38       38              
  Lines        2636     2653      +17     
==========================================
+ Hits         2363     2380      +17     
  Misses        273      273              
Impacted Files Coverage Δ
src/array/array.jl 96.45% <100.00%> (+0.48%) :arrow_up:

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

rafaqz commented 1 year ago

Thanks. Its unfortunate we have to do these "at least the first four args work" hacks, but better than nothing working.