Skip to content

Commit

Permalink
fix and test comparedims (#662)
Browse files Browse the repository at this point in the history
* fix and test comparedims

* fix tests
  • Loading branch information
rafaqz authored Mar 9, 2024
1 parent 338fa26 commit 6a90cd4
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 12 deletions.
21 changes: 11 additions & 10 deletions src/Dimensions/primitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ function comparedims end
)
type && basetypeof(a) != basetypeof(b) && _dimsmismatcherror(a, b)
valtype && typeof(parent(a)) != typeof(parent(b)) && _valtypeerror(a, b)
val && parent(a) != parent(b) && _valerror(a, b)
val && parent(lookup(a)) != parent(lookup(b)) && _valerror(a, b)
if order
(isnolookup(a) || isnolookup(b) || LU.order(a) == LU.order(b)) || _ordererror(a, b)
end
Expand Down Expand Up @@ -587,7 +587,8 @@ end
@inline _comparedims(T::Type{Bool}, a::Dimension, b::AnonDim; kw...) = true
@inline _comparedims(T::Type{Bool}, a::AnonDim, b::Dimension; kw...) = true
@inline function _comparedims(::Type{Bool}, a::Dimension, b::Dimension;
type=true, valtype=false, val=false, length=true, order=false, ignore_length_one=false, warn=nothing,
type=true, valtype=false, val=false, length=true, order=false, ignore_length_one=false,
warn::Union{Nothing,String}=nothing,
)
if type && basetypeof(a) != basetypeof(b)
isnothing(warn) || _dimsmismatchwarn(a, b, warn)
Expand All @@ -597,12 +598,12 @@ end
isnothing(warn) || _valtypewarn(a, b, warn)
return false
end
if val && parent(a) != parent(b)
if val && parent(lookup(a)) != parent(lookup(b))
isnothing(warn) || _valwarn(a, b, warn)
return false
end
if order && !(isnolookup(a) || isnolookup(b) || LU.order(a) == LU.order(b))
_orderwarn(a, b, warn)
isnothing(warn) || _orderwarn(a, b, warn)
return false
end
if ignore_length_one && (Base.length(a) == 1 || Base.length(b) == 1)
Expand Down Expand Up @@ -767,12 +768,12 @@ _ordermsg(a, b) = "Lookups do not all have the same order: $(order(a)), $(order(
_typemsg(a, b) = "Lookups do not all have the same type: $(order(a)), $(order(b))."

# Warning: @noinline to avoid allocations when it isn't used
@noinline _dimsmismatchwarn(a, b, msg="") = @warn _dimsmismatchmsg(a, b) * msg
@noinline _valwarn(a, b, msg="") = @warn _valmsg(a, b) * msg
@noinline _dimsizewarn(a, b, msg="") = @warn _dimsizemsg(a, b) * msg
@noinline _valtypewarn(a, b, msg="") = @warn _valtypemsg(a, b) * msg
@noinline _extradimswarn(dims, msg="") = @warn _extradimsmsg(dims) * msg
@noinline _orderwarn(a, b, msg="") = @warn _ordermsg(a, b) * msg
@noinline _dimsmismatchwarn(a, b, msg="") = @warn string(_dimsmismatchmsg(a, b), msg)
@noinline _valwarn(a, b, msg="") = @warn string(_valmsg(a, b), msg)
@noinline _dimsizewarn(a, b, msg="") = @warn string(_dimsizemsg(a, b), msg)
@noinline _valtypewarn(a, b, msg="") = @warn string(_valtypemsg(a, b), msg)
@noinline _extradimswarn(dims, msg="") = @warn string(_extradimsmsg(dims), msg)
@noinline _orderwarn(a, b, msg="") = @warn string(_ordermsg(a, b), msg)

# Error
@noinline _dimsmismatcherror(a, b) = throw(DimensionMismatch(_dimsmismatchmsg(a, b)))
Expand Down
2 changes: 1 addition & 1 deletion src/array/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ Base.similar(A::AbstractDimArray, ::Type{T}, D::Tuple{}) where T =

# Keep the same type in `similar`
_noname(A::AbstractBasicDimArray) = _noname(name(A))
_noname(s::String) = @show s
_noname(s::String) = ""
_noname(::NoName) = NoName()
_noname(::Symbol) = Symbol("")
_noname(name::Name) = name # Keep the name so the type doesn't change
Expand Down
61 changes: 60 additions & 1 deletion test/primitives.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using DimensionalData, Dates, Test , BenchmarkTools
using DimensionalData.Lookups, DimensionalData.Dimensions

using .Dimensions: _dim_query, _wraparg, _reducedims, AlwaysTuple, MaybeFirst
using .Dimensions: _dim_query, _wraparg, _reducedims, AlwaysTuple, MaybeFirst, comparedims

