-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcocon.m
616 lines (523 loc) · 23.4 KB
/
cocon.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
% cocon.m
%1243544%
% $Id$
% usage: cocon
% by: justin gardner
% date: 02/25/2020
% copyright: (c) 2006 Justin Gardner (GPL see mgl/COPYING)
% purpose: 2AFC motion direction paradigm for testing COherence CONtext
%
% getConfidence=1 (sets whether to run confidence measurement)
% taskType=newsome (which task, can be newsome or dirdiff)
% runType=staircase (first run as staircase to get threshold. Then run as low or high
% for low or high cohernce context);
%
function myscreen = cocon(varargin)
% check arguments
getArgs(varargin,{'stimulusType=dots','getConfidence=0','scannerMode=0','taskType=newsome','runType=staircase','threshold=nan'});
% initalize the screen
myscreen.background = 0.5;
myscreen = initScreen(myscreen.background);
% init the stimulus
global stimulus;
myscreen = initStimulus('stimulus',myscreen);
% init info about what kind of stimulus and task we are doing
stimulus.stimulusType = lower(stimulusType);
stimulus.getConfidence = getConfidence;
stimulus.taskType = lower(taskType);
stimulus.runType = lower(runType);
% set which variable to staircase
stimulus.staircase = 'coherence';
% set which variable over which each staircase is made
% for example, you might want to have one staricase for
% each direction difference
stimulus.staircaseOver = 'delta';
% stimulus parameters
stimulus.width = 10;
stimulus.speed = 2.5;
stimulus.eccentricity = [7.5];
stimulus.coherence = [0.25 0.5];
stimulus.delta = 15;
% select the range of values to use for low and high coherence. These are multiplicative of threshold
stimulus.lowVals = [0.5 1 1.5];
stimulus.highVals = [1.5 2 2.5];
stimulus.fullVals = [0.5 1 1.5 2 2.5];
stimulus.fixedFullVals = [0 5 7 10 12 15 20 25];
% init the stimulus
if strcmp(stimulus.stimulusType,'dots')
% init the dots
stimulus = initDotsStimulus(stimulus, myscreen);
stimulus.isGrating = false;
else
disp(sprintf('(cocon) Unknown stimulus type: %s',stimulus.stimulusType));
endScreen(myscreen);
return
end
if strcmp(stimulus.runType,'staircase')
% init the staircases
initialThreshold = 25;
initialStepsize = 5;
% displays the staircase as trial data comes in a figure
dispStaircaseFig = 1;
% number of trials per staircase
nTrialsPerStaircase = 40;
% how many staircases to run until program ends
stimulus.nStaircases = 2;
% whether to try to start from the last computed threshold
useLastThreshold = false;
% call with the above parameters
stimulus = initStaircases(stimulus, myscreen, initialThreshold, initialStepsize, nTrialsPerStaircase, dispStaircaseFig, useLastThreshold);
elseif any(strcmp(stimulus.runType,{'josh1','josh2','full','fixedfull'}))
% this is a regular run, set number of trials
stimulus.nTrials = 50;
% get threshold
if isnan(threshold)
stimulus.threshold = getThreshold(stimulus, myscreen);
else
% use passed in thershold
stimulus.threshold.threshold = threshold;
end
disp(sprintf('(cocon) Threshold: %f',stimulus.threshold.threshold));
if isempty(stimulus.threshold)
endScreen(myscreen);
return
end
% now init constant stimulus
stimulus = initConstantStimuli(stimulus);
end
% wait for backtick to start
task{1}.waitForBacktick = scannerMode;
% task timing and response
if stimulus.getConfidence
% init the confidence display parameters (see function definition below for definition of parameters)
stimulus = initConfidence(stimulus,0,0,3,8,2,[1 1 1],[0.3 0.3 0.3]);
stimulus.feedback.segnum = 5;
% the fifth segment is the one in which the confidence judgement comes up
task{1}.segmin = [0.5 1 inf inf 0.5];
task{1}.segmax = [0.5 1 inf inf 0.5];
task{1}.getResponse = [0 1 1 0 0];
else
% set feedback segment
stimulus.feedback.segnum = [2 3];
% not confidence judgement
task{1}.segmin = [0.5 1 2];
task{1}.segmax = [0.5 1 2];
task{1}.getResponse = [0 1 1];
end
if ~strcmp(stimulus.runType,'staircase')
% no feedback
stimulus.feedback.segnum = nan;
end
% taks parameters
task{1}.parameter.eccentricity = stimulus.eccentricity;
task{1}.randVars.calculated.confidnece = nan;
task{1}.randVars.calculated.correctIncorrect = nan;
task{1}.randVars.uniform.whichAnswer = [1 2];
task{1}.random = 1;
task{1}.randVars.calculated.direction = nan;
% now set the variable that we will staircase on
task{1}.randVars.calculated.(stimulus.staircase) = nan;
% and the variable that we will staircase over
task{1}.parameter.(stimulus.staircaseOver) = stimulus.(stimulus.staircaseOver);
task{1}.randVars.calculated.initialConfidence = nan;
task{1}.randVars.uniform.rangeGetConfidence = [0:0.1:1];
% initialize the task
len = length(task);
for phaseNum = 1:len
[task{phaseNum} myscreen] = initTask(task{phaseNum},myscreen,@startSegmentCallback,@screenUpdateCallback,@responseCallback);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% run the eye calibration
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
myscreen = eyeCalibDisp(myscreen);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Main display loop
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
phaseNum = 1;
while ~all(stimulus.staircaseCompleted == stimulus.nStaircases) && ~myscreen.userHitEsc
% update the task
[task myscreen phaseNum] = updateTask(task,myscreen,phaseNum);
% flip screen
myscreen = tickScreen(myscreen,task);
end
% if we got here, we are at the end of the experiment
myscreen = endTask(myscreen,task);
if ~myscreen.userHitEsc
% display thresholds
for iStaircase = 1:length(stimulus.eccentricity);
% compute threshold
t = doStaircase('threshold',stimulus.s(iStaircase,:));
% display it
disp(sprintf('Eccentricity: %0.2f Threshold: %0.2f',stimulus.eccentricity(iStaircase),t.threshold));
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function that gets called at the start of each segment
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [task myscreen] = startSegmentCallback(task, myscreen)
global stimulus;
if task.thistrial.thisseg == 1
% get which staircase, note that we have kxn staircases - k coherences and n staircases
stimulus.whichStaircase = find(stimulus.(stimulus.staircaseOver)==task.thistrial.(stimulus.staircaseOver));
stimulus.staircaseNum = stimulus.staircaseCompleted(stimulus.whichStaircase)+1;
% get the staircase value to test
[staircaseVal stimulus.s(stimulus.whichStaircase,stimulus.staircaseNum)] = doStaircase('testValue',stimulus.s(stimulus.whichStaircase,stimulus.staircaseNum));
% now set the values
task.thistrial.(stimulus.staircase) = staircaseVal;
% for dirdiff task, we set up two patches
if strcmp(stimulus.taskType,'dirdiff')
% make sure delta does not go below zero
task.thistrial.delta = max(0,task.thistrial.delta);
% set direction
task.thistrial.direction = round(rand*360);
if task.thistrial.whichAnswer == 1
leftDirection = mod(task.thistrial.direction+stimulus.delta,360);
rightDirection = task.thistrial.direction;
else
leftDirection = task.thistrial.direction;
rightDirection = mod(task.thistrial.direction+stimulus.delta,360);
end
% display what is going on
disp(sprintf('%i: Coherence: %0.1f Side: %i delta: %0.2f (Direction %0.1f vs %0.1f Eccentricity: %0.1f)',task.trialnum,task.thistrial.coherence,task.thistrial.whichAnswer,stimulus.delta,leftDirection,rightDirection,task.thistrial.eccentricity));
% set the coherence
stimulus.dotsLeft = stimulus.dotsLeft.setCoherence(stimulus.dotsLeft,task.thistrial.coherence/100);
stimulus.dotsRight = stimulus.dotsRight.setCoherence(stimulus.dotsRight,task.thistrial.coherence/100);
% set the position
stimulus.dotsLeft = stimulus.dotsLeft.setCenter(stimulus.dotsLeft,-task.thistrial.eccentricity,0);
stimulus.dotsRight = stimulus.dotsRight.setCenter(stimulus.dotsRight,task.thistrial.eccentricity,0);
% set the direction
stimulus.dotsLeft = stimulus.dotsLeft.setDir(stimulus.dotsLeft,leftDirection);
stimulus.dotsRight = stimulus.dotsRight.setDir(stimulus.dotsRight,rightDirection);
elseif strcmp(stimulus.taskType,'newsome')
% do a left/right discrimination task
stimulus.dots = stimulus.dots.setCoherence(stimulus.dots,task.thistrial.coherence/100);
stimulus.dots = stimulus.dots.setCenter(stimulus.dots,0,task.thistrial.eccentricity);
% set the direction to left if answer is 1 and right for answer is 2
stimulus.dots = stimulus.dots.setDir(stimulus.dots,180*(1-(task.thistrial.whichAnswer-1)));
disp(sprintf('%i: Coherence: %0.1f Direction: %i',task.trialnum,task.thistrial.coherence,180*(1-(task.thistrial.whichAnswer-1))));
end
% set the fixation color
stimulus.fixColor = stimulus.normalFixColor;
% confidence segment
elseif stimulus.getConfidence && (task.thistrial.thisseg == stimulus.confidence.segnum)
% set starting confidnece
task.thistrial.confidence = randsample(task.randVars.uniform.rangeGetConfidence, 1);
task.thistrial.initialConfidence = task.thistrial.confidence ;
scrollEvents = mglListener('getAllScrollEvents');
mglListener('getAllMouseEvents');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function that gets called to draw the stimulus each frame
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [task myscreen] = screenUpdateCallback(task, myscreen)
global stimulus
% clear screen to gray
mglClearScreen(stimulus.backgroundColor);
if task.thistrial.thisseg == 2
if strcmp(stimulus.taskType,'dirdiff')
% update the dots
stimulus.dotsLeft = stimulus.dotsLeft.update(stimulus.dotsLeft);
stimulus.dotsRight = stimulus.dotsRight.update(stimulus.dotsRight);
% draw the dots
stimulus.dotsLeft = stimulus.dotsLeft.draw(stimulus.dotsLeft);
stimulus.dotsRight = stimulus.dotsRight.draw(stimulus.dotsRight);
else
% update and draw single patch
stimulus.dots = stimulus.dots.update(stimulus.dots);
stimulus.dots = stimulus.dots.draw(stimulus.dots);
end
elseif stimulus.getConfidence && (task.thistrial.thisseg == stimulus.confidence.segnum)
% set the confidence
[task.thistrial.confidence confidenceDone] = setConfidence(task.thistrial.confidence, stimulus);
if confidenceDone
task = jumpSegment(task);
disp(sprintf('(cocon) Confidence: %0.2f',task.thistrial.confidence));
task.thistrial.confidnece = task.thistrial.confidence;
end
end
% feedback segment
%
if any(task.thistrial.thisseg == stimulus.feedback.segnum)
% if correct
if isequal(task.thistrial.correctIncorrect,1)
stimulus.fixColor = stimulus.correctFixColor;
% if incorrect
elseif isequal(task.thistrial.correctIncorrect,0)
stimulus.fixColor = stimulus.incorrectFixColor;
end
end
% draw fixation cross
mglFixationCross(1,1,stimulus.fixColor);
%%%%%%%%%%%%%%%%%%%%%%%%
% initConfidence %
%%%%%%%%%%%%%%%%%%%%%%%%
function stimulus = initConfidence(stimulus,centerX,centerY,width,height,lineSize,lineColor,fillColor)
% set the segment in which the confidence judgement happens
stimulus.confidence.segnum = 4;
% set the dimensions of the confidence display
stimulus.confidence.width = width;
stimulus.confidence.height = height;
stimulus.confidence.centerX = centerX;
stimulus.confidence.centerY = centerY;
% set line size and color
stimulus.confidence.outlineSize = lineSize;
stimulus.confidence.outlineColor = lineColor;
stimulus.confidence.fillColor = fillColor;
% now compute the x and y of the outline
xL = stimulus.confidence.centerX-stimulus.confidence.width/2;
xR = stimulus.confidence.centerX+stimulus.confidence.width/2;
yB = stimulus.confidence.centerY-stimulus.confidence.height/2;
yT = stimulus.confidence.centerY+stimulus.confidence.height/2;
% dimensions of rectangle
stimulus.confidence.X0 = [xL xR xR xL];
stimulus.confidence.X1 = [xR xR xL xL];
stimulus.confidence.Y0 = [yB yB yT yT];
stimulus.confidence.Y1 = [yB yT yT yB];
% dimensions of fill
stimulus.confidence.fillX = [xL xR xR xL];
stimulus.confidence.fillY = [yB yB yT yT];
% values that need to be changed to reflect confidence level
stimulus.confidence.fillTop = [0 0 1 1];
% turn off scrolling
mglListener('eatscroll');
% read all pending scroll events
scrollEvents = mglListener('getAllScrollEvents');
%%%%%%%%%%%%%%%%%%%%%%%%
% drawConfidence %
%%%%%%%%%%%%%%%%%%%%%%%%
function drawConfidence(confidenceLevel, stimulus)
% draw confidence level as a filled bar.
% draw filled inside, compute top coordinate
fillY = stimulus.confidence.fillY;
fillY(find(stimulus.confidence.fillTop)) = stimulus.confidence.centerY + (-0.5+confidenceLevel)*stimulus.confidence.height;
% now draw as a filled polygon
mglPolygon(stimulus.confidence.fillX,fillY,stimulus.confidence.fillColor);
% draw outline
mglLines2(stimulus.confidence.X0,stimulus.confidence.Y0,stimulus.confidence.X1,stimulus.confidence.Y1,stimulus.confidence.outlineSize,stimulus.confidence.outlineColor);
%%%%%%%%%%%%%%%%%%%%%%%
% setConfidence %
%%%%%%%%%%%%%%%%%%%%%%%
function [confidence confidenceDone] = setConfidence(confidence, stimulus)
% get scroll
scrollEvents = mglListener('getAllScrollEvents');
if ~isempty(scrollEvents)
% get the sum of the vertical and horizontal scrolls
verticalScroll = -sum(scrollEvents.scrollVertical);
horizontalScroll = sum(scrollEvents.scrollHorizontal);
% set confidence
confidence = confidence+verticalScroll/100;
else
horizontalScroll = 0;
end
% check bounds
if confidence > 1,confidence = 1;end
if confidence < 0,confidence = 0;end
% draw the confidence
drawConfidence(confidence,stimulus);
% if mouse button down (or horizontal scroll is non-zero) then we are done setting confidence
mouse = mglGetMouse;
keyEvent = mglGetKeyEvent;
if ~isequal(mouse.buttons,0) || ~isempty(keyEvent)
confidenceDone = 1;
else
confidenceDone = 0;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%
% responseCallback %
%%%%%%%%%%%%%%%%%%%%%%%%%%
function [task myscreen] = responseCallback(task,myscreen)
global stimulus
% check the response
if task.thistrial.gotResponse < 1
% see if it is correct
if isequal(task.thistrial.whichButton,task.thistrial.whichAnswer)
% report answer
disp(sprintf(' !! Correct !!. Reaction time: %0.2f',task.thistrial.reactionTime));
% save it in task
task.thistrial.correctIncorrect = 1;
% change fixation color
stimulus.fixColor = stimulus.responseFixColor;
% and update staircase
stimulus.s(stimulus.whichStaircase,stimulus.staircaseNum) = doStaircase('update',stimulus.s(stimulus.whichStaircase,stimulus.staircaseNum),1,task.thistrial.(stimulus.staircase));
else
% report answer
disp(sprintf(' ++ Incorrect ++. Reaction time: %0.2f',task.thistrial.reactionTime));
% save it in task
task.thistrial.correctIncorrect = 0;
% change fixation color
stimulus.fixColor = stimulus.responseFixColor;
% and update staircase
stimulus.s(stimulus.whichStaircase,stimulus.staircaseNum) = doStaircase('update',stimulus.s(stimulus.whichStaircase,stimulus.staircaseNum),0,task.thistrial.(stimulus.staircase));
end
% see if we are done
if doStaircase('stop',stimulus.s(stimulus.whichStaircase,stimulus.staircaseNum))
% update variable that says we are done
stimulus.staircaseCompleted(stimulus.whichStaircase) = stimulus.staircaseCompleted(stimulus.whichStaircase) + 1;
% and initialze the next staircase
if stimulus.staircaseNum < stimulus.nStaircases
stimulus.s(stimulus.whichStaircase,stimulus.staircaseNum+1) = doStaircase('init',stimulus.s(stimulus.whichStaircase,stimulus.staircaseNum));
end
end
if stimulus.getConfidence
% jump to confidence rating part of text
task = jumpSegment(task, stimulus.confidence.segnum);
else
mglClose;
keyboard;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function to init the dots stimulus
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function stimulus = initDotsStimulus(stimulus,myscreen)
if strcmp(stimulus.taskType,'dirdiff')
% init the dot patchs
stimulus.dotsLeft = dotsInit('framesPerSecond',myscreen.framesPerSecond,'dir=0','width',stimulus.width,'speed',stimulus.speed);
stimulus.dotsRight = dotsInit('framesPerSecond',myscreen.framesPerSecond,'dir=180','width',stimulus.width,'speed',stimulus.speed);
elseif strcmp(stimulus.taskType,'newsome')
stimulus.dots = dotsInit('framesPerSecond',myscreen.framesPerSecond,'dir=180','width',stimulus.width,'speed',stimulus.speed);
else
disp(sprintf('(cocon:initDotsStimulus) Unknown task type: %s',stimulus.taskType));
keyboard
end
% set background color
stimulus.backgroundColor = 0.5;
% and fixation color
stimulus.normalFixColor = [1 1 1];
stimulus.correctFixColor = [0 1 0];
stimulus.incorrectFixColor = [1 0 0];
stimulus.responseFixColor = [1 1 1];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function to init the staircases
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function stimulus = initStaircases(stimulus,myscreen,initialThreshold,initialStepsize,nTrials,dispStaircaseFig,useLastThreshold)
if ~useLastThreshold
% if we are not using last threshold,
% then don't bother trying to load last stimfile
stimfile = [];
else
% get the last stimfile
stimfile = getLastStimfile(myscreen);
end
% check that the last stimfile had the same values
if ~isempty(stimfile)
if ~isfield(stimfile,'stimulus') || ~isfield(stimfile.stimulus,stimulus.staircaseOver) || ~isequal(stimfile.stimulus.(stimulus.staircaseOver),stimulus.(stimulus.staircaseOver))
% dump this stimfile, since it does not match current eccentricity
disp(sprintf('(cocon) Found stimfile, but does not have %s match. Ignoring',stimulus.staircaseOver));
stimfile = [];
end
end
% get the variable to staircase over
staircaseOver = stimulus.(stimulus.staircaseOver);
nStaircase = length(staircaseOver);
% if no stimfile found
if isempty(stimfile)
% then initialize
disp('here');
for iStaircase = 1:nStaircase
% print message of what we are doing
disp(sprintf('(cocon) Initializing staircase for %s: %0.2f',stimulus.staircaseOver,staircaseOver(iStaircase)));
% init staircase
% stimulus.s(iStaircase, 1) = doStaircase('init','quest','tGuess',log10(0.5),'tGuessSd=2','nTrials', nTrials,'subplotCols',nStaircase,'subplotNum',iStaircase,'dispFig',dispStaircaseFig);
stimulus.s(iStaircase,1) = doStaircase('init','upDown','nup=1','ndown=2','initialStepsize',initialStepsize,'nTrials',nTrials,'initialThreshold',initialThreshold,'subplotCols',nStaircase,'subplotNum',iStaircase,'dispFig',dispStaircaseFig,'subplotName',sprintf('%s: %0.2f',stimulus.staircaseOver,staircaseOver(iStaircase)),'minThreshold',0,'stepRule=levitt','maxStepsize',0.5,'minStepsize',0.005);
end
else
disp('no, here');
disp(sprintf('(cocon) Found stimfile'))
% init using threshold from last stimfile
for iStaircase = 1:nStaircase
% print message of what we are doing
threshold = doStaircase('threshold',stimfile.stimulus.s(iStaircase,:));
disp(sprintf('(cocon) Initializing staircase for %s: %0.2f from last stimfile with threshold: %0.2f',stimulus.staircaseOver,staircaseOver(iStaircase),threshold.threshold));
% init staircase
stimulus.s(iStaircase,1) = doStaircase('init',stimfile.stimulus.s(iStaircase,end));
end
end
% set that the staircases are not yet done
stimulus.staircaseCompleted = zeros(1,nStaircase);
% and clear threshold field
stimulus.threshold = [];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function to get threshold
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function threshold = getThreshold(stimulus,myscreen)
% get the last stimfile
stimfile = getLastStimfile(myscreen);
if isempty(stimfile)
disp(sprintf('(cocon:getThreshold) No stimfile found, you need to run a staircase first!'));
threshold = [];
return
end
disp(sprintf('(cocon) Found stimfile'));
% now see if we have the proper staircases run for these conditions
% check that the last stimfile had the same values
if ~isfield(stimfile,'stimulus') || ~isfield(stimfile.stimulus,stimulus.staircaseOver) || ~isequal(stimfile.stimulus.(stimulus.staircaseOver),stimulus.(stimulus.staircaseOver))
% dump this stimfile, since it does not match current variable settings
disp(sprintf('(cocon:getThreshold) Found stimfile, but does not have %s match. Need to run staircase again.',stimulus.staircaseOver));
threshold = [];
return
end
% see if threshold already exists
if isfield(stimulus,'threshold') && ~isempty(stimulus.threshold)
disp(sprintf('(cocon:getThreshold) Found existing threshold: %s',num2str(stimulus.threshold.threshold)));
threshold = stimulus.threshold;
return
end
% get number of staircases
staircaseOver = stimulus.(stimulus.staircaseOver);
nStaircase = length(staircaseOver);
nRepeats = size(stimfile.stimulus.s,2);
% now get the threshold values
for iStaircase = 1:nStaircase
staircases = {};
% collect all staircases with more than 80% of trials run
for iRepeat = 1:nRepeats
% get the staircase
thisStaircase = stimfile.stimulus.s(iStaircase,iRepeat);
% get how many trials were run
if (thisStaircase.trialNum/thisStaircase.stopCriterion) > 0.8
disp(sprintf('(cocon:getThreshold) Using staircase for %s: %0.2f (%i/%i) has (%i/%i) trials',stimulus.staircaseOver,staircaseOver(iStaircase),iRepeat,nRepeats,thisStaircase.trialNum,thisStaircase.stopCriterion));
staircases{end+1} = thisStaircase;
else
disp(sprintf('(cocon:getThreshold) IGNORING Staircase for %s: %0.2f (%i/%i) has (%i/%i) trials - TOO FEW TRIALS',stimulus.staircaseOver,staircaseOver(iStaircase),iRepeat,nRepeats,thisStaircase.trialNum,thisStaircase.stopCriterion));
end
end
% no staircases found, return empty
if isempty(staircases)
disp(sprintf('(cocon:getThreshold) No staircases found'));
threshold = [];
return
end
% get threshold
threshold(iStaircase) = doStaircase('threshold',cell2mat(staircases));
% print message of what we are doing
disp(sprintf('(cocon:getThreshold) Staircase for %s: %0.2f from last stimfile had threshold: %0.2f',stimulus.staircaseOver,staircaseOver(iStaircase),threshold.threshold));
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function to init constant stimuli
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function stimulus = initConstantStimuli(stimulus,myscreen)
% get number of thresholds
nThreshold = length(stimulus.threshold);
disp(stimulus.runType);
% set doStaircase to run constant stimuli
for iThreshold = 1:nThreshold
threshold = stimulus.threshold(iThreshold).threshold;
% get the fixed values
if strcmp(stimulus.runType,'josh1')
fixedVals = [0 1.5 2.5];
elseif strcmp(stimulus.runType,'josh2')
fixedVals = [2.5 6 8];
elseif strcmp(stimulus.runType,'full')
fixedVals = threshold * stimulus.fullVals;
elseif strcmp(stimulus.runType,'fixedfull')
fixedVals = stimulus.fixedFullVals;
end
% display what we are doing
disp(sprintf('(cocon:initConstantStimuli) Running %s with values: %s (threshold=%f)',stimulus.runType,num2str(fixedVals),threshold));
% set the staircase to run constant stimuli
stimulus.s(iThreshold,1) = doStaircase('init','fixed','fixedVals',fixedVals,'nTrials',stimulus.nTrials,'dispFig',true);
end
% set this so the code knows how many staircases have been run (in this case staircase is a set of constant stimuli)
stimulus.staircaseCompleted = zeros(1,nThreshold);
stimulus.nStaircases = 1;