File size: 7,630 Bytes
bb04d63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3

"""
pca_autoencoder.py

This script demonstrates how to:
  1) Load a dataset of MRI volumes using MONAI transforms (as in brain2vec_linearAE.py).
  2) Flatten each 3D volume into a 1D vector (614,400 features if 80x96x80).
  3) Perform IncrementalPCA to reduce dimensionality to 1200 components.
  4) Provide a 'forward()' method that returns (reconstruction, embedding),
     mimicking the interface of a linear autoencoder.
"""

import os
import argparse
import numpy as np
import pandas as pd

import torch
from torch.utils.data import DataLoader

from monai import transforms
from monai.data import Dataset, PersistentDataset

from sklearn.decomposition import IncrementalPCA

###################################################################
# Constants for your typical config
###################################################################
RESOLUTION = 2
INPUT_SHAPE_AE = (80, 96, 80)
N_COMPONENTS = 1200

###################################################################
# Helper classes/functions
###################################################################
def get_dataset_from_pd(df: pd.DataFrame, transforms_fn, cache_dir: str):
    """
    Returns a monai.data.Dataset or monai.data.PersistentDataset
    if `cache_dir` is defined, to speed up loading.
    """
    if cache_dir and cache_dir.strip():
        os.makedirs(cache_dir, exist_ok=True)
        dataset = PersistentDataset(data=df.to_dict(orient='records'),
                                    transform=transforms_fn,
                                    cache_dir=cache_dir)
    else:
        dataset = Dataset(data=df.to_dict(orient='records'),
                          transform=transforms_fn)
    return dataset


class PCAAutoencoder:
    """
    A PCA 'autoencoder' using IncrementalPCA for memory efficiency, 
    providing:
      - fit(X): partial fit on batches
      - transform(X): get embeddings
      - inverse_transform(Z): reconstruct from embeddings
      - forward(X): returns (X_recon, Z) for a direct API 
                    similar to a shallow linear AE.
    """
    def __init__(self, n_components=N_COMPONENTS, batch_size=128):
        self.n_components = n_components
        self.batch_size = batch_size
        self.ipca = IncrementalPCA(n_components=self.n_components)

    def fit(self, X: np.ndarray):
        """
        Incrementally fit the PCA model on batches of data.
        X: shape (n_samples, n_features).
        """
        n_samples = X.shape[0]
        for start_idx in range(0, n_samples, self.batch_size):
            end_idx = min(start_idx + self.batch_size, n_samples)
            self.ipca.partial_fit(X[start_idx:end_idx])

    def transform(self, X: np.ndarray) -> np.ndarray:
        """
        Projects data into the PCA latent space in batches.
        Returns Z: shape (n_samples, n_components).
        """
        results = []
        n_samples = X.shape[0]
        for start_idx in range(0, n_samples, self.batch_size):
            end_idx = min(start_idx + self.batch_size, n_samples)
            Z_chunk = self.ipca.transform(X[start_idx:end_idx])
            results.append(Z_chunk)
        return np.vstack(results)

    def inverse_transform(self, Z: np.ndarray) -> np.ndarray:
        """
        Reconstruct data from PCA latent space in batches.
        Returns X_recon: shape (n_samples, n_features).
        """
        results = []
        n_samples = Z.shape[0]
        for start_idx in range(0, n_samples, self.batch_size):
            end_idx = min(start_idx + self.batch_size, n_samples)
            X_chunk = self.ipca.inverse_transform(Z[start_idx:end_idx])
            results.append(X_chunk)
        return np.vstack(results)

    def forward(self, X: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
        """
        Mimics a linear AE's forward() returning (X_recon, Z).
        """
        Z = self.transform(X)
        X_recon = self.inverse_transform(Z)
        return X_recon, Z


def load_and_flatten_dataset(csv_path: str, cache_dir: str, transforms_fn) -> np.ndarray:
    """
    Loads the dataset from csv_path, applies the monai transforms, 
    and flattens each 3D MRI into a 1D vector of shape (80*96*80).
    Returns a numpy array X with shape (n_samples, 614400).
    """
    df = pd.read_csv(csv_path)
    dataset = get_dataset_from_pd(df, transforms_fn, cache_dir)

    # We'll put the flattened data into this list, then stack.
    X_list = []

    # If memory allows, you can simply do a single-threaded loop 
    # or multi-worker DataLoader for speed.  
    # We'll demonstrate a simple single-worker here for clarity.
    loader = DataLoader(dataset, batch_size=1, num_workers=0)

    for batch in loader:
        # batch["image"] shape: (1, 1, 80, 96, 80)
        img = batch["image"].squeeze(0)  # shape: (1, 80, 96, 80)
        img_np = img.numpy()  # convert to np array, shape: (1, D, H, W)
        flattened = img_np.flatten()     # shape: (614400,)
        X_list.append(flattened)

    X = np.vstack(X_list)  # shape: (n_samples, 614400)
    return X


def main():
    parser = argparse.ArgumentParser(description="PCA Autoencoder with MONAI transforms example.")
    parser.add_argument("--inputs_csv", type=str, required=True, help="CSV with 'image_path' column.")
    parser.add_argument("--cache_dir", type=str, default="", help="Cache directory for MONAI PersistentDataset.")
    parser.add_argument("--output_dir", type=str, default="./pca_outputs", help="Where to save PCA model and embeddings.")
    parser.add_argument("--batch_size_ipca", type=int, default=128, help="Batch size for IncrementalPCA partial_fit().")
    parser.add_argument("--n_components", type=int, default=1200, help="Number of PCA components.")
    args = parser.parse_args()

    os.makedirs(args.output_dir, exist_ok=True)

    # Same transforms as in brain2vec_linearAE.py
    transforms_fn = transforms.Compose([
        transforms.CopyItemsD(keys={'image_path'}, names=['image']),
        transforms.LoadImageD(image_only=True, keys=['image']),
        transforms.EnsureChannelFirstD(keys=['image']),
        transforms.SpacingD(pixdim=RESOLUTION, keys=['image']),
        transforms.ResizeWithPadOrCropD(spatial_size=INPUT_SHAPE_AE, mode='minimum', keys=['image']),
        transforms.ScaleIntensityD(minv=0, maxv=1, keys=['image']),
    ])

    print("Loading and flattening dataset from:", args.inputs_csv)
    X = load_and_flatten_dataset(args.inputs_csv, args.cache_dir, transforms_fn)
    print(f"Dataset shape after flattening: {X.shape}")

    # Build PCAAutoencoder
    model = PCAAutoencoder(n_components=args.n_components, batch_size=args.batch_size_ipca)

    # Fit the PCA model
    print("Fitting IncrementalPCA in batches...")
    model.fit(X)
    print("Done fitting PCA. Transforming data to embeddings...")

    # Get embeddings & reconstruction
    X_recon, Z = model.forward(X)
    print("Embeddings shape:", Z.shape)
    print("Reconstruction shape:", X_recon.shape)

    # Optional: Save
    embeddings_path = os.path.join(args.output_dir, "pca_embeddings.npy")
    recons_path = os.path.join(args.output_dir, "pca_reconstructions.npy")
    np.save(embeddings_path, Z)
    np.save(recons_path, X_recon)
    print(f"Saved embeddings to {embeddings_path} and reconstructions to {recons_path}")

    # If you want to store the actual PCA components for future usage:
    # from joblib import dump
    # ipca_model_path = os.path.join(args.output_dir, "pca_model.joblib")
    # dump(model.ipca, ipca_model_path)
    # print(f"Saved PCA model to {ipca_model_path}")


if __name__ == "__main__":
    main()