Skip to content

Commit 6089f97

Browse files
authored
1.77.0 -- Fix stim events TSV issue with conditions names with spaces - 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.
1 parent 1680519 commit 6089f97

File tree

10 files changed

+118
-37
lines changed

10 files changed

+118
-37
lines changed

AppSettings.cfg

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ don't ask again
1111
one processing element per file
1212
1313
% Load Stim From TSV File # Yes, No
14-
Yes
14+
No
15+
16+
% Replace TSV File Tabs with Spaces # Yes, No
17+
No
1518
1619
% END

DataTree/AcquiredData/AcqDataClass.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@
9292
if ~ischar(fileobj)
9393
fileobj = '';
9494
end
95-
obj.LoadBids(fileobj);
9695
end
9796

9897

@@ -162,7 +161,11 @@ function Initialize(obj)
162161
if isempty(file)
163162
return
164163
end
165-
obj.bids.stim = readTsv([filesepStandard(p), file(1).name],'numstr2num');
164+
[obj.bids.stim, err] = readTsv([filesepStandard(p), file(1).name],'numstr2num');
165+
% if err < 0
166+
% obj.SetError(-8);
167+
% return;
168+
% end
166169
if isempty(obj.bids.stim)
167170
return
168171
end

DataTree/AcquiredData/Snirf/SnirfClass.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,10 @@ function SortStims(obj)
455455
function err = LoadStim(obj, fileobj)
456456
err = 0;
457457

458-
if obj.LoadStimOverride(obj.GetFilename())
458+
if obj.LoadStimOverride(obj.GetFilename())
459+
% if obj.GetError()<0
460+
% err = -1;
461+
% end
459462
return
460463
end
461464

DataTree/Examples/EditStimExample.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function EditStimExample(dataSetDir)
8282

8383
pause(2);
8484

85-
obj.EditStim();
85+
obj.EditStim(1);
8686

8787
obj.ClosePlots();
8888
dataTree.ReloadStim();

DataTree/TreeNodeClass.m

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,10 @@ function ReloadStim(obj)
549549

550550

551551
% ----------------------------------------------------------------------------------
552-
function EditStim(obj)
552+
function EditStim(obj, waitForInput)
553+
if ~exist('waitForInput','var')
554+
waitForInput = 0;
555+
end
553556
if isempty(obj.acquired)
554557
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)]));
555558
return;
@@ -578,7 +581,9 @@ function EditStim(obj)
578581
end
579582
editorTab = editorTabs(ii);
580583
editorTab.makeActive;
581-
% MenuBox('Please edit TSV stim file and save it, then click the ''OK'' button.');
584+
if waitForInput
585+
MenuBox('Please edit TSV stim file and save it, then click the ''OK'' button.');
586+
end
582587
else
583588
if ispc()
584589
cmd = sprintf('start notepad %s', filenameEvents);
@@ -588,7 +593,9 @@ function EditStim(obj)
588593
cmd = sprintf('open -a TextEdit %s', filenameEvents);
589594
obj.logger.Write('cmd: "%s"', cmd);
590595
system(cmd);
591-
% 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));
596+
if waitForInput
597+
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));
598+
end
592599
end
593600
end
594601
end

DataTree/Version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.12.0
1+
1.12.1

Utils/Shared/MenuBox.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
if ~exist('textLineWidth','var') || isempty(textLineWidth)
4848
textLineWidth = 70;
4949
end
50-
if ~exist('options','var')
50+
if ~exist('options','var') || isempty(options)
5151
options = '';
5252
end
5353

@@ -78,7 +78,7 @@
7878

7979

8080
% Initial X size and position of text
81-
Wtext = 70;
81+
Wtext = textLineWidth;
8282

8383
nNewLines = length(find(msg == sprintf('\n')))+4; %#ok<SPRINTFN>
8484
nLines = ceil(length(msg) / Wtext)*1.5;
@@ -186,6 +186,9 @@
186186
uicontrol('parent',hf, 'style','text', 'string',bttns{k}, 'units','characters', 'position',[p(1)+4, p(2), p(3), p(4)], ...
187187
'horizontalalignment','left', 'fontsize',fs(2), 'userdata',2, 'backgroundcolor',[1.0, 1.0, 1.0]);
188188
else
189+
if nbttns==1
190+
p(1) = floor(Wfig/2 - Wbttn/2);
191+
end
189192
uicontrol('parent',hf, 'style',selectionStyle, 'string',bttns{k}, 'units','characters', 'position',[p(1), p(2), p(3), p(4)+Hbttn/2], ...
190193
'tag',sprintf('%d', k), 'fontsize',fs(2), 'callback',@pushbuttonGroup_Callback, 'userdata',2, 'backgroundcolor',[1.0, 1.0, 1.0]);
191194
end

Utils/Shared/Version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.4.6
1+
1.5.0

Utils/Shared/tsv/readTsv.m

