source: aedes_data_read.m @ 174

Last change on this file since 174 was 174, checked in by tjniskan, 8 years ago
  • Added first alpha version for Bruker data support.

NOTE: Only reconstructed 2dseq files work at this time. This is the first alpha version so caution is adviced...

M aedes_getfilefilter.m
A aedes_readbruker.m
M aedes_data_read.m
M aedes_revision.m
M aedes_getdataformat.m

File size: 15.7 KB
Line 
1function DATA = aedes_data_read(filename,varargin)
2% AEDES_DATA_READ - Read various image data formats to data structure
3%   
4%
5% Synopsis:
6%       DATA=aedes_data_read(filename,file_format,default_dir,varargin)
7%       
8%       or
9%
10%       DATA=aedes_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=aedes_data_read;   % Read image data
29%       aedes(DATA)    % Open data in Aedes
30%
31% See also:
32%       AEDES_READFID, AEDES_READCTDATA, AEDES_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] = aedes_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 = aedes_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 = aedes_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 aedes_calc_wait
132  if showWbar
133    [h,txh]=aedes_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(strncmpi(tmp,'data',4)) || ...
143      any(strncmpi(tmp,'images',6)))
144    if showWbar
145      delete(h)
146    end
147    DATA=[];
148    error('The MAT-file doesn''t contain the required variable "Data" or "images"!')
149  end
150 
151  % Use the "data" variable by default
152  ind=find(strncmpi(tmp,'data',4));
153  if isempty(ind)
154    % If "data" variable is not found, use the "images" variable
155    ind=find(strncmpi(tmp,'images',6));
156  end
157  dataFieldName = tmp{ind(1)};
158 
159  if showWbar
160    set(txh,'string',sprintf('%s\n%s',...
161          'Loading data from Matlab MAT-File...',...
162          ['using variable "',dataFieldName,'"']));
163    drawnow
164  end
165 
166  % Load MAT-file
167  try
168    img=load(filename,'-mat');
169  catch
170    if showWbar
171      delete(h)
172        end
173        DATA=[];
174        error('Could not read MAT-file "%s"',filename)
175  end
176 
177  % Check if data is structure or matrix
178  data = img.(dataFieldName);
179  if isstruct(data) || iscell(data)
180    DATA = data;
181    if isfield(img,'DataRotation') && ...
182        isfield(img,'DataFlip') && iscell(DATA)
183      DATA{1}.DataRotation = img.DataRotation;
184      DATA{1}.DataFlip = img.DataFlip;
185    end
186    if isfield(img,'SliceClim') && iscell(DATA)
187      DATA{1}.SliceClim = img.SliceClim;
188    end
189  elseif isnumeric(data) || islogical(data)
190     DATA.FTDATA = img.(dataFieldName);
191     DATA.HDR.fname = f_name;
192     DATA.HDR.fpath = f_path;
193     DATA.HDR.DataFormat = dataformat;
194  else
195    if showWbar
196      delete(h)
197    end
198    clear img data;
199        DATA=[];
200        error('The variable "DATA" is invalid!')
201  end
202       
203  if showWbar
204        pause(0.3)
205    delete(h)
206  end
207 
208 
209 %%%%%%%%%%%%%%%%%%%%%%%%%%%%
210 % Read S.M.I.S. SUR-files
211 %%%%%%%%%%%%%%%%%%%%%%%%%%%%
212 case {'sur','mri'}
213 
214  %% Set data format string
215  try
216        DATA=aedes_smisread(filename);
217  catch
218        DATA=[];
219        error('Could not read file "%s"!',filename)
220  end
221 
222  %%%%%%%%%%%%%%%%%%%%%%%%%%%
223  % Read S.M.I.S. MRD-files
224  %%%%%%%%%%%%%%%%%%%%%%%%%%%
225  case 'mrd'
226       
227        % To be written ...
228        error('Reading of S.M.I.S. MRD-Files has not been implemented!')
229 
230  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
231  % Read Analyze 7.5 and NIfTI files
232  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233 case {'nifti'}
234 
235  % Show aedes_calc_wait
236  if showWbar
237    [h,txh]=aedes_calc_wait('Reading data in NIfTI/Analyze75 format...');
238  end
239 
240  % Read NIfTI and Analyze 7.5 header
241  [fp,fn,fe] = fileparts(filename);
242  if ~isempty(fe) && strcmpi(fe,'.img')
243    [DATA,msg]=aedes_read_nifti(filename);
244    if isempty(DATA)
245      if showWbar
246        delete(h)
247      end
248      error('Could not read data from file "%s"!',filename)
249    end
250    delete(h);
251  else
252    [DATA,msg]=aedes_read_nifti(filename,'header');
253    if isempty(DATA)
254      if showWbar
255        delete(h)
256      end
257      error('Could not read header from file "%s"!',filename)
258    end
259   
260    % Read NIfTI and Analyze 7.5 format data
261    [DATA,msg]=aedes_read_nifti(DATA.HDR);
262    if isempty(DATA)
263      if showWbar
264        delete(h)
265          end
266          error('Could not read data from file "%s"!',filename)
267    end
268    if showWbar
269      delete(h);
270    end
271  end
272 
273  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274  % Read Varian VNMR files (FID)
275  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 case 'vnmr'
277 
278  if showWbar
279    showWbar = 'on';
280  else
281    showWbar = 'off';
282  end
283 
284  %% Read parameters from procpar file
285  [procpar,msg]=aedes_readprocpar([f_path,'procpar']);%,'wbar',showWbar);
286  if isempty(procpar)
287    DATA=[];
288    error(msg);
289    return
290  end
291 
292  %% Fallback defaults for reading VNMR files
293  ReadfidReturn = 1;
294  ReadfidDCcorrection = 'off';
295  ReadfidZeropadding = 'auto';
296  ReadfidSorting = 'on';
297  ReadfidFastRead = 'on';
298  ReadfidPrecision = 'single';
299  OrientImages = 'on';
300  RemoveEPIphaseIm = 'off';
301  VnmrUseOldReadFcn = true;
302 
303  %% Get defaults for Return
304  if ispref('Aedes','ReadfidReturn')
305    ReadfidReturn = getpref('Aedes','ReadfidReturn');
306  end
307
308  %% Get defaults for DC correction
309  if ispref('Aedes','ReadfidDCcorrection')
310    if getpref('Aedes','ReadfidDCcorrection')
311      ReadfidDCcorrection = 'on';
312    else
313      ReadfidDCcorrection = 'off';
314    end
315  end
316
317  %% Get defaults for Zeropadding
318  if ispref('Aedes','ReadfidZeropadding')
319    if getpref('Aedes','ReadfidZeropadding')==0
320      ReadfidZeropadding = 'off';
321    elseif getpref('Aedes','ReadfidZeropadding')==1
322      ReadfidZeropadding = 'on';
323    elseif getpref('Aedes','ReadfidZeropadding')==2
324      ReadfidZeropadding = 'auto';
325    else
326      ReadfidZeropadding = 'auto';
327    end
328  end
329
330  %% Get defaults for Sorting
331  if ispref('Aedes','ReadfidSorting')
332    if getpref('Aedes','ReadfidSorting')
333      ReadfidSorting = 'on';
334    else
335      ReadfidSorting = 'off';
336    end
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  end
347 
348  %% Get defaults for Precision
349  if ispref('Aedes','ReadfidPrecision')
350    if strcmpi(getpref('Aedes','ReadfidPrecision'),'single')
351      ReadfidPrecision = 'single';
352    else
353      ReadfidPrecision = 'double';
354    end
355  end
356 
357%   %% Get defaults for Reorienting EPI data
358%   if ispref('Aedes','ReadfidReorientEPI')
359%     ReorientEPI = getpref('Aedes','ReadfidReorientEPI');
360%   else
361%     ReorientEPI = 'off';
362%   end
363 
364  %% Get defaults for Reorienting EPI data
365  if ispref('Aedes','ReadfidOrientImages')
366    OrientImages = getpref('Aedes','ReadfidOrientImages');
367  else
368    OrientImages = 'on';
369  end
370 
371  %% Get defaults for removing phase image from EPI
372  if ispref('Aedes','ReadfidRemoveEPIphaseIm')
373    RemoveEPIphaseIm = getpref('Aedes','ReadfidRemoveEPIphaseIm');
374  else
375    RemoveEPIphaseIm = 'off';
376  end
377 
378  %% Get default read function
379  if ispref('Aedes','VnmrUseOldReadFcn')
380    VnmrUseOldReadFcn = getpref('Aedes','VnmrUseOldReadFcn');
381  else
382    VnmrUseOldReadFcn = true;
383  end
384 
385 
386 
387  %% Read data from fid file
388  try
389    if VnmrUseOldReadFcn
390      DATA=aedes_readfid([f_path,'fid'],...
391        'procpar',procpar,...
392        'wbar',showWbar,...
393        'Return',ReadfidReturn,...
394        'DCcorrection',ReadfidDCcorrection,...
395        'Zeropadding',ReadfidZeropadding,...
396        'sorting',ReadfidSorting,...
397        'FastRead',ReadfidFastRead,...
398        'Precision',ReadfidPrecision,...
399        'OrientImages',OrientImages,...
400        'RemoveEPIphaseIm',RemoveEPIphaseIm);
401    else
402      DATA=aedes_readvnmr([f_path,'fid'],...
403        'procpar',procpar,...
404        'wbar',showWbar,...
405        'Return',ReadfidReturn,...
406        'DCcorrection',ReadfidDCcorrection,...
407        'Zeropadding',ReadfidZeropadding,...
408        'sorting',ReadfidSorting,...
409        'FastRead',ReadfidFastRead,...
410        'Precision',ReadfidPrecision,...
411        'OrientImages',OrientImages,...
412        'RemoveEPIphaseIm',RemoveEPIphaseIm);
413    end
414    if isempty(DATA)
415      DATA=[];
416      error('Unknown error while reading "%s".',[f_path,'fid'])
417      return
418    end
419  catch
420    DATA=[];
421    error(lasterr)
422        end
423 
424        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
425  % Read Bruker file formats
426  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
427        case {'bruker_raw','bruker_reco'}
428
429                % Read Bruker data file
430                DATA = aedes_readbruker(filename);
431               
432               
433  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
434  % Read DICOM image files (DCM)
435  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436 case 'dcm'
437 
438  % Show aedes_calc_wait
439  if showWbar
440    [h,txh]=aedes_calc_wait('Reading data from DICOM file...');
441  end
442   
443  %% Set data format string
444  DATA.DataFormat = 'dcm';
445 
446  %% Read DICOM header
447  try
448    hdr = dicominfo(filename);
449    DATA.HDR.FileHeader=hdr;
450    DATA.HDR.fname = f_name;
451    DATA.HDR.fpath = f_path;
452  catch
453        DATA=[];
454        if showWbar
455          delete(h)
456        end
457        error('Could not read header from DICOM file "%s"',...
458          filename);
459       
460  end
461 
462  %% Read DICOM data
463  try
464    DATA.FTDATA=dicomread(hdr);
465  catch
466        DATA=[];
467        if showWbar
468          delete(h)
469        end
470        error('Could not read image data from DICOM file "%s"',...
471          filename);
472  end
473  if showWbar
474        delete(h)
475  end
476 
477 
478  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
479  % Read SPECT/CT Files
480  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
481 case 'spect/ct'
482 
483  %% Read Data
484  try
485    [DATA,msg]=aedes_readctdata(filename);
486    if isempty(DATA)
487      return
488    end
489  catch
490        DATA=[];
491        error('An error occurred while reading SPECT/CT data from "%s"!',...
492          filename)
493  end
494 
495  %% Set data format string
496  DATA.DataFormat = 'spect/ct';
497 
498 
499  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
500  % Read Varian FDF Files
501  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
502  case 'fdf'
503   
504    %% Read data
505    DATA=aedes_readfdf(filename);
506    if isempty(DATA)
507      DATA=[];
508      error('An error occurred while reading FDF file "%s"!',...
509        filename)
510    end
511 
512  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
513  % Read SWIFT SGL-Files
514  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
515  case 'swift_sgl'
516   
517    % Read data
518    [DATA,msg]=aedes_readswiftsgl(filename,...
519      'datainterpsizeprompt',true,...
520      'wbar',true);
521    if isempty(DATA)
522      error(msg)
523    end
524   
525        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
526        % Read Aedes ROI-Files
527        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
528  case 'roi'
529       
530        %% Try to load the ROI-file
531    try
532      roifile = load(filename,'-mat');
533        catch
534          error('Error while reading ROI-file "%s".',...
535                filename)
536    end
537   
538    %% See if the ROI-file includes file information
539    if ~isfield(roifile,'FileInfo') || ...
540        any(cellfun(@isempty,roifile.FileInfo.DataFileName))
541          error('The ROI-file does not contain file information.')
542    end
543   
544    filenames=roifile.FileInfo.DataFileName;
545    pathnames=roifile.FileInfo.DataPathName;
546   
547    DATA={};
548        if showWbar
549          [calc_h,txh]=aedes_calc_wait({['Reading file 1/' num2str(length(filenames))], ...
550                '""'});
551        end
552
553    %% Read the files
554    for ii=1:length(filenames)
555     
556      %% Check if the file exists
557      if exist([pathnames{ii},filenames{ii}],'file')==0
558        delete(calc_h)
559                DATA=[];
560                error('Cannot find the data file "%s".',...
561                  [pathnames{ii},filenames{ii}])
562      end
563     
564      set(txh,'String',...
565              sprintf('%s\n%s',...
566                      ['Reading file ' num2str(ii) ...
567                       '/' num2str(length(filenames))],...
568                      ['"',pathnames{ii},filenames{ii},'"']))
569      drawnow
570         
571          try
572                DATA{ii}=aedes_data_read([pathnames{ii},filenames{ii}],'wbar','off');
573          catch
574                error(['Unknown error occurred while reading ROI-file "%s". ',...
575                  lasterr],...
576                  [pathnames{ii},filenames{ii}])
577          end
578    end
579    delete(calc_h)
580   
581    %Dat.LoadRoiAtStartUp = true;
582   
583    %% Use rotation information
584    if isfield(roifile,'RotateFlip')
585          if iscell(roifile.RotateFlip)
586                RotateFlip3d = roifile.RotateFlip;
587                DataRotation = false(1,length(DATA));
588                DataFlip = false(1,length(DATA));
589          else
590                DataRotation = roifile.RotateFlip.Rotate;
591                DataFlip = roifile.RotateFlip.Flip;
592                RotateFlip3d = {};
593          end
594        else
595          DataRotation = false(1,length(DATA));
596          DataFlip = false(1,length(DATA));
597          RotateFlip3d = {};
598        end
599       
600        %% Use SliceClim information
601        if isfield(roifile,'SliceClim')
602          DATA{1}.SliceClim = SliceClim;
603        end
604   
605    %% Assign the ROI-structure to the first structure
606    DATA{1}.ROI = roifile.ROI;
607    clear('roifile')
608   
609    %% Rotate images if necessary
610        if ~isempty(RotateFlip3d)
611         
612          for ii=1:length(RotateFlip3d)
613                if strcmpi(RotateFlip3d{ii}{1},'rotate')
614                  k = RotateFlip3d{ii}{2};
615                  dim = RotateFlip3d{ii}{3};
616                  DATA{1}.FTDATA = aedes_rot3d(DATA{1}.FTDATA,k,dim);
617                elseif strcmpi(RotateFlip3d{ii}{1},'flip')
618                  dim = RotateFlip3d{ii}{2};
619                  DATA{1}.FTDATA = flipdim(DATA{1}.FTDATA,dim);
620                end
621          end
622          DATA{1}.RotateFlip3d = RotateFlip3d;
623        elseif ( ~all(DataRotation==0) || ~all(DataFlip==0) )
624      for ii=1:length(DATA)
625        if DataRotation(ii)~=0
626          DATA{ii}.FTDATA = rot90(DATA{ii}.FTDATA,DataRotation(ii));
627        end
628     
629        if DataFlip(ii)~=0
630          if DataFlip(ii)==1
631            DATA{ii}.FTDATA = flipud(DATA{ii}.FTDATA);
632          elseif DataFlip(ii)==2
633            DATA{ii}.FTDATA = fliplr(DATA{ii}.FTDATA);
634          end
635        end
636          end
637          DATA{1}.DataRotation = DataRotation;
638          DATA{1}.DataFlip = DataFlip;
639        end
640       
641   
642 
643        %%%%%%%%%%%%%%%%%%%%%%%%%%%%
644        % Unknown File Format
645        %%%%%%%%%%%%%%%%%%%%%%%%%%%%
646  otherwise
647        DATA=[];
648        error('Unknown file format')
649end
650
Note: See TracBrowser for help on using the repository browser.

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