R_netSNE: Compute similarities, Bht-SNE and Net-SNE.

Description Usage Arguments Examples

Description

A function that enables to run Computes_similarities, RunBhtSNE and RunNetSNE in a single time, or to chose between them. It also facilitates the managemnt of the output and input files created by those function.
Input: Depends on the paramter: 'to.run'.
Output: Depends on the paramter: 'to.run'.
Requirment: C++ implementation of Net-SNE installed and functional.

Usage

1
R_netSNE(to.run = c("Compute.sim", "BhtSNE", "NetSNE.basic", "NetSNE.train", "NetSNE.project"), path.netSNE.dir, out.dims = 2L, max.iter = 1000L, perp = 70L, theta.BhtSNE = 0.5, theta.NetSNE = 0.5, mom.init = 0.5, mom.final = 0.8, mom.switch.iter = 250L, early.exag.iter = 250L, learn.rate.bhtSNE = 200, learn.rate.netSNE = 0.02, NN.layers = 2L, NN.units = 50L, NN.function = "relu", sgd = TRUE, batch.frac.bhtSNE = NULL, batch.frac.netSNE = 0.1, random.init = TRUE, local.sample = 20L, min.sample.Z = 0.1, l2.reg.param = 0, step.method = "adam", save.iters.cache = NULL, permute.after.iters = NULL, seed = -1, verbose = TRUE, path.output.dir = path.output.dir, train.data = NULL, test.data = NULL, ref.embedding = c("BhtSNE", "NetSNE.basic"), path.to.bin.train.file = NULL, path.to.bin.test.file = NULL, force.rm.old.outdir = TRUE, name.bhtSNE.output.dir = "Bhtsne_out", name.netSNE.output.dir = "Netsne_out")

Arguments

to.run

Character; Vector to choose the functions to use (default: c("Compute.sim", "BhtSNE", "NetSNE.basic", "NetSNE.train", "NetSNE.project") for running everything)

path.netSNE.dir

Character; The path to the directory containing the executables created after Net-SNE installation (usually : path/to/netsne-master/bin). Must end with the name of directory with exectuables (here: '/bin')

out.dims

Integer; Specifies the output dimensionality (default: 2)

max.iter

Integer; Specifies the number of iterations (default: 1e3)

perp

Integer; Stands for 'Perplexity', an integer between 0 and 100 (see t-SNE documentation, default: 70)

theta.BhtSNE

Double; Bounded by 0 and 1, controls the accuracy-efficiency tradeoff in SPTree for Bht-SNE gradient computation; 0 means exact (default: 0.5)

theta.NetSNE

Double; Bounded by 0 and 1, controls the accuracy-efficiency tradeoff in SPTree for Net-SNE gradient computation; 0 means exact (default: 0.5)

mom.init

Double; Bounded by 0 and 1, defines the momentum used before n=mom.switch.iter iterations (default: 0.5)

mom.final

Double; Bounded by 0 and 1, defines the momentum used after n=mom.switch.iter iterations (default: 0.8)

mom.switch.iter

Integer; Number of iterations before switching the value of momentum (default: 250)

early.exag.iter

Integer; Number of iterations of early exaggeration (default: 250)

learn.rate.bhtSNE

Double; Learning rate used for Bht-SNE gradient steps (default: 200.0)

learn.rate.netSNE

Double; Learning rate used for Net-SNE gradient steps (default: 0.02)

NN.layers

Integer: Number of layers in Net-SNE's Neural Network (default: 2)

NN.units

Integer: Number of units for each layer in Net-SNE's Neural Network (default: 50)

NN.function

Character: Specifies the activation function of Net-SNE's Neural Network. Possible values: 'sigmoid' or 'relu' (default: relu)

sgd

Logical; Set to TRUE to use SGD acceleration during Net-SNE computation. If set to FALSE (effective for small datasets), equivalent to t-SNE with an additional backpropagation step to train a neural network (default: TRUE)

batch.frac.bhtSNE

Integer: Number of points to update at each iteration of Bht-SNE (default: NULL, i.e. all of them)

batch.frac.netSNE

Integer: Fraction of data to sample for Net-SNE's mini-batch (default: 0.1, i.e. 10%)

random.init

Logical; Set to TRUE to allow random initialization for Bht-SNE (default: TRUE)

local.sample

Integer: Number of local samples for each data point in Net-SNE's mini-batch (default: 20)

min.sample.Z

Double; Minimum fraction of data to use for approximating the normalization factor Z in Net-SNE's gradient (default: 0.1, i.e. 10%)

l2.reg.param

Numerical; L2 regularization parameter for introducing sparcity in Net-SNE's Neural Network and avoid train data overfitting (default: 0, i.e. no sparcity)

