Skip to content

Commit

Permalink
Enhance run support
Browse files Browse the repository at this point in the history
Actually run should be placed after task field, but for historical reasons it is placed at the last column. Here workaround it by using `relocate()`.

Signed-off-by: Liang Zhang <psychelzh@outlook.com>
  • Loading branch information
psychelzh committed Mar 3, 2024
1 parent dfbfd8f commit 75f1a26
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 42 deletions.
52 changes: 37 additions & 15 deletions R/constants.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,33 +76,55 @@ params_fmri_tasks <- tibble::tribble(
"1", "movie", 2,
"2", "rest", 2,
"2", "wm", 3,
"2", "movie", 2,
"0", "latent", 0
"2", "movie", 2
) |>
tidyr::uncount(runs, .id = "run_id")
# Note: sesion: 0 = all sessions, run_id: 0 = all runs
params_fmri_special <- dplyr::bind_rows(
tibble::tibble(
session = "0",
task = "latent",
run_id = c(0, 1)
),
tibble::tibble(
session = "0",
task = c("rest", "movie"),
run_id = 0
)
)

params_conmat <- tibble::tibble(
xcpd = c(
"gsr", # with global signal regression
"no_gsr" # no global signal regression
),
atlas = "4S256Parcels"
)
) |>
dplyr::filter(xcpd == "gsr")
config_fmri <- tidyr::expand_grid(
params_fmri_tasks,
params_conmat
) |>
dplyr::filter(xcpd == "gsr")
)
config_fc <- dplyr::bind_rows(
config_fmri,
config_fmri |>
dplyr::mutate(run = "full") |>
dplyr::select(-runs),
config_fmri |>
tidyr::uncount(runs, .id = "run") |>
dplyr::mutate(run = paste("run", run, sep = "")),
config_fmri |>
dplyr::filter(task == "wm") |>
dplyr::mutate(run = "run12") |>
dplyr::select(-runs)
)
dplyr::distinct(dplyr::pick(!run_id)) |>
dplyr::mutate(
run_id = ifelse(task == "wm", list(c(0, 12)), list(0))
) |>
tidyr::unnest(run_id),
tidyr::expand_grid(params_fmri_special, params_conmat)
) |>
dplyr::mutate(
run = ifelse(
run_id == 0,
"full",
sprintf("run%d", run_id)
),
.keep = "unused",
# backward compatibility
.after = dplyr::last_col()
)

# used in CPM modeling building
hypers_cpm <- dplyr::bind_rows(
Expand Down
3 changes: 2 additions & 1 deletion R/prepare_data_neural.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# functional connectivity data preparation ----
prepare_meta_time_series <- function(xcpd, session, task, atlas) {
prepare_meta_time_series <- function(xcpd, session, task, atlas, run) {
path_xcpd <- fs::path(
Sys.getenv("ROOT_BIDS_DERIV"),
sprintf("xcpd_%s", xcpd)
Expand All @@ -10,6 +10,7 @@ prepare_meta_time_series <- function(xcpd, session, task, atlas) {
session = session,
task = task,
atlas = atlas,
run = run,
suffix = "timeseries",
extension = "tsv"
)
Expand Down
47 changes: 21 additions & 26 deletions _scripts/prepare_neural.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ tar_option_set(
controller = setup_crew_controller("fc")
)
setup_parallel_plan()
config_meta <- config_fmri |>
dplyr::select(!runs) |>

config_fc_calc <- config_fc |>
tidyr::unite("name_suffix", everything(), remove = FALSE) |>
dplyr::mutate(
session = lapply(session, \(x) if (x == "0") character() else x),
run = lapply(run, parse_digits),
meta_time_series = rlang::syms(
sprintf("meta_time_series_%s", name_suffix)
)
paste0("meta_time_series_", name_suffix)
),
fc = rlang::syms(paste0("fc_", name_suffix))
)
config_fc_calc <- config_fc |>
dplyr::left_join(config_meta, by = c("xcpd", "session", "task", "atlas")) |>
dplyr::mutate(fc = rlang::syms(sprintf("fc_%s_%s", name_suffix, run)))

targets_fd <- tarchetypes::tar_map(
dplyr::filter(params_fmri_tasks, task != "latent"),
dplyr::distinct(params_fmri_tasks, session, task),
names = !runs,
tar_target(
meta_data_motion,
Expand All @@ -34,15 +34,17 @@ targets_fd <- tarchetypes::tar_map(
list(
# prepare functional connectivity (FC) data ----
tarchetypes::tar_eval(
tar_target(
meta_time_series,
prepare_meta_time_series(xcpd, session, task, atlas)
list(
tar_target(
meta_time_series,
prepare_meta_time_series(xcpd, session, task, atlas, run)
),
tar_target(
fc,
prepare_data_fc(meta_time_series)
)
),
dplyr::filter(config_meta, task != "latent")
),
tarchetypes::tar_eval(
tar_target(fc, prepare_data_fc(meta_time_series)),
dplyr::filter(config_fc_calc, run == "full" & task != "latent")
dplyr::filter(config_fc_calc, task != "latent")
),
tarchetypes::tar_eval(
tar_target(
Expand All @@ -54,23 +56,16 @@ list(
dplyr::filter(task == "latent") |>
dplyr::left_join(
config_fc_calc |>
dplyr::filter(run == "full" & task != "latent") |>
dplyr::filter(task != "latent") |>
dplyr::summarise(
call_list_fc = list(
as.call(c(quote(list), fc))
),
.by = xcpd
.by = c(xcpd, run)
),
by = "xcpd"
by = c("xcpd", "run")
)
),
tarchetypes::tar_eval(
tar_target(
fc,
prepare_data_fc(meta_time_series, .data[["run"]] %in% parse_digits(run))
),
dplyr::filter(config_fc_calc, grepl("^run\\d+$", run))
),
# extract mean frame-wise displacement (FD) ----
targets_fd,
tarchetypes::tar_combine(
Expand Down

0 comments on commit 75f1a26

Please sign in to comment.