bandhic.band_hic_matrix.__setitem__#

band_hic_matrix.__setitem__(index, values)[source]#

Assign values to matrix entries using NumPy-like indexing.

Parameters:
  • index (int, tuple of (row_idx, col_idx), slice, or band_hic_matrix) – Index expression for rows and columns. May be:

  • column. (- A single integer for both row and)

  • int (- A tuple of row and column indices (can be)

  • slice

  • array-like). (or)

  • region. (- A single slice selecting a square)

  • indexing. (- A band_hic_matrix object with dtype of bool for boolean)

  • values (scalar or array-like) – Values to assign. Can be a single scalar or an array-like object.

Raises:
  • ValueError – If index is a slice with step not equal to 1, or if indices exceed matrix dimensions.

  • TypeError – If values is not a scalar or array-like object.

  • Supports:

  • - Integer indexing – mat[i, j] = value assigns to a single element.:

  • - Slice indexing – mat[i:j, i:j] = array or scalar assigns to a square submatrix.:

  • - Single-axis slice – mat[i:j] = … is equivalent to mat[i:j, i:j].:

  • - Fancy (array) indexing – mat[[i1, i2], [j1, j2]] = array or scalar for scattered assignments.:

  • - Mixed indexing – combinations of integer, slice, and array-like indices.:

  • - Boolean indexing – boolean mask (another band_hic_matrix with dtype=bool) selects entries to set.:

Return type:

None

Examples

>>> import bandhic as bh
>>> import numpy as np
>>> mat = bh.band_hic_matrix(np.zeros((4,4)), diag_num=2, dtype=int)

# Single element assignment

>>> mat[1, 2] = 5
>>> mat[1, 2]
5

# Slice assignment to square submatrix

>>> mat[0:2, 0:2] = [[1, 2], [2, 4]]
>>> mat[0:2, 0:2].todense()
array([[1, 2],
       [2, 4]])

# Single-axis slice assignment (equivalent square slice)

>>> mat[2:4] = 0
>>> mat[2:4].todense()
array([[0, 0],
       [0, 0]])

# Fancy indexing for scattered assignments

>>> mat[[0, 3], [1, 2]] = [7, 8]
>>> mat[0, 1], mat[3, 2]
(7, 8)

# Boolean mask assignment

>>> mat2 = bh.band_hic_matrix(np.eye(4), diag_num=2, dtype=int)
>>> bool_mask = mat2 > 0
>>> mat2[bool_mask] = 9
>>> mat2.todense()
array([[9, 0, 0, 0],
      [0, 9, 0, 0],
      [0, 0, 9, 0],
      [0, 0, 0, 9]])

Notes

  • Assigning to masked entries updates underlying data but does not automatically unmask.

  • For multidimensional assignments, scalar values broadcast to all selected positions, while array values must match the number of targeted elements.

  • If a boolean mask is used, it must be a band_hic_matrix with dtype bool and the same shape as the original matrix.

  • If a single slice is provided, it behaves like mat[i:j, i:j] for square submatrices.