Archive

Author Archive

Convert images to matrix

September 2nd, 2010

Quite often you need to convert an image (or multiple images) to a MatLab matrix for further analysis and visualization (e.g. extracting time series, multivariate pattern analysis, etc). SPM provides handy functions for this:

P = spm_select; % select 1 or more images
V = spm_vol(P);
M = spm_read_vols(V);

The dimension of M is 3D or 4D depending on how many images you selected. The 1st 3 dimensions are spatial and the last one is temporal.

If you want to save a matrix to a image file, use

V.fname = 'b.img';%
spm_write_vol(V, M);
Author: Xu Cui Categories: brain, matlab Tags:

Wavelet Coherence

August 31st, 2010

Wavelet transform coherence (WTC) is a method for analyzing the coherence and phase lag between two time series as a function of both time and frequency (Chang and Glover 2010). Here I played with it using the MatLab toolbox provided by Grinsted et al.

In the following examples, I created two time series, x (blue) and y (red) with different properties (phase shift, frequency and amplitude) and run wtc(x,y,’mcc’,0) command. Small white noise was added to the time series.

1. Phase shift and angle. A rightward arrow indicates 0 lag; a bottom-right arrow indicates a small lead of x; a leftward arrow indicates x and y is anti-correlated.

Phase

Phase

2. Frequency. At what frequency (or period) are x and y correlated?

frequency

frequency

3. Amplitude. It seems amplitude doesn’t matter much.

amplitude

amplitude

Toolbox can be found:
http://www.pol.ac.uk/home/research/waveletcoherence/

Source code:

t =[1:1000]/10;

%% effect of phase
x = sin(t) + randn(size(t))/5;
y = [sin(t(1:250)) sin(t(251:500)-pi/4) sin(t(501:750)-pi/2) sin(t(751:end)-pi)] + randn(size(t))/5;

figure('color','w');
subplot(2,1,1);
wtc(x,y,'mcc',0)
subplot(2,1,2);
plot(x);
hold on;
plot(y,'r')

%% effect of frequency
x = [sin(2*t(1:250)) sin(2*t(251:500)) sin(4*t(501:750)) sin(8*t(751:end))] + randn(size(t))/5;
y = [sin(2*t(1:250)) sin(2*t(251:500)) sin(4*t(501:750)) sin(8*t(751:end))] + randn(size(t))/5;

figure('color','w');
subplot(2,1,1);
wtc(x,y,'mcc',0)
subplot(2,1,2);
plot(x);
hold on;
plot(y,'r')

%% effect of amplitude
x = [sin(2*t(1:250)) sin(2*t(251:500)) sin(4*t(501:750)) sin(8*t(751:end))] + randn(size(t))/5;
y = [sin(2*t(1:250)) sin(2*t(251:500))/2 sin(4*t(501:750))/3 sin(8*t(751:end))/4] + randn(size(t))/5;

figure('color','w');
subplot(2,1,1);
wtc(x,y,'mcc',0)
subplot(2,1,2);
plot(x);
hold on;
plot(y,'r')
Author: Xu Cui Categories: brain, matlab Tags:

【转】静静的小河

August 24th, 2010

离家不远处有一条静静的小河
闲暇时我和孩子总有一段时光在那儿度过
清清的河水静静地流淌
只偶有几头到河边吃草的老牛慢慢走过
柔顺飘逸的苔丝和水草为小河装点了绿意
一群群小鱼在上面散步跳舞唱歌

思绪像随风的浮云那样闲适随意
心儿像逐流的鱼儿那样悠然自乐
我们用诱饵和器具将小鱼捕获
又在河滩上为放生的鱼儿拦坝做窝
孩子捧起小鱼和它们喃喃私语
鱼儿听懂后摇着身子点头致意
又用一根小棍和小鱼逗乐
也为不慎误伤小鱼而伤心难过

清清的河水静静地流着
在大自然母亲的怀抱中孩子的童趣永远没有个够
再三催促后才恋恋不舍地准备往家走
我们和小河鱼儿约定了下次见面的日期
婀娜多姿的苔丝和我们挥手告别
小鱼跳跃着提醒我们别把约会的时间记错

出处:百草岭  作者:陆平

Author: Xu Cui Categories: life Tags:

Standard deviation and standard error

August 18th, 2010

Standard deviation (std): standard deviation of the sample
Standard error, or standard error of the mean (sem), is the standard deviation of the mean. \(sem=std/\sqrt{N}\)

