具有不同值的Rcpp函数填充矩阵

问题描述

我正在构建一个将实例化NumericMatrix并用Sorenson-Dice相似系数(相似矩阵)填充的过程。矩阵本身的尺寸可变,并取决于要处理的元素数量。通常,可以随时比较100多个单独的元素(因此,矩阵尺寸通常为100+ 100+)。到目前为止,我所构建的将创建矩阵,并计算系数,然后将这些计算出的值填充到矩阵中。但是,当我重复运行该函数时,我注意到矩阵内的值在每次运行之间都会发生变化,这不是预期的行为,因为要比较的数据在每次运行之间均未发生更改或重新排序。我也得到大于1的相似性,这绝对不应该发生。我有四个函数一个用于查找系数的分子,一个用于查找分母,一个用于使用分子和分母函数来计算系数,第四个用于将系数放入矩阵中。

这是c ++代码

// function to calculate the denominator of the dice coefficient
int diceDenomcpp(NumericVector val1,NumericVector val2){
  
  
  int val1Len = na_omit(val1).size();
  int val2Len = na_omit(val2).size();
  int bands = 0;
  
  
  bands = val1Len + val2Len;
  // return the computed total data points within both arrays
  
  
  return bands;
}

//######################################################################
//######################################################################
//######################################################################

// function to calculate the numerator for the dice coefficient
int diceNumcpp(NumericVector iso1,NumericVector iso2){
  
  // declare and initialize vectors with the element band data
  // remove any NA values within each vector
  NumericVector is1 = na_omit(iso1);
  NumericVector is2 = na_omit(iso2);
  
  // declare and initialize some counter variables
  int n = 0;
  int m = 0;
  int match = 0;
  
  // loop through the first element's first datum and check for matching datum
  // with the second element then continue to loop through each datum within each element 
  while (n<=is1.size()){
    if (m>=is2.size()){
      n++;
      m=0;
    }
    // if a suitable match is found,increment the match variable
    if((fabs(is1[n]-is2[m])/is1[n])<0.01 && (fabs(is1[n]-is2[m])/is2[m])<0.01){
      match++;
      
    }
    m++;
  }
  return match;
}

//########################################################################
//########################################################################
//########################################################################

// function to put the coefficient together
double diceCoefcpp(NumericVector val1,NumericVector val2){
  
  NumericVector is1 = clone(val1);
  NumericVector is2 = clone(val2);
  double dVal;
  double num = 2*diceNumcpp(is1,is2);
  double denom = diceDenomcpp(is1,is2);
  
  dVal = num/denom;
  
  return dVal;
  
}

//#######################################################################
//#######################################################################
//#######################################################################


// function to build the similarity matrix with the coefficients

NumericMatrix simMatGencpp(NumericMatrix df){
  
  // clone the input data frame
  NumericMatrix rapdDat = clone(df);

  // create a data frame for the output 
  NumericMatrix simMat(rapdDat.nrow(),rapdDat.nrow());
    std::fill(simMat.begin(),simMat.end(),NumericVector::get_na());
  
  // declare and initialize the iterator
  int i = 0;

  // declare and initialize the column counter
  int col = 0;  
  
  // declare an initialize the isolate counter
  int iso = 0;
  
  //simMat(_,0)=rapdDat(_,0);
  
  while (iso < rapdDat.nrow()){
    if (iso+i > rapdDat.nrow()){
      coL++;
      i=0;
      iso++;
    }
    if (iso+i < rapdDat.nrow()){
      simMat(iso+i,col) = diceCoefcpp(rapdDat(iso,_),rapdDat(iso+i,_));
      
    }
    i++;
  }
  
  
  //Rcout << "SimMatrix:" << simMat << "\n";
  
  return simMat;
}

这里是输入数据的样例。 。

sampleData

    band1  band2  band3  band4  band5  band6
1   593.05 578.04 439.01     NA     NA     NA
2   589.07 567.03     NA     NA     NA     NA
3   591.04 575.10 438.12     NA     NA     NA
4   591.04     NA     NA     NA     NA     NA
5   588.08 573.18     NA     NA     NA     NA
6   591.04 576.09 552.10     NA     NA     NA
7  1805.00 949.00 639.19 589.07 576.09 440.06
8   952.00 588.08 574.14 550.04     NA     NA
9  1718.00 576.09 425.01     NA     NA     NA
10 1708.00 577.05 425.01     NA     NA     NA

如果数据集足够小,则每次输出simMatGencpp()函数都会产生相同的结果,但是,当数据集变大时,值将开始在运行之间变化。