@dim Tst

Expand Down Expand Up @@ -300,6 +300,65 @@ end
@test_throws DimensionMismatch combinedims((X(1:2), Y(1:5)), (X(1:10), Z(3:10)))
end

@testset "comparedims" begin
@testset "default keywords" begin
@test comparedims(Bool, X(1:2), X(1:2))
@test !comparedims(Bool, X(1:2), Y(1:2))
@test !comparedims(Bool, X(1:2), X(1:3))
@test_warn "Found both lengths 2 and 3" comparedims(Bool, X(1:2), X(1:3); warn="")
@test_warn "X and Y dims on the same axis" comparedims(Bool, X(1:2), Y(1:2); warn="")
@test comparedims(X(1:2), X(1:2)) == X(1:2)
@test_throws DimensionMismatch comparedims(X(1:2), Y(1:2))
@test_throws DimensionMismatch comparedims(X(1:2), X(1:3))
end
@testset "compare type" begin
@test comparedims(Bool, X(1:2), Y(1:2); type=false)
@test !comparedims(Bool, X(1:2), Y(1:2); type=true)
@test_warn "X and Y dims on the same axis" comparedims(Bool, X(1:2), Y(1:2); type=true, warn="")
@test comparedims(X(1:2), Y(1:2); type=false) == X(Sampled(1:2))
@test_throws DimensionMismatch comparedims(X(Sampled(1:2)), Y(Sampled(1:2)); type=true)
end
@testset "compare val type" begin
@test comparedims(Bool, X(Sampled(1:2)), X(Categorical(1:2)); valtype=false)
@test !comparedims(Bool, X(Sampled(1:2)), X(Categorical(1:2)); valtype=true)
@test comparedims(X(Sampled(1:2)), X(Categorical(1:2)); valtype=false) == X(Sampled(1:2))
@test_throws DimensionMismatch comparedims(X(Sampled(1:2)), X(Categorical(1:2)); valtype=true)
@test comparedims(Bool, X(Sampled(1:2)), X(Sampled([1, 2])); valtype=false)
@test !comparedims(Bool, X(Sampled(1:2)), X(Sampled([1, 2])); valtype=true)
@test comparedims(X(Sampled(1:2)), X(Sampled([1, 2])); valtype=false) == X(Sampled(1:2))
@test_throws DimensionMismatch comparedims(X(Sampled([1, 2])), X(Sampled(1:2)); valtype=true)
end
@testset "compare values" begin
@test comparedims(Bool, X(1:2), X(2:3); val=false)
@test !comparedims(Bool, X(1:2), X(2:3); val=true)
@test_warn "do not match" comparedims(Bool, X(1:2), X(2:3); val=true, warn="")
@test comparedims(Bool, X(Sampled(1:2)), X(Sampled(2:3)); val=false)
@test !comparedims(Bool, X(Sampled(1:2)), X(Sampled(2:3)); val=true)
@test comparedims(X(Sampled(1:2)), X(Sampled(2:3)); val=false) == X(Sampled(1:2))
@test_throws DimensionMismatch comparedims(X(Sampled(1:2)), X(Sampled(2:3)); val=true)
end
@testset "compare length" begin
@test comparedims(Bool, X(1:2), X(1:3); length=false)
@test !comparedims(Bool, X(1:2), X(1:3); length=true)
@test_warn "Found both lengths" comparedims(Bool, X(1:2), X(1:3); length=true, warn="")
@test comparedims(X(1:2), X(1:3); length=false) == X(1:2)
@test_throws DimensionMismatch comparedims(X(1:2), X(1:3); length=true)
@test comparedims(Bool, X(1:2), X(1:1); length=true, ignore_length_one=true)
@test !comparedims(Bool, X(1:2), X(1:1); length=true, ignore_length_one=false)
@test comparedims(X(1:2), X(1:1); length=false, ignore_length_one=true) == X(1:2)
@test_throws DimensionMismatch comparedims(X(1:2), X(1:1); length=true, ignore_length_one=false)
end
@testset "compare order" begin
a, b = X(Sampled(1:2); order=ForwardOrdered()), X(Sampled(1:2); order=ReverseOrdered())
@test comparedims(Bool, a, b; order=false)
@test !comparedims(Bool, a, b; order=true)
@test comparedims(Bool, a, b; order=false)
@test_nowarn comparedims(Bool, a, b; order=true)
@test_warn "Lookups do not all have the same order" comparedims(Bool, a, b; order=true, warn="")
@test_throws DimensionMismatch comparedims(a, b; order=true)
end
end

@testset "setdims" begin
A = setdims(da, X(Sampled(LinRange(150,152,2))))
@test index(A, X()) == LinRange(150,152,2)
Expand Down

0 comments on commit 6a90cd4

Please sign in to comment.