Home > bioelectromagnetism > load_AvgNetCDFv5.m

load_AvgNetCDFv5

PURPOSE ^

load_AvgNetCDFv5 : reads a set of netMEG files, and returns the good data for the specified stimulus

SYNOPSIS ^

function [F,time_labels,Rcoils,Ocoils,stim_num,coilWeights,nc_montage,bad_channels_list,baseline_variance,EEGwf,EEGpickupLoc,EEGreferenceLoc,EEGpickupRadius,EEGreferenceRadius,success] = load_AvgNetCDFv5(ncfnames,stim_num)

DESCRIPTION ^

 load_AvgNetCDFv5 :  reads a set of netMEG files, and returns the good data for the specified stimulus
       for Matlab 5.0 and larger
 
 useage [F,time_labels,Rcoils,Ocoils,stim_num,coilWeights,nc_montage,bad_channels_list,baseline_variance,EEGwf, ...
  EEGpickupLoc,EEGreferenceLoc,EEGpickupRadius,EEGreferenceRadius,success] = load_AvgNetCDF( ...
  ncnames,stim_num)

 Input:
    ncfnames ... array of names of all netCDF files in the set
    stim_num ...  stim to read from each file in the set (scalar)

 Output:
 F       : Data matrix of MEG measurements - probes x latencies
 time_labels : Row vector of latencies 
 Rcoils  : MEG sensor Location matrix - probes X (3 [x y z ])*ncoils 
           contains location info for each coil
 Ocoils  : MEG sensor Orientation matrix - probes X (3 [x' y' z'])*ncoils 
           orientation info for each coil
 stim_num: Stimulus number for the experiment
 coilWeights : vector of the weight to be applied to the values calculated at each MEG sensor coil. (ncoils)
  (assumes all sensors have the same weighting. This could be changed -- the netMEG file saves the weights
   separately for each sensor)
 nc_montage : string, name of sensor system
 bad_channels_list : string, list of channels whose data was not used
 baseline_variance : column vector of noise for the selected stim (nsensors,1)
 EEGwf : Data matrix of EEG measurements - sensors x latencies
 EEGpickupLoc : location of EEG pickup sensor, 3 X EEGsensors 
 EEGreferenceLoc : location of EEG reference sensor,  3 X EEGsensors 
 EEGpickupRadius : Radius of EEG pickup sensor,  EEGsensors
 EEGreferenceRadius : Radius of EEG reference sensor,  EEGsensors
 success : if the file couldn't be read for any reason, success = 0

 Elaine Best, Los Alamos National Laboratory, 10/95
 Altered 1/6/98 to read version 1.4 files, and warn of old ones
 Altered 7/98 to also work with new version of netCDF library for MATLAB 5.0

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [F,time_labels,Rcoils,Ocoils,stim_num,coilWeights,nc_montage,bad_channels_list,baseline_variance,EEGwf, ...
0002    EEGpickupLoc,EEGreferenceLoc,EEGpickupRadius,EEGreferenceRadius,success] = load_AvgNetCDFv5( ...
0003    ncfnames,stim_num)
0004 
0005 % load_AvgNetCDFv5 :  reads a set of netMEG files, and returns the good data for the specified stimulus
0006 %       for Matlab 5.0 and larger
0007 %
0008 % useage [F,time_labels,Rcoils,Ocoils,stim_num,coilWeights,nc_montage,bad_channels_list,baseline_variance,EEGwf, ...
0009 %  EEGpickupLoc,EEGreferenceLoc,EEGpickupRadius,EEGreferenceRadius,success] = load_AvgNetCDF( ...
0010 %  ncnames,stim_num)
0011 %
0012 % Input:
0013 %    ncfnames ... array of names of all netCDF files in the set
0014 %    stim_num ...  stim to read from each file in the set (scalar)
0015 %
0016 % Output:
0017 % F       : Data matrix of MEG measurements - probes x latencies
0018 % time_labels : Row vector of latencies
0019 % Rcoils  : MEG sensor Location matrix - probes X (3 [x y z ])*ncoils
0020 %           contains location info for each coil
0021 % Ocoils  : MEG sensor Orientation matrix - probes X (3 [x' y' z'])*ncoils
0022 %           orientation info for each coil
0023 % stim_num: Stimulus number for the experiment
0024 % coilWeights : vector of the weight to be applied to the values calculated at each MEG sensor coil. (ncoils)
0025 %  (assumes all sensors have the same weighting. This could be changed -- the netMEG file saves the weights
0026 %   separately for each sensor)
0027 % nc_montage : string, name of sensor system
0028 % bad_channels_list : string, list of channels whose data was not used
0029 % baseline_variance : column vector of noise for the selected stim (nsensors,1)
0030 % EEGwf : Data matrix of EEG measurements - sensors x latencies
0031 % EEGpickupLoc : location of EEG pickup sensor, 3 X EEGsensors
0032 % EEGreferenceLoc : location of EEG reference sensor,  3 X EEGsensors
0033 % EEGpickupRadius : Radius of EEG pickup sensor,  EEGsensors
0034 % EEGreferenceRadius : Radius of EEG reference sensor,  EEGsensors
0035 % success : if the file couldn't be read for any reason, success = 0
0036 %
0037 % Elaine Best, Los Alamos National Laboratory, 10/95
0038 % Altered 1/6/98 to read version 1.4 files, and warn of old ones
0039 % Altered 7/98 to also work with new version of netCDF library for MATLAB 5.0
0040 %
0041 
0042 success = 1;
0043 
0044 % loop to get the data from each file
0045 [nfiles,d] = size(ncfnames);
0046 Rcoils = [];
0047 Ocoils = [];
0048 
0049   % loop over netMEG files
0050   for ifile = 1:nfiles
0051   
0052     % open the file in read only mode
0053     filename = ncfnames(ifile,:);
0054     nc = netcdf(filename,'nowrite');
0055 
0056     % get a list of the variable names
0057     variables = var(nc);
0058     var_names = ncnames(variables);
0059   
0060     % find out which sensor array (montage) and netCDF file version we are using
0061       nc_montage = nc.MontageName(:);
0062       bad_channels_list = nc.BadChannelsDeleted(:);
0063       nc_version = nc.netCDFfileVersion(:);
0064   
0065        samp_int = nc{'SamplingInterval'}(:);
0066        prestim = nc{'LengthOfPrestim'}(stim_num);
0067        npts = nc{'numSamples'}(stim_num);
0068   
0069        time_labels = (0:npts-1)*samp_int - prestim;
0070   
0071      if (strcmp(nc_version(7:9),'1.1') | strcmp(nc_version(7:9),'1.2') )
0072        disp(' ')
0073        disp('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ')
0074        disp('ERROR: ')
0075        disp('Your netMEG file is an obsolete version.')
0076        disp('You can read it into MEGAN to update it.')
0077        disp('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ')
0078        disp(' ')
0079        success = 0;
0080        return
0081      end
0082      if (strcmp(nc_version(7:9),'1.3') )
0083        disp('Your netMEG file is a bad version.')
0084        disp('Only a few of these files exist.  There is no')
0085        disp('way to update them. Please discard the file.')
0086        success = 0;
0087        return
0088      end
0089   
0090     % get the channel type array
0091     ind = find(strcmp(var_names,'ChannelTypes'));
0092     if length(ind) ~= 0 
0093       ChannelTypes = nc{'ChannelTypes'}(:);
0094     else 
0095       disp('Returning: netMEG Error reading  reading ChannelTypes')
0096       success = 0;
0097       return;
0098     end
0099     [nchans,dummy] = size(ChannelTypes);
0100   
0101     % get the channel status array if it exists, otherwise make one that indicates
0102     % that all of the data is good
0103     ind = find(strcmp(var_names,'ChannelStatus'));
0104     if length(ind) ~= 0 
0105       ChannelStatus = nc{'ChannelStatus'}(:);
0106     else 
0107       ChannelStatus = ones(nchans(1),1) 
0108     end
0109   
0110     % See if we have any EEG sensor location data, read it if we do.
0111     % (It is possible to have EEG channel data without EEG sensor locations.
0112     %  However, we can't localize without the sensor locations.)
0113 
0114     % EEG pickup  and reference locations and radii
0115     ind = find(strcmp(var_names,'EEGpickupLocation'));
0116     if length(ind) ~= 0 
0117       EEGpickupLoctmp = nc{'EEGpickupLocation'}(:);
0118       [neeg_sensors,dummy] = size(EEGpickupLoctmp);
0119     else 
0120       neeg_sensors = 0;
0121     end
0122 
0123     if neeg_sensors > 0 
0124         ind = find(strcmp(var_names,'EEGreferenceLocation'));
0125         if length(ind) ~= 0 
0126           EEGreferenceLoctmp = nc{'EEGreferenceLocation'}(:);
0127         else 
0128           disp('Returning: netMEG Error reading  EEGreferenceLocation')
0129           success = 0;
0130           return;
0131         end
0132 
0133         ind = find(strcmp(var_names,'EEGpickupRadius'));
0134         if length(ind) ~= 0 
0135           EEGpickupRadiustmp = nc{'EEGpickupRadius'}(:);
0136         else 
0137           disp('Returning: netMEG Error reading  EEGpickupRadius')
0138           success = 0;
0139           return;
0140         end
0141 
0142         ind = find(strcmp(var_names,'EEGreferenceRadius'));
0143         if length(ind) ~= 0 
0144           EEGreferenceRadiustmp = nc{'EEGreferenceRadius'}(:);
0145         else 
0146           disp('Returning: netMEG Error reading  EEGreferenceRadius')
0147           success = 0;
0148           return;
0149         end
0150     end
0151 
0152     % figure out which channels are MEG and which (if any) are EEG, and
0153     % select data only for channels with a status of 1.
0154       
0155     good_MEG_indices = find(ChannelTypes(:,1)=='M' & ChannelTypes(:,2) == 'E' & ...
0156              ChannelTypes(:,3) == 'G' & ChannelStatus == 1);
0157     ngood_MEG = size(good_MEG_indices,1);
0158 
0159     if (neeg_sensors > 0)
0160         good_EEG_indices = find(ChannelTypes(:,1)=='E' & ChannelTypes(:,2) == 'E' & ...
0161              ChannelTypes(:,3) == 'G' & ChannelStatus == 1);
0162         EEG_indices = find(ChannelTypes(:,1)=='E' & ChannelTypes(:,2) == 'E' & ...
0163              ChannelTypes(:,3) == 'G' );
0164         EEG_sensor_status = ChannelStatus(EEG_indices);
0165         good_EEG_sensors = find(EEG_sensor_status == 1);
0166          
0167         ngood_EEG = size(good_EEG_indices,1);
0168         if ifile == 1
0169            EEGpickupLoc = EEGpickupLoctmp(good_EEG_sensors,:)';
0170            EEGreferenceLoc = EEGreferenceLoctmp(good_EEG_sensors,:)';
0171            EEGpickupRadius = EEGpickupRadiustmp(good_EEG_sensors);
0172            EEGreferenceRadius = EEGreferenceRadiustmp(good_EEG_sensors);
0173         else
0174            EEGpickupLoc = [EEGpickupLoc,EEGpickupLoctmp(good_EEG_sensors,:)'];
0175            EEGreferenceLoc = [EEGreferenceLoc,EEGreferenceLoctmp(good_EEG_sensors,:)'];
0176            EEGpickupRadius = [EEGpickupRadius;EEGpickupRadiustmp(good_EEG_sensors)];
0177            EEGreferenceRadius = [EEGreferenceRadius;EEGreferenceRadiustmp(good_EEG_sensors)];
0178         end
0179     else
0180         ngood_EEG = 0;
0181         EEGpickupLoc = 0;
0182         EEGreferenceLoc = 0;
0183         EEGpickupRadius = 0;
0184         EEGreferenceRadius = 0;
0185     end
0186   
0187     if (ngood_MEG > 0 )
0188       % read the coil location and orientation data
0189       % and concatenate it with the previously read data, if any
0190     
0191       ind = find(strcmp(var_names,'SensorElementsOrient'));
0192       if length(ind) ~= 0 
0193         SensorElementsOrient = nc{'SensorElementsOrient'}(:);
0194       else 
0195         disp('Returning: netMEG Error reading  SensorElementsOrient')
0196         success = 0;
0197         return;
0198       end
0199     
0200       ind = find(strcmp(var_names,'SensorElementsLoc'));
0201       if length(ind) ~= 0 
0202         SensorElementsLoc = nc{'SensorElementsLoc'}(:);
0203       else 
0204         disp('Returning: netMEG Error reading  SensorElementsLoc')
0205         success = 0;
0206         return;
0207       end
0208   
0209       [dum1,max_sens_elem,dum2] = size(SensorElementsLoc);
0210     
0211       % loop over the coils, and load the data for each coil into
0212       % the Rcoils and Ocoils arrays
0213       % (Matlab 4 could not handle arrays larger than 2 dimensions, so we store
0214       % the data in a 2 D array.)
0215       for igood = 1:ngood_MEG
0216         isn = good_MEG_indices(igood);
0217         coil_loc = reshape(SensorElementsLoc(isn,:,:),max_sens_elem,3);
0218         sens_orient = reshape(SensorElementsOrient(isn,:,:),max_sens_elem,3);
0219       
0220         % load the coil locations and orientations into the Rcoils and Ocoils arrays
0221         if ifile == 1 & igood == 1
0222           Rcoils = [coil_loc(1,:)];
0223           Ocoils = [sens_orient(1,:)];
0224           for j = 2: max_sens_elem
0225             Rcoils = [Rcoils,coil_loc(j,:)];
0226             Ocoils = [Ocoils,sens_orient(j,:)];
0227           end
0228         else
0229           Rcoils_row = [coil_loc(1,:)];
0230           Ocoils_row = [sens_orient(1,:)];
0231           for j = 2: max_sens_elem
0232             Rcoils_row = [Rcoils_row,coil_loc(j,:)];
0233             Ocoils_row = [Ocoils_row,sens_orient(j,:)];
0234           end
0235           Rcoils = [Rcoils;Rcoils_row];
0236           Ocoils = [Ocoils;Ocoils_row];
0237         end
0238       end %for
0239 
0240       % get the coil weights
0241       % we store the weights for each sensor, but for now are assuming that they
0242       % are the same for each sensor, and are just using a vector.
0243        ind = find(strcmp(var_names,'CoilWeight'));
0244        if length(ind) ~= 0 
0245          coilWeight = nc{'CoilWeight'}(:);
0246        else 
0247          disp('Returning: netMEG Error reading  CoilWeight')
0248          success = 0;
0249          return;
0250        end
0251        coilWeights = coilWeight(1,:)';
0252   
0253       % read the MEG noise
0254        ind = find(strcmp(var_names,'BaselineVariance'));
0255        if length(ind) ~= 0 
0256          baseline_variance_tmp = nc{'BaselineVariance'}(:);
0257        else 
0258          disp('Returning: netMEG Error reading  BaselineVariance')
0259          success = 0;
0260          return;
0261        end
0262 
0263     end % ngood_MEG
0264   
0265     % read the amplitudes
0266     % get the full array of data for this stim
0267     % from the file, and then extract the data we want
0268     ind = find(strcmp(var_names,'Waveforms'));
0269     if length(ind) ~= 0 
0270       Ftmp = nc{'Waveforms'}(stim_num,1:npts,:);
0271       [np,nch,dum] = size(Ftmp);
0272       Ftmp = Ftmp';
0273     else 
0274       disp('Returning: netMEG Error reading  Waveforms')
0275       success = 0;
0276       return;
0277     end
0278   
0279     % MEG waveform data
0280       if (ngood_MEG > 0)
0281         if ifile == 1
0282            F = Ftmp(good_MEG_indices,:);
0283            baseline_variance = baseline_variance_tmp(stim_num,good_MEG_indices)';
0284         else
0285            F = [F ; Ftmp(good_MEG_indices,:)];
0286            baseline_variance = [baseline_variance;baseline_variance_tmp(stim_num,good_MEG_indices)'];
0287         end
0288       end
0289 
0290     % EEG waveform data
0291       if (ngood_EEG > 0)
0292         % EEG Waveforms
0293         if ifile == 1
0294            EEGwf = Ftmp(good_EEG_indices,:);
0295         else
0296            EEGwf = [EEGwf ; Ftmp(good_EEG_indices,:)];
0297         end
0298       else
0299         EEGwf = 0;
0300       end
0301   
0302     % close the file
0303     nc = close(nc);
0304   
0305   end % for loop over files
0306   time_labels = time_labels';
0307 
0308 end % code for version 5
0309 
0310 return

Generated on Mon 15-Aug-2005 15:36:19 by m2html © 2003