Nowosad / spatialexplain

http://jakubnowosad.com/spatialexplain/
Other
1 stars 0 forks source link

Parallelization #1

Open Nowosad opened 1 week ago

Nowosad commented 1 week ago

The result loop seems to be easily parallelized....

Nowosad commented 4 days ago
library(doFuture)
library(foreach)
registerDoFuture()
plan(multisession) 
  result = foreach(i = seq_len(nrow(x_df)), .combine = rbind,
                   .options.future = list(packages = c("DALEX", "stats"))) %dofuture% {
    if (stats::complete.cases(x_df[i, ])) {
      pp = DALEX::predict_parts(explainer, new_observation = x_df[i, ], type = type)
      if (type == "shap") {
        pp_mean_contribution = tapply(pp$contribution, pp$variable, mean, na.rm = TRUE)
        pp_df = data.frame(contribution = pp_mean_contribution,
                           variable_name = unique(pp$variable_name),
                           label = unique(pp$label))
      } else if (type %in% c("oscillations", "oscillations_uni", "oscillations_emp")) {
        pp_df = data.frame(contribution = pp$oscillations,
                           variable_name = pp$`_vname_`,
                           label = NA)
      } else {
        pp_df = data.frame(contribution = pp$contribution,
                           variable_name = pp$variable_name,
                           label = pp$label)
      }
      pp_df = stats::reshape(pp_df, idvar = "label", timevar = "variable_name", direction = "wide")
      names(pp_df) = gsub("contribution.", "", names(pp_df))
      pp_df[, colnames(result0)]
    } else {
      rep(NA_real_, ncol(result0))
    }
  }