Beispiel #1
0
def plot_categ_spatial(mod,
                       adata,
                       sample_col,
                       color,
                       n_columns=2,
                       figure_size=(24, 5.7),
                       point_size=0.8,
                       text_size=9):

    for_plot = adata.obs[["imagecol", "imagerow", sample_col]]
    for_plot["color"] = color

    # fix types
    for_plot["color"] = pd.Categorical(for_plot["color"], ordered=True)
    # for_plot['color'] = pd.to_numeric(for_plot['color'])
    for_plot["sample"] = pd.Categorical(for_plot[sample_col], ordered=False)
    for_plot["imagecol"] = pd.to_numeric(for_plot["imagecol"])
    for_plot["imagerow"] = -pd.to_numeric(for_plot["imagerow"])

    ax = (
        plotnine.ggplot(
            for_plot, plotnine.aes(x="imagecol", y="imagerow", color="color"))
        + plotnine.geom_point(size=point_size)  # + plotnine.scale_color_cmap()
        + plotnine.coord_fixed() + plotnine.theme_bw() + plotnine.theme(
            panel_background=plotnine.element_rect(
                fill="black", colour="black", size=0, linetype="solid"),
            panel_grid_major=plotnine.element_line(
                size=0, linetype="solid", colour="black"),
            panel_grid_minor=plotnine.element_line(
                size=0, linetype="solid", colour="black"),
            strip_text=plotnine.element_text(size=text_size),
        ) + plotnine.facet_wrap("~sample", ncol=n_columns) +
        plotnine.theme(figure_size=figure_size))

    return ax
Beispiel #2
0
def plot_counts(counts: pd.DataFrame):
	counts.bc_l = pd.Categorical(counts.bc_l, bc_range(counts.bc_l))
	counts.bc_r = pd.Categorical(counts.bc_r, bc_range(counts.bc_r))
	log = np.log
	return (
		ggplot(counts, aes('bc_r', 'bc_l', fill='log(Count)'))
		+ geom_tile()
		# + scale_y_reverse()
		+ coord_fixed()
		+ scale_fill_cmap('inferno')
		+ theme(axis_text_x=element_text(angle=90, vjust=.5))
		+ labs(x='Right Barcode', y='Left Barcode')
	)
Beispiel #3
0
    def pictures(self, mode='bw', subset=None, n_random=10):
        """Returns a picture of the selected images.

        Creates either a colored or a black-white picture of the selected
        images.

        Args:
            mode: Should the picture be black-white ('bw') or in color
                ('color')?
            subset: Optional list of picture indices that should be included in
                the dataframe. If specified, n_random will be ignored.
            n_random: Optional number of randomly selected images. If neither
                subset nor n_random are specified, all images will be included.

        Returns:
            A plotnine object including all pictures with their label.

        Raises:
            NotImplementedError: mode must be either 'bw' or 'color'."""
        dataframe = self.rgb_dataframe(subset=subset, n_random=n_random)
        if mode == 'bw':
            fill_key = 'rgb_bw'
        elif mode == 'color':
            fill_key = 'rgb'
        else:
            raise NotImplementedError("Pictures are either in black-white"
                                      "('bw') or in color ('color').")
        picture = (
            gg.ggplot(dataframe, gg.aes(x='x', y='y', fill=fill_key)) +
            gg.geom_tile() + gg.theme_void() +
            gg.theme(legend_position='none') + gg.scale_fill_manual(
                values={key: key
                        for key in dataframe[fill_key].unique()}) +
            gg.facet_wrap('image_id', labeller=self.labeller) +
            gg.scale_y_reverse() + gg.coord_fixed())
        return picture
Beispiel #4
0
def plot_factor_spatial(
        adata,
        fact,
        cluster_names,
        fact_ind=[0],
        trans="log",
        sample_name=None,
        samples_col="sample",
        obs_x="imagecol",
        obs_y="imagerow",
        n_columns=6,
        max_col=5000,
        col_breaks=[0.1, 100, 1000, 3000],
        figure_size=(24, 5.7),
        point_size=0.8,
        text_size=9,
):
    r"""Plot expression of factors / cell types in space.
    Convenient but not as powerful as scanpy plotting.

    :param adata: anndata object with spatial data
    :param fact: pd.DataFrame with spatial expression of factors (W), e.g. mod.spot_factors_df
    :param cluster_names: names of those factors to show on a plot
    :param fact_ind: index of factors to plot
    :param trans: transform colorscale? passed to plotnine.scale_color_cmap
    :param sample_name: if anndata object contains multiple samples specify which sample to plot (no warning given if not)
    :param samples_col: if anndata object contains multiple which .obs columns specifies sample?
    :param obs_x: which .obs columns specifies x coordinate?
    :param obs_y: which .obs columns specifies y coordinate?
    :param n_columns: how many factors / clusters to plot in each row (plotnine.facet_grid)
    :param max_col: colorscale maximum expression in fact
    :param col_breaks: colorscale breaks
    :param figure_size: figures size works weirdly (only x axis has an effect, use 24 for 6-column plot, 12 for 3, 8 for 2 ...).
    :param point_size: point size of spots
    :param text_size: text size
    """

    if sample_name is not None:
        sample_ind = np.isin(adata.obs[samples_col], sample_name)
    else:
        sample_ind = np.repeat(True, adata.shape[0])

    # adata.obsm['X_spatial'][:,0] vs adata.obs['imagecol'] & adata.obs['imagerow']

    for_plot = np.concatenate(
        (
            adata.obs[obs_x].values.reshape((adata.obs.shape[0], 1)),
            -adata.obs[obs_y].values.reshape((adata.obs.shape[0], 1)),
            fact.iloc[:, fact_ind[0]].values.reshape((adata.obs.shape[0], 1)),
            np.array([
                cluster_names[fact_ind[0]] for j in range(adata.obs.shape[0])
            ]).reshape((adata.obs.shape[0], 1)),
        ),
        1,
    )
    for_plot = pd.DataFrame(
        for_plot,
        index=adata.obs.index,
        columns=["imagecol", "imagerow", "weights", "cluster"])
    # select only correct sample
    for_plot = for_plot.loc[sample_ind, :]

    for i in fact_ind[1:]:
        for_plot1 = np.concatenate(
            (
                adata.obs[obs_x].values.reshape((adata.obs.shape[0], 1)),
                -adata.obs[obs_y].values.reshape((adata.obs.shape[0], 1)),
                fact.iloc[:, i].values.reshape((adata.obs.shape[0], 1)),
                np.array([cluster_names[i]
                          for j in range(adata.obs.shape[0])]).reshape(
                              (adata.obs.shape[0], 1)),
            ),
            1,
        )
        for_plot1 = pd.DataFrame(
            for_plot1,
            index=adata.obs.index,
            columns=["imagecol", "imagerow", "weights", "cluster"])
        # select only correct sample
        for_plot1 = for_plot1.loc[sample_ind, :]
        for_plot = pd.concat((for_plot, for_plot1))

    for_plot["imagecol"] = pd.to_numeric(for_plot["imagecol"])
    for_plot["imagerow"] = pd.to_numeric(for_plot["imagerow"])
    for_plot["weights"] = pd.to_numeric(for_plot["weights"])
    for_plot["cluster"] = pd.Categorical(for_plot["cluster"],
                                         categories=cluster_names[fact_ind],
                                         ordered=True)

    # print(np.log(np.max(for_plot['weights'])))
    ax = (plotnine.ggplot(
        for_plot, plotnine.aes("imagecol", "imagerow", color="weights")) +
          plotnine.geom_point(size=point_size) +
          plotnine.scale_color_cmap("magma",
                                    trans=trans,
                                    limits=[0.1, max_col],
                                    breaks=col_breaks + [max_col]) +
          plotnine.coord_fixed() + plotnine.theme_bw() + plotnine.theme(
              panel_background=plotnine.element_rect(
                  fill="black", colour="black", size=0, linetype="solid"),
              panel_grid_major=plotnine.element_line(
                  size=0, linetype="solid", colour="black"),
              panel_grid_minor=plotnine.element_line(
                  size=0, linetype="solid", colour="black"),
              strip_text=plotnine.element_text(size=text_size),
          ) + plotnine.facet_wrap("~cluster", ncol=n_columns) +
          plotnine.ggtitle("nUMI from each cell type") +
          plotnine.theme(figure_size=figure_size))

    return ax
                                    on="site").merge(loc_df,
                                                     on="site_location")

# Plot total number of cells per well
cell_count_totalcells_df = (cell_count_df.groupby(
    ["x_loc", "y_loc", "well", "site_location",
     "site"])["total_cell_count"].mean().reset_index())