Most errorbars in scientific publications refer to standard error. Quite often a T-test is accompanied to show the effect is significant. This makes sense if they try to compare if the mean is different. Sometimes - quite often in neuroimage - while the mean is significantly different, it is almost impossible to tell the difference on the individual level (or classify individual observation). If the authors used standard deviation, the errorbars would have been huge and figures got ugly.

Author: Xu Cui Categories: brain Tags:

Interview with David Shelly

July 28th, 2010

David is an amazing person: he has published 5 papers in Nature and 1 in Science, two of which were published this year. Check out what he says in Peaya interview (click the photo below).

David Shelly

David Shelly

Author: Xu Cui Categories: life Tags:

gmail is getting smarter

July 21st, 2010

I noticed that gmail will remind you if you have something like “… is attached” in the email body but actually you don’t attach any files. This is very considerate.

Author: Xu Cui Categories: life Tags:

Meaning of correlation coefficient

July 13th, 2010

If variable X and Y has correlation 0.1, how much does it help to predict Y based on X? In the simplest binary case, the probability (p) to correctly predict Y based on X is a linear function of correlation (c), i.e.
$$p=\frac{c+1}{2}$$

That means, a 0.1 correlation will increase the prediction probability from 0.5 to 0.55, a 5% increase. This could be significant if you are betting a lot of money :)

Author: Xu Cui Categories: brain Tags:

Octopus Paul, hypothesis, p-Value and significance

July 12th, 2010

Null hypothesis (H0): Octopus Paul doesn’t have the ability to predict. Or, the probability that he predicts correctly on each event is 1/2.
Data: In 2010 World Cup, Octopus Paul correctly predicted the outcomes of 8 games out of 8 games.
p-Value: (the probability to obtain the data assuming the null hypothesis is true): 1/2^8 = 0.0039
Statistical significance threshold: alpha = 0.05
Conclusion: as pvalue < alpha, we conclude that the null hypothesis should be rejected. Loosely speaking, octopus Paul does have prediction power.

Author: Xu Cui Categories: fun, life Tags:

Peaya officially launches

July 7th, 2010

The goal of Peaya  is to make your academic life more fun and more productive.

Peaya Comics

Comic strips about our academia life – the excitement, joy, depression, and pain.  We hope the strips will make you smile. Below is the latest comics:

Comics


Peaya Paper

A software tool that allows you to manage papers and PDFs and access them anywhere (Windows, Mac, and Linux). It also makes it easy to share with friends and collaborators. We hope Peaya Paper will make your work more productive. See how happy Iris (below) is after using Peaya Paper!

Peaya Paper


Peaya Cite

A MS Word add-in that offers a completely new way to insert citations — Cite Seamlessly with real-time context-dependent reference hinting. We hope Peaya Cite will allow you to concentrate on writing itself rather than on trivial citation insertion or formatting.

Peaya Cite

Author: Xu Cui Categories: life Tags:

How to determine the voxel size in an image

June 17th, 2010

Tools: SPM, cor2mni

Assume the image is “a.img”, do

v = spm_vol('a.img');
v.mat

If v.mat is a diagonal matrix, you can simply read the number and they are the voxel size in mm.

If not, a trick is to calculate the distance between adjacent voxels. For example, if v.mat =

   -3.4337   -0.0518   -0.1776  110.2825
   -0.0919    3.3036    1.1004  -97.8366
   -0.1324   -0.9487    3.8416   19.7459
         0         0         0    1.0000

Use cor2mni.m to calculate the voxel coordinate in MNI space.

point1 = cor2mni([1 1 1], v.mat);
point2 = cor2mni([1 1 2], v.mat);

Then calculate the distance between point1 and point2 you will get the voxel size in z direction. Repeat for x and y direction.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% cor2mni
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function mni = cor2mni(cor, T)
% function mni = cor2mni(cor, T)
% convert matrix coordinate to mni coordinate
%
% cor: an Nx3 matrix
% T: (optional) rotation matrix
% mni is the returned coordinate in mni space
%
% caution: if T is not given, the default T is
% T = ...
%     [-4     0     0    84;...
%      0     4     0  -116;...
%      0     0     4   -56;...
%      0     0     0     1];
%
% xu cui
% 2004-8-18
% last revised: 2005-04-30

if nargin == 1
    T = ...
        [-4     0     0    84;...
         0     4     0  -116;...
         0     0     4   -56;...
         0     0     0     1];
end

cor = round(cor);
mni = T*[cor(:,1) cor(:,2) cor(:,3) ones(size(cor,1),1)]';
mni = mni';
mni(:,4) = [];
return;
Author: Xu Cui Categories: brain Tags: