-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge new feature (v1.58.0) to export and load stim to and from BIDS …
…events TSV files.
- Loading branch information
Showing
48 changed files
with
1,453 additions
and
439 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
Oops, something went wrong.