Skip to content

Commit 72603c8

Browse files
committed
Merge pull request #418 from MRtrix3/script_doc
Enable auto-documentation of scripts
2 parents 2ebae4d + 08d7110 commit 72603c8

File tree

9 files changed

+129
-15
lines changed

9 files changed

+129
-15
lines changed

scripts/act_anat_prepare_freesurfer

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ from lib.errorMessage import errorMessage
2525
from lib.printMessage import printMessage
2626
from lib.runCommand import runCommand
2727

28+
lib.app.author = 'Robert E. Smith (robert.smith@florey.edu.au)'
2829
lib.app.initCitations([ 'ACT' ])
2930
lib.app.initParser('Generate a 5TT image suitable for ACT from a FreeSurfer segmentation')
3031
lib.app.parser.add_argument('input', help='The input image series; this should be one of the \'aseg\' images provided by FreeSurfer')

scripts/act_anat_prepare_fsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ from lib.printMessage import printMessage
1616
from lib.runCommand import runCommand
1717
from lib.warnMessage import warnMessage
1818

19+
lib.app.author = 'Robert E. Smith (robert.smith@florey.edu.au)'
1920
lib.app.initCitations([ 'ACT', 'bet', 'fast', 'first', 'FSL' ])
2021
lib.app.initParser('Generate a 5TT image suitable for ACT from a T1 image using FSL tools')
2122
lib.app.parser.add_argument('input', help='The input T1 image')

scripts/dwibiascorrect

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ from lib.getFSLSuffix import getFSLSuffix
1313
from lib.getHeaderInfo import getHeaderInfo
1414
from lib.runCommand import runCommand
1515

16+
lib.app.author = 'Robert E. Smith (robert.smith@florey.edu.au)'
1617
lib.app.initCitations([ 'fast', 'FSL' ])
1718
lib.app.initParser('Perform B1 field inhomogeneity correction for a DWI volume series')
1819
lib.app.parser.add_argument('input', help='The input image series to be corrected')

scripts/fs_parc_replace_sgm_first

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ from lib.errorMessage import errorMessage
2323
from lib.printMessage import printMessage
2424
from lib.runCommand import runCommand
2525

26+
lib.app.author = 'Robert E. Smith (robert.smith@florey.edu.au)'
2627
lib.app.initCitations([ 'first', 'FSL', 'SIFT_followup' ])
2728
lib.app.initParser('In a FreeSurfer parcellation image, replace the sub-cortical grey matter structure delineations using FSL FIRST')
2829
lib.app.parser.add_argument('parc', help='The input FreeSurfer parcellation image')

scripts/lib/app.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
# * -verbose prints all commands and all command outputs
55

66
args = ''
7+
author = 'No author specified'
78
citationWarning = ''
89
cleanup = True
9-
epilog = ''
1010
lastFile = ''
1111
mrtrixQuiet = '-quiet'
1212
mrtrixNThreads = ''
1313
numArgs = 0
1414
parser = ''
15+
refList = ''
16+
standardOptions = ''
1517
tempDir = ''
1618
verbosity = 1
1719
workingDir = ''
@@ -37,16 +39,16 @@ def error(self, message):
3739
def initCitations(cmdlist):
3840

3941
import lib.citations
40-
global epilog, citationWarning
42+
global refList, citationWarning
4143
max_level = 0
4244
for name in cmdlist:
4345
entry = [item for item in lib.citations.list if item[0] == name][0]
4446
if entry:
4547
max_level = max(max_level, entry[1]);
4648
# Construct string containing all relevant citations that will be fed to the argument parser epilog
47-
if not epilog:
48-
epilog = 'Relevant citations for tools / algorithms used in this script:\n\n'
49-
epilog += entry[0] + ':\n' + entry[2] + '\n\n'
49+
if refList:
50+
refList += '\n'
51+
refList += entry[0] + ': ' + entry[2] + '\n'
5052

5153
if max_level:
5254
citationWarning += 'Note that this script makes use of commands / algorithms that have relevant articles for citation'
@@ -59,14 +61,18 @@ def initCitations(cmdlist):
5961

