File size: 11,798 Bytes
ef814bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
"""
Validation Results Analysis
This module provides functions to create comprehensive DataFrames containing
sample-level predictions, labels, and metadata from cross-validation results.
"""

import pandas as pd
import numpy as np
import torch
from torch.utils.data import DataLoader, Subset
from utils.helpers import create_multimodal_model
from models import SingleTransformer

def get_sample_predictions_dataframe(
    model_type, 
    multimodal_dataset, 
    fold_results, 
    model_config, 
    device='cpu',
    batch_size=32,
    adata_rna=None,
    adata_atac=None,
    threshold=0.5
):
    """
    Creates a comprehensive DataFrame with sample-level predictions and metadata.
    
    Parameters
    ----------
    model_type : str
        Type of model: 'Multi', 'RNA', 'ATAC', or 'Flux'
    multimodal_dataset : MultiModalDataset
        The multimodal dataset containing all samples
    fold_results : list
        List of fold result dictionaries from cross-validation
    model_config : dict
        Model configuration dictionary
    device : str, optional
        Device to run predictions on ('cpu', 'cuda', 'mps')
    batch_size : int, optional
        Batch size for predictions
    adata_rna : AnnData, optional
        RNA AnnData object for additional metadata
    adata_atac : AnnData, optional
        ATAC AnnData object for additional metadata
    threshold : float, optional
        Classification threshold for binary predictions (default: 0.5)
    
    Returns
    -------
    pd.DataFrame
        DataFrame with columns:
        - ind: Sample index in the dataset
        - fold: Fold number
        - label_numeric: Actual label (0 or 1)
        - label: Actual label name ('dead-end' or 'reprogramming')
        - predicted_value: Predicted probability [0, 1]
        - predicted_class_numeric: Predicted class (0 or 1)
        - predicted_class: Predicted class name ('dead-end' or 'reprogramming')
        - correct: Whether prediction matches label
        - abs_error: Absolute error of prediction
        - modality: Available modalities for this sample (e.g., 'RAF', 'A', 'RF')
        - batch_no: Batch number
        - pct: Percentage metadata (if available)
        - clone_size: Clone size (if available)
        - clone_id: Clone ID (if available)
        - (additional RNA/ATAC metadata if adata objects provided)
    """
    
    # Collect all predictions across folds
    all_predictions = []
    all_labels = []
    all_indices = []
    all_folds = []
    
    print(f"Processing {len(fold_results)} folds...")
    
    for fold_idx, fold in enumerate(fold_results):
        model_path = fold['best_model_path']
        val_idx = fold['val_idx']
        
        # Create validation subset
        val_subset = Subset(multimodal_dataset, val_idx)
        val_loader = DataLoader(val_subset, batch_size=batch_size, shuffle=False)
        
        # Load model
        if model_type == 'Multi':
            model = create_multimodal_model(model_config, device, use_mlm=False)
        else:
            model = SingleTransformer(id=model_type, **model_config).to(device)
        
        # Load weights
        state_dict = torch.load(model_path, map_location='cpu')
        model.load_state_dict(state_dict)
        model = model.to(device)
        model.eval()
        
        # Get predictions
        fold_preds = []
        fold_labels = []
        
        with torch.no_grad():
            for batch in val_loader:
                x, b, y = batch
                
                if isinstance(x, list):
                    rna = x[0].to(device)
                    atac = x[1].to(device)
                    flux = x[2].to(device)
                    x = (rna, atac, flux)
                else:
                    x = x.to(device)
                
                b = b.to(device)
                
                # Get predictions
                preds, _ = model(x, b)
                preds = preds.squeeze()
                
                if preds.dim() == 0:
                    preds = preds.unsqueeze(0)
                if y.dim() == 0:
                    y = y.unsqueeze(0)
                
                fold_preds.extend(preds.cpu().numpy())
                fold_labels.extend(y.numpy())
        
        # Store results
        all_predictions.extend(fold_preds)
        all_labels.extend(fold_labels)
        all_indices.extend(val_idx)
        all_folds.extend([fold_idx + 1] * len(val_idx))
        
        print(f"  Fold {fold_idx + 1}: {len(val_idx)} samples processed")
    
    # Convert to arrays
    all_predictions = np.array(all_predictions)
    all_labels = np.array(all_labels)
    all_indices = np.array(all_indices)
    all_folds = np.array(all_folds)
    
    # Determine modality availability for each sample
    modalities = _get_modality_info(multimodal_dataset, all_indices)
    
    # Get additional metadata
    df_indices = multimodal_dataset.df_indics if hasattr(multimodal_dataset, 'df_indics') else None
    pcts = multimodal_dataset.pcts if hasattr(multimodal_dataset, 'pcts') else None
    label_names = multimodal_dataset.label_names if hasattr(multimodal_dataset, 'label_names') else None
    
    # Build base dataframe
    samples_data = []
    
    for i, (idx, pred, label, fold) in enumerate(zip(all_indices, all_predictions, all_labels, all_folds)):
        # Compute error
        abs_error = abs(label - pred)
        
        # Determine if correct
        pred_class = int(pred >= threshold)
        is_correct = pred_class == int(label)
        
        # Get batch number
        batch_no = int(multimodal_dataset.batch_no[idx].item())
        
        # Base sample info
        sample_info = {
            'ind': idx,
            'fold': fold,
            'label_numeric': int(label),
            'label': 'reprogramming' if label == 1 else 'dead-end',
            'predicted_value': float(pred),
            'predicted_class_numeric': pred_class,
            'predicted_class': 'reprogramming' if pred_class == 1 else 'dead-end',
            'correct': int(is_correct),
            'abs_error': float(abs_error),
            'modality': modalities[i],
            'batch_no': batch_no,
        }
        
        # Add percentage if available
        if pcts is not None:
            sample_info['pct'] = float(pcts[idx])
        
        # Add additional metadata from AnnData objects if available
        if df_indices is not None and (adata_rna is not None or adata_atac is not None):
            rna_id = df_indices.iloc[idx, 0] if df_indices.shape[1] > 0 else None
            atac_id = df_indices.iloc[idx, 1] if df_indices.shape[1] > 1 else None
            
            # Try to get metadata from RNA or ATAC
            metadata_added = False
            
            if adata_rna is not None and rna_id is not None and rna_id in adata_rna.obs.index:
                obs = adata_rna.obs.loc[rna_id]
                _add_obs_metadata(sample_info, obs)
                metadata_added = True
            
            if not metadata_added and adata_atac is not None and atac_id is not None and atac_id in adata_atac.obs.index:
                obs = adata_atac.obs.loc[atac_id]
                _add_obs_metadata(sample_info, obs)
        
        samples_data.append(sample_info)
    
    # Create DataFrame
    df_samples = pd.DataFrame(samples_data)
    
    # Sort by index for easier analysis
    df_samples = df_samples.sort_values('ind').reset_index(drop=True)
    
    print(f"\nTotal samples: {len(df_samples)}")
    print(f"Correct predictions: {df_samples['correct'].sum()} ({100 * df_samples['correct'].mean():.2f}%)")
    print(f"Mean absolute error: {df_samples['abs_error'].mean():.4f}")
    
    return df_samples


def _get_modality_info(dataset, indices):
    """
    Determine which modalities are available for each sample.
    
    Returns a list of modality strings:
    - 'RAF': RNA, ATAC, Flux all available
    - 'RA': RNA and ATAC available
    - 'RF': RNA and Flux available  
    - 'AF': ATAC and Flux available
    - 'R': Only RNA available
    - 'A': Only ATAC available
    - 'F': Only Flux available
    """
    modalities = []
    
    for idx in indices:
        # Check if each modality has data
        has_rna = (dataset.rna_data[idx] != 0).any().item()
        has_atac = (dataset.atac_data[idx] != 0).any().item()
        has_flux = (dataset.flux_data[idx] != 0).any().item()
        
        # Build modality string
        modality = ''
        if has_rna:
            modality += 'R'
        if has_atac:
            modality += 'A'
        if has_flux:
            modality += 'F'
        
        modalities.append(modality if modality else 'None')
    
    return modalities


def _add_obs_metadata(sample_info, obs):
    """Add metadata from AnnData obs to sample_info dictionary."""
    metadata_fields = [
        'clone_size', 'clone_id', 'cells_RNA', 'cells_ATAC',
        'cells_RNA_D3', 'cells_ATAC_D3', 'n_genes', 'phase',
        'G2M_score', 'pct_counts_mt', 'total_counts'
    ]
    
    for field in metadata_fields:
        if field in obs:
            value = obs[field]
            # Handle different data types
            if pd.notna(value):
                if isinstance(value, (int, float, np.integer, np.floating)):
                    sample_info[field] = value
                else:
                    sample_info[field] = str(value)


def summarize_by_modality(df_samples):
    """
    Summarize prediction performance by modality.
    
    Parameters
    ----------
    df_samples : pd.DataFrame
        DataFrame from get_sample_predictions_dataframe
    
    Returns
    -------
    pd.DataFrame
        Summary statistics grouped by modality
    """
    summary = df_samples.groupby('modality').agg({
        'ind': 'count',
        'correct': 'mean',
        'abs_error': 'mean',
        'predicted_value': ['mean', 'std']
    }).round(4)
    
    summary.columns = ['n_samples', 'accuracy', 'mean_abs_error', 'mean_pred', 'std_pred']
    summary = summary.reset_index()
    summary = summary.sort_values('n_samples', ascending=False)
    
    return summary


def summarize_by_fold(df_samples):
    """
    Summarize prediction performance by fold.
    
    Parameters
    ----------
    df_samples : pd.DataFrame
        DataFrame from get_sample_predictions_dataframe
    
    Returns
    -------
    pd.DataFrame
        Summary statistics grouped by fold
    """
    summary = df_samples.groupby('fold').agg({
        'ind': 'count',
        'correct': 'mean',
        'abs_error': 'mean',
        'predicted_value': ['mean', 'std']
    }).round(4)
    
    summary.columns = ['n_samples', 'accuracy', 'mean_abs_error', 'mean_pred', 'std_pred']
    summary = summary.reset_index()
    
    return summary
def get_misclassified_samples(df_samples):
    """
    Get only misclassified samples.
    
    Parameters
    ----------
    df_samples : pd.DataFrame
        DataFrame from get_sample_predictions_dataframe
    
    Returns
    -------
    pd.DataFrame
        DataFrame containing only misclassified samples
    """
    return df_samples[df_samples['correct'] == 0].copy()
def get_samples_by_modality(df_samples, modality):
    """
    Get samples filtered by modality.
    
    Parameters
    ----------
    df_samples : pd.DataFrame
        DataFrame from get_sample_predictions_dataframe
    modality : str
        Modality string (e.g., 'RAF', 'A', 'RF')
    
    Returns
    -------
    pd.DataFrame
        Filtered DataFrame
    """
    return df_samples[df_samples['modality'] == modality].copy()


if __name__ == "__main__":
    # Example usage
    print("This module provides functions to analyze validation results.")
    print("Main function: get_sample_predictions_dataframe()")