Commit 3868bc6b by Wenqi Li

Merge branch 'fixes-shape-infer-from-image-header' into 'dev'

Minor fixes - shape infer. from image header See merge request CMIC/NiftyNet!240
parents a5f3f7d8 3172bc25
Pipeline #11505 failed with stages
in 33 seconds
......@@ -502,7 +502,7 @@ For a 2-D slice, e.g, `Nx1xM`, the second dimension of `border` should be `0`.
|[inference_iter](#inference_iter)|Integer|`inference_iter=1000`|`-1`|
|[save_seg_dir](#save_seg_dir)|String|`save_seg_dir=output/test`| `output`|
|[output_interp_order](#output_interp_order)|Non-negative integer|`output_interp_order=0`|`0`|
|[dataset_to_infer](#dataset_to_infer) :: `Training` or `Validation` or `Inference` :: `dataset_to_infer=Training` :: `''``
|[dataset_to_infer](#dataset_to_infer) | `Training` or `Validation` or `Inference` | `dataset_to_infer=Training` |`''`|
###### `spatial_window_size`
Array of integers indicating the size of input window.
......@@ -526,7 +526,7 @@ Prediction directory name. If it's a relative path, it is set to be relative to
Interpolation order of the network outputs.
###### `dataset_to_infer`
String Specifies which dataset ('Training', 'Validation', 'Inference') to compute inference for.
String specifies which dataset ('Training', 'Validation', 'Inference') to compute inference for.
By default 'Inference' dataset is used.
## Global-settings
......
......@@ -525,7 +525,7 @@ For a 2-D slice, e.g, `Nx1xM`, the second dimension of `border` should be `0`.
- [inference_iter](#inference-iter) :: `integer` :: `inference_iter=1000` :: `-1`
- [save_seg_dir](#save-seg-dir) :: `string` :: `save_seg_dir=output/test` :: `output`
- [output_interp_order](#output-interp-order) :: `non-negative integer` :: `output_interp_order=0` :: `0`
- [dataset_to_infer](#dataset-to-infer) :: `Training|Validation|Inference` :: `dataset_to_infer=Training` :: `''``
- [dataset_to_infer](#dataset-to-infer) :: `Training|Validation|Inference` :: `dataset_to_infer=Training` :: `''`
###### `spatial_window_size`
Array of integers indicating the size of input window. By default, the window
......@@ -549,5 +549,5 @@ Prediction directory name. If it's a relative path, it is set to be relative to
Interpolation order of the network outputs.
###### `dataset_to_infer`
String Specifies which dataset ('Training', 'Validation', 'Inference') to compute inference for.
String specifies which dataset ('Training', 'Validation', 'Inference') to compute inference for.
By default 'Inference' dataset is used.
......@@ -88,7 +88,7 @@ def load_image_from_file(filename, loader=None):
"""
if loader and loader in SUPPORTED_LOADERS:
if loader not in AVAILABLE_LOADERS:
raise ValueError('Image Loader {} supported buy library not found.'
raise ValueError('Image Loader {} supported but library not found.'
' Required libraries: {}'
.format(loader, SUPPORTED_LOADERS[loader]))
tf.logging.debug('Using requested loader: {}'.format(loader))
......
......@@ -199,6 +199,9 @@ class SpatialImage2D(DataFromFile):
n_modalities = \
np.sum([int(shape[4]) for shape in self._original_shape])
self._original_shape = non_modality_shapes.pop() + (n_modalities,)
self._original_shape = \
tuple([shape_i if shape_i > 0 else 1
for shape_i in self._original_shape])
return self._original_shape
def _load_header(self):
......@@ -649,16 +652,18 @@ class ImageFactory(object):
try:
file_path = resolve_file_name(file_path, ('.', home_folder))
if os.path.isfile(file_path):
ndims = misc.infer_ndims_from_file(file_path)
loader = kwargs.get('loader', None)
ndims = misc.infer_ndims_from_file(file_path, loader)
image_type = cls.INSTANCE_DICT.get(ndims, None)
except (TypeError, IOError):
except (TypeError, IOError, AttributeError):
pass
if image_type is None:
try:
file_path = [resolve_file_name(path, ('.', home_folder))
for path in file_path]
ndims = misc.infer_ndims_from_file(file_path[0])
loader = kwargs.get('loader', (None,))
ndims = misc.infer_ndims_from_file(file_path[0], loader[0])
ndims = ndims + (1 if len(file_path) > 1 else 0)
image_type = cls.INSTANCE_DICT.get(ndims, None)
except (AssertionError, TypeError, IOError, AttributeError):
......
......@@ -30,9 +30,9 @@ FILE_LOG_FORMAT = "%(levelname)s:niftynet:%(asctime)s: %(message)s"
#### utilities for file headers
def infer_ndims_from_file(file_path):
def infer_ndims_from_file(file_path, loader=None):
# todo: loader specified by the user is not used for ndims infer.
image_header = load_image_from_file(file_path).header
image_header = load_image_from_file(file_path, loader).header
try:
return int(image_header['dim'][0])
except TypeError:
......
......@@ -170,9 +170,9 @@ class ImageTypeTest(tf.test.TestCase):
file_path=CASE_5D,
name='5d_image',
interp_order=3,
output_pixdim=(None,),
output_pixdim=None,
output_axcodes=None,
loader=(None,))
loader=None)
self.assertIsInstance(image, SpatialImage5D)
output = image.get_data()
self.assertAllClose(np.array([208, 256, 256, 1, 1]), output.shape)
......@@ -183,8 +183,8 @@ class ImageTypeTest(tf.test.TestCase):
file_path=CASE_5D,
name='5d_image',
interp_order=1,
output_pixdim=((4, 8, 6.0),),
output_axcodes=(None,),
output_pixdim=(4, 8, 6.0),
output_axcodes=None,
loader=None)
self.assertIsInstance(image, SpatialImage5D)
output = image.get_data()
......@@ -193,12 +193,12 @@ class ImageTypeTest(tf.test.TestCase):
def test_5d_reorientation(self):
image = ImageFactory.create_instance(
file_path=CASE_5D,
file_path=(CASE_5D,),
name='5d_image',
interp_order=3,
output_pixdim=(None,),
output_axcodes=(('S', 'A', 'L'),),
loader=None)
loader=(None,))
self.assertIsInstance(image, SpatialImage5D)
output = image.get_data()
self.assertAllClose(np.array([256, 256, 208, 1, 1]), output.shape)
......@@ -206,12 +206,12 @@ class ImageTypeTest(tf.test.TestCase):
def test_5d_reorientation_resample(self):
image = ImageFactory.create_instance(
file_path=CASE_5D,
file_path=(CASE_5D,),
name='5d_image',
interp_order=3,
output_pixdim=((8, 9, 10),),
output_axcodes='RSA',
loader=None)
output_axcodes=('RSA',),
loader=(None,))
self.assertIsInstance(image, SpatialImage5D)
output = image.get_data()
self.assertAllClose(np.array([29, 31, 29, 1, 1]), output.shape)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment