Skip to content

Commit

Permalink
Update utility methods to handle rownames in U
Browse files Browse the repository at this point in the history
Update the utility methods (susceptible, infected and recovered) to
handle the added rownames (model compartment names) to U. The rownames
are cleared in the utlity methods since the resulting matrix contains
data from one or more compartments.
  • Loading branch information
stewid committed Feb 25, 2017
1 parent 6218c0e commit 600de64
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 37 deletions.
12 changes: 9 additions & 3 deletions R/SEIR.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ setMethod("susceptible",
if (!is.null(i))
ii <- ii[i]
j <- seq(from = 1, to = dim(model@U)[2], by = by)
as.matrix(model@U[ii, j, drop = FALSE])
result <- as.matrix(model@U[ii, j, drop = FALSE])
rownames(result) <- NULL
result
}
)

Expand All @@ -169,7 +171,9 @@ setMethod("infected",
if (!is.null(i))
ii <- ii[i]
j <- seq(from = 1, to = dim(model@U)[2], by = by)
as.matrix(model@U[ii, j, drop = FALSE])
result <- as.matrix(model@U[ii, j, drop = FALSE])
rownames(result) <- NULL
result
}
)

Expand All @@ -185,7 +189,9 @@ setMethod("recovered",
if (!is.null(i))
ii <- ii[i]
j <- seq(from = 1, to = dim(model@U)[2], by = by)
as.matrix(model@U[ii, j, drop = FALSE])
result <- as.matrix(model@U[ii, j, drop = FALSE])
rownames(result) <- NULL
result
}
)

Expand Down
12 changes: 9 additions & 3 deletions R/SIR.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ setMethod("susceptible",
if (!is.null(i))
ii <- ii[i]
j <- seq(from = 1, to = dim(model@U)[2], by = by)
as.matrix(model@U[ii, j, drop = FALSE])
result <- as.matrix(model@U[ii, j, drop = FALSE])
rownames(result) <- NULL
result
}
)

Expand All @@ -163,7 +165,9 @@ setMethod("infected",
if (!is.null(i))
ii <- ii[i]
j <- seq(from = 1, to = dim(model@U)[2], by = by)
as.matrix(model@U[ii, j, drop = FALSE])
result <- as.matrix(model@U[ii, j, drop = FALSE])
rownames(result) <- NULL
result
}
)

Expand All @@ -179,7 +183,9 @@ setMethod("recovered",
if (!is.null(i))
ii <- ii[i]
j <- seq(from = 1, to = dim(model@U)[2], by = by)
as.matrix(model@U[ii, j, drop = FALSE])
result <- as.matrix(model@U[ii, j, drop = FALSE])
rownames(result) <- NULL
result
}
)

Expand Down
8 changes: 6 additions & 2 deletions R/SISe.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ setMethod("susceptible",
if (!is.null(i))
ii <- ii[i]
j <- seq(from = 1, to = dim(model@U)[2], by = by)
as.matrix(model@U[ii, j, drop = FALSE])
result <- as.matrix(model@U[ii, j, drop = FALSE])
rownames(result) <- NULL
result
}
)

Expand All @@ -194,7 +196,9 @@ setMethod("infected",
if (!is.null(i))
ii <- ii[i]
j <- seq(from = 1, to = dim(model@U)[2], by = by)
as.matrix(model@U[ii, j, drop = FALSE])
result <- as.matrix(model@U[ii, j, drop = FALSE])
rownames(result) <- NULL
result
}
)

Expand Down
2 changes: 2 additions & 0 deletions R/SISe3.R
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ setMethod("susceptible",
}
}

rownames(result) <- NULL
result
}
)
Expand Down Expand Up @@ -286,6 +287,7 @@ setMethod("infected",
}
}

rownames(result) <- NULL
result
}
)
Expand Down
2 changes: 2 additions & 0 deletions R/SISe3_sp.R
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ setMethod("susceptible",
}
}

rownames(result) <- NULL
result
}
)
Expand Down Expand Up @@ -296,6 +297,7 @@ setMethod("infected",
}
}

rownames(result) <- NULL
result
}
)
Expand Down
8 changes: 6 additions & 2 deletions R/SISe_sp.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ setMethod("susceptible",
if (!is.null(i))
ii <- ii[i]
j <- seq(from = 1, to = dim(model@U)[2], by = by)
as.matrix(model@U[ii, j, drop = FALSE])
result <- as.matrix(model@U[ii, j, drop = FALSE])
rownames(result) <- NULL
result
}
)

