<jupyter_start><jupyter_text>This demo provides examples of `ImageReader` class from `niftynet.io.image_reader` module.What is `ImageReader`?The main functionality of `ImageReader` is to search a set of folders, return a list of image files, and load the images into memory in an iterative manner.A `tf.data.Dataset` instance can be initialised from an `ImageReader`, this makes the module readily usable as an input op to many tensorflow-based applications.Why `ImageReader`? - designed for medical imaging formats and applications  - works well with multi-modal input volumes - works well with `tf.data.Dataset`  Before the demo... First make sure the source code is available, and import the module.For NiftyNet installation, please checkout:http://niftynet.readthedocs.io/en/dev/installation.html<jupyter_code>import sys
niftynet_path = '/Users/bar/Documents/Niftynet/'
sys.path.append(niftynet_path)

from niftynet.io.image_reader import ImageReader<jupyter_output>INFO:tensorflow:TensorFlow version 1.5.0
CRITICAL:tensorflow:Optional Python module cv2 not found, please install cv2 and retry if the application fails.
INFO:tensorflow:Available Image Loaders:
['nibabel', 'skimage', 'pillow', 'simpleitk', 'dummy'].<jupyter_text>For demonstration purpose we download some demo data to `~/niftynet/data/`:<jupyter_code>from niftynet.utilities.download import download
download('anisotropic_nets_brats_challenge_model_zoo_data')<jupyter_output>Accessing: https://cmiclab.cs.ucl.ac.uk/CMIC/NiftyNetExampleServer
anisotropic_nets_brats_challenge_model_zoo_data: OK. 
Already downloaded. Use the -r option to download again.<jupyter_text>Use case: loading 3D volumes<jupyter_code>from niftynet.io.image_reader import ImageReader

data_param = {'MR': {'path_to_search': '~/niftynet/data/BRATS_examples/HGG'}}
reader = ImageReader().initialise(data_param)
reader.shapes, reader.tf_dtypes
# read data using the initialised reader
idx, image_data, interp_order = reader(idx=0)
image_data['MR'].shape, image_data['MR'].dtype
# randomly sample the list of images
for _ in range(3):
    idx, image_data, _ = reader()
    print('{} image: {}'.format(idx, image_data['MR'].shape))<jupyter_output>3 image: (134, 167, 135, 1, 1)
6 image: (152, 169, 130, 1, 1)
8 image: (152, 169, 130, 1, 1)<jupyter_text>The images are always read into a 5D-array, representing:`[height, width, depth, time, channels]`  User case: loading pairs of image and label by matching filenames(In this case the loaded arrays are not concatenated.)<jupyter_code>from niftynet.io.image_reader import ImageReader

data_param = {'image': {'path_to_search': '~/niftynet/data/BRATS_examples/HGG',
                        'filename_contains': 'T2'},
              'label': {'path_to_search': '~/niftynet/data/BRATS_examples/HGG',
                        'filename_contains': 'Label'}}
reader = ImageReader().initialise(data_param)
# image file information (without loading the volumes)
reader.get_subject(0)
idx, image_data, interp_order = reader(idx=0)

image_data['image'].shape, image_data['label'].shape<jupyter_output><empty_output><jupyter_text>User case: loading multiple modalities of image and label by matching filenamesThe following code initialises a reader with four modalities, and the `'image'` output is a concatenation of arrays loaded from these files. (The files are concatenated at the fifth dimension)<jupyter_code>from niftynet.io.image_reader import ImageReader

data_param = {'T1':    {'path_to_search': '~/niftynet/data/BRATS_examples/HGG',
                        'filename_contains': 'T1', 'filename_not_contains': 'T1c'},
              'T1c':   {'path_to_search': '~/niftynet/data/BRATS_examples/HGG',
                        'filename_contains': 'T1c'},
              'T2':    {'path_to_search': '~/niftynet/data/BRATS_examples/HGG',
                        'filename_contains': 'T2'},
              'Flair': {'path_to_search': '~/niftynet/data/BRATS_examples/HGG',
                        'filename_contains': 'Flair'},
              'label': {'path_to_search': '~/niftynet/data/BRATS_examples/HGG',
                        'filename_contains': 'Label'}}
