Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Commit 28f4008

Browse files
committed
Merge branch 'read-the-docs' into 'dev'
Read the docs See merge request !158
2 parents 684c6a7 + f5446a5 commit 28f4008

File tree

8 files changed

+61
-31
lines changed

8 files changed

+61
-31
lines changed

niftynet/io/image_reader.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ def initialise(self, data_param, task_param, file_list):
8181
``task_param`` specifies how to combine user input modalities.
8282
e.g., for multimodal segmentation 'image' corresponds to multiple
8383
modality sections, 'label' corresponds to one modality section
84+
85+
This function converts elements of ``file_list`` into
86+
dictionaries of image objects, and save them to ``self.output_list``.
8487
"""
8588
if not self.names:
8689
tf.logging.fatal('Please specify data input keywords, this should '
@@ -101,6 +104,7 @@ def initialise(self, data_param, task_param, file_list):
101104
for name in self.names)
102105
required_sections = \
103106
sum([list(vars(task_param).get(name)) for name in self.names], [])
107+
104108
for required in required_sections:
105109
try:
106110
if (file_list is None) or \
@@ -115,7 +119,14 @@ def initialise(self, data_param, task_param, file_list):
115119
'file_list parameter should be a '
116120
'pandas.DataFrame instance and has input '
117121
'section name [%s] as a column name.', required)
122+
if required_sections:
123+
tf.logging.fatal('Reader requires section(s): %s',
124+
required_sections)
125+
if file_list is not None:
126+
tf.logging.fatal('Configuration input sections are: %s',
127+
list(file_list))
118128
raise
129+
119130
self._file_list = file_list
120131
self.output_list = _filename_to_image_list(
121132
self._file_list, self._input_sources, data_param)
@@ -284,16 +295,20 @@ def _filename_to_image_list(file_list, mod_dict, data_param):
284295
"""
285296
volume_list = []
286297
for idx in range(len(file_list)):
298+
# create image instance for each subject
287299
print_progress_bar(idx, len(file_list),
288300
prefix='reading datasets headers',
289301
decimals=1, length=10, fill='*')
302+
290303
# combine fieldnames and volumes as a dictionary
291304
_dict = {}
292305
for field, modalities in mod_dict.items():
293-
_dict[field] = _create_image(
294-
file_list, idx, modalities, data_param)
306+
_dict[field] = _create_image(file_list, idx, modalities, data_param)
307+
308+
# skipping the subject if there're missing image components
295309
if _dict and None not in list(_dict.values()):
296310
volume_list.append(_dict)
311+
297312
if not volume_list:
298313
tf.logging.fatal(
299314
"Empty filename lists, please check the csv "

niftynet/io/image_sets_partitioner.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class ImageSetsPartitioner(object):
6161
def initialise(self,
6262
data_param,
6363
new_partition=False,
64-
data_split_file="./test.csv",
64+
data_split_file=None,
6565
ratios=None):
6666
"""
6767
Set the data partitioner parameters
@@ -77,7 +77,10 @@ def initialise(self,
7777
and get_file_list always returns all subjects.
7878
"""
7979
self.data_param = data_param
80-
self.data_split_file = data_split_file
80+
if data_split_file is None:
81+
self.data_split_file = os.path.join('.', 'dataset_split.csv')
82+
else:
83+
self.data_split_file = data_split_file
8184
self.ratios = ratios
8285

8386
self._file_list = None

niftynet/utilities/niftynet_global_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def __load_or_create(config_file):
7373
creates a default one.
7474
7575
:param config_file: no sanity checks are performed, as this
76-
method is for internal use only
76+
method is for internal use only
7777
:type config_file: `os.path`
7878
:returns: a dictionary of parsed configuration options
7979
:rtype: `dict`

niftynet/utilities/user_parameters_custom.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# Standardised string is defined in
2020
# niftynet.utilities.user_parameters_helper.standardise_string
2121
# the section name will be filtered with,
22-
# re.sub('[^0-9a-zA-Z ]+', '', input_string.strip())
22+
# re.sub('[^0-9a-zA-Z_\- ]+', '', input_string.strip())
2323
#
2424
# the value should be __add_mytask_args()
2525
#

niftynet/utilities/user_parameters_default.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818

1919

2020
def add_application_args(parser):
21-
parser.add_argument(
22-
"action",
23-
help="train or inference",
24-
choices=['train', 'inference'])
21+
# parser.add_argument(
22+
# "action",
23+
# help="train or inference action",
24+
# choices=['train', 'inference'])
2525

2626
parser.add_argument(
2727
"--cuda_devices",

niftynet/utilities/user_parameters_helper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def str_array(string_input):
5353
except ValueError:
5454
raise argparse.ArgumentTypeError(
5555
"list of strings expected, for each list element the allowed"
56-
"characters: [ a-zA-Z0-9], but received {}".format(string_input))
56+
"characters: [ a-zA-Z0-9_\-], but received {}".format(string_input))
5757
return output_tuple
5858

5959

@@ -98,7 +98,7 @@ def standardise_string(input_string):
9898
"""
9999
if not isinstance(input_string, string_types):
100100
return input_string
101-
new_name = re.sub('[^0-9a-zA-Z ]+', '', input_string.strip())
101+
new_name = re.sub('[^0-9a-zA-Z_\- ]+', '', input_string.strip())
102102
return new_name
103103

104104

niftynet/utilities/user_parameters_parser.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import argparse
1010
import os
11+
import textwrap
1112

1213
from niftynet.engine.application_factory import ApplicationFactory
1314
from 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
'\nUnknown 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,

tests/entry_point_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ def test_wrong_param(self):
5454
net_run.main()
5555

5656
def test_empty(self):
57-
with self.assertRaisesRegexp(IOError, ''):
57+
with self.assertRaisesRegexp(SystemExit, ''):
5858
net_run.main()
59-
with self.assertRaisesRegexp(IOError, ''):
59+
with self.assertRaisesRegexp(SystemExit, ''):
6060
net_gan.main()
61-
with self.assertRaisesRegexp(IOError, ''):
61+
with self.assertRaisesRegexp(SystemExit, ''):
6262
net_segment.main()
63-
with self.assertRaisesRegexp(IOError, ''):
63+
with self.assertRaisesRegexp(SystemExit, ''):
6464
net_regress.main()
65-
with self.assertRaisesRegexp(IOError, ''):
65+
with self.assertRaisesRegexp(SystemExit, ''):
6666
net_autoencoder.main()
6767

6868

0 commit comments

Comments
 (0)