Exemplo n.º 1
0
def paint_similarity_trace(b,
                           oks,
                           output=None,
                           figsize=(3, 3),
                           dpi=200,
                           **kwargs):
    clu_base = Clustering()
    fig, ax = plt.subplots(figsize=figsize, dpi=300)
    e_sim_list = []
    clu_base.from_membership_list(b)
    for g in oks.trace_mb.values():
        clu = Clustering()
        clu.from_membership_list(g[1])
        e_sim_list += [sim.element_sim(clu_base, clu)]

    ax.autoscale()
    ax.margins(0.1)
    # ax.set_aspect(1)
    plt.xlabel("steps")
    plt.ylabel("Element-centric similarity")
    plt.yticks(np.linspace(0, 1, 5))
    ax.tick_params(direction="in")
    plt.plot(e_sim_list)
    if output is not None:
        plt.savefig(output, dpi=dpi, transparent=True)
Exemplo n.º 2
0
def calc_nonoverlap_nmi(pred_membership, gt_membership):
    from clusim.clustering import Clustering
    import clusim.sim as sim

    pred = Clustering()
    pred.from_membership_list(pred_membership)

    gt = Clustering()
    gt.from_membership_list(gt_membership)

    ret = sim.nmi(pred, gt, norm_type='sum')
    return ret
Exemplo n.º 3
0
def paint_mds(oks, figsize=(20, 20)):
    l2 = len(oks.trace_mb.keys())
    l = int(l2**0.5)
    X = np.zeros([l2, l2])
    for idx_1, pair_1 in enumerate(combinations(range(1, l + 1), 2)):
        b = oks.trace_mb[pair_1]
        clu_1 = Clustering()
        clu_1.from_membership_list(b)
        for idx_2, pair_2 in enumerate(combinations(range(1, l + 1), 2)):
            b = oks.trace_mb[pair_2]
            clu_2 = Clustering()
            clu_2.from_membership_list(b)

            X[idx_1][idx_2] = 1 - sim.element_sim(clu_1, clu_2)
            X[idx_2][idx_1] = X[idx_1][idx_2]

    def _plot_embedding(X, title=None):
        x_min, x_max = np.min(X, 0), np.max(X, 0)
        X = (X - x_min) / (x_max - x_min)

        plt.figure(figsize=figsize)
        for ind, i in enumerate(range(X.shape[0])):
            plt.text(X[i, 0],
                     X[i, 1],
                     str(list(oks.trace_mb.keys())[ind]),
                     color=plt.cm.Set1(1 / 10.),
                     fontdict={
                         'weight': 'bold',
                         'size': 12
                     })
        plt.xticks([]), plt.yticks([])
        if title is not None:
            plt.title(title)

    clf = manifold.MDS(n_components=2,
                       n_init=10,
                       max_iter=10000,
                       dissimilarity="precomputed")
    X_mds = clf.fit_transform(X)
    _plot_embedding(X_mds)
Exemplo n.º 4
0
def generate_random_partition_all(n_elements, tol=1.0e-15):
    """
        This function creates a random clustering according to the 'All'
        random model by uniformly selecting a clustering from the ensemble of all
        clusterings with n_elements.

        :param int n_elements:
            The number of elements

        :param float tol: (optional)
            The tolerance used by the algorithm to approximate the probability distrubtion

        :returns: The randomly genderated clustering.

        >>> import clusim.clugen as clugen
        >>> from clusim.clustering import print_clustering
        >>> clu = clugen.generate_random_partition_all(n_elements = 9)
        >>> print_clustering(clu)
    """

    if (n_elements, tol) in all_partition_weight_dict:
        weights = all_partition_weight_dict[(n_elements, tol)]
    else:
        weights = []
        u = 1
        b = mpmath.bell(n_elements)
        while sum(weights) < 1.0 - tol:
            weights.append(mpmath.power(u, n_elements)/(b * mpmath.e * mpmath.factorial(u)))
            u += 1
        all_partition_weight_dict[(n_elements, tol)] = weights

    K = np.random.choice(np.arange(1, len(weights) + 1), p=weights)
    colors = np.random.randint(K, size=n_elements)

    new_clustering = Clustering()
    new_clustering.from_membership_list(colors)
    return new_clustering