rafaqz / DimensionalData.jl

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

Add DimUnitRange #439

Closed sethaxen closed 1 year ago

sethaxen commented 1 year ago

As suggested in #437, this PR adds a DimUnitRange type. Now all calls to axes return an object of this type. This allows similar to take axes of a dimensional array and still produce a dimensional array type. It also allows DimArrays to be constructed from other array types.

Here's an example:

julia> using DimensionalData

julia> da = DimArray(randn(10, 4, 2), (:draw, :chain, :var));

julia> axes(da)  # this could be prettier
(DimensionalData.Dimensions.DimUnitRange(range=Base.OneTo(10), dim=Dim{:draw, DimensionalData.Dimensions.LookupArrays.NoLookup{Base.OneTo{Int64}}}([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])), DimensionalData.Dimensions.DimUnitRange(range=Base.OneTo(4), dim=Dim{:chain, DimensionalData.Dimensions.LookupArrays.NoLookup{Base.OneTo{Int64}}}([1, 2, 3, 4])), DimensionalData.Dimensions.DimUnitRange(range=Base.OneTo(2), dim=Dim{:var, DimensionalData.Dimensions.LookupArrays.NoLookup{Base.OneTo{Int64}}}([1, 2])))

julia> similar(da, Int, axes(da))
10×4×2 DimArray{Int64,3} with dimensions: Dim{:draw}, Dim{:chain}, Dim{:var}
[:, :, 1]
               3  140601401134352               10  140601401134992
               3  140601401134416               11  140601401135056
 140601401134096                7  140601401134736               15
 140601401134160                8  140601401134800               17
               4  140601401134480               12  140601401135120
               5  140601401134544               12  140601401135184
 140601401134224                9  140601401134864               18
 140601401134288                9  140601401134928               26
               6  140601401134608               13  140601401135248
               6  140601401134672               14  140601401135312
[and 1 more slices...]

julia> similar(trues(2, 2), axes(da))
10×4×2 DimArray{Bool,3} with dimensions: Dim{:draw}, Dim{:chain}, Dim{:var}
[:, :, 1]
 1  0  0  0
 0  0  0  0
 0  0  0  0
 0  0  0  0
 0  0  0  0
 0  0  0  0
 0  0  0  0
 0  0  0  0
 0  0  0  0
 0  0  0  0
[and 1 more slices...]

julia> similar(BitArray, (axes(da, 1),))
10-element DimArray{Bool,1} with dimensions: Dim{:draw}
  1  0
  2  0
  3  0
  4  0
  5  0
  6  0
  7  0
  8  0
  9  0
 10  0

julia> ax = axes(da, 1);

julia> similar(ax)
10-element DimArray{Int64,1} with dimensions: Dim{:draw}
  1  1
  2  2
  3  1
  4  1
  5  1
  6  1
  7  1
  8  1
  9  0
 10  1

julia> map(sin, ax)
10-element DimArray{Float64,1} with dimensions: Dim{:draw}
  1   0.841471
  2   0.909297
  3   0.14112
  4  -0.756802
  5  -0.958924
  6  -0.279415
  7   0.656987
  8   0.989358
  9   0.412118
 10  -0.544021

There are still a handful of methods I need to write tests for.

Currently the test suite fails due to #438

sethaxen commented 1 year ago

All methods except print now have tests. Perhaps you have suggestions on how to have a prettier print method? The default for AbstractUnitRange would look like 1:1:10, but it would be nice to 1) show as the range the underlying range object (e.g. Base.OneTo) and 2) also show the dim, though it seems unnecessarily verbose to show the dim indices.

rafaqz commented 1 year ago

If you are showing it with keyword args we need a keyword constructor so it's runnable code

sethaxen commented 1 year ago

If you are showing it with keyword args we need a keyword constructor so it's runnable code

Good point. A keyword construct would be unnecessary, so I've removed keywords from the show method.

codecov-commenter commented 1 year ago

Codecov Report

Merging #439 (5849f71) into main (d907a8e) will increase coverage by 0.05%. The diff coverage is 93.93%.

@@            Coverage Diff             @@
##             main     #439      +/-   ##
==========================================
+ Coverage   89.26%   89.31%   +0.05%     
==========================================
  Files          36       37       +1     
  Lines        2543     2574      +31     
==========================================
+ Hits         2270     2299      +29     
- Misses        273      275       +2     
Impacted Files Coverage Δ
src/Dimensions/dimunitrange.jl 89.47% <89.47%> (ø)
src/Dimensions/dimension.jl 88.33% <100.00%> (+0.09%) :arrow_up:
src/array/array.jl 95.16% <100.00%> (+0.47%) :arrow_up:

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

sethaxen commented 1 year ago

@rafaqz do you mind if I tag a patch release after merging this PR?

rafaqz commented 1 year ago

Go for it. I sent you a collaborator invite.