0001 function [eeg_data, channel_info] = ...
0002 vb_eegfile_load_data(eegfile, loadspec, newfile, verbose_swt)
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083 if ~exist('eegfile', 'var'), eegfile = []; end
0084 if ~exist('loadspec', 'var'), loadspec = []; end
0085 if ~exist('newfile', 'var'), newfile = ''; end
0086 if ~exist('verbose_swt', 'var'), verbose_swt = []; end
0087 [eegfile, loadspec, newfile, ch_list, target_sample, tr_list, ...
0088 eegfile_dir, VERBOSE] = ...
0089 inner_check_arguments(eegfile, loadspec, newfile, verbose_swt);
0090
0091
0092
0093
0094 func_ = mfilename;
0095 vb_define_device;
0096
0097 old_eeginfo = vb_eegfile_load_eeginfo(eegfile);
0098
0099
0100 eeg_labels = vb_eeginfo_get_channel_label(old_eeginfo, false);
0101 ext_labels = vb_eeginfo_get_channel_label_extra(old_eeginfo, false);
0102
0103
0104 [eeg_ch_list, eeg_idx] = vb_util_get_labels_to_read(eeg_labels, ...
0105 loadspec.ChannelName, loadspec.ChannelSwitch);
0106 [ext_ch_list, ext_idx] = vb_util_get_labels_to_read(ext_labels, ...
0107 loadspec.ChannelName, loadspec.ChannelSwitch);
0108
0109
0110 N_channel = vb_info_get_channel_number(old_eeginfo);
0111 ext_idx_tmp = ext_idx + N_channel;
0112
0113
0114
0115
0116 act_ch_list = [eeg_ch_list;ext_ch_list];
0117 act_idx = [eeg_idx;ext_idx_tmp];
0118
0119 n_channel_new = length(act_ch_list);
0120 if n_channel_new == 0
0121 error('(%s)cannot make channel list', func_);
0122 end
0123
0124 n_sample_new = target_sample(1,2)-target_sample(1,1) + 1;
0125
0126 n_trial = vb_info_get_Nrepeat(old_eeginfo);
0127
0128 if n_trial == 1
0129 n_trial_new = size(target_sample, 1);
0130
0131 else
0132 n_trial_new = length(tr_list);
0133 end
0134
0135
0136 eeg_data = zeros(n_channel_new, n_sample_new, n_trial_new);
0137
0138
0139 data_type = vb_eeginfo_get_datatype(old_eeginfo);
0140 act_data_type = data_type(act_idx);
0141
0142 file_ext = FILE_EXT_BIN_CH_EEG;
0143
0144
0145
0146 internal_data = vb_eegfile_load_internal_data(eegfile);
0147
0148
0149 if isempty(internal_data)
0150
0151
0152 n_sample = vb_info_get_sample_number(old_eeginfo);
0153
0154
0155 src_data_dir = [eegfile_dir filesep vb_eeginfo_get_datadir(old_eeginfo)];
0156
0157
0158 EDF = vb_eeginfo_get_header(old_eeginfo);
0159
0160
0161
0162
0163
0164
0165
0166 for i_ch = 1:n_channel_new
0167
0168 src_datafile = sprintf('%s%s%s.%s', ...
0169 src_data_dir, filesep, act_ch_list{i_ch}, file_ext);
0170
0171 fid = fopen(src_datafile, 'r');
0172 if fid < 0
0173 error('(%s)cannot open : %s', func_, src_datafile);
0174 end
0175
0176
0177 if strcmp(act_ch_list{i_ch}, STATUS_CH_LABEL)
0178 status_ch = true;
0179 data_size = STATUS_BIN_DATA_SIZE;
0180 else
0181 status_ch = false;
0182 data_size = STANDARD_BIN_DATA_SIZE;
0183 end
0184
0185 if VERBOSE, fprintf(' --- read channel: ''%s''\n', act_ch_list{i_ch}); end
0186
0187
0188 ch_data = fread(fid, inf, act_data_type{i_ch});
0189 fclose(fid);
0190
0191 if n_trial == 1
0192
0193
0194 for i_trial = 1:n_trial_new
0195 sample_from = target_sample(i_trial, 1);
0196 sample_to = target_sample(i_trial, 2);
0197 sample_idx = sample_from:sample_to;
0198
0199 this_eeg = ch_data(sample_idx);
0200
0201
0202 eeg_data(i_ch,:,i_trial) = this_eeg;
0203 end
0204
0205 else
0206
0207 org_ch_data = reshape(ch_data, n_sample, n_trial);
0208
0209
0210 sample_from = target_sample(1,1);
0211 sample_to = target_sample(1,2);
0212 sample_idx = sample_from:sample_to;
0213 eeg_data(i_ch,:,:) = org_ch_data(sample_idx, tr_list);
0214 end
0215
0216 if VERBOSE
0217 fprintf(' ------------------------------------------------------\n');
0218 end
0219 end
0220
0221
0222 else
0223
0224
0225 n_trial = vb_info_get_Nrepeat(old_eeginfo);
0226 if n_trial == 1
0227
0228 n_trial_new = size(target_sample, 1);
0229
0230
0231 for i_trial = 1:n_trial_new
0232 sample_idx = target_sample(i_trial,1):target_sample(i_trial,2);
0233 eeg_data(:,:,i_trial) = internal_data(act_idx, sample_idx);
0234 end
0235
0236 else
0237
0238 sample_idx = target_sample(1,1):target_sample(1,2);
0239 eeg_data = internal_data(act_idx, sample_idx, tr_list);
0240 end
0241 end
0242
0243
0244
0245
0246 EEGinfo = old_eeginfo;
0247
0248 EEGinfo.ChannelID = old_eeginfo.ChannelID(eeg_idx);
0249 EEGinfo.ChannelName = eeg_ch_list;
0250 EEGinfo.Nchannel = length(eeg_idx);
0251
0252
0253 old_sensor_pos = vb_eeginfo_get_sensor_position(EEGinfo);
0254 if ~isempty(old_sensor_pos)
0255 EEGinfo = vb_eeginfo_set_sensor_position(EEGinfo, old_sensor_pos(eeg_idx,:));
0256 end
0257
0258
0259 EEGinfo.DataType = EEGinfo.DataType(act_idx);
0260
0261
0262 EEGinfo.Nsample = n_sample_new;
0263 EEGinfo.RecordTime = EEGinfo.Nsample / EEGinfo.SampleFrequency;
0264
0265
0266 old_exinfo = vb_info_get_channel_info(old_eeginfo, 1);
0267 new_exinfo.Channel_active = old_exinfo.Active(ext_idx);
0268 new_exinfo.Channel_name = old_exinfo.Name(ext_idx);
0269 new_exinfo.Channel_id = old_exinfo.ID(ext_idx);
0270
0271 if isempty(old_exinfo.Type)
0272 new_exinfo.Channel_type = ones(length(ext_idx), 1);
0273 else
0274 new_exinfo.Channel_type = old_exinfo.Type(ext_idx);
0275 end
0276
0277 EEGinfo.ExtraChannelInfo = new_exinfo;
0278
0279
0280 if ~isempty(loadspec.TrialNumber)
0281
0282 old_trial = vb_info_get_trial_data(EEGinfo, loadspec.TrialNumber);
0283
0284 sample_idx = target_sample(1,1):target_sample(1,2);
0285
0286 for i_trial = 1:length(old_trial)
0287 new_trial(i_trial) = old_trial(i_trial);
0288
0289
0290 sample_old = old_trial(i_trial).sample;
0291 new_trial(i_trial).sample = sample_old(sample_idx);
0292 end
0293
0294 else
0295 old_trial = vb_info_get_trial_data(EEGinfo);
0296 n_trial_old = length(old_trial);
0297
0298 if n_trial_old == 1
0299
0300 n_trial_new = size(target_sample, 1);
0301 for i_trial = 1:n_trial_new
0302 sample_idx = target_sample(i_trial,1):target_sample(i_trial,2);
0303 new_trial(i_trial).sample = sample_idx;
0304 new_trial(i_trial).number = i_trial;
0305 new_trial(i_trial).Active = true;
0306 end
0307 else
0308
0309 sample_idx = target_sample(1,1):target_sample(1,2);
0310
0311 for i_trial = length(old_trial)
0312 new_trial(i_trial) = old_trial(i_trial);
0313
0314
0315 sample_old = old_trial(i_trial).sample;
0316 new_trial(i_trial).sample = sample_old(sample_idx);
0317 end
0318 end
0319 end
0320
0321 EEGinfo.Trial = vb_util_arrange_list(new_trial, 0);
0322
0323 EEGinfo = vb_info_adjust_trial(EEGinfo);
0324
0325
0326 if ~isempty(loadspec.Pretrigger)
0327 EEGinfo = vb_info_set_pre_trigger(EEGinfo, loadspec.Pretrigger);
0328 end
0329
0330
0331 old_active_channel = vb_info_get_active_channel(old_eeginfo);
0332 new_ActiveChannel = old_active_channel(eeg_idx);
0333 EEGinfo = vb_info_set_active_channel(EEGinfo, new_ActiveChannel);
0334
0335
0336
0337
0338
0339
0340 eeg_channel_info = ...
0341 vb_eeginfo_make_channel_info(EEGinfo, loadspec.ChannelName, 1);
0342 EEGinfo.ChannelInfo = eeg_channel_info;
0343
0344
0345 channel_info = vb_eeginfo_make_channel_info(EEGinfo, loadspec.ChannelName, 0);
0346
0347 if ~isempty(newfile)
0348
0349
0350 ParentInfo = inner_make_parent_info(eegfile);
0351
0352 Measurement = 'EEG';
0353 if VERBOSE
0354 fprintf(' --- now on saving to new EEG-MAT file : %s ...', newfile);
0355 end
0356
0357
0358 if ~isfield(loadspec, 'new_data_dir') || isempty(loadspec.new_data_dir)
0359 data_in = true;
0360 else
0361 data_in = false;
0362 end
0363
0364 EEGinfo.File.BaseFile = eegfile;
0365 EEGinfo.File.OutputDir = loadspec.new_path;
0366 EEGinfo.File.EEGFile = loadspec.new_name;
0367 EEGinfo.File.DataDir = loadspec.bin_data_dir;
0368
0369 loadspec = inner_remove_temporary_fields(loadspec);
0370 LoadSpec = loadspec;
0371
0372 if data_in
0373
0374 vb_fsave(newfile, ...
0375 'eeg_data', 'EEGinfo', 'Measurement', 'ParentInfo', 'LoadSpec');
0376 else
0377
0378 vb_fsave(newfile, ...
0379 'EEGinfo', 'Measurement', 'ParentInfo', 'LoadSpec');
0380
0381 dst_data_dir = loadspec.new_data_dir;
0382 for i_ch = 1:n_channel_new
0383 inner_store_data_externally( dst_data_dir, ...
0384 act_ch_list{i_ch}, eeg_data(i_ch,:), act_data_type{i_ch}, file_ext)
0385 end
0386 end
0387
0388 if VERBOSE
0389 fprintf(' done!\n');
0390 end
0391 end
0392
0393 return;
0394
0395
0396
0397
0398
0399
0400
0401
0402 function [eegfile, loadspec, newfile, ...
0403 read_ch_list, target_samples, trial_list, ...
0404 eegfile_dir, verbose_swt] = ...
0405 inner_check_arguments(eegfile, loadspec, newfile, verbose_swt)
0406
0407 func_ = mfilename;
0408
0409 if isempty(eegfile)
0410 error('(%s)eegfile is a required parameter', func_);
0411 end
0412
0413 if exist(eegfile, 'file') ~= 2
0414 error('(%s)cannot find eegfile: %s', func_, eegfile);
0415 end
0416
0417 eegfile_dir = vb_get_file_parts(eegfile);
0418
0419
0420
0421
0422 loadspec = vb_loadspec_check(loadspec, eegfile);
0423
0424
0425 eeginfo = vb_eegfile_load_eeginfo(eegfile);
0426 [read_ch_list] = vb_eeginfo_get_read_channels(eeginfo, ...
0427 loadspec.ChannelName, loadspec.ChannelSwitch, loadspec.ActiveChannel);
0428
0429 if isempty(read_ch_list)
0430 error('(%s) read channels are empty', func_);
0431 end
0432
0433 loadspec.ChannelName = read_ch_list;
0434 loadspec.ChannelSwitch = true;
0435
0436 target_samples = vb_loadspec_make_trial_sample(loadspec);
0437 if isempty(target_samples)
0438 n_sample = vb_info_get_sample_number(eeginfo);
0439 target_samples = [1 n_sample];
0440 end
0441
0442
0443 N_trial = vb_info_get_Nrepeat(eeginfo);
0444 if N_trial == 1
0445 trial_list = [];
0446 else
0447
0448 if size(target_samples, 1) > 1
0449 fprintf('(%s) loadspec.Trigger may be wrong\n', mfilename);
0450 target_samples = target_samples(1,:);
0451 end
0452
0453 if isempty(loadspec.TrialNumber)
0454 cur_trial = vb_info_get_trial_data(eeginfo);
0455 else
0456 cur_trial = vb_info_get_trial_data(eeginfo, loadspec.TrialNumber);
0457 end
0458
0459 trial_list = [cur_trial.number];
0460
0461 if loadspec.ActiveTrial
0462
0463
0464 if vb_info_active_trial_is_valid(eeginfo)
0465 trial_list = vb_info_get_active_trial_index(eeginfo);
0466 else
0467 active_list = [cur_trial.Active];
0468 trial_list = trial_list(active_list==true);
0469 end
0470 end
0471 end
0472 loadspec.TrialNumber = trial_list;
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484
0485
0486
0487
0488 if ~isempty(newfile)
0489 [new_path, new_name] = vb_get_file_parts(newfile);
0490
0491 if ~isempty(new_path) && exist(new_path, 'dir') ~= 7
0492 vb_mkdir(new_path);
0493 end
0494
0495 loadspec.new_path = new_path;
0496 loadspec.new_name = new_name;
0497
0498 if ~isfield(loadspec, 'bin_data_dir') || isempty(loadspec.bin_data_dir)
0499
0500 loadspec.bin_data_dir = '';
0501 new_data_dir = '';
0502 else
0503 new_data_dir = [new_path '/' loadspec.bin_data_dir];
0504 end
0505
0506 if ~isempty(new_data_dir) && exist(new_data_dir, 'dir') ~= 7
0507 vb_mkdir(new_data_dir);
0508 end
0509 loadspec.new_data_dir = new_data_dir;
0510 end
0511
0512 if isempty(verbose_swt)
0513
0514 verbose_swt = false;
0515 end
0516 return;
0517
0518
0519
0520
0521
0522 function parent_info = inner_make_parent_info(eegfile)
0523 tmp_eeg = load(eegfile);
0524 parent_info.EEGinfo = tmp_eeg.EEGinfo;
0525 if isfield(tmp_eeg, 'ParentInfo')
0526 parent_info.ParentInfo = tmp_eeg.ParentInfo;
0527 end
0528 if isfield(tmp_eeg, 'LoadSpec')
0529 parent_info.LoadSpec = tmp_eeg.LoadSpec;
0530 end
0531 return;
0532
0533
0534
0535
0536
0537
0538 function inner_store_data_externally(data_dir, ch_name, cur_data, d_type, f_ext)
0539
0540 datafile = sprintf('%s%s%s.%s', data_dir, filesep, ch_name, f_ext);
0541 cur_fid = fopen(datafile, 'wb');
0542 if cur_fid == -1
0543 error('(%s)cannot open file (%s)', mfilename, datafile);
0544 end
0545
0546 fwrite(cur_fid, cur_data, d_type);
0547 fclose(cur_fid);
0548
0549 return;
0550
0551
0552
0553
0554
0555 function loadspec = inner_remove_temporary_fields(loadspec)
0556 target_field{1} = 'new_path';
0557 target_field{2} = 'new_name';
0558 target_field{3} = 'new_data_dir';
0559
0560 for i_field = 1:length(target_field)
0561 if isfield(loadspec, target_field{i_field})
0562 loadspec = rmfield(loadspec, target_field{i_field});
0563 end
0564 end
0565
0566 return;
0567
0568
0569
0570
0571
0572
0573