lazyarray.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:lazyarray 作者: NeuralEnsemble 项目源码 文件源码
def _array_indices(self, addr):
        self.check_bounds(addr)

        def axis_indices(x, max):
            if isinstance(x, (int, long, numpy.integer)):
                return x
            elif isinstance(x, slice):  # need to handle negative values in slice
                return numpy.arange((x.start or 0),
                                    (x.stop or max),
                                    (x.step or 1),
                                    dtype=int)
            elif isinstance(x, collections.Sized):
                if hasattr(x, 'dtype') and x.dtype == bool:
                    return numpy.arange(max)[x]
                else:
                    return numpy.array(x)
            else:
                raise TypeError("Unsupported index type %s" % type(x))
        addr = self._full_address(addr)
        if isinstance(addr, numpy.ndarray) and addr.dtype == bool:
            if addr.ndim == 1:
                return (numpy.arange(self._shape[0])[addr],)
            else:
                raise NotImplementedError()
        elif all(isinstance(x, collections.Sized) for x in addr):
            indices = [numpy.array(x) for x in addr]
            return indices
        else:
            indices = [axis_indices(x, max) for (x, max) in zip(addr, self._shape)]
            if len(indices) == 1:
                return indices
            elif len(indices) == 2:
                if isinstance(indices[0], collections.Sized):
                    if isinstance(indices[1], collections.Sized):
                        mesh_xy = numpy.meshgrid(*indices)
                        return (mesh_xy[0].T, mesh_xy[1].T)  # meshgrid works on (x,y), not (i,j)
                return indices
            else:
                raise NotImplementedError("Only 1D and 2D arrays supported")
评论列表


问题


面经


文章

微信
公众号

扫码关注公众号