def __init__(self, which_norb, which_set, image_dtype='uint8'):
        """
        Reads the specified NORB dataset from a memmap cache.
        Creates this cache first, if necessary.

        Parameters
        ----------

        which_norb : str
            Valid values: 'big' or 'small'.
            Chooses between the (big) 'NORB dataset', and the 'Small NORB
            dataset'.

        which_set : str
            Valid values: 'test', 'train', or 'both'.
            Chooses between the testing set or the training set. If 'both',
            the two datasets will be stacked together (testing data in the
            first N rows, then training data).

        image_dtype : str, or numpy.dtype
            The dtype to store image data as in the memmap cache.
            Default is uint8, which is what the original NORB files use.
        """

        if which_norb not in ('big', 'small'):
            raise ValueError("Expected which_norb argument to be either 'big' "
                             "or 'small', not '%s'" % str(which_norb))

        if which_set not in ('test', 'train', 'both'):
            raise ValueError("Expected which_set argument to be either 'test' "
                             "or 'train', not '%s'." % str(which_set))

        # This will check that dtype is a legitimate dtype string.
        image_dtype = numpy.dtype(image_dtype)

        # Maps column indices of self.y to the label type it contains.
        # Names taken from http://www.cs.nyu.edu/~ylclab/data/norb-v1.0/
        self.label_index_to_name = ('category',
                                    'instance',
                                    'elevation',
                                    'azimuth',
                                    'lighting condition')

        # Big NORB has additional label types
        if which_norb == 'big':
            self.label_index_to_name = (self.label_index_to_name +
                                        ('horizontal shift',  # in pixels
                                         'vertical shift',  # in pixels
                                         'lumination change',
                                         'contrast',
                                         'object scale',
                                         'rotation'))

        # Maps label type names to the corresponding column indices of self.y
        self.label_name_to_index = {}
        for index, name in enumerate(self.label_index_to_name):
            self.label_name_to_index[name] = index

        self.label_to_value_funcs = (get_category_value,
                                     get_instance_value,
                                     get_elevation_value,
                                     get_azimuth_value,
                                     get_lighting_value)
        if which_norb == 'big':
            self.label_to_value_funcs = (self.label_to_value_funcs +
                                         (get_horizontal_shift_value,
                                          get_vertical_shift_value,
                                          get_lumination_change_value,
                                          get_contrast_change_value,
                                          get_scale_change_value,
                                          get_rotation_change_value))

        # The size of one side of the image
        image_length = 96 if which_norb == 'small' else 108

        def read_norb_files(norb_files, output):
            """
            Reads the contents of a list of norb files into a matrix.
            Data is assumed to be in row-major order.
            """

            def read_norb_file(norb_file_path, debug=False):
                """
                Returns the numbers in a single NORB file as a 1-D ndarray.

                Parameters
                ----------

                norb_file_path : str
                  A NORB file from which to read.
                  Can be uncompressed (*.mat) or compressed (*.mat.gz).

                debug : bool
                  Set to True if you want debug printfs.
                """

                if not (norb_file_path.endswith(".mat") or
                        norb_file_path.endswith(".mat.gz")):
                    raise ValueError("Expected norb_file_path to end in "
                                     "either '.mat' or '.mat.gz'. Instead "
                                     "got '%s'" % norb_file_path)

                if not os.path.isfile(norb_file_path):
                    raise IOError("Could not find NORB file '%s' in expected "
                                  "directory '%s'." %
                                  reversed(os.path.split(norb_file_path)))

                file_handle = (gzip.open(norb_file_path)
                               if norb_file_path.endswith('.mat.gz')
                               else open(norb_file_path))

                def readNums(file_handle, num_type, count):
                    """
                    Reads some numbers from a file and returns them as a
                    numpy.ndarray.

                    Parameters
                    ----------

                    file_handle : file handle
                      The file handle from which to read the numbers.

                    num_type : str, numpy.dtype
                      The dtype of the numbers.

                    count : int
                      Reads off this many numbers.
                    """
                    num_bytes = count * numpy.dtype(num_type).itemsize
                    string = file_handle.read(num_bytes)
                    return numpy.fromstring(string, dtype=num_type)

                (elem_type,
                 elem_size,
                 _num_dims,
                 shape,
                 num_elems) = read_header(file_handle, debug)
                del _num_dims

                beginning = file_handle.tell()

                result = None
                if isinstance(file_handle, (gzip.GzipFile, bz2.BZ2File)):
                    result = readNums(file_handle,
                                      elem_type,
                                      num_elems * elem_size).reshape(shape)
                else:
                    result = numpy.fromfile(file_handle,
                                            dtype=elem_type,
                                            count=num_elems).reshape(shape)

                return result  # end of read_norb_file()

            row_index = 0
            for norb_file in norb_files:
                print("copying NORB file %s" % os.path.split(norb_file)[1])
                norb_data = read_norb_file(norb_file)
                norb_data = norb_data.reshape(-1, output.shape[1])
                end_row = row_index + norb_data.shape[0]
                output[row_index:end_row, :] = norb_data
                row_index = end_row

            assert end_row == output.shape[0]  # end of read_norb_files

        if which_norb == 'small':
            training_set_size = 24300
            testing_set_size = 24300
        else:
            assert which_norb == 'big'
            num_rows_per_file = 29160
            training_set_size = num_rows_per_file * 10
            testing_set_size = num_rows_per_file * 2

        def load_images(which_norb, which_set, dtype):
            """
            Reads image data from memmap disk cache, if available. If not, then
            first builds the memmap file from the NORB files.

            Parameters
            ----------
            which_norb : str
            'big' or 'small'.

            which_set : str
            'test', 'train', or 'both'.

            dtype : numpy.dtype
            The dtype of the image memmap cache file. If a
            cache of this dtype doesn't exist, it will be created.
            """

            assert type(dtype) == numpy.dtype

            memmap_path = get_memmap_path(which_norb, 'images_%s' % str(dtype))
            row_size = 2 * (image_length ** 2)
            shape = (training_set_size + testing_set_size, row_size)

            def make_memmap():
                dat_files = get_norb_file_paths(which_norb, 'both', 'dat')

                memmap_dir = os.path.split(memmap_path)[0]
                if not os.path.isdir(memmap_dir):
                    os.mkdir(memmap_dir)

                print("Allocating memmap file %s" % memmap_path)
                writeable_memmap = numpy.memmap(filename=memmap_path,
                                                dtype=dtype,
                                                mode='w+',
                                                shape=shape)

                read_norb_files(dat_files, writeable_memmap)

            if not os.path.isfile(memmap_path):
                print("Caching images to memmap file. This "
                      "will only be done once.")
                make_memmap()

            images = numpy.memmap(filename=memmap_path,
                                  dtype=dtype,
                                  mode='r',
                                  shape=shape)
            if which_set == 'train':
                images = images[:training_set_size, :]
            elif which_set == 'test':
                images = images[training_set_size:, :]

            return images

        def load_labels(which_norb, which_set):
            """
            Reads label data (both category and info data) from memmap disk
            cache, if available. If not, then first builds the memmap file from
            the NORB files.
            """
            memmap_path = get_memmap_path(which_norb, 'labels')
            dtype = numpy.dtype('int32')
            row_size = 5 if which_norb == 'small' else 11
            shape = (training_set_size + testing_set_size, row_size)

            def make_memmap():
                cat_files, info_files = [get_norb_file_paths(which_norb,
                                                             'both',
                                                             x)
                                         for x in ('cat', 'info')]

                memmap_dir = os.path.split(memmap_path)[0]
                if not os.path.isdir(memmap_dir):
                    os.mkdir(memmap_dir)

                print("allocating labels' memmap...")
                writeable_memmap = numpy.memmap(filename=memmap_path,
                                                dtype=dtype,
                                                mode='w+',
                                                shape=shape)
                print("... done.")

                cat_memmap = writeable_memmap[:, :1]   # 1st column
                info_memmap = writeable_memmap[:, 1:]  # remaining columns

                for norb_files, memmap in safe_zip((cat_files, info_files),
                                                   (cat_memmap, info_memmap)):
                    read_norb_files(norb_files, memmap)

            if not os.path.isfile(memmap_path):
                print("Caching images to memmap file %s.\n"
                      "This will only be done once." % memmap_path)
                make_memmap()

            labels = numpy.memmap(filename=memmap_path,
                                  dtype=dtype,
                                  mode='r',
                                  shape=shape)

            if which_set == 'train':
                labels = labels[:training_set_size, :]
            elif which_set == 'test':
                labels = labels[training_set_size:, :]

            return labels

        def get_norb_dir(which_norb):
            datasets_dir = os.getenv('PYLEARN2_DATA_PATH')
            if datasets_dir is None:
                raise RuntimeError("Please set the 'PYLEARN2_DATA_PATH' "
                                   "environment variable to tell pylearn2 "
                                   "where the datasets are.")

            if not os.path.isdir(datasets_dir):
                raise IOError("The PYLEARN2_DATA_PATH directory (%s) "
                              "doesn't exist." % datasets_dir)

            return os.path.join(datasets_dir,
                                'norb' if which_norb == 'big'
                                else 'norb_small')

        norb_dir = get_norb_dir(which_norb)

        def get_memmap_path(which_norb, file_basename):
            assert which_norb in ('big', 'small')
            assert (file_basename == 'labels' or
                    file_basename.startswith('images')), file_basename

            memmap_dir = os.path.join(norb_dir, 'memmaps_of_original')
            return os.path.join(memmap_dir, "%s.npy" % file_basename)

        def get_norb_file_paths(which_norb, which_set, norb_file_type):
            """
            Returns a list of paths for a given norb file type.

            For example,

                get_norb_file_paths('big', 'test', 'cat')

            Will return the category label files ('cat') for the big NORB
            dataset's test set.
            """

            assert which_set in ('train', 'test', 'both')

            if which_set == 'both':
                return (get_norb_file_paths(which_norb,
                                            'train',
                                            norb_file_type) +
                        get_norb_file_paths(which_norb,
                                            'test',
                                            norb_file_type))

            norb_file_types = ('cat', 'dat', 'info')
            if norb_file_type not in norb_file_types:
                raise ValueError("Expected norb_file_type to be one of %s, "
                                 "but it was '%s'" % (str(norb_file_types),
                                                      norb_file_type))

            instance_list = '01235' if which_set == 'test' else '46789'

            if which_norb == 'small':
                templates = ['smallnorb-5x%sx9x18x6x2x96x96-%sing-%%s.mat' %
                             (instance_list, which_set)]
            else:
                numbers = range(1, 3 if which_set == 'test' else 11)
                templates = ['norb-5x%sx9x18x6x2x108x108-%sing-%02d-%%s.mat' %
                             (instance_list, which_set, n) for n in numbers]

            original_files_dir = os.path.join(norb_dir, 'original')
            return [os.path.join(original_files_dir, norb_file_type % t)
                    for t in templates]

        def make_view_converter(which_norb, which_set):
            image_length = 96 if which_norb == 'small' else 108
            datum_shape = (2,  # number of images per stereo pair
                           image_length,  # image height
                           image_length,  # image width
                           1)  # number of channels
            axes = ('b', 's', 0, 1, 'c')
            return StereoViewConverter(datum_shape, axes)

        images = load_images(which_norb, which_set, image_dtype)
        labels = load_labels(which_norb, which_set)
        view_converter = make_view_converter(which_norb, which_set)

        super(NORB, self).__init__(X=images,
                                   y=labels,
                                   y_labels=numpy.max(labels) + 1,
                                   view_converter=view_converter)

        # Needed for pickling / unpickling.
        # These are set during pickling, by __getstate__()
        self.X_memmap_info = None
        self.y_memmap_info = None
dataset/ETHPy150Open lisa-lab/pylearn2/pylearn2/datasets/new_norb.py/NORB.__init__/swapped operands of `%`