Exemple #1
0
def plot_latent_sweep(latent, n_pairs):
    for d in range(latent.shape[1]):
        dim = latent[:, d]
        fig, ax = plt.subplots(1, 1, figsize=(10, 10))
        data_df = pd.DataFrame()
        data_df["Label"] = n_pairs * ["Left"] + n_pairs * ["Right"]
        data_df["Latent"] = dim[:2 * n_pairs]
        data_df["Index"] = list(range(n_pairs)) + list(range(n_pairs))
        ax = sns.scatterplot(data=data_df,
                             x="Index",
                             y="Latent",
                             hue="Label",
                             ax=ax,
                             s=15)
        add_connections(
            range(n_pairs),
            range(n_pairs),
            dim[:n_pairs],
            dim[n_pairs:2 * n_pairs],
            ax=ax,
            color="grey",
        )
        remove_spines(ax)
        ax.xaxis.set_major_locator(plt.FixedLocator([0]))
        ax.yaxis.set_major_locator(plt.FixedLocator([0]))
        ax.set_title(f"Dimension {d}")
Exemple #2
0
def plot_connectors(data, Z, x, y, ax, mins, maxs):
    sns.scatterplot(
        data=data,
        y=plot_vars[y],
        x=plot_vars[x],
        s=3,
        alpha=0.05,
        ax=ax,
        linewidth=0,
        color="black",
    )
    unused = np.setdiff1d([0, 1, 2], [x, y])[0]
    projection = Z.sum(axis=unused)
    if x > y:
        projection = projection.T
    ax.imshow(
        np.rot90(projection),  # integrate out the unused dim
        cmap=plt.cm.Reds,
        extent=[mins[x], maxs[x], mins[y], maxs[y]],
        vmin=0,
    )
    ax.set_xlim([mins[x], maxs[x]])
    ax.set_ylim([maxs[y], mins[y]])
    ax.set_xticks([])
    ax.set_yticks([])
    ax.set_xlabel("")
    ax.set_ylabel("")
    remove_spines(ax)
Exemple #3
0
        prob_mat[:, t] = np.squeeze(new_state_vec)
        state_vec = new_state_vec
    sensory_mats.append(prob_mat)

# %% [markdown]
# #
from src.visualization import remove_spines

plot_df = pd.DataFrame(data=sensory_mats[0], columns=list(range(n_timesteps)))
# plot_df["Merge Class"] = mg.meta["Merge Class"]
# plot_df["ind"] = range(len(plot_df))
i = 21
fig, ax = plt.subplots(n_timesteps, 1, figsize=(5, 10))
for t in range(n_timesteps):
    ax[t].plot(sensory_mats[i][:, t])
    remove_spines(ax[t])
plt.suptitle(mg.meta.iloc[sensory_inds[i], 0] + " " +
             mg.meta.iloc[sensory_inds[i], 10])

# %% [markdown]
# #
from sklearn.decomposition import PCA

pca = PCA(n_components=2)
X = np.array(sensory_mats)
X = X.reshape((len(sensory_inds), -1))
pcs = pca.fit_transform(X)
df = pd.DataFrame(data=pcs)
df["class"] = mg.meta.loc[is_sensory, "Merge Class"]
sns.scatterplot(data=df, x=0, y=1, hue="class")
Exemple #4
0
        height = max_y - min_y
        x_label = bound_df.index[x]
        y_label = bound_df.index[y]

        edge = blockmodel_edges[
            (blockmodel_edges["source"] == y_label)
            & (blockmodel_edges["target"] == x_label)
        ]
        rect = patches.Rectangle((min_x, min_y), width, height)
        main_ax.add_patch(rect)


main_ax.set_xlabel("")
main_ax.set_ylabel("")

remove_spines(main_ax)
divider = make_axes_locatable(main_ax)


meta = full_meta.copy()

