Skip to content

Commit 52e80f6

Browse files
committed
Changed the code to using just one regular expression as discussed in issue #133, need to fix bugs
1 parent d08c635 commit 52e80f6

File tree

4 files changed

+24
-36
lines changed

4 files changed

+24
-36
lines changed

pkg/R/smcABC.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ simulate.trees <- function(workspace, theta, model, seed=NA, ...) {
4949

5050

5151
# formally 'distance'
52-
kernel.dist <- function(t1, t2, decay.factor, rbf.variance, sst.control, norm.mode) {
52+
kernel.dist <- function(t1, t2, decay.factor, rbf.variance, sst.control, norm.mode, regex, gamma) {
5353
if (is.null(t1$kernel)) {
5454
stop("t1 missing self kernel in distance()")
5555
}
@@ -62,7 +62,10 @@ kernel.dist <- function(t1, t2, decay.factor, rbf.variance, sst.control, norm.mo
6262
t2,
6363
lambda=decay.factor,
6464
sigma=rbf.variance,
65-
rho=sst.control
65+
rho=sst.control,
66+
label1 <- gsub(regex,t1$tip.label),
67+
label2 <- gsub(regex,t2$tip.label),
68+
gamma=gamma
6669
)
6770

6871
result <- 1. - k / sqrt(t1$kernel * t2$kernel)

pkg/R/smcConfig.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ load.config <- function(file) {
4343
decay.factor=0.2,
4444
rbf.variance=100.0,
4545
sst.control=1.0,
46-
norm.mode='NONE'
46+
norm.mode='NONE',
47+
regex= "",
48+
gamma=0
4749
)
4850
class(config) <- 'smc.config'
4951

@@ -119,8 +121,7 @@ load.config <- function(file) {
119121
config$rbf.variance <- kernel.settings$rbf.variance
120122
config$sst.control <- kernel.settings$sst.control
121123
config$norm.mode <- kernel.settings$norm.mode
122-
config$label1 <- kernel.settings$label1
123-
config$label2 <- kernel.settings$label2
124+
config$regex <- kernel.settings$regex
124125
config$gamma <- kernel.settings$gamma
125126
}
126127
} else if (is.character(settings$distances)) {
@@ -146,8 +147,7 @@ load.config <- function(file) {
146147
config$rbf.variance <- as.numeric(values["rbf.variance"])
147148
config$sst.control <- as.numeric(values["sst.control"])
148149
config$norm.mode <- values["norm.mode"]
149-
config$label1 <- values["label1"]
150-
config$label2 <- values["label2"]
150+
config$regex <- values["regex"]
151151
config$gamma <- as.numeric(values["gamma"])
152152
}
153153
}

pkg/R/treekernel.R

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,17 @@
1313
# You should have received a copy of the GNU General Public License
1414
# along with Kaphi. If not, see <http://www.gnu.org/licenses/>.
1515

16-
# cache self-kernel score (only if kernel distance is desired for distance metric; specified on user-level)
17-
# FIXME: this won't work for labelled kernel
18-
if (grepl("kernel", config$dist)) {
19-
label <- gsub(config$label1,tree$tip.label) # when using different regex for label1 and label2, need to decide which one to use
20-
tree$kernel <- tree.kernel(tree, tree,
21-
lambda=config$decay.factor,
22-
sigma=config$rbf.variance,
23-
rho=config$sst.control,
24-
normalize=0,
25-
label1=label,
26-
label2=label,
27-
gamma=config$gamma
28-
)
29-
}
30-
31-
utk <- function(t1, t2, config) {
32-
# convenience wrapper for unlabelled tree shape kernel
33-
result <- tree.kernel(t1, t2,
34-
lambda=config$decay.factor,
35-
sigma=config$rbf.variance,
36-
rho=as.double(config$sst.control),
37-
normalize=0
38-
)
39-
return(result)
40-
}
16+
# DEPRECATED
17+
# utk <- function(t1, t2, config) {
18+
# convenience wrapper for unlabelled tree shape kernel
19+
# result <- tree.kernel(t1, t2,
20+
# lambda=config$decay.factor,
21+
# sigma=config$rbf.variance,
22+
# rho=as.double(config$sst.control),
23+
# normalize=0
24+
# )
25+
# return(result)
26+
# }
4127

4228
tree.kernel <- function(tree1, tree2,
4329
lambda, # decay factor
@@ -52,8 +38,8 @@ tree.kernel <- function(tree1, tree2,
5238
use.label <- if (any(is.na(label1)) || any(is.na(label2)) || is.null(label1) || is.null(label2)) {
5339
FALSE
5440
} else {
55-
label1 <- gsub(config$label1,tree$tip.label)
56-
label2 <- gsub(config$label2,tree$tip.label)
41+
label1 <- gsub(config$regex,tree1$tip.label)
42+
label2 <- gsub(config$regex,tree2$tip.label)
5743
TRUE
5844
}
5945

tests/fixtures/test-bisse.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ distances:
7777
rbf.variance: 100.0
7878
sst.control: 1.0
7979
norm.mode: 'NONE'
80-
label1: "'^.+_([01])$', '\\1'"
81-
label2: "'^.+_([01])$', '\\1'"
80+
regex: "'^.+_([01])$', '\\1'"
8281
gamma: 1
8382

0 commit comments

Comments
 (0)