def draw_mlp_clustering_report(weight_directory,
                               double_clustering_results,
                               n_cluster=4,
                               title=None,
                               figsize=(20, 30)):

    weight_paths = get_weights_paths(weight_directory)

    fig, axes = plt.subplots(2, 2, figsize=figsize)

    if title is not None:
        fig.suptitle(title)

    axes[0][0].set_title('Unpruned')
    draw_clustered_mlp(
        weight_paths[True],  # True represents **un**pruned
        double_clustering_results[True],
        n_clusters=n_cluster,
        ax=axes[0][0])

    draw_cluster_by_layer(weight_paths[True],
                          double_clustering_results[True],
                          n_clusters=n_cluster,
                          ax=axes[1][0])

    axes[0][1].set_title('Pruned')
    draw_clustered_mlp(weight_paths[False],
                       double_clustering_results[False],
                       n_clusters=n_cluster,
                       ax=axes[0][1])

    draw_cluster_by_layer(weight_paths[False],
                          double_clustering_results[False],
                          n_clusters=n_cluster,
                          ax=axes[1][1])
def build_weighted_dist_mat(model_path,
                            clustering_result,
                            normalize_in_out=False):

    weight_path = get_weights_paths(model_path)[False]  # pruned

    G = build_cluster_graph(
        weight_path,
        clustering_result[False],  # pruned
        normalize_in_out=normalize_in_out)

    df = pd.DataFrame([{
        'start': start,
        'end': end,
        'dist': _compute_weighted_dist(G, start, end)
    } for start, end in it.combinations(G.nodes, 2)])

    df = df[df != 0].dropna()

    # The distance is normalized to [0, 1] inside the paths between two specific layers
    # The max weighted sitance is one.
    df['layers'] = df.apply(
        lambda r: r['start'].split('-')[0] + '-' + r['end'].split('-')[0],
        axis=1)
    df['normalized_dist'] = df['dist'] / df.groupby(
        'layers')['dist'].transform('max')

    mat = df.pivot('start', 'end', 'normalized_dist')

    return mat
def plot_eigenvalue_report(weight_directory,
                           unpruned_n_eigenvalues=None,
                           pruned_n_eigenvalues=None,
                           figsize=(10, 5)):
    weight_paths = get_weights_paths(weight_directory)

    is_slice = (unpruned_n_eigenvalues is not None
                or pruned_n_eigenvalues is not None)

    n_rows = 2 if is_slice else 1

    _, axes = plt.subplots(n_rows, 2, squeeze=False, figsize=figsize)

    axes[0][0].set_title('Unpruned')
    plot_eigenvalues(weight_paths[True], ax=axes[0][0])

    if is_slice:
        plot_eigenvalues(weight_paths[True],
                         unpruned_n_eigenvalues,
                         ax=axes[1][0])

    axes[0][1].set_title('Pruned')
    plot_eigenvalues(weight_paths[False], ax=axes[0][1])

    if is_slice:
        plot_eigenvalues(weight_paths[False],
                         pruned_n_eigenvalues,
                         ax=axes[1][1])
def run_double_spectral_cluster(
    weight_directory,
    with_shuffle=True,
    n_clusters=4,
    shuffle_method='layer',
    n_samples=None,
    n_workers=None,
    with_shuffled_ncuts=False,
    random_state=RANDOM_STATE,
):

    weight_paths = get_weights_paths(weight_directory)

    return {
        is_unpruned:
        run_spectral_cluster(weight_path, with_shuffle, n_clusters,
                             shuffle_method, n_samples, n_workers,
                             with_shuffled_ncuts, random_state)
        for is_unpruned, weight_path in weight_paths.items()
    }