List names of data inputs is not used, only the order
Hi @vagudets, @abatalla, @tkariyat,
I found a bug in Apply(). When data
is provided as a named list, the names of the list are not used for the arguments to be passed in the correct order to the function. For instance, my function fun
has three arguments (x,y,z)
. If data = list(x = x, y = y, z = z)
, the result is different than if data = list(x = x, z = z, y = y)
.
library(multiApply)
fun <- function(x,y,z){
return(x+y-z)
}
x <- array(rnorm(1000), dim = c(lat = 2, lon = 5))
y <- array(rnorm(1000), dim = c(lat = 2, lon = 5))
z <- array(rnorm(1000), dim = c(lat = 2, lon = 5))
res_ordered_named <- Apply(data = list(x = x, y = y, z = z), fun = fun, target_dims = NULL, ncores = 1)$output1
res_notordered_named <- Apply(data = list(x = x, z = z, y = y), fun = fun, target_dims = NULL, ncores = 1)$output1
res_ordered_notnamed <- Apply(data = list(x, y, z), fun = fun, target_dims = NULL, ncores = 1)$output1
res_notordered_notnamed <- Apply(data = list(x, z, y), fun = fun, target_dims = NULL, ncores = 1)$output1
identical(res_ordered_named, res_notordered_named) # FALSE
identical(res_ordered_named, res_ordered_notnamed) # TRUE
identical(res_notordered_named, res_notordered_notnamed) # TRUE
This bug can be a big problem if any of our functions (those in s2dv, CSTools, ClimProjDiags, etc) that call Apply do not have the same order of the arguments as the atomic function, as well as if any of us is using Apply in their scripts (it's how I realised about this bug). I think all of them do have the same order, but I think it is worth checking it (I checked some in s2dv and they are not affected). I would also check in SUNSET, just in case.
Thanks a lot in advance,
Carlos @nperez @vtorralba @eduzenli