Analysis on donuts#
[1]:
%load_ext autoreload
%autoreload 2
#
from astropathdb import AstroDB
from astropathdb import load_astrosample
from sidus import plotting as pl
from astropathdb import tools as tl
import datashader as ds
[2]:
# For actual center cells:
# select cellid from donut_CS2.dbo.DiscriminantsTable where D5 > 0.45
# For randomcells:
# select cellid from donut_CS2.dbo.DiscriminantsTableDR where D5 > 0.45
#
# There are also ptype_lung and other useful columns in there.
# (For CS2 it's the same as normal ptype. For melanoma the ptypes
# were defined in a different order, so I have ptype_local and ptype_lung.)
#
# trainingsetid:
# 1: train on lung
# 2: Do not use
# 3: train on melanoma
# distancebinselectionid:
# 1: 5-10 micron donuts
# 2: bigger ones but they don't work so well
[2]:
db = AstroDB(database = 'wsi02')
[21]:
sampleid = 540
sample = load_astrosample(sampleid = sampleid, astrodb=db, cells_table = True, cells_positions = True)
# add d5 column
sql = f"""
select c.cellid, D5
from donut_CS2.dbo.DiscriminantsTable d, dbo.celltag c
where d.sampleid = {sampleid}
and trainingsetid = 1
and distancebinselectionid = 1
and d.cellid = c.cellid
and ptype = ptype_lung
"""
sample_all_cell_donuts = db.query(sql)
#
sample_all_cell_donuts['cellid'] = sample_all_cell_donuts['cellid'].astype(str)
sample_all_cell_donuts.set_index('cellid', inplace = True)
sample['sampleid_540_mIF'].obs.loc[sample_all_cell_donuts.index, 'd5'] = sample_all_cell_donuts['D5']
#
# subset signal
#
sample['sampleid_540_mIF_cell_donuts'] = sample['sampleid_540_mIF'][sample['sampleid_540_mIF'].obs['d5'] > 0.4999].copy()
sample['sampleid_540_mIF_cell_donuts_background'] = sample['sampleid_540_mIF'][sample['sampleid_540_mIF'].obs['d5'] <= 0.4999].copy()
[39]:
cmap = {0.0: '#005592', 1.0: '#cccc00', 2.0:'#d62728', 3.0:'#ff7f0e', 4.0:'#cc00cc', 5.0:'#C16C95'}
shapes_id = f"sampleid_{sampleid}_cells_positions"
table_id_normal = f"sampleid_{sampleid}_mIF"
table_id = f"sampleid_{sampleid}_mIF_cell_donuts"
table_id_background = f"sampleid_{sampleid}_mIF_cell_donuts_background"
#
annotations_kwargs = dict(
title='Annotations (t: orange, r:red)',
sampleid = sampleid,
shapes_element_id=f'sampleid_{sampleid}_annotations',
column = 'lname',
groups ={'lname': ['tumor', 'regression']},
shapes_kwargs={'lw': 1, 'alpha': .25, 'cmap': ['tab:red', 'tab:orange']}
)
#
dotplot_args= dict(
sampleid=sampleid, shapes_element_id=shapes_id, column = 'ptype', imvisible = False,
shapes_kwargs = dict(cmap=cmap, as_datashader=True)
)
#
densityplot_args= dict(
sampleid=sampleid, shapes_element_id=shapes_id, column = 'ptype', imvisible = False,
shapes_kwargs = dict(cmap='plasma', as_datashader=True, agg = ds.count())
)
[40]:
#
pl.SubPlots(
pl.ShowImage(sample, sampleid = '540', title = f"SampleID: {sampleid} Image"),
pl.ShowShapes(sample, title = "dotplot all cells",table_name = table_id_normal,**dotplot_args),
pl.ShowShapes(sample, title = "density plot all cells", table_name = table_id_normal, **densityplot_args),
pl.ShowShapes(sample, **annotations_kwargs),
pl.ShowShapes(sample, title = "dotplot donuts",table_name = table_id,**dotplot_args),
pl.ShowShapes(sample, title = "density plot donuts", table_name = table_id, **densityplot_args),
columns = 3, figsize = (10, 6),
)
[40]:
<sidus.plotting.vizbase.SubPlots at 0x2170921c0d0>
[41]:
args = dict(
sampleid=sampleid, shapes_element_id=shapes_id, table_name = table_id,
column = 'ptype', imvisible = True,
)
#
shapes_kwargs = dict(cmap=cmap, as_datashader=True)
[48]:
args = {'shapes_element_id': shapes_id, 'table_name': table_id}
pl.SubPlots(
pl.ShowImage(sample, title = f"SampleID: {sampleid} Image"),
pl.ShowShapes(sample, title = 'Other', groups={'ptype': [0]}, shapes_kwargs = {'cells_kwargs': {'cmap':[cmap[0.0]], 'as_datashader':True, 'scale': 128}}, **args),
pl.ShowShapes(sample, title = 'CD8', groups={'ptype': [1]}, shapes_kwargs = {'cells_kwargs': {'cmap':[cmap[1.0]], 'as_datashader':True, 'scale': 128}}, **args),
pl.ShowShapes(sample, title = 'FoxP3', groups={'ptype': [2]}, shapes_kwargs = {'cells_kwargs': {'cmap':[cmap[2.0]], 'as_datashader':True, 'scale': 128}}, **args),
pl.ShowShapes(sample, **annotations_kwargs),
pl.ShowShapes(sample, title = 'Tumor', groups={'ptype': [3]}, shapes_kwargs = {'cells_kwargs': {'cmap':[cmap[3.0]], 'as_datashader':True, 'scale': 128}}, **args),
pl.ShowShapes(sample, title = 'CD163', groups={'ptype': [4]}, shapes_kwargs = {'cells_kwargs': {'cmap':[cmap[4.0]], 'as_datashader':True, 'scale': 128}}, **args),
pl.ShowShapes(sample, title = 'CD8FoxP3', groups={'ptype': [5]}, shapes_kwargs = {'cells_kwargs': {'cmap':[cmap[5.0]], 'as_datashader':True, 'scale': 128}}, **args),
columns = 4, figsize = (10, 4)
)
None
None
None
lname
None
None
None
None
None
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[48], line 2
1 args = {'shapes_element_id': shapes_id, 'table_name': table_id}
----> 2 pl.SubPlots(
3 pl.ShowImage(sample, title = f"SampleID: {sampleid} Image"),
4 pl.ShowShapes(sample, title = 'Other', groups={'ptype': [0]}, shapes_kwargs = {'cells_kwargs': {'cmap':[cmap[0.0]], 'as_datashader':True, 'scale': 128}}, **args),
5 pl.ShowShapes(sample, title = 'CD8', groups={'ptype': [1]}, shapes_kwargs = {'cells_kwargs': {'cmap':[cmap[1.0]], 'as_datashader':True, 'scale': 128}}, **args),
6 pl.ShowShapes(sample, title = 'FoxP3', groups={'ptype': [2]}, shapes_kwargs = {'cells_kwargs': {'cmap':[cmap[2.0]], 'as_datashader':True, 'scale': 128}}, **args),
7 pl.ShowShapes(sample, **annotations_kwargs),
8 pl.ShowShapes(sample, title = 'Tumor', groups={'ptype': [3]}, shapes_kwargs = {'cells_kwargs': {'cmap':[cmap[3.0]], 'as_datashader':True, 'scale': 128}}, **args),
9 pl.ShowShapes(sample, title = 'CD163', groups={'ptype': [4]}, shapes_kwargs = {'cells_kwargs': {'cmap':[cmap[4.0]], 'as_datashader':True, 'scale': 128}}, **args),
10 pl.ShowShapes(sample, title = 'CD8FoxP3', groups={'ptype': [5]}, shapes_kwargs = {'cells_kwargs': {'cmap':[cmap[5.0]], 'as_datashader':True, 'scale': 128}}, **args),
11 columns = 4, figsize = (10, 4)
12 )
File E:\bgreen\sidus\src\sidus\plotting\vizbase.py:257, in SubPlots.__init__(self, columns, set_aspect, *args, **kwargs)
253 pl.ax = self.fig.add_subplot(
254 rows, columns, i + 1, autoscale_on=True
255 )
256 self.axs.append(pl.ax)
--> 257 pl.show()
258 #
259 if set_aspect:
File E:\bgreen\sidus\src\sidus\plotting\vizshapes.py:656, in ShowShapes.show(self)
654 super().show()
655 for key in ["cells", "spots", "shapes", "annos"]:
--> 656 self._plot_shape(getattr(self, f"_{key}_data", None), key)
657 #
658 self.ax.set(**self._axes_kwargs)
File E:\bgreen\sidus\src\sidus\plotting\vizshapes.py:646, in ShowShapes._plot_shape(self, geom_data, shape_key)
630 ShowGeomShapes(
631 geom_data,
632 geom_kwargs,
(...)
636 self.__shapes.bb,
637 ).plot_geoms()
638 else:
639 ShowDatashaderShapes(
640 geom_data,
641 geom_kwargs,
642 shape_key,
643 self.ax,
644 self.__shapes.transform,
645 self.__shapes.bb,
--> 646 ).plot_datashader()
File E:\bgreen\sidus\src\sidus\plotting\vizshapes.py:415, in ShowDatashaderShapes.plot_datashader(self)
413 #
414 if not column:
--> 415 self._datashader_continuous(canvas_size)
416 return
417 if self._geom_data.get("categorical", False) or _is_discrete(
418 self._geom_data[column]
419 ):
File E:\bgreen\sidus\src\sidus\plotting\vizshapes.py:386, in ShowDatashaderShapes._datashader_continuous(self, canvas_size, column)
384 agg_function = ds.count()
385 #
--> 386 self._handle_color_defaults("datashader", categorical=True)
387 #
388 shade_args = ShowShapesOptions.DATASHADE.copy()
File E:\bgreen\sidus\src\sidus\plotting\vizshapes.py:83, in ShowShapesBase._handle_color_defaults(self, backend, categorical)
81 #
82 print(self._geom_kwargs.get("column"))
---> 83 self._geom_kwargs["cmap"] = validate_cmap(
84 cmap=self._geom_kwargs.get("cmap"),
85 column=self._geom_kwargs.get("column"),
86 data_df=self._geom_data,
87 plotting_backend=backend,
88 categorical=categorical,
89 )
File E:\bgreen\sidus\src\sidus\tools\_utils.py:493, in validate_cmap(cmap, column, data_df, plotting_backend, categorical)
470 def validate_cmap(
471 cmap: Union[str, DataFrame, Mapping[str, Any]] = None,
472 column: Optional[str] = None,
(...)
475 categorical: bool = True,
476 ) -> Union[Mapping[str, Any], Colormap, list]:
477 """
478 Validate and prepare a colormap for use in plotting.
479
(...)
490 Validated colormap ready for plotting.
491 """
--> 493 color_maps = ColorMaps(cmap, column, data_df, plotting_backend, categorical)
494 color_maps.validate_cmap()
496 return color_maps.cmap
File E:\bgreen\sidus\src\sidus\tools\_utils.py:343, in ColorMaps.__init__(self, cmap, column, data_df, plotting_backend, categorical)
333 def __init__(
334 self,
335 cmap: Union[str, DataFrame, Mapping[str, Any]] = None,
(...)
339 categorical=True,
340 ):
342 self.cmap = self._handle_cmap_defaults(cmap, column, categorical)
--> 343 self._column = column.lower()
344 self._plotting_backend = plotting_backend
345 self._data_df = data_df
AttributeError: 'NoneType' object has no attribute 'lower'
[56]:
args = {'shapes_element_id': shapes_id, 'table_name': table_id, 'shapes_kwargs': {'agg': ds.count(), 'cmap':'plasma', 'as_datashader':True, 'scale': 128}}
pl.SubPlots(
pl.ShowImage(sample, title = f"SampleID: {sampleid} Image"),
pl.ShowShapes(sample, title = 'Other', column={'ptype': 0}, **args),
pl.ShowShapes(sample, title = 'CD8', column={'ptype': 1}, **args),
pl.ShowShapes(sample, title = 'FoxP3', column={'ptype': 2}, **args),
pl.ShowShapes(sample, **annotations_kwargs),
pl.ShowShapes(sample, title = 'Tumor', column={'ptype': 3}, **args),
pl.ShowShapes(sample, title = 'CD163', column={'ptype': 4}, **args),
pl.ShowShapes(sample, title = 'CD8FoxP3', column={'ptype': 5}, **args),
columns = 4, figsize = (10, 4)
)
[56]:
<astropathdb.plotting.vizbase.SubPlots at 0x1503d67f220>
[42]:
args = {'shapes_element_id': shapes_id, 'table_name': table_id, 'shapes_kwargs': {'cmap':'plasma'}}
pl.SubPlots(
pl.ShowImage(sample, title = f"SampleID: {sampleid} Image"),
pl.ShowShapes(sample, title = 'PDL1', column='cmembrane520', **args),
pl.ShowShapes(sample, title = 'CD8', column='cmembrane540', **args),
pl.ShowShapes(sample, title = 'FoxP3', column='cnucleus570', **args),
pl.ShowShapes(sample, **annotations_kwargs),
pl.ShowShapes(sample, title = 'Tumor', column='ccytomembrane620', **args),
pl.ShowShapes(sample, title = 'PD1', column='cmembrane650', **args),
pl.ShowShapes(sample, title = 'CD163', column='cmembrane690', **args),
columns = 4, figsize = (10, 4)
)
[42]:
<astropathdb.plotting.vizbase.SubPlots at 0x14feed41330>
[71]:
args = {'shapes_element_id': shapes_id, 'table_name': table_id_normal}
pl.SubPlots(
pl.ShowImage(sample, title = f"SampleID: {sampleid} Image"),
pl.ShowShapes(sample, title = 'Other', column={'ptype': 0}, shapes_kwargs = {'cells_kwargs': {'cmap':[cmap['cmap'][0.0]], 'as_datashader':True, 'scale': 128}}, **args),
pl.ShowShapes(sample, title = 'CD8', column={'ptype': 1}, shapes_kwargs = {'cells_kwargs': {'cmap':[cmap['cmap'][1.0]], 'as_datashader':True, 'scale': 128}}, **args),
pl.ShowShapes(sample, title = 'FoxP3', column={'ptype': 2}, shapes_kwargs = {'cells_kwargs': {'cmap':[cmap['cmap'][2.0]], 'as_datashader':True, 'scale': 128}}, **args),
pl.ShowShapes(sample, **annotations_kwargs),
pl.ShowShapes(sample, title = 'Tumor', column={'ptype': 3}, shapes_kwargs = {'cells_kwargs': {'cmap':[cmap['cmap'][3.0]], 'as_datashader':True, 'scale': 128}}, **args),
pl.ShowShapes(sample, title = 'CD163', column={'ptype': 4}, shapes_kwargs = {'cells_kwargs': {'cmap':[cmap['cmap'][4.0]], 'as_datashader':True, 'scale': 128}}, **args),
pl.ShowShapes(sample, title = 'CD8FoxP3', column={'ptype': 5}, shapes_kwargs = {'cells_kwargs': {'cmap':[cmap['cmap'][5.0]], 'as_datashader':True, 'scale': 128}}, **args),
columns = 4, figsize = (10, 4)
)
[71]:
<astropathdb.plotting.vizbase.SubPlots at 0x1501ff8d390>
[70]:
args = {'shapes_element_id': shapes_id, 'table_name': table_id_normal, 'shapes_kwargs': {'agg': ds.count(), 'cmap':'plasma', 'as_datashader':True, 'scale': 128}}
pl.SubPlots(
pl.ShowImage(sample, title = f"SampleID: {sampleid} Image"),
pl.ShowShapes(sample, title = 'Other', column={'ptype': 0}, **args),
pl.ShowShapes(sample, title = 'CD8', column={'ptype': 1}, **args),
pl.ShowShapes(sample, title = 'FoxP3', column={'ptype': 2}, **args),
pl.ShowShapes(sample, **annotations_kwargs),
pl.ShowShapes(sample, title = 'Tumor', column={'ptype': 3}, **args),
pl.ShowShapes(sample, title = 'CD163', column={'ptype': 4}, **args),
pl.ShowShapes(sample, title = 'CD8FoxP3', column={'ptype': 5}, **args),
columns = 4, figsize = (10, 4)
)
[70]:
<astropathdb.plotting.vizbase.SubPlots at 0x15003adc610>
[69]:
args = {'shapes_element_id': shapes_id, 'table_name': table_id_normal, 'shapes_kwargs': {'cmap':'plasma'}}
pl.SubPlots(
pl.ShowImage(sample, title = f"SampleID: {sampleid} Image"),
pl.ShowShapes(sample, title = 'PDL1', column='cmembrane520', **args),
pl.ShowShapes(sample, title = 'CD8', column='cmembrane540', **args),
pl.ShowShapes(sample, title = 'FoxP3', column='cnucleus570', **args),
pl.ShowShapes(sample, **annotations_kwargs),
pl.ShowShapes(sample, title = 'Tumor', column='ccytomembrane620', **args),
pl.ShowShapes(sample, title = 'PD1', column='cmembrane650', **args),
pl.ShowShapes(sample, title = 'CD163', column='cmembrane690', **args),
columns = 4, figsize = (10, 4)
)
[69]:
<astropathdb.plotting.vizbase.SubPlots at 0x150237c2f50>
[66]:
args = {'shapes_element_id': shapes_id, 'table_name': table_id_background, 'shapes_kwargs': {'agg': ds.count(), 'cmap':'plasma', 'as_datashader':True, 'scale': 128}}
pl.SubPlots(
pl.ShowImage(sample, title = f"SampleID: {sampleid} Image"),
pl.ShowShapes(sample, title = 'Other', column={'ptype': 0}, **args),
pl.ShowShapes(sample, title = 'CD8', column={'ptype': 1}, **args),
pl.ShowShapes(sample, title = 'FoxP3', column={'ptype': 2}, **args),
pl.ShowShapes(sample, **annotations_kwargs),
pl.ShowShapes(sample, title = 'Tumor', column={'ptype': 3}, **args),
pl.ShowShapes(sample, title = 'CD163', column={'ptype': 4}, **args),
pl.ShowShapes(sample, title = 'CD8FoxP3', column={'ptype': 5}, **args),
columns = 4, figsize = (10, 4)
)
[66]:
<astropathdb.plotting.vizbase.SubPlots at 0x107c4cda440>
[67]:
args = {'shapes_element_id': shapes_id, 'table_name': table_id_background, 'shapes_kwargs': {'cmap':'plasma'}}
pl.SubPlots(
pl.ShowImage(sample, title = f"SampleID: {sampleid} Image"),
pl.ShowShapes(sample, title = 'PDL1', column='cmembrane520', **args),
pl.ShowShapes(sample, title = 'CD8', column='cmembrane540', **args),
pl.ShowShapes(sample, title = 'FoxP3', column='cnucleus570', **args),
pl.ShowShapes(sample, **annotations_kwargs),
pl.ShowShapes(sample, title = 'Tumor', column='ccytomembrane620', **args),
pl.ShowShapes(sample, title = 'PD1', column='cmembrane650', **args),
pl.ShowShapes(sample, title = 'CD163', column='cmembrane690', **args),
columns = 4, figsize = (10, 4)
)
[67]:
<astropathdb.plotting.vizbase.SubPlots at 0x107c408fe20>
[63]:
#
dotplot_args = {'shapes_element_id':shapes_id,'column':'ptype','shapes_kwargs' :{**cmap},'imshow_kwargs': {'alpha': 0.45}}
densityplot_args = {'shapes_element_id':shapes_id,'column':'ptype','shapes_kwargs' :{'cmap':'plasma','agg': ds.count()},'imshow_kwargs': {'alpha': 0.45}}
#
pl.SubPlots(
pl.ShowImage(sample, title = f"SampleID: {sampleid} Image"),
pl.ShowShapes(sample, title = "dotplot all cells",table_name = table_id_normal,**dotplot_args),
pl.ShowShapes(sample, title = "dotplot donuts signal",table_name = table_id,**dotplot_args),
pl.ShowShapes(sample, title = "dotplot donuts background",table_name = table_id_background,**dotplot_args),
pl.ShowShapes(sample, **annotations_kwargs),
pl.ShowShapes(sample, title = "density plot all cells", table_name = table_id_normal, **densityplot_args),
pl.ShowShapes(sample, title = "density plot donuts signal", table_name = table_id, **densityplot_args),
pl.ShowShapes(sample, title = "density plot donuts background", table_name = table_id_background, **densityplot_args),
columns = 4, figsize = (10, 4),
)
[63]:
<astropathdb.plotting.vizbase.SubPlots at 0x1082682b760>
[65]:
#
densityplot_args = {'shapes_element_id':shapes_id,'column':'ptype','shapes_kwargs' :{'cmap':'plasma','agg': ds.count()},'imshow_kwargs': {'alpha': 0.45}}
densityplot_argsd5 = {'shapes_element_id':shapes_id,'column':'d5','shapes_kwargs' :{'cmap':'plasma'},'imshow_kwargs': {'alpha': 0.45}}
#
pl.SubPlots(
pl.ShowImage(sample, title = f"SampleID: {sampleid} Image"),
pl.ShowShapes(sample, title = "density plot all cells", table_name = table_id_normal, **densityplot_args),
pl.ShowShapes(sample, title = "density plot donuts signal", table_name = table_id, **densityplot_args),
pl.ShowShapes(sample, title = "density plot donuts background", table_name = table_id_background, **densityplot_args),
pl.ShowShapes(sample, **annotations_kwargs),
pl.ShowShapes(sample, title = "D5 all cells",table_name = table_id_normal,**densityplot_argsd5),
pl.ShowShapes(sample, title = "D5 signal only", table_name = table_id, **densityplot_argsd5),
pl.ShowShapes(sample, title = "D5 background only", table_name = table_id_background, **densityplot_argsd5),
columns = 4, figsize = (10, 4),
)
[65]:
<astropathdb.plotting.vizbase.SubPlots at 0x10782827010>