Skip to content

Commit

Permalink
Merge branch 'main' into fix/atlasbundleseg
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVCaron authored Dec 19, 2024
2 parents 760c0cd + fb6f35a commit c36b6f9
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 70 deletions.
18 changes: 12 additions & 6 deletions modules/nf-neuro/betcrop/antsbet/main.nf
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,9 @@ process BETCROP_ANTSBET {
"""

stub:
def args = task.ext.args ?: ''
def prefix = task.ext.prefix ?: "${meta.id}"

"""
antsBrainExtraction.sh -h
scil_volume_math.py -h
mrcalc -h
"""
touch ${prefix}__t1_bet.nii.gz
touch ${prefix}__t1_bet_mask.nii.gz
Expand All @@ -62,5 +57,16 @@ process BETCROP_ANTSBET {
mrtrix: \$(mrcalc -version 2>&1 | sed -n 's/== mrcalc \\([0-9.]\\+\\).*/\\1/p')
ants: \$(antsRegistration --version | grep "Version" | sed -E 's/.*v([0-9]+\\+\\).*/\\1/')
END_VERSIONS
function handle_code () {
local code=\$?
ignore=( 1 )
exit \$([[ " \${ignore[@]} " =~ " \$code " ]] && echo 0 || echo \$code)
}
trap 'handle_code' ERR
antsBrainExtraction.sh
scil_volume_math.py -h
mrcalc -h
"""
}
72 changes: 66 additions & 6 deletions modules/nf-neuro/betcrop/antsbet/tests/main.nf.test
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ nextflow_process {
tag "subworkflows/load_test_data"

test("betcrop - antsbet") {

setup {
run("LOAD_TEST_DATA", alias: "LOAD_DATA") {
script "../../../../../subworkflows/nf-neuro/load_test_data/main.nf"
Expand All @@ -26,7 +25,6 @@ nextflow_process {
}
}
}

when {
process {
"""
Expand Down Expand Up @@ -68,14 +66,76 @@ nextflow_process {
"""
}
}

then {
assertAll(
{ assert process.success },
{ assert snapshot(process.out).match() }
{ assert snapshot(
niftiMD5SUM(process.out.t1.get(0).get(1)),
niftiMD5SUM(process.out.mask.get(0).get(1)),
process.out.versions
).match() }
)
}
}
test("betcrop - antsbet - stub-run") {
options "-stub-run"
setup {
run("LOAD_TEST_DATA", alias: "LOAD_DATA") {
script "../../../../../subworkflows/nf-neuro/load_test_data/main.nf"
process {
"""
input[0] = Channel.from( [ "T1w.zip", "transform.zip", "antsbet.zip" ] )
input[1] = "test.load-test-data"
"""
}
}
}
when {
process {
"""
ch_split_test_data = LOAD_DATA.out.test_data_directory
.branch{
t1: it.simpleName == "T1w"
transform: it.simpleName == "transform"
template: it.simpleName == "antsbet"
}
ch_t1 = ch_split_test_data.t1.map{
test_data_directory -> [
[ id:'test' ],
file("\${test_data_directory}/T1w.nii.gz")
]
}
ch_template = ch_split_test_data.template.map{
test_data_directory -> [
[ id: 'test' ],
file("\${test_data_directory}/t1_template.nii.gz"),
file("\${test_data_directory}/t1_brain_probability_map.nii.gz")
]
}
ch_mask = ch_split_test_data.transform.map{
test_data_directory -> [
[ id:'test' ],
file("\${test_data_directory}/t1_to_bet_template_mask.nii.gz")
]
}
ch_transform = ch_split_test_data.transform.map{
test_data_directory -> [
[ id:'test' ],
file("\${test_data_directory}/t1_to_bet_template.mat")
]
}
input[0] = ch_t1
.join(ch_template)
.join(ch_mask)
.join(ch_transform)
"""
}
}
then {
assertAll(
{ assert process.success },
{ assert snapshot(process.out.versions).match() }
)
}

}

}
61 changes: 19 additions & 42 deletions modules/nf-neuro/betcrop/antsbet/tests/main.nf.test.snap
Original file line number Diff line number Diff line change
@@ -1,51 +1,28 @@
{
"betcrop - antsbet": {
"content": [
{
"0": [
[
{
"id": "test"
},
"test__t1_bet.nii.gz:md5,bd666c4ce559eab93690131f8b2a9101"
]
],
"1": [
[
{
"id": "test"
},
"test__t1_bet_mask.nii.gz:md5,48c91d01a6c59d48e4ce84431d5e4971"
]
],
"2": [
"versions.yml:md5,9fde1445bc10857db69e440781e58cb7"
],
"mask": [
[
{
"id": "test"
},
"test__t1_bet_mask.nii.gz:md5,48c91d01a6c59d48e4ce84431d5e4971"
]
],
"t1": [
[
{
"id": "test"
},
"test__t1_bet.nii.gz:md5,bd666c4ce559eab93690131f8b2a9101"
]
],
"versions": [
"versions.yml:md5,9fde1445bc10857db69e440781e58cb7"
]
}
"test__t1_bet.nii.gz:md5:header,72167012fa8893d3c3531d5d1245b84a,data,e31b24e1dadf5b067211544fca1f3f01",
"test__t1_bet_mask.nii.gz:md5:header,f7389fe98c9a7e3a87c90b7ca05ea14a,data,1572808125554e50ff73fbe0e28037a9",
[
"versions.yml:md5,9fde1445bc10857db69e440781e58cb7"
]
],
"meta": {
"nf-test": "0.9.0",
"nextflow": "24.04.4"
"nextflow": "24.10.1"
},
"timestamp": "2024-10-30T17:42:18.337235926"
"timestamp": "2024-12-17T20:31:09.4739"
},
"betcrop - antsbet - stub-run": {
"content": [
[
"versions.yml:md5,9fde1445bc10857db69e440781e58cb7"
]
],
"meta": {
"nf-test": "0.9.0",
"nextflow": "24.10.1"
},
"timestamp": "2024-12-12T10:20:29.72452"
}
}
11 changes: 9 additions & 2 deletions modules/nf-neuro/betcrop/synthbet/main.nf
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,21 @@ process BETCROP_SYNTHBET {
def prefix = task.ext.prefix ?: "${meta.id}"

"""
mri_synthstrip -h
touch ${prefix}__bet_image.nii.gz
touch ${prefix}__brain_mask.nii.gz
cat <<-END_VERSIONS > versions.yml
"${task.process}":
Freesurfer: \$(mri_convert -version | grep "freesurfer" | sed -E 's/.* ([0-9]+\\.[0-9]+\\.[0-9]+).*/\\1/')
END_VERSIONS
function handle_code () {
local code=\$?
ignore=( 1 )
exit \$([[ " \${ignore[@]} " =~ " \$code " ]] && echo 0 || echo \$code)
}
trap 'handle_code' ERR
mri_synthstrip -h
"""
}
37 changes: 34 additions & 3 deletions modules/nf-neuro/betcrop/synthbet/tests/main.nf.test
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ nextflow_process {
tag "subworkflows/load_test_data"

test("betcrop - synthbet") {

setup {
run("LOAD_TEST_DATA", alias: "LOAD_DATA") {
script "../../../../../subworkflows/nf-neuro/load_test_data/main.nf"
Expand All @@ -26,7 +25,6 @@ nextflow_process {
}
}
}

when {
process {
"""
Expand All @@ -40,12 +38,45 @@ nextflow_process {
"""
}
}

then {
assertAll(
{ assert process.success },
{ assert snapshot(process.out).match() }
)
}
}

test("betcrop - synthbet - stub-run") {
options "-stub-run"
setup {
run("LOAD_TEST_DATA", alias: "LOAD_DATA") {
script "../../../../../subworkflows/nf-neuro/load_test_data/main.nf"
process {
"""
input[0] = Channel.from( [ "freesurfer.zip" ] )
input[1] = "test.load-test-data"
"""
}
}
}
when {
process {
"""
input[0] = LOAD_DATA.out.test_data_directory.map{
test_data_directory -> [
[ id:'test', single_end:false ], // meta map
file("\${test_data_directory}/anat_image.nii.gz"),
[]
]
}
"""
}
}
then {
assertAll(
{ assert process.success },
{ assert snapshot(process.out.versions).match() }
)
}
}
}
14 changes: 13 additions & 1 deletion modules/nf-neuro/betcrop/synthbet/tests/main.nf.test.snap
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
{
"betcrop - synthbet - stub-run": {
"content": [
[
"versions.yml:md5,c639461870ca534b5105f61d672f740f"
]
],
"meta": {
"nf-test": "0.9.0",
"nextflow": "24.10.1"
},
"timestamp": "2024-12-12T10:54:37.644838"
},
"betcrop - synthbet": {
"content": [
{
Expand Down Expand Up @@ -52,4 +64,4 @@
},
"timestamp": "2024-11-25T18:19:01.459548926"
}
}
}
38 changes: 28 additions & 10 deletions subworkflows/nf-neuro/load_test_data/main.nf
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def locate_local_cache () {
cache_location.mkdirs()
}
catch (Exception _e) {
error "Failed to create cache location: $cache_location"
error "Failed to create cache location: $cache_location | $_e"
}
}

