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:
tensorA,
assertthat
Imports:
methods,
Rcpp (>= 1.0.4),
gtools,
hashmap,
R6,
cluster,
stats,
......@@ -28,7 +29,9 @@ Imports:
SPIn,
grDevices,
reshape2
RoxygenNote: 6.0.1
LinkingTo: Rcpp
DynLib: sourceR
RoxygenNote: 7.1.0
KeepSource: TRUE
LazyData: TRUE
Suggests:
......
......@@ -24,3 +24,5 @@ importFrom(SPIn,SPIn)
importFrom(grDevices,col2rgb)
importFrom(grDevices,colorRampPalette)
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(
names(nk_local) <- names(nk)
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]
logpiplus <- log(self$node$conc) +
......@@ -554,13 +554,11 @@ PoisGammaDPUpdate <- R6::R6Class(
self$node$theta$erase(s_old)
enqueue(self$node$idBucket, 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
}
# Update theta
# Sample theta from the conditional posterior
for (label in self$node$theta$keys())
{
sumy <- sum(y[self$node$s == label])
......
......@@ -354,14 +354,14 @@ DirichletProcessNode <- R6::R6Class( # TODO: Make this accept a generic base dis
self$baseRate <- rate
self$idBucket <- queue()
for(i in 1:length(s)) enqueue(self$idBucket, as.character(i))
h = replicate(length(theta), dequeue(self$idBucket))
self$theta <- hashmap::hashmap(h, theta)
self$s <- h[s]
keys = replicate(length(theta), dequeue(self$idBucket))
self$theta <- HashTable(keys, theta)
self$s <- keys[s]
},
getData = function()
self$theta$find(self$s),
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 @@
#' The main sourceR method is \code{\link{HaldDP}}.
#' @docType package
#' @name sourceR
#' @useDynLib sourceR
NULL
Rcpp::loadModule("HashTable_module")
......@@ -4,9 +4,8 @@
\name{AdaptiveDirMRW}
\alias{AdaptiveDirMRW}
\title{AdaptiveDirMRW}
\format{Object of \code{\link{R6Class}} with methods for updating a \link{DirichletNode} instance.}
\usage{
AdaptiveDirMRW
\format{
Object of \code{\link{R6Class}} with methods for updating a \link{DirichletNode} instance.
}
\value{
Object of \code{\link{AdaptiveDirMRW}}
......@@ -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
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{
\item{\code{cov}}{the current covariance}
......@@ -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{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}{
\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}
\keyword{node}
}
\if{html}{\out{<hr>}}
\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 @@
\name{AdaptiveLogDirMRW}
\alias{AdaptiveLogDirMRW}
\title{AdaptiveLogDirMRW}
\format{Object of \code{\link{R6Class}} with methods for updating a \link{DirichletNode} instance.}
\usage{
AdaptiveLogDirMRW
\format{
Object of \code{\link{R6Class}} with methods for updating a \link{DirichletNode} instance.
}
\value{
Object of \code{\link{AdaptiveLogDirMRW}}
......@@ -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
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{
\item{\code{cov}}{the current covariance}
......@@ -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{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}{
\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}
\keyword{node}
}
\if{html}{\out{<hr>}}
\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 @@
\name{AdaptiveMultiMRW}
\alias{AdaptiveMultiMRW}
\title{AdaptiveMultiMRW}
\format{Object of \code{\link{R6Class}} with methods for updating a \link{Node} instance.}
\usage{
AdaptiveMultiMRW
\format{
Object of \code{\link{R6Class}} with methods for updating a \link{Node} instance.
}
\value{
Object of \code{\link{AdaptiveMultiMRW}}
......@@ -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
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{
\item{\code{cov}}{the current covariance}
......@@ -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{node}}{the node to which the updater is attached}
}}
}
\if{html}{\out{</div>}}
}
\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}
\keyword{node}
}
\if{html}{\out{<hr>}}
\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
% Please edit documentation in R/data.R
\docType{data}
\name{Alpha_}
\alias{Alpha_}
\title{Alpha prior hyperparameter class}
\format{An object of class \code{R6ClassGenerator} of length 24.}
\usage{
Alpha_
}
\description{
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
% Please edit documentation in R/model.R
\docType{data}
\name{DPModel_impl}
\alias{DPModel_impl}
\title{Builds the source attribution model. Is not intended to be used by a regular user.
Developers only here!}
\format{\code{\link{R6Class}} object.}
\usage{
DPModel_impl
\format{
\code{\link{R6Class}} object.
}
\value{
Object of \code{\link{R6Class}}.
}
\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.
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}
\item{\code{theta}}{vector giving initial values for each group in the DP}
\item{\code{alpha}}{3D array of [source, time, location] giving initial values for the alpha
parameters}
}
\if{html}{\out{</div>}}
}
\section{Active bindings}{
\if{html}{\out{<div class="r6-active-bindings">}}
\describe{
\item{\code{y}}{3D array of [type, time, location] of the number of human cases}
......@@ -53,6 +94,57 @@ Developers only here!
\item{\code{alpha}}{3D array of [source, time, location] giving initial values for the alpha
parameters}
}}
}
\if{html}{\out{</div>}}
}
\section{Methods}{
\subsection{Public methods}{
\itemize{
\item \href{#method-new}{\code{DPModel_impl$new()}}
\item \href{#method-clone}{\code{DPModel_impl$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{DPModel_impl$new(
y,
X,
R,
Time,
Location,
Sources,
Type,
prev,
a_q,
a_theta,
b_theta,
a_r,
a_alpha,
s,
theta,
alpha
)}\if{html}{\out{</div>}}
}
\keyword{datasets}
}
\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{DPModel_impl$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 @@
\name{DataNode}
\alias{DataNode}
\title{DataNode}
\format{Object of \code{\link{R6Class}} with methods for constructing a DAG.}
\usage{
DataNode
\format{
Object of \code{\link{R6Class}} with methods for constructing a DAG.
}
\value{
Object of \code{\link{DataNode}}
......@@ -17,12 +16,6 @@ Represents a static data node in a DAG.
\details{
Derived from \link{Node}, please see base class documentation.
}
\section{Fields}{
\describe{
\item{\code{data}}{the data}
}}
\section{Methods}{
\describe{
......@@ -32,3 +25,68 @@ Derived from \link{Node}, please see base class documentation.
\keyword{DAG}
\keyword{node}
\section{Super class}{
\code{\link[sourceR:Node]{sourceR::Node}} -> \code{DataNode}
}