<commit_before>from __future__ import absolute_import, division, print_function

import numpy as np
import tensorflow as tf
from tensorflow.contrib.data.python.ops.dataset_ops import Dataset

from niftynet.engine.image_window import ImageWindow
from niftynet.layer.base_layer import Layer
from niftynet.layer.grid_warper import AffineGridWarperLayer
from niftynet.layer.resampler import ResamplerLayer
from niftynet.layer.linear_resize import LinearResizeLayer as Resize


class PairwiseResizeSampler(Layer):
    def __init__(self,
                 reader_0,
                 reader_1,
                 data_param,
                 batch_size=1):
        Layer.__init__(self, name='pairwise_sampler_resize')
        # reader for the fixed images
        self.reader_0 = reader_0
        # reader for the moving images
        self.reader_1 = reader_1

        # TODO:
        # 0) check the readers should have the same length file list
        # 1) detect window shape mismatches or defaulting
        #    windows to the fixed image reader properties
        # 2) reshape images to (supporting multi-modal data)
        #    [batch, x, y, channel] or [batch, x, y, z, channels]
        # 3) infer spatial rank
        # 4) make ``label`` optional
        self.batch_size = batch_size
        self.spatial_rank = 3
        self.window = ImageWindow.from_data_reader_properties(
            self.reader_0.input_sources,
            self.reader_0.shapes,
            self.reader_0.tf_dtypes,
            data_param)
        if self.window.has_dynamic_shapes:
            tf.logging.fatal('Dynamic shapes not supported.\nPlease specify '
                             'all spatial dims of the input data, for the '
                             'spatial_window_size parameter.')
            raise NotImplementedError
        # TODO: check spatial dims the same across input modalities
        self.image_shape = \
            self.reader_0.shapes['fixed_image'][:self.spatial_rank]
        self.moving_image_shape = \
            self.reader_1.shapes['moving_image'][:self.spatial_rank]
        self.window_size = self.window.shapes['fixed_image']

        # initialise a dataset prefetching pairs of image and label volumes
        n_subjects = len(self.reader_0.output_list)
        rand_ints = list(range(n_subjects))
        image_dataset = Dataset.from_tensor_slices(rand_ints)
        # mapping random integer id to 4 volumes moving/fixed x image/label
        # tf.py_func wrapper of ``get_pairwise_inputs``<commit_msg>make the dataset sequence length divisible by batch size, so that all elements get popped out<commit_after>from __future__ import absolute_import, division, print_function

import numpy as np
import tensorflow as tf
from tensorflow.contrib.data.python.ops.dataset_ops import Dataset

from niftynet.engine.image_window import ImageWindow
from niftynet.layer.base_layer import Layer
from niftynet.layer.grid_warper import AffineGridWarperLayer
from niftynet.layer.resampler import ResamplerLayer
from niftynet.layer.linear_resize import LinearResizeLayer as Resize


class PairwiseResizeSampler(Layer):
    def __init__(self,
                 reader_0,
                 reader_1,
                 data_param,
                 batch_size=1):
        Layer.__init__(self, name='pairwise_sampler_resize')
        # reader for the fixed images
        self.reader_0 = reader_0
        # reader for the moving images
        self.reader_1 = reader_1

        # TODO:
        # 0) check the readers should have the same length file list
        # 1) detect window shape mismatches or defaulting
        #    windows to the fixed image reader properties
        # 2) reshape images to (supporting multi-modal data)
        #    [batch, x, y, channel] or [batch, x, y, z, channels]
        # 3) infer spatial rank
        # 4) make ``label`` optional
        self.batch_size = int(batch_size)
        assert self.batch_size > 0, "batch size must be greater than 0"
        self.spatial_rank = 3
        self.window = ImageWindow.from_data_reader_properties(
            self.reader_0.input_sources,
            self.reader_0.shapes,
            self.reader_0.tf_dtypes,
            data_param)
        if self.window.has_dynamic_shapes:
            tf.logging.fatal('Dynamic shapes not supported.\nPlease specify '
                             'all spatial dims of the input data, for the '
                             'spatial_window_size parameter.')
            raise NotImplementedError
        # TODO: check spatial dims the same across input modalities
        self.image_shape = \
            self.reader_0.shapes['fixed_image'][:self.spatial_rank]
        self.moving_image_shape = \
            self.reader_1.shapes['moving_image'][:self.spatial_rank]
        self.window_size = self.window.shapes['fixed_image']

        # initialise a dataset prefetching pairs of image and label volumes
        n_subjects = len(self.reader_0.output_list)
        int_seq = list(range(n_subjects))
        # make the list of sequence divisible by batch size
        while len(int_seq) > 0 and len(int_seq) % self.batch_size != 0:
            int_seq.append(int_seq[-1])

        image_dataset = Dataset.from_tensor_slices(int_seq)
        # mapping random integer id to 4 volumes moving/fixed x image/label
        # tf.py_func wrapper of ``get_pairwise_inputs``
{'fraction_non_alphanumeric': 0.06834264432029795, 'fraction_numerical': 0.008379888268156424, 'mean_word_length': 2.4808813998703823, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5710730', 'n_tokens_mistral': 1542, 'n_tokens_neox': 1490, 'n_words': 421}