From 2f9b30a2161b7128d6919ea5627b9e4ab0aa1f61 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Thu, 29 Feb 2024 17:44:14 +0800 Subject: [PATCH] Add permutations and use new paradigms Signed-off-by: Liang Zhang --- _scripts/predict_phenotypes.R | 107 ++++++++++++++++++++++------------ 1 file changed, 70 insertions(+), 37 deletions(-) diff --git a/_scripts/predict_phenotypes.R b/_scripts/predict_phenotypes.R index c6bd71f..d1aebc2 100644 --- a/_scripts/predict_phenotypes.R +++ b/_scripts/predict_phenotypes.R @@ -7,71 +7,104 @@ tar_option_set( ) setup_parallel_plan() +config_cpm <- prepare_config_cpm( + config == "gsr", + run == "full", + thresh_method == "alpha", + thresh_level == 0.01 +) cpm_branches <- tarchetypes::tar_map( config_cpm, - names = !starts_with("file"), - tarchetypes::tar_rep( + names = !c(file_fc, fd), + tar_target( cpm_result, apply( - qs::qread(file_scores_factor)[subjs_to_keep, ], 2, - \(scores) { - cpmr::cpm( - qs::qread(file_fc)[subjs_to_keep, ], - scores, - confounds = qs::qread(file_confounds)[subjs_to_keep, ], - thresh_method = thresh_method, - thresh_level = thresh_level, - kfolds = 10 - ) - } + scores, 2, + perform_cpm, + fc = qs::qread(file_fc)[subjs_keep_neural, ], + confounds = match_confounds(users_confounds, fd), + bias_correct = FALSE, + thresh_method = thresh_method, + thresh_level = thresh_level ), - batches = 4, - reps = 5, - iteration = "list", retrieval = "worker", storage = "worker" ), - tarchetypes::tar_rep2( + tar_target( cpm_performance, lapply_tar_batches( cpm_result, extract_cpm_performance ) |> list_rbind(names_to = "latent"), - cpm_result, retrieval = "worker", storage = "worker" ) ) -list( - tar_target( - file_scores_factor, - path_obj_from_proj("scores_bf_full", "confirm_factors"), - format = "file_fast" + +cpm_branches_perms <- tarchetypes::tar_map( + dplyr::filter( + config_cpm, + task == "am", + thresh_method == "alpha", + thresh_level == 0.01 ), + names = !c(file_fc, fd), + tarchetypes::tar_rep( + cpm_result_perm, + apply( + scores, 2, + perform_cpm_perm, + fc = qs::qread(file_fc)[subjs_keep_neural, ], + confounds = match_confounds(users_confounds, fd), + bias_correct = FALSE, + thresh_method = thresh_method, + thresh_level = thresh_level + ), + batches = 100, + reps = 10, + iteration = "list", + retrieval = "worker", + storage = "worker" + ), + tarchetypes::tar_rep2( + cpm_performance_perm, + lapply_tar_batches( + cpm_result_perm, + extract_cpm_performance + ) |> + list_rbind_tar_batches(names_to = "index"), + cpm_result_perm, + retrieval = "worker", + storage = "worker" + ) +) + +list( tarchetypes::tar_file_read( - subjs_keep_neural, - path_obj_from_proj("subjs_keep_neural", "prepare_neural"), + scores, + path_obj_from_proj("scores", "cognition_structure"), read = qs::qread(!!.x) ), - tar_target( - subjs_to_keep, - # intersect() does not work for integer64 - # https://github.com/truecluster/bit64/issues/29 - intersect( - as.character(subjs_keep_neural), - rownames(qs::qread(file_scores_factor)) - ) - ), - tar_prep_files_cpm(), + tar_prepare_cpm(), cpm_branches, tarchetypes::tar_combine( cpm_performance, cpm_branches$cpm_performance, command = bind_rows_meta( !!!.x, - .prefix = "cpm_performance", - .type = "cpm" + .names = c(names(config_fc), names(hypers_cpm)), + .prefix = "cpm_performance" + ) + ), + cpm_branches_perms, + tarchetypes::tar_combine( + cpm_performance_perm, + cpm_branches_perms$cpm_performance_perm, + command = bind_rows_meta( + !!!.x, + .names = c(names(config_fc), names(hypers_cpm)), + .prefix = "cpm_performance_perm" ) ) )