Expand Down Expand Up @@ -73,7 +73,7 @@ def add_cache_entry ( name, manager ) {
}
catch (Exception _e) {
manager.delete_entry(name)
error "Failed to fetch test data archive: $name | $_e"
error "Failed to download test archive: $name | $_e"
}

return cache_entry
Expand Down Expand Up @@ -102,7 +102,7 @@ def delete_cache_entry ( name, manager ) {
cache_entry.delete()
}
catch (Exception _e) {
error "Failed to delete test data archive: $name"
error "Failed to delete cache entry for test archive: $name | $_e"
}
}
}
Expand Down Expand Up @@ -136,12 +136,13 @@ def setup_cache () {
return cache_manager
}

def unzip_test_archive ( archive, destination ) {
// Unzip the test data archive to the destination directory.
// Exception are not handled here, and are propagated to the caller.

def fetch_archive ( name, destination, manager ) {
// Unzip all archive content to destination
def content = null
try {
content = new java.util.zip.ZipFile("${manager.get_entry(name)}")
content = new java.util.zip.ZipFile("$archive")
content.entries().each{ entry ->
def local_target = file("$destination/${entry.getName()}")
if (entry.isDirectory()) {
Expand All @@ -154,13 +155,30 @@ def fetch_archive ( name, destination, manager ) {
}
}
content.close()

return destination.resolve("${name.take(name.lastIndexOf('.'))}")
}
catch (Exception _e) {
if (content) content.close()
manager.delete_entry(name)
error "Failed to extract test data archive: $name | $_e"
throw _e
}
}

def fetch_archive ( name, destination, manager ) {
// Unzip all archive content to destination
try {
unzip_test_archive(manager.get_entry(name), destination)

return destination.resolve("${name.take(name.lastIndexOf('.'))}")
}
catch (java.util.zip.ZipException _e) {
try {
manager.delete_entry(name)
unzip_test_archive(manager.get_entry(name), destination)

return destination.resolve("${name.take(name.lastIndexOf('.'))}")
}
catch (Exception _ee) {
error "Failed to fetch test archive: $name | $_ee"
}
}
}

Expand Down

0 comments on commit c36b6f9

Please sign in to comment.