Skip to content

Commit

Permalink
First commit of a starting GRIDlab codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjuliancaldwell committed Mar 29, 2017
0 parents commit 17f0182
Show file tree
Hide file tree
Showing 4,394 changed files with 417,014 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
31 changes: 31 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
##---------------------------------------------------
## Remove autosaves generated by the Matlab editor
## We have git for backups!
## from https://github.com/github/gitignore/blob/master/Global/Matlab.gitignore ## We have git for backups!

##---------------------------------------------------

# images
*.png
*.jpg
*.jpeg
*.svg
*.fig

# Windows default autosave extension
*.asv

# OSX / *nix default autosave extension
*.m~

# Compiled MEX binaries (all platforms)
*.mex*

# Simulink Code Generation
slprj/

# Session info
octave-workspace

# Simulink autosave extension
.autosave
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# CodeBase
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
61 changes: 61 additions & 0 deletions StartingCodeRepo/DataPrep/AnonymizeCodes.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
function AnonymizeCodes(subjID, dirToStartIn)

if dirToStartIn(end) ~= '\'
dirToStartIn(end+1) = '\';
end

patientDir = [dirToStartIn subjID '\'];

ProcessDir(patientDir, subjID)
% ProcessDir(dirToStartIn, subjID);

fprintf('DONE\n');
end

function ProcessDir(directory, subjID)

subDirs = dir(directory);
subDirs = subDirs([subDirs.isdir]);
subDirs = {subDirs.name};

subFiles = dir(directory);
subFiles = subFiles(~[subFiles.isdir]);
subFiles = {subFiles.name};


for next = subFiles
fullFile = [directory next{:}];
locs = regexp(next{:},['(?i)' subjID], 'once');
if ~isempty(locs)
fprintf('Converting file %s...\n', fullFile);
oldFile = fullFile;
newFile = [directory strrep(next{:},subjID,genPID(subjID))];
cmd = sprintf('movefile(''%s'', ''%s'');', oldFile, newFile);
eval(cmd);
end
end




for target=subDirs
if strcmp(target{:},'..') == 1 || strcmp(target{:},'.') == 1
continue
end
ProcessDir([directory target{:} '\'], subjID);
end

slashies = find(directory == '\',2,'last');
if isempty(slashies)
return;
end
subDirHasSubjID = regexp(directory(slashies(1):slashies(2)),['(?i)' subjID], 'once');
if isempty(subDirHasSubjID)
return
end
fprintf('Converting directory %s...\n', directory);
oldDir = directory;
newDir = [directory(1:slashies(1)) strrep(directory(slashies(1)+1:slashies(2)),subjID,genPID(subjID))];
cmd = sprintf('movefile(''%s'', ''%s'');', oldDir, newDir);
eval(cmd);
end
21 changes: 21 additions & 0 deletions StartingCodeRepo/DataPrep/CleanBCI2000ParamStruct.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function params = CleanBCI2000ParamStruct(parms)

flds = fieldnames(parms);
for i=flds';
try
tempField = parms.(i{1});
% fprintf('%s ',tempField.Type);
switch tempField.Type
case 'string'
eval(sprintf('params.%s = tempField.Value;',i{1}));
case 'matrix'
eval(sprintf('params.%s = tempField.Value;',i{1}));
otherwise
numVal = double(tempField.NumericValue);
eval(sprintf('params.%s = numVal;',i{1}));
end
catch
bad = cell2mat(i);
fprintf(' ignoring params.%s, not numerical\n', bad);
end
end
40 changes: 40 additions & 0 deletions StartingCodeRepo/DataPrep/CreateSmoothGloveTrace.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function gloveTrace = CreateSmoothGloveTrace(states, parameters, method)
% function [gloveTrace] = CreateSmoothGloveTrace(states, parameters, method)
%
% Changelog
% 8/11/11 - tmb - originally written
% 6/16/21 - jdw - changed to accept multiple cyberglove states naming
% conventions
%
% Simple function to take in the states and parameter structure from
% BCI2000 and use a spline to interpolate the stairstepped glove trace.
%
% states - the bci2k state struct
% parameters - the bci2k parameters structure
% method - 'spline' or 'pchip', which is the piecewise cubic Hermite
% interpolation. Slightly less organic than the spline, but avoids the
% flat-step-flat artifacts that occur with splines

if isstruct(parameters.SamplingRate) == 1
params = CleanBCI2000ParamStruct(parameters);
else
params = parameters;
end

if (isfield(states, 'rCyber1'))
prefix = 'r';
elseif (isfield(states, 'lCyber1'))
prefix = 'l';
else
prefix = '';
end

for i=1:22
eval(sprintf('gloveTrace(:,i) = double(states.%sCyber%i);',prefix,i));
zeroPeriod = find(gloveTrace(:,i) > 0, 1, 'first')-1;
gloveTrace(1:zeroPeriod,i) = gloveTrace(zeroPeriod+1,i);

% gloveTrace(:,i) =
% spline(1:params.SampleBlockSize:length(gloveTrace(:,i)),gloveTrace(1:params.SampleBlockSize:end,i),1:length(gloveTrace(:,i)));
gloveTrace(:,i) = interp1(1:params.SampleBlockSize:length(gloveTrace(:,i)),gloveTrace(1:params.SampleBlockSize:end,i),1:length(gloveTrace(:,i)),method);
end
160 changes: 160 additions & 0 deletions StartingCodeRepo/DataPrep/GetBciClinicalData.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
%% TODO, needs to cross validate recording DATE for BCI2k File and EDF.
%% Doesn't currently happen and this can lead to false results.

curDir = pwd;

%% select files of interest and get output filename
cd (myGetenv('subject_dir'));

fprintf('select an edf file: \n');
[filename, directory] = uigetfile('*.rec;*.edf','MultiSelect', 'off');
edfFilename = [directory filename];

fprintf('select a bci2k file: \n');
[filename, directory] = uigetfile('*.dat','MultiSelect', 'off');
bci2kFilename = [directory filename];

subjid = extractSubjid(bci2kFilename);

%% load bci2kdata
[sig, sta, par] = load_bcidat(bci2kFilename);
defMaxChanNum = size(sig,2);


%% get channel montages for comparison
bChans = input(sprintf('channels from the BCI2K file to compare (default is [1:%d]): ', defMaxChanNum));

if (isempty(bChans))
bChans = 1:defMaxChanNum;
end

cChans = input(sprintf('channels from the Clinical file to compare (default is [2:%d]): ', defMaxChanNum+1));

if (isempty(cChans))
cChans = 2:defMaxChanNum+1;
end

[clinicalMontage, channelConfig] = getMontageFromEDF(edfFilename);

fprintf(' here is the clinical montage: \n\n');

for c = 1:length(clinicalMontage)
fprintf(' %d) %s\n', c, clinicalMontage{c});
end

selectedMontage = input(sprintf('select the components of the clinical montage to extract (typical matlab vector format [a b:c]): '));
sChans = find(ismember(channelConfig, selectedMontage));

%% match bcidata to clinical data

% [signals, offsets] = matchClinicalData(edfFilename, bci2kFilename, true, 2:chans+1);
[signals, offsets] = matchClinicalData(edfFilename, bci2kFilename, true, cChans, bChans, sChans);


%% save result

% massage the offsets a little bit to account for synch errors either on
% our side or the clinical side
offsets = round(offsets/100)*100;

offsets
mode(offsets)

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));
result = input(' is this sufficient to save the clinical data? ([Y]\\n): ', 's');

trodesFilename = [myGetenv('subject_dir') subjid '\trodes.mat'];
if (exist(trodesFilename, 'file'))
load(trodesFilename);
end % otherwise all the trodes will be 0,0,0

if (isempty(result) || strcmp(lower(result), 'y') == 1)
defOutputFilename = strrep(bci2kFilename, '.dat', '_clinical.mat');
outputFilename = input(sprintf('filename to save [%s]: ', strrep(defOutputFilename,'\','\\')), 's');

if (isempty(outputFilename))
outputFilename = defOutputFilename;
end

mOutputFilename = strrep(outputFilename, '.mat', '_montage.mat');

bci2kFs = par.SamplingRate.NumericValue;

EDF = sdfopen(edfFilename, 'r', 2);
fs = round(mode(EDF.SampleRate));
sdfclose(EDF);

% feedback = resampleBci2kDiscreteState(sta.Feedback, bci2kFs, fs);
% targetCode = resampleBci2kDiscreteState(sta.TargetCode, bci2kFs, fs);
% resultCode = resampleBci2kDiscreteState(sta.ResultCode, bci2kFs, fs);
%
% fprintf('saving %s\n', outputFilename);
% save(outputFilename, 'signals', 'feedback', 'targetCode', 'resultCode', 'fs');

stimulusCode = resampleBci2kDiscreteState(sta.StimulusCode, bci2kFs, fs);

fprintf('saving %s\n', outputFilename);
save(outputFilename, 'signals', 'stimulusCode', 'fs');

for c = 1:length(selectedMontage)
Montage.Montage(c) = sum(channelConfig == selectedMontage(c));
end

Montage.MontageTokenized = {clinicalMontage{selectedMontage}};

Montage.MontageTrodes = [];

for c = 1:length(Montage.MontageTokenized)
% build montage string
if (c == 1)
Montage.MontageString = Montage.MontageTokenized{c};
else
Montage.MontageString = [Montage.MontageString ' ' Montage.MontageTokenized{c}];
end

% build electrodes
mElt = regexpi(Montage.MontageTokenized{c}, '([a-z]+).+', 'tokens', 'once');
mElt = mElt{1};

if (exist(mElt, 'var'))
eval(sprintf('Montage.MontageTrodes = cat(1, Montage.MontageTrodes, %s);', mElt));
else
warning ('could not find electrode locations for %s, these will need to be set manually in the montage file', mElt);
Montage.MontageTrodes = cat(1, Montage.MontageTrodes, zeros(Montage.Montage(c), 3));
end
end

fprintf('Bad channels must be updated manually in the montage.\n');

fprintf('saving %s\n', mOutputFilename);
save(mOutputFilename, 'Montage');

end

cd (curDir);


% %% create the appropriate variables in the .mat file
% % signals, feedback, targetCode, resultCode, fs, Montage
% bci2kFs = par.SamplingRate.NumericValue;
%
% EDF = sdfopen(edfFilename, 'r', 2);
% fs = round(mode(EDF.SampleRate));
% sdfclose(EDF);
%
% feedback = resampleBci2kDiscreteState(sta.Feedback, bci2kFs, fs);
% targetCode = resampleBci2kDiscreteState(sta.TargetCode, bci2kFs, fs);
% resultCode = resampleBci2kDiscreteState(sta.ResultCode, bci2kFs, fs);
%
% montageFilename = strrep(bci2kFilename, '.dat', '_montage.mat');
% if (exist(montageFilename, 'file'))
% load(montageFilename);
% else
% Montage.Montage = size(signals, 2);
% Montage.BadChannels = [];
% end
%
% save(outputFilename, 'signals', 'feedback', 'targetCode', 'resultCode', 'fs');
%
% cd (curDir);
%
Loading

0 comments on commit 17f0182

Please sign in to comment.