def _create(cls, n_rows, n_cols, data, row_major, block_size): """Private method for creating small test matrices.""" bdm = Env.hail().utils.richUtils.RichDenseMatrixDouble.apply( n_rows, n_cols, jarray(Env.jvm().double, data), row_major) return cls(Env.hail().linalg.BlockMatrix.fromBreezeMatrix( Env.hc()._jsc, bdm, block_size))
def filter(self, rows_to_keep, cols_to_keep): """Filters matrix rows and columns. Notes ----- This method has the same effect as :meth:`BlockMatrix.filter_cols` followed by :meth:`BlockMatrix.filter_rows` (or vice versa), but filters the block matrix in a single pass which may be more efficient. Parameters ---------- rows_to_keep: :obj:`list` of :obj:`int` Indices of rows to keep. Must be non-empty and increasing. cols_to_keep: :obj:`list` of :obj:`int` Indices of columns to keep. Must be non-empty and increasing. Returns ------- :class:`.BlockMatrix` """ return BlockMatrix(self._jbm.filter(jarray(Env.jvm().long, rows_to_keep), jarray(Env.jvm().long, cols_to_keep)))
def filter_cols(self, cols_to_keep): """Filters matrix columns. Parameters ---------- cols_to_keep: :obj:`list` of :obj:`int` Indices of columns to keep. Must be non-empty and increasing. Returns ------- :class:`.BlockMatrix` """ return BlockMatrix(self._jbm.filterCols(jarray(Env.jvm().long, cols_to_keep)))
def filter_rows(self, rows_to_keep): """Filters matrix rows. Parameters ---------- rows_to_keep: :obj:`list` of :obj:`int` Indices of rows to keep. Must be non-empty and increasing. Returns ------- :class:`.BlockMatrix` """ return BlockMatrix(self._jbm.filterRows(jarray(Env.jvm().long, rows_to_keep)))
def sparsify_row_intervals(self, starts, stops, blocks_only=False): """Creates a sparse block matrix by filtering to an interval for each row. Examples -------- Consider the following block matrix: >>> import numpy as np >>> nd = np.array([[ 1.0, 2.0, 3.0, 4.0], ... [ 5.0, 6.0, 7.0, 8.0], ... [ 9.0, 10.0, 11.0, 12.0], ... [13.0, 14.0, 15.0, 16.0]]) >>> bm = BlockMatrix.from_numpy(nd, block_size=2) Set all elements outside the given row intervals to zero and collect to NumPy: >>> (bm.sparsify_row_intervals(starts=[1, 0, 2, 2], ... stops= [2, 0, 3, 4]) ... .to_numpy()) .. code-block:: text array([[ 0., 2., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 11., 0.], [ 0., 0., 15., 16.]]) Set all blocks fully outside the given row intervals to blocks of zeros and collect to NumPy: >>> (bm.sparsify_row_intervals(starts=[1, 0, 2, 2], ... stops= [2, 0, 3, 4], ... blocks_only=True) ... .to_numpy()) .. code-block:: text array([[ 1., 2., 0., 0.], [ 5., 6., 0., 0.], [ 0., 0., 11., 12.], [ 0., 0., 15., 16.]]) Notes ----- This methods creates a sparse block matrix by zeroing out all blocks which are disjoint from all row intervals. By default, all elements outside the row intervals but inside blocks that overlap the row intervals are set to zero as well. Sparse block matrices do not store the zeroed blocks explicitly; rather zeroed blocks are dropped both in memory and when stored on disk. This enables computations, such as banded correlation for a huge number of variables, that would otherwise be prohibitively expensive, as dropped blocks are never computed in the first place. Matrix multiplication by a sparse block matrix is also accelerated in proportion to the block sparsity. Matrix product ``@`` currently always results in a dense block matrix. Sparse block matrices also support transpose, :meth:`diagonal`, and all non-mathematical operations except filtering. Element-wise mathematical operations are currently supported if and only if they cannot transform zeroed blocks to non-zero blocks. For example, all forms of element-wise multiplication ``*`` are supported, and element-wise multiplication results in a sparse block matrix with block support equal to the intersection of that of the operands. On the other hand, scalar addition is not supported, and matrix addition is supported only between block matrices with the same block sparsity. This method requires the number of rows to be less than :math:`2^{31}`. Parameters ---------- starts: :obj:`list` of :obj:`int` Start indices for each row (inclusive). stops: :obj:`list` of :obj:`int` Stop indices for each row (exclusive). blocks_only: :obj:`bool` If ``False``, set all elements outside row intervals to zero. If ``True``, only set all blocks outside row intervals to blocks of zeros; this is more efficient. Returns ------- :class:`.BlockMatrix` Sparse block matrix. """ return BlockMatrix(self._jbm.filterRowIntervals( jarray(Env.jvm().long, starts), jarray(Env.jvm().long, stops), blocks_only))
def filter(self, rows_to_keep, cols_to_keep): return BlockMatrix(self._jbm.filter(jarray(Env.jvm().long, rows_to_keep), jarray(Env.jvm().long, cols_to_keep)))
def filter_rows(self, rows_to_keep): return BlockMatrix(self._jbm.filterRows(jarray(Env.jvm().long, rows_to_keep)))
def filter_cols(self, cols_to_keep): return BlockMatrix(self._jbm.filterCols(jarray(Env.jvm().long, cols_to_keep)))