示例#1
0
    def invert_coord(self, coord):
        validate_match(coord, self.shape)

        if len(coord) < self._expand:
            return coord
        else:
            coord = list(coord)
            del coord[self._expand]
            return tuple(coord)
示例#2
0
    def map_coord(self, source_coord):
        validate_match(source_coord, self._source_shape)

        if len(source_coord) < self._expand:
            return source_coord
        else:
            coord = list(source_coord)
            coord.insert(self._expand, 0)
            return tuple(coord)
示例#3
0
    def __setitem__(self, match, value):
        match = validate_match(match, self.shape)

        if isinstance(value, SparseTensor):
            dest = self[match]
            if dest.shape != value.shape:
                value = value.broadcast(dest.shape)

            for coord, val in value.filled():
                dest[coord] = val

        elif value == self.dtype(0):
            self._delete_filled(match)
        else:
            affected_range = affected(match, self.shape)

            if isinstance(value, Tensor):
                if value.shape != self[match].shape:
                    value = value.broadcast(self[match].shape)

                value = iter(value)
                for coord in itertools.product(*affected_range):
                    self.table.upsert(coord, (next(value), ))
            else:
                value = self.dtype(value)
                for coord in itertools.product(*affected_range):
                    self.table.upsert(coord, (value, ))
示例#4
0
    def __init__(self, source_shape, match):
        match = validate_match(match, source_shape)

        shape = []
        offset = {}
        elided = []
        inverted_axes = []

        for axis in range(len(match)):
            if match[axis] is None:
                shape.append(source_shape[axis])
                inverted_axes.append(axis)
                offset[axis] = 0
            elif isinstance(match[axis], slice):
                s = match[axis]
                shape.append(math.ceil((s.stop - s.start) / s.step))
                inverted_axes.append(axis)
                offset[axis] = s.start
            elif isinstance(match[axis], tuple):
                shape.append(len(match[axis]))
                inverted_axes.append(axis)
            else:
                elided.append(axis)

        for axis in range(len(match), len(source_shape)):
            shape.append(source_shape[axis])
            inverted_axes.append(axis)
            offset[axis] = 0

        self.match = match
        self.shape = tuple(shape)
        self._inverted_axes = inverted_axes
        self._source_shape = source_shape
        self._elided = elided
        self._offset = offset
示例#5
0
 def __getitem__(self, match):
     match = validate_match(match, self.shape)
     if len(match) == self.ndim and all(isinstance(c, int) for c in match):
         return self.accessor[match]
     else:
         slice_accessor = self.accessor[match]
         return SparseTensor(slice_accessor.shape, self.dtype,
                             slice_accessor)
示例#6
0
文件: dense.py 项目: haydnv/tensor.py
    def __getitem__(self, match):
        match = validate_match(match, self.shape)

        if len(match) == self.ndim and all(isinstance(c, int) for c in match):
            index = sum(np.array(match) * self._coord_index)
            return self._blocks[index // PER_BLOCK][index % PER_BLOCK]
        else:
            return BlockListSlice(self, match)
示例#7
0
 def __getitem__(self, match):
     match = validate_match(match, self.shape)
     if len(match) == 2 and all(isinstance(c, int) for c in match):
         if match[0] == match[1]:
             return self.dtype(1)
         else:
             return self.dtype(0)
     else:
         return SparseSlice(self, match)
示例#8
0
    def __getitem__(self, match):
        match = validate_match(match, self.shape)
        if len(match) == self.ndim and all(isinstance(c, int) for c in match):
            selector = dict(zip(range(len(match)), match))
            for (value, ) in self.table.slice(selector).select(["value"]):
                return value

            return self.dtype(0)
        else:
            return SparseSlice(self, match)
示例#9
0
文件: dense.py 项目: haydnv/tensor.py
    def __getitem__(self, match):
        if not isinstance(match, tuple):
            match = (match, )

        if len(match) == self.ndim and all(isinstance(c, int) for c in match):
            match = validate_match(match, self.shape)
            return self._block_list[match]
        else:
            block_list = self._block_list[match]
            return DenseTensor(block_list.shape, self.dtype, block_list)
示例#10
0
    def filled(self, match=None):
        one = self.dtype(1)

        if match is None:
            for i in range(self.shape[0]):
                yield (i, i), one
            return

        match = validate_match(match, self.shape)
        for coord in itertools.product(*affected(match, self.shape)):
            assert len(coord) == 2
            if coord[0] == coord[1]:
                yield coord, one
示例#11
0
文件: dense.py 项目: haydnv/tensor.py
    def __setitem__(self, match, value):
        match = validate_match(match, self.shape)

        if len(match) == self.ndim and all(isinstance(c, int) for c in match):
            index = sum(np.array(match) * self._coord_index)
            self._blocks[index // PER_BLOCK][index %
                                             PER_BLOCK] = self.dtype(value)
        else:
            update_shape = shape_of(self.shape, match)

            if isinstance(value, Tensor):
                if not isinstance(value, DenseTensor):
                    value = DenseTensor.from_sparse(value)

                if value.shape != update_shape:
                    value = value.broadcast(update_shape)
                value = list(iter(value))
            else:
                value = list(iter(value for _ in range(product(update_shape))))

            assert len(value) == product(update_shape)

            affected_coords = itertools.product(*affected(match, self.shape))
            for coords in chunk_iter(affected_coords, PER_BLOCK):
                indices = np.sum(coords * self._coord_index, 1)
                block_ids = indices // PER_BLOCK
                offsets = indices % PER_BLOCK
                for block_id in np.unique(block_ids):
                    num_values = np.sum(block_ids == block_id)
                    block_offsets = offsets[:num_values]
                    block_values = value[:num_values]
                    self._blocks[block_id][block_offsets] = np.array(
                        block_values)

                    offsets = offsets[num_values:]
                    value = value[num_values:]
示例#12
0
 def __setitem__(self, match, value):
     match = validate_match(match, self.shape)
     match = self._rebase.invert_coord(match)
     self._source[match] = value
示例#13
0
文件: dense.py 项目: haydnv/tensor.py
 def __getitem__(self, match):
     match = validate_match(match, self.shape)
     if len(match) == self.ndim and all(isinstance(x, int) for x in match):
         return self._source[match]
     else:
         return BlockListSparse(self._source[match])
示例#14
0
文件: dense.py 项目: haydnv/tensor.py
 def __setitem__(self, match, value):
     match = validate_match(match, self.shape)
     self._block_list[match] = value
示例#15
0
文件: dense.py 项目: haydnv/tensor.py
 def __getitem__(self, match):
     match = validate_match(match, self.shape)
     return self._source[self._rebase.invert_coord(match)]
示例#16
0
 def __setitem__(self, match, value):
     match = validate_match(match, self.shape)
     self.accessor[match] = value
示例#17
0
 def __init__(self, source, match):
     self._match = validate_match(match, source.shape)
     rebase = transform.Slice(source.shape, self._match)
     SparseRebase.__init__(self, rebase, source)