source: an2_data_read.m @ 78

Last change on this file since 78 was 78, checked in by tjniskan, 10 years ago
  • Moved fMRI related plugins to their own subdirectories
  • Created a subdirectory for map calculation plugins and added a few

basic plugins for those. At the moment these plugins do not support
masking etc. and I didn't test them at all so be cautious...

  • Made a minor memory optimation to an2_readfid.m
  • Fixed a few issues in files related updating via SVN
  • Disabled some annoying warnings that Matlab R2008a and higher

generate from uitree and uitreenode functions inan2_juigetfiles.m

  • Coded something minor to an2_maptool.m. This is still stub and does

not work at all. Please don't try to use it.

  • Fixed a bug that caused Aedes to fail without generating an error

when reading VNMR FID-files that don't have PROCPAR in the same folder.

  • Fixed a javaFrame -warning (with R2008a and higher) in aedes.m

M an2_revision.m
M an2_data_read.m
M an2_maptool.m
M aedes.m
D plugins/calculate_t2_map.m
D plugins/calculate_t1r_map.m
D plugins/basic_fmri_analysis.m
D plugins/resting_state_fc.m
A plugins/fmri_plugins
A plugins/fmri_plugins/basic_fmri_analysis.m
A plugins/fmri_plugins/resting_state_fc.m
A plugins/map_plugins
A plugins/map_plugins/t2.m
A plugins/map_plugins/t1_3_parameter_fit.m
A plugins/map_plugins/t1_saturation_recovery.m
A plugins/map_plugins/t1_rho.m
A plugins/map_plugins/t2_rho.m
A plugins/map_plugins/t1_inversion_recovery.m
M an2_readfid.m
M an2_check_updates.m
M an2_juigetfiles.m
M an2_update.m

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

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