# Path: dltk/io/augmentation.py
# def extract_random_example_array(image_list,
#                                  example_size=[1, 64, 64],
#                                  n_examples=1):
#     """Randomly extract training examples from image (and a corresponding label).
#         Returns an image example array and the corresponding label array.
# 
#     Args:
#         image_list (np.ndarray or list or tuple): image(s) to extract random
#             patches from
#         example_size (list or tuple): shape of the patches to extract
#         n_examples (int): number of patches to extract in total
# 
#     Returns:
#         np.ndarray, np.ndarray: class-balanced patches extracted from full
#         images with the shape [batch, example_size..., image_channels]
#     """
# 
#     assert n_examples > 0
# 
#     was_singular = False
#     if isinstance(image_list, np.ndarray):
#         image_list = [image_list]
#         was_singular = True
# 
#     assert all([i_s >= e_s for i_s, e_s in zip(image_list[0].shape, example_size)]), \
#         'Image must be bigger than example shape'
#     assert (image_list[0].ndim - 1 == len(example_size) or image_list[0].ndim == len(example_size)), \
#         'Example size doesnt fit image size'
# 
#     for i in image_list:
#         if len(image_list) > 1:
#             assert (i.ndim - 1 == image_list[0].ndim or i.ndim == image_list[0].ndim or i.ndim + 1 == image_list[0].ndim),\
#                 'Example size doesnt fit image size'
# 
#             assert all([i0_s == i_s for i0_s, i_s in zip(image_list[0].shape, i.shape)]), \
#                 'Image shapes must match'
# 
#     rank = len(example_size)
# 
#     # Extract random examples from image and label
#     valid_loc_range = [image_list[0].shape[i] - example_size[i] for i in range(rank)]
# 
#     rnd_loc = [np.random.randint(valid_loc_range[dim], size=n_examples)
#                if valid_loc_range[dim] > 0
#                else np.zeros(n_examples, dtype=int) for dim in range(rank)]
# 
#     examples = [[]] * len(image_list)
#     for i in range(n_examples):
#         slicer = [slice(rnd_loc[dim][i], rnd_loc[dim][i] + example_size[dim])
#                   for dim in range(rank)]
# 
#         for j in range(len(image_list)):
#             ex_image = image_list[j][slicer][np.newaxis]
#             # Concatenate and return the examples
#             examples[j] = np.concatenate((examples[j], ex_image), axis=0) \
#                 if (len(examples[j]) != 0) else ex_image
# 
#     if was_singular:
#         return examples[0]
#     return examples
# 
# def flip(imagelist, axis=1):
#     """Randomly flip spatial dimensions
# 
#     Args:
#         imagelist (np.ndarray or list or tuple): image(s) to be flipped
#         axis (int): axis along which to flip the images
# 
#     Returns:
#         np.ndarray or list or tuple: same as imagelist but randomly flipped
#             along axis
#     """
# 
#     # Check if a single image or a list of images has been passed
#     was_singular = False
#     if isinstance(imagelist, np.ndarray):
#         imagelist = [imagelist]
#         was_singular = True
# 
#     # With a probility of 0.5 flip the image(s) across `axis`
#     do_flip = np.random.random(1)
#     if do_flip > 0.5:
#         for i in range(len(imagelist)):
#             imagelist[i] = np.flip(imagelist[i], axis=axis)
#     if was_singular:
#         return imagelist[0]
#     return imagelist
# 
# Path: dltk/io/preprocessing.py
# def whitening(image):
#     """Whitening. Normalises image to zero mean and unit variance."""
# 
#     image = image.astype(np.float32)
# 
#     mean = np.mean(image)
#     std = np.std(image)
# 
#     if std > 0:
#         ret = (image - mean) / std
#     else:
#         ret = image * 0.
#     return ret


import SimpleITK as sitk
import tensorflow as tf
import os
import numpy as np
from dltk.io.augmentation import extract_random_example_array, flip
from dltk.io.preprocessing import whitening
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import





