flr/FLCore

FLBiol to FLStock: wrong iter dimension

ssanchezAZTI opened this issue · 3 comments

When transforming a FLBiol object, with more than one iteration, into a FLStock object, the object obtained has different number of iterations within slots, as the number of iterations is set to one for the slots which are not inherited from the original object (i.e. all slots except: stock.n, stock.wt, m, mat, m.swpn and harvest.spwn).

Here it is an example and information on my R session:

library(FLBEIA)
data(oneIt)
a <- as(oneItBio[[1]],'FLStock')
dim(a@stock)
[1] 1 36 1 1 1 1
dim(a@stock.n)
[1] 12 36 1 1 1 3
dim(a@catch)
[1] 1 36 1 1 1 1
dim(a@catch.n)
[1] 12 36 1 1 1 1

sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

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

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

other attached packages:
[1] FLAssess_2.5.20150717 FLash_2.5.7 FLBEIA_1.15 ggplot2_2.2.1 FLFleet_2.5.20150311
[6] FLCore_2.6.4.9000 lattice_0.20-33 MASS_7.3-45 devtools_1.12.0

loaded via a namespace (and not attached):
[1] Rcpp_0.12.11 magrittr_1.5 munsell_0.4.3 colorspace_1.2-6 R6_2.1.2
[6] stringr_1.0.0 httr_1.2.1 plyr_1.8.4 tools_3.3.1 grid_3.3.1
[11] gtable_0.2.0 latticeExtra_0.6-28 git2r_0.15.0 withr_1.0.2 lazyeval_0.2.0
[16] digest_0.6.9 assertthat_0.1 tibble_1.1 Matrix_1.2-6 reshape2_1.4.2
[21] RColorBrewer_1.1-2 curl_0.9.7 memoise_1.0.0 labeling_0.3 stringi_1.1.1
[26] scales_0.4.1 stats4_3.3.1

The idea for this change in FLStock() is to save space, as all slots get extended for iterations even if they are initially empty or will only hold the same information across iters.

Is this a problem in your code? Arithmetic operations and other methods can handle it, but I will be glad to know what methods currently do not work across objects with and without iters.

Yes, some of the functions currently defined in FLBEIA did start crashing due to that change.

For example in perfectObs.

library(FLBEIA)
data(oneIt)
a <- as(oneItBio[[1]],'FLStock')
a@landings.n[] <- apply(landStock(oneItFl, 'stk1'), c(1:2,6),sum)
Error in do.call("[<-", c(list(x = y), iper, list(value = aperm(unname(value), :
number of items to replace is not a multiple of replacement length

However, if we take out the brackets for these assigments it works again...

a@landings.n <- apply(landStock(oneItFl, 'stk1'), c(1:2,6),sum)

That could be a posible way of fixing the problem. Is there any other alternative better than this?

You should not be using the square brackets to assign a whole object, as apply(landStock(oneItFl, 'stk1'), c(1:2,6),sum) returns already an FLQuant, which you can assign directly to the slot, using the replacement method

landings.n(a) <- apply(landStock(oneItFl, 'stk1'), c(1:2,6),sum)