|
| 1 | +context("read_preproc_scans.bids_project") |
| 2 | +library(testthat) |
| 3 | +library(bidser) |
| 4 | + |
| 5 | +create_read_preproc_fixture <- function() { |
| 6 | + tmp <- tempfile("bidser_read_preproc_") |
| 7 | + dir.create(tmp, recursive = TRUE) |
| 8 | + |
| 9 | + readr::write_tsv( |
| 10 | + tibble::tibble(participant_id = "sub-01"), |
| 11 | + file.path(tmp, "participants.tsv") |
| 12 | + ) |
| 13 | + jsonlite::write_json( |
| 14 | + list(Name = "ReadPreproc", BIDSVersion = "1.8.0"), |
| 15 | + file.path(tmp, "dataset_description.json"), |
| 16 | + auto_unbox = TRUE |
| 17 | + ) |
| 18 | + |
| 19 | + dir.create(file.path(tmp, "sub-01", "func"), recursive = TRUE) |
| 20 | + file.create(file.path(tmp, "sub-01", "func", "sub-01_task-rest_run-01_bold.nii.gz")) |
| 21 | + file.create(file.path(tmp, "sub-01", "func", "sub-01_task-rest_run-02_bold.nii.gz")) |
| 22 | + |
| 23 | + deriv_root <- file.path(tmp, "derivatives", "fmriprep", "sub-01", "func") |
| 24 | + dir.create(deriv_root, recursive = TRUE) |
| 25 | + jsonlite::write_json( |
| 26 | + list( |
| 27 | + Name = "fmriprep", |
| 28 | + BIDSVersion = "1.8.0", |
| 29 | + DatasetType = "derivative", |
| 30 | + GeneratedBy = list(list(Name = "fmriprep", Version = "test")) |
| 31 | + ), |
| 32 | + file.path(tmp, "derivatives", "fmriprep", "dataset_description.json"), |
| 33 | + auto_unbox = TRUE |
| 34 | + ) |
| 35 | + |
| 36 | + bold_arr <- array(seq_len(2 * 2 * 2 * 3), dim = c(2, 2, 2, 3)) |
| 37 | + mask_arr <- array(1, dim = c(2, 2, 2)) |
| 38 | + |
| 39 | + for (run in c("01", "02")) { |
| 40 | + RNifti::writeNifti( |
| 41 | + bold_arr, |
| 42 | + file.path( |
| 43 | + deriv_root, |
| 44 | + paste0("sub-01_task-rest_run-", run, "_space-MNI152NLin2009cAsym_desc-preproc_bold.nii.gz") |
| 45 | + ) |
| 46 | + ) |
| 47 | + RNifti::writeNifti( |
| 48 | + mask_arr, |
| 49 | + file.path( |
| 50 | + deriv_root, |
| 51 | + paste0("sub-01_task-rest_run-", run, "_space-MNI152NLin2009cAsym_desc-brain_mask.nii.gz") |
| 52 | + ) |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + tmp |
| 57 | +} |
| 58 | + |
| 59 | +test_that("read_preproc_scans returns one NeuroVec per matched file", { |
| 60 | + skip_if_not_installed("RNifti") |
| 61 | + skip_if_not_installed("neuroim2") |
| 62 | + |
| 63 | + tmp <- create_read_preproc_fixture() |
| 64 | + on.exit(unlink(tmp, recursive = TRUE, force = TRUE), add = TRUE) |
| 65 | + |
| 66 | + proj <- bids_project(tmp, fmriprep = TRUE, index = "none") |
| 67 | + fnames <- preproc_scans(proj, subid = "01", task = "rest", full_path = TRUE) |
| 68 | + scans <- bidser:::read_preproc_scans.bids_project(proj, subid = "01", task = "rest") |
| 69 | + |
| 70 | + expect_type(scans, "list") |
| 71 | + expect_length(scans, 2) |
| 72 | + expect_equal(names(scans), fnames) |
| 73 | + expect_true(all(vapply(scans, inherits, logical(1), "NeuroVec"))) |
| 74 | +}) |
0 commit comments