bandhic.band_hic_matrix.__getitem__#
- band_hic_matrix.__getitem__(index)[source]#
Retrieve matrix entries or submatrix using NumPy-like indexing.
Supports:
Integer indexing: mat[i, j] returns a single value.
Slice indexing: mat[i:j, i:j] returns a band_hic_matrix for square slices.
Single-axis slice: mat[i:j] returns a band_hic_matrix same as mat[i:j, i:j].
Fancy (array) indexing: mat[[i1, i2], [j1, j2]] returns an ndarray or MaskedArray according to mask.
Mixed indexing: combinations of integer, slice, and array-like indices.
Boolean indexing: ‘band_hic_matrix’ object with dtype bool can be used to index entries.
When both row and column indices specify the same slice (or a single slice is provided), a new band_hic_matrix representing that square submatrix is returned. New submatrix is the view of the original matrix, sharing the same data and mask. If the mask or data is altered in the submatrix, the original matrix will reflect those changes as well.
If a single integer index is provided for both row and column, a scalar value is returned.
If a mask is set, masked entries will return as numpy.ma.masked for scalars, or as a numpy.ma.MaskedArray for arrays.
If a mask is not set, the scalar value is returned directly, or a numpy.ndarray for arrays.
If a square slice is provided, a new band_hic_matrix is returned with the same diagonal number and shape as the original matrix.
If a single slice is provided, it returns a band_hic_matrix with the same diagonal number and shape as the original matrix.
If fancy indexing is used, it returns a numpy.ndarray or numpy.ma.MaskedArray depending on whether the mask is set.
In all other cases, a numpy.ndarray (if no mask) or numpy.ma.MaskedArray (if mask present) is returned.
- Parameters:
index (int, slice, band_hic_matrix, array-like of int, or tuple of these) –
index expression for rows and columns. May be:
A pair (row_idx, col_idx) of ints, slices, or array-like for mixed indexing.
A single slice selecting a square region.
A band_hic_matrix object with dtype of bool for boolean indexing.
- Returns:
scalar : when both row and column are integer indices.
numpy.ndarray : for fancy or mixed indexing without mask.
numpy.ma.MaskedArray : for fancy or mixed indexing when mask is set.
band_hic_matrix : for square slice results.
- Return type:
scalar or ndarray or MaskedArray or band_hic_matrix
- Raises:
ValueError – If a slice step is not 1, or if indices are out of bounds.
Examples
>>> import numpy as np >>> import bandhic as bh >>> mat = bh.band_hic_matrix(np.arange(16).reshape(4,4), diag_num=2)
# Single-element access (scalar)
>>> mat[1, 2] 6
# Masked element returns masked
>>> mat2 = bh.band_hic_matrix(np.eye(4), dtype=int, diag_num=2, mask=([0],[1])) >>> mat2[0, 1] masked
# Square submatrix via two-slice indexing returns band_hic_matrix
>>> sub = mat[1:3, 1:3] >>> isinstance(sub, bh.band_hic_matrix) True
# Single-axis slice returns band_hic_matrix for square region
>>> sub2 = mat[0:2] # equivalent to mat[0:2, 0:2] >>> isinstance(sub2, bh.band_hic_matrix) True
# Fancy indexing returns ndarray or MaskedArray
>>> arr = mat[[0,2,3], [1,2,0]] >>> isinstance(arr, np.ndarray) True
>>> mat.add_mask([0,1],[1,2]) # Add mask to some entries >>> masked_arr = mat[[0,1], [1,2]] >>> isinstance(masked_arr, np.ma.MaskedArray) True
# Boolean indexing with band_hic_matrix
>>> mat3 = bh.band_hic_matrix(np.eye(4), diag_num=2, mask=([0,1],[1,2])) >>> bool_mask = mat3 > 0 # Create a boolean mask >>> result = mat3[bool_mask] # Use boolean mask for indexing >>> isinstance(result, np.ma.MaskedArray) True
>>> result masked_array(data=[1.0, 1.0, 1.0, 1.0], mask=[False, False, False, False], fill_value=0.0)