Exemplo n.º 1
0
    def __getitem__(self, key):
        def asindices(x):
            try:
                x = np.asarray(x, dtype=np.intc)
            except:
                raise IndexError('invalid index')
            else:
                return x
        def check_bounds(indices,N):
            max_indx = indices.max()
            if max_indx >= N:
                raise IndexError('index (%d) out of range' % max_indx)

            min_indx = indices.min()
            if min_indx < -N:
                raise IndexError('index (%d) out of range' % (N + min_indx))

            return (min_indx,max_indx)

        def extractor(indices,N):
            """Return a sparse matrix P so that P*self implements
            slicing of the form self[[1,2,3],:]
            """
            indices = asindices(indices)
    
            (min_indx,max_indx) = check_bounds(indices,N)

            if min_indx < 0:
                indices = indices.copy()
                indices[indices < 0] += N

            indptr  = np.arange(len(indices) + 1, dtype=np.intc)
            data    = np.ones(len(indices), dtype=self.dtype)
            shape   = (len(indices),N)

            return csr_matrix((data,indices,indptr), shape=shape)


        if isinstance(key, tuple):
            row = key[0]
            col = key[1]

            if isintlike(row):
                #[1,??]
                if isintlike(col):
                    return self._get_single_element(row, col) #[i,j]
                elif isinstance(col, slice):
                    return self._get_row_slice(row, col)      #[i,1:2]
                else:
                    P = extractor(col,self.shape[1]).T        #[i,[1,2]]
                    return self[row,:]*P

            elif isinstance(row, slice):
                #[1:2,??]
                if isintlike(col) or isinstance(col, slice):
                    return self._get_submatrix(row, col)      #[1:2,j]
                else:
                    P = extractor(col,self.shape[1]).T        #[1:2,[1,2]]
                    return self[row,:]*P

            else:
                #[[1,2],??] or [[[1],[2]],??]
                if isintlike(col) or isinstance(col,slice):
                    P = extractor(row, self.shape[0])        #[[1,2],j] or [[1,2],1:2]
                    return (P*self)[:,col]

                else:
                    row = asindices(row)
                    col = asindices(col)
                    if len(row.shape) == 1:
                        if len(row) != len(col):             #[[1,2],[1,2]]
                            raise IndexError('number of row and column indices differ')

                        check_bounds(row, self.shape[0])
                        check_bounds(col, self.shape[1])

                        num_samples = len(row)
                        val = np.empty(num_samples, dtype=self.dtype)
                        csr_sample_values(self.shape[0], self.shape[1],
                                          self.indptr, self.indices, self.data,
                                          num_samples, row, col, val)
                        #val = []
                        #for i,j in zip(row,col):
                        #    val.append(self._get_single_element(i,j))
                        return np.asmatrix(val)

                    elif len(row.shape) == 2:
                        row = np.ravel(row)                   #[[[1],[2]],[1,2]]
                        P = extractor(row, self.shape[0])
                        return (P*self)[:,col]

                    else:
                        raise NotImplementedError('unsupported indexing')

        elif isintlike(key) or isinstance(key,slice):
            return self[key,:]                                #[i] or [1:2]
        else:
            return self[asindices(key),:]                     #[[1,2]]
Exemplo n.º 2
0
    def __getitem__(self, key):
        def asindices(x):
            try:
                x = np.asarray(x, dtype=np.intc)
            except:
                raise IndexError('invalid index')
            else:
                return x

        def check_bounds(indices, N):
            max_indx = indices.max()
            if max_indx >= N:
                raise IndexError('index (%d) out of range' % max_indx)

            min_indx = indices.min()
            if min_indx < -N:
                raise IndexError('index (%d) out of range' % (N + min_indx))

            return (min_indx, max_indx)

        def extractor(indices, N):
            """Return a sparse matrix P so that P*self implements
            slicing of the form self[[1,2,3],:]
            """
            indices = asindices(indices)

            (min_indx, max_indx) = check_bounds(indices, N)

            if min_indx < 0:
                indices = indices.copy()
                indices[indices < 0] += N

            indptr = np.arange(len(indices) + 1, dtype=np.intc)
            data = np.ones(len(indices), dtype=self.dtype)
            shape = (len(indices), N)

            return csr_matrix((data, indices, indptr), shape=shape)

        if isinstance(key, tuple):
            row = key[0]
            col = key[1]

            if isintlike(row):
                #[1,??]
                if isintlike(col):
                    return self._get_single_element(row, col)  #[i,j]
                elif isinstance(col, slice):
                    return self._get_row_slice(row, col)  #[i,1:2]
                else:
                    P = extractor(col, self.shape[1]).T  #[i,[1,2]]
                    return self[row, :] * P

            elif isinstance(row, slice):
                #[1:2,??]
                if isintlike(col) or isinstance(col, slice):
                    return self._get_submatrix(row, col)  #[1:2,j]
                else:
                    P = extractor(col, self.shape[1]).T  #[1:2,[1,2]]
                    return self[row, :] * P

            else:
                #[[1,2],??] or [[[1],[2]],??]
                if isintlike(col) or isinstance(col, slice):
                    P = extractor(row,
                                  self.shape[0])  #[[1,2],j] or [[1,2],1:2]
                    return (P * self)[:, col]

                else:
                    row = asindices(row)
                    col = asindices(col)
                    if len(row.shape) == 1:
                        if len(row) != len(col):  #[[1,2],[1,2]]
                            raise IndexError(
                                'number of row and column indices differ')

                        check_bounds(row, self.shape[0])
                        check_bounds(col, self.shape[1])

                        num_samples = len(row)
                        val = np.empty(num_samples, dtype=self.dtype)
                        csr_sample_values(self.shape[0], self.shape[1],
                                          self.indptr, self.indices, self.data,
                                          num_samples, row, col, val)
                        #val = []
                        #for i,j in zip(row,col):
                        #    val.append(self._get_single_element(i,j))
                        return np.asmatrix(val)

                    elif len(row.shape) == 2:
                        row = np.ravel(row)  #[[[1],[2]],[1,2]]
                        P = extractor(row, self.shape[0])
                        return (P * self)[:, col]

                    else:
                        raise NotImplementedError('unsupported indexing')

        elif isintlike(key) or isinstance(key, slice):
            return self[key, :]  #[i] or [1:2]
        else:
            return self[asindices(key), :]  #[[1,2]]