API
Creation
PairwiseListMatrices.PairwiseListMatrix — TypePairwiseListMatrix{T, diagonal, VT} is a (squared) symmetric matrix that stores a list of type VT with values of type T for the pairwise comparison/evaluation of nelements. If diagonal is true the first element of the list is 1, 1 otherwise is 1, 2. If diagonal is false the diagonal values are stored in a vector on the diag field.
Getters
PairwiseListMatrices.hasdiagonal — FunctionReturns true if the list has diagonal values.
PairwiseListMatrices.getlist — FunctionRetuns the list vector.
PairwiseListMatrices.getdiag — FunctionRetuns the diag vector (which contains the diagonal values if diagonal is false).
Helpers
PairwiseListMatrices.lengthlist — FunctionReturns the length of the list field
Returns the list length needed for a pairwise measures or comparisons of nelements. If diagonal is true, diagonal values are included in the list.
julia> using PairwiseListMatrices
julia> plm = PairwiseListMatrix([1, 2, 3, 4, 5, 6], false)
4×4 PairwiseListMatrix{Int64,false,Array{Int64,1}}:
0 1 2 3
1 0 4 5
2 4 0 6
3 5 6 0
julia> lengthlist(4, false)
6
julia> lengthlist(plm)
6
PairwiseListMatrices.ij2k — FunctionReturns the k index of the list from the indixes i and j with i<j from a matrix of nelements by nelements. diagonal should be true or Val{true} if the diagonal values are on the list. You must not use it with i>j.
julia> using PairwiseListMatrices
julia> plm = PairwiseListMatrix([10,20,30,40,50,60], true)
3×3 PairwiseListMatrix{Int64,true,Array{Int64,1}}:
10 20 30
20 40 50
30 50 60
julia> ij2k(1, 2, 3, true)
2
julia> getlist(plm)[2]
20
PairwiseListMatrices.diagonal — FunctionReturns a vector of type VT from a PairwiseListMatrix{T, false, VT} that has the diagonal values.
julia> using PairwiseListMatrices
julia> plm = PairwiseListMatrix([10,20,30,40,50,60], true)
3×3 PairwiseListMatrix{Int64,true,Array{Int64,1}}:
10 20 30
20 40 50
30 50 60
julia> diagonal(plm)
3-element Array{Int64,1}:
10
40
60
PairwiseListMatrices.apply2list — FunctionThe apply2list function applies the function fun, the first argument, for each element on list but avoiding getfield calls inside the loop. The second argument of the macro is the PairwiseListMatrix that will be iterated. fun should take two arguments; the first is the list field of the PairwiseListMatrix, and the second is the index over that list at a given iteration.
julia> using PairwiseListMatrices
julia> plm = PairwiseListMatrix([1,2,3], false)
3×3 PairwiseListMatrix{Int64,false,Array{Int64,1}}:
0 1 2
1 0 3
2 3 0
julia> apply2list((list, k) -> println(list[k]), plm)
1
2
3
PairwiseListMatrices.apply2diag — FunctionThe apply2diag function applies the function fun, the first argument, for each element on the diag field of aPairwiseListMatrix{T,false,VT} but avoiding getfield calls inside the loop. The second argument of the macro is the PairwiseListMatrix that will be iterated. fun should take two arguments; the first is the diag field of the PairwiseListMatrix, and the second is the index over that vector of diagonal elements at a given iteration.
julia> using PairwiseListMatrices
julia> plm = PairwiseListMatrix([1,2,3], false)
3×3 PairwiseListMatrix{Int64,false,Array{Int64,1}}:
0 1 2
1 0 3
2 3 0
julia>apply2diag((diag, k) -> diag[k] += 10k, plm)
julia> plm
3×3 PairwiseListMatrix{Int64,false,Array{Int64,1}}:
10 1 2
1 20 3
2 3 30
PairwiseListMatrices.apply2upper — FunctionThe apply2upper function applies the fun function over the upper triangular part of the PairwiseListMatrix. Set use_diag to true if the diagonal needs to be included in the iteration. The function should take four arguments, first the list and diag fields of the PairwiseListMatrix, the second is the index k over that list, and the third and fourth are the i and j indexes for the position k in the upper triangular part of the matrix.
julia> using PairwiseListMatrices
julia> plm = PairwiseListMatrix([1,2,3], true)
2×2 PairwiseListMatrix{Int64,true,Array{Int64,1}}:
1 2
2 3
julia> mat = zeros(Int, 2, 2)
2×2 Array{Int64,2}:
0 0
0 0
julia> apply2upper((list, k, i, j) -> mat[i, j] = list[k], plm; use_diag = true)
julia> mat
2×2 Array{Int64,2}:
1 2
0 3
Labels/Names
PairwiseListMatrices.getlabels — FunctionIt gets the labels of a PairwiseListMatrix.
julia> using PairwiseListMatrices
julia> plm = PairwiseListMatrix(ones(3), false)
3×3 PairwiseListMatrix{Float64,false,Array{Float64,1}}:
0.0 1.0 1.0
1.0 0.0 1.0
1.0 1.0 0.0
julia> getlabels(plm)
3-element Array{String,1}:
"1"
"2"
"3"
julia> nplm = setlabels(plm, ["a","b","c"])
3×3 Named PairwiseListMatrix{Float64,false,Array{Float64,1}}
A ╲ B │ a b c
──────┼──────────────
a │ 0.0 1.0 1.0
b │ 1.0 0.0 1.0
c │ 1.0 1.0 0.0
julia> getlabels(nplm)
3-element Array{String,1}:
"a"
"b"
"c"
PairwiseListMatrices.setlabels — FunctionCreates a Named PairwiseListMatrix.
julia> using PairwiseListMatrices
julia> plm = PairwiseListMatrix(ones(3), false)
3×3 PairwiseListMatrix{Float64,false,Array{Float64,1}}:
0.0 1.0 1.0
1.0 0.0 1.0
1.0 1.0 0.0
julia> nplm = setlabels(plm, ["a","b","c"])
3×3 Named PairwiseListMatrix{Float64,false,Array{Float64,1}}
A ╲ B │ a b c
──────┼──────────────
a │ 0.0 1.0 1.0
b │ 1.0 0.0 1.0
c │ 1.0 1.0 0.0
PairwiseListMatrices.setlabels! — FunctionIt changes the labels of a Named PairwiseListMatrix
Join
Base.join — FunctionThis function join two PairwiseListMatrices by their labels, returning two PairwiseListMatrices with same size and labels. There are 4 kinds of joins:
:inner: Intersect. The output matrices only include the labels that are in both PairwiseListMatrices:outer: Union. Include the labels of the two PairwiseListMatrices.:left: Only use labels from the first argument.:right: Only use labels from the second argument.
NaNs are filled in where needed to complete joins. The default value for missing values can be changed passing a tuple to missing.
julia> using PairwiseListMatrices
julia> l = setlabels(PairwiseListMatrix([1.,2.,3.], false), ["a","b","c"]) # a b c
3×3 Named PairwiseListMatrix{Float64,false,Array{Float64,1}}
A ╲ B │ a b c
──────┼──────────────
a │ 0.0 1.0 2.0
b │ 1.0 0.0 3.0
c │ 2.0 3.0 0.0
julia> r = setlabels(PairwiseListMatrix([1.,2.,3.], false), ["b","c","d"]) # b c d
3×3 Named PairwiseListMatrix{Float64,false,Array{Float64,1}}
A ╲ B │ b c d
──────┼──────────────
b │ 0.0 1.0 2.0
c │ 1.0 0.0 3.0
d │ 2.0 3.0 0.0
julia> join(l, r, kind=:inner) # b c
(2×2 Named PairwiseListMatrix{Float64,false,Array{Float64,1}}
A ╲ B │ b c
──────┼─────────
b │ 0.0 3.0
c │ 3.0 0.0, 2×2 Named PairwiseListMatrix{Float64,false,Array{Float64,1}}
A ╲ B │ b c
──────┼─────────
b │ 0.0 1.0
c │ 1.0 0.0)
julia> join(l, r, kind=:outer) # a b c d
(4×4 Named PairwiseListMatrix{Float64,false,Array{Float64,1}}
A ╲ B │ a b c d
──────┼───────────────────
a │ 0.0 1.0 2.0 NaN
b │ 1.0 0.0 3.0 NaN
c │ 2.0 3.0 0.0 NaN
d │ NaN NaN NaN NaN, 4×4 Named PairwiseListMatrix{Float64,false,Array{Float64,1}}
A ╲ B │ a b c d
──────┼───────────────────
a │ NaN NaN NaN NaN
b │ NaN 0.0 1.0 2.0
c │ NaN 1.0 0.0 3.0
d │ NaN 2.0 3.0 0.0)
Statistics
PairwiseListMatrices.sum_nodiag — FunctionSum the values outside the diagonal
PairwiseListMatrices.mean_nodiag — FunctionMean of the values outside the diagonal
PairwiseListMatrices.zscore — FunctionIt's like zscore! but without modifying the PairwiseListMatrix
PairwiseListMatrices.zscore! — FunctionThis function takes a vector of PairwiseListMatrix objects and a PairwiseListMatrix and fill the matrix with the zscore value using the median and std of the vector.
IO
PairwiseListMatrices.to_table — FunctionCreates a Matrix{Any}, labels are stored in the columns 1 and 2, and the values in the column 3. Diagonal values are included by default.
julia> using PairwiseListMatrices
julia> plm = PairwiseListMatrix([10,20,30], false)
3×3 PairwiseListMatrix{Int64,false,Array{Int64,1}}:
0 10 20
10 0 30
20 30 0
julia> to_table(plm)
6×3 Array{Any,2}:
"1" "1" 0
"1" "2" 10
"1" "3" 20
"2" "2" 0
"2" "3" 30
"3" "3" 0
julia> to_table(plm, diagonal=false)
3×3 Array{Any,2}:
"1" "2" 10
"1" "3" 20
"2" "3" 30
PairwiseListMatrices.to_dict — FunctionIt takes a PairwiseListMatrix and converts it to a Dict of Symbols to arrays. The returned dictionary can be easily converted into a DataFrame.
julia> using PairwiseListMatrices, DataFrames
julia> nplm = setlabels(PairwiseListMatrix([10,20,30], false), ["a","b","c"])
3×3 Named PairwiseListMatrix{Int64,false,Array{Int64,1}}
A ╲ B │ a b c
──────┼───────────
a │ 0 10 20
b │ 10 0 30
c │ 20 30 0
julia> dict = to_dict(nplm, diagonal=false)
Dict{Symbol,Array{T,1} where T} with 3 entries:
:values => [10, 20, 30]
:j => ["b", "c", "c"]
:i => ["a", "a", "b"]
julia> DataFrame(dict)
3×3 DataFrames.DataFrame
│ Row │ i │ j │ values │
│ │ String │ String │ Int64 │
├─────┼────────┼────────┼────────┤
│ 1 │ a │ b │ 10 │
│ 2 │ a │ c │ 20 │
│ 3 │ b │ c │ 30 │PairwiseListMatrices.from_table — FunctionCreation of a PairwiseListMatrix from a Matrix, DataFrame or similar structure. By default the columns with the labels for i (slow) and j (fast) are 1 and 2. Values are taken from the column 3 by default.
julia> using PairwiseListMatrices, Pkg, DelimitedFiles
julia> import PairwiseListMatrices
julia> filename = joinpath(dirname(pathof(PairwiseListMatrices)), "..", "test", "example.csv");
julia> dat = readdlm(filename, ',')
3×3 Array{Any,2}:
"A" "B" 10
"A" "C" 20
"B" "C" 30
julia> from_table(dat, false)
3×3 Named PairwiseListMatrix{Any,false,Array{Any,1}}
A ╲ B │ A B C
──────┼──────────────────────────
A │ nothing 10 20
B │ 10 nothing 30
C │ 20 30 nothingThis is also useful to create a PairwiseListMatrix from a DataFrame:
julia> using PairwiseListMatrices, DataFrames, CSV, Pkg
julia> import PairwiseListMatrices
julia> filename = joinpath(dirname(pathof(PairwiseListMatrices)), "..", "test", "example.csv");
julia> df = CSV.read(filename, header=false)
3×3 DataFrames.DataFrame
│ Row │ Column1 │ Column2 │ Column3 │
│ │ String⍰ │ String⍰ │ Int64⍰ │
├─────┼─────────┼─────────┼─────────┤
│ 1 │ A │ B │ 10 │
│ 2 │ A │ C │ 20 │
│ 3 │ B │ C │ 30 │
julia> from_table(df, false)
3×3 Named PairwiseListMatrix{Union{Missing, Int64},false,Array{Union{Missing, Int64},1}}
A ╲ B │ A B C
──────┼──────────────────────────
A │ missing 10 20
B │ 10 missing 30
C │ 20 30 missingDelimitedFiles.writedlm — FunctionThis function takes the filename as first argument and a PairwiseListMatrix as second argument. If the diagonal keyword argument is true (default), the diagonal is included in the output. The keyword argument delim (by default is ' ') allows to modified the character used as delimiter.
julia> using PairwiseListMatrices, DelimitedFiles
julia> plm = PairwiseListMatrix(trues(3), false)
3×3 PairwiseListMatrix{Bool,false,BitArray{1}}:
false true true
true false true
true true false
julia> writedlm("example.csv", plm, diagonal=false, delim=',')
julia> println(read("example.csv", String))
1,2,true
1,3,true
2,3,true