### Plotting Setup Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_2.ipynb Initializes plotting parameters and potentially sets up axes for visualization. ```python ax.axis('off') ax[1, 1].set_title('error rate: {:.4f}'.format((SR_cell_type_label_vis.error == 1).sum() / SR_cell_type_label_vis.shape[0])) ax[1, 1].get_yaxis().set_visible(False) ax[1, 1].get_xaxis().set_visible(False) ax[1, 1].get_legend().remove() for i in range(x_seg.shape[0]): ax[1, 1].plot([x_seg[i] for _ in y_seg],y_seg,linestyle='dashed', c = 'salmon', lw = .2, alpha = 0.5) for i in range(y_seg.shape[0]): ax[1, 1].plot(x_seg,[y_seg[i] for _ in x_seg],linestyle='dashed', c = 'salmon', lw = .2, alpha = 0.5) ``` -------------------------------- ### Plotting Setup Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_2.ipynb Initializes plot settings and creates a figure and axes for plotting. ```python plt.rcParams.update(plt.rcParamsDefault) %matplotlib inline plt.style.use('seaborn-whitegrid') sns.set_context('paper',font_scale=2) fig, ax = plt.subplots(1,1, figsize = (20, 7), dpi = 150) plt.subplots_adjust(hspace = 0.5) ``` -------------------------------- ### Plotting Setup (3 Methods) Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_2.ipynb Initializes plot settings and creates a figure and axes for plotting when comparing three methods. ```python plt.rcParams.update(plt.rcParamsDefault) %matplotlib inline plt.style.use('seaborn-whitegrid') sns.set_context('paper',font_scale=2) fig, ax = plt.subplots(1,1, figsize = (28, 7), dpi = 150) plt.subplots_adjust(hspace = 0.5) ``` -------------------------------- ### Plotting setup Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_2.ipynb Sets up matplotlib and seaborn for plotting, including style and context. ```python plt.rcParams.update(plt.rcParamsDefault) %matplotlib inline plt.style.use('seaborn-whitegrid') sns.set_context('paper',font_scale=2) fig, ax = plt.subplots(1,1, figsize = (20, 8), dpi = 150) plt.subplots_adjust(hspace = 0.5) with plt.style.context('seaborn-whitegrid'): ``` -------------------------------- ### Directory Setup Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_6.ipynb Sets up base directories for saving data based on grid dimensions and sample UMI counts. ```python base_path = '/home/share/xwanaf/sour_sep/revision/data/RCTD_MOp_3D' if not os.path.exists(base_path): os.mkdir(base_path) base_path = os.path.join(base_path, str(num_x) + 'x' + str(num_y)) if not os.path.exists(base_path): os.mkdir(base_path) for sample_UMI in sample_UMIs: save_data_dir = os.path.join(base_path, f'{int(sample_UMI)}UMI') print(save_data_dir) if not os.path.exists(save_data_dir): os.mkdir(save_data_dir) ``` -------------------------------- ### Plotting Setup Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_1.ipynb Configures matplotlib and seaborn for plotting, setting style, context, figure size, and DPI. ```python plt.rcParams.update(plt.rcParamsDefault) %matplotlib inline plt.style.use('seaborn-whitegrid') sns.set_context('paper',font_scale=4) fig, ax = plt.subplots(1,1, figsize = (40, 8), dpi = 150) plt.subplots_adjust(hspace = 0.5) palette = ['#EB8E83'] * (len(vis_nu_list) - 1) + ['#D8A8EB'] with plt.style.context('seaborn-whitegrid'): ``` -------------------------------- ### Legend Example Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_5.ipynb Example of creating a legend for a plot. ```python legend2 = ax.legend(handles, labels, loc="upper right", title="cell number") ``` -------------------------------- ### Plotting Setup Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_6.ipynb This snippet sets up the plotting environment using matplotlib and seaborn, including style, context, figure size, and color palette. ```python plt.rcParams.update(plt.rcParamsDefault) %matplotlib inline plt.style.use('seaborn-whitegrid') # seaborn-whitegrid sns.set_context('paper',font_scale=2) fig, ax = plt.subplots(2,1, figsize = (20, 7), dpi = 150, gridspec_kw={'height_ratios': [1, 3]}) starting_point = 0 bar_width = 3 #1.5 width_between_bar = 2 #1 order = ['single slice', 'multiple slices'] hue_len = len(order) dist = bar_width * hue_len + width_between_bar palette = ['#EB8E83', '#A02439'] #* (len(vis_nu_list) - 1) + ['#D8A8EB'] ``` -------------------------------- ### RMSE Plot Setup Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_3.ipynb Sets up plotting parameters and displays a bar plot of RMSE values for different methods and nu values. ```python plt.rcParams.update(plt.rcParamsDefault) %matplotlib inline plt.style.use('seaborn-whitegrid') sns.set_context('paper',font_scale=2) fig, ax = plt.subplots(1,1, figsize = (20, 7), dpi = 150) plt.subplots_adjust(hspace = 0.5) with plt.style.context('seaborn-whitegrid'): sns.barplot(x="nu", y="value",hue='method',data=vis_decon_df[vis_decon_df.metric == 'rmse'], palette=['#EA8E83', '#AFCF78', '#8AAEC9'], ax = ax) # ax.set_ylim(0.4,0.95) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) plt.legend(bbox_to_anchor=(1,0.6)) #ax.set_yticks([0.84, 0.86, 0.88]) #ax.set_xticklabels(['130 UMIs','260 UMIs','520 UMIs']) plt.ylabel('RMSE') plt.xlabel(r'$ u$ value') plt.show() ``` -------------------------------- ### PCC Plot Setup Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_3.ipynb Sets up plotting parameters and displays a bar plot of PCC values for different methods and nu values. ```python plt.rcParams.update(plt.rcParamsDefault) %matplotlib inline plt.style.use('seaborn-whitegrid') sns.set_context('paper',font_scale=2) fig, ax = plt.subplots(1,1, figsize = (20, 7), dpi = 150) plt.subplots_adjust(hspace = 0.5) with plt.style.context('seaborn-whitegrid'): sns.barplot(x="nu", y="value",hue='method',data=vis_decon_df[vis_decon_df.metric == 'pcc'], palette=['#EA8E83', '#AFCF78', '#8AAEC9'], ax = ax) ax.set_ylim(0.7,1.02) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) plt.legend(bbox_to_anchor=(1,0.6)) #ax.set_yticks([0.84, 0.86, 0.88]) #ax.set_xticklabels(['130 UMIs','260 UMIs','520 UMIs']) plt.ylabel('PCC') plt.xlabel(r'$ u$ value') plt.show() ``` -------------------------------- ### Check Installation Status Source: https://github.com/yanglabhkust/spatialscope/blob/master/README.md Verifies the installation by running a Python script with the help flag. ```shell python ./src/Cell_Type_Identification.py -h ``` -------------------------------- ### Scatter Plot Example Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_4.ipynb Example of creating a scatter plot using seaborn. ```python ax = sns.scatterplot(data=sp_adata.obs, x="X", y="Y", s = 50, palette = "rocket_r") ``` -------------------------------- ### Installation Source: https://github.com/yanglabhkust/spatialscope/blob/master/README.md Clones the repository, sets up a conda environment, and applies a fix for squidpy. ```shell git clone https://github.com/YangLabHKUST/SpatialScope.git cd SpatialScope conda env create -f environment.yml conda activate SpatialScope # fix bug of squidpy, locate the lib with `which python` rsync ./src/_feature_mixin.py ~/.conda/envs/SpatialScope/lib/python3.9/site-packages/squidpy/im/_feature_mixin.py ``` -------------------------------- ### Import Libraries Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Mouse-MOp-MERFISH.ipynb Imports necessary Python libraries for data analysis and visualization. ```python import numpy as np import pandas as pd import pathlib import matplotlib.pyplot as plt import matplotlib as mpl import seaborn as sns import scanpy as sc import pickle import sys sys.path.append('../src') from utils import * import warnings warnings.filterwarnings('ignore') ``` -------------------------------- ### Set up base path and create directories Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_5.ipynb Sets the base path for saving data and creates necessary directories for different sample UMI counts. ```python base_path = '/home/share/xwanaf/sour_sep/revision/data/RCTDnv_STARMAP_PLUS' if not os.path.exists(base_path): os.mkdir(base_path) base_path = os.path.join(base_path, str(num_x) + 'x' + str(num_y)) if not os.path.exists(base_path): os.mkdir(base_path) for sample_UMI in sample_UMIs: save_data_dir = os.path.join(base_path, f'{int(sample_UMI)}UMI') print(save_data_dir) if not os.path.exists(save_data_dir): os.mkdir(save_data_dir) ``` -------------------------------- ### Load Initial Proportions Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Mouse-Cerebellum-Slideseq.ipynb Loads initial proportion data from a pickle file. ```python with open('../output/cere/InitProp.pickle', 'rb') as handle: InitProp = pickle.load(handle) ``` -------------------------------- ### Get shape of spatial data Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Mouse-Cerebellum-Slideseq.ipynb Prints the shape (number of cells and genes) of the spatial data object. ```python ad_sp.shape ``` -------------------------------- ### Loading Results Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_1.ipynb Loads benchmarking results from specified directories. ```bash load results from /home/share/xwanaf/sour_sep/revision/data/benchmarking_tutorial/130UMI ``` ```bash load results from /home/share/xwanaf/sour_sep/revision/data/benchmarking_tutorial/260UMI ``` ```bash load results from /home/share/xwanaf/sour_sep/revision/data/benchmarking_tutorial/520UMI ``` -------------------------------- ### Training scRNA-seq reference model Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Mouse-Brain.ipynb Command to train a scRNA-seq reference model using SpatialScope. ```bash python ./src/Train_scRef.py \ --ckpt_path ./Ckpts_scRefs/VISp \ --scRef ./Ckpts_scRefs/VISp/Ref_scRNA_VISp_qc2_2Kgenes.h5ad \ --cell_class_column cell_subclass \ --gpus 0,1,2,3 ``` -------------------------------- ### Get Unique Spatial Cell Types Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_1.ipynb Retrieves and prints the unique cell types from the spatial data. ```python np.unique(sp_adata.obs[sp_cell_class_column]) ``` -------------------------------- ### Get Unique Cell Types Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_1.ipynb Retrieves and prints the unique cell types from the single-cell reference data. ```python np.unique(sc_adata.obs[cell_class_column]) ``` -------------------------------- ### Run SpatialScope for Gene Expression Decomposition (Part 1) Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Mouse-Brain.ipynb Command to run SpatialScope for gene expression decomposition for the first spot range. ```bash python ./src/Decomposition.py --tissue cortex --out_dir ./output --SC_Data ./Ckpts_scRefs/VISp/Ref_scRNA_VISp_qc2_2Kgenes.h5ad --cell_class_column cell_subclass --ckpt_path ./Ckpts_scRefs/VISp/model_5000.pt --spot_range 0,500 --gpu 2,3,4,8 ``` -------------------------------- ### Visualize the data Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_2.ipynb Sets up plotting context and generates scatter plots for both simulated spatial data and simulated single-cell reference data, highlighting cell types. ```python sns.set_context('paper',font_scale=3) plt.rcParams["legend.markerscale"] = 3 plt.figure(figsize=(24, 12),dpi = 100) ax1 = plt.subplot(4,6,(8,14)) ax2 = plt.subplot(1,2,2) ax = [ax1, ax2] sns.scatterplot(data=sp_adata.obs, x="x", y="y", hue='cell_type_annot', s = 15, palette = color_dict, ax = ax[0], legend = False) ax[0].set_title('data used to make \n simulated spatial data') ax[0].invert_yaxis() ax[0].axis('off') # ax[0].legend(bbox_to_anchor=(1.02, 1), loc='upper left', borderaxespad=0, frameon=False) sns.scatterplot(data=sc_adata.obs, x="x", y="y", hue='cell_type_annot', s = 15, palette = color_dict, ax = ax[1]) ax[1].set_title('simulated single-cell \n reference data') ax[1].invert_yaxis() ax[1].axis('off') ax[1].legend(bbox_to_anchor=(1.02, 1), loc='upper left', borderaxespad=0, frameon=False) ``` -------------------------------- ### Figure Setup for Multiple Plots Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_5.ipynb Initializes a matplotlib figure and axes for plotting, with specific height ratios for subplots. ```python plt.rcParams.update(plt.rcParamsDefault) %matplotlib inline plt.style.use('seaborn-whitegrid') sns.set_context('paper',font_scale=2) fig, ax = plt.subplots(2,1, figsize = (20, 7), dpi = 150, gridspec_kw={'height_ratios': [4, 1]}) with plt.style.context('seaborn-whitegrid'): ``` -------------------------------- ### Run SpatialScope for Gene Expression Decomposition (Part 2) Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Mouse-Brain.ipynb Command to run SpatialScope for gene expression decomposition for the second spot range. ```bash python ./src/Decomposition.py --tissue cortex --out_dir ./output --SC_Data ./Ckpts_scRefs/VISp/Ref_scRNA_VISp_qc2_2Kgenes.h5ad --cell_class_column cell_subclass --ckpt_path ./Ckpts_scRefs/VISp/model_5000.pt --spot_range 500,1000 --gpu 2,3,4,8 ``` -------------------------------- ### Get Unique Cell Types Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_4.ipynb Retrieves and prints the unique cell type annotations present in the spatial data. ```python np.unique(sp_adata.obs['cell_type_annot']) ``` -------------------------------- ### Get Unique Values of 'nu' Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_5.ipynb Retrieves and displays the unique values present in the 'nu' column of the error rate DataFrame. ```python np.unique(error_rate_df['nu']) ``` -------------------------------- ### Data Loading and Grid Initialization Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_2.ipynb Loads spatial and single-cell reference data, defines cell class column, and sets up grid parameters. ```python sp_adata = sc.read('/home/xwanaf/bio/multiSlices_SS/mouseOB/SpatialScope/demo_data/revision/benchmarking/MERFISH_MBA_sp.h5ad') sc_adata = sc.read('/home/xwanaf/bio/multiSlices_SS/mouseOB/SpatialScope/demo_data/revision/benchmarking/MERFISH_MBA_scref.h5ad') sp_cell_class_column = cell_class_column = 'cell_type_annot' x_min = sp_adata.obs['x'].min() x_max = sp_adata.obs['x'].max() y_min = sp_adata.obs['y'].min() y_max = sp_adata.obs['y'].max() num_x = 48 num_y = 48 spatial_adata = sp_adata.copy() # make grid x_seg = np.linspace(x_min, x_max, num_x) y_seg = np.linspace(y_min, y_max, num_y) ``` -------------------------------- ### Get unique values of 'nu' column Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_4.ipynb Finds and returns the unique values present in the 'nu' column of the decon_df DataFrame. ```python np.unique(decon_df.nu) ``` -------------------------------- ### Train scRNA-seq reference Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Mouse-MOp-MERFISH.ipynb This command trains a scRNA-seq reference model using four GPUs. It specifies the checkpoint path, the reference data, the cell class column, and the GPUs to use. ```bash python ./src/Train_scRef.py \ --ckpt_path ./Ckpts_scRefs/MOp \ --scRef ./Ckpts_scRefs/MOp/Ref_snRNA_mop_qc3_2Kgenes.h5ad \ --cell_class_column subclass_label \ --gpus 0,1,2,3 ``` -------------------------------- ### Data Preparation for Visualization (3 Methods) Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_2.ipynb Prepares a DataFrame for visualizing results including RCTD, SpatialScope, and StarDist+RCTD. ```python vis_decon_df = pd.DataFrame() vis_decon_df['value'] = decon_df_temp.RCTD.tolist() + decon_df_temp.SpatialScope.tolist() + decon_df_temp.SR.tolist() vis_decon_df['method'] = ['RCTD'] * decon_df_temp.shape[0] + ['SpatialScope'] * decon_df_temp.shape[0] + ['StarDist+RCTD'] * decon_df_temp.shape[0] vis_decon_df['nu'] = decon_df.nu.tolist() * 3 vis_decon_df['metric'] = decon_df.metric.tolist() * 3 ``` -------------------------------- ### Run SpatialScope for Gene Expression Decomposition (Part 3) Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Mouse-Brain.ipynb Command to run SpatialScope for gene expression decomposition for the third spot range. ```bash python ./src/Decomposition.py --tissue cortex --out_dir ./output --SC_Data ./Ckpts_scRefs/VISp/Ref_scRNA_VISp_qc2_2Kgenes.h5ad --cell_class_column cell_subclass --ckpt_path ./Ckpts_scRefs/VISp/model_5000.pt --spot_range 1000,1715 --gpu 1,2,4,8 ``` -------------------------------- ### Iterating through Deconvolution Parameters Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_5.ipynb Python code snippet demonstrating the setup for iterating through different deconvolution parameters (nu) and repetitions (rep), including loading initial proportions. ```python nu_list = [0, 0.000001, 5] + np.arange(10,110,10).tolist() + [120, 150, 200, 500, 1000] + ['RCTD'] decon_df = pd.DataFrame() decon_pcc_df = pd.DataFrame() decon_rmse_df = pd.DataFrame() for j, nu in enumerate(nu_list): for i, rep in enumerate(range(10)): print(f"nu = {nu}; rep = {rep}") ################################################################################################################## RCTD resutls with open(os.path.join(DataDir, 'rep' + str(rep), 'InitProp.pickle'), 'rb') as handle: InitProp = pickle.load(handle) ``` -------------------------------- ### Initialization Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_6.ipynb Initializes lists and dataframes for storing results. ```python nu_list = [0, 5] + np.arange(10,110,10).tolist() + [120, 150, 200, 500, 1000] + ['RCTD'] error_rate_df = pd.DataFrame(columns = {'error_rate', 'nu', '3D', 'rep'}) decon_df = pd.DataFrame() decon_pcc_df = pd.DataFrame() decon_rmse_df = pd.DataFrame() ``` -------------------------------- ### Prepare Plotting DataFrame and Setup Plotting Environment Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_1.ipynb Creates a DataFrame 'df_plot' from the UMAP embeddings, assigns sizes and color categories based on cell type (reference, selected spatial, generated), and sets up the plotting style and parameters for a high-resolution figure. ```python df_plot = pd.DataFrame(adata_all.obsm['X_umap'], columns = ['x', 'y']) color_mu = np.ones(df_plot.shape[0]) color_mu[-(n_cells + n_cells_ss):-(n_cells_ss)] = 12 color_mu[-(n_cells_ss):] = 4 df_plot['size'] = color_mu color_mu = np.array(['All cells from the single-cell reference data ' for _ in range(df_plot.shape[0])]) color_mu[-(n_cells + n_cells_ss):-(n_cells_ss)] = 'Selected scRNA-seq Cells from spatial' color_mu[-(n_cells_ss):] = 'Generated Cells by SpatialScope' df_plot['color'] = color_mu # sns.set_context('paper',font_scale=3) # plt.rcParams["legend.markerscale"] = 3 sns.set_style('white') sns.set_context('paper',font_scale=2.6) plt.rcParams["legend.markerscale"] = 2.6 colors = ['#C3D5CB','#2ca02c','#d62728','#9467bd','#ff7f0e'] fig, ax = plt.subplots(1,1,figsize = (12,12),dpi = 150) ``` -------------------------------- ### Load and preprocess Visium data for slice 2 Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Mouse-Brain.ipynb Loads the Visium data for slice 2, applies a transformation to the expression matrix (exponential of log1p), and prepares it for analysis. ```python slice2_file = '../demo_data/Visium_MouceBrain_Cortex_section2.h5ad' slice2 = sc.read(slice2_file) slice2.X = np.exp(slice2.X.A)-1 ``` -------------------------------- ### Plotting Setup and Error Rate Bar Plot Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_1.ipynb Sets up the plotting environment using seaborn and matplotlib, then generates a bar plot for error rates. This includes setting the figure size, style, and context, and displaying a horizontal line for the mean RCTD error rate. ```python plt.rcParams.update(plt.rcParamsDefault) %matplotlib inline plt.style.use('seaborn-whitegrid') sns.set_context('paper',font_scale=2) fig, ax = plt.subplots(1,1, figsize = (24, 8), dpi = 150) plt.subplots_adjust(hspace = 0.5) with plt.style.context('seaborn-whitegrid'): # sns.barplot(x="nu", y="error_rate",hue='subsample',data=vis_df[(vis_df['correct_label'])&(vis_df['cell_num']==1)], palette="Set3", ax = ax) sns.barplot(x="nu", y="error_rate",data=error_rate_df, palette="Set3", ax = ax) ax.set_ylim(0.17,0.32) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) ax.set_xticklabels(vis_nu_list) plt.xlabel(r'$\nu$ value & StarDist + RCTD') plt.axhline(y=error_rate_df[error_rate_df['nu'] == 'RCTD'].error_rate.mean(), color='r', linestyle='--') plt.show() ``` -------------------------------- ### Import Libraries Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Mouse-Cerebellum-Slideseq.ipynb Imports necessary libraries for data analysis and visualization. ```python import numpy as np import pandas as pd import pathlib import matplotlib.pyplot as plt import matplotlib as mpl import seaborn as sns import scanpy as sc import sys sys.path.append('../src') from utils import * import pickle import warnings warnings.filterwarnings('ignore') ``` -------------------------------- ### Parameter Iteration Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_1.ipynb Iterates through different values of 'nu' and 'rep' for benchmarking. ```text nu = 0; rep = 0 nu = 0; rep = 1 nu = 0; rep = 2 nu = 0; rep = 3 nu = 0; rep = 4 nu = 0; rep = 5 nu = 0; rep = 6 nu = 0; rep = 7 nu = 0; rep = 8 nu = 0; rep = 9 nu = 5; rep = 0 nu = 5; rep = 1 nu = 5; rep = 2 nu = 5; rep = 3 nu = 5; rep = 4 nu = 5; rep = 5 nu = 5; rep = 6 nu = 5; rep = 7 nu = 5; rep = 8 nu = 5; rep = 9 nu = 10; rep = 0 nu = 10; rep = 1 nu = 10; rep = 2 nu = 10; rep = 3 nu = 10; rep = 4 nu = 10; rep = 5 nu = 10; rep = 6 nu = 10; rep = 7 nu = 10; rep = 8 nu = 10; rep = 9 nu = 20; rep = 0 nu = 20; rep = 1 nu = 20; rep = 2 nu = 20; rep = 3 nu = 20; rep = 4 nu = 20; rep = 5 nu = 20; rep = 6 nu = 20; rep = 7 nu = 20; rep = 8 nu = 20; rep = 9 nu = 30; rep = 0 nu = 30; rep = 1 nu = 30; rep = 2 nu = 30; rep = 3 nu = 30; rep = 4 nu = 30; rep = 5 nu = 30; rep = 6 nu = 30; rep = 7 nu = 30; rep = 8 nu = 30; rep = 9 nu = 40; rep = 0 nu = 40; rep = 1 nu = 40; rep = 2 nu = 40; rep = 3 nu = 40; rep = 4 nu = 40; rep = 5 nu = 40; rep = 6 nu = 40; rep = 7 nu = 40; rep = 8 nu = 40; rep = 9 nu = 50; rep = 0 nu = 50; rep = 1 nu = 50; rep = 2 nu = 50; rep = 3 nu = 50; rep = 4 nu = 50; rep = 5 nu = 50; rep = 6 nu = 50; rep = 7 nu = 50; rep = 8 nu = 50; rep = 9 nu = 60; rep = 0 nu = 60; rep = 1 nu = 60; rep = 2 nu = 60; rep = 3 nu = 60; rep = 4 nu = 60; rep = 5 nu = 60; rep = 6 nu = 60; rep = 7 nu = 60; rep = 8 nu = 60; rep = 9 nu = 70; rep = 0 nu = 70; rep = 1 nu = 70; rep = 2 nu = 70; rep = 3 nu = 70; rep = 4 nu = 70; rep = 5 nu = 70; rep = 6 nu = 70; rep = 7 nu = 70; rep = 8 nu = 70; rep = 9 nu = 80; rep = 0 nu = 80; rep = 1 nu = 80; rep = 2 nu = 80; rep = 3 nu = 80; rep = 4 nu = 80; rep = 5 nu = 80; rep = 6 nu = 80; rep = 7 nu = 80; rep = 8 nu = 80; rep = 9 nu = 90; rep = 0 nu = 90; rep = 1 nu = 90; rep = 2 nu = 90; rep = 3 nu = 90; rep = 4 nu = 90; rep = 5 nu = 90; rep = 6 nu = 90; rep = 7 nu = 90; rep = 8 nu = 90; rep = 9 nu = 100; rep = 0 nu = 100; rep = 1 nu = 100; rep = 2 nu = 100; rep = 3 nu = 100; rep = 4 nu = 100; rep = 5 nu = 100; rep = 6 nu = 100; rep = 7 nu = 100; rep = 8 nu = 100; rep = 9 nu = 120; rep = 0 nu = 120; rep = 1 nu = 120; rep = 2 nu = 120; rep = 3 nu = 120; rep = 4 nu = 120; rep = 5 nu = 120; rep = 6 nu = 120; rep = 7 nu = 120; rep = 8 nu = 120; rep = 9 nu = 150; rep = 0 nu = 150; rep = 1 nu = 150; rep = 2 nu = 150; rep = 3 nu = 150; rep = 4 nu = 150; rep = 5 nu = 150; rep = 6 nu = 150; rep = 7 nu = 150; rep = 8 nu = 150; rep = 9 nu = 200; rep = 0 nu = 200; rep = 1 nu = 200; rep = 2 nu = 200; rep = 3 nu = 200; rep = 4 nu = 200; rep = 5 nu = 200; rep = 6 nu = 200; rep = 7 nu = 200; rep = 8 nu = 200; rep = 9 nu = 500; rep = 0 nu = 500; rep = 1 nu = 500; rep = 2 nu = 500; rep = 3 nu = 500; rep = 4 nu = 500; rep = 5 nu = 500; rep = 6 nu = 500; rep = 7 nu = 500; rep = 8 nu = 500; rep = 9 nu = 1000; rep = 0 nu = 1000; rep = 1 nu = 1000; rep = 2 nu = 1000; rep = 3 nu = 1000; rep = 4 nu = 1000; rep = 5 nu = 1000; rep = 6 nu = 1000; rep = 7 nu = 1000; rep = 8 nu = 1000; rep = 9 nu = RCTD; rep = 0 nu = RCTD; rep = 1 nu = RCTD; rep = 2 nu = RCTD; rep = 3 nu = RCTD; rep = 4 nu = RCTD; rep = 5 nu = RCTD; rep = 6 nu = RCTD; rep = 7 nu = RCTD; rep = 8 nu = RCTD; rep = 9 ``` -------------------------------- ### Create spatial grid Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_2.ipynb Generates a grid of points within the spatial data boundaries and visualizes it with scatter plots and dashed lines. ```python x_min = sp_adata.obs['x'].min() x_max = sp_adata.obs['x'].max() y_min = sp_adata.obs['y'].min() y_max = sp_adata.obs['y'].max() num_x = 48 num_y = 48 spatial_adata = sp_adata.copy() # make grid x_seg = np.linspace(x_min, x_max, num_x) y_seg = np.linspace(y_min, y_max, num_y) with mpl.rc_context({'figure.figsize': (8, 8), 'figure.dpi': 150}): ax = sns.scatterplot(data=spatial_adata.obs, x="x", y="y", hue=sp_cell_class_column, s = 25, palette = color_dict, legend = False) ax.invert_yaxis() for i in range(x_seg.shape[0]): ax.plot([x_seg[i] for _ in y_seg],y_seg,linestyle='dashed', c = 'salmon', lw = 1, alpha = 0.5) for i in range(y_seg.shape[0]): ax.plot(x_seg,[y_seg[i] for _ in x_seg],linestyle='dashed', c = 'salmon', lw = 1, alpha = 0.5) # ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) ax.axis('off') ``` -------------------------------- ### Load AnnData Objects Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Mouse-Brain.ipynb Loads two .h5ad files, slice1 and slice2, which have undergone nuclei segmentation. ```python slice1 = sc.read(slice1_file) slice2 = sc.read(slice2_file) ``` -------------------------------- ### Import PASTE and load ST slices Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Mouse-Brain.ipynb Imports the PASTE library for spatial alignment and defines the file paths for two Visium mouse brain cortex slices. ```python import paste as pst from scipy.spatial.distance import pdist import matplotlib.patches as mpatches slice1_file = '../demo_data/Visium_MouseBrain_Cortex_section1.h5ad' slice2_file = '../demo_data/Visium_MouseBrain_Cortex_section2.h5ad' ``` -------------------------------- ### Load scRef and sampled pseudo-cells Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Mouse-Brain.ipynb Loads scRef data and sampled cells, sets the cell type key, and plots the data. ```python ad_sc = sc.read('../Ckpts_scRefs/VISp/Ref_scRNA_VISp_qc2_2Kgenes.h5ad') sampled_cells = sc.read('../Ckpts_scRefs/VISp/model_5000.h5ad') # 2K cells sampled from the learned gene expression distribution of scRef cell_type_key='cell_subclass' sns.set_context('paper',font_scale=1.5) adata_all = PlotSampledData(sampled_cells,ad_sc,cell_type_key,palette=color_dict) ``` -------------------------------- ### Benchmarking Loop Initialization Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_1.ipynb Initializes lists and dataframes for storing error rates and deconvolution results during the benchmarking process. ```python nu_list = [0.000001, 5] + np.arange(10,110,10).tolist() + [120, 150, 200, 500, 1000] + ['RCTD'] error_rate_df = pd.DataFrame(columns = {'error_rate', 'nu', 'rep'}) decon_df = pd.DataFrame() decon_pcc_df = pd.DataFrame() decon_rmse_df = pd.DataFrame() ``` -------------------------------- ### Display first few rows of observation data Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_2.ipynb Shows the first few rows of the observation metadata for the spatial dataset. ```python sp_adata.obs.head() ``` -------------------------------- ### Simulating Spots and Generating Data Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_1.ipynb Aggregates cells on uniform grids to simulate spots, generates fake spatial data with varying UMI counts, and filters spots with low UMI counts. It also prepares the data for saving. ```python x_min = sp_adata.obs['X'].min() x_max = sp_adata.obs['X'].max() y_min = sp_adata.obs['Y'].min() y_max = sp_adata.obs['Y'].max() num_x = 16 num_y = 58 spatial_adata = sp_adata.copy() # make grid x_seg = np.linspace(x_min, x_max, num_x) y_seg = np.linspace(y_min, y_max, num_y) with mpl.rc_context({'figure.figsize': (3, 7)}): ax = sns.scatterplot(data=spatial_adata.obs, x="X", y="Y", hue=sp_cell_class_column, s = 25, palette = color_dict, legend = False) ax.invert_yaxis() for i in range(x_seg.shape[0]): ax.plot([x_seg[i] for _ in y_seg],y_seg,linestyle='dashed', c = 'salmon', lw = .1, alpha = 0.5) for i in range(y_seg.shape[0]): ax.plot(x_seg,[y_seg[i] for _ in x_seg],linestyle='dashed', c = 'salmon', lw = .1, alpha = 0.5) # ax.legend(loc='center left', bbox_to_anchor=(1, 0.5)) ax.axis('off') for sample_UMI in [130, 260, 520]: spatial_adata = sp_adata.copy() # create fake spatial data with 'cell_locations' in uns (We assume the truth cell number in each fake spot is known in simulation study, thus skip "Nuclei segmentation" step in SpatialScope) spatial_adata = add_spot_label(spatial_adata, x_seg, y_seg) print(f'cell number range: {np.unique(generated_spot_adata.obs.cell_nums)}') generated_spot_adata = generate_spot_adata_func(spatial_adata, sample_UMI = sample_UMI) temp_obs = rename_cell_locations_obs(spatial_adata) generated_spot_adata.uns['cell_locations'] = temp_obs # filter spot that UMI counts smaller than 20 generated_spot_adata_filtered = generated_spot_adata.copy()[~(generated_spot_adata.X.sum(1) < 20)] index = ~generated_spot_adata.uns['cell_locations']['spot_index_int'].isin(np.where(generated_spot_adata.X.sum(1) < 20)[0]) generated_spot_adata_filtered.uns['cell_locations'] = generated_spot_adata.uns['cell_locations'][index].copy() # save data save_data_dir = os.path.join(SpatialScope_base_path, f'demo_data/simulation/{int(sample_UMI)}UMI') ## spatial generated_spot_adata_filtered.obs = generated_spot_adata_filtered.obs.rename(columns = {'X':'x', 'Y':'y'}) generated_spot_adata_filtered.obsm['spatial'] = generated_spot_adata_filtered.obs[['x','y']].values cell_locations = generated_spot_adata_filtered.uns['cell_locations'].copy() cell_locations = cell_locations.rename(columns = {'X':'x', 'Y':'y'}) generated_spot_adata_filtered.uns['cell_locations'] = cell_locations generated_spot_adata_filtered.obs_names_make_unique() generated_spot_adata_filtered.var_names_make_unique() save_sp_dir = os.path.join(save_data_dir, 'spatial.h5ad') print(f'saved simulated spatial data of subsample {sample_UMI} UMI in {save_sp_dir}') ``` -------------------------------- ### Data Preparation for Visualization Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_2.ipynb Prepares a DataFrame for visualizing deconvoluted cell type proportions, combining results from different methods. ```python temp = decon_df_temp.copy() temp = temp[(temp.nu == 0)] temp['nu'] = [str(0.000001)] * temp.shape[0] temp['SpatialScope'] = small_nv_decon_df.SpatialScope.tolist() temp = pd.concat([temp, decon_df_temp.copy()], axis = 0) temp ``` -------------------------------- ### StarDist + RCTD Comparison Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_1.ipynb This code snippet shows how to run the StarDist + RCTD method for comparison, iterating through different repetitions. ```shell for rep in {0..9} do python ./compared_methods/SDRCTD.py \ --out_dir ./output/RCTDnv_MOp/260UMI/rep$rep \ --ST_Data ./demo_data/simulation/260UMI/spatial.h5ad \ --SC_Data ./Ckpts_scRefs/MERFISH_MOp_4Kcells/MERFISH_MOp_4Kcells.h5ad \ --cell_class_column subclass \ --cell_num_column cell_nums \ --hs_ST \ done ``` -------------------------------- ### Initialization and Data Loading Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_6.ipynb Initializes nu_list and loads decon_df, decon_pcc_df, and decon_rmse_df from CSV files. It then iterates through nu_list and repetitions. ```python nu_list = [0] decon_df = pd.read_csv(os.path.join(DataDir, 'decon_df.csv'), index_col = 0) decon_pcc_df = pd.DataFrame() decon_rmse_df = pd.DataFrame() for j, nu in enumerate(nu_list): for i, rep in enumerate(range(10)): print(f"nu = {nu}; rep = {rep}") ################################################################################################################## RCTD resutls with open(os.path.join(DataDir, neighbor_range, 'rep' + str(rep), 'InitProp.pickle'), 'rb') as handle: InitProp = pickle.load(handle) RCTD_results = (InitProp['results'] / np.array(InitProp['results'].sum(1)))[:, None]) RCTD_results = RCTD_results.loc[:, np.unique(RCTD_results.columns)] ################################################################################################################## StarDist + RCTD resutls SR_results = sc.read(os.path.join(DataDir, neighbor_range, 'rep' + str(rep), 'SDRCTD_results/single_cell_type_label_bySDRCTD.h5ad')) SR_cell_type_label = SR_results.uns['cell_locations'].copy() prop = label_to_matrix(np.array(SR_cell_type_label['spot_index_int']), np.array(SR_cell_type_label['SDRCTD_cell_type'])) prop = prop/prop.sum(1)[:,None] SR_results = prop SR_results.index = np.arange(len(SR_results)) SR_results = SR_results.loc[:,np.unique(SR_results.columns)] ################################################################################################################## SpatialScope_3D resuslts # palette = ['#F0EFF6', '#D0E2F8', '#c44e52'] cell_locations = pd.read_csv(os.path.join(DataDir, neighbor_range, 'rep' + str(rep), 'CellTypeLabel_nu' + str(nu) + '.csv'), index_col = 0) prop = label_to_matrix(np.array(cell_locations['spot_index_int']), np.array(cell_locations['discrete_label_ct'])) prop = prop/prop.sum(1)[:,None] SpatialScope_results = prop SpatialScope_results.index = np.arange(len(RCTD_results)) SpatialScope_results = SpatialScope_results.loc[:,np.unique(SpatialScope_results.columns)] print(os.path.join(DataDir, neighbor_range, 'rep' + str(rep), 'SpatialScope_result'+ '_nu' + str(nu) +'.txt')) SpatialScope_results.to_csv(os.path.join(DataDir, neighbor_range, 'rep' + str(rep), 'SpatialScope_result'+ '_nu' + str(nu) +'.txt')) ################################################################################################################## SpatialScope resuslts # palette = ['#F0EFF6', '#D0E2F8', '#c44e52'] cell_locations = pd.read_csv(os.path.join(DataDir, '3D_single', neighbor_range, 'rep' + str(rep), 'CellTypeLabel_nu' + str(nu) + '.csv'), index_col = 0) prop = label_to_matrix(np.array(cell_locations['spot_index_int']), np.array(cell_locations['discrete_label_ct'])) prop = prop/prop.sum(1)[:,None] SpatialScope_3D_single_results = prop SpatialScope_3D_single_results.index = np.arange(len(SpatialScope_3D_single_results)) SpatialScope_3D_single_results = SpatialScope_3D_single_results.loc[:,np.unique(SpatialScope_3D_single_results.columns)] ################################################################################################################## RCTD_results = RCTD_results.loc[:,gd_results.columns] SR_results = SR_results.loc[:,gd_results.columns] SpatialScope_results = SpatialScope_results.loc[:,gd_results.columns] SpatialScope_3D_single_results = SpatialScope_3D_single_results.loc[:,gd_results.columns] starmap_spots_pcc = compare_results( gd_results, [RCTD_results,SR_results, SpatialScope_3D_single_results, SpatialScope_results], columns = ['RCTD', 'SR', 'SpatialScope', 'SpatialScope_3D'], axis=0, metric='pcc' ) starmap_spots_rmse = compare_results( gd_results, [RCTD_results,SR_results, SpatialScope_3D_single_results, SpatialScope_results], columns = ['RCTD', 'SR', 'SpatialScope', 'SpatialScope_3D'], axis=0, metric='rmse' ) starmap_spots_pcc['nu'] = [int(nu)] * starmap_spots_pcc.shape[0] starmap_spots_pcc['metric'] = ['pcc'] * starmap_spots_pcc.shape[0] starmap_spots_rmse['nu'] = [int(nu)] * starmap_spots_rmse.shape[0] starmap_spots_rmse['metric'] = ['rmse'] * starmap_spots_rmse.shape[0] starmap_spots_pcc['cell_nums'] = starmap_spots_rmse['cell_nums'] = sp_adata.obs.cell_nums.tolist() decon_df = pd.concat([decon_df, starmap_spots_pcc, starmap_spots_rmse], axis = 0) decon_pcc_df = pd.concat([decon_pcc_df, starmap_spots_pcc], axis = 0) decon_rmse_df = pd.concat([decon_rmse_df, starmap_spots_rmse], axis = 0) ``` -------------------------------- ### Load Spatial Datasets Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Mouse-Brain.ipynb Loads two sections of Visium Mouse Brain Cortex data using scanpy. ```python slice1_file = '../demo_data/Visium_MouseBrain_Cortex_section1.h5ad' slice2_file = '../demo_data/Visium_MouseBrain_Cortex_section2.h5ad' slice1 = sc.read(slice1_file) slice2 = sc.read(slice2_file) ``` -------------------------------- ### Displaying DataFrame Source: https://github.com/yanglabhkust/spatialscope/blob/master/demos/Benchmarking-Dataset_2.ipynb Displays the content of the 'test_df' DataFrame. ```python test_df ```