Skip to content

Commit

Permalink
Merge new feature (v1.58.0) to export and load stim to and from BIDS …
Browse files Browse the repository at this point in the history
…events TSV files.
  • Loading branch information
jayd1860 committed Dec 15, 2022
2 parents 93401dc + 3747310 commit c187f58
Show file tree
Hide file tree
Showing 48 changed files with 1,453 additions and 439 deletions.
2 changes: 2 additions & 0 deletions AppSettings.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ don't ask again
% Export HRF Mean Output Style # all child processing elements in one file, one processing element per file
one processing element per file
% Load Stim From TSV File # Yes, No
Yes
% END
112 changes: 112 additions & 0 deletions DataTree/AcquiredData/AcqDataClass.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
classdef AcqDataClass < matlab.mixin.Copyable

properties (Access = public)
bids
end
properties (Access = private)
logger
end
Expand Down Expand Up @@ -76,19 +79,39 @@

methods

% -------------------------------------------------------
function obj = AcqDataClass(fileobj)
if nargin == 0
return
end
if iscell(fileobj)
if ~isempty(fileobj)
fileobj = fileobj{1};
end
end
if ~ischar(fileobj)
fileobj = '';
end
obj.LoadBids(fileobj);
end



% -------------------------------------------------------
function Initialize(obj)
global logger
obj.logger = InitLogger(logger);
end



% -------------------------------------------------------
function err = Error(obj)
err = obj.GetError();
end



% ---------------------------------------------------------
function msg = GetErrorMsg(obj)
msg = '';
Expand All @@ -106,6 +129,77 @@ function Initialize(obj)
end



% -------------------------------------------------------
function err = LoadBids(obj, fileobj)
err = obj.LoadStimOverride(fileobj);
end



% -------------------------------------------------------
function status = LoadStimOverride(obj, fileobj)
global cfg
status = false;
cfg = InitConfig(cfg);
if strcmpi(cfg.GetValue('Load Stim From TSV File'), 'no')
return
end
obj.bids = struct('stim',{{}});
if isempty(fileobj)
return
end
[p,f] = fileparts(fileobj);
if isempty(p)
p = filesepStandard(pwd);
end
k = strfind(f, '_nirs');
if isempty(k)
k = length(f)+1;
end
fnameTsv = [filesepStandard(p), f(1:k-1), '_events.tsv'];
file = mydir(fnameTsv);
if isempty(file)
return
end
obj.bids.stim = readTsv([filesepStandard(p), file(1).name],'numstr2num');
if isempty(obj.bids.stim)
return
end
s = TsvFile2Snirf(obj.bids.stim);
obj.stim = s.stim.copy();
status = true;
end



% -------------------------------------------------------
function err = ReloadStim(obj, fileobj)
err = 0;
obj.bids = struct('stim',{{}});
if isempty(fileobj)
return
end
[p,f] = fileparts(fileobj);
if isempty(p)
p = filesepStandard(pwd);
end
k = strfind(f, '_nirs');
if isempty(k)
k = length(f)+1;
end
fnameTsv = [filesepStandard(p), f(1:k-1), '_events.tsv'];
file = mydir(fnameTsv);
if isempty(file)
return
end
obj.bids.stim = readTsv([filesepStandard(p), file(1).name],'numstr2num');
s = TsvFile2Snirf(obj.bids.stim);
obj.stim = s.stim.copy();
end



% -------------------------------------------------------
function FreeMemory(obj, filename)
if ~exist('filename','var')
Expand All @@ -118,6 +212,7 @@ function FreeMemory(obj, filename)
end



% ---------------------------------------------------------
function bbox = GetSdgBbox(obj)
bbox = [];
Expand Down Expand Up @@ -150,6 +245,7 @@ function FreeMemory(obj, filename)
end



% ----------------------------------------------------------------------------------
function varval = GetVar(obj, varname)
if ismethod(obj,['Get_', varname])
Expand Down Expand Up @@ -179,6 +275,7 @@ function FreeMemory(obj, filename)
end



% ----------------------------------------------------------------------------------
function t = GetTimeCombined(obj)
% Function combines the time vectors for all data blocks into one time vectors.
Expand All @@ -204,6 +301,7 @@ function FreeMemory(obj, filename)
end



% ----------------------------------------------------------------------------------
function data = GetStimData(~, ~)
data = [];
Expand Down Expand Up @@ -250,6 +348,20 @@ function FreeMemory(obj, filename)



% ----------------------------------------------------------------------------------
function fnameTsv = GetStimTsvFilename(obj)
fnameTsv = '';
[pname, fname] = fileparts(obj.GetFilename());
k = strfind(fname, '_nirs');
if isempty(k)
k = length(fname)+1;
end
if isempty(fname)
return;
end
fnameTsv = [filesepStandard(pname), fname(1:k-1), '_events.tsv'];
end

end

