Skip to content

spatial_pscore

Short Description

sm.pl.spatial_pscore: This function offers a visual representation of proximity volume and density scores, essential for understanding the spatial relationships and interactions among cell types or phenotypes within tissue samples. To ensure accurate and meaningful visualizations, it is crucial to compute these scores beforehand using sm.tl.spatial_pscore. Through customizable bar plots, users can delve into the intricacies of spatial co-occurrence patterns, facilitating deeper insights into the cellular microenvironment.

Function

spatial_pscore(adata, label='spatial_pscore', plot_score='both', order_xaxis=None, color='grey', figsize=None, **kwargs)

Parameters:

Name Type Description Default
adata AnnData

The annotated data matrix with spatial proximity scores.

required
label str

The label/key used to access the spatial proximity scores stored in adata.uns. This should match the label used during the sm.tl.spatial_pscore computation. Default is 'spatial_pscore'.

'spatial_pscore'
plot_score str

Determines which score(s) to plot. Options are: - 'Proximity Density' for plotting only the Proximity Density scores, - 'Proximity Volume' for plotting only the Proximity Volume scores, - 'both' for plotting both scores side by side. Default is 'both'.

'both'
order_xaxis list

Custom order for the x-axis categories. Pass a list of category names in the desired order. This can be useful for comparing specific regions or samples in a specific sequence.

None
color str

Color to use for the bar plots. This can enhance plot readability or align with publication themes.

'grey'
**kwargs

Additional keyword arguments passed directly to seaborn's barplot function, allowing for further customization of the plots.

{}

Returns:

Name Type Description
Plot matplotlib

Displays the generated bar plots.

Example
1
2
3
4
5
6
7
8
# Basic visualization of both Proximity Density and Proximity Volume
sm.pl.spatial_pscore(adata, label='spatial_pscore', plot_score='both', color='skyblue')

# Customized plot for Proximity Density with ordered x-axis and specific color
sm.pl.spatial_pscore(adata, plot_score='Proximity Density', order_xaxis=['Sample1', 'Sample2', 'Sample3'], color='salmon', edgecolor: 'black'})

# Focused plot on Proximity Volume with seaborn customization through kwargs
sm.pl.spatial_pscore(adata, plot_score='Proximity Volume', color='lightgreen', saturation: 0.8, alpha: 0.7})
Source code in scimap/plotting/spatial_pscore.py
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
def spatial_pscore (adata, 
                    label='spatial_pscore', 
                    plot_score='both', 
                    order_xaxis = None,
                    color='grey',
                    figsize=None,
                    **kwargs):
    """
Parameters:
        adata (anndata.AnnData):  
            The annotated data matrix with spatial proximity scores.

        label (str, optional):  
            The label/key used to access the spatial proximity scores stored in `adata.uns`. 
            This should match the label used during the `sm.tl.spatial_pscore` computation. Default is 'spatial_pscore'.

        plot_score (str, optional):  
            Determines which score(s) to plot. Options are:
            - 'Proximity Density' for plotting only the Proximity Density scores,
            - 'Proximity Volume' for plotting only the Proximity Volume scores,
            - 'both' for plotting both scores side by side. Default is 'both'.

        order_xaxis (list, optional):  
            Custom order for the x-axis categories. Pass a list of category names in the desired order.
            This can be useful for comparing specific regions or samples in a specific sequence.

        color (str, optional):  
            Color to use for the bar plots. This can enhance plot readability or align with publication themes.

        **kwargs:  
            Additional keyword arguments passed directly to seaborn's barplot function, allowing for further customization of the plots.

Returns:
        Plot (matplotlib): 
            Displays the generated bar plots.

Example:
    ```python

    # Basic visualization of both Proximity Density and Proximity Volume
    sm.pl.spatial_pscore(adata, label='spatial_pscore', plot_score='both', color='skyblue')

    # Customized plot for Proximity Density with ordered x-axis and specific color
    sm.pl.spatial_pscore(adata, plot_score='Proximity Density', order_xaxis=['Sample1', 'Sample2', 'Sample3'], color='salmon', edgecolor: 'black'})

    # Focused plot on Proximity Volume with seaborn customization through kwargs
    sm.pl.spatial_pscore(adata, plot_score='Proximity Volume', color='lightgreen', saturation: 0.8, alpha: 0.7})

    ```
    """


    # Isolate the data from anndata object
    data = adata.uns[label]

    # Order the x-axis if needed
    if order_xaxis is not None:
        data = data.reindex(order_xaxis)


    # Generate the x and y axis
    x  = data.index
    y_pd = data['Proximity Density'].values
    y_pv = data['Proximity Volume'].values

    if figsize is None:
        # Dynamically calculate figsize based on the data size
        figsize_width_scale = 1.0  # Adjust based on your preference and the expected data length
        figsize_height_scale = 0.5  # Adjust this to change how tall the plots are
        # For 'both', we might want a wider figure
        figsize_width = max(12, len(x) * figsize_width_scale)
        figsize_height = max(8, len(x) * figsize_height_scale)
        figsize = (figsize_width, figsize_height)

    # Plot what user requests
    if plot_score == 'Proximity Density':
        plt.figure(figsize=figsize)
        ax = sns.barplot(x=x, y=y_pd, color=color, **kwargs).set_title('Proximity Density')
        ax = plt.xticks(rotation=90)
        plt.tight_layout()
    if plot_score == 'Proximity Volume':
        plt.figure(figsize=figsize)
        ax = sns.barplot(x=x, y=y_pv, color=color, **kwargs).set_title('Proximity Volume')
        ax = plt.xticks(rotation=90)
        plt.tight_layout()
    if plot_score == 'both':
        plt.figure(figsize=figsize)
        fig, ax = plt.subplots(1,2)
        sns.barplot(x=x, y=y_pd, color=color, ax=ax[0], **kwargs).set_title('Proximity Density')
        ax[0].tick_params(axis='x', rotation=90)
        sns.barplot(x=x, y=y_pv, color=color, ax=ax[1], **kwargs).set_title('Proximity Volume')
        ax[1].tick_params(axis='x', rotation=90)
        plt.tight_layout()
        fig.show()