def actin_ring_histograms(self): for place in ["nuc", "rng"]: for _df, flt in [(self.all, "all"), (self.df, "flt")]: fig = plt.figure(figsize=(8, 4), dpi=150) ax = fig.gca() histogram_with_errorbars(_df, "hist_edges", "act_%s_hist" % place) ax.set_xscale('log') ax.xaxis.set_major_formatter(self.formatter) ax.yaxis.set_major_formatter(self.formatter) ax.set_xlim([100, 1e4]) path = o.ensure_dir( os.path.join(self.cc.base_path, 'out', 'graphs', 'histogram_actin_%s_%s.pdf' % (place, flt))) fig.savefig(path) plt.close() # if flt == "all": continue g = sns.FacetGrid(_df, row="Compound", height=3, aspect=2, legend_out=True) g = (g.map_dataframe( histogram_of_every_row, "act_%s_hist" % place).set(xscale='log').set(xlim=(100, 1e4)).add_legend()) path = o.ensure_dir( os.path.join(self.cc.base_path, 'out', 'graphs', 'histogram_actin_%s_%s.png' % (place, flt))) g.savefig(path, dpi=150) plt.close()
def dna_vs_actin_intesity(self): g = sns.FacetGrid(self.dmax, hue="Compound", legend_out=True) g = (g.map(sns.kdeplot, "nuc_dens", "act_rng_dens", shade=True, shade_lowest=False).add_legend()) for _ax in g.axes[0]: _ax.xaxis.set_major_formatter(self.formatter) _ax.yaxis.set_major_formatter(self.formatter) path = o.ensure_dir( os.path.join(self.cc.base_path, 'out', 'graphs', 'dna_vs_actin_kde_dens.pdf')) g.savefig(path) plt.close() g = sns.FacetGrid(self.dmax, hue="Compound", legend_out=True) g = (g.map(sns.kdeplot, "nuc_int", "act_rng_int", shade=True, shade_lowest=False).add_legend()) for _ax in g.axes[0]: _ax.xaxis.set_major_formatter(self.formatter) _ax.yaxis.set_major_formatter(self.formatter) path = o.ensure_dir( os.path.join(self.cc.base_path, 'out', 'graphs', 'dna_vs_actin_kde_int.pdf')) g.savefig(path) plt.close()
def actin_intensity_and_density_histogram(self): bins = np.logspace(2**-1, np.log2(self.dmax["ring_dens_ratio"].max()), 1000) logform = ticker.LogFormatterMathtext(base=2) ticks = ticker.LogLocator(base=2, ).tick_values( 2**-1, np.log2(self.dmax["ring_dens_ratio"].max())) labels = [logform(i) for i in ticks] g = sns.FacetGrid(self.dmax, hue="Compound", legend_out=True) g = (g.map_dataframe(_distplot, "ring_dens_ratio", bins=bins).set(xscale='log').set( xticks=ticks, xticklabels=labels, xlim=(min(ticks), max(ticks))).add_legend()) path = o.ensure_dir( os.path.join(self.cc.base_path, 'out', 'graphs', 'actinring_dens_hist_all.pdf')) g.savefig(path) plt.close() g = sns.FacetGrid(self.dmax, row="Compound", hue="Compound", legend_out=True) g = (g.map_dataframe(_distplot, "ring_dens_ratio", bins=bins).set(xscale='log').set( xticks=ticks, xticklabels=labels, xlim=(min(ticks), max(ticks))).add_legend()) path = o.ensure_dir( os.path.join(self.cc.base_path, 'out', 'graphs', 'actinring_dens_hist_each.pdf')) g.savefig(path) plt.close()
def select_images(df, operetta_folder, method="copy"): render_path = o.ensure_dir(os.path.join(operetta_folder, 'out', 'render')) manager = enlighten.get_manager() bar = manager.counter(total=len(df), desc='Progress', unit='files') for ix, r in df.iterrows(): destination_folder = o.ensure_dir( os.path.join(operetta_folder, 'out', 'selected-images', '%s@%s' % (r["Cell Type"], r["Cell Count"]), r["Compound"])) # name, original_path = o.ConfiguredChannels.filename_of_render(r, render_path) name = 'r%d-c%d-f%d-p%s-i%d.jpg' % (r["row"], r["col"], r["fid"], str(r["p"]), r["zid"]) original_path = os.path.join(render_path, name) destination_path = o.ensure_dir(os.path.join(destination_folder, name)) try: if method == "link": logger.debug('linking %s to %s' % (name, destination_folder)) os.symlink(original_path, destination_path, False) elif method == "copy": logger.debug('copying %s to %s' % (name, destination_folder)) copyfile(original_path, destination_path) elif method == "move": logger.debug('moving %s to %s' % (name, destination_folder)) os.rename(original_path, destination_path) bar.update() except Exception as e: logger.warning('no render for %s' % original_path) logger.warning(e) # traceback.print_stack() manager.stop()
def line_integrals(self): for a, kind in zip([self.lines, self.lines_filtered], ["all", "flt"]): # for a, kind in zip([self.lines_filtered], ["flt"]): # optional: filter a subgroup # col_order = ["Arrest", "Cycling"] # col_order = ['Cycling', 'Arrest', 'Release', # 'Cyto2ug-Cyc', 'Cyto2ug-Arr', 'Cyto2ug-Rel', # 'Noc20ng-Cyc', 'Noc20ng-Arr', 'Noc20ng-Rel'] col_order = a["Compound"].unique() print(col_order) a = a[a["Compound"].isin(col_order)] # get only one row per z-stack idx = a.groupby(["unit"])["s_max"].transform(max) == a["s_max"] a = a.loc[idx] fig = plt.figure(figsize=(8, 8), dpi=150) ax = fig.gca() sns.boxenplot(x="Compound", y="sum", order=col_order, data=a) ax.yaxis.set_major_formatter(self.formatter) ax.set_yscale('log') ax.set_xticklabels(ax.xaxis.get_ticklabels(), rotation=45, multialignment='right') path = o.ensure_dir( os.path.join(self.cc.base_path, 'out', 'graphs', 'line_boxplot_%s.pdf' % kind)) fig.savefig(path) plt.close() fig = plt.figure(figsize=(8, 8), dpi=150) ax = fig.gca() sns.scatterplot(x="v_width", y="sum", data=a, hue="Compound", alpha=0.1, rasterized=True) # plt.xscale('log') # plt.yscale('log') ax.set_xlim((0, 16)) ax.set_ylim((0, 350e3)) ax.xaxis.set_major_formatter(self.formatter) ax.yaxis.set_major_formatter(self.formatter) path = o.ensure_dir( os.path.join(self.cc.base_path, 'out', 'graphs', 'lines_scatter_%s.pdf' % kind)) fig.savefig(path) plt.close()
def __init__(self, operetta: ConfiguredChannels, is_valid_fn=None): assert type( operetta ) == ConfiguredChannels, 'need a ConfiguredChannels object.' self._cf = operetta assert not self._cf.samples.empty, 'samples in operetta out folder are empty.' self.df = self._cf.samples[self._cf.samples.apply( is_valid_fn, axis=1)] if is_valid_fn is not None else self._cf.samples self.cfgfile = os.path.join(self._cf.base_path, 'out', 'gate', 'gate.cfg') if not os.path.exists(self.cfgfile): o.ensure_dir(self.cfgfile) self._generate_gate_file() self.cfg = self._read_gate_config()
def move_images(self): df = self.df df.loc[:, "facs"] = df.apply(lambda row: Point( row['dna_int'] / 1e6 / 6, np.log(row['edu_int'])), axis=1) ax = plt.gca() render_path = o.ensure_dir( os.path.join(self._cf.base_path, 'out', 'render')) for g in self._read_gate_config(): ellipse1, ellipse2, circle, rows, cols, cell_type, cell_count, compound, concentration = g logger.info('moving images for %s|%s|%s|%s...' % (cell_type, cell_count, compound, concentration)) if not (len(rows) == 0 or len(cols) == 0): df = df[(df["row"].isin(rows)) & (df["col"].isin(cols))] den = DraggableEightNote(ax, ellipse1, ellipse2, circle, number_of_sphase_segments=4) # assign a cluster id for every polygon for i, poly in enumerate(den.polygons()): ix = df['facs'].apply(lambda g: g.within(poly)) df.loc[ix, 'cluster'] = i df = df[~df["cluster"].isna()] for ix, dfg in df.groupby("cluster"): _path = os.path.join(self._cf.base_path, 'out', '%s@%s' % (cell_type, cell_count), compound, concentration, 'render', str(int(ix))) destination_folder = o.ensure_dir(_path) for i, r in dfg.iterrows(): name, original_path = o.ConfiguredChannels.filename_of_render( r, render_path) destination_path = os.path.join(destination_folder, name) try: logger.info('moving %s to %s' % (name, destination_folder)) os.rename(original_path, destination_path) except Exception: logger.warning('no render for %s' % name)
def actin_ring(self): fig = plt.figure(figsize=(8, 8), dpi=150) ax = fig.gca() sns.boxenplot(x="Compound", y="ring_int_ratio", data=self.dmax) ax.yaxis.set_major_formatter(self.formatter) path = o.ensure_dir( os.path.join(self.cc.base_path, 'out', 'graphs', 'actinring_boxplot_cond_int.pdf')) fig.savefig(path) plt.close() fig = plt.figure(figsize=(8, 8), dpi=150) ax = fig.gca() sns.boxenplot(x="Compound", y="ring_dens_ratio", data=self.dmax) ax.yaxis.set_major_formatter(self.formatter) path = o.ensure_dir( os.path.join(self.cc.base_path, 'out', 'graphs', 'actinring_boxplot_cond_dens.pdf')) fig.savefig(path) plt.close()
def nuclei_filtered(self): fig = plt.figure(figsize=(16, 4), dpi=150) ax = fig.gca() self.df.loc[:, "nuc_area"] = self.df.apply( lambda row: shapely.wkt.loads(row['nucleus']).area, axis=1) self.df["nuc_area"].plot.hist(ax=ax, bins=1000) plt.xlim([-1, 1e3]) plt.ylim([0, 300]) ax.xaxis.set_major_locator(ticker.MultipleLocator(100)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(10)) path = o.ensure_dir( os.path.join(self.cc.base_path, 'out', 'graphs', 'filtered_nuclei_area_histogram.pdf')) fig.savefig(path) plt.close()
def actin_intensity_vs_density(self): fig = plt.figure(figsize=(8, 8), dpi=150) ax = fig.gca() sns.scatterplot(x="ring_int_ratio", y="ring_dens_ratio", data=self.df, hue="Compound", alpha=0.01) plt.xscale('log') plt.yscale('log') ax.xaxis.set_major_formatter(self.formatter) ax.yaxis.set_major_formatter(self.formatter) path = o.ensure_dir( os.path.join(self.cc.base_path, 'out', 'graphs', 'actin_int_vs_dens_scatter.png')) fig.savefig(path) plt.close()
def gate(self): df = self.df df.loc[:, "area_nucleus"] = df.apply( lambda row: shapely.wkt.loads(row['nucleus']).area, axis=1) df.loc[:, "min_dist_to_nuclear_boundary"] = df[[ 'c1_d_nuc_bound', 'c2_d_nuc_bound' ]].apply(min, axis=1) df.loc[:, "min_dist_to_nuclear_center"] = df[[ 'c1_d_nuc_centr', 'c2_d_nuc_centr' ]].apply(min, axis=1) df.loc[:, "min_dist_to_cell_boundary"] = df[[ 'c1_d_cell_bound', 'c2_d_cell_bound' ]].apply(min, axis=1) df.loc[:, "facs"] = df.apply(lambda row: Point( row['dna_int'] / 1e6 / 6, np.log(row['edu_int'])), axis=1) config = configparser.RawConfigParser() s = self._cf.samples.groupby(['row', 'col' ]).size().reset_index().drop(columns=0) l = self._cf.layout l = l[l[['row', 'col']].apply(tuple, axis=1).isin( s.apply(tuple, axis=1))].replace(pd.np.nan, '-') # see above config.add_section('General') config.set('General', 'Version', 'v0.2') config.set('General', 'Gating instances', len(l)) for g in self._read_gate_config(): section, ellipse1, ellipse2, circle, rows, cols, cell_type, cell_count, compound, concentration = g logger.info('gating %s|%s|%s|%s...' % (cell_type, cell_count, compound, concentration)) assert (len(rows) > 0 and len(cols) > 0), 'no rows or cols for this gating.' dfg = df[(df["row"].isin(rows)) & (df["col"].isin(cols))] dest_path = os.path.join(self._cf.base_path, 'out', 'gate', '%s@%s' % (cell_type, cell_count), compound, concentration) o.ensure_dir(os.path.join(dest_path, 'nil')) # gating in a matplotlib window fig = plt.figure() ax = fig.gca() ax.set_aspect('equal') # p.facs(dfg, ax=ax, xlim=[1, 8], ylim=[12, 18.5]) p.facs(dfg, ax=ax) den = DraggableEightNote(ax, ellipse1, ellipse2, circle, number_of_sphase_segments=4) fig.subplots_adjust(top=0.99, bottom=0.3) plt.show() extent = ax.get_window_extent().transformed( fig.dpi_scale_trans.inverted()) fig.savefig('{:s}/gate.png'.format(dest_path), bbox_inches=extent) plt.close(fig) config = self._store_section(config, l, section, cell_type, cell_count, compound, concentration, ellipse1, ellipse2, circle) # assign a cluster id for every polygon if dfg.empty: continue for i, poly in enumerate(den.polygons()): ix = dfg['facs'].apply(lambda g: g.within(poly)) dfg.loc[ix, 'cluster'] = i dfg = dfg[~dfg["cluster"].isna()] if dfg.empty: continue rorder = sorted(dfg["cluster"].unique()) g = sns.FacetGrid(dfg, row="cluster", row_order=rorder, height=1.5, aspect=5) g = g.map(sns.distplot, "min_dist_to_nuclear_center", rug=True) g.axes[-1][0].set_xlim([-1, 20]) g.savefig( '{:s}/centr-distribution-nucleus-center.pdf'.format(dest_path)) plt.close(g.fig) g = sns.FacetGrid(dfg, row="cluster", row_order=rorder, height=1.5, aspect=5) g = g.map(sns.distplot, "min_dist_to_nuclear_boundary", rug=True) g.axes[-1][0].set_xlim([-1, 20]) g.savefig('{:s}/centr-distribution-nucleus-boundary.pdf'.format( dest_path)) plt.close(g.fig) g = sns.FacetGrid(dfg, row="cluster", row_order=rorder, height=1.5, aspect=5) g = g.map(sns.distplot, "min_dist_to_cell_boundary", rug=True) g.axes[-1][0].set_xlim([-1, 30]) g.savefig( '{:s}/centr-distribution-cell-boundary.pdf'.format(dest_path)) plt.close(g.fig) g = sns.FacetGrid(dfg, row="cluster", row_order=rorder, height=1.5, aspect=5) g = g.map(sns.distplot, "nuc_centr_d_cell_centr", rug=True) g.axes[-1][0].set_xlim([-1, 20]) g.savefig('{:s}/centr-distribution-nuc-cell.pdf'.format(dest_path)) plt.close(g.fig) # g = sns.FacetGrid(df, row="cluster", row_order=rorder, height=1.5, aspect=5) # g = g.map(sns.distplot, "c1_d_c2", rug=True) # g.axes[-1][0].set_xlim([-1, 10]) # g.savefig('{:s}/centr-distribution-inter-centr.pdf'.format(out_path)) # plt.close(g.fig) g = sns.FacetGrid(dfg, row="cluster", row_order=rorder, height=1.5, aspect=5) g = g.map(sns.distplot, "area_nucleus", rug=True) g.savefig('{:s}/nucleus-distribution-area.pdf'.format(dest_path)) plt.close(g.fig) # rorder = sorted(dfg["cluster"].unique()) # dfg = dfg.melt(id_vars=["row", "col", "cluster"]) # corder = ["c1_d_nuc_centr", "c1_d_nuc_bound", "c1_d_cell_bound", "nuc_centr_d_cell_centr"] # dfg = dfg[dfg["variable"].isin(corder)] # # g = sns.FacetGrid(dfg, row="cluster", row_order=rorder, col="variable", height=1.5, aspect=3) # g = g.map_dataframe(_distplot, "value", rug=True) # g.savefig('{:s}/distribution-across-cc.pdf'.format(out_path)) with open(self.cfgfile, 'w') as configfile: config.write(configfile)
def line_measurements(self): a = self.lines_filtered # optional: filter a subgroup # a = a[a["Compound"].isin(["Arrest", "Cycling"])] # get only one row per z-stack idx = a.groupby(["unit"])["s_max"].transform(max) == a["s_max"] a = a[idx] # remove constant component of signal vector a.loc[:, "crit"] = a['signal'].apply(lambda v: v.max() - v.min()) a.loc[:, "v_mean"] = a['signal'].apply(lambda v: v.mean()) a.loc[:, "signal_n"] = a["signal"] - a["v_mean"] a.drop(columns=[ "v_mean", "crit", "signal", "x", "sum", "v_width", "xpeak" ], inplace=True) b = m.vector_column_to_long_fmt(a, val_col='signal_n', ix_col='x_center') # build a new index b = b.set_index(['unit', 'variable']) b.index = [b.index.map('{0[0]}|{0[1]}'.format)] b = b.reset_index().rename(columns={"level_0": "unit"}) # plots x_var = 'x_center' y_var = 'signal_n' g = sns.FacetGrid(b, hue="Compound", row='Compound', height=2, aspect=2) g = ( g.map_dataframe(sns.lineplot, x=x_var, y=y_var, units='unit', estimator=None, alpha=1, lw=0.1) # .set(yscale='log') .set(xlim=(-20, 20))) for _ax in g.axes[0]: _ax.xaxis.set_major_formatter(self.formatter) _ax.yaxis.set_major_formatter(self.formatter) path = o.ensure_dir( os.path.join(self.cc.base_path, 'out', 'graphs', 'lines_flt_indiv.pdf')) g.savefig(path) plt.close() fig = plt.figure(figsize=(6, 4), dpi=150) ax = fig.gca() sns.lineplot(x=x_var, y=y_var, data=b, hue='Compound', ax=ax) ax.xaxis.set_major_formatter(self.formatter) ax.yaxis.set_major_formatter(self.formatter) ax.xaxis.set_major_locator(ticker.MultipleLocator(10)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(5)) ax.set_xlim([-20, 20]) # plt.yscale('log') path = o.ensure_dir( os.path.join(self.cc.base_path, 'out', 'graphs', 'lines_trend.pdf')) fig.savefig(path) plt.close()
def histogram_areas(self): a = self.all a.loc[:, 'rng_hist_area'] = a.apply( lambda r: hist_area(r['hist_edges'], r['act_rng_hist']), axis=1) a.loc[:, 'nuc_hist_area'] = a.apply( lambda r: hist_area(r['hist_edges'], r['act_nuc_hist']), axis=1) a.loc[:, 'hist_area_ratio'] = a['rng_hist_area'] / a['nuc_hist_area'] self.all = a fig = plt.figure(figsize=(8, 8), dpi=150) ax = fig.gca() sns.scatterplot(x="nuc_hist_area", y="rng_hist_area", data=a, hue="Compound", alpha=0.01, rasterized=True) plt.xscale('log') plt.yscale('log') ax.xaxis.set_major_formatter(self.formatter) ax.yaxis.set_major_formatter(self.formatter) path = o.ensure_dir( os.path.join(self.cc.base_path, 'out', 'graphs', 'histareas_scatter.pdf')) fig.savefig(path) plt.close() logbins = np.logspace(0, np.log10(1e6), 1000) g = sns.FacetGrid(a, hue="Compound") g = ( g.map_dataframe(_distplot, "nuc_hist_area", bins=logbins).set(xscale='log') # .set(xticks=ticks, xticklabels=labels, xlim=(min(ticks), max(ticks))) .add_legend()) path = o.ensure_dir( os.path.join(self.cc.base_path, 'out', 'graphs', 'histareas_nuc_dist.pdf')) g.savefig(path) plt.close() g = sns.FacetGrid(a, hue="Compound") g = ( g.map_dataframe(_distplot, "rng_hist_area", bins=logbins).set(xscale='log') # .set(xticks=ticks, xticklabels=labels, xlim=(min(ticks), max(ticks))) .add_legend()) path = o.ensure_dir( os.path.join(self.cc.base_path, 'out', 'graphs', 'histareas_rng_dist.pdf')) g.savefig(path) plt.close() logbins = np.logspace(0, np.log10(a["hist_area_ratio"].max()), 1000) g = sns.FacetGrid(a, hue="Compound") g = ( g.map_dataframe(_distplot, "hist_area_ratio", bins=logbins).set(xscale='log') # .set(xticks=ticks, xticklabels=labels, xlim=(min(ticks), max(ticks))) .add_legend()) path = o.ensure_dir( os.path.join(self.cc.base_path, 'out', 'graphs', 'histareas_ratio_dist.pdf')) g.savefig(path) plt.close()
col_wrap=2, col_order=_order, hue_order=_order, height=2) g = (g.map_dataframe(sns.lineplot, x=x_var, y=y_var, style='folder', units='indiv', estimator=None, alpha=1, lw=0.2).set(xlim=(0, 4))) for _ax in g.axes: _ax.xaxis.set_major_formatter(formatter) _ax.yaxis.set_major_formatter(formatter) path = o.ensure_dir(os.path.join(args.folder, 'lines_indiv.pdf')) g.savefig(path) g = sns.FacetGrid(b, hue="condition", col='condition', col_wrap=2, col_order=_order, hue_order=_order, height=2) g = (g.map_dataframe(sns.lineplot, x=x_var, y=y_var, style='folder').set(xlim=(0, 4))) for _ax in g.axes: _ax.xaxis.set_major_formatter(formatter) _ax.yaxis.set_major_formatter(formatter) path = o.ensure_dir(os.path.join(args.folder, 'lines_trend.pdf'))