import functools
import copy as cp
import logging

import numpy as np


class ImageSheet(object):


    def append(self, new_cell):
        
        if not hasattr(self, 'images'):
            self.images = [new_cell]
        else:
            self.images.append(new_cell)


    def apply(self, fn, *args, **kwargs):
        fn = functools.partial(fn, *args, **kwargs)
        self.images = map(fn, self.images)


    def copy(self):
        new_sheet = ImageSheet()
        new_sheet.images = cp.deepcopy(self.images)
        return new_sheet


    def get_output(self, axis):
        output = np.concatenate(self.images, axis=axis)
        logging.info('concatenated sheet has size: {0}'.format(output.shape))
        return output


    @staticmethod
    def build_from_image(image, n, axis):

        images = np.split(image, n, axis)

        sheet = ImageSheet()
        sheet.images = images

        return sheet
