Skip to content

Commit c5bafc1

Browse files
committed
WIP: updated defaults
1 parent ed15296 commit c5bafc1

File tree

4 files changed

+52
-38
lines changed

4 files changed

+52
-38
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: ANTsR
22
Type: Package
33
Title: ANTs in R: Quantification Tools for Biomedical Images
4-
Version: 0.5.7.0
4+
Version: 0.5.7.1
55
Date: 2020-05-20
66
Authors@R: c(
77
person(c("Brian", "B"), "Avants", role = c("aut", "cre"), email = "[email protected]"),

R/multiscaleSVDxpts.R

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,15 +2441,20 @@ mild <- function( dataFrame, voxmats, basisK,
24412441
#' k = 2, uAlgorithm = 'pca' )
24422442
#'
24432443
#' @export
2444-
initializeSimlr <- function( voxmats, k, jointReduction = TRUE,
2444+
initializeSimlr <- function( voxmats, k, jointReduction = FALSE,
24452445
zeroUpper = FALSE, uAlgorithm = 'svd', addNoise = 0 ) {
24462446
nModalities = length( voxmats )
2447+
localAlgorithm = uAlgorithm
2448+
if ( uAlgorithm == 'avg' ) {
2449+
jointReduction = FALSE
2450+
localAlgorithm = 'pca'
2451+
}
24472452
if ( uAlgorithm == 'randomProjection' & jointReduction )
24482453
jointReduction = FALSE
24492454
if ( jointReduction ) {
24502455
X <- Reduce( cbind, voxmats ) # bind all matrices
2451-
if ( uAlgorithm == 'pca' ) {
2452-
X.pcr <- stats::prcomp( t(X), rank. = k ) # PCA
2456+
if ( uAlgorithm == 'pca' | uAlgorithm == 'svd' ) {
2457+
X.pcr <- stats::prcomp( t(X), rank. = k, scale. = uAlgorithm == 'svd' ) # PCA
24532458
u <- ( X.pcr$rotation )
24542459
} else if ( uAlgorithm == 'ica' ) {
24552460
u = t( fastICA::fastICA( t(X), method = 'C', n.comp=k )$A )
@@ -2468,18 +2473,19 @@ initializeSimlr <- function( voxmats, k, jointReduction = TRUE,
24682473
uOut = list()
24692474
uRand = replicate(k, rnorm(nrow(voxmats[[1]])))
24702475
for ( s in 1:nModalities) {
2471-
if ( uAlgorithm == 'pca ') {
2472-
uOut[[s]] = t( stats::prcomp( t(voxmats[[s]]), rank. = k )$rotation )
2473-
} else if ( uAlgorithm == 'ica' ) {
2474-
uOut[[s]] = t( fastICA::fastICA( t(voxmats[[s]]), method = 'C', n.comp=k )$A )
2475-
} else if ( uAlgorithm == 'cca' ) {
2476-
uOut[[s]] = sparseDecom2( list(voxmats[[1]], voxmats[[s]]),
2476+
X = Reduce( cbind, voxmats[ -s ] )
2477+
if ( localAlgorithm == 'pca ' | localAlgorithm == 'svd') {
2478+
uOut[[s]] = ( stats::prcomp( t( X ), rank. = k, scale. = uAlgorithm == 'svd' )$rotation )
2479+
} else if ( localAlgorithm == 'ica' ) {
2480+
uOut[[s]] = t( fastICA::fastICA( t( X ), method = 'C', n.comp=k )$A )
2481+
} else if ( localAlgorithm == 'cca' ) {
2482+
uOut[[s]] = sparseDecom2( list( X, voxmats[[s]]),
24772483
sparseness = c(0.5,0.5), nvecs = k, its = 3, ell1=0.1 )$projections2
2478-
} else if ( uAlgorithm == 'randomProjection' ) {
2484+
} else if ( localAlgorithm == 'randomProjection' ) {
24792485
uOut[[s]] = t( ( t(uRand) %*% voxmats[[s]] ) %*% t( voxmats[[s]] ) )
24802486
uOut[[s]] = uOut[[s]] / norm( uOut[[s]], "F" )
24812487
} else {
2482-
uOut[[s]] = replicate(k, rnorm(nrow(voxmats[[1]])))
2488+
uOut[[s]] = ( stats::prcomp( t( X ), rank. = k, scale. = uAlgorithm == 'svd' )$rotation )
24832489
}
24842490
if ( addNoise > 0 ) uOut[[s]] = uOut[[s]] + replicate(k, rnorm(nrow(voxmats[[1]]))) * addNoise
24852491
if ( zeroUpper ) uOut[[s]][upper.tri(uOut[[s]])] <- 0
@@ -2683,25 +2689,25 @@ simlr <- function(
26832689
sparsenessQuantiles,
26842690
positivities,
26852691
initialUMatrix,
2686-
mixAlg = c( 'svd', 'ica', 'avg', 'rrpca-l', 'rrpca-s', 'pca', 'stochastic' ),
2692+
mixAlg = c( 'svd', 'ica', 'avg', 'rrpca-l', 'rrpca-s', 'pca', 'stochastic' ),
26872693
orthogonalize = FALSE,
26882694
repeatedMeasures = NA,
26892695
lineSearchRange = c( -1e10, 1e10 ),
26902696
lineSearchTolerance = 1e-8,
26912697
randomSeed,
2692-
constraint = c("Grassmann","none","Stiefel"),
2693-
energyType = c('regression', 'normalized', 'cca', 'ucca', 'lowRank'),
2698+
constraint = c("none","Grassmann","Stiefel"),
2699+
energyType = c('cca','regression', 'normalized', 'ucca', 'lowRank'),
26942700
vmats,
26952701
connectors = NULL,
26962702
optimizationStyle = c("lineSearch","mixed","greedy") ,
26972703
scale = c( 'sqrtnp', 'np', 'centerAndScale', 'center', 'norm', 'none', 'impute', 'eigenvalue', 'robust'),
26982704
expBeta = 0.0,
26992705
verbose = FALSE ) {
2700-
if ( missing( scale ) ) scale = c( "centerAndScale", "np" )
2701-
if ( missing( energyType ) ) energyType = "regression"
2702-
if ( missing( mixAlg ) ) mixAlg = "ica"
2706+
if ( missing( scale ) ) scale = c( "centerAndScale" )
2707+
if ( missing( energyType ) ) energyType = "cca"
2708+
if ( missing( mixAlg ) ) mixAlg = "svd"
27032709
if ( missing( optimizationStyle ) ) optimizationStyle = "lineSearch"
2704-
if ( ! missing( "randomSeed" ) ) set.seed( randomSeed )
2710+
if ( ! missing( "randomSeed" ) ) set.seed( randomSeed ) # else set.seed( 0 )
27052711
energyType = match.arg(energyType)
27062712
constraint = match.arg(constraint)
27072713
optimizationStyle = match.arg(optimizationStyle)
@@ -2717,6 +2723,19 @@ simlr <- function(
27172723
}
27182724
# \sum_i \| X_i - \sum_{ j ne i } u_j v_i^t \|^2 + \| G_i \star v_i \|_1
27192725
# \sum_i \| X_i - \sum_{ j ne i } u_j v_i^t - z_r v_r^ T \|^2 + constraints
2726+
#
2727+
constrainG <- function( vgrad, i, constraint ) {
2728+
if ( constraint == "Grassmann") {
2729+
# grassmann manifold - see https://stats.stackexchange.com/questions/252633/optimization-with-orthogonal-constraints
2730+
# Edelman, A., Arias, T. A., & Smith, S. T. (1998). The geometry of algorithms with orthogonality constraints. SIAM journal on Matrix Analysis and Applications, 20(2), 303-353.
2731+
projjer = diag( ncol( vgrad ) ) - t( vmats[[i]] ) %*% vmats[[i]]
2732+
return( vgrad %*% projjer )
2733+
}
2734+
if ( constraint == "Stiefel" ) # stiefel manifold
2735+
vgrad = vgrad - vmats[[i]] %*% ( t( vgrad ) %*% ( vmats[[i]] ) )
2736+
return( vgrad )
2737+
}
2738+
27202739
normalized = FALSE
27212740
ccaEnergy = FALSE
27222741
nModalities = length( voxmats )
@@ -2827,6 +2846,8 @@ simlr <- function(
28272846
initialUMatrix = list( )
28282847
for ( i in 1:nModalities )
28292848
initialUMatrix[[ i ]] = randmat
2849+
} else if ( class(initialUMatrix)[1] == 'numeric' ) {
2850+
initialUMatrix = initializeSimlr( voxmats, initialUMatrix, uAlgorithm=mixAlg, jointReduction = FALSE )
28302851
}
28312852

28322853
if ( length( initialUMatrix ) != nModalities &
@@ -2846,9 +2867,12 @@ simlr <- function(
28462867
if ( verbose ) print(" <0> BUILD-V <0> BUILD-V <0> BUILD-V <0> BUILD-V <0> ")
28472868
vmats = list()
28482869
for ( i in 1:nModalities ) {
2849-
# vmats[[ i ]] = t(voxmats[[i]]) %*% initialUMatrix[[i]]
2850-
vmats[[ i ]] = matrix( rnorm( ncol( voxmats[[i]] ) * basisK ), ncol = basisK )
2851-
vmats[[ i ]] = vmats[[ i ]] / norm( vmats[[ i ]], "F" )
2870+
vmats[[ i ]] = t(voxmats[[i]]) %*% initialUMatrix[[i]]
2871+
# 0 # svd( temp, nu=basisK, nv=0 )$u
2872+
# for ( kk in 1:nModalities ) vmats[[ i ]] = vmats[[ i ]] + t(voxmats[[i]]) %*% initialUMatrix[[kk]]
2873+
# vmats[[ i ]] = # svd( vmats[[ i ]], nu=basisK, nv=0 )$u
2874+
# ( stats::prcomp( vmats[[ i ]], retx=TRUE, rank.=basisK, scale.=TRUE )$x )
2875+
# vmats[[ i ]] = vmats[[ i ]] / norm( vmats[[ i ]], "F" )
28522876
}
28532877
}
28542878

@@ -2867,7 +2891,7 @@ simlr <- function(
28672891
as.matrix( smoothingMatrices[[whichModality]] %*% myenergysearchv ),
28682892
sparsenessQuantiles[whichModality],
28692893
orthogonalize = FALSE,
2870-
positivity = positivities[whichModality], softThresholding = FALSE )
2894+
positivity = positivities[whichModality], softThresholding = TRUE )
28712895

28722896
if ( ccaEnergy ) {
28732897
#( v'*X'*Y )/( norm2(X*v ) * norm2( u ) )
@@ -2913,17 +2937,6 @@ simlr <- function(
29132937
totalEnergy = c( initialEnergy )
29142938
if ( verbose ) print( paste( "initialDataTerm:", initialEnergy,
29152939
" <o> mixer:", mixAlg, " <o> E: ", energyType ) )
2916-
constrainG <- function( vgrad, i, constraint ) {
2917-
if ( constraint == "Grassmann") {
2918-
# grassmann manifold - see https://stats.stackexchange.com/questions/252633/optimization-with-orthogonal-constraints
2919-
# Edelman, A., Arias, T. A., & Smith, S. T. (1998). The geometry of algorithms with orthogonality constraints. SIAM journal on Matrix Analysis and Applications, 20(2), 303-353.
2920-
projjer = diag( ncol( vgrad ) ) - t( vmats[[i]] ) %*% vmats[[i]]
2921-
return( vgrad %*% projjer )
2922-
}
2923-
if ( constraint == "Stiefel" ) # stiefel manifold
2924-
vgrad = vgrad - vmats[[i]] %*% ( t( vgrad ) %*% ( vmats[[i]] ) )
2925-
return( vgrad )
2926-
}
29272940

29282941
getSyMGnorm <- function( v, i, myw, mixAlg ) {
29292942
# norm2( X/norm2(X) - U * V'/norm2(U * V') )^2
@@ -3113,7 +3126,7 @@ simlr <- function(
31133126
sparsenessQuantiles[i],
31143127
orthogonalize = FALSE, positivity = positivities[i],
31153128
unitNorm = FALSE,
3116-
softThresholding = FALSE )
3129+
softThresholding = TRUE )
31173130
if ( normalized ) vmats[[i]] = vmats[[i]] / norm( vmats[[i]], "F" )
31183131
}
31193132
if ( ccaEnergy ) {
@@ -3220,6 +3233,7 @@ simlr <- function(
32203233
#'
32213234
#' @seealso \code{\link{simlr}}
32223235
#' @export
3236+
32233237
simlrU <- function( projections, mixingAlgorithm, initialW,
32243238
orthogonalize = FALSE, connectors = NULL ) {
32253239
# some gram schmidt code

man/initializeSimlr.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/simlr.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)