Skip to content

Commit

Permalink
Bug repair to compile last changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Luc Janss committed May 17, 2021
1 parent 4f5c1b1 commit 07b5e77
Show file tree
Hide file tree
Showing 10 changed files with 659 additions and 46 deletions.
3 changes: 2 additions & 1 deletion src/dcModelTerm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ dcModelTerm::dcModelTerm(std::string modelTerm, Rcpp::DataFrame &d) :
}
else {
varianceObjects.push_back(getVariableObject(d,varianceNames[i]));
if(varianceObjects.back() == R_NilValue)
if(varianceObjects.back() == R_NilValue) {
throw generalRbayzError("Kernel object not found: "+varianceNames[i]);
}
varianceType=2; // flag variance type 2 if some matrices are not IDEN, WEI, MIXT
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/kernelMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class kernelMatrix : public labeledMatrix {
}
Rcpp::NumericVector eigvalues = eigdecomp["values"];
Rcpp::NumericMatrix eigvectors = eigdecomp["vectors"];
// re-attach the dimnames again to the eigvectors matrix for correct further processing
if (kerneldata.hasAttribute("dimnames")) {
Rcpp::List dimnames = Rcpp::as<Rcpp::List>(kerneldata.attr("dimnames"));
eigvectors.attr("dimnames") = dimnames;
}
// Determine how many evectors to keep
double rrankpct=90; // rrankpct not coming correctly from the modelterm now
size_t nColUsed = 0;
Expand All @@ -41,10 +46,9 @@ class kernelMatrix : public labeledMatrix {
double eval_cutoff = rrankpct * sumeval / 100.0l;
sumeval = 0.0l;
while (sumeval < eval_cutoff) sumeval += eigvalues[nColUsed++];
// this message can be moved to the modelTerm routine; and it must be different for ran2f_2cor.
Rcpp::Rcout << "For kernel " << name << " with rrankpct=" << rrankpct << " using "
<< nColUsed << " eigenvectors\n";
this->initWith(eigvectors, nColUsed);
this->initWith(eigvectors, name, nColUsed);
weights.initWith(eigvalues, nColUsed);
}

Expand Down Expand Up @@ -99,14 +103,14 @@ class kernelMatrix : public labeledMatrix {
tempLabels.push_back(this->rownames[rowi]+"%"+K2->rownames[rowj]);
}
}
Rcpp::Rcout << "Interaction kernel retains " << nEvalUsed << " eigenvalues\n";
// Swap the old data with the new data.
// Note the old data is removed when tempEvecs, tempEvals and tempLabels here go out of scope.
this->swap(&tempEvecs);
weights.swap(&tempEvals);
std::swap(rownames,tempLabels);
}