6062
def initParser(desc):
6163
import argparse
62-
global epilog, parser
64+
global parser, refList, standardOptions
65+
epilog = ''
66+
if refList:
67+
epilog = 'Relevant citations for tools / algorithms used in this script:\n\n' + refList + '\n'
68+
epilog += 'Author:\n' + author + '\n\nCopyright (C) 2008 Brain Research Institute, Melbourne, Australia. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n'
6369
parser = Parser(description=desc, epilog=epilog, formatter_class=argparse.RawDescriptionHelpFormatter)
64-
standard_options = parser.add_argument_group('standard options')
65-
standard_options.add_argument('-continue', nargs=2, dest='cont', metavar=('<TempDir>', '<LastFile>'), help='Continue the script from a previous execution; must provide the temporary directory path, and the name of the last successfully-generated file')
66-
standard_options.add_argument('-nocleanup', action='store_true', help='Do not delete temporary directory at script completion')
67-
standard_options.add_argument('-nthreads', metavar='number', help='Use this number of threads in MRtrix multi-threaded applications (0 disables multi-threading)')
68-
standard_options.add_argument('-tempdir', metavar='/path/to/tmp/', help='Manually specify the path in which to generate the temporary directory')
69-
verbosity_group = standard_options.add_mutually_exclusive_group()
70+
standardOptions = parser.add_argument_group('standard options')
71+
standardOptions.add_argument('-continue', nargs=2, dest='cont', metavar=('<TempDir>', '<LastFile>'), help='Continue the script from a previous execution; must provide the temporary directory path, and the name of the last successfully-generated file')
72+
standardOptions.add_argument('-nocleanup', action='store_true', help='Do not delete temporary directory at script completion')
73+
standardOptions.add_argument('-nthreads', metavar='number', help='Use this number of threads in MRtrix multi-threaded applications (0 disables multi-threading)')
74+
standardOptions.add_argument('-tempdir', metavar='/path/to/tmp/', help='Manually specify the path in which to generate the temporary directory')
75+
verbosity_group = standardOptions.add_mutually_exclusive_group()
7076
verbosity_group.add_argument('-quiet', action='store_true', help='Suppress all console output during script execution')
7177
verbosity_group.add_argument('-verbose', action='store_true', help='Display additional information for every command invoked')
7278

@@ -75,9 +81,15 @@ def initParser(desc):
7581
def initialise():
7682
import argparse, os, random, string, sys
7783
from lib.printMessage import printMessage
84+
from lib.printUsageMarkdown import printUsageMarkdown
7885
from lib.readMRtrixConfSetting import readMRtrixConfSetting
79-
global args, cleanup, lastFile, mrtrixNThreads, mrtrixQuiet, tempDir, verbosity, workingDir
86+
global args, author, cleanup, lastFile, mrtrixNThreads, mrtrixQuiet, standardOptions, parser, refList, tempDir, verbosity, workingDir
8087
global colourClear, colourConsole, colourError, colourPrint, colourWarn
88+
89+
if len(sys.argv) == 2 and sys.argv[1] == '__print_usage_markdown__':
90+
printUsageMarkdown(parser, standardOptions, refList, author)
91+
exit(0)
92+
8193
workingDir = os.getcwd()
8294
args = parser.parse_args()
8395

scripts/lib/printUsageMarkdown.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
def printUsageMarkdown (args, stdopts, refs, author):
2+
import argparse, os, sys
3+
4+
argument_list = [ ]
5+
for arg in args._positionals._group_actions:
6+
argument_list.append(arg.dest)
7+
8+
standard_option_list = [ ]
9+
for opt in stdopts._group_actions:
10+
standard_option_list.append(opt.option_strings[0])
11+
12+
help_option = ''
13+
option_list = [ ]
14+
for opt in args._actions:
15+
if opt.option_strings and not opt.option_strings[0] in argument_list and not opt.option_strings[0] in standard_option_list:
16+
if opt.option_strings[0] == '-h':
17+
help_option = opt
18+
else:
19+
option_list.append(opt.option_strings[0])
20+
21+
print ('## Synopsis')
22+
print ('')
23+
print (' ' + args.prog + ' [ options ] ' + ' '.join(argument_list))
24+
print ('')
25+
26+
for arg in args._positionals._group_actions:
27+
_printArgument(arg)
28+
29+
print ('')
30+
print ('## Description')
31+
print ('')
32+
print (vars(args)['description'])
33+
print ('')
34+
35+
if option_list:
36+
print ('## Options')
37+
print ('')
38+
39+
for opt in args._actions:
40+
if opt.option_strings and opt.option_strings[0] in option_list:
41+
_printOption(opt)
42+
print ('')
43+
44+
print ('#### Standard options')
45+
print ('')
46+
47+
_printOption(help_option)
48+
print ('')
49+
50+
for opt in stdopts._group_actions:
51+
_printOption(opt)
52+
print ('')
53+
54+
print ('#### References')
55+
print ('')
56+
print (refs)
57+
print ('')
58+
print ('---')
59+
print ('')
60+
print ('**Author:** ' + author)
61+
print ('')
62+
print ('**Copyright:** Copyright (C) 2008 Brain Research Institute, Melbourne, Australia. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.')
63+
64+
65+
66+
67+
def _printArgument(arg):
68+
print ('- *' + arg.dest + '*: ' + arg.help)
69+
70+
71+
72+
def _printOption(opt):
73+
usage = ''
74+
if len(opt.option_strings) == 1:
75+
usage = [ opt.option_strings[0] ]
76+
else:
77+
usage = [ '[ ' + ' '.join(opt.option_strings) + ' ]' ]
78+
if type(opt.metavar) is tuple:
79+
for arg in opt.metavar:
80+
usage.append(arg)
81+
elif type(opt.metavar) is str:
82+
usage.append(opt.metavar)
83+
print ('+ **' + ' '.join(usage) + '**<br>' + opt.help)
84+

