source: an2_data_read.m @ 73

Last change on this file since 73 was 69, checked in by tjniskan, 11 years ago
  • Added possibility to add the fMRI paradigm into the "View voxel

time-series" figure

  • Added global normalization routines for fMRI analysis
  • Minor fixes here and there

M misclib/fmri_analysis.m
M misclib/fmri_corr.m
M an2_revision.m
M an2_data_read.m
M aedes.m
M plugins/basic_fmri_analysis.m
M plugins/resting_state_fc.m

File size: 14.1 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  %% Get defaults for Reorienting EPI data
362  if ispref('Aedes','ReadfidReorientEPI')
363    ReorientEPI = getpref('Aedes','ReadfidReorientEPI');
364  else
365    ReorientEPI = 'off';
366  end
367 
368  %% Get defaults for removing phase image from EPI
369  if ispref('Aedes','ReadfidRemoveEPIphaseIm')
370    RemoveEPIphaseIm = getpref('Aedes','ReadfidRemoveEPIphaseIm');
371  else
372    RemoveEPIphaseIm = 'off';
373  end
374 
375 
376  %% Read data from fid file
377  try
378    DATA=an2_readfid([f_path,'fid'],...
379          'procpar',procpar,...
380          'wbar',showWbar,...
381          'Return',ReadfidReturn,...
382          'DCcorrection',ReadfidDCcorrection,...
383          'Zeropadding',ReadfidZeropadding,...
384          'sorting',ReadfidSorting,...
385          'FastRead',ReadfidFastRead,...
386          'Precision',ReadfidPrecision,...
387    'ReorientEPI',ReorientEPI,...
388    'RemoveEPIphaseIm',RemoveEPIphaseIm);
389    if isempty(DATA)
390      DATA=[];
391      return
392    end
393  catch
394    DATA=[];
395    error(lasterr)
396  end
397 
398 
399  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400  % Read DICOM image files (DCM)
401  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
402 case 'dcm'
403 
404  % Show an2_calc_wait
405  if showWbar
406    [h,txh]=an2_calc_wait('Reading data from DICOM file...');
407  end
408   
409  %% Set data format string
410  DATA.DataFormat = 'dcm';
411 
412  %% Read DICOM header
413  try
414    hdr = dicominfo(filename);
415    DATA.HDR=hdr;
416    DATA.HDR.fname = f_name;
417    DATA.HDR.fpath = f_path;
418  catch
419        DATA=[];
420        if showWbar
421          delete(h)
422        end
423        error('Could not read header from DICOM file "%s"',...
424          filename);
425       
426  end
427 
428  %% Read DICOM data
429  try
430    DATA.FTDATA=dicomread(hdr);
431  catch
432        DATA=[];
433        if showWbar
434          delete(h)
435        end
436        error('Could not read image data from DICOM file "%s"',...
437          filename);
438  end
439  if showWbar
440        delete(h)
441  end
442 
443 
444  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
445  % Read SPECT/CT Files
446  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
447 case 'spect/ct'
448 
449  %% Read Data
450  try
451    [DATA,msg]=an2_readctdata(filename);
452    if isempty(DATA)
453      return
454    end
455  catch
456        DATA=[];
457        error('An error occurred while reading SPECT/CT data from "%s"!',...
458          filename)
459  end
460 
461  %% Set data format string
462  DATA.DataFormat = 'spect/ct';
463 
464 
465  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
466  % Read Varian FDF Files
467  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
468  case 'fdf'
469   
470    %% Read data
471    DATA=an2_readfdf(filename);
472    if isempty(DATA)
473          DATA=[];
474          error('An error occurred while reading FDF file "%s"!',...
475                filename)
476        end
477       
478        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
479        % Read Aedes ROI-Files
480        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
481  case 'roi'
482       
483        %% Try to load the ROI-file
484    try
485      roifile = load(filename,'-mat');
486        catch
487          error('Error while reading ROI-file "%s".',...
488                filename)
489    end
490   
491    %% See if the ROI-file includes file information
492    if ~isfield(roifile,'FileInfo') || ...
493        any(cellfun(@isempty,roifile.FileInfo.DataFileName))
494          error('The ROI-file does not contain file information.')
495    end
496   
497    filenames=roifile.FileInfo.DataFileName;
498    pathnames=roifile.FileInfo.DataPathName;
499   
500    DATA={};
501        if showWbar
502          [calc_h,txh]=an2_calc_wait({['Reading file 1/' num2str(length(filenames))], ...
503                '""'});
504        end
505
506    %% Read the files
507    for ii=1:length(filenames)
508     
509      %% Check if the file exists
510      if exist([pathnames{ii},filenames{ii}],'file')==0
511        delete(calc_h)
512                DATA=[];
513                error('Cannot find the data file "%s".',...
514                  [pathnames{ii},filenames{ii}])
515      end
516     
517      set(txh,'String',...
518              sprintf('%s\n%s',...
519                      ['Reading file ' num2str(ii) ...
520                       '/' num2str(length(filenames))],...
521                      ['"',pathnames{ii},filenames{ii},'"']))
522      drawnow
523         
524          try
525                DATA{ii}=an2_data_read([pathnames{ii},filenames{ii}],'wbar','off');
526          catch
527                error(['Unknown error occurred while reading ROI-file "%s". ',...
528                  lasterr],...
529                  [pathnames{ii},filenames{ii}])
530          end
531    end
532    delete(calc_h)
533   
534    %Dat.LoadRoiAtStartUp = true;
535   
536    %% Use rotation information
537    if isfield(roifile,'RotateFlip')
538          if iscell(roifile.RotateFlip)
539                RotateFlip3d = roifile.RotateFlip;
540                DataRotation = false(1,length(DATA));
541                DataFlip = false(1,length(DATA));
542          else
543                DataRotation = roifile.RotateFlip.Rotate;
544                DataFlip = roifile.RotateFlip.Flip;
545                RotateFlip3d = {};
546          end
547        else
548          DataRotation = false(1,length(DATA));
549          DataFlip = false(1,length(DATA));
550          RotateFlip3d = {};
551        end
552       
553        %% Use SliceClim information
554        if isfield(roifile,'SliceClim')
555          DATA{1}.SliceClim = SliceClim;
556        end
557   
558    %% Assign the ROI-structure to the first structure
559    DATA{1}.ROI = roifile.ROI;
560    clear('roifile')
561   
562    %% Rotate images if necessary
563        if ~isempty(RotateFlip3d)
564         
565          for ii=1:length(RotateFlip3d)
566                if strcmpi(RotateFlip3d{ii}{1},'rotate')
567                  k = RotateFlip3d{ii}{2};
568                  dim = RotateFlip3d{ii}{3};
569                  DATA{1}.FTDATA = an2_rot3d(DATA{1}.FTDATA,k,dim);
570                elseif strcmpi(RotateFlip3d{ii}{1},'flip')
571                  dim = RotateFlip3d{ii}{2};
572                  DATA{1}.FTDATA = flipdim(DATA{1}.FTDATA,dim);
573                end
574          end
575          DATA{1}.RotateFlip3d = RotateFlip3d;
576        elseif ( ~all(DataRotation==0) || ~all(DataFlip==0) )
577      for ii=1:length(DATA)
578        if DataRotation(ii)~=0
579          DATA{ii}.FTDATA = rot90(DATA{ii}.FTDATA,DataRotation(ii));
580        end
581     
582        if DataFlip(ii)~=0
583          if DataFlip(ii)==1
584            DATA{ii}.FTDATA = flipud(DATA{ii}.FTDATA);
585          elseif DataFlip(ii)==2
586            DATA{ii}.FTDATA = fliplr(DATA{ii}.FTDATA);
587          end
588        end
589          end
590          DATA{1}.DataRotation = DataRotation;
591          DATA{1}.DataFlip = DataFlip;
592        end
593       
594   
595 
596        %%%%%%%%%%%%%%%%%%%%%%%%%%%%
597        % Unknown File Format
598        %%%%%%%%%%%%%%%%%%%%%%%%%%%%
599  otherwise
600        DATA=[];
601        error('Unknown file format')
602end
603
Note: See TracBrowser for help on using the repository browser.

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