Skip to content

Commit c208afa

Browse files
committed
Ignore mask sidecar JSON files
1 parent 530141c commit c208afa

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

R/bidsio.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ create_preproc_mask.bids_project <- function(x, subid, thresh=.99,
235235
}
236236
maskfiles <- unique(maskfiles)
237237

238+
# Restrict to actual image masks; search_files can also return JSON sidecars
239+
# that share the same desc/kind entities.
240+
maskfiles <- maskfiles[grepl("\\.nii(\\.gz)?$", maskfiles)]
241+
238242
# Restrict to functional (BOLD) masks: require `_task-` in the filename.
239243
# Anatomical brain masks (anat/) lack a task entity, so this reliably
240244

tests/testthat/test_preproc_mask.R

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,42 @@ test_that("create_preproc_mask locates mask files", {
2626
expect_true(inherits(mask, "LogicalNeuroVol"))
2727
})
2828

29+
test_that("create_preproc_mask ignores JSON sidecars for mask matches", {
30+
skip_if_not_installed("RNifti")
31+
32+
tmp <- tempfile("preproc_mask_sidecar_")
33+
dir.create(tmp, recursive = TRUE)
34+
on.exit(unlink(tmp, recursive = TRUE, force = TRUE), add = TRUE)
35+
36+
readr::write_tsv(
37+
tibble::tibble(participant_id = "sub-1001"),
38+
file.path(tmp, "participants.tsv")
39+
)
40+
jsonlite::write_json(
41+
list(Name = "MaskSidecar", BIDSVersion = "1.8.0"),
42+
file.path(tmp, "dataset_description.json"),
43+
auto_unbox = TRUE
44+
)
45+
dir.create(file.path(tmp, "sub-1001", "func"), recursive = TRUE)
46+
file.create(file.path(tmp, "sub-1001", "func",
47+
"sub-1001_task-audio_run-01_bold.nii.gz"))
48+
49+
deriv_root <- file.path(tmp, "derivatives", "fmriprep", "sub-1001", "func")
50+
dir.create(deriv_root, recursive = TRUE)
51+
jsonlite::write_json(
52+
list(Name = "fmriprep", BIDSVersion = "1.8.0", DatasetType = "derivative"),
53+
file.path(tmp, "derivatives", "fmriprep", "dataset_description.json"),
54+
auto_unbox = TRUE
55+
)
56+
57+
mask_path <- file.path(
58+
deriv_root,
59+
"sub-1001_task-audio_run-01_space-MNI152NLin2009cAsym_res-2_desc-brain_mask.nii.gz"
60+
)
61+
RNifti::writeNifti(array(1, dim = c(2, 2, 2)), mask_path)
62+
writeLines("{}", sub("\\.nii\\.gz$", ".json", mask_path))
63+
64+
proj <- bids_project(tmp, fmriprep = TRUE)
65+
mask <- create_preproc_mask(proj, subid = "1001")
66+
expect_true(inherits(mask, "LogicalNeuroVol"))
67+
})

0 commit comments

Comments
 (0)