@@ -1428,51 +1428,43 @@ setup_apply_aroma <- function(ppcfg = list(), fields = NULL) {
14281428
14291429
14301430
1431- # ' Determine expected output files for a postprocessing stream
1431+ # ' List postprocessed output files for a stream based on its input spec
14321432# '
1433- # ' Given a directory of candidate input files and a specification for
1434- # ' matching those inputs, this function returns the full paths to the
1435- # ' corresponding postprocessed NIfTI files. The output filenames are
1436- # ' derived by replacing the `desc` entity in each input file with the
1437- # ' stream's `bids_desc` value.
1433+ # ' Converts a postprocess input specification into a pattern that targets
1434+ # ' postprocessed outputs by ensuring the `desc` entity matches `bids_desc`.
14381435# '
1439- # ' @param input_dir Directory containing the input NIfTI files to be
1440- # ' postprocessed.
1441- # ' @param input_regex Specification used to match the input files. This may be
1442- # ' a space-separated set of BIDS entities (e.g., "desc:preproc suffix:bold")
1443- # ' or a regular expression prefixed with "regex:".
1444- # ' @param bids_desc The `desc` value to use for the output filenames.
1436+ # ' @param input_dir Directory containing postprocessed outputs.
1437+ # ' @param input_regex Specification used to match the input files for the stream.
1438+ # ' May be a space-separated set of BIDS entities or a regex prefixed with "regex:".
1439+ # ' @param bids_desc The `desc` value used for postprocessed outputs.
14451440# '
1446- # ' @return A character vector of full paths to the expected postprocessed
1447- # ' NIfTI files. The vector is named with the corresponding input file.
1441+ # ' @return A character vector of full paths to matching postprocessed outputs.
14481442# ' @export
1449- # ' @examples
1450- # ' \dontrun{
1451- # ' get_postproc_stream_outputs(
1452- # ' input_dir = "/path/to/subject",
1453- # ' input_regex = "desc:preproc suffix:bold",
1454- # ' bids_desc = "clean"
1455- # ' )
1456- # ' }
1457- # ' @importFrom checkmate assert_directory_exists assert_string
1458- get_postproc_stream_outputs <- function (input_dir , input_regex , bids_desc ) {
1443+ get_postproc_output_files <- function (input_dir , input_regex , bids_desc ) {
14591444 checkmate :: assert_directory_exists(input_dir )
14601445 if (is.null(input_regex )) input_regex <- " desc:preproc suffix:bold"
1461- checkmate :: assert_string (input_regex )
1446+ checkmate :: assert_character (input_regex , min.len = 1L )
14621447 checkmate :: assert_string(bids_desc )
14631448
1464- pattern <- construct_bids_regex(input_regex )
1465- in_files <- list.files(path = input_dir , pattern = pattern , recursive = TRUE , full.names = TRUE )
1449+ resolve_spec <- function (spec ) {
1450+ spec <- trimws(spec )
1451+ if (grepl(" ^regex:" , spec )) {
1452+ return (trimws(sub(" ^regex\\ s*:" , " " , spec )))
1453+ }
14661454
1467- if (length(in_files ) == 0L ) return (character (0 ))
1455+ if (grepl(" \\ bdesc:" , spec )) {
1456+ spec <- sub(" \\ bdesc:[^[:space:]]+" , paste0(" desc:" , bids_desc ), spec )
1457+ } else {
1458+ spec <- paste(spec , paste0(" desc:" , bids_desc ))
1459+ }
1460+
1461+ construct_bids_regex(spec )
1462+ }
14681463
1469- out_files <- vapply(in_files , function (f ) {
1470- info <- as.list(extract_bids_info(f ))
1471- info $ description <- bids_desc
1472- construct_bids_filename(info , full.names = TRUE )
1473- }, character (1 ))
1464+ patterns <- vapply(input_regex , resolve_spec , character (1 ))
1465+ files <- unlist(lapply(patterns , function (pat ) {
1466+ list.files(path = input_dir , pattern = pat , recursive = TRUE , full.names = TRUE )
1467+ }), use.names = FALSE )
14741468
1475- # leave as unnamed for now (don't need to remember input file)
1476- # names(out_files) <- in_files
1477- out_files
1469+ unique(files )
14781470}
0 commit comments