Skip to content

addPredictions

Short Description

The addPredictions function serves as a link between cspot and scimap package. It's useful for evaluating model performance. The function transforms results stored in anndata.uns to anndata.obs so they can be visualized using the scimap package's sm.pl.image viewer function. This displays positive and negative cells overlaid on the raw image.

The addPredictions function can take in two methods. cspotOutput displays the result of running the cspot function, while csScore shows the raw output produced by the csScore function, which returns a probability score. The midpoint parameter, with a default value of 0.5, can be adjusted to define what is considered a positive result, when method is set to csScore.

Function

addPredictions(csObject, method='cspotOutput', cspotOutput='cspotOutput', csScore='csScore', midpoint=0.5, outputDir=None)

Parameters:

Name Type Description Default
csObject anndata

Single or combined CSPOT object.

required
method str

There are two options: cspotOutput and csScore. cspotOutput displays the result of running the CSPOT function, while csScore shows the raw output produced by the csScore function, which returns a probability score. The midpoint parameter, with a default value of 0.5, can be adjusted to define what is considered a positive result, when method is set to csScore.

'cspotOutput'
cspotOutput str

The name under which the cspotOutput is stored.

'cspotOutput'
csScore str

The name under which the csScore is stored.

'csScore'
midpoint float

The threshold for determining positive cells, in conjunction with 'csScore'.

0.5
outputDir string

Provide the path to the output directory. Kindly take note that this particular output will not be automatically saved in a predetermined directory, unlike the other outputs. The file will be saved in the directory specified by the outputDir parameter. If None, the csObject will be returned to memory.

None

Returns:

Name Type Description
csObject anndata

If output directory is provided the csObject will be stored else it will be returned to memory. The results are stored in anndata.obs with a p_ appended to the markers names. So if you would like to vizulaize CD3, the column that you are looking for is p_CD3.

Example
# Path to projectDir
projectDir = '/Users/aj/Documents/cspotExampleData'

# Path to csObject
csObject = projectDir + '/CSPOT/cspotOutput/exampleImage_cspotPredict.ome.h5ad'

adata = cs.addPredictions (csObject, 
                    method='cspotOutput',
                    cspotOutput='cspotOutput',
                    csScore='csScore', 
                    midpoint=0.5)

# Same function if the user wants to run it via Command Line Interface
python addPredictions.py             --csObject Users/aj/Documents/cspotExampleData/CSPOT/cspotOutput/exampleImage_cspotPredict.ome.h5ad        
Source code in cspot/addPredictions.py
 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
def addPredictions (csObject, 
                    method='cspotOutput',
                    cspotOutput='cspotOutput',
                    csScore='csScore', 
                    midpoint=0.5,
                    outputDir=None):
    """
Parameters:
    csObject (anndata):  
        Single or combined CSPOT object.

    method (str, optional):  
        There are two options: `cspotOutput` and `csScore`. 
        `cspotOutput` displays the result of running the `CSPOT` function, 
        while `csScore` shows the raw output produced by the `csScore` 
        function, which returns a probability score. The `midpoint` parameter, 
        with a default value of 0.5, can be adjusted to define what is 
        considered a `positive` result, when method is set to `csScore`.

    cspotOutput (str, optional):  
        The name under which the `cspotOutput` is stored.

    csScore (str, optional):  
        The name under which the `csScore` is stored.

    midpoint (float, optional):  
        The threshold for determining positive cells, in conjunction with 'csScore'.

    outputDir (string, optional):  
        Provide the path to the output directory. Kindly take note that this particular 
        output will not be automatically saved in a predetermined directory, 
        unlike the other outputs. The file will be saved in the directory 
        specified by the `outputDir` parameter. If `None`, the `csObject` will 
        be returned to memory.

Returns:
    csObject (anndata):  
        If output directory is provided the `csObject` will 
        be stored else it will be returned to memory. The results are stored in 
        `anndata.obs` with a `p_` appended to the markers names. So if you would 
        like to vizulaize `CD3`, the column that you are looking for is `p_CD3`.

Example:
    	```python    

        # Path to projectDir
        projectDir = '/Users/aj/Documents/cspotExampleData'

        # Path to csObject
        csObject = projectDir + '/CSPOT/cspotOutput/exampleImage_cspotPredict.ome.h5ad'

        adata = cs.addPredictions (csObject, 
                            method='cspotOutput',
                            cspotOutput='cspotOutput',
                            csScore='csScore', 
                            midpoint=0.5)

        # Same function if the user wants to run it via Command Line Interface
        python addPredictions.py \
            --csObject Users/aj/Documents/cspotExampleData/CSPOT/cspotOutput/exampleImage_cspotPredict.ome.h5ad    	
        ```

    """
    # Load the adata
    if isinstance(csObject, str):
        adata = ad.read(csObject)
    else: 
        adata = csObject

    # function to convert the prob scores to binary pos or neg
    def assign_labels(df, midpoint):
        df = df.applymap(lambda x: 'neg' if x < midpoint else 'pos')
        return df

    # intialize the data    
    if method == 'cspotOutput':
        attach_df = adata.uns[cspotOutput].copy()
    elif method == 'csScore':
        df = adata.uns[csScore].copy()
        attach_df = assign_labels (df, midpoint=midpoint)


    obs = adata.obs.copy()
    columns_to_drop = [col for col in obs.columns if col.startswith('p_')]
    obs.drop(columns_to_drop, axis=1, inplace=True)

    new_col_names = ['p_{}'.format(idx) for idx in attach_df.columns]
    attach_df.columns = new_col_names
    # add to obs
    final_obs = pd.concat([obs, attach_df], axis=1)
    adata.obs = final_obs


    # Return to adata
    # Save data if requested
    if outputDir is not None:    
        finalPath = pathlib.Path(outputDir)     
        if not os.path.exists(finalPath):
            os.makedirs(finalPath)
        # determine file name
        if isinstance (csObject, str):
            imid = pathlib.Path(csObject).stem
        else:
            imid = 'addPredictions'    
        adata.write(finalPath / f'{imid}.h5ad')
    else:
        # Return data
        return adata