plate = cell_count_df["plate"].unique()[0]

os.makedirs(output_figuresdir, exist_ok=True)
by_well_gg = (
    gg.ggplot(cell_count_totalcells_df, gg.aes(x="x_loc", y="y_loc")) +
    gg.geom_point(gg.aes(fill="total_cell_count"), size=10) +
    gg.geom_text(gg.aes(label="site_location"), color="lightgrey") +
    gg.facet_wrap("~well") + gg.coord_fixed() + gg.theme_bw() +
    gg.ggtitle(f"Total Cells/Well\n{plate}") + gg.theme(
        axis_text=gg.element_blank(),
        axis_title=gg.element_blank(),
        strip_background=gg.element_rect(colour="black", fill="#fdfff4"),
    ) + gg.labs(fill="Cells") + gg.scale_fill_cmap(name="magma"))

output_file = pathlib.Path(output_figuresdir,
                           "plate_layout_cells_count_per_well.png")
if check_if_write(output_file, force, throw_warning=True):
    by_well_gg.save(output_file, dpi=300, verbose=False)

# Plot cell category ratios per well
ratio_df = pd.pivot_table(
    cell_count_df,
    values="cell_count",
Beispiel #6
0
def test_coord_fixed():
    assert p + coord_fixed(0.5) == 'coord_fixed'
Beispiel #7
0
def test_coord_fixed():
    assert p + coord_fixed(0.5) == 'coord_fixed'
Beispiel #8
0
    cell_count_df.groupby(["x_loc", "y_loc", "well", "site_location", "site"])[
        "total_cell_count"
    ]
    .mean()
    .reset_index()
)

plate = cell_count_df["plate"].unique()[0]

os.makedirs(output_figuresdir, exist_ok=True)
by_well_gg = (
    gg.ggplot(cell_count_totalcells_df, gg.aes(x="x_loc", y="y_loc"))
    + gg.geom_point(gg.aes(fill="total_cell_count"), size=10)
    + gg.geom_text(gg.aes(label="site_location"), color="lightgrey")
    + gg.facet_wrap("~well")
    + gg.coord_fixed()
    + gg.theme_bw()
    + gg.ggtitle(f"Total Cells/Well\n{plate}")
    + gg.theme(
        axis_text=gg.element_blank(),
        axis_title=gg.element_blank(),
        strip_background=gg.element_rect(colour="black", fill="#fdfff4"),
    )
    + gg.labs(fill="Cells")
    + gg.scale_fill_cmap(name="magma")
)

output_file = pathlib.Path(output_figuresdir, "plate_layout_cells_count_per_well.png")
if check_if_write(output_file, force, throw_warning=True):
    by_well_gg.save(output_file, dpi=300, verbose=False)
Beispiel #9
0
feed = {}
feed.update(tr_isotropic.params["range"].tf_feed_entry)
feed.update(tr_anis_2D.params["maxrange"].tf_feed_entry)
feed.update(tr_anis_2D.params["minrange_fct"].tf_feed_entry)
feed.update(tr_anis_2D.params["azimuth"].tf_feed_entry)
feed.update(tr_proj.params["azimuth"].tf_feed_entry)
feed.update(tr_proj.params["range"].tf_feed_entry)
with tf.Session(graph=g) as session:
    session.run(init, feed_dict=feed)
    out_identity = test_identity.eval(session=session, feed_dict=feed)
    out_isotropic = test_isotropic.eval(session=session, feed_dict=feed)
    out_anis_2D = test_anis_2D.eval(session=session, feed_dict=feed)
    out_proj = test_proj.eval(session=session, feed_dict=feed)

# visual testing
df = pd.DataFrame({"x": test_identity[:, 0], "y": test_identity[:, 1]})
fig_id = p9.ggplot(df) +\
         p9.geom_point(p9.aes("x", "y")) +\
         p9.coord_fixed()
        
df = pd.DataFrame({"x": test_isotropic[:, 0], "y": test_isotropic[:, 1]})
fig_iso = p9.ggplot(df) +\
          p9.geom_point(p9.aes("x", "y")) +\
          p9.coord_fixed()
        
df = pd.DataFrame({"x": test_anis_2D[:, 0], "y": test_anis_2D[:, 1]})
fig_anis2D = p9.ggplot(df) +\
             p9.geom_point(p9.aes("x", "y")) +\
             p9.coord_fixed()