Skip to content

Commit 17f0182

Browse files
First commit of a starting GRIDlab codebase
0 parents  commit 17f0182

File tree

4,394 files changed

+417014
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

4,394 files changed

+417014
-0
lines changed

.gitignore

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
##---------------------------------------------------
2+
## Remove autosaves generated by the Matlab editor
3+
## We have git for backups!
4+
## from https://github.com/github/gitignore/blob/master/Global/Matlab.gitignore ## We have git for backups!
5+
6+
##---------------------------------------------------
7+
8+
# images
9+
*.png
10+
*.jpg
11+
*.jpeg
12+
*.svg
13+
*.fig
14+
15+
# Windows default autosave extension
16+
*.asv
17+
18+
# OSX / *nix default autosave extension
19+
*.m~
20+
21+
# Compiled MEX binaries (all platforms)
22+
*.mex*
23+
24+
# Simulink Code Generation
25+
slprj/
26+
27+
# Session info
28+
octave-workspace
29+
30+
# Simulink autosave extension
31+
.autosave

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# CodeBase
2+
This is the github repository for the GRIDlab code base. The first existing code (circa JDW, 2.10.2015), has been pushed to a "StartingCodeRepo" folder. Variations or substantial deviations should be based off of this in other subfolders under the master Codebase folder
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
function AnonymizeCodes(subjID, dirToStartIn)
2+
3+
if dirToStartIn(end) ~= '\'
4+
dirToStartIn(end+1) = '\';
5+
end
6+
7+
patientDir = [dirToStartIn subjID '\'];
8+
9+
ProcessDir(patientDir, subjID)
10+
% ProcessDir(dirToStartIn, subjID);
11+
12+
fprintf('DONE\n');
13+
end
14+
15+
function ProcessDir(directory, subjID)
16+
17+
subDirs = dir(directory);
18+
subDirs = subDirs([subDirs.isdir]);
19+
subDirs = {subDirs.name};
20+
21+
subFiles = dir(directory);
22+
subFiles = subFiles(~[subFiles.isdir]);
23+
subFiles = {subFiles.name};
24+
25+
26+
for next = subFiles
27+
fullFile = [directory next{:}];
28+
locs = regexp(next{:},['(?i)' subjID], 'once');
29+
if ~isempty(locs)
30+
fprintf('Converting file %s...\n', fullFile);
31+
oldFile = fullFile;
32+
newFile = [directory strrep(next{:},subjID,genPID(subjID))];
33+
cmd = sprintf('movefile(''%s'', ''%s'');', oldFile, newFile);
34+
eval(cmd);
35+
end
36+
end
37+
38+
39+
40+
41+
for target=subDirs
42+
if strcmp(target{:},'..') == 1 || strcmp(target{:},'.') == 1
43+
continue
44+
end
45+
ProcessDir([directory target{:} '\'], subjID);
46+
end
47+
48+
slashies = find(directory == '\',2,'last');
49+
if isempty(slashies)
50+
return;
51+
end
52+
subDirHasSubjID = regexp(directory(slashies(1):slashies(2)),['(?i)' subjID], 'once');
53+
if isempty(subDirHasSubjID)
54+
return
55+
end
56+
fprintf('Converting directory %s...\n', directory);
57+
oldDir = directory;
58+
newDir = [directory(1:slashies(1)) strrep(directory(slashies(1)+1:slashies(2)),subjID,genPID(subjID))];
59+
cmd = sprintf('movefile(''%s'', ''%s'');', oldDir, newDir);
60+
eval(cmd);
61+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function params = CleanBCI2000ParamStruct(parms)
2+
3+
flds = fieldnames(parms);
4+
for i=flds';
5+
try
6+
tempField = parms.(i{1});
7+
% fprintf('%s ',tempField.Type);
8+
switch tempField.Type
9+
case 'string'
10+
eval(sprintf('params.%s = tempField.Value;',i{1}));
11+
case 'matrix'
12+
eval(sprintf('params.%s = tempField.Value;',i{1}));
13+
otherwise
14+
numVal = double(tempField.NumericValue);
15+
eval(sprintf('params.%s = numVal;',i{1}));
16+
end
17+
catch
18+
bad = cell2mat(i);
19+
fprintf(' ignoring params.%s, not numerical\n', bad);
20+
end
21+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function gloveTrace = CreateSmoothGloveTrace(states, parameters, method)
2+
% function [gloveTrace] = CreateSmoothGloveTrace(states, parameters, method)
3+
%
4+
% Changelog
5+
% 8/11/11 - tmb - originally written
6+
% 6/16/21 - jdw - changed to accept multiple cyberglove states naming
7+
% conventions
8+
%
9+
% Simple function to take in the states and parameter structure from
10+
% BCI2000 and use a spline to interpolate the stairstepped glove trace.
11+
%
12+
% states - the bci2k state struct
13+
% parameters - the bci2k parameters structure
14+
% method - 'spline' or 'pchip', which is the piecewise cubic Hermite
15+
% interpolation. Slightly less organic than the spline, but avoids the
16+
% flat-step-flat artifacts that occur with splines
17+
18+
if isstruct(parameters.SamplingRate) == 1
19+
params = CleanBCI2000ParamStruct(parameters);
20+
else
21+
params = parameters;
22+
end
23+
24+
if (isfield(states, 'rCyber1'))
25+
prefix = 'r';
26+
elseif (isfield(states, 'lCyber1'))
27+
prefix = 'l';
28+
else
29+
prefix = '';
30+
end
31+
32+
for i=1:22
33+
eval(sprintf('gloveTrace(:,i) = double(states.%sCyber%i);',prefix,i));
34+
zeroPeriod = find(gloveTrace(:,i) > 0, 1, 'first')-1;
35+
gloveTrace(1:zeroPeriod,i) = gloveTrace(zeroPeriod+1,i);
36+
37+
% gloveTrace(:,i) =
38+
% spline(1:params.SampleBlockSize:length(gloveTrace(:,i)),gloveTrace(1:params.SampleBlockSize:end,i),1:length(gloveTrace(:,i)));
39+
gloveTrace(:,i) = interp1(1:params.SampleBlockSize:length(gloveTrace(:,i)),gloveTrace(1:params.SampleBlockSize:end,i),1:length(gloveTrace(:,i)),method);
40+
end
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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

Comments
 (0)