Skip to content

rms_permute

rms_permute(confusion_matrix, labels)

Perform reverse merge/split (RMS) permutation analysis on a confusion matrix.

Analyzes and permutes a confusion matrix to identify merge and split relationships between predicted and ground truth labels.

Parameters:

Name Type Description Default
confusion_matrix ndarray

The confusion matrix to analyze, shape (n_classes, n_classes)

required
labels ndarray

The labels corresponding to the confusion matrix rows/columns, shape (n_classes,)

required

Returns:

Type Description
RMSResult

Named tuple with fields: - permuted_matrix: reordered confusion matrix - permuted_labels: reordered labels - rms_label_map: dict of merge/split relationships - rms_map_matrix: Nx4 matrix of relationships - rms_map_type: array of 'rmerge' or 'rsplit'

Examples:

>>> import numpy as np
>>> conf_mat = np.array([[2, 1, 0], [0, 3, 1], [1, 0, 2]])
>>> labels = np.array([1, 2, 3])
>>> result = rms_permute(conf_mat, labels)
>>> result.permuted_matrix  # or unpack as tuple
Source code in src/mheatmap/_rms_permute.py
def rms_permute(confusion_matrix: np.ndarray, labels: np.ndarray) -> RMSResult:
    """`rms_permute(confusion_matrix, labels)`

    Perform reverse merge/split (RMS) permutation analysis
    on a confusion matrix.

    Analyzes and permutes a confusion matrix to identify merge
    and split relationships between predicted and ground truth
    labels.

    Parameters
    ----------
    confusion_matrix : numpy.ndarray
        The confusion matrix to analyze, shape (n_classes, n_classes)
    labels : numpy.ndarray
        The labels corresponding to the confusion matrix
        rows/columns, shape (n_classes,)

    Returns
    -------
    RMSResult
        Named tuple with fields:
        - permuted_matrix: reordered confusion matrix
        - permuted_labels: reordered labels
        - rms_label_map: dict of merge/split relationships
        - rms_map_matrix: Nx4 matrix of relationships
        - rms_map_type: array of 'rmerge' or 'rsplit'

    Examples
    --------
    >>> import numpy as np
    >>> conf_mat = np.array([[2, 1, 0], [0, 3, 1], [1, 0, 2]])
    >>> labels = np.array([1, 2, 3])
    >>> result = rms_permute(conf_mat, labels)
    >>> result.permuted_matrix  # or unpack as tuple
    """
    rms_map = _make_rms_map(confusion_matrix)
    n = len(confusion_matrix)
    P = _make_rms_p(rms_map, n)
    rms_label_map = _make_rms_label_map(labels, rms_map)

    permuted_matrix = _permute_matrix(confusion_matrix, P)
    permuted_labels = _permute_labels(labels, P)
    rms_map_matrix, rms_map_type = _compute_rms_map_matrix(rms_label_map)

    return RMSResult(
        permuted_matrix,
        permuted_labels,
        rms_label_map,
        rms_map_matrix,
        rms_map_type,
    )