我尝试对单个元素独立运行diceNumcpp(),diceDenomcpp()和diceCoefcpp()函数,并且每次均获得一致的预期输出。一旦我使用了simMatGencpp(),那么输出就会再次变得混乱。因此,我尝试如下循环每个单独的函数

示例:

for(i in 1:100){
  print(diceNumcpp(sampleData[7,],sampleData[3,]))
}

上面的预期输出应该是3,但有时是4。每次运行此循环时,无论输出4是何时变化,有时是第二次迭代,有时是第14次迭代,或者根本没有迭代,或者一次是3次。行。

我的第一个想法是,也许由于垃圾回收并没有在c ++中完全发生,所以先前运行的函数调用可能会将旧的向量留在内存中,因为输出对象的名称在运行之间并没有改变。但是随后this帖子说,当函数退出时,在函数调用范围内创建的任何对象也会被破坏。

当我仅用R代码编写相同的解决方案时,运行时很烂,但每次都会始终返回具有相同值的矩阵或示例向量。

我很茫然。任何人在这个问题上可能遇到的任何帮助或光明,都将得到极大的欢迎!

感谢您的帮助。

更新2020-08-19

我希望这将有助于为那些更精通c ++的人提供一些见识,以便您可能对正在发生的事情有更多的了解。我有一些示例数据,类似于上面显示内容,该数据长187行,这意味着这些数据的相似性矩阵将包含17578个元素。我一直在使用这种代码和示例数据在该解决方案的R版本和该解决方案的C ++版本之间进行比较:

# create the similarity matrix with the R-solution to compare iteratively
# with another R-solution similarity matrix
simMat1 <- simMatGen(isoMat)
resultsR <- c()
for(i in 1:100){
  
  simMat2 <- simMatGen(isoMat)

  # check for any mis-matched elements in each matrix
  resultsR[[i]]<-length(which(simMat1 == simMat2)==TRUE)

  #######################################################################
  # everytime this runs I get the expected number of true values 17578
  # and check this by subtracting the mean(resultsR) from the expected 
  # number of true values of 17578 
}

mean(resultsR)

现在,当我使用C ++版本执行相同的过程时,事情会急剧而迅速地改变。我只是在64位和32位R-3.6.0上尝试了此操作。

simMat1 <- simMatGen(isoMat)
isoMat <- as.matrix(isoMat)
resultscpp <- c()
for(i in 1:10000){
  
  simMat2 <- simMatGencpp(isoMat)
  resultscpp[[i]]<-length(which(simMat1 == simMat2)==TRUE)

  ############  64 bit R  ##############
  # first iteration length(which(simMat1 == simMat2)==TRUE)-17578 equals 2
  # second iteration 740 elements differ: length(which(simMat1 == simMat2)==TRUE)-17578 equals 740 
  # third iteration 1142 elements differ
  # after 100 iterations the average difference is 2487.7 elements
  # after 10000 iterations the average difference is 2625.91 elements
  
  ############  32 bit R  ##############
  # first iteration difference = 1
  # second iteration difference = 694
  # 100 iterations difference = 2520.94
  # 10000 iterations difference = 2665.04
}

mean(resultscpp)

这是sessionInfo()

R version 3.6.0 (2019-04-26)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  Grdevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5        rstudioapi_0.10   magrittr_1.5      usethis_1.5.0     devtools_2.1.0    pkgload_1.0.2     R6_2.4.0          rlang_0.4.4      
 [9] tools_3.6.0       pkgbuild_1.0.3    sessioninfo_1.1.1 cli_1.1.0         withr_2.1.2       remotes_2.1.0     assertthat_0.2.1  digest_0.6.20    
[17] rprojroot_1.3-2   Crayon_1.3.4      processx_3.3.1    callr_3.2.0       fs_1.3.1          ps_1.3.0          testthat_2.3.1    memoise_1.1.0    
[25] glue_1.3.1        compiler_3.6.0    desc_1.2.0        backports_1.1.5   prettyunits_1.0.2

解决方法

在这里犯了一个菜鸟c ++错误。

在diceNumcpp()中,我没有进行任何检查,以免意外引用数组中的越界元素。

// if a suitable match is found,increment the match variable
    if((fabs(is1[n]-is2[m])/is1[n])<0.01 && (fabs(is1[n]-is2[m])/is2[m])<0.01){
      match++;
}

已更改为:

// if a suitable match is found,increment the match variable
    if(n<=(is1.size()-1) && (m<=is2.size()-1)){ // <- here need to make sure it stays inbounds 
     if((fabs(is1[n]-is2[m])/is1[n])<0.01 && (fabs(is1[n]-is2[m])/is2[m])<0.01){
       match++;
     }
    }

运行1000次后每次都能获得正确的结果。

每天学习新东西。

干杯。