Skip to content
Snippets Groups Projects
Commit e3c11b3e authored by Sean Fitzgibbon's avatar Sean Fitzgibbon
Browse files

added migp demo

parent c58e06a6
No related branches found
No related tags found
No related merge requests found
% MIGP - MELODIC's Incremental Group-PCA
% Steve Smith, FMRIB, 2012-2013
% not yet finally tweaked/evaluated, or published - please do not pass on.
% adapted by Sean Fitz, 2020
%%%%%%%%%%%%%%%%%%%%%%%%%% USER OPTIONS
INlist=dir('data/cobre/fmri_*.nii.gz'); % list of input 4D NIFTI standard space datasets
INmask='data/brain_mask.nii.gz'; % 3D NIFTI mask image
GO='matMIGP'; % output filename
dPCAint=550; % internal number of components - typically 2-4 times number of timepoints in each run (if you have enough RAM for that)
dPCAout=100; % number of eigenvectors to output - should be less than dPCAint and more than the final ICA dimensionality
%%%%%%%%%%%%%%%%%%%%%%%%%% END OF USER OPTIONS
Nsub=length(INlist); [~,r]=sort(rand(Nsub,1)); % will process subjects in random order
mask=read_avw(INmask);
masksize=size(mask);
mask=reshape(mask,prod(masksize),1);
demean = @(x) x - repmat(mean(x,1),[size(x,1), 1, 1]);
ss_svds = @(x,n) svds(x, n);
for i = 1:Nsub
% read data
filename=[INlist(r(i)).folder, '/', INlist(r(i)).name];
grot=double(read_avw(filename));
grot=reshape(grot,prod(masksize),size(grot,4));
grot=demean(grot(mask~=0,:)');
% var-norm
[uu,ss,vv]=ss_svds(grot,30);
vv(abs(vv)<2.3*std(vv(:)))=0;
stddevs=max(std(grot-uu*ss*vv'),0.001);
grot=grot./repmat(stddevs,size(grot,1),1);
if (i==1)
W=demean(grot); clear grot;
else
% concat
W=[W; demean(grot)]; clear grot;
% reduce W to dPCAint eigenvectors
if size(W,1)-10 > dPCAint
[uu,dd]=eigs(W*W',dPCAint);
W=uu'*W;
clear uu;
end
end
end
grot=zeros(prod(masksize),dPCAout);
grot(mask~=0,:)=W(1:dPCAout,:)'; grot=reshape(grot,[masksize ,dPCAout]);
save_avw(grot,GO,'f',[1 1 1 1]);
system(sprintf('fslcpgeom %s %s -d',filename,GO));
%% Cell type:code id: tags:
``` python
from nilearn import plotting
from nilearn import image
import nibabel as nb
import matplotlib.pyplot as plt
import numpy as np
```
%% Cell type:code id: tags:
``` python
%%bash
melodic -i matMIGP.nii.gz \
--mask=data/brain_mask.nii.gz \
-d 20 \
-v \
--nobet \
--disableMigp \
--varnorm \
-o matMIGP_dim20.ica
```
%% Cell type:code id: tags:
``` python
ics = nb.load('matMIGP_dim20.ica/melodic_IC.nii.gz')
N = ics.shape[-1]
fig, ax = plt.subplots(int(np.ceil((N/2))),2, figsize=(12, N))
for img, ax0 in zip(image.iter_img(ics), ax.ravel()):
coord = plotting.find_xyz_cut_coords(img, activation_threshold=2.3)
plotting.plot_stat_map(img, cut_coords=coord, vmax=5, axes=ax0)
```
%% Cell type:code id: tags:
``` python
```
%% Cell type:code id: tags:
``` python
import glob
import random
import nibabel as nb
import numpy as np
from scipy.sparse.linalg import svds, eigs
import matplotlib.pyplot as plt
from nilearn import plotting
from nilearn import image
```
%% Cell type:code id: tags:
``` python
in_list = [nb.load(f) for f in glob.glob('data/cobre/fmri_*.nii.gz')]
in_mask = nb.load('data/brain_mask.nii.gz')
go = '3DMIGP'
dPCA_int = 550
dPCA_out = 100
```
%% Cell type:code id: tags:
``` python
random.shuffle(in_list)
```
%% Cell type:code id: tags:
``` python
mask = in_mask.get_fdata().ravel()
def demean(x):
return x - np.mean(x, axis=0)
for i, f in enumerate(in_list):
print(i, end=' ')
# read data
grot = f.get_fdata()
grot = np.reshape(grot, [-1, grot.shape[-1]])
grot = grot[mask!=0, :].T
grot = demean(grot)
# var-norm
[uu, ss, vt] = svds(grot, k=30)
vt[np.abs(vt) < (2.3 * np.std(vt))] = 0;
stddevs = np.maximum(np.std(grot - (uu @ np.diag(ss) @ vt), axis=0), 0.001)
grot = grot/stddevs
if i == 0:
W = demean(grot)
else:
# concat
W = np.concatenate((W, demean(grot)), axis=0)
# reduce W to dPCA_int eigenvectors
if W.shape[0]-10 > dPCA_int:
uu = eigs(W@W.T, dPCA_int)[1]
uu = np.real(uu)
W = uu.T @ W
f.uncache()
```
%% Cell type:code id: tags:
``` python
grot = np.zeros([mask.shape[0], dPCA_out])
grot[mask!=0, :] = W[:dPCA_out, :].T
grot = np.reshape(grot, in_list[0].shape[:3] + (dPCA_out,))
```
%% Cell type:code id: tags:
``` python
nb.Nifti1Image(grot, affine=in_list[0].affine, header=in_list[0].header).to_filename('pyMIGP.nii.gz')
```
%% Cell type:code id: tags:
``` python
%%bash
melodic -i pyMIGP.nii.gz \
--mask=data/brain_mask.nii.gz \
-d 20 \
-v \
--nobet \
--disableMigp \
--varnorm \
-o pymigp_dim20.ica
```
%% Cell type:code id: tags:
``` python
ics = nb.load('pymigp_dim20.ica/melodic_IC.nii.gz')
N = ics.shape[-1]
fig, ax = plt.subplots(int(np.ceil((N/2))),2, figsize=(12, N))
for img, ax0 in zip(image.iter_img(ics), ax.ravel()):
coord = plotting.find_xyz_cut_coords(img, activation_threshold=1.5)
plotting.plot_stat_map(img, cut_coords=coord, vmax=5, axes=ax0)
```
%% Cell type:code id: tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment