source: an2_data_read.m @ 63

Last change on this file since 63 was 62, checked in by tjniskan, 11 years ago
  • Added functions for basic fMRI analysis and resting-state
  • Added trend in the "show voxel timeseries" figure. In EPI datas the

the reference image is excluded from the time series and the y-axis
units are changed to "BOLD (%)".

  • Fixed an endiannes bug in an2_readfdf.m
  • Added plugins basic fmri analysis and resting-state analysis

M misclib/fmri_spm_volumes.m
A misclib/fmri_analysis.m
M an2_readfdf.m
M an2_revision.m
M an2_fitmaps.m
M an2_data_read.m
M aedes.m
A plugins/basic_fmri_analysis.m
A plugins/resting_state_fc.m

File size: 13.7 KB
Line 
1function DATA = an2_data_read(filename,varargin)
2% AN2_DATA_READ - Read various image data formats to data structure
3%   
4%
5% Synopsis:
6%       DATA=an2_data_read(filename,file_format,default_dir,varargin)
7%       
8%       or
9%
10%       DATA=an2_data_read;  % (interactive mode, opens a file dialog)
11%
12% Description:
13%       The function reads image data from various different file formats
14%       into a DATA-structure. The first input argument "filename" is the
15%       full path to the data file. If the first input argument is given as
16%       an empty string, the open file dialog is shown. The second input
17%       argument is format string that defines the data format; valid
18%       format strings are: 'vnmr', 'nifti', 'sur', 'mri', 'dcm',
19%       'spect/ct', 'mat'. If the format string is not given as an input
20%       argument the file extension is used to determine the data format.
21%
22%       The "default_dir" input argument is a path string defining the
23%       default directory for the open file dialog. If "default_dir" is
24%       omitted, the current directory (pwd) is used to open the file
25%       dialog.
26%
27% Examples:
28%       DATA=an2_data_read;   % Read image data
29%       aedes(DATA)    % Open data in Aedes
30%
31% See also:
32%       AN2_READFID, AN2_READCTDATA, AN2_READ_NIFTI, AEDES
33
34% This function is a part of Aedes - A graphical tool for analyzing
35% medical images
36%
37% Copyright (C) 2006 Juha-Pekka Niskanen <Juha-Pekka.Niskanen@uku.fi>
38%
39% Department of Physics, Department of Neurobiology
40% University of Kuopio, FINLAND
41%
42% This program may be used under the terms of the GNU General Public
43% License version 2.0 as published by the Free Software Foundation
44% and appearing in the file LICENSE.TXT included in the packaging of
45% this program.
46%
47% This program is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
48% WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
49
50
51showWbar = true; % Show wbar by default
52ddir = [pwd,filesep];
53if ~isempty(varargin)
54  if any(strcmpi(varargin,'default_dir'))
55    ind=find((strcmpi(varargin,'default_dir')));
56    try
57      ddir = varargin{ind+1};
58    catch
59      ddir = [pwd,filesep];
60    end
61  end
62end
63
64
65%% Parse input arguments
66if nargin<1 || isempty(filename)
67 
68  % Check if default directory is given
69  %if nargin==3
70  %  ddir = default_dir;
71  %else
72  %  ddir = [pwd,filesep];
73  %end
74  [filefilt,dataformats] = an2_getfilefilter;
75  [f_name, f_path, f_index] = uigetfile(...
76        filefilt, ...
77        'Select data file',ddir,...
78        'MultiSelect', 'off');
79  if isequal(f_name,0) % Cancel is pressed
80        DATA=[];
81        return
82  end
83  filename = fullfile(f_path,f_name);
84  dataformat = an2_getdataformat(filename);
85 
86elseif nargin>=1
87  if ischar(filename)
88    [f_path,f_name,f_ext] = fileparts(filename);
89    f_path=[f_path,filesep];
90        f_name = [f_name,f_ext];
91        dataformat = an2_getdataformat(filename);
92  else
93    error('First input argument has to be of class STRING!')
94  end
95end
96
97% Parse varargin
98for ii=1:2:length(varargin)
99  switch lower(varargin{ii})
100   case 'wbar'
101    if strcmpi(varargin{ii+1},'on')
102      showWbar = true;
103    else
104      showWbar = false;
105    end
106   
107    case 'dataformat'
108          if ~isempty(varargin{ii+1})
109                dataformat = varargin{ii+1};
110          end
111     
112    case 'default_dir'
113      % This is just a dummy case to prevent from accidently going to
114      % the "otherwise" case...
115     
116    otherwise
117      error('Unknown parameter "%s"',varargin{ii})
118   
119  end
120end
121
122
123%% Read data
124switch dataformat
125 
126  %%%%%%%%%%%%%%%%%%%%%%%%%%%
127  % Read Matlab MAT-File
128  %%%%%%%%%%%%%%%%%%%%%%%%%%%
129 case 'mat'
130
131  % Show an2_calc_wait
132  if showWbar
133    [h,txh]=an2_calc_wait('Loading data from Matlab MAT-File...');
134    drawnow
135  end
136 
137  %% Set data format string
138  DATA.DataFormat = 'mat';
139 
140  %% Check variables in the MAT-file
141  tmp=who('-file',filename);
142  if isempty(tmp) || ~any(ismember(lower(tmp),{'data','images'}))
143    if showWbar
144      delete(h)
145        end
146        DATA=[];
147        error('The MAT-file doesn''t contain the required variable "Data" or "images"!')
148  end
149 
150  % Use the "data" variable by default
151  ind=find(strcmpi(tmp,'data'));
152  if isempty(ind)
153        % If "data" variable is not found, use the "images" variable
154        ind=find(strcmpi(tmp,'images'));
155  end
156  dataFieldName = tmp{ind(1)};
157 
158  if showWbar
159    set(txh,'string',sprintf('%s\n%s',...
160          'Loading data from Matlab MAT-File...',...
161          ['using variable "',dataFieldName,'"']));
162    drawnow
163  end
164 
165  % Load MAT-file
166  try
167    img=load(filename,'-mat');
168  catch
169    if showWbar
170      delete(h)
171        end
172        DATA=[];
173        error('Could not read MAT-file "%s"',filename)
174  end
175 
176  % Check if data is structure or matrix
177  data = img.(dataFieldName);
178  if isstruct(data) || iscell(data)
179    DATA = data;
180    if isfield(img,'DataRotation') && ...
181        isfield(img,'DataFlip') && iscell(DATA)
182      DATA{1}.DataRotation = img.DataRotation;
183      DATA{1}.DataFlip = img.DataFlip;
184    end
185    if isfield(img,'SliceClim') && iscell(DATA)
186      DATA{1}.SliceClim = img.SliceClim;
187    end
188  elseif isnumeric(data) || islogical(data)
189     DATA.FTDATA = img.(dataFieldName);
190     DATA.HDR.fname = f_name;
191     DATA.HDR.fpath = f_path;
192     DATA.HDR.DataFormat = dataformat;
193  else
194    if showWbar
195      delete(h)
196    end
197    clear img data;
198        DATA=[];
199        error('The variable "DATA" is invalid!')
200  end
201       
202  if showWbar
203        pause(0.3)
204    delete(h)
205  end
206 
207 
208 %%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 % Read S.M.I.S. SUR-files
210 %%%%%%%%%%%%%%%%%%%%%%%%%%%%
211 case {'sur','mri'}
212 
213  %% Set data format string
214  try
215        DATA=an2_smisread(filename);
216  catch
217        DATA=[];
218        error('Could not read file "%s"!',filename)
219  end
220 
221  %%%%%%%%%%%%%%%%%%%%%%%%%%%
222  % Read S.M.I.S. MRD-files
223  %%%%%%%%%%%%%%%%%%%%%%%%%%%
224  case 'mrd'
225       
226        % To be written ...
227        error('Reading of S.M.I.S. MRD-Files has not been implemented!')
228 
229  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
230  % Read Analyze 7.5 and NIfTI files
231  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232 case {'nifti'}
233 
234  % Show an2_calc_wait
235  if showWbar
236    [h,txh]=an2_calc_wait('Reading data in NIfTI/Analyze75 format...');
237  end
238 
239  % Read NIfTI and Analyze 7.5 header
240  [fp,fn,fe] = fileparts(filename);
241  if ~isempty(fe) && strcmpi(fe,'.img')
242    [DATA,msg]=an2_read_nifti(filename);
243    if isempty(DATA)
244      if showWbar
245        delete(h)
246          end
247        error('Could not read data from file "%s"!',filename)
248    end
249    delete(h);
250  else
251    [DATA,msg]=an2_read_nifti(filename,'header');
252    if isempty(DATA)
253      if showWbar
254        delete(h)
255          end
256          error('Could not read header from file "%s"!',filename)
257    end
258   
259    % Read NIfTI and Analyze 7.5 format data
260    [DATA,msg]=an2_read_nifti(DATA.HDR);
261    if isempty(DATA)
262      if showWbar
263        delete(h)
264          end
265          error('Could not read data from file "%s"!',filename)
266    end
267    if showWbar
268      delete(h);
269    end
270  end
271 
272  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273  % Read Varian VNMR files (FID)
274  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 case 'vnmr'
276 
277  if showWbar
278    showWbar = 'on';
279  else
280    showWbar = 'off';
281  end
282 
283  %% Read parameters from procpar file
284  try
285    [procpar,msg]=an2_readprocpar([f_path,'procpar']);%,'wbar',showWbar);
286    if isempty(procpar)
287      DATA=[];
288      return
289    end
290  catch
291        DATA=[];
292        error('An error occurred while reading procpar file!')
293  end
294 
295  %% Get defaults for Return
296  if ispref('Aedes','ReadfidReturn')
297        ReadfidReturn = getpref('Aedes','ReadfidReturn');
298  else
299        ReadfidReturn = 1;
300  end
301
302  %% Get defaults for DC correction
303  if ispref('Aedes','ReadfidDCcorrection')
304        if getpref('Aedes','ReadfidDCcorrection')
305          ReadfidDCcorrection = 'on';
306        else
307          ReadfidDCcorrection = 'off';
308        end
309  else
310        ReadfidDCcorrection = 'off';
311  end
312
313  %% Get defaults for Zeropadding
314  if ispref('Aedes','ReadfidZeropadding')
315        if getpref('Aedes','ReadfidZeropadding')==0
316          ReadfidZeropadding = 'off';
317        elseif getpref('Aedes','ReadfidZeropadding')==1
318          ReadfidZeropadding = 'on';
319        elseif getpref('Aedes','ReadfidZeropadding')==2
320          ReadfidZeropadding = 'auto';
321        else
322          ReadfidZeropadding = 'auto';
323        end
324  else
325        ReadfidZeropadding = 'auto';
326  end
327
328  %% Get defaults for Sorting
329  if ispref('Aedes','ReadfidSorting')
330        if getpref('Aedes','ReadfidSorting')
331          ReadfidSorting = 'on';
332        else
333          ReadfidSorting = 'off';
334        end
335  else
336        ReadfidSorting = 'on';
337  end
338
339  %% Get defaults for FastRead
340  if ispref('Aedes','ReadfidFastRead')
341        if getpref('Aedes','ReadfidFastRead')
342          ReadfidFastRead = 'on';
343        else
344          ReadfidFastRead = 'off';
345        end
346  else
347        ReadfidFastRead = 'off';
348  end
349 
350  %% Get defaults for Precision
351  if ispref('Aedes','ReadfidPrecision')
352        if strcmpi(getpref('Aedes','ReadfidPrecision'),'single')
353          ReadfidPrecision = 'single';
354        else
355          ReadfidPrecision = 'double';
356        end
357  else
358        ReadfidPrecision = 'double';
359  end
360 
361  %% Read data from fid file
362  try
363    [DATA,msg]=an2_readfid([f_path,'fid'],...
364          'procpar',procpar,...
365          'wbar',showWbar,...
366          'Return',ReadfidReturn,...
367          'DCcorrection',ReadfidDCcorrection,...
368          'Zeropadding',ReadfidZeropadding,...
369          'sorting',ReadfidSorting,...
370          'FastRead',ReadfidFastRead,...
371          'Precision',ReadfidPrecision);
372    if isempty(DATA)
373      DATA=[];
374      return
375    end
376  catch
377        DATA=[];
378        error('An error occurred while reading data from "%s"!',...
379          [f_path,'fid'])
380  end
381 
382 
383  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
384  % Read DICOM image files (DCM)
385  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
386 case 'dcm'
387 
388  % Show an2_calc_wait
389  if showWbar
390    [h,txh]=an2_calc_wait('Reading data from DICOM file...');
391  end
392   
393  %% Set data format string
394  DATA.DataFormat = 'dcm';
395 
396  %% Read DICOM header
397  try
398    hdr = dicominfo(filename);
399    DATA.HDR=hdr;
400    DATA.HDR.fname = f_name;
401    DATA.HDR.fpath = f_path;
402  catch
403        DATA=[];
404        if showWbar
405          delete(h)
406        end
407        error('Could not read header from DICOM file "%s"',...
408          filename);
409       
410  end
411 
412  %% Read DICOM data
413  try
414    DATA.FTDATA=dicomread(hdr);
415  catch
416        DATA=[];
417        if showWbar
418          delete(h)
419        end
420        error('Could not read image data from DICOM file "%s"',...
421          filename);
422  end
423  if showWbar
424        delete(h)
425  end
426 
427 
428  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
429  % Read SPECT/CT Files
430  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
431 case 'spect/ct'
432 
433  %% Read Data
434  try
435    [DATA,msg]=an2_readctdata(filename);
436    if isempty(DATA)
437      return
438    end
439  catch
440        DATA=[];
441        error('An error occurred while reading SPECT/CT data from "%s"!',...
442          filename)
443  end
444 
445  %% Set data format string
446  DATA.DataFormat = 'spect/ct';
447 
448 
449  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
450  % Read Varian FDF Files
451  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
452  case 'fdf'
453   
454    %% Read data
455    DATA=an2_readfdf(filename);
456    if isempty(DATA)
457          DATA=[];
458          error('An error occurred while reading FDF file "%s"!',...
459                filename)
460        end
461       
462        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
463        % Read Aedes ROI-Files
464        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
465  case 'roi'
466       
467        %% Try to load the ROI-file
468    try
469      roifile = load(filename,'-mat');
470        catch
471          error('Error while reading ROI-file "%s".',...
472                filename)
473    end
474   
475    %% See if the ROI-file includes file information
476    if ~isfield(roifile,'FileInfo') || ...
477        any(cellfun(@isempty,roifile.FileInfo.DataFileName))
478          error('The ROI-file does not contain file information.')
479    end
480   
481    filenames=roifile.FileInfo.DataFileName;
482    pathnames=roifile.FileInfo.DataPathName;
483   
484    DATA={};
485        if showWbar
486          [calc_h,txh]=an2_calc_wait({['Reading file 1/' num2str(length(filenames))], ...
487                '""'});
488        end
489
490    %% Read the files
491    for ii=1:length(filenames)
492     
493      %% Check if the file exists
494      if exist([pathnames{ii},filenames{ii}],'file')==0
495        delete(calc_h)
496                DATA=[];
497                error('Cannot find the data file "%s".',...
498                  [pathnames{ii},filenames{ii}])
499      end
500     
501      set(txh,'String',...
502              sprintf('%s\n%s',...
503                      ['Reading file ' num2str(ii) ...
504                       '/' num2str(length(filenames))],...
505                      ['"',pathnames{ii},filenames{ii},'"']))
506      drawnow
507         
508          try
509                DATA{ii}=an2_data_read([pathnames{ii},filenames{ii}],'wbar','off');
510          catch
511                error(['Unknown error occurred while reading ROI-file "%s". ',...
512                  lasterr],...
513                  [pathnames{ii},filenames{ii}])
514          end
515    end
516    delete(calc_h)
517   
518    %Dat.LoadRoiAtStartUp = true;
519   
520    %% Use rotation information
521    if isfield(roifile,'RotateFlip')
522          if iscell(roifile.RotateFlip)
523                RotateFlip3d = roifile.RotateFlip;
524                DataRotation = false(1,length(DATA));
525                DataFlip = false(1,length(DATA));
526          else
527                DataRotation = roifile.RotateFlip.Rotate;
528                DataFlip = roifile.RotateFlip.Flip;
529                RotateFlip3d = {};
530          end
531        else
532          DataRotation = false(1,length(DATA));
533          DataFlip = false(1,length(DATA));
534          RotateFlip3d = {};
535        end
536       
537        %% Use SliceClim information
538        if isfield(roifile,'SliceClim')
539          DATA{1}.SliceClim = SliceClim;
540        end
541   
542    %% Assign the ROI-structure to the first structure
543    DATA{1}.ROI = roifile.ROI;
544    clear('roifile')
545   
546    %% Rotate images if necessary
547        if ~isempty(RotateFlip3d)
548         
549          for ii=1:length(RotateFlip3d)
550                if strcmpi(RotateFlip3d{ii}{1},'rotate')
551                  k = RotateFlip3d{ii}{2};
552                  dim = RotateFlip3d{ii}{3};
553                  DATA{1}.FTDATA = an2_rot3d(DATA{1}.FTDATA,k,dim);
554                elseif strcmpi(RotateFlip3d{ii}{1},'flip')
555                  dim = RotateFlip3d{ii}{2};
556                  DATA{1}.FTDATA = flipdim(DATA{1}.FTDATA,dim);
557                end
558          end
559          DATA{1}.RotateFlip3d = RotateFlip3d;
560        elseif ( ~all(DataRotation==0) || ~all(DataFlip==0) )
561      for ii=1:length(DATA)
562        if DataRotation(ii)~=0
563          DATA{ii}.FTDATA = rot90(DATA{ii}.FTDATA,DataRotation(ii));
564        end
565     
566        if DataFlip(ii)~=0
567          if DataFlip(ii)==1
568            DATA{ii}.FTDATA = flipud(DATA{ii}.FTDATA);
569          elseif DataFlip(ii)==2
570            DATA{ii}.FTDATA = fliplr(DATA{ii}.FTDATA);
571          end
572        end
573          end
574          DATA{1}.DataRotation = DataRotation;
575          DATA{1}.DataFlip = DataFlip;
576        end
577       
578   
579 
580        %%%%%%%%%%%%%%%%%%%%%%%%%%%%
581        % Unknown File Format
582        %%%%%%%%%%%%%%%%%%%%%%%%%%%%
583  otherwise
584        DATA=[];
585        error('Unknown file format')
586end
587
Note: See TracBrowser for help on using the repository browser.

Powered by Trac 1.0.9.Copyright © Juha-Pekka Niskanen 2008