-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKINOMOStrategyIterative-class.R
640 lines (508 loc) · 20.4 KB
/
KINOMOStrategyIterative-class.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
#' @include KINOMOStrategy-class.R
#' @include KINOMOfit-class.R
NULL
# Define union class for generalised function slots, e.g., slot 'KINOMOStrategyIterative::Stop'
setClassUnion('.GfunctionSlotNULL', c('character', 'integer', 'numeric', 'function', 'NULL'))
#'
setClass('KINOMOStrategyIterative'
, representation(
onInit = '.functionSlotNULL',
Update = '.functionSlot', # update method
Stop = '.GfunctionSlotNULL', # method called just after the update
onReturn = '.functionSlotNULL' # method called just before returning the resulting KINOMO object
)
, prototype=prototype(
onInit = NULL
, Update = ''
, Stop = NULL
, onReturn = NULL
)
, contains = 'KINOMOStrategy'
, validity = function(object){
if( is.character(object@Update) && object@Update == '' )
return("Slot 'Update' is required")
# check the arguments of methods 'Update' and 'Stop'
# (except for the 3 mandatory ones)
n.update <- names(formals(object@Update))
# at least 3 arguments for 'Update'
if( length(n.update) < 3 ){
return(str_c("Invalid 'Update' method - must have at least 3 arguments: ",
"current iteration number [i], ",
"target matrix [y], ",
"current KINOMO model iterate [x]"))
}
n.update <- n.update[-seq(3)]
# argument '...' must be present in method 'Update'
if( !is.element('...', n.update) )
return("Invalid 'Update' method: must have argument '...' (even if not used)")
# at least 3 arguments for 'Stop'
if( !is.null(object@Stop) ){
# retrieve the stopping criterion and check its intrinsic validity
.stop <- tryCatch( KINOMOStop(object@Stop, check=TRUE),
error = function(e) return(message(e)))
# Update and Stop methods cannot have overlapping arguments
n.stop <- names(formals(.stop))
overlap <- intersect(n.update, n.stop)
overlap <- overlap[which(overlap!='...')]
if( length(overlap) > 0 ){
return(str_c("Invalid 'Update' and 'Stop' methods: conflicting arguments ",
str_out(overlap, Inf)))
}
}
TRUE
}
)
#' Show method for objects of class \code{KINOMOStrategyIterative}
#' @export
setMethod('show', 'KINOMOStrategyIterative',
function(object){
#cat('<object of class: KINOMOStrategyIterative>')
callNextMethod()
cat(" <Iterative schema>\n")
# go through the slots
s.list <- names(getSlots('KINOMOStrategyIterative'))
s.list <- setdiff(s.list, names(getSlots('KINOMOStrategy')))
#s.list <- s.list[s.list=='ANY']
# s.list <- c('Update', 'Stop', 'WrapKINOMO')
out <-
sapply(s.list, function(sname){
svalue <- slot(object,sname)
svalue <-
if( is.function(svalue) ) {
str_args(svalue, exdent=12)
} else if( is.null(svalue) ){
'none'
} else {
paste("'", svalue,"'", sep='')
}
str_c(sname, ": ", svalue)
})
cat(str_c(' ', out, collapse='\n'), "\n", sep='')
return(invisible())
}
)
###% This class is an auxiliary class that defines the strategy's methods by directly callable functions.
setClass('KINOMOStrategyIterativeX'
, contains = 'KINOMOStrategyIterative'
, representation = representation(
workspace = 'environment' # workspace to use persistent variables accross methods
)
)
###% Creates a KINOMOStrategyIterativeX object from a KINOMOStrategyIterative object.
xifyStrategy <- function(strategy, workspace=new.env(emptyenv())){
# do nothing if already executable
if( is(strategy, 'KINOMOStrategyIterativeX') ) return(strategy)
# first check the strategy's validity
if( is.character(err <- validObject(strategy, test=TRUE)) ){
stop("Invalid strategy definition:\n\t- ", err)
}
# intanciate the KINOMOStrategyIterativeX, creating the strategy's workspace
strategyX <- new('KINOMOStrategyIterativeX', strategy, workspace=workspace)
# define auxiliary function to preload the 'function' slots in class KINOMOStrategyIterativeX
preload.slot <- function(strategy, sname, default){
# get the content of the slot
svalue <- slot(strategy,sname)
# if the slot is valid (i.e. it's a non-empty character string), then process the name into a valid function
fun <-
if( is.null(svalue) && !missing(default) ) default
else if( sname == 'Stop' ) KINOMOStop(svalue)
else if( is.character(svalue) && nchar(svalue) > 0 ){
# set the slot with the executable version of the function
getFunction(svalue)
}else if( is.function(svalue) ) svalue
else
stop("KINOMOStrategyIterativeX - could not pre-load slot '", sname, "'")
# return the loaded function
fun
}
# preload the function slots
slot(strategyX, 'Update') <- preload.slot(strategyX, 'Update')
slot(strategyX, 'Stop') <- preload.slot(strategyX, 'Stop', function(strategy, i, target, data, ...){FALSE})
slot(strategyX, 'onReturn') <- preload.slot(strategyX, 'onReturn', identity)
# load the objective function
objective(strategyX) <- KINOMODistance(objective(strategy))
# valid the preloaded object
validObject(strategyX)
# return the executable strategy
strategyX
}
staticVar <- local({
.Workspace <- NULL
function(name, value, init=FALSE){
# return last workspace
if( missing(name) ) return(.Workspace)
else if( is.null(name) ){ # reset workspace
.Workspace <<- NULL
return()
} else if( is.environment(name) ){ # setup up static environment
KINOMO.debug('Strategy Workspace', "initialize static workspace: ", capture.output(.Workspace), "=", capture.output(name))
.Workspace <<- name
}else if( isString(name) && is.environment(.Workspace) ){
if( missing(value) ){
get(name, envir=.Workspace, inherits=FALSE)
}else{
if( !init || !exists(name, envir=.Workspace, inherits=FALSE) )
{
if( init ) KINOMO.debug('Strategy Workspace', "initialize variable '", name, "'")
assign(name, value, envir=.Workspace)
}
# return current value
get(name, envir=.Workspace, inherits=FALSE)
}
}else{
stop("Invalid KINOMO workspace query: .Workspace=", class(.Workspace), '| name=', name
, if( !missing(value) ) paste0(' | value=', class(value)))
}
}
})
setMethod('run', signature(object='KINOMOStrategyIterative', y='matrix', x='KINOMOfit'),
function(object, y, x, .stop=NULL, maxIter = KINOMO.getOption('maxIter') %||% 2000L, ...){
method <- object
# override the stop method on runtime
if( !is.null(.stop) ){
method@Stop <- KINOMOStop(.stop)
# honour maxIter unless .stop is an integer and maxIter is not passed
# either directly or from initial call
# NB: maxIter may be not missing in the call to run() due to the application
# of default arguments from the Strategy within KINOMO(), in which case one does not
# want to honour it, since it is effectively missing in the original call.
if( is.integer(.stop) && (missing(maxIter) || !('maxIter' %in% names(x@call))) )
maxIter <- .stop[1]
}
# debug object in debug mode
if( KINOMO.getOption('debug') ) show(method)
#Vc# Define local workspace for static variables
# this function can be called in the methods to get/set/initialize
# variables that are persistent within the strategy's workspace
.Workspace <- new.env()
staticVar(.Workspace)
on.exit( staticVar(NULL) )
# runtime resolution of the strategy's functions by their names if necessary
strategyX = xifyStrategy(method, .Workspace)
run(strategyX, y, x, maxIter=maxIter, ...)
})
#' @rdname KINOMOStrategy
setMethod('run', signature(object='KINOMOStrategyIterativeX', y='matrix', x='KINOMOfit'),
function(object, y, x, maxIter, ...){
strategy <- object
v <- y
seed <- x
#V!# KINOMOStrategyIterativeX::run
#Vc# Define workspace accessor function
# this function can be called in the methods to get/set/initialize
# variables that are persistent within the strategy's workspace
#Vc# initialize the strategy
# check validity of arguments if possible
method.args <- KINOMOFormals(strategy, runtime=TRUE)
internal.args <- method.args$internals
expected.args <- method.args$defaults
passed.args <- names(list(...))
forbidden.args <- is.element(passed.args, c(internal.args))
if( any(forbidden.args) ){
stop("KINOMO::run - Update/Stop method : formal argument(s) "
, paste( paste("'", passed.args[forbidden.args],"'", sep=''), collapse=', ')
, " already set internally.", call.=FALSE)
}
# !is.element('...', expected.args) &&
if( any(t <- !pmatch(passed.args, names(expected.args), nomatch=FALSE)) ){
stop("KINOMO::run - onInit/Update/Stop method for algorithm '", name(strategy),"': unused argument(s) "
, paste( paste("'", passed.args[t],"'", sep=''), collapse=', '), call.=FALSE)
}
# check for required arguments
required.args <- sapply(expected.args, function(x){ x <- as.character(x); length(x) == 1 && nchar(x) == 0 } )
required.args <- names(expected.args[required.args])
required.args <- required.args[required.args!='...']
if( any(t <- !pmatch(required.args, passed.args, nomatch=FALSE)) )
stop("KINOMO::run - Update/Stop method for algorithm '", name(strategy),"': missing required argument(s) "
, paste( paste("'", required.args[t],"'", sep=''), collapse=', '), call.=FALSE)
#Vc# Start iterations
KINOMOData <- seed
# cache verbose level
verbose <- verbose(KINOMOData)
# clone the object to allow the updates to work in place
if( verbose > 1L )
message("# Cloning KINOMO model seed ... ", appendLF=FALSE)
KINOMOFit <- clone(fit(KINOMOData))
if( verbose > 1L )
message("[", C.ptr(fit(KINOMOData)), " -> ", C.ptr(KINOMOFit), "]")
## onInit
if( is.function(strategy@onInit) ){
if( verbose > 1L ) message("# Step 1 - onInit ... ", appendLF=TRUE)
KINOMOFit <- strategy@onInit(strategy, v, KINOMOFit, ...)
if( verbose > 1L ) message("OK")
}
##
# pre-load slots
updateFun <- strategy@Update
stopFun <- strategy@Stop
showNIter.step <- 50L
showNIter <- verbose && maxIter >= showNIter.step
if( showNIter ){
ndIter <- nchar(as.character(maxIter))
itMsg <- paste0('Iterations: %', ndIter, 'i', "/", maxIter)
cat(itMsgBck <- sprintf(itMsg, 0))
itMsgBck <- nchar(itMsgBck)
}
i <- 0L
while( TRUE ){
#Vc# Stopping criteria
# check convergence (generally do not stop for i=0L, but only initialise static variables
stop.signal <- stopFun(strategy, i, v, KINOMOFit, ...)
# if the strategy ask for stopping, then stop the iteration
if( stop.signal || i >= maxIter ) break;
# increment i
i <- i+1L
if( showNIter && (i==1L || i %% showNIter.step == 0L) ){
cat(paste0(rep("\r", itMsgBck), sprintf(itMsg, i)))
}
#Vc# update the matrices
KINOMOFit <- updateFun(i, v, KINOMOFit, ...)
# every now and then track the error if required
KINOMOData <- trackError(KINOMOData, deviance(strategy, KINOMOFit, v, ...), niter=i)
}
if( showNIter ){
ended <- if( stop.signal ) 'converged' else 'stopped'
cat("\nDONE (", ended, " at ",i,'/', maxIter," iterations)\n", sep='')
}
# force to compute last error if not already done
KINOMOData <- trackError(KINOMOData, deviance(strategy, KINOMOFit, v, ...), niter=i, force=TRUE)
# store the fitted model
fit(KINOMOData) <- KINOMOFit
#Vc# wrap up
# let the strategy build the result
KINOMOData <- strategy@onReturn(KINOMOData)
if( !inherits(KINOMOData, 'KINOMOfit') ){
stop('KINOMOStrategyIterative[', name(strategy), ']::onReturn did not return a "KINOMO" instance [returned: "', class(KINOMOData), '"]')
}
# set the number of iterations performed
niter(KINOMOData) <- i
#return the result
KINOMO.debug('KINOMOStrategyIterativeX::run', 'Done')
invisible(KINOMOData)
})
#' @export
KINOMOFormals.KINOMOStrategyIterative <- function(x, runtime=FALSE, ...){
strategy <- xifyStrategy(x)
# from run method
m <- getMethod('run', signature(object='KINOMOStrategyIterative', y='matrix', x='KINOMOfit'))
run.args <- allFormals(m)[-(1:3)]
# onInit
init.args <- if( is.function(strategy@onInit) ) formals(strategy@onInit)
# Update
update.args <- formals(strategy@Update)
# Stop
stop.args <- formals(strategy@Stop)
# spplit internals and
internal.args <- names(c(init.args[1:3], update.args[1:3], stop.args[1:4]))
expected.args <- c(init.args[-(1:3)], update.args[-(1:3)], stop.args[-(1:4)])
if( runtime ){
# prepend registered default arguments
expected.args <- expand_list(strategy@defaults, expected.args)
list(internal=internal.args, defaults=expected.args)
}else{
args <- c(run.args, expected.args)
# prepend registered default arguments
expand_list(strategy@defaults, args)
}
}
KINOMO_update.KL.h <- std.divergence.update.h <- function(v, w, h, nbterms=0L, ncterms=0L, copy=TRUE)
{
.Call("divergence_update_H", v, w, h, nbterms, ncterms, copy, PACKAGE='KINOMO')
}
KINOMO_update.KL.h_R <- R_std.divergence.update.h <- function(v, w, h, wh=NULL)
{
# compute WH if necessary
if( is.null(wh) ) wh <- w %*% h
# divergence-reducing KINOMO iterations
# H_au = H_au ( sum_i [ W_ia V_iu / (WH)_iu ] ) / ( sum_k W_ka ) -> each row of H is divided by a the corresponding colSum of W
h * crossprod(w, v / wh) / colSums(w)
}
KINOMO_update.KL.w <- std.divergence.update.w <- function(v, w, h, nbterms=0L, ncterms=0L, copy=TRUE)
{
.Call("divergence_update_W", v, w, h, nbterms, ncterms, copy, PACKAGE='KINOMO')
}
KINOMO_update.KL.w_R <- R_std.divergence.update.w <- function(v, w, h, wh=NULL)
{
# compute WH if necessary
if( is.null(wh) ) wh <- w %*% h
# W_ia = W_ia ( sum_u [H_au A_iu / (WH)_iu ] ) / ( sum_v H_av ) -> each column of W is divided by a the corresponding rowSum of H
#x2 <- matrix(rep(rowSums(h), nrow(w)), ncol=ncol(w), byrow=TRUE);
#w * tcrossprod(v / wh, h) / x2;
sweep(w * tcrossprod(v / wh, h), 2L, rowSums(h), "/", check.margin = FALSE) # optimize version?
}
KINOMO_update.euclidean.h <- std.euclidean.update.h <-
function(v, w, h, eps=10^-9, nbterms=0L, ncterms=0L, copy=TRUE){
.Call("euclidean_update_H", v, w, h, eps, nbterms, ncterms, copy, PACKAGE='KINOMO')
}
KINOMO_update.euclidean.h_R <- R_std.euclidean.update.h <- function(v, w, h, wh=NULL, eps=10^-9){
# compute WH if necessary
den <- if( is.null(wh) ) crossprod(w) %*% h
else{ t(w) %*% wh}
# H_au = H_au (W^T V)_au / (W^T W H)_au
pmax(h * crossprod(w,v),eps) / (den + eps);
}
KINOMO_update.euclidean.w <- std.euclidean.update.w <-
function(v, w, h, eps=10^-9, nbterms=0L, ncterms=0L, weight=NULL, copy=TRUE){
.Call("euclidean_update_W", v, w, h, eps, weight, nbterms, ncterms, copy, PACKAGE='KINOMO')
}
#' @rdname KINOMO_update_euclidean
#' @export
KINOMO_update.euclidean.w_R <- R_std.euclidean.update.w <- function(v, w, h, wh=NULL, eps=10^-9){
# compute WH if necessary
den <- if( is.null(wh) ) w %*% tcrossprod(h)
else{ wh %*% t(h)}
# W_ia = W_ia (V H^T)_ia / (W H H^T)_ia and columns are rescaled after each iteration
pmax(w * tcrossprod(v, h), eps) / (den + eps);
}
KINOMOStop <- function(s, check=TRUE){
key <- s
fun <-
if( is.integer(key) ) KINOMO.stop.iteration(key)
else if( is.numeric(key) ) KINOMO.stop.threshold(key)
else if( is.function(key) ) key
else if( is.character(key) ){
# update .stop for back compatibility:
if( key == 'KINOMO.stop.consensus') key <- 'connectivity'
# first lookup for a `KINOMO.stop.*` function
key2 <- paste('KINOMO.stop.', key, sep='')
e <- pkgmaker::packageEnv()
sfun <- getFunction(key2, mustFind=FALSE, where = e)
if( is.null(sfun) ) # lookup for the function as such
sfun <- getFunction(key, mustFind = FALSE, where = e)
if( is.null(sfun) )
stop("Invalid key ['", key,"']: could not find functions '",key2, "' or '", key, "'")
sfun
}else if( identical(key, FALSE) ) # create a function that does not stop
function(strategy, i, target, data, ...){FALSE}
else
stop("Invalid key: should be a function, a character string or a single integer/numeric value. See ?KINOMOStop.")
# execute if generator (i.e. no arguments)
if( length(formals(fun)) == 0L ) fun <- fun()
# check validity if requested
if( check ){
n.stop <- names(formals(fun))
if( length(n.stop) < 4 ){
stop("Invalid 'Stop' method - must have at least 4 arguments: ",
"KINOMO strategy object [strategy], ",
"current iteration number [i], ",
"target matrix [y], ",
"current KINOMO model iterate [x]")
}
n.stop <- n.stop[-seq(4)]
# argument '...' must be present in method 'Stop'
if( !is.element('...', n.stop) )
stop("Invalid 'Stop' method: must have argument '...' (even if not used)")
}
# return function
fun
}
KINOMO.stop.iteration <- function(n){
KINOMO.debug("Using stopping criterion - Fixed number of iterations: ", n)
if( !is.numeric(n) )
stop("Invalid argument `n`: must be an integer value")
if( length(n) > 1 )
warning("KINOMO::KINOMO - Argument `n` [", deparse(substitute(n)), "] has length > 1: only using the first element.")
.max <- n[1]
function(object, i, y, x, ...) i >= .max
}
KINOMO.stop.threshold <- function(threshold){
KINOMO.debug("Using stopping criterion - Stationarity threshold: ", threshold)
if( !is.numeric(threshold) )
stop("Invalid argument `threshold`: must be a numeric value")
if( length(threshold) > 1 )
warning("KINOMO::KINOMO - Argument `threshold` [", deparse(substitute(threshold)), "] has length > 1: only using the first element.")
eval(parse(text=paste("function(strategy, i, target, data, stationary.th=", threshold, ", ...){
KINOMO.stop.stationary(strategy, i, target, data, stationary.th=stationary.th, ...)
}")))
}
KINOMO.stop.stationary <- local({
# static variable
.last.objective.value <- c(-Inf, Inf)
.niter <- 0L
.store_value <- function(value){
.niter <<- .niter + 1L
.last.objective.value <<- c(max(.last.objective.value[1L], value)
, min(.last.objective.value[2L], value))
}
.reset_value <- function(){
.last.objective.value <<- c(-Inf, Inf)
.niter <<- 0L
}
function(object, i, y, x, stationary.th=.Machine$double.eps, check.interval=5*check.niter, check.niter=10L, ...){
# check validity
if( check.interval < check.niter ){
stop("Invalid argument values: `check.interval` must always be greater than `check.niter`")
}
# initialisation call: compute initial objective value
if( i == 0L || (i == 1L && is.null(.last.objective.value)) ){
.reset_value()
# give the chance to update once and estimate from a partial model
if( is.partial.KINOMO(x) ) return( FALSE )
# compute initial deviance
current.value <- deviance(object, x, y, ...)
# check for NaN, i.e. probably infinitely small value (cf. bug reported by Nadine POUKEN SIEWE)
if( is.nan(current.value) ) return(TRUE)
# store value in static variable for next calls
.store_value(current.value)
return(FALSE)
}
# test convergence only every 10 iterations
if( .niter==0L && i %% check.interval != 0 ) return( FALSE );
# get last objective value from static variable
current.value <- deviance(object, x, y, ...)
# check for NaN, i.e. probably infinitely small value (cf. bug reported by Nadine POUKEN SIEWE)
if( is.nan(current.value) ) return(TRUE)
# update static variables
.store_value(current.value)
# once values have been computed for check.niter iterations:
# check if the difference in the extreme objective values is small enough
if( .niter == check.niter+1 ){
crit <- abs(.last.objective.value[1L] - .last.objective.value[2L]) / check.niter
if( crit <= stationary.th ){
if( KINOMO.getOption('verbose') ){
message(crit)
}
return( TRUE )
}
.reset_value()
}
# do NOT stop
FALSE
}
})
KINOMO.stop.connectivity <- local({
# static variables
.consold <- NULL
.inc <- NULL
function(object, i, y, x, stopconv=40, check.interval=10, ...){
if( i == 0L ){ # initialisation call
# Initialize consensus variables
# => they are static variables within the strategy's workspace so that
# they are persistent and available throughout across the calls
p <- ncol(x)
.consold <<- matrix(0, p, p)
.inc <<- 0
return(FALSE)
}
# test convergence only every 10 iterations
if( i %% check.interval != 0 ) return( FALSE );
# retrieve metaprofiles
h <- coef(x, all=FALSE)
# construct connectivity matrix
index <- apply(h, 2, function(x) which.max(x) )
cons <- outer(index, index, function(x,y) ifelse(x==y, 1,0));
changes <- cons != .consold
if( !any(changes) ) .inc <<- .inc + 1 # connectivity matrix has not changed: increment the count
else{
.consold <<- cons;
.inc <<- 0; # else restart counting
}
# prints number of changing elements
#if( verbose(x) ) cat( sprintf('%d ', sum(changes)) )
#cat( sprintf('%d ', sum(changes)) )
# assume convergence is connectivity stops changing
if( .inc > stopconv ) return( TRUE );
# do NOT stop
FALSE
}
})