Skip to content

Commit fcea952

Browse files
committed
Fix from_table
1 parent af80cd7 commit fcea952

File tree

5 files changed

+41
-16
lines changed

5 files changed

+41
-16
lines changed

Diff for: NEWS.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## PairwiseListMatrices.jl Release Notes
22

3+
## Changes from v0.10 to v0.11
4+
5+
Fix bug in `from_table`.
6+
37
## Changes from v0.9 to v0.10
48

59
* Add the `@iterateupper,`, `@iteratelist` and `@iteratediag` macros back again

Diff for: Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "PairwiseListMatrices"
22
uuid = "f9da4da7-9382-5435-b973-175f5d8dfb32"
3-
version = "0.10.0"
3+
version = "0.11.0"
44

55
[deps]
66
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"

Diff for: README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,21 @@ a │ 0 1 2
6868
b │ 1 0 3
6969
c │ 2 3 0
7070

71-
julia> to_table(nplm)
72-
6×3 Array{Any,2}:
71+
julia> table = to_table(nplm)
72+
6×3 Matrix{Any}:
7373
"a" "a" 0
7474
"a" "b" 1
7575
"a" "c" 2
7676
"b" "b" 0
7777
"b" "c" 3
7878
"c" "c" 0
7979

80+
julia> from_table(table, true)
81+
3×3 Named PairwiseListMatrix{Any, true, Vector{Any}}
82+
A ╲ B │ a b c
83+
──────┼────────
84+
a │ 0 1 2
85+
b │ 1 0 3
86+
c │ 2 3 0
87+
8088
```

Diff for: src/pairwiselistmatrix.jl

+17-13
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,24 @@ end
107107
# Empties
108108
# -------
109109

110-
function _diagonal_value(diagonal::Bool, ::Type{T}) where T
111-
if !diagonal
112-
if hasmethod(zero, (T,))
113-
return zero(T)
114-
elseif T <: Union{Missing, Int}
115-
return missing
110+
function _diagonal_value(::Type{T}) where T
111+
if hasmethod(zero, (T,))
112+
return zero(T)
113+
else
114+
if T isa Union
115+
T_types = Base.uniontypes(T)
116+
if Missing T_types
117+
return missing
118+
end
119+
if Nothing T_types
120+
return nothing
121+
end
116122
elseif T === Any
117123
return nothing
118-
else
119-
throw(ErrorException(
120-
"Please use the last argument to fill the diagonal. It should be of type $T."))
121124
end
122125
end
123-
Array{T}(undef, 1)[1]
126+
throw(ErrorException(
127+
"Please use the last argument to fill the diagonal. It should be of type $T."))
124128
end
125129

126130

@@ -139,7 +143,7 @@ PairwiseListMatrix(Int, 3, true)
139143
function PairwiseListMatrix(::Type{T},
140144
nelements::Int,
141145
diagonal::Bool = false,
142-
diagonalvalue::T = _diagonal_value(diagonal, T)) where T
146+
diagonalvalue::T = _diagonal_value(T)) where T
143147
if diagonal
144148
return PairwiseListMatrix{T, true, Vector{T}}(
145149
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
283287
"""
284288
function PairwiseListMatrix(list::AbstractVector{T},
285289
diagonal::Bool = false,
286-
diagonalvalue::T = _diagonal_value(diagonal, T)) where T
290+
diagonalvalue::T = _diagonal_value(T)) where T
287291
VT = typeof(list)
288292
if diagonal
289293
nelements = _nelements_with_diagonal(length(list))
@@ -1170,7 +1174,7 @@ function from_table(table,
11701174
@assert size(table,2) >= 3
11711175
values = table[:,valuecol]
11721176
if diagonalvalue == :default
1173-
diagonalvalue = _diagonal_value(diagonal, eltype(values))
1177+
diagonalvalue = _diagonal_value(eltype(values))
11741178
end
11751179
plm = PairwiseListMatrix(values, diagonal, diagonalvalue)
11761180
nplm = NamedArray(plm)

Diff for: test/runtests.jl

+9
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,15 @@ end
582582

583583
@test to_table(list) == to_table(list_diag)
584584
@test to_table(list, diagonal=false) == to_table(list_diag, diagonal=false)
585+
586+
@testset "README" begin
587+
# using Any type instead of Int to allow @test
588+
plm = PairwiseListMatrix(Any[1,2,3], false)
589+
nplm = setlabels(plm, ["a","b","c"])
590+
table = to_table(nplm)
591+
new_nplm = from_table(table, true)
592+
@test new_nplm == plm
593+
end
585594
end
586595

587596
@testset "DataFrames" begin

0 commit comments

Comments
 (0)