tpq / propr

[OFFICIAL] [CoDA] An R package to calculate proportionality and other measures for compositional data
65 stars 8 forks source link

Error in labRcpp(ncol(lr)) : Index out of bounds #15

Closed grayfall closed 4 years ago

grayfall commented 4 years ago

Hello again. Having resolved the overflow issue (https://github.com/tpq/propr/pull/14), I have proceeded with my work to find another bizarre bug. First of all, I'd like to reiterate that I no longer have issues with the number of features. The following code with synthetic data runs perfectly fine:

n.features <- 60000
n.samples <- 80
sample.counts <- rpois(n.features*n.samples, 1000) %>% matrix(nrow=n.samples, ncol=n.features)
phs <- propr(sample.counts, metric='phs')

Alert: Saving log-ratio transformed counts to @logratio.
Alert: Fixing permutations to active random seed.
Alert: Use '[' to index proportionality matrix.
Alert: Use 'updateCutoffs' to calculate FDR.

Now on to the new issue. It seems that propr can't handle an odd number of features. Here is an example

n.features <- 100 + 1
n.samples <- 10

sample.counts <- rpois(n.features*n.samples, 1000) %>% matrix(nrow=n.samples, ncol=n.features)
phs <- propr(sample.counts, metric='phs')

Alert: Saving log-ratio transformed counts to @logratio.
Alert: Fixing permutations to active random seed.
Error in labRcpp(ncol(lr)) : 
  Index out of bounds: [index=5000; extent=5000].

Here's the traceback

3. stop(structure(list(message = "Index out of bounds: [index=5000; extent=5000].", call = labRcpp(ncol(lr)), cppstack = NULL), class = c("Rcpp::index_out_of_bounds", "C++Error", "error", "condition")))
2. labRcpp(ncol(lr))
1. propr(sample.counts, metric = "phs")

The same thing happens with any odd number of features and never happens with an even number. To be clear, I'm running propr installed from the master branch with my PR. The PR hasn't changed any logic, so it shouldn't be at fault.

grayfall commented 4 years ago

I take my words back. It was my PR that broke things. Reordering int llt = nfeats * (nfeats - 1) / 2; into int llt = (nfeats / 2) * (nfeats - 1); solves the overflow issue, but it introduces a problem with integer division and rounding.