Expand All @@ -205,7 +207,9 @@ setMethod("infected",
if (!is.null(i))
ii <- ii[i]
j <- seq(from = 1, to = dim(model@U)[2], by = by)
as.matrix(model@U[ii, j, drop = FALSE])
result <- as.matrix(model@U[ii, j, drop = FALSE])
rownames(result) <- NULL
result
}
)

Expand Down
6 changes: 3 additions & 3 deletions tests/SEIR.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ S_expected <- structure(c(0L, 1L, 2L, 3L, 4L, 5L, 0L, 1L, 2L, 3L, 4L, 5L, 0L,
2L, 3L, 4L, 5L, 0L, 1L, 2L, 3L, 4L, 5L, 0L, 1L, 2L,
3L, 4L, 5L, 0L, 1L, 2L, 3L, 4L, 5L, 0L, 1L, 2L, 3L,
4L, 5L, 0L, 1L, 2L, 3L, 4L, 5L),
.Dim = c(6L, 10L))
.Dim = c(6L, 10L), .Dimnames = list(NULL, NULL))

S_observed <- susceptible(result)

Expand All @@ -158,7 +158,7 @@ I_expected <- structure(c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
.Dim = c(6L, 10L))
.Dim = c(6L, 10L), .Dimnames = list(NULL, NULL))

I_observed <- infected(result)

Expand All @@ -169,7 +169,7 @@ R_expected <- structure(c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
.Dim = c(6L, 10L))
.Dim = c(6L, 10L), .Dimnames = list(NULL, NULL))

R_observed <- recovered(result)

Expand Down
6 changes: 3 additions & 3 deletions tests/SIR.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ S_expected <- structure(c(0L, 1L, 2L, 3L, 4L, 5L, 0L, 1L, 2L, 3L, 4L, 5L, 0L,
2L, 3L, 4L, 5L, 0L, 1L, 2L, 3L, 4L, 5L, 0L, 1L, 2L,
3L, 4L, 5L, 0L, 1L, 2L, 3L, 4L, 5L, 0L, 1L, 2L, 3L,
4L, 5L, 0L, 1L, 2L, 3L, 4L, 5L),
.Dim = c(6L, 10L))
.Dim = c(6L, 10L), .Dimnames = list(NULL, NULL))

S_observed <- susceptible(result)

Expand All @@ -118,7 +118,7 @@ I_expected <- structure(c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
.Dim = c(6L, 10L))
.Dim = c(6L, 10L), .Dimnames = list(NULL, NULL))

I_observed <- infected(result)

Expand All @@ -129,7 +129,7 @@ R_expected <- structure(c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
.Dim = c(6L, 10L))
.Dim = c(6L, 10L), .Dimnames = list(NULL, NULL))

R_observed <- recovered(result)

Expand Down
8 changes: 4 additions & 4 deletions tests/SISe.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## SimInf, a framework for stochastic disease spread simulations
## Copyright (C) 2015 Pavol Bauer
## Copyright (C) 2015 - 2016 Stefan Engblom
## Copyright (C) 2015 - 2016 Stefan Widgren
## Copyright (C) 2015 - 2017 Stefan Engblom
## Copyright (C) 2015 - 2017 Stefan Widgren
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -1062,7 +1062,7 @@ S_expected <- structure(c(0L, 1L, 2L, 3L, 4L, 5L, 0L, 1L, 2L, 3L, 4L, 5L, 0L,
2L, 3L, 4L, 5L, 0L, 1L, 2L, 3L, 4L, 5L, 0L, 1L, 2L,
3L, 4L, 5L, 0L, 1L, 2L, 3L, 4L, 5L, 0L, 1L, 2L, 3L,
4L, 5L, 0L, 1L, 2L, 3L, 4L, 5L),
.Dim = c(6L, 10L))
.Dim = c(6L, 10L), .Dimnames = list(NULL, NULL))

S_observed <- susceptible(result)

Expand All @@ -1073,7 +1073,7 @@ I_expected <- structure(c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
.Dim = c(6L, 10L))
.Dim = c(6L, 10L), .Dimnames = list(NULL, NULL))

I_observed <- infected(result)

Expand Down
8 changes: 4 additions & 4 deletions tests/SISe3.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## SimInf, a framework for stochastic disease spread simulations
## Copyright (C) 2015 Pavol Bauer
## Copyright (C) 2015 - 2016 Stefan Engblom
## Copyright (C) 2015 - 2016 Stefan Widgren
## Copyright (C) 2015 - 2017 Stefan Engblom
## Copyright (C) 2015 - 2017 Stefan Widgren
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -1486,7 +1486,7 @@ S_expected <- structure(c(0L, 1L, 2L, 3L, 4L, 5L, 0L, 1L, 2L, 3L, 4L, 5L, 0L,
2L, 3L, 4L, 5L, 0L, 1L, 2L, 3L, 4L, 5L, 0L, 1L, 2L,
3L, 4L, 5L, 0L, 1L, 2L, 3L, 4L, 5L, 0L, 1L, 2L, 3L,
4L, 5L, 0L, 1L, 2L, 3L, 4L, 5L),
.Dim = c(6L, 10L))
.Dim = c(6L, 10L), .Dimnames = list(NULL, NULL))

