async def do_cluster():
    app_state.generator = ClusterGenerator(dsm_mat=app_state.dsm)
    c_orig = app_state.generator.cluster(app_state.dsm)
    total_coord_cost = app_state.generator.total_coord_cost
    cost_history = app_state.generator.cost_history
    c = ClusterMatrix.reorder(c_orig)

    new_dsm = DSMMatrix.reorder_by_cluster(app_state.dsm, c)

    d_new_g = DSMMatrix.place_diag(new_dsm)
    app_state.cluster_result = c
    app_state.dsm_result = d_new_g
    app_state.dsm_annotated = DSMMatrix.annotate_clusters(d_new_g, c)

    return {
        "dsm": app_state.dsm_result.tojson(),
        "labels": app_state.dsm_result.labels,
        "cluster": app_state.cluster_result.tojson(),
        "dsm_a": app_state.dsm_annotated.tojson(),
    }
Exemple #2
0
def graph_matrix(gen_matrix, cluster_matrix=None,
    x_title='Element', y_title='Element', graph_title='DSM Matrix',
    x_tcklabel=None, y_tcklabel=None, print_flag=0):
    """ Graph the DSM and cluster matrix.

    """
    assert isinstance(gen_matrix, DSMMatrix) or isinstance(gen_matrix, ClusterMatrix)
    # if not isinstance(cluster_matrix, ClusterMatrix)

    ds_mat = gen_matrix.mat

    (row_in, col_out) = np.where(ds_mat)
    m_value = ds_mat[ds_mat != 0]

    max_row = np.max(row_in)
    max_col = np.max(col_out)

    axis = np.array([0, max_col, max_row]);

    max_value = np.max(m_value)
    data_scale = np.ceil(500/max_value)/20

    if x_tcklabel is None and isinstance(gen_matrix, DSMMatrix):
        x_tcklabel = gen_matrix.labels
    elif isinstance(x_tcklabel, list):
        if len(x_tcklabel) != ds_mat.shape[0]:
            x_tcklabel = [str(x+1) for x in range(ds_mat.shape[0])]
    else:
        x_tcklabel = [str(x+1) for x in range(ds_mat.shape[0])]

    if y_tcklabel is None and isinstance(gen_matrix, DSMMatrix):
        y_tcklabel = gen_matrix.labels
    elif isinstance(y_tcklabel, list):
        if len(y_tcklabel) != ds_mat.shape[0]:
            y_tcklabel = [str(x+1) for x in range(ds_mat.shape[0])]
    else:
        y_tcklabel = [str(x+1) for x in range(ds_mat.shape[0])]

    if isinstance(cluster_matrix, ClusterMatrix):
        new_gen_matrix = DSMMatrix.annotate_clusters(gen_matrix, cluster_matrix)
        orig_ds_mat = gen_matrix.mat
        ds_mat = new_gen_matrix.mat

    fig, ax = plt.subplots()
    im = ax.imshow(ds_mat, cmap=plt.cm.Blues)

    ax.set_xticks(np.arange(len(x_tcklabel)))
    ax.set_yticks(np.arange(len(y_tcklabel)))

    ax.set_xticklabels(x_tcklabel)
    ax.set_yticklabels(y_tcklabel)
    ax.set_xlabel(x_title)
    ax.set_ylabel(y_title)
    ax.xaxis.set_label_position('top')

    ax.tick_params(top=True, bottom=False,
                   labeltop=True, labelbottom=False)

    # plt.setp(ax.get_xticklabels(), rotation=90, ha="right",
    #      rotation_mode="anchor")

    ax.set_title(graph_title)
    fig.tight_layout()
    plt.draw()