Skip to content

filter

Functions:

threshold_connected

threshold_connected(
    K: SimsMat, bin: bool = True
) -> SimsMat

filter edge-weights until the graph is (barely) connected.

Uses binary search to efficiently find maximum threshold, and cardinality of a breadth-first search to check for connectivity (must be at least same edges as a spanning tree)

Parameters:

  • K

    (SimsMat) –

    square matrix (e.g. kernel) to be thresholded

  • bin

    (bool, default: True ) –

    whether to binarize the output, or cut-off only.

Source code in src/affinis/filter.py
def threshold_connected(K: SimsMat, bin: bool = True) -> SimsMat:
    """filter edge-weights until the graph is (barely) connected.

    Uses binary search to efficiently find maximum threshold,
    and cardinality of a breadth-first search to check for
    connectivity (must be at least same edges as a spanning tree)

    Args:
      K: square matrix (e.g. kernel) to be thresholded
      bin: whether to binarize the output, or cut-off only.

    """
    # edges = sq2flat(K)
    # edges_masked = min_connected_filter(edges)
    n = K.shape[0]
    candidates = np.sort(sq2flat(K).data)
    # print(len(candidates))
    def connected(thres):

        bfs = sp.csgraph.breadth_first_tree(threshold_value(K,thres),0,directed=False)
        return bfs.nnz ==n-1

    if candidates.shape[0]<1:
        warnings.warn("no candidate threshold values, default t=0.")
        min_thres=0.
    else: 
        min_idx, min_thres = _binary_search_greatest(
            candidates,
            connected,
            0,
            # len(candidates) - n + 1,  # need at least n-1 edges for connected
            candidates.shape[0]-1,
        )
    return threshold_value(K, min_thres, bin=bin)

threshold_value

threshold_value(
    K: SimsMat, t: float, bin: bool = True
) -> SimsMat

Filter edge-weights below a given value.

Parameters:

  • K

    (SimsMat) –

    square matrix (e.g. kernel) to be thresholded

  • t

    (float) –

    cutoff-value

  • bin

    (bool, default: True ) –

    whether to binarize the output, or cut-off only.

Source code in src/affinis/filter.py
def threshold_value(K: SimsMat, t: float, bin: bool = True) -> SimsMat:
    """Filter edge-weights below a given value.

    Args:
      K: square matrix (e.g. kernel) to be thresholded
      t: cutoff-value
      bin: whether to binarize the output, or cut-off only.
    """
    # edges = sq2flat(K)

    Kthres = K>=t
    if bin:
        # return flat2sq((~edges_masked.mask).astype(int))
        return Kthres
    else:
        # return flat2sq(edges_masked.filled(0))
        return K*Kthres