88
99import argparse
1010import os
11+ import textwrap
1112
1213from niftynet .engine .application_factory import ApplicationFactory
1314from niftynet .engine .application_factory import SUPPORTED_APP
@@ -42,25 +43,33 @@ def run():
4243 of the configuration file. based on the application_name
4344 or meta_parser.prog name, the section parsers are organised
4445 to find system parameters and application specific
45- parameters
46+ parameters.
4647
4748 :return: system parameters is a group of parameters including
4849 SYSTEM_SECTIONS and app_module.REQUIRED_CONFIG_SECTION
4950 input_data_args is a group of input data sources to be
5051 used by niftynet.io.ImageReader
5152 """
5253 meta_parser = argparse .ArgumentParser (
53- epilog = epilog_string )
54+ description = "Launch a NiftyNet application." ,
55+ formatter_class = argparse .RawDescriptionHelpFormatter ,
56+ epilog = textwrap .dedent (epilog_string ))
5457 version_string = get_niftynet_version_string ()
58+ meta_parser .add_argument ("action" ,
59+ help = "train networks or run inferences" ,
60+ metavar = 'ACTION' ,
61+ choices = ['train' , 'inference' ])
5562 meta_parser .add_argument ("-v" , "--version" ,
5663 action = 'version' ,
5764 version = version_string )
5865 meta_parser .add_argument ("-c" , "--conf" ,
59- help = "Specify configurations from a file" ,
60- metavar = "File" , )
66+ help = "specify configurations from a file" ,
67+ metavar = "CONFIG_FILE" )
6168 meta_parser .add_argument ("-a" , "--application_name" ,
62- help = "Specify application name" ,
63- default = "" , )
69+ help = "specify an application module name" ,
70+ metavar = 'APPLICATION_NAME' ,
71+ default = "" )
72+
6473 meta_args , args_from_cmdline = meta_parser .parse_known_args ()
6574 print (version_string )
6675
@@ -70,13 +79,11 @@ def run():
7079 "forget '-c' command argument?{}" .format (epilog_string ))
7180 raise IOError
7281
73- # Read global config file
74- global_config = NiftyNetGlobalConfig ()
75-
82+ # Resolve relative configuration file location
7683 config_path = meta_args .conf
7784 if not os .path .isfile (config_path ):
7885 relative_conf_file = os .path .join (
79- global_config .get_default_examples_folder (),
86+ NiftyNetGlobalConfig () .get_default_examples_folder (),
8087 config_path ,
8188 config_path + "_config.ini" )
8289 if os .path .isfile (relative_conf_file ):
@@ -134,9 +141,11 @@ def run():
134141 '\n Unknown parameter: {}{}' .format (args_from_cmdline , epilog_string )
135142
136143 # split parsed results in all_args
137- # into dictionary of system_args and input_data_args
144+ # into dictionaries of system_args and input_data_args
138145 system_args = {}
139146 input_data_args = {}
147+
148+ # copy system default sections to ``system_args``
140149 for section in all_args :
141150 if section in SYSTEM_SECTIONS :
142151 system_args [section ] = all_args [section ]
@@ -148,6 +157,7 @@ def run():
148157 all_args ['SYSTEM' ].model_dir = os .path .join (
149158 os .path .dirname (meta_args .conf ), 'model' )
150159
160+ # copy non-default sections to ``input_data_args``
151161 for section in all_args :
152162 if section in SYSTEM_SECTIONS :
153163 continue
@@ -165,8 +175,9 @@ def run():
165175 else :
166176 input_data_args [section ].csv_file = ''
167177
168- # update conf path
178+ # preserve ``config_file`` and ``action parameter`` from the meta_args
169179 system_args ['CONFIG_FILE' ] = argparse .Namespace (path = meta_args .conf )
180+ system_args ['SYSTEM' ].action = meta_args .action
170181 return system_args , input_data_args
171182
172183
@@ -184,13 +195,14 @@ def _parse_arguments_by_section(parents,
184195
185196 Commandline inputs only override system/custom parameters.
186197 input data related parameters needs to be defined in config file.
198+
187199 :param parents: a list, parsers will be created as
188- subparsers of parents
200+ subparsers of parents
189201 :param section: section name to be parsed
190202 :param args_from_config_file: loaded parameters from config file
191203 :param args_from_cmd: dictionary commandline parameters
192204 :return: parsed parameters of the section and unknown
193- commandline params.
205+ commandline params.
194206 """
195207 section_parser = argparse .ArgumentParser (
196208 parents = parents ,
0 commit comments