Skip to content

Commit

Permalink
✨ 3dmaskave_grp: regexp mask
Browse files Browse the repository at this point in the history
  • Loading branch information
WillForan committed Nov 13, 2024
1 parent 449c627 commit f9469f9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
34 changes: 32 additions & 2 deletions 3dmaskave_grp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ $(basename $0) -csv dlpfc.csv -mask "mask.nii.gz" -- paths/to/1*_2*/inputs.nii.g
3dmaskave_grp -csv MPFC-ACC_1.csv -m "ACC=mask.nii.gz" -m "PFC=mask_many.nii.gz<3>" -- paths/to/1*_2*/inputs.nii.gz
-m|-mask NAME=MASK input mask optionally with afni selector
MASK can also be a perl regular expression like 's:search:replace:'
-m tian_nacc.core='s:[^/]*:warps/tian_nacc_rs.nii.gz<1>:'
-csv FILE save csv as FILE (defaults to NAME.csv)
-datatable FILE use datatable as input. req Subj is first and InputFile last col
-label_col IDX req. for datatable. what col index to use for labels
Expand Down Expand Up @@ -105,11 +108,31 @@ parse_args(){
return 0
}

# check if we input something like s:patt:replace:
name_is_regex(){
# start like s/ or s:
[[ "$*" =~ ^s([/:]) ]] &&
# ends with the same search delim but optionally 'g' or 'i'
[[ "$*" =~ ${BASH_REMATCH[1]}[gi]*$ ]]
}

# analgous to 'test -r $mask' but works for '$file<roinum>' AFNI syntax
# abstracted from check_mask so we can use if mask is a regexp
check_roi() {
mask="${1:?input mask<roi> to confirm count is nonzero}"
maskval=$(3dmaskave -q -mask "$mask" "$mask" 2>/dev/null|| echo 0)
[[ $maskval == 0 ]] && warn "ERROR:BAD MASK: '$mask'" && return 1
return 0
}

# wrap check_roi with 3dcalc and regexp checks
check_mask() {
mask="$1"
[[ $mask =~ ^3dcalc ]] && warn "WARNING: inline 3dcalc will be run for each input file!" && return 0
maskval=$(3dmaskave -q -mask "$mask" "$mask" 2>/dev/null|| echo 0)
[[ $maskval == 0 ]] && warn "ERROR:BAD MASK: '$mask'" && return 1
name_is_regex "$mask" &&
warn "WARNING: using regexp mask '$mask'. cannot preemptivly check if mask exists" &&
return 0
check_roi "$mask"
return 0
}

Expand Down Expand Up @@ -188,6 +211,13 @@ maskave_csv() {
id="$1"; shift

already_exists "$mask_name,$id,$input_name," && return 0
if name_is_regex "$mask_file"; then
local regexp="$mask_file"
mask_file=$(perl -plne "$regexp" <<< "$cfile")
! check_roi "$mask_file" &&
echo "ERROR: mask regexp applied but '$mask_file' doesnt exist or roi is empty. see: perl -pne '$regexp' <<< '$cfile'" >&2 &&
return 1
fi

val="$(maskave_NA "$mask_file" "$cfile")"
echo "${mask_name},${id},${input_name},${val}"
Expand Down
34 changes: 33 additions & 1 deletion t/3dmaskave_grp.bats
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,39 @@ function test_maskave { # @test
run 3dmaskave_grp \
-pattern data -csv $BATS_TEST_TMPDIR/test.csv \
-m roi1=$BATS_TEST_TMPDIR/roi1.nii.gz'<1>'\
-m roi2=$BATS_TEST_TMPDIR/roi1.nii.gz -- /tmp/data.nii.gz
-m roi2=$BATS_TEST_TMPDIR/roi1.nii.gz -- $BATS_TEST_TMPDIR/data.nii.gz
[[ $status -eq 0 ]]
run tail -n1 $BATS_TEST_TMPDIR/test.csv
[[ "$output" == "roi2,data,data,1" ]]
}

function test_isregexp { # @test
run name_is_regex "s:a:b:"
[ "$status" -eq 0 ]
run name_is_regex "s/a/b/"
[ "$status" -eq 0 ]
run name_is_regex "s/a/b/i"
[ "$status" -eq 0 ]
run name_is_regex "s/path/to/i.nii.gz<1>"
[ "$status" -eq 1 ]
}

function test_isroi { # @test
run check_roi $BATS_TEST_TMPDIR/roi1.nii.gz
[ "$status" -eq 0 ]

run check_roi $BATS_TEST_TMPDIR/roi1.nii.gz'<1>'
[ "$status" -eq 0 ]

run check_roi $BATS_TEST_TMPDIR/roi1.nii.gz'<2>'
[ "$status" -ne 0 ]
}

function test_maskrelregexp { # @test
run 3dmaskave_grp \
-pattern data -csv $BATS_TEST_TMPDIR/test.csv \
-m roi1='s:data.nii.gz:roi1.nii.gz<1>:'\
-m roi2=$BATS_TEST_TMPDIR/roi1.nii.gz -- $BATS_TEST_TMPDIR/data.nii.gz
[[ $status -eq 0 ]]
run tail -n1 $BATS_TEST_TMPDIR/test.csv
[[ "$output" == "roi2,data,data,1" ]]
Expand Down

0 comments on commit f9469f9

Please sign in to comment.