PlexoraViewer
A running Plexora viewer, returned by plexora.view().
from plexora import PlexoraViewerPlexoraViewer(
datasource,
data_dir=None,
proxy='auto',
height=850,
width='100%',
base_url=None,
plugins=None,
tool=None,
overlay=None,
channels=None,
memory=False,
start=True,
)Displaying it -- as a Jupyter cell's last expression, or by calling
.iframe() or .open() -- starts a sidecar Plexora server (or reuses one
already running for this project) and shows the viewer. In local Jupyter,
JupyterLab and VS Code it renders as an iframe pointing at 127.0.0.1; on
JupyterHub, Open OnDemand and Colab it is proxied through the notebook
server's own origin instead, since a direct localhost URL would point at
the browser's machine rather than the kernel's.
When to use
Constructed directly when a project already exists and only the
display options need choosing. To register data and open a viewer on
it in one call, use plexora.view() or the from_files,
from_anndata and from_memory classmethods instead.
Constructor parameters
datasourcestrrequiredThe project's name -- what appears in the URL and
what .refresh() reloads.
data_dirstr | Pathdefault NoneWhere this project's data lives.
Defaults to PLEXORA_DATA_PATH, or else the platform data
directory.
proxybool | strdefault 'auto'How to build the viewer's URL.
"auto" (the default) inspects the environment: local Jupyter
and VS Code Remote get a direct 127.0.0.1 URL, while
JupyterHub, Open OnDemand and Colab get the proxied form each of
them needs. True always proxies through the notebook server;
False always uses a direct URL, which only works when the
browser and the kernel are on the same machine.
heightintdefault 850The iframe's height in pixels. Defaults to
850.
widthint | strdefault '100%'The iframe's width, as a CSS size.
Defaults to "100%".
base_urlstrdefault NoneThe notebook server's own base URL (or a
full origin), for the rare case proxy="auto" guesses wrong.
Passing this always proxies under it.
pluginsstrdefault NoneA comma-separated allowlist of plugins to
load in the sidecar it starts, or "" for core only. Leaving it
unset loads whatever is installed.
toolstrdefault NoneThe plugin panel this viewer should already have open when it appears.
overlaystrdefault NoneThe metadata column to draw over the cells when this viewer appears.
channelslist | dictdefault NoneThe image channels this viewer
should turn on: a list of channel names, or a mapping of name to
{"color": "#rrggbb", "range": (low, high)}.
memorybooldefault FalseWhether this project reads its data out of
this kernel's memory, which .refresh() needs to know. Set for
you by from_anndata and from_memory. Defaults to False.
startbooldefault TrueWhether to start (or reuse) the sidecar
server immediately. Defaults to True; pass False to construct
the viewer without a server running yet and call .start()
later.
Raises
ServerStartErrorIf start is true and the sidecar server fails to
come up.
Properties
url
urlThe viewer's address, as a string, including its token and launch state.
Starts the sidecar (or confirms the one already running) if this
viewer has not started yet, then returns the exact URL used
everywhere the viewer is shown: the iframe src, .open(), and the
printed fallback for a hosted notebook. Only this entry URL carries
the token -- the server trades it for a scoped cookie on the first
request, so nothing else needs to carry it.
ServerStartErrorIf the sidecar server fails to come up.
RuntimeErrorOn Colab, if the notebook frontend has not answered
which public URL proxies this port. Call .iframe() instead,
which does not need that answer, or re-run this cell on its
own.
Methods
from_files
@classmethod
from_files(
name,
image,
segmentation,
features,
x,
y,
id_column='CellID',
celltype_column=None,
channel_names=None,
copy=False,
data_dir=None,
**viewer_kwargs,
)Register a project from files on disk, then open a viewer on it.
A thin wrapper around plexora.register_datasource followed by the
constructor. Everything not listed under Args is forwarded to
PlexoraViewer.__init__ as **viewer_kwargs.
namestrrequiredThe project's name.
imagestr | PathrequiredThe whole-slide image.
segmentationstr | PathrequiredThe segmentation mask outlining cells.
featuresstr | PathrequiredThe cell table (CSV), one row per cell.
xstrrequiredThe features column holding each cell's X coordinate.
ystrrequiredThe features column holding each cell's Y coordinate.
id_columnstrdefault 'CellID'The features column holding each
cell's unique ID. Defaults to "CellID".
celltype_columnstrdefault NoneA features column that already
holds a cell type or phenotype label.
channel_nameslistdefault NoneA name for each image channel, in channel order. Defaults to names derived from the image or the table.
copybooldefault FalseWhether to copy image, segmentation and
features into the project's own directory rather than read
them from where they already are. Defaults to False.
data_dirstr | Pathdefault NoneWhere to create the project.
Defaults to PLEXORA_DATA_PATH, or else the platform data
directory.
**viewer_kwargsPassed on to PlexoraViewer.
PlexoraViewerA viewer on the newly registered project.
import plexora
viewer = plexora.PlexoraViewer.from_files(
"tonsil", image="slide.ome.tif", segmentation="mask.tif",
features="cells.csv", x="X_centroid", y="Y_centroid")from_anndata
@classmethod
from_anndata(
name,
image,
features=None,
adata=None,
segmentation=None,
coordinate_source=None,
obsm_key=None,
x=None,
y=None,
feature_source='X',
layer=None,
feature_obs_columns=None,
obs_id_field=None,
celltype_column=None,
subset_by=None,
subset_value=None,
channel_names=None,
copy=False,
data_dir=None,
to_disk=False,
**viewer_kwargs,
)Register an AnnData-backed project, then open a viewer on it.
Give either features (a path to an existing .h5ad) or adata (a
live AnnData object held in this kernel), not both. A live object is
served directly from this kernel's memory rather than written to disk
first, so the loop this is for -- annotate, look, annotate again --
costs no disk write and no re-import, and viewer.refresh(adata)
picks up the next edit. Pass to_disk=True to write it to
<data_dir>/<name>.h5ad and read that instead -- worth it when the
project should outlive this kernel, or the table is large enough that
a second copy in memory is the wrong trade.
coordinate_source, obsm_key, x, y, feature_source and
layer say where in the AnnData to find coordinates and feature
values; leaving them unset auto-detects adata.obsm["spatial"] for
coordinates and reads features from adata.X.
namestrrequiredThe project's name.
imagestr | PathrequiredThe whole-slide image.
featuresstr | Pathdefault NoneAn existing .h5ad file. Give
this or adata, not both.
adataAnnDatadefault NoneA live AnnData object held in this
kernel. Give this or features, not both.
segmentationstr | Pathdefault NoneThe segmentation mask outlining cells.
coordinate_sourcestrdefault NoneWhere cell coordinates come
from: "obsm" or "obs". Defaults to auto-detecting
adata.obsm["spatial"].
obsm_keystrdefault NoneThe adata.obsm key holding
coordinates, when coordinate_source="obsm". Defaults to
"spatial".
xstrdefault NoneThe adata.obs column holding each cell's X
coordinate, when coordinate_source="obs".
ystrdefault NoneThe adata.obs column holding each cell's Y
coordinate, when coordinate_source="obs".
feature_sourcestrdefault 'X'Where marker values come from:
"X" (the default), "obs", or "layer".
layerstrdefault NoneThe adata.layers key to read features
from, when feature_source="layer".
feature_obs_columnslistdefault NoneThe adata.obs columns to
treat as features, when feature_source="obs".
obs_id_fieldstrdefault NoneThe adata.obs column to use as
each cell's ID. Defaults to adata's own index.
celltype_columnstrdefault NoneAn adata.obs column that
already holds a cell type or phenotype label.
subset_bystrdefault NoneAn adata.obs column used to select
one image's cells out of an AnnData covering several.
subset_valuedefault NoneThe value of subset_by that identifies
this image's cells.
channel_nameslistdefault NoneA name for each image channel, in
channel order. Defaults to names derived from the image or
from adata.
copybooldefault FalseWhether to copy image and segmentation
into the project's own directory. Defaults to False.
data_dirstr | Pathdefault NoneWhere to create the project.
Defaults to PLEXORA_DATA_PATH, or else the platform data
directory.
to_diskbooldefault FalseWrite a live adata to .h5ad and read
that instead of serving it from memory. Defaults to False.
**viewer_kwargsPassed on to PlexoraViewer.
PlexoraViewerA viewer on the newly registered project.
import plexora
viewer = plexora.PlexoraViewer.from_anndata(
"tonsil", image="slide.ome.tif", adata=adata)from_memory
@classmethod
from_memory(
name,
image=None,
*,
data_dir=None,
tool=None,
overlay=None,
channels=None,
proxy='auto',
height=850,
width='100%',
base_url=None,
plugins=None,
start=True,
**data_kwargs,
)Register a project from objects this kernel is holding, then open a viewer on it.
plexora.view(name, adata=..., ...) dispatches here. Every data
argument in **data_kwargs -- adata, table, segmentation,
sdata and the rest that plexora.register_memory_datasource
accepts -- is snapshotted and served from this kernel rather than
written to disk; image may be a path (read from disk as usual) or
an in-memory array. Calling this again with the same name replaces
the snapshots and re-reads the table, which is what makes the
notebook loop -- annotate, call again, look -- cheap.
When to use. When the data to display lives in this kernel's variables rather
than in files. For a project already saved to disk, construct
PlexoraViewer directly or call plexora.view(name).
namestrrequiredThe project's name.
imagestr | Pathdefault NoneThe whole-slide image, as a path or an in-memory array.
data_dirstr | Pathdefault NoneWhere this project's other state
(config, ROIs, figures) lives. Defaults to
PLEXORA_DATA_PATH, or else the platform data directory.
toolstrdefault NoneThe plugin panel this viewer should already have open when it appears.
overlaystrdefault NoneThe metadata column to draw over the cells when this viewer appears.
channelslist | dictdefault NoneThe image channels this viewer should turn on.
proxybool | strdefault 'auto'How to build the viewer's URL. See
PlexoraViewer. Defaults to "auto".
heightintdefault 850The iframe's height in pixels. Defaults
to 850.
widthint | strdefault '100%'The iframe's width, as a CSS size.
Defaults to "100%".
base_urlstrdefault NoneThe notebook server's own base URL, for
the rare case proxy="auto" guesses wrong.
pluginsstrdefault NoneA comma-separated allowlist of plugins to load in the sidecar it starts.
startbooldefault TrueWhether to start (or reuse) the sidecar
immediately. Defaults to True.
**data_kwargsThe data to serve -- adata, table,
segmentation and the rest of
plexora.register_memory_datasource's arguments.
PlexoraViewerA viewer on the newly registered project.
import plexora
viewer = plexora.PlexoraViewer.from_memory(
"tonsil", image="slide.ome.tif", adata=adata)start
start()Start the sidecar server this viewer needs, or reuse one already running.
Called automatically by the constructor when start=True (the
default), so calling it directly is only needed after constructing
with start=False. Displaying the viewer, .url and .open() all
call it too, so it never needs to be called more than once by hand.
intThe port the sidecar is listening on.
ServerStartErrorIf the sidecar server fails to come up.
refresh
refresh(adata=None, table=None, image=None, segmentation=None, **data_kwargs)Push the next round of annotation to a viewer serving in-memory data.
The second half of the notebook loop this viewer is for: annotate,
refresh(), look. Whatever is passed here is re-snapshotted and
re-described -- so an obs column that did not exist a cell ago
reaches the project's vocabulary and can be chosen as an overlay --
and whatever is left as None is left alone, tiles and all.
Only works for a project registered from memory (plexora.view(..., adata=...), or from_anndata/from_memory); a project that reads
its data from files has nothing here to refresh from.
adataAnnDatadefault NoneThe updated AnnData object.
tabledefault NoneThe updated cell table, for a project
registered with table= rather than adata=.
imagedefault NoneThe updated image, if it has changed too.
segmentationdefault NoneThe updated segmentation mask, if it has changed too.
**data_kwargsPassed on to plexora.register_memory_datasource.
PlexoraViewerThis viewer, so a cell can end with
viewer.refresh(adata) and redisplay it.
RuntimeErrorIf this project reads its data from files rather than from this kernel's memory.
iframe
iframe()Return the viewer's iframe for display, starting the server first.
Equivalent to letting the viewer display itself as a cell's last
expression, except the result can be handed to
IPython.display.display() explicitly, and it is the reliable way to
show a viewer on Colab, where a bare cell expression cannot resolve
the port under "Run all" or after a reconnect.
IPython.display.HTML | strThe iframe, ready to display -- an
HTML object where IPython is installed, otherwise the raw HTML
string. On Colab the iframe is displayed directly through
Colab's own helper instead, and this returns whatever that
helper returns.
ServerStartErrorIf the sidecar server fails to come up.
open
open()Open the viewer in a new browser tab, when that is possible from here.
Works for a direct 127.0.0.1 URL -- ordinary local Jupyter and VS
Code. For a hosted notebook (JupyterHub, Open OnDemand, Colab) the
URL is a path on the notebook server's own origin rather than a full
address, so it is printed instead of being opened.
strThe viewer's URL, whether or not a browser was actually opened.
ServerStartErrorIf the sidecar server fails to come up.
Examples
import plexora
viewer = plexora.PlexoraViewer("tonsil", tool="cell_explorer",
overlay="leiden")
viewer # displays the iframe as the cell's last expressionNotes
tool, overlay and channels are ephemeral: they belong to this
one viewer and are never written to the project, so a notebook that
opens the same project with a different overlay every cell does not
overwrite what was last set up by hand in the browser. The project's
saved channels and saved overlay are still exactly what a plain
plexora.view(name) restores.
Related
view
Open a notebook-displayable viewer on datasource.
ServerStartError
The sidecar Plexora server a viewer needs failed to start.