left_ax = divider.append_axes("left", size="20%", pad=0, sharey=main_ax)
ax = left_ax
# ax.set_ylim((-gap, (2 * n_pairs + gap * n_leaf)))
ax.set_xlim((-0.5, lowest_level + 0.5))
draw_bar_dendrogram(meta, ax)
ax.set_yticks([])
ax.spines["left"].set_visible(False)
ax.set_xlabel("Level")
ax.set_xticks(np.arange(lowest_level + 1))
ax.spines["bottom"].set_visible(False)
Exemple #5
0
def run_experiment(seed=None, graph_type=None, threshold=None, param_key=None):
    np.random.seed(seed)
    if BLIND:
        temp_param_key = param_key.replace(
            " ", "")  # don't want spaces in filenames
        savename = f"{temp_param_key}-cell-types-"
        title = param_key
    else:
        savename = f"{graph_type}-t{threshold}-cell-types"
        title = f"{graph_type}, threshold = {threshold}"

    mg = load_metagraph(graph_type, version=VERSION)

    # simple threshold
    # TODO they will want symmetric threshold...
    # TODO maybe make that a parameter
    adj = mg.adj.copy()
    adj[adj <= threshold] = 0
    meta = mg.meta.copy()
    meta = pd.DataFrame(mg.meta["neuron_name"])
    mg = MetaGraph(adj, meta)

    # run the graphtool code
    temp_loc = f"maggot_models/data/interim/temp-{param_key}.graphml"
    block_series = run_minimize_blockmodel(mg, temp_loc)

    # manage the output
    mg = load_metagraph(graph_type, version=VERSION)
    mg.meta = pd.concat((mg.meta, block_series), axis=1)
    mg.meta["Original index"] = range(len(mg.meta))
    keep_inds = mg.meta[~mg.meta["block_label"].isna(
    )]["Original index"].values
    mg.reindex(keep_inds)
    if graph_type != "G":
        mg.verify(10000, graph_type=graph_type, version=VERSION)

    # deal with class labels
    lineage_labels = mg.meta["lineage"].values
    lineage_labels = np.vectorize(lambda x: "~" + x)(lineage_labels)
    class_labels = mg["Merge Class"]
    skeleton_labels = mg.meta.index.values
    classlin_labels, color_dict, hatch_dict = augment_classes(
        skeleton_labels, class_labels, lineage_labels)
    block_label = mg["block_label"].astype(int)

    # barplot with unknown class labels merged in, proportions
    _, _, order = barplot_text(
        block_label,
        classlin_labels,
        norm_bar_width=True,
        color_dict=color_dict,
        hatch_dict=hatch_dict,
        title=title,
        figsize=(24, 18),
        return_order=True,
    )
    stashfig(savename + "barplot-mergeclasslin-props")
    category_order = np.unique(block_label)[order]

    # barplot with regular class labels
    barplot_text(
        block_label,
        class_labels,
        norm_bar_width=True,
        color_dict=color_dict,
        hatch_dict=hatch_dict,
        title=title,
        figsize=(24, 18),
        category_order=category_order,
    )
    stashfig(savename + "barplot-mergeclass-props")

    # barplot with unknown class labels merged in, counts
    barplot_text(
        block_label,
        classlin_labels,
        norm_bar_width=False,
        color_dict=color_dict,
        hatch_dict=hatch_dict,
        title=title,
        figsize=(24, 18),
        return_order=True,
        category_order=category_order,
    )
    stashfig(savename + "barplot-mergeclasslin-counts")

    # barplot of hemisphere membership
    fig, ax = plt.subplots(1, 1, figsize=(10, 20))
    stacked_barplot(
        block_label,
        mg["Hemisphere"],
        norm_bar_width=True,
        category_order=category_order,
        ax=ax,
    )
    remove_spines(ax)
    stashfig(savename + "barplot-hemisphere")

    # plot block probability matrix
    counts = False
    weights = False
    prob_df = get_blockmodel_df(mg.adj,
                                block_label,
                                return_counts=counts,
                                use_weights=weights)
    prob_df = prob_df.reindex(order, axis=0)
    prob_df = prob_df.reindex(order, axis=1)
    ax = probplot(100 * prob_df,
                  fmt="2.0f",
                  figsize=(20, 20),
                  title=title,
                  font_scale=0.4)
    stashfig(savename + "probplot")
    block_series.name = param_key
    return block_series
Exemple #6
0
plot_df["P(mirror edge present)"] = props
plot_df["Proportion of edges left"] = prop_edges
plot_df["Proportion of synapses left"] = prop_syns

fig, ax = plt.subplots(1, 1, figsize=(10, 5))

sns.lineplot(data=plot_df, x="Threshold", y="P(mirror edge present)", ax=ax)
ax_right = ax.twinx()
sns.lineplot(
    data=plot_df,
    x="Threshold",
    y="Proportion of synapses left",
    ax=ax_right,
    color="orange",
)
remove_spines(ax_right)
remove_spines(ax)
ax.set_ylim((0, 1))
ax_right.set_ylim((0, 1))
ax.set_title(f"{graph_type}")
stashfig(f"thresh-sweep-{graph_type}-brain-syns")

