Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanjohnharris committed Jan 22, 2024
1 parent 1042d7c commit e5aa824
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 140 deletions.
37 changes: 22 additions & 15 deletions src/CovarianceImage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function clustercovariance(Σ²)
if !issymmetric(Dr)
@warn "Correlation distance matrix is not symmetric, so not clustering"
end
Clustering.hclust(Dr; linkage=:average, branchorder=:optimal)
Clustering.hclust(Dr; linkage = :average, branchorder = :optimal)
end

"""
Expand All @@ -27,7 +27,10 @@ Either provide as positional arguments a vector `f` of N row names and an NΓ—_ m
"""
covarianceimage
@userplot CovarianceImage
Plots.@recipe function f(g::CovarianceImage; palette=[:cornflowerblue, :crimson, :forestgreen], colormode=:top, colorbargrad=:binary, donames=true, docluster=true, verbose=true, dendrogram=false)
Plots.@recipe function f(g::CovarianceImage;
palette = [:cornflowerblue, :crimson, :forestgreen],
colormode = :top, colorbargrad = :binary, donames = true,
docluster = true, verbose = true, dendrogram = false)
if g.args[1] isa AbstractFeatureArray || g.args[1] isa AbstractDimArray
f, Σ² = string.(getdim(g.args[1], 1)), g.args[1] |> Array
elseif length(g.args) == 2 && g.args[2] isa AbstractMatrix
Expand All @@ -38,11 +41,12 @@ Plots.@recipe function f(g::CovarianceImage; palette=[:cornflowerblue, :crimson,

issymmetric(Σ²) || (Σ² = cov(Σ²'))

any(diag(Σ²) .== 0) && @warn "Covariance matrix is not positive definite, which may cause an error"
any(diag(Σ²) .== 0) &&
@warn "Covariance matrix is not positive definite, which may cause an error"
linecolor --> nothing
if docluster == true
idxs = clustercovariance(Σ²).order
elseif docluster isa Union{AbstractVector,Tuple}
elseif docluster isa Union{AbstractVector, Tuple}
idxs = docluster # Precomputed indices
elseif docluster isa Clustering.Hclust
idxs = docluster.order
Expand All @@ -58,11 +62,14 @@ Plots.@recipe function f(g::CovarianceImage; palette=[:cornflowerblue, :crimson,
colorbar --> true
else
Ξ» = (eigvals ∘ Symmetric ∘ Array)(Ξ£Μ‚Β²)
Ξ»i = sortperm(abs.(Ξ»), rev=true)
Ξ»i = sortperm(abs.(Ξ»), rev = true)
Ξ» = Ξ»[Ξ»i]
P = (eigvecs ∘ Symmetric ∘ Array)(Ξ£Μ‚Β²)[:, Ξ»i] # Now sorted by decreasing eigenvalue norm
vidxs = sortperm(abs.(P[:, 1]), rev=true)
verbose && isnothing(printstyled("Feature weights:\n", color=:red, bold=true)) && display(vcat(hcat("Feature", ["PC$i" for i ∈ 1:N]...), hcat(fΜ‚[vidxs], round.(P[vidxs, 1:N], sigdigits=3))))
vidxs = sortperm(abs.(P[:, 1]), rev = true)
verbose &&
isnothing(printstyled("Feature weights:\n", color = :red, bold = true)) &&
display(vcat(hcat("Feature", ["PC$i" for i in 1:N]...),
hcat(fΜ‚[vidxs], round.(P[vidxs, 1:N], sigdigits = 3))))
P = abs.(P)
# if colormode isa Matrix # Supply custom coloring matrix. Should be an nfeatureΓ—ncolor matrix
# P = colormode
Expand All @@ -71,30 +78,30 @@ Plots.@recipe function f(g::CovarianceImage; palette=[:cornflowerblue, :crimson,
# 𝑓′ = parse.(Colors.XYZ, palette[1:N]);
if colormode == :top # * Color by the number of PC's given by the length of the color palette
P = P[:, 1:N]
PΜ‚ = P .^ 2.0 ./ sum(P .^ 2.0, dims=2)
PΜ‚ = P .^ 2.0 ./ sum(P .^ 2.0, dims = 2)
# Square the loadings, since they are added in quadrature. Maybe not a completely faithful representation of the PC proportions, but should get the job done.
𝑓′ = parse.(Colors.XYZ, palette[1:N])
elseif colormode == :all # * Color by all PC's. This can end up very brown
Ξ£Μ‚β€²Β² = Diagonal(abs.(Ξ»))
PΜ‚ = P .^ 2.0 ./ sum(P .^ 2.0, dims=2)
PΜ‚ = P .^ 2.0 ./ sum(P .^ 2.0, dims = 2)
p = fill(:black, size(P, 2))
p[1:N] = palette[1:N]
𝑓′ = parse.(Colors.XYZ, p)
[𝑓′[i] = Ξ£Μ‚β€²Β²[i, i] * 𝑓′[i] for i ∈ 1:length(𝑓′)]
[𝑓′[i] = Ξ£Μ‚β€²Β²[i, i] * 𝑓′[i] for i in 1:length(𝑓′)]
end
𝑓 = Vector{eltype(𝑓′)}(undef, size(PΜ‚, 1))
try # Load colors by PC weights
𝑓 = PΜ‚ * 𝑓′
catch
# Equivalent but slower
@info "Iterating to load covariances"
for ii ∈ 1:length(𝑓)
𝑓[ii] = sum([PΜ‚[ii, jj] * 𝑓′[jj] for jj ∈ 1:length(𝑓′)])
for ii in 1:length(𝑓)
𝑓[ii] = sum([PΜ‚[ii, jj] * 𝑓′[jj] for jj in 1:length(𝑓′)])
end
end

H = Array{Colors.XYZA}(undef, size(Ξ£Μ‚Β²))
for (i, j) ∈ Tuple.(CartesianIndices(H)) # Apply the correlations as transparencies
for (i, j) in Tuple.(CartesianIndices(H)) # Apply the correlations as transparencies
J = (𝑓[i] + 𝑓[j]) / 2
H[i, j] = Colors.XYZA(J.x, J.y, J.z, A[i, j])
end
Expand All @@ -106,7 +113,7 @@ Plots.@recipe function f(g::CovarianceImage; palette=[:cornflowerblue, :crimson,
seriescolor --> colorbargrad
end
if backend() == Plots.GRBackend() # For some reason, GR does heatmaps differently
xs = 0.5:1:size(H, 1)+0.5
xs = 0.5:1:(size(H, 1) + 0.5)
else
xs = 1:size(H, 1)
end
Expand All @@ -127,7 +134,7 @@ Plots.@recipe function f(g::CovarianceImage; palette=[:cornflowerblue, :crimson,
markercolor := colorbargrad
(zeros(2), zeros(2))
end
for i ∈ 1:N
for i in 1:N
@series begin
seriestype := :shape
if colormode != :raw
Expand Down
223 changes: 110 additions & 113 deletions src/metadata.jl
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
const featurenames = [
:DN_HistogramMode_5
:DN_HistogramMode_10
:CO_Embed2_Dist_tau_d_expfit_meandiff
:CO_f1ecac
:CO_FirstMin_ac
:CO_HistogramAMI_even_2_5
:CO_trev_1_num
:DN_OutlierInclude_p_001_mdrmd
:DN_OutlierInclude_n_001_mdrmd
:FC_LocalSimple_mean1_tauresrat
:FC_LocalSimple_mean3_stderr
:IN_AutoMutualInfoStats_40_gaussian_fmmi
:MD_hrv_classic_pnn40
:SB_BinaryStats_diff_longstretch0
:SB_BinaryStats_mean_longstretch1
:SB_MotifThree_quantile_hh
:SC_FluctAnal_2_rsrangefit_50_1_logi_prop_r1
:SC_FluctAnal_2_dfa_50_1_2_logi_prop_r1
:SP_Summaries_welch_rect_area_5_1
:SP_Summaries_welch_rect_centroid
:SB_TransitionMatrix_3ac_sumdiagcov
:PD_PeriodicityWang_th0_01]
const featurenames = [:DN_HistogramMode_5
:DN_HistogramMode_10
:CO_Embed2_Dist_tau_d_expfit_meandiff
:CO_f1ecac
:CO_FirstMin_ac
:CO_HistogramAMI_even_2_5
:CO_trev_1_num
:DN_OutlierInclude_p_001_mdrmd
:DN_OutlierInclude_n_001_mdrmd
:FC_LocalSimple_mean1_tauresrat
:FC_LocalSimple_mean3_stderr
:IN_AutoMutualInfoStats_40_gaussian_fmmi
:MD_hrv_classic_pnn40
:SB_BinaryStats_diff_longstretch0
:SB_BinaryStats_mean_longstretch1
:SB_MotifThree_quantile_hh
:SC_FluctAnal_2_rsrangefit_50_1_logi_prop_r1
:SC_FluctAnal_2_dfa_50_1_2_logi_prop_r1
:SP_Summaries_welch_rect_area_5_1
:SP_Summaries_welch_rect_centroid
:SB_TransitionMatrix_3ac_sumdiagcov
:PD_PeriodicityWang_th0_01]

const catch24_featurenames = [featurenames..., :DN_Mean, :DN_Spread_Std]

Expand Down Expand Up @@ -51,51 +50,50 @@ const short_featurenames = [
const short_catch24_featurenames = [short_featurenames..., :mean, :std]

const featuretypes = Dict(featurenames .=> [
#DN_HistogramMode_5
Cdouble
#DN_HistogramMode_10
Cdouble
#CO_Embed2_Dist_tau_d_expfit_meandiff
Cdouble
#CO_f1ecac
Cdouble
#CO_FirstMin_ac
Cint
#CO_HistogramAMI_even_2_5
Cdouble
#CO_trev_1_num
Cdouble
#DN_OutlierInclude_p_001_mdrmd
Cdouble
#DN_OutlierInclude_n_001_mdrmd
Cdouble
#FC_LocalSimple_mean1_tauresrat
Cdouble
#FC_LocalSimple_mean3_stderr
Cdouble
#IN_AutoMutualInfoStats_40_gaussian_fmmi
Cdouble
#MD_hrv_classic_pnn40
Cdouble
#SB_BinaryStats_diff_longstretch0
Cdouble
#SB_BinaryStats_mean_longstretch1
Cdouble
#SB_MotifThree_quantile_hh
Cdouble
#SC_FluctAnal_2_rsrangefit_50_1_logi_prop_r1
Cdouble
#SC_FluctAnal_2_dfa_50_1_2_logi_prop_r1
Cdouble
#SP_Summaries_welch_rect_area_5_1
Cdouble
#SP_Summaries_welch_rect_centroid
Cdouble
#SB_TransitionMatrix_3ac_sumdiagcov
Cdouble
#PD_PeriodicityWang_th0_01
Cint])

#DN_HistogramMode_5
Cdouble
#DN_HistogramMode_10
Cdouble
#CO_Embed2_Dist_tau_d_expfit_meandiff
Cdouble
#CO_f1ecac
Cdouble
#CO_FirstMin_ac
Cint
#CO_HistogramAMI_even_2_5
Cdouble
#CO_trev_1_num
Cdouble
#DN_OutlierInclude_p_001_mdrmd
Cdouble
#DN_OutlierInclude_n_001_mdrmd
Cdouble
#FC_LocalSimple_mean1_tauresrat
Cdouble
#FC_LocalSimple_mean3_stderr
Cdouble
#IN_AutoMutualInfoStats_40_gaussian_fmmi
Cdouble
#MD_hrv_classic_pnn40
Cdouble
#SB_BinaryStats_diff_longstretch0
Cdouble
#SB_BinaryStats_mean_longstretch1
Cdouble
#SB_MotifThree_quantile_hh
Cdouble
#SC_FluctAnal_2_rsrangefit_50_1_logi_prop_r1
Cdouble
#SC_FluctAnal_2_dfa_50_1_2_logi_prop_r1
Cdouble
#SP_Summaries_welch_rect_area_5_1
Cdouble
#SP_Summaries_welch_rect_centroid
Cdouble
#SB_TransitionMatrix_3ac_sumdiagcov
Cdouble
#PD_PeriodicityWang_th0_01
Cint])

"""
Catch22.featurekeywords
Expand Down Expand Up @@ -147,53 +145,52 @@ const featurekeywords = [ # See hctsa
#PD_PeriodicityWang_th0_01
["periodicity", "spline"]]


"""
Catch22.featuredescriptions
A vector listing short descriptions of each feature, as strings.
"""
const featuredescriptions = [ # See catch22 paper
#DN_HistogramMode_5
"Mode of z-scored distribution (5-bin histogram)"
#DN_HistogramMode_10
"Mode of z-scored distribution (10-bin histogram)"
#CO_Embed2_Dist_tau_d_expfit_meandiff
"Exponential fit to successive distances in 2-d embedding space"
#CO_f1ecac
"First 1/𝑒 crossing of autocorrelation function"
#CO_FirstMin_ac
"First minimum of autocorrelation function"
#CO_HistogramAMI_even_2_5
"Automutual information, π‘š=2,𝜏=5"
#CO_trev_1_num
"Time-reversibility statistic, ⟨(π‘₯β‚œβ‚Šβ‚βˆ’π‘₯β‚œ)Β³βŸ©β‚œ"
#DN_OutlierInclude_p_001_mdrmd
"Time intervals between successive extreme events above the mean"
#DN_OutlierInclude_n_001_mdrmd
"Time intervals between successive extreme events below the mean"
#FC_LocalSimple_mean1_tauresrat
"Change in correlation length after iterative differencing"
#FC_LocalSimple_mean3_stderr
"Mean error from a rolling 3-sample mean forecasting"
#IN_AutoMutualInfoStats_40_gaussian_fmmi
"First minimum of the automutual information function"
#MD_hrv_classic_pnn40
"Proportion of successive differences exceeding 0.04𝜎 (Mietus 2002)"
#SB_BinaryStats_diff_longstretch0
"Longest period of successive incremental decreases"
#SB_BinaryStats_mean_longstretch1
"Longest period of consecutive values above the mean"
#SB_MotifThree_quantile_hh
"Shannon entropy of two successive letters in equiprobable 3-letter symbolization"
#SC_FluctAnal_2_rsrangefit_50_1_logi_prop_r1
"Proportion of slower timescale fluctuations that scale with linearly rescaled range fits"
#SC_FluctAnal_2_dfa_50_1_2_logi_prop_r1
"Proportion of slower timescale fluctuations that scale with DFA (50% sampling)"
#SP_Summaries_welch_rect_area_5_1
"Total power in lowest fifth of frequencies in the Fourier power spectrum"
#SP_Summaries_welch_rect_centroid
"Centroid of the Fourier power spectrum"
#SB_TransitionMatrix_3ac_sumdiagcov
"Trace of covariance of transition matrix between symbols in 3-letter alphabet"
#PD_PeriodicityWang_th0_01
"Periodicity measure of (Wang et al. 2007)"]
#DN_HistogramMode_5
"Mode of z-scored distribution (5-bin histogram)"
#DN_HistogramMode_10
"Mode of z-scored distribution (10-bin histogram)"
#CO_Embed2_Dist_tau_d_expfit_meandiff
"Exponential fit to successive distances in 2-d embedding space"
#CO_f1ecac
"First 1/𝑒 crossing of autocorrelation function"
#CO_FirstMin_ac
"First minimum of autocorrelation function"
#CO_HistogramAMI_even_2_5
"Automutual information, π‘š=2,𝜏=5"
#CO_trev_1_num
"Time-reversibility statistic, ⟨(π‘₯β‚œβ‚Šβ‚βˆ’π‘₯β‚œ)Β³βŸ©β‚œ"
#DN_OutlierInclude_p_001_mdrmd
"Time intervals between successive extreme events above the mean"
#DN_OutlierInclude_n_001_mdrmd
"Time intervals between successive extreme events below the mean"
#FC_LocalSimple_mean1_tauresrat
"Change in correlation length after iterative differencing"
#FC_LocalSimple_mean3_stderr
"Mean error from a rolling 3-sample mean forecasting"
#IN_AutoMutualInfoStats_40_gaussian_fmmi
"First minimum of the automutual information function"
#MD_hrv_classic_pnn40
"Proportion of successive differences exceeding 0.04𝜎 (Mietus 2002)"
#SB_BinaryStats_diff_longstretch0
"Longest period of successive incremental decreases"
#SB_BinaryStats_mean_longstretch1
"Longest period of consecutive values above the mean"
#SB_MotifThree_quantile_hh
"Shannon entropy of two successive letters in equiprobable 3-letter symbolization"
#SC_FluctAnal_2_rsrangefit_50_1_logi_prop_r1
"Proportion of slower timescale fluctuations that scale with linearly rescaled range fits"
#SC_FluctAnal_2_dfa_50_1_2_logi_prop_r1
"Proportion of slower timescale fluctuations that scale with DFA (50% sampling)"
#SP_Summaries_welch_rect_area_5_1
"Total power in lowest fifth of frequencies in the Fourier power spectrum"
#SP_Summaries_welch_rect_centroid
"Centroid of the Fourier power spectrum"
#SB_TransitionMatrix_3ac_sumdiagcov
"Trace of covariance of transition matrix between symbols in 3-letter alphabet"
#PD_PeriodicityWang_th0_01
"Periodicity measure of (Wang et al. 2007)"]
Loading

0 comments on commit e5aa824

Please sign in to comment.