step.method

Character; Specifies Net-SNE's gradient step schedule. Possible values: 'adam', 'mom' (momentum), 'mom_gain' (momentum with gains) or 'fixed' (default: adam)

save.iters.cache

Integer; Number of iterations after which an intermediary embedding is repeatidly recorded. The final embedding (Y_final.txt) is always saved (default: NULL, i.e. no intermediary embedding is recorded, only the definitive one after max.iter)

permute.after.iters

Integer; Number of iterations after which the ordering of data points is repeatidly permuted for Net-SNE's fast mini-batching (default: NULL, i.e. permute.after.iters = max.iter)

seed

Integer; Equivalent to set.seed (default: -1, i.e. use current time as seed)

verbose

Logical; Should the outputs be printed to the console? (default: TRUE)

path.output.dir

Character; The path to save to output directories of Bht-SNE (name.bhtSNE.output.dir) and Net-SNE (name.netSNE.output.dir).

train.data

Marix;

test.data
ref.embedding
path.to.bin.train.file
path.to.bin.test.file
force.rm.old.outdir
name.bhtSNE.output.dir
name.netSNE.output.dir

Examples

  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
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (to.run = c("Compute.sim", "BhtSNE", "NetSNE.basic",
    "NetSNE.train", "NetSNE.project"), path.netSNE.dir, out.dims = 2L,
    max.iter = 1000L, perp = 70L, theta.BhtSNE = 0.5, theta.NetSNE = 0.5,
    mom.init = 0.5, mom.final = 0.8, mom.switch.iter = 250L,
    early.exag.iter = 250L, learn.rate.bhtSNE = 200, learn.rate.netSNE = 0.02,
    NN.layers = 2L, NN.units = 50L, NN.function = "relu", sgd = TRUE,
    batch.frac.bhtSNE = NULL, batch.frac.netSNE = 0.1, random.init = TRUE,
    local.sample = 20L, min.sample.Z = 0.1, l2.reg.param = 0,
    step.method = "adam", save.iters.cache = NULL, permute.after.iters = NULL,
    seed = -1, verbose = TRUE, path.output.dir = path.output.dir,
    train.data = NULL, test.data = NULL, ref.embedding = c("BhtSNE",
        "NetSNE.basic"), path.to.bin.train.file = NULL, path.to.bin.test.file = NULL,
    force.rm.old.outdir = TRUE, name.bhtSNE.output.dir = "Bhtsne_out",
    name.netSNE.output.dir = "Netsne_out")
{
    wd = getwd()
    setwd("~/")
    to.run = unique(tolower(to.run))
    to.rm = c()
    if (length(to.run) == 0) {
        stop("You didn't specify anything in param: 'to.run'. Please do so !\nPossible values:\n\t- 'Compute.sim': to compute a knn-graph used by Bht-SNE and NetSNE\n\t- 'Bht-SNE': to get a usual t-SNE embedding of a dataset\n---> Requirments: to.run = 'Compute.sim'\n\t- 'NetSNE.basic': to perform a t-SNE-like embedding using Net-SNE\n---> Requirments: to.run = 'Compute.sim'\n\t- 'NetSNE.train': train Net-SNE on a training/sampled dataset to latter project new data on the embedding\n---> Requirments : to.run = c('Compute.sim', 'BhtSNE' &/| 'NetSNE.basic')\n\t- 'NetSNE.project': project a new dataset on a prior embedding\n---> Requirments: to.run = c('Compute.sim, 'BhtSNE' &/| 'NetSNE.basic', 'NetSNE.train')",
            call. = FALSE)
    }
    for (i in 1:length(to.run)) {
        if (!to.run[i] %in% c("compute.sim", "bhtsne", "netsne.basic",
            "netsne.train", "netsne.project")) {
            to.rm[(length(to.rm) + 1)] = to.run[i]
        }
    }
    if (length(to.rm) > 0) {
        stop(paste(paste(to.rm, collapse = " and "), ": Unknown task(s) for param: to.run.\nPossible values:\n\t- 'Compute.sim': to compute a knn-graph used by Bht-SNE and NetSNE\n\t- 'Bht-SNE': to get a usual t-SNE embedding of a dataset\n---> Requirments: to.run = 'Compute.sim'\n\t- 'NetSNE.basic': to perform a t-SNE-like embedding using Net-SNE\n---> Requirments: to.run = 'Compute.sim'\n\t- 'NetSNE.train': train Net-SNE on a training/sampled dataset to latter project new data on the embedding\n---> Requirments : to.run = c('Compute.sim', 'BhtSNE' &/| 'NetSNE.basic')\n\t- 'NetSNE.project': project a new dataset on a prior embedding\n---> Requirments: to.run = c('Compute.sim, 'BhtSNE' &/| 'NetSNE.basic', 'NetSNE.train')"),
            call. = FALSE)
    }
    else {
        rm(to.rm)
    }
    if ("netsne.train" %in% to.run) {
        if (length(ref.embedding) != 1) {
            stop("When training Net-SNE on a reference embedding (param: 'ref.embedding'), you can only use one of those options: 'BhtSNE' or 'NetSNE.basic' (corresponds to a t-SNE-like embedding). Therefore, c('",
                paste(ref.embedding, collapse = "', '"), "') is not an acceptable value.",
                call. = FALSE)
        }
        else {
            ref.embedding = unique(tolower(ref.embedding))
            if (length(which(c("bhtsne", "netsne.basic", "netsne") %in%
                ref.embedding)) < 1) {
                stop(ref.embedding, " is not a correct option for 'ref.embedding'.\nPossible values:\n\n\t- 'BhtSNE'\n ---> Requirment: to.run = 'BhtSNE'\n\n\t- 'NetSNE.basic'\n ---> Requirment: to.run = 'NetSNE.basic'",
                  call. = FALSE)
            }
        }
    }
    path.output.dir = strsplit(path.output.dir, "/")[[1]]
    if (0 %in% nchar(path.output.dir)) {
        path.output.dir = path.output.dir[-which(path.output.dir ==
            "")]
    }
    if (!dir.exists(paste(path.output.dir, collapse = "/"))) {
        dir.create(paste(path.output.dir, collapse = "/"), recursive = FALSE)
    }
    path.netSNE.dir = strsplit(path.netSNE.dir, "/")[[1]]
    if (0 %in% nchar(path.netSNE.dir)) {
        path.netSNE.dir = path.netSNE.dir[-which(path.netSNE.dir ==
            "")]
    }
    if (path.netSNE.dir[length(path.netSNE.dir)] != "bin") {
        path.netSNE.dir[length(path.netSNE.dir) + 1] = "bin"
    }
    path.netSNE.dir = paste(path.netSNE.dir, collapse = "/")
    if (!file.exists(path.netSNE.dir)) {
        if (!file.exists(paste("/", path.netSNE.dir, sep = ""))) {
            stop(paste("The specified path to the 'bin' directory containing the executable files (",
                path.netSNE.dir, ") does not exist", sep = ""),
                call. = FALSE)
        }
        else {
            path.netSNE.dir = paste("/", path.netSNE.dir, sep = "")
        }
    }
    if (length(which(c("netsne.basic", "netsne.train", "netsne.project") %in%
        to.run)) > 0) {
        name.netSNE.output.dir = strsplit(name.netSNE.output.dir,
            "/")[[1]]
        if (0 %in% nchar(name.netSNE.output.dir)) {
            name.netSNE.output.dir = name.netSNE.output.dir[-which(name.netSNE.output.dir ==
                "")]
        }
        name.netSNE.output.dir = name.netSNE.output.dir[length(name.netSNE.output.dir)]
    }
    if (length(which(c("compute.sim", "bhtsne", "netsne.basic",
        "netsne.train") %in% to.run)) > 0) {
        if (is.null(train.data) & is.null(path.to.bin.train.file)) {
            stop("Neither the train data ('train.data') nor the path to train data binary file ('path.to.bin.train.file') were specified, please re-lunch the function with one of them as input.\n---> Required for running ",
                paste(to.run[which(to.run %in% c("compute.sim",
                  "netsne.basic", "netsne.train"))], collapse = " and "),
                call. = FALSE)
        }
        else if (!is.null(path.to.bin.train.file)) {
            path.to.bin.train.file = strsplit(path.to.bin.train.file,
                "/")[[1]]
            if (0 %in% nchar(path.to.bin.train.file)) {
                path.to.bin.train.file = path.to.bin.train.file[-which(path.to.bin.train.file ==
                  "")]
            }
            name.train = path.to.bin.train.file[length(path.to.bin.train.file)]
            if (length(which(c("compute.sim", "bhtsne", "netsne.basic") %in%
                to.run)) > 0) {
                name.sim = strsplit(name.train, "\.")[[1]]
                extention = name.sim[length(name.sim)]
                name.sim = name.sim[-length(name.sim)]
                name.sim = paste(name.sim, collapse = ".")
                name.sim = paste(name.sim, "_", perp, "_P.",
                  extention, sep = "")
            }
            if (!dir.exists(paste(path.to.bin.train.file[1:(length(path.to.bin.train.file) -
                1)], collapse = "/"))) {
                dir.create(paste(path.to.bin.train.file[1:(length(path.to.bin.train.file) -
                  1)], collapse = "/"), recursive = TRUE)
            }
            path.to.bin.train.file = paste(path.to.bin.train.file,
                collapse = "/")
            if (!is.null(train.data)) {
                if (file.exists(path.to.bin.train.file)) {
                  status = suppressWarnings(file.remove(path.to.bin.train.file))
                  if (!status) {
                    stop("Impossible to remove the file '", name.train,
                      "' in ", substr(path.to.bin.train.file,
                        1, nchar(path.to.bin.train.file) - nchar(name.train)),
                      ". Please do it yourself or change the path for param: 'path.to.bin.train.file'.",
                      call. = FALSE)
                  }
                  else {
                    cat("\nRemoving the file '", name.train,
                      "' in ", substr(path.to.bin.train.file,
                        1, nchar(path.to.bin.train.file) - nchar(name.train)),
                      sep = "")
                  }
                }
                cat("\nWriting '", name.train, "' in ", substr(path.to.bin.train.file,
                  1, nchar(path.to.bin.train.file) - nchar(name.train)),
                  "\n", sep = "")
                Write_binary_file(train.data, path.to.bin.train.file)
            }
            else if (!file.exists(path.to.bin.train.file)) {
                stop("The file '", name.train, "' in ", substr(path.to.bin.train.file,
                  1, nchar(path.to.bin.train.file) - nchar(name.train)),
                  " doesn't exist and you didn't provide a matrix corresponding to the training data. Please provide the data train matrix or change the path for param: 'path.to.bin.train.file'.",
                  call. = FALSE)
            }
            else {
                cat("\nUsing the file '", name.train, "' in ",
                  substr(path.to.bin.train.file, 1, nchar(path.to.bin.train.file) -
                    nchar(name.train)), ".\n", sep = "")
            }
        }
        else {
            name.train = gsub(pattern = "\[.*", replacement = "",
                deparse(substitute(train.data)))
            if (length(which(c("compute.sim", "bhtsne", "netsne.basic") %in%
                to.run)) > 0) {
                name.sim = paste(name.train, "_", perp, "_P.dat",
                  sep = "")
            }
            name.train = paste(name.train, "dat", sep = ".")
            if (file.exists(paste(paste(path.output.dir, collapse = "/"),
                name.train, sep = "/"))) {
                path.to.bin.train.file = paste(paste(path.output.dir,
                  collapse = "/"), name.train, sep = "/")
                rm.file = TRUE
            }
            else {
                path.to.bin.train.file = paste(paste(path.output.dir[1:(length(path.output.dir) -
                  1)], collapse = "/"), name.train, sep = "/")
                if (file.exists(path.to.bin.train.file)) {
                  rm.file = TRUE
                }
                else {
                  rm.file = FALSE
                }
            }
            if (rm.file) {
                status = suppressWarnings(file.remove(path.to.bin.train.file))
                if (!status) {
                  stop("Impossible to remove the file '", name.train,
                    "' in ", substr(path.to.bin.train.file, 1,
                      nchar(path.to.bin.train.file) - nchar(name.train)),
                    ". Please do it yourself or change the path for param: 'path.to.bin.train.file'.",
                    call. = FALSE)
                }
                else {
                  cat("\nRemoving the file '", name.train, "' in ",
                    substr(path.to.bin.train.file, 1, nchar(path.to.bin.train.file) -
                      nchar(name.train)), "\n", sep = "")
                }
            }
            cat("\nWriting '", name.train, "' in ", substr(path.to.bin.train.file,
                1, nchar(path.to.bin.train.file) - nchar(name.train)),
                "\n", sep = "")
            Write_binary_file(train.data, path.to.bin.train.file)
        }
    }
    if ("netsne.project" %in% to.run) {
        if (is.null(test.data) & is.null(path.to.bin.test.file)) {
            stop("Neither the test data ('test.data') nor the path to test data binary file ('path.to.bin.test.file') were specified, please re-lunch the function with one of them as input.\n---> Required for running to.run = c('netsne.project')",
                call. = FALSE)
        }
        else if (!is.null(path.to.bin.test.file)) {
            path.to.bin.test.file = strsplit(path.to.bin.test.file,
                "/")[[1]]
            if (0 %in% nchar(path.to.bin.test.file)) {
                path.to.bin.test.file = path.to.bin.test.file[-which(path.to.bin.test.file ==
                  "")]
            }
            name.test = path.to.bin.test.file[length(path.to.bin.test.file)]
            if (!dir.exists(paste(path.to.bin.test.file[1:(length(path.to.bin.test.file) -
                1)], collapse = "/"))) {
                dir.create(paste(path.to.bin.test.file[1:(length(path.to.bin.test.file) -
                  1)], collapse = "/"), recursive = TRUE)
            }
            path.to.bin.test.file = paste(path.to.bin.test.file,
                collapse = "/")
            if (!is.null(test.data)) {
                if (file.exists(path.to.bin.test.file)) {
                  status = suppressWarnings(file.remove(path.to.bin.test.file))
                  if (!status) {
                    stop("Impossible to remove the file '", name.test,
                      "' in ", substr(path.to.bin.test.file,
                        1, nchar(path.to.bin.test.file) - nchar(name.test)),
                      ". Please do it yourself or change the path for param: 'path.to.bin.test.file'.",
                      call. = FALSE)
                  }
                  else {
                    cat("\nRemoving the file '", name.test, "' in ",
                      substr(path.to.bin.test.file, 1, nchar(path.to.bin.test.file) -
                        nchar(name.test)), sep = "")
                  }
                }
                cat("\nWriting '", name.test, "' in ", substr(path.to.bin.test.file,
                  1, nchar(path.to.bin.test.file) - nchar(name.test)),
                  "\n", sep = "")
                Write_binary_file(test.data, path.to.bin.test.file)
            }
            else if (!file.exists(path.to.bin.test.file)) {
                stop("The file '", name.test, "' in ", substr(path.to.bin.test.file,
                  1, nchar(path.to.bin.test.file) - nchar(name.test)),
                  " doesn't exist and you didn't provide a matrix corresponding to the testing data. Please provide the data test matrix or change the path for param: 'path.to.bin.test.file'.",
                  call. = FALSE)
            }
            else {
                cat("\nUsing the file '", name.test, "' in ",
                  substr(path.to.bin.test.file, 1, nchar(path.to.bin.test.file) -
                    nchar(name.test)), ".\n", sep = "")
            }
        }
        else {
            name.test = gsub(pattern = "\[.*", replacement = "",
                deparse(substitute(test.data)))
            name.test = paste(name.test, "dat", sep = ".")
            if (file.exists(paste(paste(path.output.dir, collapse = "/"),
                name.test, sep = "/"))) {
                path.to.bin.test.file = paste(paste(path.output.dir,
                  collapse = "/"), name.test, sep = "/")
                rm.file = TRUE
            }
            else {
                path.to.bin.test.file = paste(paste(path.output.dir[1:(length(path.output.dir) -
                  1)], collapse = "/"), name.test, sep = "/")
                if (file.exists(path.to.bin.test.file)) {
                  rm.file = TRUE
                }
                else {
                  rm.file = FALSE
                }
            }
            if (rm.file) {
                status = suppressWarnings(file.remove(path.to.bin.test.file))
                if (!status) {
                  stop("Impossible to remove the file '", name.test,
                    "' in ", substr(path.to.bin.test.file, 1,
                      nchar(path.to.bin.test.file) - nchar(name.test)),
                    ". Please do it yourself or change the path for param: 'path.to.bin.test.file'.",
                    call. = FALSE)
                }
                else {
                  cat("\nRemoving the file '", name.test, "' in ",
                    substr(path.to.bin.test.file, 1, nchar(path.to.bin.test.file) -
                      nchar(name.test)), "\n", sep = "")
                }
            }
            cat("\nWriting '", name.test, "' in ", substr(path.to.bin.test.file,
                1, nchar(path.to.bin.test.file) - nchar(name.test)),
                "\n", sep = "")
            Write_binary_file(test.data, path.to.bin.test.file)
        }
    }
    path.output.dir = paste(path.output.dir, collapse = "/")
    if (length(which(c("bhtsne", "netsne.train") %in% to.run)) >
        0) {
        name.bhtSNE.output.dir = strsplit(name.bhtSNE.output.dir,
            "/")[[1]]
        if (0 %in% nchar(name.bhtSNE.output.dir)) {
            name.bhtSNE.output.dir = name.bhtSNE.output.dir[-which(name.bhtSNE.output.dir ==
                "")]
        }
        name.bhtSNE.output.dir = name.bhtSNE.output.dir[length(name.bhtSNE.output.dir)]
        path.BhtSNE.output.dir = paste(path.output.dir, name.bhtSNE.output.dir,
            sep = "/")
    }
    if (length(which(c("compute.sim", "bhtsne", "netsne.basic") %in%
        to.run)) > 0) {
        path.to.bin.P = paste(path.output.dir, name.sim, sep = "/")
    }
    if ("compute.sim" %in% to.run) {
        if (file.exists(path.to.bin.P)) {
            if (force.rm.old.outdir) {
                status = suppressWarnings(file.remove(path.to.bin.P))
                if (!status) {
                  stop("Impossible to remove the file '", name.sim,
                    "' in ", path.output.dir, ". Please do it yourself or remove 'compute.sim' from param: to.run if the file is correct.",
                    call. = FALSE)
                }
                else {
                  compute.sim = TRUE
                }
            }
            else {
                WALL = TRUE
                while (WALL) {
                  cat("The binary file '", name.sim, "' specifying the similarities seems to already exist (PATH: ",
                    path.to.bin.P, ").\nPlease choose:\n\t- [1]: Remove the file and compute similarities,\n\t- [2]: Keep the file and skip the computing of similarities,\n\t- [3]: Stop and do nothing.\n",
                    sep = "")
                  answer = readline(prompt = "Your choice [[1]/[2]/[3]]: ")
                  if (answer %in% c(1:3)) {
                    WALL = FALSE
                    if (answer == "1") {
                      status = suppressWarnings(file.remove(path.to.bin.P))
                      if (!status) {
                        stop("Impossible to remove the file '",
                          name.sim, "' in ", path.output.dir,
                          ". Please do it yourself or remove 'compute.sim' from param: to.run if the file is correct.",
                          call. = FALSE)
                      }
                    }
                    else if (answer == "2") {
                      compute.sim = FALSE
                    }
                    else {
                      return("Stopped")
                    }
                  }
                }
            }
        }
        else {
            compute.sim = TRUE
        }
        if (compute.sim) {
            Compute_similarities(perp = perp, verbose = verbose,
                path.netSNE.dir = path.netSNE.dir, path.to.bin.file = path.to.bin.train.file,
                path.output.file = path.to.bin.P)
        }
    }
    else {
        compute.sim = FALSE
    }
    if (!compute.sim && length(which(c("bhtsne", "netsne.basic") %in%
        to.run)) > 0 && !file.exists(path.to.bin.P)) {
        stop("The file ", name.sim, " in ", path.output.dir,
            " is unreachable.", call. = FALSE)
    }
    if ("bhtsne" %in% to.run) {
        if (dir.exists(path.BhtSNE.output.dir)) {
            if (force.rm.old.outdir) {
                unlink(path.BhtSNE.output.dir, recursive = TRUE)
                run.bhtsne = TRUE
            }
            else {
                WALL = TRUE
                while (WALL) {
                  cat("The output directory '", name.bhtSNE.output.dir,
                    "' for saving the outputs of Bht-SNE seems to already exist (PATH: ",
                    path.BhtSNE.output.dir, ").\nPlease choose:\n\t- [1]: Remove the directory and run Bht-SNE,\n\t- [2]: Keep the directory and skip the computing of Bht-SNE,\n\t- [3]: Stop and do nothing.\n",
                    sep = "")
                  answer = readline(prompt = "Your choice [[1]/[2]/[3]]: ")
                  if (answer %in% c(1:3)) {
                    WALL = FALSE
                    if (answer == "1") {
                      unlink(path.BhtSNE.output.dir, recursive = TRUE)
                      run.bhtsne = TRUE
                    }
                    else if (answer == "2") {
                      run.bhtsne = FALSE
                    }
                    else {
                      return("Stopped")
                    }
                  }
                }
            }
        }
        else {
            run.bhtsne = TRUE
        }
        if (run.bhtsne) {
            RunBhtSNE(out.dims = out.dims, max.iter = max.iter,
                theta = theta.BhtSNE, mom.init = mom.init, mom.final = mom.final,
                mom.switch.iter = mom.switch.iter, early.exag.iter = early.exag.iter,
                learn.rate = learn.rate.bhtSNE, batch.frac = batch.frac.bhtSNE,
                seed = seed, verbose = verbose, random.init = random.init,
                save.iters.cache = save.iters.cache, path.netSNE.dir = path.netSNE.dir,
                path.to.bin.file = path.to.bin.train.file, path.to.simil.file = path.to.bin.P,
                path.output.dir = path.BhtSNE.output.dir)
        }
    }
    else {
        run.bhtsne = FALSE
    }
    if ("netsne.basic" %in% to.run) {
        path.NetSNE.output.dir = paste(path.output.dir, paste(name.netSNE.output.dir,
            "basic", sep = "_"), sep = "/")
        if (dir.exists(path.NetSNE.output.dir)) {
            if (force.rm.old.outdir) {
                unlink(path.NetSNE.output.dir, recursive = TRUE)
                run.netsne.basic = TRUE
            }
            else {
                WALL = TRUE
                while (WALL) {
                  cat("The output directory '", name.netSNE.output.dir,
                    "' for saving the outputs of Net-SNE (t-SNE-like use) seems to already exist (PATH: ",
                    path.NetSNE.output.dir, ").\nPlease choose:\n\t- [1]: Remove the directory and run Net-SNE.basic (t-SNE-like),\n\t- [2]: Keep the directory and skip the computing of Net-SNE (t-SNE-like),\n\t- [3]: Stop and do nothing.\n",
                    sep = "")
                  answer = readline(prompt = "Your choice [[1]/[2]/[3]]: ")
                  if (answer %in% c(1:3)) {
                    WALL = FALSE
                    if (answer == "1") {
                      unlink(path.NetSNE.output.dir, recursive = TRUE)
                      run.netsne.basic = TRUE
                    }
                    else if (answer == "2") {
                      run.netsne.basic = FALSE
                    }
                    else {
                      return("Stopped")
                    }
                  }
                }
            }
        }
        else {
            run.netsne.basic = TRUE
        }
        if (run.netsne.basic) {
            RunNetSNE(utilis = "t-SNE", out.dims = out.dims,
                max.iter = max.iter, theta = theta.NetSNE, step.method = step.method,
                NN.layers = NN.layers, NN.units = NN.units, NN.function = NN.function,
                l2.reg.param = l2.reg.param, mom.init = mom.init,
                mom.final = mom.final, mom.switch.iter = mom.switch.iter,
                early.exag.iter = early.exag.iter, learn.rate = learn.rate.netSNE,
                local.sample = local.sample, batch.frac = batch.frac.netSNE,
                min.sample.Z = min.sample.Z, sgd = sgd, seed = seed,
                verbose = verbose, permute.after.iters = permute.after.iters,
                save.iters.cache = save.iters.cache, path.netSNE.dir = path.netSNE.dir,
                path.to.bin.train.file = path.to.bin.train.file,
                path.to.bin.test.file = NULL, path.to.simil.file = path.to.bin.P,
                path.output.dir = path.NetSNE.output.dir, path.ref.embedding = NULL,
                path.to.model.dir = NULL, model.prefix = "WHATEVER")
        }
    }
    else {
        run.netsne.basic = FALSE
    }
    if ("netsne.train" %in% to.run) {
        if (ref.embedding == "bhtsne") {
            path.ref.embedding = paste(path.output.dir, name.bhtSNE.output.dir,
                "Y_final.txt", sep = "/")
        }
        else {
            path.ref.embedding = paste(path.output.dir, paste(name.netSNE.output.dir,
                "basic", sep = "_"), "Y_final.txt", sep = "/")
        }
        if (!file.exists(path.ref.embedding)) {
            stop("There is no embedding for Net-SNE to learn in ",
                path.ref.embedding, ". Please change the value of param: 'ref.embedding' or include the relevant option in 'to.run'.",
                call. = FALSE)
        }
        path.NetSNE.output.dir = paste(path.output.dir, paste(name.netSNE.output.dir,
            "model", sep = "_"), sep = "/")
        if (dir.exists(path.NetSNE.output.dir)) {
            if (force.rm.old.outdir) {
                unlink(path.NetSNE.output.dir, recursive = TRUE)
                run.netsne.train = TRUE
            }
            else {
                WALL = TRUE
                while (WALL) {
                  cat("The output directory '", name.netSNE.output.dir,
                    "' for saving the outputs of Net-SNE (model creation after learning embedding) seems to already exist (PATH: ",
                    path.NetSNE.output.dir, ").\nPlease choose:\n\t- [1]: Remove the directory and run Net-SNE.train (learn embedding),\n\t- [2]: Keep the directory and skip the computing of Net-SNE (learn embedding),\n\t- [3]: Stop and do nothing.\n",
                    sep = "")
                  answer = readline(prompt = "Your choice [[1]/[2]/[3]]: ")
                  if (answer %in% c(1:3)) {
                    WALL = FALSE
                    if (answer == "1") {
                      unlink(path.NetSNE.output.dir, recursive = TRUE)
                      run.netsne.train = TRUE
                    }
                    else if (answer == "2") {
                      run.netsne.train = FALSE
                    }
                    else {
                      return("Stopped")
                    }
                  }
                }
            }
        }
        else {
            run.netsne.train = TRUE
        }
        if (run.netsne.train) {
            RunNetSNE(utilis = "Learn", out.dims = out.dims,
                max.iter = max.iter, theta = theta.NetSNE, step.method = step.method,
                NN.layers = NN.layers, NN.units = NN.units, NN.function = NN.function,
                l2.reg.param = l2.reg.param, mom.init = mom.init,
                mom.final = mom.final, mom.switch.iter = mom.switch.iter,
                early.exag.iter = early.exag.iter, learn.rate = learn.rate.netSNE,
                local.sample = local.sample, batch.frac = batch.frac.netSNE,
                min.sample.Z = min.sample.Z, sgd = sgd, seed = seed,
                verbose = verbose, permute.after.iters = permute.after.iters,
                save.iters.cache = save.iters.cache, path.netSNE.dir = path.netSNE.dir,
                path.to.bin.train.file = path.to.bin.train.file,
                path.to.bin.test.file = NULL, path.to.simil.file = NULL,
                path.output.dir = path.NetSNE.output.dir, path.ref.embedding = path.ref.embedding,
                path.to.model.dir = NULL, model.prefix = "whatever")
        }
    }
    else {
        run.netsne.train = FALSE
    }
    if ("netsne.project" %in% to.run) {
        path.to.model.dir = paste(path.output.dir, paste(name.netSNE.output.dir,
            "model", sep = "_"), sep = "/")
        if (!dir.exists(path.to.model.dir)) {
            stop("The directory '", path.to.model.dir, "' supposed to contain the model of Net-SNE (after learning a reference embedding) does not exist.")
        }
        l = list.files(path.to.model.dir, pattern = "_.*[.txt]",
            full.names = FALSE, include.dirs = FALSE)
        l = l[which(regexpr("_L[0-9]*_[:alnum:]*", l) > 0)]
        l = gsub("_L[0-9]*_.*.txt", "", l)
        if (length(unique(l)) == 1) {
            model.prefix = unique(l)
        }
        else {
            stop("Impossible to find a model prefix (usually 'model_final') in ",
                path.to.model.dir, ". Please check the content of the directory, and re-run the function (maybe with 'netsne.train' in to.run) ",
                call. = FALSE)
        }
        path.NetSNE.output.dir = paste(path.output.dir, paste(name.netSNE.output.dir,
            "projection", sep = "_"), sep = "/")
        if (dir.exists(path.NetSNE.output.dir)) {
            if (force.rm.old.outdir) {
                unlink(path.NetSNE.output.dir, recursive = TRUE)
                run.netsne.project = TRUE
            }
            else {
                WALL = TRUE
                while (WALL) {
                  cat("The output directory '", name.netSNE.output.dir,
                    "' for saving the outputs of Net-SNE (projection) seems to already exist (PATH: ",
                    path.NetSNE.output.dir, ").\nPlease choose:\n\t- [1]: Remove the directory and run Net-SNE.project (to project),\n\t- [2]: Keep the directory and skip the computing of Net-SNE (to project),\n\t- [3]: Stop and do nothing.\n",
                    sep = "")
                  answer = readline(prompt = "Your choice [[1]/[2]/[3]]: ")
                  if (answer %in% c(1:3)) {
                    WALL = FALSE
                    if (answer == "1") {
                      unlink(path.NetSNE.output.dir, recursive = TRUE)
                      run.netsne.project = TRUE
                    }
                    else if (answer == "2") {
                      run.netsne.project = FALSE
                    }
                    else {
                      return("Stopped")
                    }
                  }
                }
            }
        }
        else {
            run.netsne.project = TRUE
        }
        if (run.netsne.project) {
            RunNetSNE(utilis = "Project", verbose = TRUE, path.netSNE.dir = path.netSNE.dir,
                path.to.bin.train = "irrelevant", path.to.bin.test = path.to.bin.test.file,
                path.output.dir = path.NetSNE.output.dir, path.to.model.dir = path.to.model.dir,
                model.prefix = model.prefix)
        }
    }
    else {
        run.netsne.project = FALSE
    }
    Result = list()
    i = 1
    if (run.bhtsne) {
        Result[[i]] = read.table(file = paste(path.BhtSNE.output.dir,
            "Y_final.txt", sep = "/"), header = FALSE, skip = 2,
            sep = "")
        names(Result)[i] = "Bht-SNE"
        i = i + 1
    }
    if (run.netsne.basic) {
        Result[[i]] = read.table(file = paste(path.output.dir,
            paste(name.netSNE.output.dir, "basic", sep = "_"),
            "Y_final.txt", sep = "/"), header = FALSE, skip = 2,
            sep = "")
        names(Result)[i] = "Net-SNE (Basic)"
        i = i + 1
    }
    if (run.netsne.project) {
        Result[[i]] = read.table(file = paste(path.output.dir,
            paste(name.netSNE.output.dir, "projection", sep = "_"),
            "Y_final.txt", sep = "/"), header = FALSE, skip = 2,
            sep = "")
        names(Result)[i] = "Net-SNE (Projection)"
        i = i + 1
    }
    setwd(wd)
    return(Result)
  }

schwikowskilab/rNetSNE documentation built on May 4, 2019, 6:40 p.m.