std::vector<std::string> rownames, colnames;
simpleDblVector weights;

};
Expand Down
8 changes: 4 additions & 4 deletions src/labeledMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class labeledMatrix : public simpleMatrix {

public:

labeledMatrix() : simpleMatrix() { }
labeledMatrix() : simpleMatrix() {
}

labeledMatrix(Rcpp::RObject col, std::string & name) : simpleMatrix(col) {
// I need to temporarily redo the conversion of the input Robject to
Expand All @@ -34,13 +35,12 @@ class labeledMatrix : public simpleMatrix {
addMatrixNames(colnames, tempdata, 2); // no throw here, colnames are optional
}

void initWith(Rcpp::NumericMatrix M, size_t useCol) {
void initWith(Rcpp::NumericMatrix & M, std::string & name, size_t useCol) {
simpleMatrix::initWith(M, useCol);
if (addMatrixNames(rownames, M, 1) >0) {
throw generalRbayzError("No rownames on matrix xxx\n"); // no name available here!
throw generalRbayzError("No rownames on matrix "+name);
}
addMatrixNames(colnames, M, 2); // no throw here, colnames are optional

}

~labeledMatrix() {
Expand Down
12 changes: 7 additions & 5 deletions src/modelRanf_cor.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,21 @@ class modelRanf_cor : public modelFactor {
}
// Get the first kernel and then add (making kronecker products) with second etc., if available
K = new kernelMatrix(modeldescr.varianceObjects[0], modeldescr.varianceNames[0]);
Rcpp::Rcout << "1\n";
if (modeldescr.varianceNames.size()==2) { // combine with a second kernel if present
Rcpp::Rcout << "2\n";
kernelMatrix* K2 = new kernelMatrix(modeldescr.varianceObjects[1], modeldescr.varianceNames[1]);
Rcpp::Rcout << "3\n";
K->addKernel(K2);
Rcpp::Rcout << "4\n";
delete K2;
}
Rcpp::Rcout << "5\n";
if (modeldescr.varianceNames.size()>2) { // need to think if I can keep combining kernels with addKernel()
throw(generalRbayzError("Not yet ready to combine more than 2 kernels for interaction"));
}
// note: modelled parameters are regressions on the eigenvectors and are stored in separate
// vector regcoeff (size K->ncol). The par-vector has backtransfor to random effects
// (eigen-vectors x regressions, size K->nrow).
par.initWith(K->nrow,0.0l);
parLabels = K->rownames; // I believe this makes a deep copy, but a shallow copy would be enough
// note: par-vector and names are set-up in modelFactor and follow levels of the data factor.
// Here add a vector regcoeff (size K->ncol) to hold the regresssion on eigenvectors.
regcoeff.initWith(K->ncol, 0.0l);
builObsIndex(obsIndex,F,K);
}
Expand Down
30 changes: 16 additions & 14 deletions src/nameTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,30 @@ inline bool compString2Pair(const std::pair<std::string, size_t> & p, const std:
return p.first < s;
}

// getMatrixNames: retrieve row or col-names (use dim=1 or 2 as in apply()) from an Rcpp
// NumericMatrix, returns NULL vector when dimnames not present or the requested dim is empty.
// addMatrixNames: fills matrix row or col-names (dim=1 or 2) in an c++ vector<string>,
// return value 0 for success, 1 for errors (missing names).
// Note: addMatrixNames uses push_back, typical use is to pass an empty vector<string> as 'names'
// argument and it will be filled.
// argument and it will be filled.

Rcpp::CharacterVector getMatrixNames(Rcpp::NumericMatrix & mat, int dim) {
int addMatrixNames(std::vector<std::string> & names, Rcpp::NumericMatrix & mat, int dim) {
if (mat.hasAttribute("dimnames")) {
Rcpp::List dimnames = Rcpp::as<Rcpp::List>(mat.attr("dimnames"));
if(dim==1) return dimnames[0];
else return dimnames[1];
if(dim==1 && dimnames[0] != R_NilValue) {
Rcpp::CharacterVector matNames = Rcpp::as<Rcpp::CharacterVector>(dimnames[0]);
CharVec2cpp(names, matNames);
return 0; // sucess
}
else if (dim==2 && dimnames[1] != R_NilValue) {
Rcpp::CharacterVector matNames = Rcpp::as<Rcpp::CharacterVector>(dimnames[1]);
CharVec2cpp(names, matNames);
return 0; // sucess
}
else {
return 1; // the dimnames for the requested dim must have been empty
}
}
else return R_NilValue;
}

int addMatrixNames(std::vector<std::string> & names, Rcpp::NumericMatrix & mat, int dim) {
Rcpp::CharacterVector matNames = getMatrixNames(mat, dim);
if(matNames.isNULL()) return 1;
else {
CharVec2cpp(names, matNames);
return 0;
return 1; // no dimnames attributes
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/rbayz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ void collectLoggedSamples(std::vector<modelBase *> & model, Rcpp::IntegerVector

// [[Rcpp::export]]
Rcpp::List rbayz_cpp(Rcpp::Formula modelFormula, Rcpp::DataFrame inputData,
Rcpp::IntegerVector chain, int silent) {

Rcpp::IntegerVector chain, int silent)
{
// Some check of chain settings is needed. Also the rbayz wrapper function now
// handles chain being NULL, but it can be done here, so that warning message
// can come in output.
Expand Down Expand Up @@ -97,8 +97,8 @@ Rcpp::List rbayz_cpp(Rcpp::Formula modelFormula, Rcpp::DataFrame inputData,
size_t nEstimates = estimNames.size();

// Check the chain settings and make list of output sample cycle-numbers.
if (chain[1]==0 && chain[2]==0 && chain[3]==0) { // chain was not set
chain[1]=1100; chain[2]=100; chain[3]=10;
if (chain[0]==0 && chain[1]==0 && chain[2]==0) { // chain was not set
chain[0]=1100; chain[1]=100; chain[2]=10;
Rcpp::Rcout << "Warning: chain was not set, running 1100 cycles but it may be too short for many analyses\n";
}
if (chain.size() != 3) throw (generalRbayzError("The chain settings do not have 3 elements"));
Expand Down
3 changes: 2 additions & 1 deletion src/simpleMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class simpleMatrix {

public:

simpleMatrix() { }
simpleMatrix() {
}

simpleMatrix(size_t nr, size_t nc) {
doalloc(nr, nc);
Expand Down
6 changes: 3 additions & 3 deletions src/simpleVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class simpleDblVector {

public:

simpleDblVector() : nelem(0) { }
simpleDblVector() { }

simpleDblVector(size_t n) {
doalloc(n);
Expand Down Expand Up @@ -107,7 +107,7 @@ class simpleDblVector {
throw(generalRbayzError("useElem is larger than actual nelem in simpleDblVector"));
}
doalloc(useElem);
for(size_t i=0; i<nelem; i++)
for(size_t i=0; i<useElem; i++)
data[i] = v[i];
}

Expand All @@ -131,7 +131,7 @@ class simpleDblVector {
}

double *data;
size_t nelem;
size_t nelem=0;

private:
void doalloc(size_t n) {
Expand Down

0 comments on commit 07b5e77

Please sign in to comment.