From fcea952fdff99a21279e792b71d8be2cbc018977 Mon Sep 17 00:00:00 2001 From: Diego Javier Zea Date: Tue, 25 May 2021 23:08:40 +0200 Subject: [PATCH] Fix from_table --- NEWS.md | 4 ++++ Project.toml | 2 +- README.md | 12 ++++++++++-- src/pairwiselistmatrix.jl | 30 +++++++++++++++++------------- test/runtests.jl | 9 +++++++++ 5 files changed, 41 insertions(+), 16 deletions(-) diff --git a/NEWS.md b/NEWS.md index 582c6f7..e1da03b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ ## PairwiseListMatrices.jl Release Notes +## Changes from v0.10 to v0.11 + +Fix bug in `from_table`. + ## Changes from v0.9 to v0.10 * Add the `@iterateupper,`, `@iteratelist` and `@iteratediag` macros back again diff --git a/Project.toml b/Project.toml index 16568ae..de264e6 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "PairwiseListMatrices" uuid = "f9da4da7-9382-5435-b973-175f5d8dfb32" -version = "0.10.0" +version = "0.11.0" [deps] DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab" diff --git a/README.md b/README.md index 39d9bb4..76e0614 100644 --- a/README.md +++ b/README.md @@ -68,8 +68,8 @@ a │ 0 1 2 b │ 1 0 3 c │ 2 3 0 -julia> to_table(nplm) -6×3 Array{Any,2}: +julia> table = to_table(nplm) +6×3 Matrix{Any}: "a" "a" 0 "a" "b" 1 "a" "c" 2 @@ -77,4 +77,12 @@ julia> to_table(nplm) "b" "c" 3 "c" "c" 0 +julia> from_table(table, true) +3×3 Named PairwiseListMatrix{Any, true, Vector{Any}} +A ╲ B │ a b c +──────┼──────── +a │ 0 1 2 +b │ 1 0 3 +c │ 2 3 0 + ``` diff --git a/src/pairwiselistmatrix.jl b/src/pairwiselistmatrix.jl index 763d067..8e6c4ab 100644 --- a/src/pairwiselistmatrix.jl +++ b/src/pairwiselistmatrix.jl @@ -107,20 +107,24 @@ end # Empties # ------- -function _diagonal_value(diagonal::Bool, ::Type{T}) where T - if !diagonal - if hasmethod(zero, (T,)) - return zero(T) - elseif T <: Union{Missing, Int} - return missing +function _diagonal_value(::Type{T}) where T + if hasmethod(zero, (T,)) + return zero(T) + else + if T isa Union + T_types = Base.uniontypes(T) + if Missing ∈ T_types + return missing + end + if Nothing ∈ T_types + return nothing + end elseif T === Any return nothing - else - throw(ErrorException( - "Please use the last argument to fill the diagonal. It should be of type $T.")) end end - Array{T}(undef, 1)[1] + throw(ErrorException( + "Please use the last argument to fill the diagonal. It should be of type $T.")) end @@ -139,7 +143,7 @@ PairwiseListMatrix(Int, 3, true) function PairwiseListMatrix(::Type{T}, nelements::Int, diagonal::Bool = false, - diagonalvalue::T = _diagonal_value(diagonal, T)) where T + diagonalvalue::T = _diagonal_value(T)) where T if diagonal return PairwiseListMatrix{T, true, Vector{T}}( Array{T}(undef, lengthlist(nelements, Val{true})), @@ -283,7 +287,7 @@ instead of being on the list. The `diag` vector can be filled with the optional """ function PairwiseListMatrix(list::AbstractVector{T}, diagonal::Bool = false, - diagonalvalue::T = _diagonal_value(diagonal, T)) where T + diagonalvalue::T = _diagonal_value(T)) where T VT = typeof(list) if diagonal nelements = _nelements_with_diagonal(length(list)) @@ -1170,7 +1174,7 @@ function from_table(table, @assert size(table,2) >= 3 values = table[:,valuecol] if diagonalvalue == :default - diagonalvalue = _diagonal_value(diagonal, eltype(values)) + diagonalvalue = _diagonal_value(eltype(values)) end plm = PairwiseListMatrix(values, diagonal, diagonalvalue) nplm = NamedArray(plm) diff --git a/test/runtests.jl b/test/runtests.jl index ca6516b..53a1197 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -582,6 +582,15 @@ end @test to_table(list) == to_table(list_diag) @test to_table(list, diagonal=false) == to_table(list_diag, diagonal=false) + + @testset "README" begin + # using Any type instead of Int to allow @test + plm = PairwiseListMatrix(Any[1,2,3], false) + nplm = setlabels(plm, ["a","b","c"]) + table = to_table(nplm) + new_nplm = from_table(table, true) + @test new_nplm == plm + end end @testset "DataFrames" begin