Skip to content

Commit

Permalink
[ENH] Add script that wraps the call to Singularity image
Browse files Browse the repository at this point in the history
Merge pull request #61 from Medical-Image-Analysis-Laboratory/58_singularity_wrapper_v2.0.1-dev
  • Loading branch information
sebastientourbier authored Dec 10, 2020
2 parents 1b3fa62 + 3bbae6d commit 7ae8def
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
# This software is distributed under the open-source license Modified BSD.

"""This module defines the mialsuperresolutiontoolkit-bidsapp script that wraps calls to the BIDS APP."""
"""This module defines the `mialsuperresolutiontoolkit_bidsapp_docker` script that wraps calls to the Docker BIDS APP image."""

# General imports
import sys
Expand Down
103 changes: 103 additions & 0 deletions pymialsrtk/cli/mialsuperresolutiontoolkit_bidsapp_singularity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/usr/bin/env python
#
# Copyright © 2016-2020
# Medical Image Analysis Laboratory,
# University Hospital Center and University of Lausanne (UNIL-CHUV), Switzerland,
# and Contributors
#
# This software is distributed under the open-source license Modified BSD.

"""This module defines the `mialsuperresolutiontoolkit_bidsapp_singularity` script that wraps calls to the Singularity BIDS APP image."""

# General imports
import sys

# Own imports
from pymialsrtk.info import __version__
from pymialsrtk.parser import get_parser
from pymialsrtk.interfaces.utils import run


def create_singularity_cmd(args):
"""Function that creates and returns the BIDS App singularity run command.
Parameters
----------
args : dict
Dictionary of parsed input argument in the form::
{
'bids_dir': "/path/to/bids/dataset/directory",
'output_dir': "/path/to/output/directory",
'param_file': "/path/to/configuration/parameter/file",
'analysis_level': "participant",
'participant_label': ['01', '02', '03'],
'openmp_nb_of_cores': 1,
'nipype_nb_of_cores': 1,
'masks_derivatives_dir': 'manual_masks'
}
Returns
-------
cmd : string
String containing the command to be run via `subprocess.run()`
"""
# Docker run command prelude
cmd = 'singularity run --containall '
cmd += f'--bind {args.bids_dir}:/bids_dir '
cmd += f'--bind {args.output_dir}:/output_dir '
cmd += f'--bind {args.param_file}:/bids_dir/code/participants_params.json '
cmd += f'library://tourbier/mialsuperresolutiontoolkit-bidsapp:v{__version__} '

# Standard BIDS App inputs
cmd += '/bids_dir '
cmd += '/output_dir '
cmd += f'{args.analysis_level} '
cmd += '--participant_label '
for label in args.participant_label:
cmd += f'{label} '

# MIALSRTK BIDS App inputs
cmd += '--param_file /bids_dir/code/participants_params.json '
if args.masks_derivatives_dir != '':
cmd += f'--masks_derivatives_dir {args.masks_derivatives_dir} '
cmd += f'--openmp_nb_of_cores {args.openmp_nb_of_cores} '
cmd += f'--nipype_nb_of_cores {args.nipype_nb_of_cores}'

return cmd


def main():
"""Main function that creates and executes the BIDS App singularity command.
Returns
-------
exit_code : {0, 1}
An exit code given to `sys.exit()` that can be:
* '0' in case of successful completion
* '1' in case of an error
"""
# Create and parse arguments
parser = get_parser()
args = parser.parse_args()

# Create the singularity run command
cmd = create_singularity_cmd(args)

# Execute the singularity run command
try:
print(f'... cmd: {cmd}')
run(cmd)
exit_code = 0
except Exception as e:
print('Failed')
print(e)
exit_code = 1

return exit_code


if __name__ == '__main__':
sys.exit(main())
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ def main():
url='https://github.com/Medical-Image-Analysis-Laboratory/mialsuperresolutiontoolkit',
entry_points={
"console_scripts": [
'mialsuperresolutiontoolkit_bidsapp = pymialsrtk.cli.mialsuperresolutiontoolkit_bidsapp:main'
'mialsuperresolutiontoolkit_bidsapp_docker = pymialsrtk.cli.mialsuperresolutiontoolkit_bidsapp_docker:main',
'mialsuperresolutiontoolkit_bidsapp_singularity = pymialsrtk.cli.mialsuperresolutiontoolkit_bidsapp_singularity:main'
]
},
license='BSD-3-Clause',
Expand Down
7 changes: 4 additions & 3 deletions setup_pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ def main():
author_email='sebastien.tourbier@alumni.epfl.ch',
url='https://github.com/Medical-Image-Analysis-Laboratory/mialsuperresolutiontoolkit',
entry_points={
"console_scripts": [
'mialsuperresolutiontoolkit_bidsapp = pymialsrtk.cli.mialsuperresolutiontoolkit_bidsapp:main'
]
"console_scripts": [
'mialsuperresolutiontoolkit_bidsapp_docker = pymialsrtk.cli.mialsuperresolutiontoolkit_bidsapp_docker:main',
'mialsuperresolutiontoolkit_bidsapp_singularity = pymialsrtk.cli.mialsuperresolutiontoolkit_bidsapp_singularity:main'
]
},
license='BSD-3-Clause',
classifiers=[
Expand Down

0 comments on commit 7ae8def

Please sign in to comment.