S_observed <- susceptible(result, age = 1)

Expand All @@ -1497,7 +1497,7 @@ I_expected <- structure(c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
.Dim = c(6L, 10L))
.Dim = c(6L, 10L), .Dimnames = list(NULL, NULL))

I_observed <- infected(result, age = 1)

Expand Down
8 changes: 4 additions & 4 deletions tests/SISe3_sp.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## SimInf, a framework for stochastic disease spread simulations
## Copyright (C) 2015 - 2016 Stefan Engblom
## Copyright (C) 2015 - 2016 Stefan Widgren
## Copyright (C) 2015 - 2017 Stefan Engblom
## Copyright (C) 2015 - 2017 Stefan Widgren
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -1604,7 +1604,7 @@ S_expected <- structure(c(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L),
.Dim = 9:10)
.Dim = 9:10, .Dimnames = list(NULL, NULL))

S_observed <- susceptible(result, age = 1)

Expand All @@ -1620,7 +1620,7 @@ I_expected <- structure(c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
.Dim = 9:10)
.Dim = 9:10, .Dimnames = list(NULL, NULL))

I_observed <- infected(result, age = 1)

Expand Down
8 changes: 4 additions & 4 deletions tests/SISe_sp.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## SimInf, a framework for stochastic disease spread simulations
## Copyright (C) 2015 - 2016 Stefan Engblom
## Copyright (C) 2015 - 2016 Stefan Widgren
## Copyright (C) 2015 - 2017 Stefan Engblom
## Copyright (C) 2015 - 2017 Stefan Widgren
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -1076,7 +1076,7 @@ S_expected <- structure(c(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L),
.Dim = 9:10)
.Dim = 9:10, .Dimnames = list(NULL, NULL))

S_observed <- susceptible(result)

Expand All @@ -1092,7 +1092,7 @@ I_expected <- structure(c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
.Dim = 9:10)
.Dim = 9:10, .Dimnames = list(NULL, NULL))

I_observed <- infected(result)

Expand Down
2 changes: 1 addition & 1 deletion tests/sample_select.R
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ model@events@E[5, 1] <- 1
S_expected <- structure(c(0L, 6L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L),
.Dim = c(2L, 11L))
.Dim = c(2L, 11L), .Dimnames = list(NULL, NULL))

res <- run(model, threads = 1)
stopifnot(identical(susceptible(res), S_expected))
Expand Down
8 changes: 4 additions & 4 deletions tests/siminf.R
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ stopifnot(identical(model@S, result@S))
stopifnot(identical(
susceptible(result, age = 2, i = 1) + infected(result, age = 2, i = 1),
structure(c(0L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L),
.Dim = c(1L, 11L))))
.Dim = c(1L, 11L), .Dimnames = list(NULL, NULL))))
stopifnot(identical(
susceptible(result, age = 3, i = 1) + infected(result, age = 3, i = 1),
structure(c(0L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L),
.Dim = c(1L, 11L))))
.Dim = c(1L, 11L), .Dimnames = list(NULL, NULL))))
stopifnot(identical(sum(result@U[,1]), 45L))
stopifnot(identical(model@ldata, result@ldata))
stopifnot(identical(model@tspan, result@tspan))
Expand All @@ -226,11 +226,11 @@ if (SimInf:::have_openmp()) {
stopifnot(identical(
susceptible(result, age = 2, i = 1) + infected(result, age = 2, i = 1),
structure(c(0L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L),
.Dim = c(1L, 11L))))
.Dim = c(1L, 11L), .Dimnames = list(NULL, NULL))))
stopifnot(identical(
susceptible(result, age = 3, i = 1) + infected(result, age = 3, i = 1),
structure(c(0L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L),
.Dim = c(1L, 11L))))
.Dim = c(1L, 11L), .Dimnames = list(NULL, NULL))))
stopifnot(identical(sum(result_omp@U[,1]), 45L))
stopifnot(identical(model@ldata, result_omp@ldata))
stopifnot(identical(model@tspan, result_omp@tspan))
Expand Down

0 comments on commit 600de64

Please sign in to comment.