scripts/revpe_distcorr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ from lib.printMessage import printMessage
2626
from lib.runCommand import runCommand
2727
from lib.warnMessage import warnMessage
2828

29+
lib.app.author = 'Robert E. Smith (robert.smith@florey.edu.au)'
2930
lib.app.initCitations([ 'eddy/topup', 'FSL' ])
3031
lib.app.initParser('Perform EPI distortion correction of a volume series using a reversed phase-encode image pair to estimate the inhomogeneity field')
3132
lib.app.parser.add_argument('pe_axis', help='The phase encode direction / axis; can be an axis number (0, 1 or 2) or a code (e.g. AP, LR, IS)')

scripts/revpe_dwicombine

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ from lib.printMessage import printMessage
3232
from lib.runCommand import runCommand
3333
from lib.warnMessage import warnMessage
3434

35+
lib.app.author = 'Robert E. Smith (robert.smith@florey.edu.au)'
3536
lib.app.initCitations([ 'eddy/topup', 'FSL', 'Skare2010' ])
3637
lib.app.initParser('Perform EPI distortion & recombination of a pair of image volumes, where the diffusion gradient table is identical between the two series, but the phase-encode direction is reversed')
3738
lib.app.parser.add_argument('pe_axis', help='The phase encode direction / axis; can be an axis number (0, 1 or 2) or a code (e.g. AP, LR, IS)')

update_doc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ folder=$(mktemp -d)
3434

3535
rm -rf mrtrix3.wiki/commands/list
3636
mkdir -p mrtrix3.wiki/commands/list
37+
rm -rf mrtrix3.wiki/scripts/list
38+
mkdir -p mrtrix3.wiki/scripts/list
3739

3840
echo '[[Home]]
3941
@@ -47,13 +49,23 @@ folder=$(mktemp -d)
4749
echo '[['$(basename $n)']]<br>' >> mrtrix3.wiki/commands/_Sidebar.md
4850
done
4951

52+
echo '[[Home]]
53+
54+
**Scripts**<br>
55+
' >> mrtrix3.wiki/scripts/_Sidebar.md
56+
57+
for n in `find mrtrix3/scripts/ -type f -print0 | xargs -0 grep -l "lib.app.initParser"`; do
58+
$n __print_usage_markdown__ > mrtrix3.wiki/scripts/list/$(basename $n).md
59+
echo '[['$(basename $n)']]<br>' >> mrtrix3.wiki/scripts/_Sidebar.md
60+
done
61+
5062
grep -rn --include=\*.h --include=\*.cpp '^\s*//CONF\b ' mrtrix3 | sed -ne 's/^.*CONF \(.*\)/\1/p' | ./format_config_options > mrtrix3.wiki/DesignPrinciples/List-of-configuration-file-options.md
5163

5264

5365
(
5466
cd mrtrix3.wiki
55-
git add commands DesignPrinciples/List-of-configuration-file-options.md
56-
git commit -m "automatic update of command documentation"
67+
git add commands scripts DesignPrinciples/List-of-configuration-file-options.md
68+
git commit -m "automatic update of command and script documentation"
5769
git push
5870
)
5971

0 commit comments

Comments
 (0)