Skip to content

Commit 99756bc

Browse files
committed
fix reverse performance, cleanup for code cov
1 parent 1fbf72f commit 99756bc

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

src/array.jl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ Base.similar(bc::Broadcast.Broadcasted{Broadcast.ArrayStyle{AbDimArray}}, ::Type
7070
# these aren't usefull unless you inherit from AbDimArray.
7171

7272
Base.mapslices(f, a::AbDimArray; dims=1, kwargs...) = begin
73-
println((f, dims, dimnum(a, dims)))
7473
dimnums = dimnum(a, dims)
7574
data = mapslices(f, parent(a); dims=dimnums, kwargs...)
7675
rebuildsliced(a, data, dims2indices(a, reducedims(DimensionalData.dims(a, dimnums))))
@@ -100,15 +99,23 @@ for fname in (:cor, :cov)
10099
end
101100
end
102101

103-
Base.reverse(a::AbDimArray{T,N}; dims=1) where {T,N} = begin
102+
@inline Base.reverse(a::AbDimArray{T,N}; dims=1) where {T,N} = begin
104103
dnum = dimnum(a, dims)
105104
# Reverse the dimension. TODO: make this type stable
106-
newdims = Tuple(map((x, n) -> n == dnum ? basetype(x)(reverse(val(x))) : x, DimensionalData.dims(a), 1:N))
105+
newdims = revdims(DimensionalData.dims(a), dnum)
107106
# Reverse the data
108107
newdata = reverse(parent(a); dims=dnum)
109108
rebuild(a, newdata, newdims, refdims(a))
110109
end
111110

111+
@inline revdims(dimstorev::Tuple, dnum) = begin
112+
dim = dimstorev[end]
113+
if length(dimstorev) == dnum
114+
dim = rebuild(dim, reverse(val(dim)))
115+
end
116+
(revdims(Base.front(dimstorev), dnum)..., dim)
117+
end
118+
@inline revdims(dims::Tuple{}, i) = ()
112119

113120

114121
"""
@@ -128,7 +135,7 @@ DimensionalArray(a::AbstractArray, dims; refdims=()) =
128135
Base.parent(a::DimensionalArray) = a.data
129136

130137
# DimensionalArray interface
131-
rebuild(a::DimensionalArray, data, dims, refdims) =
138+
@inline rebuild(a::DimensionalArray, data, dims, refdims) =
132139
DimensionalArray(data, dims, refdims)
133140

134141
refdims(a::DimensionalArray) = a.refdims

src/dimension.jl

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,11 @@ rebuild(dim::AbDim, val) = basetype(dim)(val, metadata(dim), order(dim))
4848

4949
dims(x::AbDim) = x
5050
dims(x::AbDimTuple) = x
51-
52-
name(dims::AbDimTuple) = (name(dims[1]), name(tail(dims))...)
53-
name(dims::Tuple{}) = ()
5451
name(dim::AbDim) = name(typeof(dim))
55-
5652
shortname(d::AbDim) = shortname(typeof(d))
5753
shortname(d::Type{<:AbDim}) = name(d)
58-
5954
units(dim::AbDim) = metadata(dim) == nothing ? "" : get(metadata(dim), :units, "")
6055

61-
label(dims::AbDimTuple) = join(join.(zip(name.(dims), string.(shorten.(val.(dims)))), ": ", ), ", ")
62-
6356
bounds(a, args...) = bounds(dims(a), args...)
6457
bounds(dims::AbDimTuple, lookupdims::Tuple) = bounds(dims[[dimnum(dims, lookupdims)...]])
6558
bounds(dims::AbDimTuple, dim::DimOrDimType) = bounds(dims[dimnum(dims, dim)])
@@ -174,6 +167,7 @@ end
174167
@inline Dim{X}(val=:; metadata=nothing, order=Forward()) where X =
175168
Dim{X}(val, metadata, order)
176169
name(::Type{<:Dim{X}}) where X = "Dim $X"
170+
shortname(::Type{<:Dim{X}}) where X = "$X"
177171
basetype(::Type{<:Dim{X,T,N}}) where {X,T,N} = Dim{X}
178172

179173

src/interface.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Return the units of a dimension. This could be a string, a unitful unit, or noth
6262
"""
6363
function units end
6464
units(x) = ""
65+
units(xs::Tuple) = map(units, xs)
6566

6667
"""
6768
name(x)
@@ -71,6 +72,7 @@ Get the name of data or a dimension.
7172
function name end
7273
name(x) = name(typeof(x))
7374
name(x::Type) = ""
75+
name(xs::Tuple) = map(name, xs)
7476

7577
"""
7678
shortname(x)
@@ -79,6 +81,7 @@ Get the short name of array data or a dimension.
7981
"""
8082
function shortname end
8183
shortname(x) = shortname(typeof(x))
84+
shortname(xs::Tuple) = map(shortname, xs)
8285
shortname(x::Type) = ""
8386

8487
"""
@@ -88,4 +91,8 @@ Get a plot label for data or a dimension. This will include the name and units
8891
if they exist, and anything else that should be shown on a plot.
8992
"""
9093
function label end
91-
label(x) = string(string(name(x)), " ", getstring(units(x)))
94+
label(x) = begin
95+
u = getstring(units(x))
96+
string(name(x), (u == "" ? "" : string(" ", u)))
97+
end
98+
label(xs::Tuple) = join(map(label, xs), ", ")

test/runtests.jl

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ using DimensionalData: val, basetype, slicedims, dims2indices, formatdims,
1212
@test val(TestDim(:test)) == :test
1313
@test metadata(TestDim(1, "metadata", Forward())) == "metadata"
1414
@test units(TestDim) == ""
15-
# TODO get rid of the trailing whitespace
16-
@test_broken label(TestDim) == "Test dimension"
15+
@test label(TestDim) == "Test dimension"
1716
@test eltype(TestDim(1)) == Int
1817
@test eltype(TestDim([1,2,3])) == Vector{Int}
1918
@test length(TestDim(1)) == 1
@@ -26,6 +25,10 @@ a = ones(5, 4)
2625
da = DimensionalArray(a, (X((140, 148)), Y((2, 11))))
2726
dimz = dims(da)
2827
@test slicedims(dimz, (2:4, 3)) == ((X(LinRange(142,146,3)),), (Y(8.0),))
28+
@test name(dimz) == ("X", "Y")
29+
@test shortname(dimz) == ("X", "Y")
30+
@test units(dimz) == ("", "")
31+
@test label(dimz) == ("X, Y")
2932

3033
a = [1 2 3 4
3134
2 3 4 5
@@ -169,6 +172,8 @@ a = [1 2 3 4
169172
dimz = (Dim{:row}((10, 30)), Dim{:column}((-20, 10)))
170173
da = DimensionalArray(a, dimz)
171174
@test name(dimz) == ("Dim row", "Dim column")
175+
@test shortname(dimz) == ("row", "column")
176+
@test label(dimz) == ("Dim row, Dim column")
172177
@test da[Dim{:row}(2)] == [3, 4, 5, 6]
173178
@test da[Dim{:column}(4)] == [4, 6, 7]
174179
@test da[Dim{:column}(1), Dim{:row}(3)] == 4
@@ -218,7 +223,6 @@ da = DimensionalArray(a, dimz)
218223
@test var(da; dims=X()) == [2.0 2.0]
219224
@test var(da; dims=Y()) == [0.5 0.5]'
220225
@test dims(var(da; dims=Y())) == (X(LinRange(143.0, 145.0, 2)), Y(LinRange(-38.0, -38.0, 1)))
221-
222226
a = [1 2 3; 4 5 6]
223227
da = DimensionalArray(a, dimz)
224228
@test median(da; dims=Y()) == [2.0 5.0]'
@@ -271,8 +275,7 @@ dsp = permutedims(da)
271275
@test dims(dsp) == reverse(dims(da))
272276
da = DimensionalArray(ones(5, 2, 4), (Y(10:20), Time(10:11), X(1:4)))
273277
dsp = permutedims(da, [3, 1, 2])
274-
dsp = permutedims(da, (3, 1, 2))
275-
# Dim dispatch
278+
# Dim dispatch arg possibilities
276279
dsp = permutedims(da, [X, Y, Time])
277280
dsp = permutedims(da, (X, Y, Time))
278281
dsp = permutedims(da, [X(), Y(), Time()])
@@ -416,11 +419,11 @@ println("Dims with UnitRange")
416419
@btime d3($g);
417420

418421
a = rand(5, 4, 3);
419-
dimz = (Y((1u"m", 5u"m")), X(1:4), Time(1:3))
420-
da = DimensionalArray(a, dimz)
422+
da = DimensionalArray(a, (Y((1u"m", 5u"m")), X(1:4), Time(1:3)))
423+
dimz = dims(da)
421424

422425
if VERSION > v"1.1-"
423-
println("eachslice: normal, numbers + rebuild, dims + rebuild")
426+
println("\n\neachslice: normal, numbers + rebuild, dims + rebuild")
424427
@btime (() -> eachslice($a; dims=2))();
425428
@btime (() -> eachslice($da; dims=2))();
426429
@btime (() -> eachslice($da; dims=Y))();
@@ -431,7 +434,8 @@ if VERSION > v"1.1-"
431434
@test [slice for slice in eachslice(da; dims=1)] == [slice for slice in eachslice(da; dims=Y)]
432435
end
433436

434-
println("mean: normal, numbers + rebuild, dims + rebuild")
437+
438+
println("\n\nmean: normal, numbers + rebuild, dims + rebuild")
435439
@btime mean($a; dims=2);
436440
@btime mean($da; dims=2);
437441
@btime mean($da; dims=X);
@@ -442,4 +446,4 @@ println("permutedims: normal, numbers + rebuild, dims + rebuild")
442446
println("reverse: normal, numbers + rebuild, dims + rebuild")
443447
@btime reverse($a; dims=1)
444448
@btime reverse($da; dims=1)
445-
@btime reverse($da; dims=Y())
449+
@btime reverse($da; dims=Y)

0 commit comments

Comments
 (0)