Ejemplo n.º 1
0
    def from_np(cls, arr, block_shape, copy, system):
        dtype_str = str(arr.dtype)
        grid = ArrayGrid(arr.shape, block_shape, dtype_str)
        rarr = SparseBlockArray(grid, system)
        grid_entry_iterator = grid.get_entry_iterator()
        for grid_entry in grid_entry_iterator:
            grid_slice = grid.get_slice(grid_entry)
            block = scipy.sparse.csr_matrix(arr[grid_slice])

            rarr.blocks[grid_entry].oid = system.put(block)
            rarr.blocks[grid_entry].dtype = getattr(np, dtype_str)
        return rarr
Ejemplo n.º 2
0
 def from_np(cls, arr, block_shape, copy, system):
     dtype_str = str(arr.dtype)
     grid = ArrayGrid(arr.shape, block_shape, dtype_str)
     rarr = BlockArray(grid, system)
     grid_entry_iterator = grid.get_entry_iterator()
     for grid_entry in grid_entry_iterator:
         grid_slice = grid.get_slice(grid_entry)
         block = arr[grid_slice]
         if copy:
             block = np.copy(block)
         rarr.blocks[grid_entry].oid = system.put(block)
         rarr.blocks[grid_entry].dtype = getattr(np, dtype_str)
     return rarr
Ejemplo n.º 3
0
    def reshape(self, shape=None, block_shape=None):
        # TODO (hme): Add support for arbitrary reshape.
        if shape is None:
            shape = self.shape
        if block_shape is None:
            block_shape = self.block_shape
        if shape == self.shape and block_shape == self.block_shape:
            return self

        temp_shape = shape
        temp_block_shape = block_shape
        shape = []
        block_shape = []
        negative_one = False
        for i, dim in enumerate(temp_shape):
            if dim == -1:
                assert len(self.shape) == 1
                if negative_one:
                    raise Exception("Only one -1 permitted in reshape.")
                negative_one = True
                shape.append(self.shape[i])
                assert temp_block_shape[i] == -1
                block_shape.append(self.block_shape[0])
            else:
                shape.append(dim)
                block_shape.append(temp_block_shape[i])
        del temp_shape
        shape = tuple(shape)
        block_shape = tuple(block_shape)

        assert np.product(shape) == np.product(self.shape)
        # Make sure the difference is either a preceding or succeeding one.
        if len(shape) > len(self.shape):
            if shape[0] == 1:
                grid_entry_op = "shift"
                assert shape[1:] == self.shape
            elif shape[-1] == 1:
                grid_entry_op = "pop"
                assert shape[:-1] == self.shape
            else:
                raise Exception()
        elif len(shape) < len(self.shape):
            if self.shape[0] == 1:
                grid_entry_op = "prep"
                assert self.shape[1:] == shape
            elif self.shape[-1] == 1:
                grid_entry_op = "app"
                assert self.shape[:-1] == shape
            else:
                raise Exception()
        else:
            grid_entry_op = "none"
            assert self.shape == shape

        grid = ArrayGrid(shape=shape,
                         block_shape=block_shape,
                         dtype=self.grid.dtype.__name__)
        grid_meta = grid.to_meta()
        rarr = BlockArray(grid, self.system)
        for grid_entry in grid.get_entry_iterator():
            rarr.blocks[grid_entry].oid = self.system.empty(grid_entry, grid_meta,
                                                            syskwargs={
                                                                "grid_entry": grid_entry,
                                                                "grid_shape": grid.grid_shape
                                                            })
            grid_entry_slice = grid.get_slice(grid_entry)
            if grid_entry_op == "shift":
                grid_entry_slice = tuple([0] + list(grid_entry_slice)[1:])
                self_grid_entry_slice = self.grid.get_slice(grid_entry[1:])
            elif grid_entry_op == "pop":
                grid_entry_slice = tuple(list(grid_entry_slice)[:-1] + [0])
                self_grid_entry_slice = self.grid.get_slice(grid_entry[:-1])
            elif grid_entry_op == "prep":
                self_grid_entry_slice = self.grid.get_slice(tuple([0] + list(grid_entry)))
            elif grid_entry_op == "prep":
                self_grid_entry_slice = self.grid.get_slice(tuple(list(grid_entry) + [0]))
            else:
                assert grid_entry_op == "none"
                self_grid_entry_slice = grid_entry_slice

            # TODO (hme): This is costly.
            rarr[grid_entry_slice] = self[self_grid_entry_slice]
        return rarr