Skip to content

Commit

Permalink
Fix from_table
Browse files Browse the repository at this point in the history
  • Loading branch information
diegozea committed May 25, 2021
1 parent af80cd7 commit fcea952
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 16 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,21 @@ 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
"b" "b" 0
"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

```
30 changes: 17 additions & 13 deletions src/pairwiselistmatrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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})),
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

2 comments on commit fcea952

@diegozea
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/37506

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.11.0 -m "<description of version>" fcea952fdff99a21279e792b71d8be2cbc018977
git push origin v0.11.0

Please sign in to comment.