source: aedes_data_read.m @ 151

Last change on this file since 151 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
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  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426  % Read DICOM image files (DCM)
427  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
428 case 'dcm'
429 
430  % Show aedes_calc_wait
431  if showWbar
432    [h,txh]=aedes_calc_wait('Reading data from DICOM file...');
433  end
434   
435  %% Set data format string
436  DATA.DataFormat = 'dcm';
437 
438  %% Read DICOM header
439  try
440    hdr = dicominfo(filename);
441    DATA.HDR.FileHeader=hdr;
442    DATA.HDR.fname = f_name;
443    DATA.HDR.fpath = f_path;
444  catch
445        DATA=[];
446        if showWbar
447          delete(h)
448        end
449        error('Could not read header from DICOM file "%s"',...
450          filename);
451       
452  end
453 
454  %% Read DICOM data
455  try
456    DATA.FTDATA=dicomread(hdr);
457  catch
458        DATA=[];
459        if showWbar
460          delete(h)
461        end
462        error('Could not read image data from DICOM file "%s"',...
463          filename);
464  end
465  if showWbar
466        delete(h)
467  end
468 
469 
470  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
471  % Read SPECT/CT Files
472  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
473 case 'spect/ct'
474 
475  %% Read Data
476  try
477    [DATA,msg]=aedes_readctdata(filename);
478    if isempty(DATA)
479      return
480    end
481  catch
482        DATA=[];
483        error('An error occurred while reading SPECT/CT data from "%s"!',...
484          filename)
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
497    DATA=aedes_readfdf(filename);
498    if isempty(DATA)
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   
517        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
518        % Read Aedes ROI-Files
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
541          [calc_h,txh]=aedes_calc_wait({['Reading file 1/' num2str(length(filenames))], ...
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}])
554      end
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
564                DATA{ii}=aedes_data_read([pathnames{ii},filenames{ii}],'wbar','off');
565          catch
566                error(['Unknown error occurred while reading ROI-file "%s". ',...
567                  lasterr],...
568                  [pathnames{ii},filenames{ii}])
569          end
570    end
571    delete(calc_h)
572   
573    %Dat.LoadRoiAtStartUp = true;
574   
575    %% Use rotation information
576    if isfield(roifile,'RotateFlip')
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
586        else
587          DataRotation = false(1,length(DATA));
588          DataFlip = false(1,length(DATA));
589          RotateFlip3d = {};
590        end
591       
592        %% Use SliceClim information
593        if isfield(roifile,'SliceClim')
594          DATA{1}.SliceClim = SliceClim;
595        end
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
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};
608                  DATA{1}.FTDATA = aedes_rot3d(DATA{1}.FTDATA,k,dim);
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) )
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
628          end
629          DATA{1}.DataRotation = DataRotation;
630          DATA{1}.DataFlip = DataFlip;
631        end
632       
633   
634 
635        %%%%%%%%%%%%%%%%%%%%%%%%%%%%
636        % Unknown File Format
637        %%%%%%%%%%%%%%%%%%%%%%%%%%%%
638  otherwise
639        DATA=[];
640        error('Unknown file format')
641end
642
Note: See TracBrowser for help on using the repository browser.

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