Lines changed: 86 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
function [tsv, tabReplacefFlag] = readTsv(filename, option)
1+
function [tsv, err] = readTsv(filename, option)
2+
global cfg
3+
cfg = InitConfig(cfg);
4+
err = 0;
5+
26
tsv = struct([]);
37
if ~exist('filename', 'var')
48
filename = '';
@@ -20,9 +24,9 @@
2024
numstr2num = true;
2125
end
2226
fid = fopen(filename, 'rt');
23-
kk = 1;
27+
nLines = 0;
2428
ncols = 0;
25-
tabReplacefFlag = false;
29+
tabReplacefFlag = [];
2630
while 1
2731
line = fgetl(fid);
2832
if line==-1
@@ -32,25 +36,32 @@
3236
continue;
3337
end
3438

39+
nLines = nLines+1;
40+
3541
% Get rid of any non-ascii characters from read line
3642
if ischar(line)
3743
line(line>130) = '';
3844
line(line<9) = '';
3945
end
4046

4147
delimiter = findDelimiter(strtrim(line), ncols);
48+
c = str2cell(strtrim(line), delimiter);
49+
if ncols == 0
50+
ncols = length(c);
51+
end
52+
53+
% Check for errors
54+
if errorCheck([f,e], nLines, c, ncols, delimiter)<0
55+
err = -1;
56+
return;
57+
end
4258

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

50-
c = str2cell(strtrim(line), delimiter);
51-
if ncols == 0
52-
ncols = length(c);
53-
end
5465
if isempty(tsv)
5566
tsv = cell(1,length(c));
5667
end
@@ -61,17 +72,24 @@
6172
end
6273
end
6374
end
64-
tsv(kk,:) = c;
65-
kk = kk+1;
75+
tsv(nLines,:) = c;
6676
end
6777
fclose(fid);
6878

69-
7079
% If delimiter contains non-tab spaces then raise flag that
71-
% they need to be replaces by tabs in a tav file
72-
if tabReplacefFlag
73-
fprintf('Rewriting file %s to replace space separators with tabs\n', filename);
74-
writeTsv(filename, tsv);
80+
% they need to be replaces by tabs in a tsv file
81+
if ~isempty(tabReplacefFlag)
82+
fprintf('WARNING: File "%s" uses space separators instead of tabs at lines: [ %s ]. This could confuse some applications\n', ...
83+
[f,e], num2str(tabReplacefFlag));
84+
val = '';
85+
if ~isempty(cfg)
86+
val = cfg.GetValue('Replace TSV File Tabs with Spaces');
87+
end
88+
if strcmpi(val, 'Yes')
89+
printMethod(sprintf('Rewriting file %s to replace space separators with tabs\n', filename));
90+
copyfile(filename, [filename, '.orig']);
91+
writeTsv(filename, tsv);
92+
end
7593
end
7694

7795

@@ -97,15 +115,59 @@
97115
delimiter = sprintf('\t');
98116
elseif ntabs==0 && nspaces>0 && nspaces+1==ncols
99117
delimiter = sprintf(' ');
100-
elseif ntabs>0 && nspaces>0
101-
if ntabs+1 == ncols
102-
delimiter = sprintf(' ');
103-
elseif nspaces+1 == ncols
104-
delimiter = sprintf('\t');
105-
else
106-
delimiter = {sprintf('\t'), ' '};
118+
elseif ntabs>0 && nspaces>0 && nspaces+1==ncols
119+
delimiter = {sprintf('\t'), ' '};
120+
elseif ntabs>0
121+
delimiter = sprintf('\t');
122+
end
123+
end
124+
125+
126+
127+
128+
% -----------------------------------------------------------
129+
function err = errorCheck(filename, nLines, c, ncols, delimiter)
130+
err = 0;
131+
errmsg = '';
132+
if length(c) ~= ncols
133+
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', ...
134+
length(c), nLines, filename, ncols);
135+
for ii = 1:length(c)
136+
errmsg{ii+1} = sprintf('col %d: "', ii);
137+
c2 = str2cell(c{ii}, {delimiter, sprintf('\t')});
138+
for jj = 1:length(c2)
139+
errmsg{ii+1} = [errmsg{ii+1}, sprintf('%s ', c2{jj})];
107140
end
141+
errmsg{ii+1} = [errmsg{ii+1}, sprintf('"\n')];
108142
end
109143
end
144+
if ~isempty(errmsg)
145+
errmsgLen = length([errmsg{:}]);
146+
if errmsgLen > 70
147+
errmsgLen = 100;
148+
end
149+
MenuBox(errmsg,{},[],errmsgLen);
150+
err = -1;
151+
end
152+
printMethod(errmsg);
153+
154+
155+
156+
% -------------------------------------------------------------------------
157+
function printMethod(msg)
158+
global logger
159+
if isempty(msg)
160+
return;
161+
end
162+
if isa(logger', 'Logger')
163+
try
164+
logger.Write(msg);
165+
catch
166+
fprintf(msg);
167+
end
168+
else
169+
fprintf(msg);
170+
end
171+
110172

111173

Version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.76.0
1+
1.77.0

0 commit comments

Comments
 (0)