Skip to content
GitLab
Projects Groups Topics Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in
  • multiApply multiApply
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributor statistics
    • Graph
    • Compare revisions
  • Issues 6
    • Issues 6
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 0
    • Merge requests 0
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Artifacts
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Computational Earth SciencesComputational Earth Sciences
  • multiApplymultiApply
  • Merge requests
  • !34

Reorder data elements to match fun arguments when names are provided

  • Review changes

  • Download
  • Patches
  • Plain diff
Merged vagudets requested to merge dev-reorder_named_data into master May 28, 2025
  • Overview 2
  • Commits 8
  • Pipelines 8
  • Changes 4

#20 (closed)

Apply() allows for the input data to be a named list, but the names are not used when applying the user-defined function fun. Instead, Apply() expects the order of the elements in data to match the order of the arguments in fun:

#'@param data One or a list of vectors, matrices or arrays. They must be in the
#'  same order as expected by the function provided in the parameter 'fun'. The
#'  dimensions do not necessarily have to be ordered. If the 'target_dims'
#'  require a different order than the provided, \code{Apply} will automatically
#'  reorder the dimensions as needed.

In this MR, if fun has arguments and data is a named list, data is reordered inside Apply() to match the order of the arguments in fun. If there are both named and unnamed elements, the unnamed elements will be placed at the end, in the same original order. This can be useful for example in the case where fun has the ... parameter, as in the following example:

library(multiApply)
source("R/Apply.R")
source("R/zzz.R")
library(abind)
library(plyr)
library(ClimProjDiags)
library(s2dv)

obs <- rnorm(10)
hcst1 <- rnorm(100)
hcst2 <- rnorm(100)

dim(obs) <- c(time = 1, syear = 10, ensemble = 1)
dim(hcst1) <- c(time = 1, syear = 10, ensemble = 10)
dim(hcst2) <- c(time = 1, syear = 10, ensemble = 10)

hcst <- list(hcst1, hcst2)

datos <- append(list(obs = obs), hcst)

crps <- Apply(datos, target_dims = c('syear', 'ensemble'),
              fun = function(obs, ...) {
                res <- abind(..., along = 2)
                names(dim(res)) <- names(dim(obs))
                obs <- Subset(obs, along = 'ensemble',
                             indices = 1, drop = 'selected')
                mean(s2dv:::.CRPS(exp = res, obs = obs, dat_dim = NULL,
                                  time_dim = 'syear',
                                  memb_dim = 'ensemble'))},
                ncores = 4)$output1

datos_notordered <- append(hcst, list(obs = obs))
crps_notordered <- Apply(datos_notordered, target_dims = c('syear', 'ensemble'),
              fun = function(obs, ...) {
                res <- abind(..., along = 2)
                names(dim(res)) <- names(dim(obs))
                obs <- Subset(obs, along = 'ensemble',
                             indices = 1, drop = 'selected')
                mean(s2dv:::.CRPS(exp = res, obs = obs, dat_dim = NULL,
                                  time_dim = 'syear',
                                  memb_dim = 'ensemble'))},
                ncores = 4)$output1

identical(crps, crps_notordered)

TODO:

  • Review sanity checks, add if necessary
  • Apply reorder to target_dims -> Not necessary, as target_dims names are already mapped to fun argument names
Edited May 28, 2025 by vagudets
Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: dev-reorder_named_data