grouping_param = {'image': ('T1', 'T1c', 'T2', 'Flair'), 'label':('label',)}
reader = ImageReader().initialise(data_param, grouping_param)
_, image_data, _ = reader(idx=0)
image_data['image'].shape, image_data['label'].shape<jupyter_output><empty_output><jupyter_text>More propertiesThe input specification supports additional properties include ```python{'csv_file', 'path_to_search', 'filename_contains', 'filename_not_contains', 'interp_order', 'pixdim', 'axcodes', 'spatial_window_size', 'loader'}```see also: http://niftynet.readthedocs.io/en/dev/config_spec.htmlinput-data-source-section  Using ImageReader with image-level data augmentation layers<jupyter_code>from niftynet.io.image_reader import ImageReader
from niftynet.layer.rand_rotation import RandomRotationLayer as Rotate

data_param = {'MR': {'path_to_search': '~/niftynet/data/BRATS_examples/HGG'}}
reader = ImageReader().initialise(data_param)

rotation_layer = Rotate()
rotation_layer.init_uniform_angle([-10.0, 10.0])
reader.add_preprocessing_layers([rotation_layer])

_, image_data, _ = reader(idx=0)
image_data['MR'].shape

# import matplotlib.pyplot as plt
# plt.imshow(image_data['MR'][:, :, 50, 0, 0])
# plt.show()<jupyter_output>[1mINFO:niftynet:[0m 

Number of subjects 10, input section names: ['subject_id', 'MR']
-- using all subjects (without data partitioning).

[1mINFO:niftynet:[0m Image reader: loading 10 subjects from sections ('MR',) as input [MR]<jupyter_text>Using ImageReader with `tf.data.Dataset`<jupyter_code>import tensorflow as tf
from niftynet.io.image_reader import ImageReader

# initialise multi-modal image and label reader
data_param = {'T1':    {'path_to_search': '~/niftynet/data/BRATS_examples/HGG',
                        'filename_contains': 'T1', 'filename_not_contains': 'T1c'},
              'T1c':   {'path_to_search': '~/niftynet/data/BRATS_examples/HGG',
                        'filename_contains': 'T1c'},
              'T2':    {'path_to_search': '~/niftynet/data/BRATS_examples/HGG',
                        'filename_contains': 'T2'},
              'Flair': {'path_to_search': '~/niftynet/data/BRATS_examples/HGG',
                        'filename_contains': 'Flair'},
              'label': {'path_to_search': '~/niftynet/data/BRATS_examples/HGG',
                        'filename_contains': 'Label'}}

grouping_param = {'image': ('T1', 'T1c', 'T2', 'Flair'), 'label':('label',)}
reader = ImageReader().initialise(data_param, grouping_param)

# reader as a generator
def image_label_pair_generator():
    """
    A generator wrapper of an initialised reader.
    
    :yield: a dictionary of images (numpy arrays).
    """
    while True:
        _, image_data, _ = reader()
        yield image_data

# tensorflow dataset
dataset = tf.data.Dataset.from_generator(
    image_label_pair_generator,
    output_types=reader.tf_dtypes)
    #output_shapes=reader.shapes)
dataset = dataset.batch(1)
iterator = dataset.make_initializable_iterator()

# run the tensorlfow graph
with tf.Session() as sess:
    sess.run(iterator.initializer)
    for _ in range(3):
        data_dict = sess.run(iterator.get_next())
        print(data_dict.keys())
        print('image: {}, label: {}'.format(
            data_dict['image'].shape,
            data_dict['label'].shape))<jupyter_output>[1mINFO:niftynet:[0m 

Number of subjects 2, input section names: ['subject_id', 'T1', 'T1c', 'T2', 'Flair', 'label']
-- using all subjects (without data partitioning).

[1mINFO:niftynet:[0m Image reader: loading 2 subjects from sections ('T1', 'T1c', 'T2', 'Flair') as input [image]
[1mINFO:niftynet:[0m Image reader: loading 2 subjects from sections ('label',) as input [label]
dict_keys(['image', 'label'])
image: (1, 134, 167, 135, 1, 4), label: (1, 134, 167, 135, 1, 1)
dict_keys(['image', 'label'])
image: (1, 134, 167, 135, 1, 4), label: (1, 134, 167, 135, 1, 1)
dict_keys(['image', 'label'])
image: (1, 152, 169, 130, 1, 4), label: (1, 152, 169, 130, 1, 1)
{'fraction_non_alphanumeric': 0.13535502958579881, 'fraction_numerical': 0.021819526627218935, 'mean_word_length': 4.522804628999319, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '12840', 'license': 'Apache-2.0', 'n_tokens_mistral': 2823, 'n_tokens_neox': 2612, 'n_words': 654, 'path': 'demos/module_examples/ImageReader.ipynb', 'repo_name': 'Min-Sheng/NiftyNet', 'size': '8112'}