| |
|
| | import h5py
|
| | import numpy as np
|
| | from sklearn.decomposition import IncrementalPCA
|
| | from scipy.io import loadmat
|
| | import pickle
|
| |
|
| | inverse_transformation_path = "./mask/"
|
| | filestr = "washu120_subsample1_randperm0_timeseries"
|
| | n_samples = 27255
|
| |
|
| | left_file_path = inverse_transformation_path + "Left_fMRI2Grid_192_by_192_NN.mat"
|
| | left_data = loadmat(left_file_path)
|
| | Left_inverse_transformation = left_data["inverse_transformation"]
|
| |
|
| |
|
| | right_file_path = inverse_transformation_path + "Right_fMRI2Grid_192_by_192_NN.mat"
|
| | right_data = loadmat(right_file_path)
|
| | Right_inverse_transformation = right_data["inverse_transformation"]
|
| |
|
| |
|
| |
|
| | h5filename = (
|
| | "/mnt/leuthardte/Active/Cindy/Data/" + filestr +".h5"
|
| | )
|
| |
|
| |
|
| | batch_size = 5451
|
| |
|
| | n_components = 100
|
| | ipca = IncrementalPCA(n_components=n_components)
|
| |
|
| | if not n_samples:
|
| |
|
| | with h5py.File(h5filename, "r") as file:
|
| | n_samples = file["/LeftData"].shape[0]
|
| | print(n_samples)
|
| |
|
| |
|
| | print(n_samples)
|
| | assert n_samples % batch_size == 0, "batch size not a factor of sample size"
|
| |
|
| | for start_idx in range(0, n_samples, batch_size):
|
| | end_idx = start_idx + batch_size
|
| |
|
| | print(start_idx)
|
| |
|
| |
|
| | with h5py.File(h5filename, "r") as file:
|
| | reconL = file["/LeftData"][start_idx:end_idx, :, :, :]
|
| | reconR = file["/RightData"][start_idx:end_idx, :, :, :]
|
| |
|
| |
|
| |
|
| | corticalrecon_L = (
|
| | Left_inverse_transformation
|
| | @ reconL.transpose(0, 1, 2, 3).reshape(batch_size, -1).T
|
| | )
|
| | corticalrecon_R = (
|
| | Right_inverse_transformation
|
| | @ reconR.transpose(0, 1, 2, 3).reshape(batch_size, -1).T
|
| | )
|
| |
|
| |
|
| | recon_dtseries = np.vstack((corticalrecon_L, corticalrecon_R))
|
| |
|
| |
|
| |
|
| |
|
| |
|
| | ipca.partial_fit(recon_dtseries.T)
|
| |
|
| | principal_components = ipca.components_
|
| |
|
| |
|
| | with open(
|
| | "./IncrementalPCA/pca_model_"
|
| | + filestr
|
| | + "_zdim"
|
| | + str(n_components)
|
| | + ".pkl",
|
| | "wb",
|
| | ) as f:
|
| | pickle.dump(ipca, f)
|
| |
|
| | print(ipca.mean_)
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|