Skip to content

Commit

Permalink
matlab lme toolbox on github
Browse files Browse the repository at this point in the history
  • Loading branch information
jbernal0019 committed May 5, 2014
1 parent ce7883f commit 4bca854
Show file tree
Hide file tree
Showing 75 changed files with 5,717 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Qdec/Qdec2num.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function M = Qdec2num(Qdec)
% M = Qdec2num(Qdec)
%
% Attempts to convert the two dimensional cell string array Qdec to numeric
% matrix M.
%
% Input
% Qdec: Two dimensional cell string array of Qdec data (eg. read with
% fReadQdec).
%
% Output
% M: Numeric matrix (without the name of the variables).
%
% $Revision: 1.1.1.1 $ $Date: 2012/02/02 11:25:52 $
% Original Author: Jorge Luis Bernal Rusiel
% CVS Revision Info:
% $Author: jbernal$
% $Date: 2012/02/02 11:25:52 $
% $Revision: 1.1 $
%
Qdec = Qdec(2:end,:);
M = str2double(Qdec);
34 changes: 34 additions & 0 deletions Qdec/fReadQdec.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function Qdec = fReadQdec(fname)
% Qdec = fReadQdec(fname)
%
% Reads a Freesurfer's Qdec table file into cell string array Qdec.
%
% Input
% fname: The name of a Qdec table data file.
%
% Output
% Qdec: Two dimensional cell string array with the data.
%
% $Revision: 1.1.1.1 $ $Date: 2012/02/02 11:25:52 $
% Original Author: Jorge Luis Bernal Rusiel
% CVS Revision Info:
% $Author: jbernal$
% $Date: 2012/02/02 11:25:52 $
% $Revision: 1.1 $
%
fid = fopen(fname);
tline = fgetl(fid);
Qdec = [];
i = 1;
while ischar(tline)
j = 1;
[str,remain] = strtok(tline, ' ');
while ~isempty(str)
Qdec{i,j} = str;
j = j + 1;
[str,remain] = strtok(remain, ' ');
end
i = i + 1;
tline = fgetl(fid);
end
fclose(fid);
37 changes: 37 additions & 0 deletions Qdec/fWriteQdec.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function fWriteQdec(fname,Qdec)
% fWriteQdec(fname,Qdec)
%
% Writes cell string array Qdec to a Freesurfer's Qdec table data file.
%
% Input
% fname: The name of a Qdec table data file.
% Qdec: Two dimensional cell string array with the data.
%
% $Revision: 1.1.1.1 $ $Date: 2012/02/02 11:25:52 $
% Original Author: Jorge Luis Bernal Rusiel
% CVS Revision Info:
% $Author: jbernal$
% $Date: 2012/02/02 11:25:52 $
% $Revision: 1.1 $
%
if nargin < 2
error('Too few inputs');
end;
szQ = size(Qdec);
fid = fopen (fname,'w');
if fid < 0
error('Do you have write permissions for %s?', pwd);
end
if ~strcmp(Qdec{1,1},'fsid')
warning('Dat{1,1} is not ''fsid'' will change it ');
Qdec{1,1} = 'fsid';
end;
for i=1:szQ(1)
j = 1;
while j < szQ(2)
fprintf(fid, [char(Qdec{i,j}) ' ']);
j = j + 1;
end;
fprintf(fid, [char(Qdec{i,j}) '\n']);
end;
fclose (fid);
26 changes: 26 additions & 0 deletions Qdec/findQdecVar.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function col = findQdecVar(Qdec,name)
% col = findQdecVar(Qdec,name)
%
% Gives the column of a variable in a Freesurfer's Qdec table that was read
% into the two dimensional cell string array Qdec. Returns empty if the
% variable is not in the Qdec table.
%
% Input
% Qdec: Two dimensional cell string array of Qdec data (eg. read with
% fReadQdec).
% name: A string with the name of the variable.
%
% Output
% col: Column of the variable in the cell string array Qdec.
%
% $Revision: 1.1.1.1 $ $Date: 2012/02/02 11:25:52 $
% Original Author: Jorge Luis Bernal Rusiel
% CVS Revision Info:
% $Author: jbernal$
% $Date: 2012/02/02 11:25:52 $
% $Revision: 1.1 $
%
if nargin < 2
error('Too few inputs');
end;
col = find(strcmp(getQdecVars(Qdec),name));
33 changes: 33 additions & 0 deletions Qdec/getQdecVarData.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function Qdec2 = getQdecVarData(Qdec1,Vars)
% Qdec2 = getQdecVarData(Qdec1,Vars)
%
% Returns the data for the variables in Qdec table Qdec1 that are named in
% Vars.
%
% Input
% Qdec1: Two dimensional cell string array of Qdec data (eg. read with
% fReadQdec).
% Vars: One dimensional cell string array.
%
% Output
% Qdec2: Two dimensional cell string array of Qdec data.
%
% $Revision: 1.1.1.1 $ $Date: 2012/02/02 11:25:52 $
% Original Author: Jorge Luis Bernal Rusiel
% CVS Revision Info:
% $Author: jbernal$
% $Date: 2012/02/02 11:25:52 $
% $Revision: 1.1 $
%
if nargin < 2
error('Too few inputs');
end;
Qdec2 = {};
for i=1:length(Vars)
col = findQdecVar(Qdec1,Vars{i});
if ~isempty(col)
Qdec2 = [Qdec2 Qdec1(:,col)];
else
error(['The variable ''' Vars{i} ''' is not in the Qdec table']);
end;
end;
20 changes: 20 additions & 0 deletions Qdec/getQdecVars.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function Vars = getQdecVars(Qdec)
% Vars = getQdecVars(Qdec)
%
% Returns a one dimensional cell string array with the name of the variables
% in Qdec.
%
% Input
% Qdec: Two dimensional cell string array of Qdec data.
%
% Output
% Vars: One dimensional cell string array.
%
% $Revision: 1.1.1.1 $ $Date: 2012/02/02 11:25:52 $
% Original Author: Jorge Luis Bernal Rusiel
% CVS Revision Info:
% $Author: jbernal$
% $Date: 2012/02/02 11:25:52 $
% $Revision: 1.1 $
%
Vars = Qdec(1,:)';
31 changes: 31 additions & 0 deletions Qdec/num2Qdec.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function Qdec = num2Qdec(M,Vars)
% Qdec = num2Qdec(M,Vars)
%
% Converts numeric matrix M to two dimensional cell string array Qdec.
%
% Input
% M: Numeric matrix.
% Vars: One dimensional cell string array with the name of the Qdec
% variables corresponding to the columns of M.
%
% Output
% Qdec: Two dimensional cell string array of Qdec data .
%
% $Revision: 1.1.1.1 $ $Date: 2012/02/02 11:25:52 $
% Original Author: Jorge Luis Bernal Rusiel
% CVS Revision Info:
% $Author: jbernal$
% $Date: 2012/02/02 11:25:52 $
% $Revision: 1.1 $
%
if nargin < 2
error('Too few inputs');
end;
szM = size(M);
Qdec = cell(szM(1)+1,szM(2));
Qdec(1,1:end) = Vars;
for i=2:szM(1)+1
for j=1:szM(2)
Qdec{i,j} = num2str(M(i-1,j));
end;
end;
26 changes: 26 additions & 0 deletions Qdec/rmQdecCol.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function Qdec2 = rmQdecCol(Qdec1,col)
% Qdec2 = rmQdecCol(Qdec1,col)
%
% Removes the specify column from cell string array Qdec1.
%
% Input
% Qdec1: Two dimensional cell string array of Qdec data (eg. read with
% fReadQdec).
% col: Column to be removed.
%
% Output
% Qdec2: Two dimensional cell string array of Qdec data.
%
% $Revision: 1.1.1.1 $ $Date: 2012/02/02 11:25:52 $
% Original Author: Jorge Luis Bernal Rusiel
% CVS Revision Info:
% $Author: jbernal$
% $Date: 2012/02/02 11:25:52 $
% $Revision: 1.1 $
%
if nargin < 2
error('Too few inputs');
end;
Qdec2 = [Qdec1(:,1:col-1) Qdec1(:,col+1:end)];


34 changes: 34 additions & 0 deletions fs_read_Y.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function [Y,mri] = fs_read_Y(Y_fname)
% [Y,mri] = fs_read_Y(Y_fname)
%
% Reads Freesurfer's .mgh or .mgz data files (eg. cortical thickness data
% files generated with mris_preproc).
%
% Input
% Y_fname: A Freesurfer's cortical data file.
%
% Output
% Y: Data matrix (nmxnv, nm total # of maps, nv #vertices).
% mri: Mri structure.
%
% $Revision: 1.1.1.1 $ $Date: 2012/02/02 11:25:52 $
% Original Author: Jorge Luis Bernal Rusiel
% CVS Revision Info:
% $Author: jbernal$
% $Date: 2012/02/02 11:25:52 $
% $Revision: 1.1 $
%

if nargin < 1
error('Too few inputs');
end;
mri = struct('M',[],'mr_parms',[],'volsz',[]);
[Y,M,mr_parms,volsz] = load_mgh(Y_fname);
if (isempty(M))
error(['Can not load ' Y_fname ' as an mgh or mgz file']);
end;
mri.M = M;
mri.mr_parms = mr_parms;
mri.volsz = volsz;
nv = volsz(1)*volsz(2)*volsz(3);
Y = reshape(Y,[nv volsz(4)])';
26 changes: 26 additions & 0 deletions fs_read_label.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function [vtxs,nvtxs] = fs_read_label(labelf)
% [vtxs,nvtxs] = fs_read_label(labelf)
%
% Reads the indices of the vertices of a Freesurfer's label.
%
% Input
% labelf: A Freesurfer's label file.
%
% Output
% vtxs: Indices of the vertices (1-based).
% nvtxs: The number of vertices in the label.
%
% $Revision: 1.1.1.1 $ $Date: 2012/02/02 11:25:52 $
% Original Author: Jorge Luis Bernal Rusiel
% CVS Revision Info:
% $Author: jbernal$
% $Date: 2012/02/02 11:25:52 $
% $Revision: 1.1 $
%
fid = fopen(labelf);
tline = fgetl(fid);
nvtxs = fscanf(fid,'%d',1);
vtxs = uint32(fscanf(fid,'%d %*g %*g %*g %*g',[1 nvtxs]));
vtxs = vtxs + 1;
fclose(fid);

25 changes: 25 additions & 0 deletions fs_read_surf.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function Surf = fs_read_surf(fname)
% Surf = fs_read_surf(fname)
%
% Reads coordinates and triangles of a Freesurfer's surf (Depends on
% Freesurfer's read_surf function).
%
% Input
% fname: A Freesurfer's surface file.
%
% Output
% Surf: Surface which represent a coordinate system where the analysis is
% to be done. It is a structure with Surf.tri = t x 3 matrix of triangle
% indices, 1-based, t=#triangles and Surf.coord = 3 x nv matrix of
% coordinates, nv=#vertices.
%
% $Revision: 1.1.1.1 $ $Date: 2012/02/02 11:25:52 $
% Original Author: Jorge Luis Bernal Rusiel
% CVS Revision Info:
% $Author: jbernal$
% $Date: 2012/02/02 11:25:52 $
% $Revision: 1.1 $
%
[coord,faces] = read_surf(fname);
Surf.tri = uint32(faces + 1);
Surf.coord = coord';
21 changes: 21 additions & 0 deletions fs_write_Y.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function fs_write_Y(Y,mri,fname)
% fs_write_Y(Y,mri,fname)
%
% Writes volume Y to a Freesurfer's .mgh or .mgz data file.
%
% Input
% Y: Data matrix or vector.
% mri: Mri structure (read with fs_read_Y).
% fname: Output file name.
%
% $Revision: 1.1.1.1 $ $Date: 2012/07/19 11:25:52 $
% Original Author: Jorge Luis Bernal Rusiel
% CVS Revision Info:
% $Author: jbernal$
% $Date: 2012/07/19 11:25:52 $
% $Revision: 1.1 $
%
if nargin < 3
error('Too few inputs');
end;
save_mgh(reshape(Y',mri.volsz),fname,mri.M,mri.mr_parms);
39 changes: 39 additions & 0 deletions fs_write_fstats.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function fs_write_fstats(fstats,mri,fname,data_type)
% fs_write_fstats(fstats,mri,fname,data_type)
%
% Writes F-statistic or p-value or Freesurfer significance maps to Freesurfer's
% .mgh or .mgz data file. This can be useful for visualization and post-processing
% in Freesurfer.
%
% Input
% fstats: Structure obtained from lme_mass_F.
% mri: Mri structure (read with fs_read_Y).
% fname: Output file name.
% data_type: Determines what is going to be written. This input can be one
% of three strings: 'fval' (signed F-statistic map), 'pval' (signed p-value map)
% or 'sig' (Freesurfer significance map -log10(pval).*sgn).
%
% $Revision: 1.1.1.1 $ $Date: 2012/07/19 11:25:52 $
% Original Author: Jorge Luis Bernal Rusiel
% CVS Revision Info:
% $Author: jbernal$
% $Date: 2012/07/19 11:25:52 $
% $Revision: 1.1 $
%
if nargin < 4
error('Too few inputs');
end;
mri.volsz(4) = 1;
if strcmpi(data_type,'fval')
fs_write_Y(fstats.F.*fstats.sgn,mri,fname);
elseif strcmpi(data_type,'pval')
pval = fstats.pval.*fstats.sgn;
pval(pval==1) = 1;
fs_write_Y(pval,mri,fname);
elseif strcmpi(data_type,'sig')
fs_write_Y(-log10(fstats.pval).*fstats.sgn,mri,fname);
else
error('Valid strings for data_type are ''fval'' or ''pval'' or ''sig''');
end;


Loading

0 comments on commit 4bca854

Please sign in to comment.