Skip to content

Commit

Permalink
Merge pull request #53 from Marco-Congedo/dev
Browse files Browse the repository at this point in the history
v0.1.11
  • Loading branch information
Marco-Congedo authored Mar 19, 2020
2 parents 5bc7e2e + 6937bef commit 74d69d9
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Diagonalizations"
uuid = "9cd687f3-b62d-43f3-8fd3-ffcd9e581047"
authors = ["Marco-Congedo <[email protected]>"]
version = "0.1.10"
version = "0.1.11"

[deps]
CovarianceEstimation = "587fd27a-f159-11e8-2dae-1979310e6154"
Expand Down
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
authors = ["Marco-Congedo <[email protected]>"]
version = "0.1.10"
version = "0.1.11"

[deps]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Expand Down
18 changes: 17 additions & 1 deletion src/ajd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,10 @@ function ajd(𝐂::ℍVector;
eVarMeth :: Function = searchsortedfirst,
simple :: Bool = false)

eVar isa Int && eVarC isa Int && eVar>eVarC && throw(ArgumentError(📌*", ajd function: `eVar` cannot be larger than `eVarC`"))
args=("Approximate Joint Diagonalization", false)
k, n=length(𝐂), size(𝐂[1], 1)
if simple whitening=false end
simple && (whitening=false)

if algorithm == :QNLogLike
U, V, λ, iter, conv=qnLogLike(𝐂;
Expand Down Expand Up @@ -438,6 +439,13 @@ function ajd(𝐂::ℍVector;
throw(ArgumentError(📌*", ajd constructor: invalid `algorithm` argument: $algorithm"))
end

# alert user if eVar passed as an integer exceeds the minimum whitening dim
whiteDim=size(U, 2)
if eVar isa Int && eVar>whiteDim
@warn(📌*", ajd function: the whitening step reduced the rank to $(whiteDim); `eVar` has been lowered to this value.")
eVar=whiteDim
end

λ = _checkλ(λ) # make sure no imaginary noise is present (for complex data)

simple ? LF(U, V, Diagonal(λ), ○, ○, ○, args...) :
Expand Down Expand Up @@ -696,6 +704,7 @@ function majd(𝑿::VecVecMat;

if dims===○ dims=_set_dims(𝑿) end
#TODO _check_data(𝑿, dims, covEst, meanX, ○)===○ && return
eVar isa Int && eVarC isa Int && eVar>eVarC && throw(ArgumentError(📌*", majd function: `eVar` cannot be larger than `eVarC`"))
(n, t)=dims==1 ? reverse(size(𝑿[1][1])) : size(𝑿[1][1])
k, m=length(𝑿), length(𝑿[1])
args=("Multiple Approximate Joint Diagonalization", false)
Expand All @@ -711,6 +720,13 @@ function majd(𝑿::VecVecMat;
throw(ArgumentError(📌*", majd constructor: invalid `algorithm` argument"))
end

# alert user if eVar passed as an integer exceeds the minimum whitening dim
whiteDim=size(𝐔[1], 2)
if eVar isa Int && eVar>whiteDim
@warn(📌*", majd function: the whitening step reduced the rank to $(whiteDim); `eVar` has been lowered to this value.")
eVar=whiteDim
end

λ = _checkλ(λ) # make sure no imaginary noise is present (for complex data)

simple ? LF(𝐔, 𝐕, Diagonal(λ), ○, ○, ○, args...) :
Expand Down
12 changes: 12 additions & 0 deletions src/cca.jl
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,23 @@ function cca(Cx :: SorH, Cy :: SorH, Cxy :: Mat;

size(Cx, 1)==size(Cxy, 1) || throw(ArgumentError(📌*", cca function: Matrices `Cx` and `Cxy` must have the same number of columns"))
size(Cy, 2)==size(Cxy, 2) || throw(ArgumentError(📌*", cca function: Matrices `Cy` and `Cxy` must have the same number of rows"))
if eVar isa Int
eVarCx isa Int && eVar>eVarCx && throw(ArgumentError(📌*", cca function: `eVar` cannot be larger than `eVarCx`"))
eVarCy isa Int && eVar>eVarCy && throw(ArgumentError(📌*", cca function: `eVar` cannot be larger than `eVarCy`"))
end

args=("Canonical Correlation Analysis", false)

x=whitening(Cx; eVar=eVarCx, eVarMeth=eVarMeth)
y=whitening(Cy; eVar=eVarCy, eVarMeth=eVarMeth)

# alert user if eVar passed as an integer exceeds the minimum whitening dim
whiteDim=min(size(x.F, 2), size(y.F, 2))
if eVar isa Int && eVar>whiteDim
@warn(📌*", cca function: the whitening step reduced the rank to $(whiteDim); `eVar` has been lowered to this value.")
eVar=whiteDim
end

m=mca(x.F'*Cxy*y.F; eVar=eVar, eVarMeth=eVarMeth)

if simple
Expand Down
29 changes: 22 additions & 7 deletions src/csp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ used to compute it (see above).
for defining the [subspace dimension](@ref) ``p``. Particularly:
- By default, the two-step procedure described above is used to find the
solution. In this case `eVarC` is used for defining the subspace dimension of
the whitening step. If `eVarC=0.` is passed,
the solution will be find by the generalized eigenvalue-eigenvector procedure.
the whitening step. If `eVarC=0.0` is passed (not to be confused with
`eVarC=0` ), the solution will be find by the generalized
eigenvalue-eigenvector procedure.
- `eVar` is the keyword optional argument for defining the
[subspace dimension](@ref) ``p`` using the `.arev` vector
given by [csp.5].
Expand Down Expand Up @@ -281,10 +282,11 @@ function csp(C₁ :: SorH, C₂ :: SorH;
selMeth :: Symbol = :extremal,
simple :: Bool = false)

#println(eVar)
# checks
size(C₁, 1)==size(C₁, 2) || throw(ArgumentError(📌*", csp function: Matrix `C₁` must be square"))
size(C₂, 1)==size(C₂, 2) || throw(ArgumentError(📌*", csp function: Matrix `C₂` must be square"))
size(C₁)==size(C₂) || throw(ArgumentError(📌*", csp function: Matrices `C₁` and `C₂` must have the same size"))
eVar isa Int && eVarC isa Int && eVar>eVarC && throw(ArgumentError(📌*", csp function: `eVar` cannot be larger than `eVarC`"))

args=("Common Spatial Pattern", false)

Expand All @@ -298,7 +300,14 @@ function csp(C₁ :: SorH, C₂ :: SorH;
LF(U, pinv(U), D, eVar, λ, arev, args...)
end
else
w=whitening(C₁+C₂; eVar=eVarC, eVarMeth=eVarMeth, simple=true)
w=whitening(C₁+C₂; eVar=eVarC, eVarMeth=eVarMeth)

# alert user if eVar passed as an integer exceeds the whitening dim
whiteDim=size(w.F, 2)
if eVar isa Int && eVar>whiteDim
@warn(📌*", csp function: the whitening step reduced the rank to $(whiteDim); `eVar` has been lowered to this value.")
eVar=whiteDim
end

λ, U = eig(Hermitian(w.F'*C₁*w.F)) # get evd of whitened C1
# Hermitian is necessary for complex data
Expand Down Expand Up @@ -575,19 +584,25 @@ function cstp( X :: Mat, C₍₁₎ :: SorH, C₍₂₎ :: SorH;

d₍₂₎, d₍₁₎, dₓ=size(C₍₂₎, 1), size(C₍₁₎, 1), size(X)
(d₍₁₎==dₓ[2] && d₍₂₎==dₓ[1]) || throw(ArgumentError(📌*", cstp function: For n⋅m matrix X, matrix C₍₁₎ must be m⋅m and matrix C₍₂₎ n⋅n"))
eVar isa Int && eVarC isa Int && eVar>eVarC && throw(ArgumentError(📌*", cstp function: `eVar` cannot be larger than `eVarC`"))
args=("Common Spatio-Temporal Pattern", false)
kwargs=(eVar=eVarC, eVarMeth=eVarMeth, simple=false)

kwargs=(eVar=eVarC, eVarMeth=eVarMeth, simple=false)
t=whitening(C₍₁₎; kwargs...)
s=whitening(C₍₂₎; kwargs...)

# alert user if eVar passed as an integer exceeds the minimum whitening dim
whiteDim=min(size(t.F, 2), size(t.F, 2))
if eVar isa Int && eVar>whiteDim
@warn(📌*", cstp function: the whitening step reduced the rank to $(whiteDim); `eVar` has been lowered to this value.")
eVar=whiteDim
end

U, λ, V = svd(s.F'*X*t.F; full=true)
λ = _checkλ(λ) # make sure no imaginary noise is present (for complex data)

simple ? LF([s.F*U, t.F*V], [U'*s.iF, V'*t.iF], Diagonal(λ), ○, ○, ○, args...) :
begin
#eVar===○ ? eVar=(2*norm(X)^2)/(tr(C₍₁₎)*size(X, 2)+tr(C₍₂₎)*size(X, 1)) : ○
#println(eVar)
eVar, D, U, V, p, arev=_ssdcstp!(eVar, λ, U, Matrix(V), _minDim(X), eVarMeth) # subspace dimension
LF([s.F*U, t.F*V], [U'*s.iF, V'*t.iF], D, eVar, λ, arev, args...)
end
Expand Down

0 comments on commit 74d69d9

Please sign in to comment.