# %% [markdown]
# # Plot these against each other
fig, ax = plt.subplots(1, 1, figsize=(10, 10))
sns.scatterplot(data=plot_df, x="P(mirror edge present)", y="Proportion of edges left")
ax.set_xlim((0, 1.1))
ax.set_ylim((0, 1.1))
remove_spines(ax)
stashfig(f"thresh-sweep-paired-{graph_type}-brain")
# %% [markdown]
Exemple #7
0
max_pair_edge_df = edgelist_df.groupby("edge pair ID").max()
edge_max_weight_map = dict(
    zip(max_pair_edge_df.index.values, max_pair_edge_df["weight"])
)
edgelist_df["max_weight"] = itemgetter(*edgelist_df["edge pair ID"])(
    edge_max_weight_map
)

thresh_result_df = threshold_sweep(edgelist_df, max_pair_edge_df)

fig, ax = plt.subplots(1, 1, figsize=(10, 6))
sns.lineplot(
    data=thresh_result_df, x="threshold", y="Prop. paired edges symmetric", ax=ax
)
remove_spines(ax)
ax_right = plt.twinx(ax)
sns.lineplot(
    data=thresh_result_df,
    x="threshold",
    y="Prop. edges left",
    ax=ax_right,
    color="orange",
    label="Edges",
)
remove_spines(ax_right)
sns.lineplot(
    data=thresh_result_df,
    x="threshold",
    y="Prop. synapses left",
    ax=ax_right,
Exemple #8
0
def remove_axis(ax):
    remove_spines(ax)
    ax.set_xlabel("")
    ax.set_ylabel("")
    ax.set_xticks([])
    ax.set_yticks([])
Exemple #9
0
            palette=cc.glasbey_light[:n_unique],
            hue="Label",
            ax=ax,
            s=20,
        )

        add_connections(
            latent[:n_pairs, dim1],
            latent[n_pairs:2 * n_pairs, dim1],
            latent[:n_pairs, dim2],
            latent[n_pairs:2 * n_pairs, dim2],
            ax=ax,
            color="grey",
        )

        remove_spines(ax)
        ax.xaxis.set_major_locator(plt.FixedLocator([0]))
        ax.yaxis.set_major_locator(plt.FixedLocator([0]))
        stashfig(f"trunc-max-sym-dim{dim1}-vs-dim{dim2}")
        plt.close()

# %% [markdown]
# #
import pandas as pd

from bokeh.layouts import column, row
from bokeh.models import Select
from bokeh.palettes import Spectral5
from bokeh.plotting import curdoc, figure, show
from bokeh.sampledata.autompg import autompg_clean as df
from bokeh.io import output_notebook, output_file
Exemple #10
0
    plot_df["merge_class"] = meta["merge_class"].values
    ax = axs[0]
    sns.scatterplot(
        data=plot_df,
        x=0,
        y=1,
        hue="merge_class",
        palette=CLASS_COLOR_DICT,
        legend=False,
        ax=ax,
        s=20,
        linewidth=0.5,
        alpha=0.7,
    )
    # ax.axis("off")
    remove_spines(ax)
    ax.set_xlabel("")
    ax.set_ylabel("")
    ax.set_xticks([])
    ax.set_yticks([])
    ax.spines["right"].set_visible(True)
    ax.set_title("Before Procrustes")
    add_connections(
        plot_df.iloc[lp_inds, 0],
        plot_df.iloc[rp_inds, 0],
        plot_df.iloc[lp_inds, 1],
        plot_df.iloc[rp_inds, 1],
        ax=ax,
    )

    left_inds = meta[meta["left"]]["inds"]
Exemple #11
0
    # cluster = GaussianCluster(
    #     min_components=2, max_components=10, covariance_type="all", n_init=100
    # )

    cluster = AutoGMMCluster(min_components=2, max_components=10)
    pred_labels = cluster.fit_predict(latent)
    ari = adjusted_rand_score(true_labels, pred_labels)
    row = {"ARI": ari, "Threshold": threshold, "Method": "GMMoASE"}
    rows.append(row)

    # do the MCMC
    block_series = run_minimize_blockmodel(mg, weight_model="discrete-poisson")
    ari = adjusted_rand_score(true_labels, block_series.values)
    row = {"ARI": ari, "Threshold": threshold, "Method": "GT-dp"}
    rows.append(row)

    # do the MCMC
    block_series = run_minimize_blockmodel(mg, weight_model=None)
    ari = adjusted_rand_score(true_labels, block_series.values)
    row = {"ARI": ari, "Threshold": threshold, "Method": "GT-None"}
    rows.append(row)
# %% [markdown]
# #
result_df = pd.DataFrame(rows)

fig, ax = plt.subplots(1, 1, figsize=(10, 5))
sns.lineplot(data=result_df, x="Threshold", y="ARI", ax=ax, hue="Method")
remove_spines(ax, keep_corner=True)
ax.set_title(f"Mushroom Body, n_components={n_components}")
stashfig(f"mb-nc{n_components}-tr{threshold_raw}")