def get_on_diagonal_densities(block_ms, block_ns):
    """
    Calculates the densities within each block (densities of on diagonals of the
    block matrix)

    Parameters
    ----------
    block_ms: 2D array
        Matrix counting the number of edges that exist between pairs of blocks
    block_ns: 1D array
        Array counting the number of nodes in each block

    Returns
    -------
    ps: 1D array
        Array of the density within each block
    """
    ps = []
    for l in range(len(block_ns)):
        n = block_ns[l]
        m = block_ms[l,l]
        if n == 0 or n == 1:
            ps.append(0)
        else:
            ps.append(2 * m / (n * (n - 1)))
    return ps