end
1 change: 1 addition & 0 deletions DataTree/AcquiredData/DataFiles/DeleteDataFiles.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ function DeleteDataFiles(varargin)
if strcmp(options, 'delete')
fprintf('Deleting %s\n', [datafiles(ii).rootdir, '/', datafiles(ii).name]);
delete([datafiles(ii).rootdir, '/', datafiles(ii).name]);
delete([datafiles(ii).rootdir, '/*_events.tsv']);
elseif strcmp(options, 'move')
fprintf('Moving %s to %s\n', [datafiles(ii).rootdir, '/', datafiles(ii).name], [datafiles(ii).rootdir, '/', datafiles(ii).name, '.old']);
movefile([datafiles(ii).rootdir, '/', datafiles(ii).name], [datafiles(ii).rootdir, '/', datafiles(ii).name, '.old']);
Expand Down
7 changes: 7 additions & 0 deletions DataTree/AcquiredData/DataFiles/DeleteTsv.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function DeleteTsv(rootdir)
if ~exist('rootdir','var')
rootdir = pwd;
end
rootdir = filesepStandard(rootdir);
Snirf2Tsv(rootdir, 'remove');

36 changes: 36 additions & 0 deletions DataTree/AcquiredData/DataFiles/Snirf2Tsv.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function Snirf2Tsv(rootdir, options)
if ~exist('rootdir','var')
rootdir = pwd;
end
if ~exist('options','var')
options = pwd;
end
rootdir = filesepStandard(rootdir);
files = DataFilesClass(rootdir, 'snirf');
files = files.files;
for ii = 1:length(files)
if files(ii).IsDir()
continue
end
fname = filesepStandard([files(ii).rootdir, files(ii).name]);
[pname, fname, ext1] = fileparts(fname);
k = strfind(fname, '_nirs');
if isempty(k)
k = length(fname)+1;
end
fnameNew = [filesepStandard(pname), fname(1:k-1), '_events.tsv'];
src = [filesepStandard(pname), fname, ext1];
dst = fnameNew;
if optionExists(options, 'delete') || optionExists(options, 'remove')
if ispathvalid(dst)
fprintf('Deleting %s\n', dst);
delete(dst);
end
else
fprintf('Converting %s to %s\n', src, dst);
SnirfFile2Tsv(src, dst, 'removeStim');
end
end



58 changes: 58 additions & 0 deletions DataTree/AcquiredData/DataFiles/SnirfFile2Tsv.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
function tsv = SnirfFile2Tsv(src, dst, options)
tsv = {};

% Parse args
if nargin==0
return
end
if ~isa(src, 'SnirfClass')
s = SnirfClass(src);
else
s = src;
end
if s.IsEmpty()
s.LoadStim(s.GetFilename());
end
if ~exist('dst','var')
dst = '';
end

if isempty(dst)
dst = s.GetStimTsvFilename();
end
if ~exist('options','var')
options = '';
end
if isempty(s.stim)
s0 = StimClass();
else
s0 = s.stim(1);
end
tsv = [s0.dataLabels(:)', 'trial_type'];
data = [];
for ii = 1:length(s.stim)
iS = size(tsv,1)+1;
iE = size(tsv,1)+size(s.stim(ii).data,1);
tsv(iS:iE,:) = [num2cell(s.stim(ii).data), repmat({s.stim(ii).name}, size(s.stim(ii).data,1),1)]; %#ok<*AGROW>
data = [data; s.stim(ii).data];
end
if ~isempty(data)
[~, order] = sortrows(data,1);
tsv(2:end,:) = tsv(order+1,:);
end
if ispathvalid(dst)
if optionExists(options, 'regenerate')
writeTsv(dst, tsv);
end
else
writeTsv(dst, tsv);
end

% Remove stim from
% if optionExists(options, 'removeStim')
% s.Load();
% s.stim = StimClass().empty();
% s.Save();
% end


68 changes: 68 additions & 0 deletions DataTree/AcquiredData/DataFiles/TsvFile2Snirf.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
function snirf = TsvFile2Snirf(src, dst, option)
snirf = SnirfClass();

% Parse args
if nargin==0
return
end
if ~exist('dst','var')
dst = '';
end
if ~exist('option','var')
option = 'nofile';
end

if ischar(src)
tsv = readTsv(src);
else
tsv = src;
src = '';
end
if isempty(tsv)
return;
end

if isempty(dst) && ispathvalid(src)
[pname, fname] = fileparts(src);
k = strfind(fname, '_events');
if isempty(k)
k = length(fname)+1;
end
dst = [filesepStandard(pname), fname(1:k-1), '_nirs.snirf'];
end

snirf.SetFilename(dst);

fields = tsv(1,:);
k = find(strcmp(fields, 'trial_type'));
if isempty(k)
return
end
for ii = 2:size(tsv,1)
jj = findStim(snirf.stim, tsv{ii,k});
if jj == 0
snirf.stim(end+1) = StimClass(tsv([1,ii],:));
else
snirf.stim(jj).AddTsvData(tsv([1,ii],:));
end
end
if strcmp(option,'nofile')
return
end
snirf.Save();


% -----------------------------------------------------------
function jj = findStim(stim, tsv)
jj = 0;
for ii = 1:length(stim)
if isnumeric(tsv)
tsv = num2str(tsv);
end
if strcmp(stim(ii).name, tsv)
jj = ii;
break;
end
end


Loading

0 comments on commit c187f58

Please sign in to comment.