|
| 1 | +%% TODO, needs to cross validate recording DATE for BCI2k File and EDF. |
| 2 | +%% Doesn't currently happen and this can lead to false results. |
| 3 | + |
| 4 | +curDir = pwd; |
| 5 | + |
| 6 | +%% select files of interest and get output filename |
| 7 | +cd (myGetenv('subject_dir')); |
| 8 | + |
| 9 | +fprintf('select an edf file: \n'); |
| 10 | +[filename, directory] = uigetfile('*.rec;*.edf','MultiSelect', 'off'); |
| 11 | +edfFilename = [directory filename]; |
| 12 | + |
| 13 | +fprintf('select a bci2k file: \n'); |
| 14 | +[filename, directory] = uigetfile('*.dat','MultiSelect', 'off'); |
| 15 | +bci2kFilename = [directory filename]; |
| 16 | + |
| 17 | +subjid = extractSubjid(bci2kFilename); |
| 18 | + |
| 19 | +%% load bci2kdata |
| 20 | +[sig, sta, par] = load_bcidat(bci2kFilename); |
| 21 | +defMaxChanNum = size(sig,2); |
| 22 | + |
| 23 | + |
| 24 | +%% get channel montages for comparison |
| 25 | +bChans = input(sprintf('channels from the BCI2K file to compare (default is [1:%d]): ', defMaxChanNum)); |
| 26 | + |
| 27 | +if (isempty(bChans)) |
| 28 | + bChans = 1:defMaxChanNum; |
| 29 | +end |
| 30 | + |
| 31 | +cChans = input(sprintf('channels from the Clinical file to compare (default is [2:%d]): ', defMaxChanNum+1)); |
| 32 | + |
| 33 | +if (isempty(cChans)) |
| 34 | + cChans = 2:defMaxChanNum+1; |
| 35 | +end |
| 36 | + |
| 37 | +[clinicalMontage, channelConfig] = getMontageFromEDF(edfFilename); |
| 38 | + |
| 39 | +fprintf(' here is the clinical montage: \n\n'); |
| 40 | + |
| 41 | +for c = 1:length(clinicalMontage) |
| 42 | + fprintf(' %d) %s\n', c, clinicalMontage{c}); |
| 43 | +end |
| 44 | + |
| 45 | +selectedMontage = input(sprintf('select the components of the clinical montage to extract (typical matlab vector format [a b:c]): ')); |
| 46 | +sChans = find(ismember(channelConfig, selectedMontage)); |
| 47 | + |
| 48 | +%% match bcidata to clinical data |
| 49 | + |
| 50 | +% [signals, offsets] = matchClinicalData(edfFilename, bci2kFilename, true, 2:chans+1); |
| 51 | +[signals, offsets] = matchClinicalData(edfFilename, bci2kFilename, true, cChans, bChans, sChans); |
| 52 | + |
| 53 | + |
| 54 | +%% save result |
| 55 | + |
| 56 | +% massage the offsets a little bit to account for synch errors either on |
| 57 | +% our side or the clinical side |
| 58 | +offsets = round(offsets/100)*100; |
| 59 | + |
| 60 | +offsets |
| 61 | +mode(offsets) |
| 62 | + |
| 63 | +fprintf('clinical data matched, %f percent of channels correlated gave the same offset (%d of %d) \n', sum(offsets == mode(offsets)) / length(offsets) * 100, sum(offsets == mode(offsets)), length(offsets)); |
| 64 | +result = input(' is this sufficient to save the clinical data? ([Y]\\n): ', 's'); |
| 65 | + |
| 66 | +trodesFilename = [myGetenv('subject_dir') subjid '\trodes.mat']; |
| 67 | +if (exist(trodesFilename, 'file')) |
| 68 | + load(trodesFilename); |
| 69 | +end % otherwise all the trodes will be 0,0,0 |
| 70 | + |
| 71 | +if (isempty(result) || strcmp(lower(result), 'y') == 1) |
| 72 | + defOutputFilename = strrep(bci2kFilename, '.dat', '_clinical.mat'); |
| 73 | + outputFilename = input(sprintf('filename to save [%s]: ', strrep(defOutputFilename,'\','\\')), 's'); |
| 74 | + |
| 75 | + if (isempty(outputFilename)) |
| 76 | + outputFilename = defOutputFilename; |
| 77 | + end |
| 78 | + |
| 79 | + mOutputFilename = strrep(outputFilename, '.mat', '_montage.mat'); |
| 80 | + |
| 81 | + bci2kFs = par.SamplingRate.NumericValue; |
| 82 | + |
| 83 | + EDF = sdfopen(edfFilename, 'r', 2); |
| 84 | + fs = round(mode(EDF.SampleRate)); |
| 85 | + sdfclose(EDF); |
| 86 | + |
| 87 | +% feedback = resampleBci2kDiscreteState(sta.Feedback, bci2kFs, fs); |
| 88 | +% targetCode = resampleBci2kDiscreteState(sta.TargetCode, bci2kFs, fs); |
| 89 | +% resultCode = resampleBci2kDiscreteState(sta.ResultCode, bci2kFs, fs); |
| 90 | +% |
| 91 | +% fprintf('saving %s\n', outputFilename); |
| 92 | +% save(outputFilename, 'signals', 'feedback', 'targetCode', 'resultCode', 'fs'); |
| 93 | + |
| 94 | + stimulusCode = resampleBci2kDiscreteState(sta.StimulusCode, bci2kFs, fs); |
| 95 | + |
| 96 | + fprintf('saving %s\n', outputFilename); |
| 97 | + save(outputFilename, 'signals', 'stimulusCode', 'fs'); |
| 98 | + |
| 99 | + for c = 1:length(selectedMontage) |
| 100 | + Montage.Montage(c) = sum(channelConfig == selectedMontage(c)); |
| 101 | + end |
| 102 | + |
| 103 | + Montage.MontageTokenized = {clinicalMontage{selectedMontage}}; |
| 104 | + |
| 105 | + Montage.MontageTrodes = []; |
| 106 | + |
| 107 | + for c = 1:length(Montage.MontageTokenized) |
| 108 | + % build montage string |
| 109 | + if (c == 1) |
| 110 | + Montage.MontageString = Montage.MontageTokenized{c}; |
| 111 | + else |
| 112 | + Montage.MontageString = [Montage.MontageString ' ' Montage.MontageTokenized{c}]; |
| 113 | + end |
| 114 | + |
| 115 | + % build electrodes |
| 116 | + mElt = regexpi(Montage.MontageTokenized{c}, '([a-z]+).+', 'tokens', 'once'); |
| 117 | + mElt = mElt{1}; |
| 118 | + |
| 119 | + if (exist(mElt, 'var')) |
| 120 | + eval(sprintf('Montage.MontageTrodes = cat(1, Montage.MontageTrodes, %s);', mElt)); |
| 121 | + else |
| 122 | + warning ('could not find electrode locations for %s, these will need to be set manually in the montage file', mElt); |
| 123 | + Montage.MontageTrodes = cat(1, Montage.MontageTrodes, zeros(Montage.Montage(c), 3)); |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + fprintf('Bad channels must be updated manually in the montage.\n'); |
| 128 | + |
| 129 | + fprintf('saving %s\n', mOutputFilename); |
| 130 | + save(mOutputFilename, 'Montage'); |
| 131 | + |
| 132 | +end |
| 133 | + |
| 134 | +cd (curDir); |
| 135 | + |
| 136 | + |
| 137 | +% %% create the appropriate variables in the .mat file |
| 138 | +% % signals, feedback, targetCode, resultCode, fs, Montage |
| 139 | +% bci2kFs = par.SamplingRate.NumericValue; |
| 140 | +% |
| 141 | +% EDF = sdfopen(edfFilename, 'r', 2); |
| 142 | +% fs = round(mode(EDF.SampleRate)); |
| 143 | +% sdfclose(EDF); |
| 144 | +% |
| 145 | +% feedback = resampleBci2kDiscreteState(sta.Feedback, bci2kFs, fs); |
| 146 | +% targetCode = resampleBci2kDiscreteState(sta.TargetCode, bci2kFs, fs); |
| 147 | +% resultCode = resampleBci2kDiscreteState(sta.ResultCode, bci2kFs, fs); |
| 148 | +% |
| 149 | +% montageFilename = strrep(bci2kFilename, '.dat', '_montage.mat'); |
| 150 | +% if (exist(montageFilename, 'file')) |
| 151 | +% load(montageFilename); |
| 152 | +% else |
| 153 | +% Montage.Montage = size(signals, 2); |
| 154 | +% Montage.BadChannels = []; |
| 155 | +% end |
| 156 | +% |
| 157 | +% save(outputFilename, 'signals', 'feedback', 'targetCode', 'resultCode', 'fs'); |
| 158 | +% |
| 159 | +% cd (curDir); |
| 160 | +% |
0 commit comments