annotate nmr_preprocessing/ptw.R @ 2:7304ec2c9ab7 draft

Uploaded
author marie-tremblay-metatoul
date Mon, 30 Jul 2018 10:33:03 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
1 ptw <- function (ref, samp, selected.traces, init.coef = c(0, 1, 0),
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
2 try = FALSE, warp.type = c("individual", "global"), optim.crit = c("WCC",
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
3 "RMS"), mode = c("forward", "backward"), smooth.param = ifelse(try,
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
4 0, 1e+05), trwdth = 20, trwdth.res = trwdth, verbose = FALSE,
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
5 ...)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
6 {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
7 optim.crit <- match.arg(optim.crit)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
8 warp.type <- match.arg(warp.type)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
9 mode <- match.arg(mode)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
10 if (is.vector(ref))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
11 ref <- matrix(ref, nrow = 1)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
12 if (is.vector(samp))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
13 samp <- matrix(samp, nrow = 1)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
14 if (nrow(ref) > 1 && nrow(ref) != nrow(samp))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
15 stop("The number of references does not equal the number of samples")
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
16 if (length(dim(ref)) > 2)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
17 stop("Reference cannot be an array")
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
18 if (length(dim(samp)) > 2)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
19 stop("Sample cannot be an array")
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
20 if (nrow(samp) == 1)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
21 warp.type <- "individual"
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
22 r <- nrow(samp)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
23 if (!missing(selected.traces)) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
24 samp <- samp[selected.traces, , drop = FALSE]
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
25 if (nrow(ref) > 1)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
26 ref <- ref[selected.traces, , drop = FALSE]
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
27 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
28 if (is.vector(init.coef))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
29 init.coef <- matrix(init.coef, nrow = 1)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
30 if (warp.type == "global") {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
31 if (nrow(init.coef) != 1)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
32 stop("Only one warping function is allowed with global alignment.")
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
33 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
34 else {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
35 if (nrow(init.coef) != nrow(samp))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
36 if (nrow(init.coef) == 1) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
37 init.coef <- matrix(init.coef, byrow = TRUE,
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
38 nrow = nrow(samp), ncol = length(init.coef))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
39 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
40 else {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
41 stop("The number of warping functions does not match the number of samples")
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
42 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
43 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
44 if (warp.type == "individual") {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
45 w <- matrix(0, nrow(samp), ncol(ref))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
46 a <- matrix(0, nrow(samp), ncol(init.coef))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
47 v <- rep(0, nrow(samp))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
48 warped.sample <- matrix(NA, nrow = nrow(samp), ncol = ncol(samp))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
49 for (i in 1:nrow(samp)) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
50 if (verbose & nrow(samp) > 1)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
51 cat(ifelse(nrow(ref) == 1, paste("Warping sample",
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
52 i, "with the reference \n"), paste("Warping sample",
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
53 i, "with reference \n", i)))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
54 if (nrow(ref) == 1) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
55 rfrnc <- ref
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
56 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
57 else {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
58 rfrnc <- ref[i, , drop = FALSE]
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
59 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
60 quad.res <- pmwarp(rfrnc, samp[i, , drop = FALSE],
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
61 optim.crit, init.coef[i, ], try = try, mode = mode,
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
62 smooth.param = smooth.param, trwdth = trwdth,
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
63 trwdth.res = trwdth.res, ...)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
64 w[i, ] <- quad.res$w
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
65 a[i, ] <- quad.res$a
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
66 v[i] <- quad.res$v
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
67 warped.sample[i, ] <- c(warp.sample(samp[i, , drop = FALSE],
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
68 w[i, ], mode = mode))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
69 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
70 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
71 else {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
72 if (nrow(ref) == 1)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
73 ref <- matrix(ref, nrow = nrow(samp), ncol = ncol(ref),
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
74 byrow = TRUE)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
75 if (verbose) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
76 if (nrow(ref) == 1) {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
77 cat("Simultaneous warping of samples with reference... \n")
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
78 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
79 else {
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
80 cat("Simultaneous warping of samples with references... \n")
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
81 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
82 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
83 quad.res <- pmwarp(ref, samp, optim.crit, c(init.coef),
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
84 try = try, mode = mode, smooth.param = smooth.param,
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
85 trwdth = trwdth, trwdth.res = trwdth.res, ...)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
86 w <- t(as.matrix(quad.res$w))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
87 a <- t(as.matrix(quad.res$a))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
88 v <- quad.res$v
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
89 warped.sample <- t(warp.sample(samp, w, mode))
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
90 }
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
91 if (verbose)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
92 cat("\nFinished.\n")
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
93 result <- list(reference = ref, sample = samp, warped.sample = warped.sample,
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
94 warp.coef = a, warp.fun = w, crit.value = v, optim.crit = optim.crit,
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
95 mode = mode, warp.type = warp.type)
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
96 class(result) <- "ptw"
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
97 result
7304ec2c9ab7 Uploaded
marie-tremblay-metatoul
parents:
diff changeset
98 }