plotting#
- scportrait.plotting.add_scalebar(ax: Axes, resolution: float, resolution_unit: str = 'um', location: str = 'lower right', color: str = 'white') None #
Add a scalebar to an axis.
- Parameters:
ax – The axis to add the scalebar to.
resolution – The resolution of the image.
resolution_unit – The unit of the resolution.
location – The location of the scalebar.
color – The color of the scalebar.
- scportrait.plotting.cell_grid(adata: AnnData, select_channel: int | None | list[int] = None, n_cells: int | None = None, cell_ids: int | list[int] | None = None, show_cell_id: bool = True, cmap='viridis', return_fig: bool = False, show_fig: bool = True)#
Visualize single-cell images of cells in an AnnData object.
Uses either cell_grid_single_channel or cell_grid_multi_channel depending on the input. Use these functions if you would like more control over the visualization.
- Parameters:
adata – An scPortrait single-cell image dataset.
select_channel – The channel to visualize. Can be either a single int value or a list of values. If None all channels will be visualized.
n_cells – The number of cells to visualize. This number of cells will randomly be selected. If set to None, cell_ids must be provided.
cell_ids – cell IDs of the cells that are to be visualiazed. If None, n_cells must be provided.
show_cell_ids – Whether to show the cell ID as titles/y-labels for each single-cell image.
cmap – The colormap to use for the images.
return_fig – If True, the function returns the figure object instead of displaying it.
show_fig – If True, the figure is displayed.
- Returns:
If return_fig=True, the figure object is returned. Otherwise, the figure is displayed.
- scportrait.plotting.cell_grid_multi_channel(adata, n_cells: int = 5, cell_ids: int | list[int] | None = None, select_channels: list[int] | None = None, title: str | None = None, show_cell_id: bool = True, label_channels: bool = True, row_labels: list[str] | None = None, cmap='viridis', spacing: float = 0.025, single_cell_size: int = 2, ax: Axes = None, return_fig: bool = False, show_fig: bool = True) None | Figure #
Plot a grid of single-cell images where each row is a unique cell and each column contains a different channel.
- Parameters:
adata – An scPortrait single-cell image dataset.
n_cells – The number of cells to visualize. This number of cells will randomly be selected. If None, cell_ids must be provided.
cell_ids – cell IDs for the specific cells that should be visualized. If None, n_cells must be provided.
select_channels – The channels to visualize. Channels are identified as int values corresponding to their order in the h5sc dataset. If None, all channels will be visualized.
title – The title of the plot.
show_cell_id – Whether to show the cell ID as row label for each cell in the image grid.
label_channels – Whether to show the channel names as titles for column in the image grid.
row_labels – can override the labels plotted on the y-axis on the first element of each row. If using not compatible with passing show_cell_id as True.
cmap – The colormap to use for the images.
spacing – The spacing between cells in the grid expressed as fraction of the cell image size.
single_cell_size – The size of each cell in the grid.
ax – The matplotlib axes object to plot on. If None, a new figure is created.
return_fig – If True, the function returns the figure object instead of displaying it.
show_fig – If True, the figure is displayed.
- Returns:
If return_fig=True, the figure object is returned. Otherwise, the figure is displayed.
- scportrait.plotting.cell_grid_single_channel(adata, select_channel: int | str, n_cells: int = 16, cell_ids: int | list[int] | None = None, cell_labels: list[str] | None = None, show_cell_id: bool = False, title: str | None = None, show_title: bool = True, cmap='viridis', ncols: int | None = None, nrows: int | None = None, single_cell_size: int = 2, spacing: float = 0.025, ax: Axes = None, return_fig: bool = False, show_fig: bool = True) None | Figure #
Visualize a single channel from h5sc object in a grid.
- Parameters:
adata – An scPortrait single-cell image dataset.
select_channel – The channel to visualize.
n_cells – The number of cells to visualize. This number of cells will randomly be selected. If None, cell_ids must be provided.
cell_ids – cell IDs for the specific cells that should be visualized. If None, n_cells must be provided.
cell_labels – Label to plot as title for each single-cell image if provided.
show_cell_id – Whether to show the cell ID as title for each single-cell image. Can not be used together with cell_labels.
title – The title of the plot.
show_title – Whether to show the title.
cmap – The colormap to use for the images.
ncols – The number of columns in the grid. If not specified will be automatically calculated to make a square grid.
nrows – The number of rows in the grid. If not specified will be automatically calculated to make a square grid.
single_cell_size – The size of each cell in the grid.
spacing – The spacing between cells in the grid expressed as fraction of the cell image size.
ax – The matplotlib axes object to plot on. If None, a new figure is created.
return_fig – If True, the function returns the figure object instead of displaying it.
show_fig – If True, the figure is displayed.
- Returns:
If return_fig=True, the figure object is returned. Otherwise, the figure is displayed.
- scportrait.plotting.plot_segmentation_mask(sdata: SpatialData, masks: list[str], background_image: str | None = 'input_image', selected_channels: int | list[int] | None = None, max_channels_to_plot: int = 4, title: str | None = None, title_fontsize: int = 20, line_width: int = 1, line_color: str = 'white', line_alpha: float = 1, fill_alpha: float = 0, fill_color: str | None = None, dpi: int | None = None, ax: Axes | None = None, return_fig: bool = False, show_fig: bool = True) Figure | None #
Visualize segmentation masks over selected background image. Will transform the provided labels layers to polygons and plot them over the background image. Requires an installed spatialdata_plot package.
- Parameters:
sdata – SpatialData object containing the input image and segmentation mask.
masks – List of keys identifying the segmentation masks to plot.
background_image – Key identifying the background image to plot the segmentation masks on. Defaults to “input_image”. If set to None then only the segmentation masks will be plotted as outlines.
selected_channels – List of indices of the channels in the background image to plot. If None then the first max_channels_to_plot channels will be plotted. Defaults to None.
title – Title of the plot. Defaults to None.
title_fontsize – Font size of the title in points.
line_width – Width of the outline of the segmentation masks.
line_color – Color of the outline of the segmentation masks.
line_alpha – Alpha value of the outline of the segmentation masks.
fill_alpha – Alpha value of the fill of the segmentation masks.
fill_color – Color of the fill of the segmentation masks. If None then no fill will be used. Defaults to None.
dpi – Dots per inch of the plot. Defaults to None.
axs – Matplotlib axis object to plot on. Defaults to None.
return_fig – Whether to return the figure. Defaults to False.
show_fig – Whether to show the figure. Defaults to True.
- scportrait.plotting.plot_image(sdata: SpatialData, image_name: str, channel_names: list[str] | list[int] | None = None, palette: list[str] | None = None, title: str | None = None, title_fontsize: int = 20, dpi: int | None = None, ax: Axes = None, return_fig: bool = False, show_fig: bool = True) Figure | None #
Plot the image with the given name from the spatialdata object.
- Parameters:
sdata – SpatialData object containing the image.
image_name – Name of the image to plot.
channel_names – List of channel names to plot. If None then all channels will be plotted. Defaults to None.
palette – List of colors to use for the channels. If None then the default palette will be used. Defaults to None.
title – Title of the plot. Defaults to None.
title_fontsize – Font size of the title in points. Defaults to 20.
dpi – Dots per inch of the plot. Defaults to None.
ax – Matplotlib axis object to plot on. Defaults to None.
return_fig – Whether to return the figure. Defaults to False.
show_fig – Whether to show the figure. Defaults to True.
- Returns:
Matplotlib figure object if return_fig is True.
- scportrait.plotting.plot_labels(sdata: SpatialData, label_layer: str, title: str | None = None, title_fontsize: int = 20, color: str = 'grey', fill_alpha: float = 1, cmap: str = None, vectorized: bool = False, dpi: int | None = None, ax: Axes | None = None, return_fig: bool = False, show_fig: bool = True) Figure | None #
Plot the segmentation mask on the input image.
- Parameters:
sdata – SpatialData object containing the input image and segmentation mask.
labels_layer – Key identifying the label layer to plot.
title – Title of the plot.
title_fontsize – Font size of the title in points.
color – color to plot the label layer in. Can be a string specifying a specific color or linking to a column in an annotating table.
fill_alpha – Alpha value of the fill of the segmentation masks.
cmap – Colormap to use for the labels.
vectorized – Whether to plot a vectorized version of the labels.
dpi – Dots per inch of the plot.
axs – Matplotlib axis object to plot on.
return_fig – Whether to return the figure.
show_fig – Whether to show the figure.
- Returns:
Matplotlib figure object if return_fig is True.
- scportrait.plotting.generate_composite(images: ndarray, colors: list[tuple[int, ...]] = None, plot: bool = False)#
Create a composite image from a multi-channel image for visualization.
- Parameters:
images (np.ndarray) – A multi-channel image to be combined.
colors (list[tuple[int, ...]], optional) – A list of colors to use for each channel. Defaults to None.
plot (bool, optional) – Whether to plot the composite image. Defaults to False.
- Returns:
The composite image.
- Return type:
np.ndarray