Animation

import unist
import scanpy as sc

The datasets used for this tutorial is the reconstruction of human metastatic lymph node and can be downloaded at:

https://drive.google.com/file/d/1FtQ-yUSH418SYTPFRgR-ONolvwmYXgJh/view?usp=sharing

## read in data
adata = sc.read("/Users/shuilan/Library/CloudStorage/OneDrive-InsideMDAnderson/3D spatial transcriptomics/data/3D/OpenST_predicted.h5ad")
adata
AnnData object with n_obs × n_vars = 831406 × 0
    obs: 'pred_label'
    obsm: 'spatial'
from unist.downstream.vis import three_d_plot, construct_pc, rotate_model
anno_colors = {
    'T_cell': '#E64B35',
    'Cytotoxic_IFN_signaling': '#D73027',
    'Dendritic_cells': '#F0E442',
    'Macrophages': '#A6CEE3',
    'M1_macrophages': '#1F78B4',
    'Mast_cells': '#FF7F00',
    'Germinal_Center_Plasma_IgM_B_cell': '#FDB462',
    'Plasma_IgA': '#B3DE69',
    'Plasma_IgG': '#33A02C',
    'Fibroblasts': '#CAB2D6',
    'CAF': '#6A3D9A',
    'CAM': '#8E0152',
    'High_Endothelial_Venules': '#4575B4',
    'Cortex_CCL21': '#91CF60',
    'Tumor': '#999999',
    'Tumor_Keratin_Pearl': '#252525'
}
adata = adata[adata.obs["pred_label"] != "unknown"].copy()
pc_model, plot_cmap = construct_pc(
    adata=adata.copy(),
    spatial_key="spatial",
    groupby="pred_label",
    key_added="pred_label",
    colormap=anno_colors
)
rotate_model(model=pc_model, angle=(90, 0, 0), inplace=True)
rotate_model(model=pc_model, angle=(0, 90, 0), inplace=True)
import os
from pathlib import Path
os.chdir(f"/Users/shuilan/Library/CloudStorage/OneDrive-InsideMDAnderson/3D spatial transcriptomics/data")
out_image_path = f"animations"
Path(out_image_path).mkdir(parents=True, exist_ok=True)
! pip install imageio[ffmpeg]
Requirement already satisfied: imageio[ffmpeg] in /opt/anaconda3/envs/unist/lib/python3.10/site-packages (2.37.2)
Requirement already satisfied: numpy in /opt/anaconda3/envs/unist/lib/python3.10/site-packages (from imageio[ffmpeg]) (1.26.4)
Requirement already satisfied: pillow>=8.3.2 in /opt/anaconda3/envs/unist/lib/python3.10/site-packages (from imageio[ffmpeg]) (12.1.1)
Collecting imageio-ffmpeg (from imageio[ffmpeg])
  Downloading imageio_ffmpeg-0.6.0-py3-none-macosx_10_9_intel.macosx_10_9_x86_64.whl.metadata (1.5 kB)
Requirement already satisfied: psutil in /opt/anaconda3/envs/unist/lib/python3.10/site-packages (from imageio[ffmpeg]) (7.2.2)
Downloading imageio_ffmpeg-0.6.0-py3-none-macosx_10_9_intel.macosx_10_9_x86_64.whl (24.9 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 24.9/24.9 MB 26.7 MB/s  0:00:00 eta 0:00:01
?25hInstalling collected packages: imageio-ffmpeg
Successfully installed imageio-ffmpeg-0.6.0
plot_text = 'PC model for metastasis lymph node'

three_d_plot(
    pc_model,
    key="pred_label",
    model_size=4,
    filename=os.path.join(out_image_path, f"whole.mp4"),
    window_size=(1500, 1500),
    view_up=(0, 0, 1),
    cpo="yz",
    show_legend=False,
    off_screen=False,
    model_style="points",
    opacity=1,
    jupyter="static",
    background="white",
    framerate=10,
    pad_ratio=0.3,
    text=plot_text,
    text_kwargs={
        "font_size": 40,
        "font_family": "arial",
        "color": "black",
        "position": "upper_left"}
)

Animation Demo