@@ -247,3 +247,128 @@ test_that("postprocess_confounds recomputes framewise displacement when only nop
247247 replace(conf_df $ csf , is.na(conf_df $ csf ), 0 )
248248 )
249249})
250+
251+ test_that(" postprocess_confounds skips processing when confound data are empty" , {
252+ skip_if_not_installed(" RNifti" )
253+
254+ tmpdir <- tempfile(" postproc_confounds_empty_" )
255+ dir.create(tmpdir )
256+ on.exit(unlink(tmpdir , recursive = TRUE ), add = TRUE )
257+
258+ conf_df <- data.frame (rot_x = numeric (0 ))
259+ conf_path <- file.path(tmpdir , " sub-01_task-rest_desc-confounds_timeseries.tsv" )
260+ data.table :: fwrite(conf_df , conf_path , sep = " \t " )
261+
262+ cfg <- list (
263+ motion_filter = list (enable = FALSE ),
264+ scrubbing = list (enable = FALSE , apply = FALSE , add_to_confounds = FALSE ),
265+ confound_regression = list (enable = TRUE , columns = " rot_x" , noproc_columns = NULL , prefix = " r" ),
266+ confound_calculate = list (enable = FALSE , columns = NULL , noproc_columns = NULL , demean = TRUE ),
267+ apply_aroma = list (enable = FALSE , nonaggressive = TRUE ),
268+ temporal_filter = list (low_pass_hz = NULL , high_pass_hz = NULL , method = " butterworth" ),
269+ bids_desc = " pp" ,
270+ tr = 1 ,
271+ overwrite = TRUE
272+ )
273+
274+ output_bids_info <- list (
275+ subject = " 01" ,
276+ task = " rest" ,
277+ description = " denoise" ,
278+ suffix = " bold" ,
279+ ext = " .nii.gz" ,
280+ directory = tmpdir
281+ )
282+
283+ proc_files <- list (
284+ confounds = conf_path ,
285+ melodic_mix = NULL ,
286+ noise_ics = integer(0 )
287+ )
288+
289+ expect_warning(
290+ regress_path <- postprocess_confounds(
291+ proc_files = proc_files ,
292+ cfg = cfg ,
293+ processing_sequence = character (0 ),
294+ output_bids_info = output_bids_info ,
295+ fsl_img = NULL ,
296+ lg = NULL
297+ ),
298+ " Confound columns were requested but no usable data were found" ,
299+ fixed = TRUE
300+ )
301+
302+ expect_null(regress_path )
303+
304+ reg_file <- construct_bids_filename(
305+ modifyList(output_bids_info , list (suffix = " regressors" , ext = " .tsv" )),
306+ full.names = TRUE
307+ )
308+ expect_false(file.exists(reg_file ))
309+ })
310+
311+ test_that(" postprocess_confounds handles empty confound data with calc/reg enabled" , {
312+ skip_if_not_installed(" RNifti" )
313+
314+ tmpdir <- tempfile(" postproc_confounds_empty_calc_" )
315+ dir.create(tmpdir )
316+ on.exit(unlink(tmpdir , recursive = TRUE ), add = TRUE )
317+
318+ conf_df <- data.frame (rot_x = numeric (0 ), framewise_displacement = numeric (0 ))
319+ conf_path <- file.path(tmpdir , " sub-01_task-rest_desc-confounds_timeseries.tsv" )
320+ data.table :: fwrite(conf_df , conf_path , sep = " \t " )
321+
322+ cfg <- list (
323+ motion_filter = list (enable = FALSE ),
324+ scrubbing = list (enable = FALSE , apply = FALSE , add_to_confounds = FALSE ),
325+ confound_regression = list (enable = TRUE , columns = " rot_x" , noproc_columns = NULL , prefix = " r" ),
326+ confound_calculate = list (enable = TRUE , columns = " framewise_displacement" , noproc_columns = NULL , demean = TRUE ),
327+ apply_aroma = list (enable = FALSE , nonaggressive = TRUE ),
328+ temporal_filter = list (low_pass_hz = NULL , high_pass_hz = NULL , method = " butterworth" ),
329+ bids_desc = " pp" ,
330+ tr = 1 ,
331+ overwrite = TRUE
332+ )
333+
334+ output_bids_info <- list (
335+ subject = " 01" ,
336+ task = " rest" ,
337+ description = " denoise" ,
338+ suffix = " bold" ,
339+ ext = " .nii.gz" ,
340+ directory = tmpdir
341+ )
342+
343+ proc_files <- list (
344+ confounds = conf_path ,
345+ melodic_mix = NULL ,
346+ noise_ics = integer(0 )
347+ )
348+
349+ expect_warning(
350+ regress_path <- postprocess_confounds(
351+ proc_files = proc_files ,
352+ cfg = cfg ,
353+ processing_sequence = character (0 ),
354+ output_bids_info = output_bids_info ,
355+ fsl_img = NULL ,
356+ lg = NULL
357+ ),
358+ " Confound columns were requested but no usable data were found" ,
359+ fixed = TRUE
360+ )
361+
362+ expect_null(regress_path )
363+
364+ reg_file <- construct_bids_filename(
365+ modifyList(output_bids_info , list (suffix = " regressors" , ext = " .tsv" )),
366+ full.names = TRUE
367+ )
368+ confound_file <- construct_bids_filename(
369+ modifyList(output_bids_info , list (suffix = " confounds" , ext = " .tsv" )),
370+ full.names = TRUE
371+ )
372+ expect_false(file.exists(reg_file ))
373+ expect_false(file.exists(confound_file ))
374+ })
0 commit comments