Skip to content

Commit

Permalink
Show brackets for dimensions (#782)
Browse files Browse the repository at this point in the history
* make showing brackets around dims optional

* show brackets around dims for array and stack show

* fix doctest

* fix show test
  • Loading branch information
rafaqz authored Aug 25, 2024
1 parent 2a11716 commit 3ed7b68
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 21 deletions.
10 changes: 5 additions & 5 deletions src/Dimensions/primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ julia> A = DimArray(ones(2, 3, 2), (X, Y, Z))
1.0 1.0 1.0
julia> dims(A, (X, Y))
↓ X, → Y
(↓ X, → Y)
```
"""
Expand Down Expand Up @@ -169,10 +169,10 @@ julia> using DimensionalData, .Dimensions
julia> A = DimArray(ones(10, 10, 10), (X, Y, Z));
julia> commondims(A, X)
↓ X
(↓ X)
julia> commondims(A, (X, Z))
↓ X, → Z
(↓ X, → Z)
julia> commondims(A, Ti)
()
Expand Down Expand Up @@ -285,10 +285,10 @@ julia> using DimensionalData, DimensionalData.Dimensions
julia> A = DimArray(ones(10, 10, 10), (X, Y, Z));
julia> otherdims(A, X)
↓ Y, → Z
(↓ Y, → Z)
julia> otherdims(A, (Y, Z))
↓ X
(↓ X)
```
"""
function otherdims end
Expand Down
4 changes: 4 additions & 0 deletions src/Dimensions/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ function show_dims(io::IO, mime::MIME"text/plain", dims::DimTuple;
)
ctx = IOContext(io, :compact => true)
inset = get(io, :inset, "")
brackets = get(io, :dim_brackets, true)
print(io, inset)
brackets && print(io, '(')
if all(map(d -> !(parent(d) isa AbstractArray) || (parent(d) isa NoLookup), dims))
dc = colors[1]
printstyled(io, dimsymbols(1), ' '; color=dc)
Expand All @@ -30,6 +32,7 @@ function show_dims(io::IO, mime::MIME"text/plain", dims::DimTuple;
printstyled(io, dimsymbols(n), ' '; color=dc)
show(IOContext(ctx, :dimcolor => dc, :dimname_len => 0), mime, d)
end
brackets && print(io, ')')
return 0
else # Dims get a line each
lines = 3
Expand All @@ -48,6 +51,7 @@ function show_dims(io::IO, mime::MIME"text/plain", dims::DimTuple;
dim_ctx = IOContext(ctx, :dimcolor => dc, :dimname_len => maxname)
show(dim_ctx, mime, d)
end
brackets && print(io, ')')
return lines
end
end
Expand Down
8 changes: 4 additions & 4 deletions src/array/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -742,13 +742,13 @@ placed at the end of `dims_new`. `others` contains other dimension pairs to be m
julia> using DimensionalData
julia> ds = (X(0:0.1:0.4), Y(10:10:100), Ti([0, 3, 4]))
↓ X 0.0:0.1:0.4,
(↓ X 0.0:0.1:0.4,
→ Y 10:10:100,
↗ Ti [0, 3, 4]
↗ Ti [0, 3, 4])
julia> mergedims(ds, (X, Y) => :space)
↓ Ti [0, 3, 4],
→ space MergedLookup{Tuple{Float64, Int64}} [(0.0, 10), (0.1, 10), …, (0.3, 100), (0.4, 100)] ↓ X, → Y
(↓ Ti [0, 3, 4],
→ space MergedLookup{Tuple{Float64, Int64}} [(0.0, 10), (0.1, 10), …, (0.3, 100), (0.4, 100)] (↓ X, → Y))
```
"""
function mergedims(x, dt1::Tuple, dts::Tuple...)
Expand Down
7 changes: 4 additions & 3 deletions src/array/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ function print_dims_block(io, mime, dims; displaywidth, blockwidth, label="dims"
printed = false
new_blockwidth = blockwidth
else
dim_lines = split(sprint(print_dims, mime, dims), '\n')
ctx = IOContext(io, :dim_brackets=>false)
dim_lines = split(sprint(print_dims, mime, dims; context=ctx), '\n')
new_blockwidth = max(blockwidth, min(displaywidth - 2, maximum(textwidth, dim_lines)))
lines += print_block_top(io, label, blockwidth, new_blockwidth)
lines += print_dims(io, mime, dims; kw...)
lines += print_block_top(ctx, label, blockwidth, new_blockwidth)
lines += print_dims(ctx, mime, dims; kw...)
println(io)
lines += 1
printed = true
Expand Down
6 changes: 3 additions & 3 deletions src/dimindices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ julia> di = DimIndices((X(1:2:4), Y(1:2:4)))
↓ X 1:2:3,
→ Y 1:2:3
└─────────────────────────────────────────────┘
↓ → 1 3
1 ↓ X 1, → Y 1 ↓ X 1, → Y 3
3 ↓ X 3, → Y 1 ↓ X 3, → Y 3
↓ → 1 3
1 (↓ X 1, → Y 1) (↓ X 1, → Y 3)
3 (↓ X 3, → Y 1) (↓ X 3, → Y 3)
julia> A[di] # Index A with these indices
╭─────────────────────────╮
Expand Down
2 changes: 1 addition & 1 deletion src/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ julia> set(da, Z=DD.NoLookup(), custom=DD.Sampled())
│ 3×4 DimArray{Float64,2} │
├─────────────────────────┴───────────────────────────────────────── dims ┐
↓ custom Sampled{Float64} 10.0:10.0:30.0 ForwardOrdered Regular Points,
→ Z
→ Z
└─────────────────────────────────────────────────────────────────────────┘
10.0 0.0 0.0 0.0 0.0
20.0 0.0 0.0 0.0 0.0
Expand Down
4 changes: 2 additions & 2 deletions src/stack/methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ julia> size(slices)
(4, 2)
julia> map(dims, axes(slices))
↓ Z Base.OneTo(4),
→ X Base.OneTo(2)
(↓ Z Base.OneTo(4),
→ X Base.OneTo(2))
julia> first(slices)
╭──────────────╮
Expand Down
4 changes: 2 additions & 2 deletions src/stack/stack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,8 @@ julia> using DimensionalData
julia> A = [1.0 2.0 3.0; 4.0 5.0 6.0];
julia> dimz = (X([:a, :b]), Y(10.0:10.0:30.0))
↓ X [:a, :b],
→ Y 10.0:10.0:30.0
(↓ X [:a, :b],
→ Y 10.0:10.0:30.0)
julia> da1 = DimArray(1A, dimz; name=:one);
Expand Down
2 changes: 1 addition & 1 deletion test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ end
@test occursin("Transformed", sv)
nds = (X(NoLookup(Base.OneTo(10))), Y(NoLookup(Base.OneTo(5))))
sv = sprint(show, MIME("text/plain"), nds)
@test sv == "↓ X, → Y"
@test sv == "(↓ X, → Y)"
end

@testset "arrays" begin
Expand Down

0 comments on commit 3ed7b68

Please sign in to comment.