Skip to content

Commit

Permalink
1.77.0 -- Fix stim events TSV issue with conditions names with spaces…
Browse files Browse the repository at this point in the history
… - we should be able to handle that. (#170)

* v1.77.0

Utils, v1.5.0
-- Fix stim events TSV issue with conditions names with spaces - we should be able to handle that. Change rule to handle spaces and how error reporting is done in readTsv.

DataTree, v1.12.1
-- Don't load events TSV files twice - once in SnirfClass and once in AcqDataClass its superclass.
  • Loading branch information
jayd1860 authored Apr 14, 2023
1 parent 1680519 commit 6089f97
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 37 deletions.
5 changes: 4 additions & 1 deletion AppSettings.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ don't ask again
one processing element per file
% Load Stim From TSV File # Yes, No
Yes
No
% Replace TSV File Tabs with Spaces # Yes, No
No
% END
7 changes: 5 additions & 2 deletions DataTree/AcquiredData/AcqDataClass.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
if ~ischar(fileobj)
fileobj = '';
end
obj.LoadBids(fileobj);
end


Expand Down Expand Up @@ -162,7 +161,11 @@ function Initialize(obj)
if isempty(file)
return
end
obj.bids.stim = readTsv([filesepStandard(p), file(1).name],'numstr2num');
[obj.bids.stim, err] = readTsv([filesepStandard(p), file(1).name],'numstr2num');
% if err < 0
% obj.SetError(-8);
% return;
% end
if isempty(obj.bids.stim)
return
end
Expand Down
5 changes: 4 additions & 1 deletion DataTree/AcquiredData/Snirf/SnirfClass.m
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,10 @@ function SortStims(obj)
function err = LoadStim(obj, fileobj)
err = 0;

if obj.LoadStimOverride(obj.GetFilename())
if obj.LoadStimOverride(obj.GetFilename())
% if obj.GetError()<0
% err = -1;
% end
return
end

Expand Down
2 changes: 1 addition & 1 deletion DataTree/Examples/EditStimExample.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function EditStimExample(dataSetDir)

pause(2);

obj.EditStim();
obj.EditStim(1);

obj.ClosePlots();
dataTree.ReloadStim();
Expand Down
13 changes: 10 additions & 3 deletions DataTree/TreeNodeClass.m
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,10 @@ function ReloadStim(obj)


% ----------------------------------------------------------------------------------
function EditStim(obj)
function EditStim(obj, waitForInput)
if ~exist('waitForInput','var')
waitForInput = 0;
end
if isempty(obj.acquired)
MenuBox(sprintf('%s level processing does not have stims. Please select a run to edit stim marks\n', [upper(obj.type(1)), obj.type(2:end)]));
return;
Expand Down Expand Up @@ -578,7 +581,9 @@ function EditStim(obj)
end
editorTab = editorTabs(ii);
editorTab.makeActive;
% MenuBox('Please edit TSV stim file and save it, then click the ''OK'' button.');
if waitForInput
MenuBox('Please edit TSV stim file and save it, then click the ''OK'' button.');
end
else
if ispc()
cmd = sprintf('start notepad %s', filenameEvents);
Expand All @@ -588,7 +593,9 @@ function EditStim(obj)
cmd = sprintf('open -a TextEdit %s', filenameEvents);
obj.logger.Write('cmd: "%s"', cmd);
system(cmd);
% MenuBox(sprintf('The events file associated with the current processing element is\n%s\n Open the file in a text editor to modify stim marks', filenameEvents));
if waitForInput
MenuBox(sprintf('The events file associated with the current processing element is\n%s\n Open the file in a text editor to modify stim marks', filenameEvents));
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion DataTree/Version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.12.0
1.12.1
7 changes: 5 additions & 2 deletions Utils/Shared/MenuBox.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
if ~exist('textLineWidth','var') || isempty(textLineWidth)
textLineWidth = 70;
end
if ~exist('options','var')
if ~exist('options','var') || isempty(options)
options = '';
end

Expand Down Expand Up @@ -78,7 +78,7 @@


% Initial X size and position of text
Wtext = 70;
Wtext = textLineWidth;

nNewLines = length(find(msg == sprintf('\n')))+4; %#ok<SPRINTFN>
nLines = ceil(length(msg) / Wtext)*1.5;
Expand Down Expand Up @@ -186,6 +186,9 @@
uicontrol('parent',hf, 'style','text', 'string',bttns{k}, 'units','characters', 'position',[p(1)+4, p(2), p(3), p(4)], ...
'horizontalalignment','left', 'fontsize',fs(2), 'userdata',2, 'backgroundcolor',[1.0, 1.0, 1.0]);
else
if nbttns==1
p(1) = floor(Wfig/2 - Wbttn/2);
end
uicontrol('parent',hf, 'style',selectionStyle, 'string',bttns{k}, 'units','characters', 'position',[p(1), p(2), p(3), p(4)+Hbttn/2], ...
'tag',sprintf('%d', k), 'fontsize',fs(2), 'callback',@pushbuttonGroup_Callback, 'userdata',2, 'backgroundcolor',[1.0, 1.0, 1.0]);
end
Expand Down
2 changes: 1 addition & 1 deletion Utils/Shared/Version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4.6
1.5.0
110 changes: 86 additions & 24 deletions Utils/Shared/tsv/readTsv.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
function [tsv, tabReplacefFlag] = readTsv(filename, option)
function [tsv, err] = readTsv(filename, option)
global cfg
cfg = InitConfig(cfg);
err = 0;

tsv = struct([]);
if ~exist('filename', 'var')
filename = '';
Expand All @@ -20,9 +24,9 @@
numstr2num = true;
end
fid = fopen(filename, 'rt');
kk = 1;
nLines = 0;
ncols = 0;
tabReplacefFlag = false;
tabReplacefFlag = [];
while 1
line = fgetl(fid);
if line==-1
Expand All @@ -32,25 +36,32 @@
continue;
end

nLines = nLines+1;

% Get rid of any non-ascii characters from read line
if ischar(line)
line(line>130) = '';
line(line<9) = '';
end

delimiter = findDelimiter(strtrim(line), ncols);
c = str2cell(strtrim(line), delimiter);
if ncols == 0
ncols = length(c);
end

% Check for errors
if errorCheck([f,e], nLines, c, ncols, delimiter)<0
err = -1;
return;
end

% If delimiter contains non-tab spaces then raise flag that
% If delimiter contains non-tab spaces then raise flag that
% they need to be replaces by tabs in a tav file
if ~isempty(find(strcmp(delimiter,' ')))
fprintf('WARNING: file %s uses space separators instead of tabs. This could confuse some applitions\n', filename);
tabReplacefFlag = true;
tabReplacefFlag = [tabReplacefFlag, nLines];
end

c = str2cell(strtrim(line), delimiter);
if ncols == 0
ncols = length(c);
end
if isempty(tsv)
tsv = cell(1,length(c));
end
Expand All @@ -61,17 +72,24 @@
end
end
end
tsv(kk,:) = c;
kk = kk+1;
tsv(nLines,:) = c;
end
fclose(fid);


% If delimiter contains non-tab spaces then raise flag that
% they need to be replaces by tabs in a tav file
if tabReplacefFlag
fprintf('Rewriting file %s to replace space separators with tabs\n', filename);
writeTsv(filename, tsv);
% they need to be replaces by tabs in a tsv file
if ~isempty(tabReplacefFlag)
fprintf('WARNING: File "%s" uses space separators instead of tabs at lines: [ %s ]. This could confuse some applications\n', ...
[f,e], num2str(tabReplacefFlag));
val = '';
if ~isempty(cfg)
val = cfg.GetValue('Replace TSV File Tabs with Spaces');
end
if strcmpi(val, 'Yes')
printMethod(sprintf('Rewriting file %s to replace space separators with tabs\n', filename));
copyfile(filename, [filename, '.orig']);
writeTsv(filename, tsv);
end
end


Expand All @@ -97,15 +115,59 @@
delimiter = sprintf('\t');
elseif ntabs==0 && nspaces>0 && nspaces+1==ncols
delimiter = sprintf(' ');
elseif ntabs>0 && nspaces>0
if ntabs+1 == ncols
delimiter = sprintf(' ');
elseif nspaces+1 == ncols
delimiter = sprintf('\t');
else
delimiter = {sprintf('\t'), ' '};
elseif ntabs>0 && nspaces>0 && nspaces+1==ncols
delimiter = {sprintf('\t'), ' '};
elseif ntabs>0
delimiter = sprintf('\t');
end
end




% -----------------------------------------------------------
function err = errorCheck(filename, nLines, c, ncols, delimiter)
err = 0;
errmsg = '';
if length(c) ~= ncols
errmsg{1} = sprintf('ERROR: Found %d data columns in line %d of %s which does not match number of columns in file (%d)\n\n', ...
length(c), nLines, filename, ncols);
for ii = 1:length(c)
errmsg{ii+1} = sprintf('col %d: "', ii);
c2 = str2cell(c{ii}, {delimiter, sprintf('\t')});
for jj = 1:length(c2)
errmsg{ii+1} = [errmsg{ii+1}, sprintf('%s ', c2{jj})];
end
errmsg{ii+1} = [errmsg{ii+1}, sprintf('"\n')];
end
end
if ~isempty(errmsg)
errmsgLen = length([errmsg{:}]);
if errmsgLen > 70
errmsgLen = 100;
end
MenuBox(errmsg,{},[],errmsgLen);
err = -1;
end
printMethod(errmsg);



% -------------------------------------------------------------------------
function printMethod(msg)
global logger
if isempty(msg)
return;
end
if isa(logger', 'Logger')
try
logger.Write(msg);
catch
fprintf(msg);
end
else
fprintf(msg);
end



2 changes: 1 addition & 1 deletion Version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.76.0
1.77.0

0 comments on commit 6089f97

Please sign in to comment.