# Path: dltk/io/augmentation.py
# def extract_random_example_array(image_list,
#                                  example_size=[1, 64, 64],
#                                  n_examples=1):
#     """Randomly extract training examples from image (and a corresponding label).
#         Returns an image example array and the corresponding label array.
# 
#     Args:
#         image_list (np.ndarray or list or tuple): image(s) to extract random
#             patches from
#         example_size (list or tuple): shape of the patches to extract
#         n_examples (int): number of patches to extract in total
# 
#     Returns:
#         np.ndarray, np.ndarray: class-balanced patches extracted from full
#         images with the shape [batch, example_size..., image_channels]
#     """
# 
#     assert n_examples > 0
# 
#     was_singular = False
#     if isinstance(image_list, np.ndarray):
#         image_list = [image_list]
#         was_singular = True
# 
#     assert all([i_s >= e_s for i_s, e_s in zip(image_list[0].shape, example_size)]), \
#         'Image must be bigger than example shape'
#     assert (image_list[0].ndim - 1 == len(example_size) or image_list[0].ndim == len(example_size)), \
#         'Example size doesnt fit image size'
# 
#     for i in image_list:
#         if len(image_list) > 1:
#             assert (i.ndim - 1 == image_list[0].ndim or i.ndim == image_list[0].ndim or i.ndim + 1 == image_list[0].ndim),\
#                 'Example size doesnt fit image size'
# 
#             assert all([i0_s == i_s for i0_s, i_s in zip(image_list[0].shape, i.shape)]), \
#                 'Image shapes must match'
# 
#     rank = len(example_size)
# 
#     # Extract random examples from image and label
#     valid_loc_range = [image_list[0].shape[i] - example_size[i] for i in range(rank)]
# 
#     rnd_loc = [np.random.randint(valid_loc_range[dim], size=n_examples)
#                if valid_loc_range[dim] > 0
#                else np.zeros(n_examples, dtype=int) for dim in range(rank)]
# 
#     examples = [[]] * len(image_list)
#     for i in range(n_examples):
#         slicer = [slice(rnd_loc[dim][i], rnd_loc[dim][i] + example_size[dim])
#                   for dim in range(rank)]
# 
#         for j in range(len(image_list)):
#             ex_image = image_list[j][slicer][np.newaxis]
#             # Concatenate and return the examples
#             examples[j] = np.concatenate((examples[j], ex_image), axis=0) \
#                 if (len(examples[j]) != 0) else ex_image
# 
#     if was_singular:
#         return examples[0]
#     return examples
# 
# def flip(imagelist, axis=1):
#     """Randomly flip spatial dimensions
# 
#     Args:
#         imagelist (np.ndarray or list or tuple): image(s) to be flipped
#         axis (int): axis along which to flip the images
# 
#     Returns:
#         np.ndarray or list or tuple: same as imagelist but randomly flipped
#             along axis
#     """
# 
#     # Check if a single image or a list of images has been passed
#     was_singular = False
#     if isinstance(imagelist, np.ndarray):
#         imagelist = [imagelist]
#         was_singular = True
# 
#     # With a probility of 0.5 flip the image(s) across `axis`
#     do_flip = np.random.random(1)
#     if do_flip > 0.5:
#         for i in range(len(imagelist)):
#             imagelist[i] = np.flip(imagelist[i], axis=axis)
#     if was_singular:
#         return imagelist[0]
#     return imagelist
# 
# Path: dltk/io/preprocessing.py
# def whitening(image):
#     """Whitening. Normalises image to zero mean and unit variance."""
# 
#     image = image.astype(np.float32)
# 
#     mean = np.mean(image)
#     std = np.std(image)
# 
#     if std > 0:
#         ret = (image - mean) / std
#     else:
#         ret = image * 0.
#     return ret


# Path: examples/applications/IXI_HH_sex_classification_resnet/reader.py
import SimpleITK as sitk
import tensorflow as tf
import os
import numpy as np
from dltk.io.augmentation import extract_random_example_array, flip
from dltk.io.preprocessing import whitening

from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import





def read_fn(file_references, mode, params=None):