source: aedes_data_read.m @ 147

Last change on this file since 147 was 147, checked in by tjniskan, 9 years ago
  • Added support for Windows network paths (
    server\share) to

aedes_juigetfiles.m (Windows only)

  • Added a link for suppressing the license information from printing to

workspace

  • Fixed some uicontrol colors in aedes_headerbrowser.m
  • Changed the buffer size for reading VNMR PROCPAR to 1MB. The

previous buffer size (10kB) was apparently too low for some SWIFT data

  • aedes_killfigs.m now has an option to force close only Aedes-related

figures

  • Updated figure tags for aedes_killfigs.m
  • Added a request for restarting Aedes to the "Update successful" dialog

M aedes_readprocpar.m
M aedes_killfigs.m
M aedes_data_read.m
M aedes_gui_defaults.m
M aedes_resviewer.m
M aedes.m
M aedes_update.m
M aedes_export_gui.m
M aedes_headerbrowser.m
M aedes_revision.m
M aedes_juigetfiles.m
M aedes_errordump.m

File size: 15.5 KB
RevLine 
[80]1function DATA = aedes_data_read(filename,varargin)
2% AEDES_DATA_READ - Read various image data formats to data structure
[2]3%   
4%
5% Synopsis:
[80]6%       DATA=aedes_data_read(filename,file_format,default_dir,varargin)
[2]7%       
8%       or
9%
[80]10%       DATA=aedes_data_read;  % (interactive mode, opens a file dialog)
[2]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
[46]17%       argument is format string that defines the data format; valid
18%       format strings are: 'vnmr', 'nifti', 'sur', 'mri', 'dcm',
[2]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:
[80]28%       DATA=aedes_data_read;   % Read image data
[37]29%       aedes(DATA)    % Open data in Aedes
[2]30%
31% See also:
[80]32%       AEDES_READFID, AEDES_READCTDATA, AEDES_READ_NIFTI, AEDES
[2]33
[39]34% This function is a part of Aedes - A graphical tool for analyzing
[36]35% medical images
[2]36%
[36]37% Copyright (C) 2006 Juha-Pekka Niskanen <Juha-Pekka.Niskanen@uku.fi>
38%
[45]39% Department of Physics, Department of Neurobiology
[39]40% University of Kuopio, FINLAND
[36]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.
[2]49
[36]50
[2]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
[80]74  [filefilt,dataformats] = aedes_getfilefilter;
[2]75  [f_name, f_path, f_index] = uigetfile(...
[24]76        filefilt, ...
77        'Select data file',ddir,...
78        'MultiSelect', 'off');
[37]79  if isequal(f_name,0) % Cancel is pressed
[24]80        DATA=[];
[37]81        return
[2]82  end
[24]83  filename = fullfile(f_path,f_name);
[80]84  dataformat = aedes_getdataformat(filename);
[2]85 
86elseif nargin>=1
87  if ischar(filename)
88    [f_path,f_name,f_ext] = fileparts(filename);
89    f_path=[f_path,filesep];
[24]90        f_name = [f_name,f_ext];
[80]91        dataformat = aedes_getdataformat(filename);
[2]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'
[24]108          if ~isempty(varargin{ii+1})
109                dataformat = varargin{ii+1};
110          end
[2]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
[80]131  % Show aedes_calc_wait
[2]132  if showWbar
[80]133    [h,txh]=aedes_calc_wait('Loading data from Matlab MAT-File...');
[2]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);
[123]142  if isempty(tmp) || ~(any(strncmpi(tmp,'data',4)) || ...
143      any(strncmpi(tmp,'images',6)))
[2]144    if showWbar
145      delete(h)
[103]146    end
147    DATA=[];
148    error('The MAT-file doesn''t contain the required variable "Data" or "images"!')
[2]149  end
[15]150 
151  % Use the "data" variable by default
[103]152  ind=find(strncmpi(tmp,'data',4));
[15]153  if isempty(ind)
[103]154    % If "data" variable is not found, use the "images" variable
155    ind=find(strncmpi(tmp,'images',6));
[15]156  end
[2]157  dataFieldName = tmp{ind(1)};
158 
[15]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 
[2]166  % Load MAT-file
167  try
168    img=load(filename,'-mat');
169  catch
170    if showWbar
171      delete(h)
[24]172        end
173        DATA=[];
174        error('Could not read MAT-file "%s"',filename)
[2]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);
[34]191     DATA.HDR.fname = f_name;
[2]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;
[24]199        DATA=[];
200        error('The variable "DATA" is invalid!')
[2]201  end
[24]202       
[2]203  if showWbar
[15]204        pause(0.3)
[2]205    delete(h)
206  end
207 
208 
209 %%%%%%%%%%%%%%%%%%%%%%%%%%%%
210 % Read S.M.I.S. SUR-files
211 %%%%%%%%%%%%%%%%%%%%%%%%%%%%
[40]212 case {'sur','mri'}
[2]213 
214  %% Set data format string
[40]215  try
[80]216        DATA=aedes_smisread(filename);
[40]217  catch
[24]218        DATA=[];
[40]219        error('Could not read file "%s"!',filename)
[2]220  end
221 
[40]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!')
[2]229 
230  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
231  % Read Analyze 7.5 and NIfTI files
232  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233 case {'nifti'}
234 
[80]235  % Show aedes_calc_wait
[2]236  if showWbar
[80]237    [h,txh]=aedes_calc_wait('Reading data in NIfTI/Analyze75 format...');
[2]238  end
239 
240  % Read NIfTI and Analyze 7.5 header
[46]241  [fp,fn,fe] = fileparts(filename);
242  if ~isempty(fe) && strcmpi(fe,'.img')
[80]243    [DATA,msg]=aedes_read_nifti(filename);
[2]244    if isempty(DATA)
245      if showWbar
246        delete(h)
[96]247      end
248      error('Could not read data from file "%s"!',filename)
[2]249    end
250    delete(h);
251  else
[80]252    [DATA,msg]=aedes_read_nifti(filename,'header');
[2]253    if isempty(DATA)
254      if showWbar
255        delete(h)
[96]256      end
257      error('Could not read header from file "%s"!',filename)
[2]258    end
259   
260    % Read NIfTI and Analyze 7.5 format data
[80]261    [DATA,msg]=aedes_read_nifti(DATA.HDR);
[2]262    if isempty(DATA)
263      if showWbar
264        delete(h)
[24]265          end
266          error('Could not read data from file "%s"!',filename)
[2]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
[80]285  [procpar,msg]=aedes_readprocpar([f_path,'procpar']);%,'wbar',showWbar);
[78]286  if isempty(procpar)
[77]287    DATA=[];
[78]288    error(msg);
289    return
[2]290  end
291 
[77]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';
[138]301  VnmrUseOldReadFcn = true;
[77]302 
[35]303  %% Get defaults for Return
[37]304  if ispref('Aedes','ReadfidReturn')
[77]305    ReadfidReturn = getpref('Aedes','ReadfidReturn');
[35]306  end
307
308  %% Get defaults for DC correction
[37]309  if ispref('Aedes','ReadfidDCcorrection')
[77]310    if getpref('Aedes','ReadfidDCcorrection')
311      ReadfidDCcorrection = 'on';
312    else
313      ReadfidDCcorrection = 'off';
314    end
[35]315  end
316
317  %% Get defaults for Zeropadding
[37]318  if ispref('Aedes','ReadfidZeropadding')
[77]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
[35]328  end
329
330  %% Get defaults for Sorting
[37]331  if ispref('Aedes','ReadfidSorting')
[77]332    if getpref('Aedes','ReadfidSorting')
333      ReadfidSorting = 'on';
334    else
335      ReadfidSorting = 'off';
336    end
[35]337  end
[77]338 
[35]339  %% Get defaults for FastRead
[37]340  if ispref('Aedes','ReadfidFastRead')
[77]341    if getpref('Aedes','ReadfidFastRead')
342      ReadfidFastRead = 'on';
343    else
344      ReadfidFastRead = 'off';
345    end
[35]346  end
347 
[36]348  %% Get defaults for Precision
[37]349  if ispref('Aedes','ReadfidPrecision')
[77]350    if strcmpi(getpref('Aedes','ReadfidPrecision'),'single')
351      ReadfidPrecision = 'single';
352    else
353      ReadfidPrecision = 'double';
354    end
[36]355  end
356 
[75]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 
[67]364  %% Get defaults for Reorienting EPI data
[75]365  if ispref('Aedes','ReadfidOrientImages')
366    OrientImages = getpref('Aedes','ReadfidOrientImages');
[67]367  else
[147]368    OrientImages = 'on';
[67]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 
[138]378  %% Get default read function
379  if ispref('Aedes','VnmrUseOldReadFcn')
380    VnmrUseOldReadFcn = getpref('Aedes','VnmrUseOldReadFcn');
381  else
382    VnmrUseOldReadFcn = true;
383  end
[67]384 
[138]385 
386 
[2]387  %% Read data from fid file
388  try
[138]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
[2]414    if isempty(DATA)
415      DATA=[];
[78]416      error('Unknown error while reading "%s".',[f_path,'fid'])
[2]417      return
418    end
419  catch
[69]420    DATA=[];
421    error(lasterr)
[2]422  end
423 
424 
425  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426  % Read DICOM image files (DCM)
427  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
428 case 'dcm'
429 
[80]430  % Show aedes_calc_wait
[39]431  if showWbar
[80]432    [h,txh]=aedes_calc_wait('Reading data from DICOM file...');
[39]433  end
434   
[2]435  %% Set data format string
436  DATA.DataFormat = 'dcm';
437 
438  %% Read DICOM header
439  try
[24]440    hdr = dicominfo(filename);
[146]441    DATA.HDR.FileHeader=hdr;
[24]442    DATA.HDR.fname = f_name;
[2]443    DATA.HDR.fpath = f_path;
444  catch
[24]445        DATA=[];
[39]446        if showWbar
447          delete(h)
448        end
[24]449        error('Could not read header from DICOM file "%s"',...
450          filename);
[39]451       
[2]452  end
453 
454  %% Read DICOM data
455  try
456    DATA.FTDATA=dicomread(hdr);
457  catch
[24]458        DATA=[];
[39]459        if showWbar
460          delete(h)
461        end
[24]462        error('Could not read image data from DICOM file "%s"',...
463          filename);
[2]464  end
[39]465  if showWbar
466        delete(h)
467  end
[2]468 
469 
470  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
471  % Read SPECT/CT Files
472  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
473 case 'spect/ct'
474 
475  %% Read Data
476  try
[80]477    [DATA,msg]=aedes_readctdata(filename);
[2]478    if isempty(DATA)
479      return
480    end
481  catch
[24]482        DATA=[];
483        error('An error occurred while reading SPECT/CT data from "%s"!',...
484          filename)
[2]485  end
486 
487  %% Set data format string
488  DATA.DataFormat = 'spect/ct';
489 
490 
491  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
492  % Read Varian FDF Files
493  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
494  case 'fdf'
495   
496    %% Read data
[80]497    DATA=aedes_readfdf(filename);
[2]498    if isempty(DATA)
[132]499      DATA=[];
500      error('An error occurred while reading FDF file "%s"!',...
501        filename)
502    end
503 
504  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
505  % Read SWIFT SGL-Files
506  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
507  case 'swift_sgl'
508   
509    % Read data
510    [DATA,msg]=aedes_readswiftsgl(filename,...
511      'datainterpsizeprompt',true,...
512      'wbar',true);
513    if isempty(DATA)
514      error(msg)
515    end
516   
[24]517        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[37]518        % Read Aedes ROI-Files
[24]519        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
520  case 'roi'
521       
522        %% Try to load the ROI-file
523    try
524      roifile = load(filename,'-mat');
525        catch
526          error('Error while reading ROI-file "%s".',...
527                filename)
528    end
529   
530    %% See if the ROI-file includes file information
531    if ~isfield(roifile,'FileInfo') || ...
532        any(cellfun(@isempty,roifile.FileInfo.DataFileName))
533          error('The ROI-file does not contain file information.')
534    end
535   
536    filenames=roifile.FileInfo.DataFileName;
537    pathnames=roifile.FileInfo.DataPathName;
538   
539    DATA={};
540        if showWbar
[80]541          [calc_h,txh]=aedes_calc_wait({['Reading file 1/' num2str(length(filenames))], ...
[24]542                '""'});
543        end
544
545    %% Read the files
546    for ii=1:length(filenames)
547     
548      %% Check if the file exists
549      if exist([pathnames{ii},filenames{ii}],'file')==0
550        delete(calc_h)
551                DATA=[];
552                error('Cannot find the data file "%s".',...
553                  [pathnames{ii},filenames{ii}])
[2]554      end
[24]555     
556      set(txh,'String',...
557              sprintf('%s\n%s',...
558                      ['Reading file ' num2str(ii) ...
559                       '/' num2str(length(filenames))],...
560                      ['"',pathnames{ii},filenames{ii},'"']))
561      drawnow
562         
563          try
[80]564                DATA{ii}=aedes_data_read([pathnames{ii},filenames{ii}],'wbar','off');
[24]565          catch
566                error(['Unknown error occurred while reading ROI-file "%s". ',...
567                  lasterr],...
568                  [pathnames{ii},filenames{ii}])
569          end
[2]570    end
[24]571    delete(calc_h)
572   
[28]573    %Dat.LoadRoiAtStartUp = true;
[24]574   
575    %% Use rotation information
576    if isfield(roifile,'RotateFlip')
[53]577          if iscell(roifile.RotateFlip)
578                RotateFlip3d = roifile.RotateFlip;
579                DataRotation = false(1,length(DATA));
580                DataFlip = false(1,length(DATA));
581          else
582                DataRotation = roifile.RotateFlip.Rotate;
583                DataFlip = roifile.RotateFlip.Flip;
584                RotateFlip3d = {};
585          end
[28]586        else
587          DataRotation = false(1,length(DATA));
588          DataFlip = false(1,length(DATA));
[53]589          RotateFlip3d = {};
[28]590        end
591       
592        %% Use SliceClim information
593        if isfield(roifile,'SliceClim')
594          DATA{1}.SliceClim = SliceClim;
595        end
[24]596   
597    %% Assign the ROI-structure to the first structure
598    DATA{1}.ROI = roifile.ROI;
599    clear('roifile')
600   
601    %% Rotate images if necessary
[53]602        if ~isempty(RotateFlip3d)
603         
604          for ii=1:length(RotateFlip3d)
605                if strcmpi(RotateFlip3d{ii}{1},'rotate')
606                  k = RotateFlip3d{ii}{2};
607                  dim = RotateFlip3d{ii}{3};
[80]608                  DATA{1}.FTDATA = aedes_rot3d(DATA{1}.FTDATA,k,dim);
[53]609                elseif strcmpi(RotateFlip3d{ii}{1},'flip')
610                  dim = RotateFlip3d{ii}{2};
611                  DATA{1}.FTDATA = flipdim(DATA{1}.FTDATA,dim);
612                end
613          end
614          DATA{1}.RotateFlip3d = RotateFlip3d;
615        elseif ( ~all(DataRotation==0) || ~all(DataFlip==0) )
[24]616      for ii=1:length(DATA)
617        if DataRotation(ii)~=0
618          DATA{ii}.FTDATA = rot90(DATA{ii}.FTDATA,DataRotation(ii));
619        end
620     
621        if DataFlip(ii)~=0
622          if DataFlip(ii)==1
623            DATA{ii}.FTDATA = flipud(DATA{ii}.FTDATA);
624          elseif DataFlip(ii)==2
625            DATA{ii}.FTDATA = fliplr(DATA{ii}.FTDATA);
626          end
627        end
[28]628          end
629          DATA{1}.DataRotation = DataRotation;
630          DATA{1}.DataFlip = DataFlip;
631        end
632       
[24]633   
[2]634 
[24]635        %%%%%%%%%%%%%%%%%%%%%%%%%%%%
636        % Unknown File Format
637        %%%%%%%%%%%%%%%%%%%%%%%%%%%%
[2]638  otherwise
[24]639        DATA=[];
640        error('Unknown file format')
[2]641end
642
Note: See TracBrowser for help on using the repository browser.

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