void IGMeasurementError::initFromList(Rcpp::List const &init_list) { NormalVarianceMixtureBaseError::initFromList(init_list); if(init_list.containsElementNamed("nu")) nu = Rcpp::as < double >( init_list["nu"]); else nu = 1.; EV = 1.; // not true it is the mode is alpha/(alpha - 1) EiV = 1.; npars += 1; digamma_nu = Digamma(nu); trigamma_nu = Trigamma(nu); int i = 0; if( init_list.containsElementNamed("Vs" )){ Rcpp::List Vs_list = init_list["Vs"]; Vs.resize(Vs_list.length()); for( Rcpp::List::iterator it = Vs_list.begin(); it != Vs_list.end(); ++it ) { Vs[i++] = Rcpp::as < Eigen::VectorXd >( it[0]); } }else throw("in IGMeasurementError::initFromList Vs must be set! \n"); }
void NormalVarianceMixtureBaseError::initFromList(Rcpp::List const &init_list) { if(init_list.containsElementNamed("sigma")) sigma = Rcpp::as < double >( init_list["sigma"]); else sigma = 1.; npars += 1; if(init_list.containsElementNamed("common_V")) common_V = Rcpp::as < int >( init_list["common_V"]); else common_V = 0; int i = 0; if( init_list.containsElementNamed("Vs" )){ Rcpp::List Vs_list = init_list["Vs"]; Vs.resize(Vs_list.length()); for( Rcpp::List::iterator it = Vs_list.begin(); it != Vs_list.end(); ++it ) { Vs[i++] = Rcpp::as < Eigen::VectorXd >( it[0]); } }else throw("in NormalVarianceMixtureBaseError::initFromList Vs must be set! \n"); }
void RSim::setInputSpikes(const Rcpp::List &l, const string &obj_name) { Ptr<SerializableBase> sp_l; if(l.containsElementNamed("values")) { sp_l = RProto::convertFromR<SerializableBase>(l); } else { try { Rcpp::List sl; sl["values"] = l; sl.attr("class") = "SpikesList"; sp_l = RProto::convertFromR<SerializableBase>(sl); } catch (...) { ERR("Expecting list with spike times of neurons or SpikesList list object\n"); } } auto slice = Factory::inst().getObjectsSlice(obj_name); for(auto it=slice.first; it != slice.second; ++it) { Factory::inst().getObject(it)->setAsInput( sp_l ); } net->spikesList().info = sp_l.as<SpikesList>()->info; for(auto &n: neurons) { n.ref().initInternal(); } }
void constMatrix::initFromList(Rcpp::List const & init_list) { npars = 1; std::vector<std::string> check_names = {"Q","loc", "h"}; check_Rcpplist(init_list, check_names, "constMatrix::initFromList"); Q = Rcpp::as<Eigen::SparseMatrix<double,0,int> >(init_list["Q"]); d = Q.rows(); int nIter = Rcpp::as<double>(init_list["nIter"]); tauVec.resize(nIter+1); npars = 0; v.setZero(1); m.resize(1,1); tau = 1.; if(init_list.containsElementNamed("tau")) tau = Rcpp::as<double >( init_list["tau"]); Q *= tau; dtau = 0.; ddtau = 0.; counter = 0; tauVec[counter] = tau; counter++; loc = Rcpp::as< Eigen::VectorXd >(init_list["loc"]); h = Rcpp::as< Eigen::VectorXd >(init_list["h"]); h_average = h.sum() / h.size(); m_loc = loc.minCoeff(); }
SEXP stochasticEnumeration(SEXP nVertices_sexp, SEXP budget_sexp, SEXP seed_sexp, SEXP options_sexp) { BEGIN_RCPP int seed = Rcpp::as<int>(seed_sexp); int nVertices = Rcpp::as<int>(nVertices_sexp); int budget = Rcpp::as<int>(budget_sexp); Rcpp::List options = options_sexp; bool outputSamples = false; if(!options.containsElementNamed("reduceChains")) throw std::runtime_error("Unable to find option named reduceChains"); if(!options.containsElementNamed("graphRepresentation")) throw std::runtime_error("Unable to find option named graphRepresentation"); bool reduceChains = Rcpp::as<bool>(options("reduceChains")); std::string graphRepresentationString = Rcpp::as<std::string>(options("graphRepresentation")); graphRepresentation representation = toRepresentation(graphRepresentationString); if (options.containsElementNamed("outputSamples")) { outputSamples = Rcpp::as<bool>(options("outputSamples")); } boost::mt19937 randomSource; randomSource.seed(seed); chordalGraph::stochasticEnumerationArgs args(randomSource); args.nVertices = nVertices; args.budget = budget; args.outputSamples = outputSamples; int maxEdges = ((nVertices - 1)*nVertices / 2); //Set up text progress bar Rcpp::Function txtProgressBar("txtProgressBar"); Rcpp::Function close("close"); Rcpp::Function setTxtProgressBar("setTxtProgressBar"); Rcpp::RObject barHandle = txtProgressBar(Rcpp::Named("style") = 3, Rcpp::Named("min") = 0, Rcpp::Named("max") = maxEdges, Rcpp::Named("initial") = 0); //List for samples Rcpp::List samples(maxEdges+1); std::vector<bool> exactVector; std::vector<int> minimumSizeForExactVector; std::vector<std::string> estimatesAsStrings; for (int nEdges = 0; nEdges < maxEdges+1; nEdges++) { setTxtProgressBar(barHandle, nEdges); args.nEdges = nEdges; args.samples.clear(); if(reduceChains) { if(representation == listRepresentation) { chordalGraph::stochasticEnumerationReduceChains<chordalGraph::cliqueTree>(args); } else { chordalGraph::stochasticEnumerationReduceChains<chordalGraph::cliqueTreeAdjacencyMatrix>(args); } } else { if(representation == listRepresentation) { chordalGraph::stochasticEnumeration<chordalGraph::cliqueTree>(args); } else { chordalGraph::stochasticEnumeration<chordalGraph::cliqueTreeAdjacencyMatrix>(args); } } estimatesAsStrings.push_back(args.estimate.str()); exactVector.push_back(args.exact); if (args.outputSamples) { Rcpp::List currentEdgesSamples; convert(currentEdgesSamples, args.samples); samples[nEdges] = currentEdgesSamples; } if(args.minimumSizeForExact == -1) { minimumSizeForExactVector.push_back(NA_INTEGER); } else { minimumSizeForExactVector.push_back(args.minimumSizeForExact); } } close(barHandle); SEXP estimatesAsStrings_sexp = Rcpp::wrap(estimatesAsStrings); SEXP minimumSizeForExactVector_sexp = Rcpp::wrap(minimumSizeForExactVector); Rcpp::Function mpfrFunction("mpfr"); Rcpp::List listResult = Rcpp::List::create(Rcpp::Named("data") = mpfrFunction(estimatesAsStrings_sexp, Rcpp::Named("prec", 50)), Rcpp::Named("exact") = Rcpp::wrap(exactVector), Rcpp::Named("minimumSizeForExact") = minimumSizeForExactVector_sexp); if (args.outputSamples) { listResult("samples") = samples; } return listResult; END_RCPP }
SEXP stochasticEnumerationSpecificEdges(SEXP nVertices_sexp, SEXP nEdges_sexp, SEXP budget_sexp, SEXP seed_sexp, SEXP options_sexp) { BEGIN_RCPP int seed = Rcpp::as<int>(seed_sexp); int nVertices = Rcpp::as<int>(nVertices_sexp); int nEdges = Rcpp::as<int>(nEdges_sexp); int budget = Rcpp::as<int>(budget_sexp); Rcpp::List options = options_sexp; bool outputSamples = false; if(!options.containsElementNamed("reduceChains")) throw std::runtime_error("Unable to find option named reduceChains"); if(!options.containsElementNamed("graphRepresentation")) throw std::runtime_error("Unable to find option named graphRepresentation"); bool reduceChains = Rcpp::as<bool>(options("reduceChains")); std::string graphRepresentationString = Rcpp::as<std::string>(options("graphRepresentation")); graphRepresentation representation = toRepresentation(graphRepresentationString); if (options.containsElementNamed("outputSamples")) { outputSamples = Rcpp::as<bool>(options("outputSamples")); } boost::mt19937 randomSource; randomSource.seed(seed); chordalGraph::stochasticEnumerationArgs args(randomSource); args.nEdges = nEdges; args.nVertices = nVertices; args.budget = budget; args.outputSamples = outputSamples; if(reduceChains) { if(representation == listRepresentation) { chordalGraph::stochasticEnumerationReduceChains<chordalGraph::cliqueTree>(args); } else { chordalGraph::stochasticEnumerationReduceChains<chordalGraph::cliqueTreeAdjacencyMatrix>(args); } } else { if(representation == listRepresentation) { chordalGraph::stochasticEnumeration<chordalGraph::cliqueTree>(args); } else { chordalGraph::stochasticEnumeration<chordalGraph::cliqueTreeAdjacencyMatrix>(args); } } std::string estimateAsString = args.estimate.str(); SEXP estimateAsString_sexp = Rcpp::wrap(estimateAsString); SEXP minimumSizeForExact_sexp; if(args.minimumSizeForExact != -1) minimumSizeForExact_sexp = Rcpp::wrap(args.minimumSizeForExact); else minimumSizeForExact_sexp = Rcpp::wrap(NA_INTEGER); Rcpp::Function mpfrFunction("mpfr"); Rcpp::List listResult = Rcpp::List::create(Rcpp::Named("data") = mpfrFunction(estimateAsString_sexp, Rcpp::Named("prec", 50)), Rcpp::Named("exact") = Rcpp::wrap(args.exact), Rcpp::Named("minimumSizeForExact") = minimumSizeForExact_sexp); if (args.outputSamples) { Rcpp::List samples; convert(samples, args.samples); listResult("samples") = samples; } return listResult; END_RCPP }