Commit 685d7dd1 authored by Poppy Miller's avatar Poppy Miller
Browse files

Merge branch 'hashmap_replace' into 'master'

Hashmap C++ module replacement

See merge request millerp/sourceR!2
parents 987b6a7d 5b67eb7b
...@@ -19,8 +19,9 @@ Depends: ...@@ -19,8 +19,9 @@ Depends:
tensorA, tensorA,
assertthat assertthat
Imports: Imports:
methods,
Rcpp (>= 1.0.4),
gtools, gtools,
hashmap,
R6, R6,
cluster, cluster,
stats, stats,
...@@ -28,7 +29,9 @@ Imports: ...@@ -28,7 +29,9 @@ Imports:
SPIn, SPIn,
grDevices, grDevices,
reshape2 reshape2
RoxygenNote: 6.0.1 LinkingTo: Rcpp
DynLib: sourceR
RoxygenNote: 7.1.0
KeepSource: TRUE KeepSource: TRUE
LazyData: TRUE LazyData: TRUE
Suggests: Suggests:
......
...@@ -24,3 +24,5 @@ importFrom(SPIn,SPIn) ...@@ -24,3 +24,5 @@ importFrom(SPIn,SPIn)
importFrom(grDevices,col2rgb) importFrom(grDevices,col2rgb)
importFrom(grDevices,colorRampPalette) importFrom(grDevices,colorRampPalette)
importFrom(stats,median) importFrom(stats,median)
importFrom(Rcpp, sourceCpp)
useDynLib(sourceR)
HashTable = function(keys=character(0), vals=numeric(0)) {
new("Rcpp_HashTable", keys, vals)
}
...@@ -515,7 +515,7 @@ PoisGammaDPUpdate <- R6::R6Class( ...@@ -515,7 +515,7 @@ PoisGammaDPUpdate <- R6::R6Class(
names(nk_local) <- names(nk) names(nk_local) <- names(nk)
nk_local[self$node$s[i]] <- nk_local[self$node$s[i]] - 1 nk_local[self$node$s[i]] <- nk_local[self$node$s[i]] - 1
theta <- self$node$theta$values() theta <- self$node$theta$find(names(nk))
logpi <- log(nk_local) + y[i] * log(theta) - theta * aX[i] logpi <- log(nk_local) + y[i] * log(theta) - theta * aX[i]
logpiplus <- log(self$node$conc) + logpiplus <- log(self$node$conc) +
...@@ -554,13 +554,11 @@ PoisGammaDPUpdate <- R6::R6Class( ...@@ -554,13 +554,11 @@ PoisGammaDPUpdate <- R6::R6Class(
self$node$theta$erase(s_old) self$node$theta$erase(s_old)
enqueue(self$node$idBucket, s_old) enqueue(self$node$idBucket, s_old)
nk <- nk[names(nk) != s_old] nk <- nk[names(nk) != s_old]
if (any(is.na(nk)))
browser()
} }
nk[self$node$s[i]] <- nk[self$node$s[i]] + 1 nk[self$node$s[i]] <- nk[self$node$s[i]] + 1
} }
# Update theta # Sample theta from the conditional posterior
for (label in self$node$theta$keys()) for (label in self$node$theta$keys())
{ {
sumy <- sum(y[self$node$s == label]) sumy <- sum(y[self$node$s == label])
......
...@@ -354,14 +354,14 @@ DirichletProcessNode <- R6::R6Class( # TODO: Make this accept a generic base dis ...@@ -354,14 +354,14 @@ DirichletProcessNode <- R6::R6Class( # TODO: Make this accept a generic base dis
self$baseRate <- rate self$baseRate <- rate
self$idBucket <- queue() self$idBucket <- queue()
for(i in 1:length(s)) enqueue(self$idBucket, as.character(i)) for(i in 1:length(s)) enqueue(self$idBucket, as.character(i))
h = replicate(length(theta), dequeue(self$idBucket)) keys = replicate(length(theta), dequeue(self$idBucket))
self$theta <- hashmap::hashmap(h, theta) self$theta <- HashTable(keys, theta)
self$s <- h[s] self$s <- keys[s]
}, },
getData = function() getData = function()
self$theta$find(self$s), self$theta$find(self$s),
getDensity = function(i) getDensity = function(i)
sum(self$base(self$theta[self$s[i]],shape=self$baseShape, rate=self$baseRate ,log = T)) sum(self$base(self$theta$find(self$s[i]),shape=self$baseShape, rate=self$baseRate ,log = T))
) )
) )
...@@ -7,4 +7,5 @@ ...@@ -7,4 +7,5 @@
#' The main sourceR method is \code{\link{HaldDP}}. #' The main sourceR method is \code{\link{HaldDP}}.
#' @docType package #' @docType package
#' @name sourceR #' @name sourceR
#' @useDynLib sourceR
NULL NULL
Rcpp::loadModule("HashTable_module")
...@@ -4,9 +4,8 @@ ...@@ -4,9 +4,8 @@
\name{AdaptiveDirMRW} \name{AdaptiveDirMRW}
\alias{AdaptiveDirMRW} \alias{AdaptiveDirMRW}
\title{AdaptiveDirMRW} \title{AdaptiveDirMRW}
\format{Object of \code{\link{R6Class}} with methods for updating a \link{DirichletNode} instance.} \format{
\usage{ Object of \code{\link{R6Class}} with methods for updating a \link{DirichletNode} instance.
AdaptiveDirMRW
} }
\value{ \value{
Object of \code{\link{AdaptiveDirMRW}} Object of \code{\link{AdaptiveDirMRW}}
...@@ -27,8 +26,22 @@ Please note that no checks are performed as to the suitability of this ...@@ -27,8 +26,22 @@ Please note that no checks are performed as to the suitability of this
algorithm for a particular \link{StochasticNode}. It is up to the user algorithm for a particular \link{StochasticNode}. It is up to the user
to use the correct update algorithm for the appropriate nodes. to use the correct update algorithm for the appropriate nodes.
} }
\section{Fields}{ \section{Methods}{
\describe{
\item{\code{new(node, toupdate = function() 1:length(node$getData()), tune = rep(0.1, length(node$getData())), burning = 100)}}{constructor takes an instance of a \link{StochasticNode}
node, function to choose the indices of the elements to update (by default all elements), initial tuning vector (diagonal of adaptive tuning matrix), and the number of calls between
adaptations.}
\item{\code{update()}}{when called, updates \code{node}}
\item{\code{acceptance()}}{return the acceptance rate}
}
}
\keyword{DAG}
\keyword{MCMC}
\keyword{node}
\section{Public fields}{
\if{html}{\out{<div class="r6-fields">}}
\describe{ \describe{
\item{\code{cov}}{the current covariance} \item{\code{cov}}{the current covariance}
...@@ -41,19 +54,75 @@ to use the correct update algorithm for the appropriate nodes. ...@@ -41,19 +54,75 @@ to use the correct update algorithm for the appropriate nodes.
\item{\code{ncalls}}{the number of times \code{update} has been called} \item{\code{ncalls}}{the number of times \code{update} has been called}
\item{\code{node}}{the node to which the updater is attached} \item{\code{node}}{the node to which the updater is attached}
}} }
\if{html}{\out{</div>}}
}
\section{Active bindings}{
\if{html}{\out{<div class="r6-active-bindings">}}
\describe{
\item{\code{cov}}{the current covariance}
\item{\code{burnin}}{the number of updates to burn in}
}
\if{html}{\out{</div>}}
}
\section{Methods}{ \section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-new}{\code{AdaptiveDirMRW$new()}}
\item \href{#method-update}{\code{AdaptiveDirMRW$update()}}
\item \href{#method-acceptance}{\code{AdaptiveDirMRW$acceptance()}}
\item \href{#method-clone}{\code{AdaptiveDirMRW$clone()}}
}
}
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-new"></a>}}
\if{latex}{\out{\hypertarget{method-new}{}}}
\subsection{Method \code{new()}}{
\subsection{Usage}{
\if{html}{\out{<div class="r">}}\preformatted{AdaptiveDirMRW$new(
node,
toupdate = function() 1:length(node$getData()),
tune = rep(0.1, length(node$getData())),
batchsize = 50,
name = "name"
)}\if{html}{\out{</div>}}
}
\describe{
\item{\code{new(node, toupdate = function() 1:length(node$getData()), tune = rep(0.1, length(node$getData())), burning = 100)}}{constructor takes an instance of a \link{StochasticNode}
node, function to choose the indices of the elements to update (by default all elements), initial tuning vector (diagonal of adaptive tuning matrix), and the number of calls between
adaptations.}
\item{\code{update()}}{when called, updates \code{node}}
\item{\code{acceptance()}}{return the acceptance rate}
} }
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-update"></a>}}
\if{latex}{\out{\hypertarget{method-update}{}}}
\subsection{Method \code{update()}}{
\subsection{Usage}{
\if{html}{\out{<div class="r">}}\preformatted{AdaptiveDirMRW$update()}\if{html}{\out{</div>}}
} }
\keyword{DAG} }
\keyword{MCMC} \if{html}{\out{<hr>}}
\keyword{node} \if{html}{\out{<a id="method-acceptance"></a>}}
\if{latex}{\out{\hypertarget{method-acceptance}{}}}
\subsection{Method \code{acceptance()}}{
\subsection{Usage}{
\if{html}{\out{<div class="r">}}\preformatted{AdaptiveDirMRW$acceptance()}\if{html}{\out{</div>}}
}
}
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-clone"></a>}}
\if{latex}{\out{\hypertarget{method-clone}{}}}
\subsection{Method \code{clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{<div class="r">}}\preformatted{AdaptiveDirMRW$clone(deep = FALSE)}\if{html}{\out{</div>}}
}
\subsection{Arguments}{
\if{html}{\out{<div class="arguments">}}
\describe{
\item{\code{deep}}{Whether to make a deep clone.}
}
\if{html}{\out{</div>}}
}
}
}
...@@ -4,9 +4,8 @@ ...@@ -4,9 +4,8 @@
\name{AdaptiveLogDirMRW} \name{AdaptiveLogDirMRW}
\alias{AdaptiveLogDirMRW} \alias{AdaptiveLogDirMRW}
\title{AdaptiveLogDirMRW} \title{AdaptiveLogDirMRW}
\format{Object of \code{\link{R6Class}} with methods for updating a \link{DirichletNode} instance.} \format{
\usage{ Object of \code{\link{R6Class}} with methods for updating a \link{DirichletNode} instance.
AdaptiveLogDirMRW
} }
\value{ \value{
Object of \code{\link{AdaptiveLogDirMRW}} Object of \code{\link{AdaptiveLogDirMRW}}
...@@ -28,8 +27,22 @@ Please note that no checks are performed as to the suitability of this ...@@ -28,8 +27,22 @@ Please note that no checks are performed as to the suitability of this
algorithm for a particular \link{StochasticNode}. It is up to the user algorithm for a particular \link{StochasticNode}. It is up to the user
to use the correct update algorithm for the appropriate nodes. to use the correct update algorithm for the appropriate nodes.
} }
\section{Fields}{ \section{Methods}{
\describe{
\item{\code{new(node, toupdate = function() 1:length(node$getData()), tune = rep(0.1, length(node$getData())), burning = 100)}}{constructor takes an instance of a \link{StochasticNode}
node, function to choose the indices of the elements to update (by default all elements), initial tuning vector (diagonal of adaptive tuning matrix), and number of calls between
adaptations.}
\item{\code{update()}}{when called, updates \code{node}}
\item{\code{acceptance()}}{return the acceptance rate}
}
}
\keyword{DAG}
\keyword{MCMC}
\keyword{node}
\section{Public fields}{
\if{html}{\out{<div class="r6-fields">}}
\describe{ \describe{
\item{\code{cov}}{the current covariance} \item{\code{cov}}{the current covariance}
...@@ -42,19 +55,75 @@ to use the correct update algorithm for the appropriate nodes. ...@@ -42,19 +55,75 @@ to use the correct update algorithm for the appropriate nodes.
\item{\code{ncalls}}{the number of times \code{update} has been called} \item{\code{ncalls}}{the number of times \code{update} has been called}
\item{\code{node}}{the node to which the updater is attached} \item{\code{node}}{the node to which the updater is attached}
}} }
\if{html}{\out{</div>}}
}
\section{Active bindings}{
\if{html}{\out{<div class="r6-active-bindings">}}
\describe{
\item{\code{cov}}{the current covariance}
\item{\code{burnin}}{the number of updates to burn in}
}
\if{html}{\out{</div>}}
}
\section{Methods}{ \section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-new}{\code{AdaptiveLogDirMRW$new()}}
\item \href{#method-update}{\code{AdaptiveLogDirMRW$update()}}
\item \href{#method-acceptance}{\code{AdaptiveLogDirMRW$acceptance()}}
\item \href{#method-clone}{\code{AdaptiveLogDirMRW$clone()}}
}
}
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-new"></a>}}
\if{latex}{\out{\hypertarget{method-new}{}}}
\subsection{Method \code{new()}}{
\subsection{Usage}{
\if{html}{\out{<div class="r">}}\preformatted{AdaptiveLogDirMRW$new(
node,
toupdate = function() 1:length(node$getData()),
tune = rep(0.1, length(node$getData())),
batchsize = 50,
name = "name"
)}\if{html}{\out{</div>}}
}
\describe{
\item{\code{new(node, toupdate = function() 1:length(node$getData()), tune = rep(0.1, length(node$getData())), burning = 100)}}{constructor takes an instance of a \link{StochasticNode}
node, function to choose the indices of the elements to update (by default all elements), initial tuning vector (diagonal of adaptive tuning matrix), and number of calls between
adaptations.}
\item{\code{update()}}{when called, updates \code{node}}
\item{\code{acceptance()}}{return the acceptance rate}
} }
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-update"></a>}}
\if{latex}{\out{\hypertarget{method-update}{}}}
\subsection{Method \code{update()}}{
\subsection{Usage}{
\if{html}{\out{<div class="r">}}\preformatted{AdaptiveLogDirMRW$update()}\if{html}{\out{</div>}}
} }
\keyword{DAG} }
\keyword{MCMC} \if{html}{\out{<hr>}}
\keyword{node} \if{html}{\out{<a id="method-acceptance"></a>}}
\if{latex}{\out{\hypertarget{method-acceptance}{}}}
\subsection{Method \code{acceptance()}}{
\subsection{Usage}{
\if{html}{\out{<div class="r">}}\preformatted{AdaptiveLogDirMRW$acceptance()}\if{html}{\out{</div>}}
}
}
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-clone"></a>}}
\if{latex}{\out{\hypertarget{method-clone}{}}}
\subsection{Method \code{clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{<div class="r">}}\preformatted{AdaptiveLogDirMRW$clone(deep = FALSE)}\if{html}{\out{</div>}}
}
\subsection{Arguments}{
\if{html}{\out{<div class="arguments">}}
\describe{
\item{\code{deep}}{Whether to make a deep clone.}
}
\if{html}{\out{</div>}}
}
}
}
...@@ -4,9 +4,8 @@ ...@@ -4,9 +4,8 @@
\name{AdaptiveMultiMRW} \name{AdaptiveMultiMRW}
\alias{AdaptiveMultiMRW} \alias{AdaptiveMultiMRW}
\title{AdaptiveMultiMRW} \title{AdaptiveMultiMRW}
\format{Object of \code{\link{R6Class}} with methods for updating a \link{Node} instance.} \format{
\usage{ Object of \code{\link{R6Class}} with methods for updating a \link{Node} instance.
AdaptiveMultiMRW
} }
\value{ \value{
Object of \code{\link{AdaptiveMultiMRW}} Object of \code{\link{AdaptiveMultiMRW}}
...@@ -25,8 +24,21 @@ Please note that no checks are performed as to the suitability of this ...@@ -25,8 +24,21 @@ Please note that no checks are performed as to the suitability of this
algorithm for a particular \link{StochasticNode}. It is up to the user algorithm for a particular \link{StochasticNode}. It is up to the user
to use the correct update algorithm for the appropriate nodes. to use the correct update algorithm for the appropriate nodes.
} }
\section{Fields}{ \section{Methods}{
\describe{
\item{\code{new(node, tune = rep(0.1, length(node$getData())), burning = 100)}}{constructor takes an instance of a \link{StochasticNode}
node, initial tuning vector (diagonal of adaptive tuning matrix), and number of burnin calls.}
\item{\code{update()}}{when called, updates \code{node}}
\item{\code{acceptance()}}{return the acceptance rate}
}
}
\keyword{DAG}
\keyword{MCMC}
\keyword{node}
\section{Public fields}{
\if{html}{\out{<div class="r6-fields">}}
\describe{ \describe{
\item{\code{cov}}{the current covariance} \item{\code{cov}}{the current covariance}
...@@ -39,18 +51,64 @@ to use the correct update algorithm for the appropriate nodes. ...@@ -39,18 +51,64 @@ to use the correct update algorithm for the appropriate nodes.
\item{\code{ncalls}}{the number of times \code{update} has been called} \item{\code{ncalls}}{the number of times \code{update} has been called}
\item{\code{node}}{the node to which the updater is attached} \item{\code{node}}{the node to which the updater is attached}
}} }
\if{html}{\out{</div>}}
}
\section{Methods}{ \section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-new}{\code{AdaptiveMultiMRW$new()}}
\item \href{#method-update}{\code{AdaptiveMultiMRW$update()}}
\item \href{#method-acceptance}{\code{AdaptiveMultiMRW$acceptance()}}
\item \href{#method-clone}{\code{AdaptiveMultiMRW$clone()}}
}
}
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-new"></a>}}
\if{latex}{\out{\hypertarget{method-new}{}}}
\subsection{Method \code{new()}}{
\subsection{Usage}{
\if{html}{\out{<div class="r">}}\preformatted{AdaptiveMultiMRW$new(
node,
tune = rep(0.1, length(node$getData())),
burnin = 100
)}\if{html}{\out{</div>}}
}
\describe{
\item{\code{new(node, tune = rep(0.1, length(node$getData())), burning = 100)}}{constructor takes an instance of a \link{StochasticNode}
node, initial tuning vector (diagonal of adaptive tuning matrix), and number of burnin calls.}
\item{\code{update()}}{when called, updates \code{node}}
\item{\code{acceptance()}}{return the acceptance rate}
} }
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-update"></a>}}
\if{latex}{\out{\hypertarget{method-update}{}}}
\subsection{Method \code{update()}}{
\subsection{Usage}{
\if{html}{\out{<div class="r">}}\preformatted{AdaptiveMultiMRW$update()}\if{html}{\out{</div>}}
} }
\keyword{DAG} }
\keyword{MCMC} \if{html}{\out{<hr>}}
\keyword{node} \if{html}{\out{<a id="method-acceptance"></a>}}
\if{latex}{\out{\hypertarget{method-acceptance}{}}}
\subsection{Method \code{acceptance()}}{
\subsection{Usage}{
\if{html}{\out{<div class="r">}}\preformatted{AdaptiveMultiMRW$acceptance()}\if{html}{\out{</div>}}
}
}
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-clone"></a>}}
\if{latex}{\out{\hypertarget{method-clone}{}}}
\subsection{Method \code{clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{<div class="r">}}\preformatted{AdaptiveMultiMRW$clone(deep = FALSE)}\if{html}{\out{</div>}}
}
\subsection{Arguments}{
\if{html}{\out{<div class="arguments">}}
\describe{
\item{\code{deep}}{Whether to make a deep clone.}
}
\if{html}{\out{</div>}}
}
}
}
% Generated by roxygen2: do not edit by hand % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/data.R % Please edit documentation in R/data.R
\docType{data}
\name{Alpha_} \name{Alpha_}
\alias{Alpha_} \alias{Alpha_}
\title{Alpha prior hyperparameter class} \title{Alpha prior hyperparameter class}
\format{An object of class \code{R6ClassGenerator} of length 24.}
\usage{
Alpha_
}
\description{ \description{
Alpha prior hyperparameter class Alpha prior hyperparameter class
Alpha prior hyperparameter class
}
\section{Super class}{
\code{\link[sourceR:Data]{sourceR::Data}} -> \code{Alpha}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-clone}{\code{Alpha_$clone()}}
}
}
\if{html}{
\out{<details open ><summary>Inherited methods</summary>}
\itemize{
\item \out{<span class="pkg-link" data-pkg="sourceR" data-topic="Data" data-id="initialize">}\href{../../sourceR/html/Data.html#method-initialize}{\code{sourceR::Data$initialize()}}\out{</span>}
}
\out{</details>}
}
\if{html}{\out{<hr>}}
\if{html}{\out{<a id="method-clone"></a>}}
\if{latex}{\out{\hypertarget{method-clone}{}}}
\subsection{Method \code{clone()}}{
The objects of this class are cloneable with this method.
\subsection{Usage}{
\if{html}{\out{<div class="r">}}\preformatted{Alpha_$clone(deep = FALSE)}\if{html}{\out{</div>}}
}
\subsection{Arguments}{
\if{html}{\out{<div class="arguments">}}
\describe{
\item{\code{deep}}{Whether to make a deep clone.}
}
\if{html}{\out{</div>}}
}
}
} }
\keyword{datasets}
% Generated by roxygen2: do not edit by hand % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/model.R % Please edit documentation in R/model.R
\docType{data}
\name{DPModel_impl} \name{DPModel_impl}
\alias{DPModel_impl} \alias{DPModel_impl}
\title{Builds the source attribution model. Is not intended to be used by a regular user. \title{Builds the source attribution model. Is not intended to be used by a regular user.
Developers only here!} Developers only here!}
\format{\code{\link{R6Class}} object.} \format{
\usage{ \code{\link{R6Class}} object.
DPModel_impl
} }
\value{ \value{
Object of \code{\link{R6Class}}. Object of \code{\link{R6Class}}.
} }
\description{ \description{
Builds the source attribution model. Is not intended to be used by a regular user.
Developers only here!
Builds the source attribution model. Is not intended to be used by a regular user. Builds the source attribution model. Is not intended to be used by a regular user.
Developers only here! Developers only here!
} }
\section{Fields}{ \section{Public fields}{
\if{html}{\out{<div class="r6-fields">}}
\describe{
\item{\code{y}}{3D array of [type, time, location] of the number of human cases}
\item{\code{X}}{3D array of the number of positive samples for each type, source and time
[type, source, time]}
\item{\code{R}}{3D array of normalised relative prevalences for each timepoint
[type, source, time]}
\item{\code{Time}}{a character vector of timepoint ids matching time dimension in y and R}
\item{\code{Location}}{a character vector of location ids matching location dimension in y}
\item{\code{Sources}}{a character vector of source ids matching the source dimension in X}
\item{\code{Type}}{a character vector of type ids matching the type dimension in X}
\item{\code{prev}}{a 2D array (matrix) of [source, time].}
\item{\code{a_q}}{concentration parameter for the DP}
\item{\code{a_theta}}{shape parameter for the Gamma base distribution for the DP}
\item{\code{b_theta}}{rate parameter for the Gamma base distribution for the DP}
\item{\code{a_r}}{3D array of [type, src, time] for the hyperprior on the relative prevalences R}
\item{\code{a_alpha}}{3D array of [source, time, location] for the prior on the alpha parameters}
\item{\code{s}}{vector giving initial group allocation for each type for the DP}