Example #1
0
    def __getitem__(self, slicing, processes=None):
        ndim = self.ndim
        if ndim >= 3:
            slicing = slc.unpack_slicing(slicing, ndim)

            slicing_z = slicing[-1]
            array = self._tif.asarray(key=slicing_z, maxworkers=processes)
            array = array_from_tif(array)

            slicing_xy = (Ellipsis, ) + slicing[-3:-1]
            if len(array.shape) > len(self._tif.pages[0].shape):
                slicing_xy = slicing_xy + (slice(None), )
            return array[slicing_xy]

        else:
            array = self._tif.asarray(maxworkers=processes)
            array = array_from_tif(array)
            return array.__getitem__(slicing)
Example #2
0
    def __setitem__(self, slicing, data, processes=None):
        e = self.expression

        shape = self.shape
        ndim = self.ndim
        ndim_list = e.ntags()

        slicing = slc.unpack_slicing(slicing, ndim)

        slicing_file = slicing[:-ndim_list]
        slicing_list = slicing[-ndim_list:]

        shape_list = shape[-ndim_list:]

        #start indices
        indices_start = self.expression.indices(self.file_list[0])
        #TODO: steps in file list

        #genereate file list to read
        #Note: indices increase according to the axes order but thier own order is in tag order
        indices = []
        for sl, s, i in zip(slicing_list, shape_list, indices_start):
            if isinstance(sl, slice):
                slice_indices = sl.indices(s)
                slice_indices = (slice_indices[0] + i, slice_indices[1] + i,
                                 slice_indices[2])
                indices.append(range(*slice_indices))
            elif isinstance(sl, (list, np.ndarray)):
                indices.append(np.array(sl) + i)
            elif isinstance(sl, numbers.Integral):
                indices.append([sl + i])
            else:
                raise IndexError('Invalid slice specification %r!' % sl)
        indices.reverse()
        indices = itertools.product(*indices)
        indices = [i[::-1] for i in indices]

        axes_to_tags = self.axes_to_tag_order()
        if len(axes_to_tags) > 1 and axes_to_tags != list(
                range(len(axes_to_tags))):
            indices = [tuple(i[j] for j in axes_to_tags) for i in indices]

        fl = [e.string_from_index(i) for i in indices]
        #print indices, fl

        #create directory if it does not exists
        #Note: move this to func if files need to be distributed accross several directories
        fu.create_directory(fl[0], split=True)

        if processes is None:
            processes = mp.cpu_count()

        @ptb.parallel_traceback
        def func(filename, index, data=data, slicing=slicing_file):
            index = (Ellipsis, ) + index
            io.write(sink=filename,
                     data=data[index],
                     slicing=slicing,
                     processes='serial')

        if processes == 'serial':
            for f, i in zip(fl, indices):
                func(f, i)
        else:
            with concurrent.futures.ThreadPoolExecutor(processes) as executor:
                executor.map(func, fl, indices)
Example #3
0
    def __getitem__(self, slicing, processes=None, order=None):
        e = self.expression

        shape = self.shape
        ndim = self.ndim
        ndim_list = e.ntags()

        slicing = slc.unpack_slicing(slicing, ndim)

        slicing_file = slicing[:-ndim_list]
        slicing_list = slicing[-ndim_list:]

        shape_file = shape[:-ndim_list]
        shape_list = shape[-ndim_list:]

        sliced_shape_file = slc.sliced_shape(slicing=slicing_file,
                                             shape=shape_file)
        #sliced_shape_list = slc.sliced_shape(slicing=slicing_list, shape=shape_list);

        #start indices
        indices_start = self.expression.indices(self.file_list[0])
        #print(indices_start)
        #TODO: steps in file list

        #genereate file list to read
        #Note: indices increase according to the axes order but their own order is in tag order
        indices = []
        slicing_list_indices = []
        shape_list_keep_dims = ()
        slicing_keep_dims_to_final = (Ellipsis, )
        for sl, s, i in zip(slicing_list, shape_list, indices_start):
            if isinstance(sl, slice):
                slice_indices = sl.indices(s)
                slice_indices = (slice_indices[0] + i, slice_indices[1] + i,
                                 slice_indices[2])
                indices.append(range(*slice_indices))
                n = len(indices[-1])
                slicing_list_indices.append(range(n))
                shape_list_keep_dims += (n, )
                slicing_keep_dims_to_final += (slice(None), )
            elif isinstance(sl, (list, np.ndarray)):
                indices.append(np.array(sl) + i)
                n = len(indices[-1])
                slicing_list_indices.append(sl)
                shape_list_keep_dims += (n, )
                slicing_keep_dims_to_final += (slice(None), )
            elif isinstance(sl, numbers.Integral):
                indices.append([sl + i])
                slicing_list_indices.append([0])
                shape_list_keep_dims += (1, )
                slicing_keep_dims_to_final += (0, )
            else:
                raise IndexError('Invalid slice specification %r!' % sl)
        indices.reverse()
        indices = itertools.product(*indices)
        indices = [i[::-1] for i in indices]
        slicing_list_indices.reverse()
        slicing_list_indices = itertools.product(*slicing_list_indices)
        slicing_list_indices = [i[::-1] for i in slicing_list_indices]
        #print(indices, slicing_list_indices, slicing_keep_dims_to_final)

        axes_to_tags = self.axes_to_tag_order()
        if len(axes_to_tags) > 1 and axes_to_tags != list(
                range(len(axes_to_tags))):
            indices = [tuple(i[j] for j in axes_to_tags) for i in indices]

        fl = [e.string_from_index(i) for i in indices]
        #print(fl);

        dtype = self.dtype

        data = np.zeros(sliced_shape_file + shape_list_keep_dims,
                        dtype=dtype,
                        order=order)

        #@ptb.parallel_traceback
        def func(filename, index, data=data, slicing=slicing_file):
            index = (Ellipsis, ) + index
            data[index] = io.read(filename,
                                  slicing=slicing,
                                  processes='serial')

        if processes is None:
            processes = mp.cpu_count()

        if processes == 'serial':
            for f, i in zip(fl, slicing_list_indices):
                func(f, i)
        else:
            with concurrent.futures.ThreadPoolExecutor(processes) as executor:
                executor.map(func, fl, slicing_list_indices)

        data = data[slicing_keep_dims_to_final]

        return data