-
Notifications
You must be signed in to change notification settings - Fork 2
/
FractControllerContrastG.j
249 lines (222 loc) · 11 KB
/
FractControllerContrastG.j
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
/*
This file is part of FrACT10, a vision test battery.
Copyright © 2021 Michael Bach, [email protected], <https://michaelbach.de>
FractControllerContrastG.j: Gratings
Created by Bach on 2020-09-02
*/
@import "FractControllerContrast.j"
@implementation FractControllerContrastG: FractControllerContrast {
float periodInPixel;
BOOL isGratingColor, isErrorDiffusion;
float specialBcmFreq, specialBcmFreqPrevious;
int specialBcmCountAtStep, specialBcmCountAtStepError;
}
// Method of limits
const specialBcmStepsize = 0.1;
- (void) modifyDeviceStimulus { //console.info("FractControllerContrastG>modifyDeviceStimulus");
responseWasCorrectCumulative = responseWasCorrectCumulative && responseWasCorrect;
if (![Settings specialBcmOn]) return;
if (iTrial == 1) {
nTrials = 200;
alternativesGenerator = [[AlternativesGenerator alloc] initWithNumAlternatives: 2 andNTrials: nTrials obliqueOnly: [Settings gratingObliqueOnly]];
trialHistoryController = [[TrialHistoryController alloc] initWithNumTrials: nTrials];
specialBcmFreq = [Settings gratingCPDmin];
specialBcmFreqPrevious = 0; specialBcmCountAtStep = 1; specialBcmCountAtStepError = 0;
} else {
if (!responseWasCorrect) specialBcmCountAtStepError++;
if (specialBcmCountAtStepError >= 2) {
[self specialBcmDone];
} else {
specialBcmCountAtStep++;
if (specialBcmCountAtStep > 10) {
specialBcmFreqPrevious = specialBcmFreq;
specialBcmFreq *= Math.pow(10, specialBcmStepsize);
if (specialBcmFreq > [Settings gratingCPDmax]) {
[self specialBcmDone];
}
specialBcmCountAtStep = 1; specialBcmCountAtStepError = 0;
}
}
}
[super modifyDeviceStimulus];
}
- (void) specialBcmDone {
spatialFreqCPD = specialBcmFreqPrevious;
nTrials = iTrial - 1; iTrial = 9999;
gSpecialBcmDone = YES;
[self runEnd];
}
- (float) freqFromThresholderunits: (float) thresholderunit {
thresholderunit = 1 - thresholderunit; // 0: strong stimulus=low spatFreq
// exponential mapping for psychometric function to frequency. Does NOT work well.
/*const expFreqMin = Math.pow(10, [Settings gratingCPDmin]);
const expFreqMax = Math.pow(10, [Settings gratingCPDmax]);
const expFreq = expFreqMin + thresholderunit * (expFreqMax - expFreqMin);
return Math.log10(expFreq);*/
// linear mapping for psychometric function to frequency
const freqMin = [Settings gratingCPDmin], freqMax = [Settings gratingCPDmax];
const freq = freqMin + thresholderunit * (freqMax - freqMin);
return freq;
// log mapping for psychometric function to frequency
/* const logFreqMin = Math.log10([Settings gratingCPDmin]);
const logFreqMax = Math.log10([Settings gratingCPDmax]);
const logFreq = logFreqMin + thresholderunit * (logFreqMax - logFreqMin);
return Math.pow(10, logFreq);*/
}
- (void) gratingSineWithPeriodInPx: (float) periodInPx direction: (int) theDirection contrast: (float) contrast {
let s2 = Math.round(Math.max(viewHeight2, viewWidth2) / 2 * 1.3) * 2;
const trigFactor = 1 / periodInPx * 2 * Math.PI; // calculate only once
const notGratingSineNotSquare = ![Settings gratingSineNotSquare]
CGContextRotateCTM(cgc, -theDirection * 22.5 * Math.PI / 180);
CGContextSetLineWidth(cgc, 1.3); // still an artifact on oblique
let lFloat, lDiscrete, lError = 0;
for (let ix = -s2; ix <= s2; ++ix) {
lFloat = Math.sin((ix % periodInPx) * trigFactor);
if (notGratingSineNotSquare) lFloat = lFloat >= 0 ? 1 : -1; // sine → square wave grating
lFloat = 0.5 + 0.5 * contrast / 100 * lFloat; // contrast, map [-1, 1] → [0,1]
if (isGratingColor) {
CGContextSetStrokeColor(cgc, [gColorFore colorWithAlphaComponent: lFloat]);
} else {
lFloat = [MiscLight devicegrayFromLuminance: lFloat]; // gamma correction
lDiscrete = lFloat;
if (isErrorDiffusion) {
lFloat = lFloat * 255 + lError; // map → [0, 255], apply previous residual
lDiscrete = Math.round(lFloat); // discrete integer values [0, 255]
lError = lFloat - lDiscrete; // keep residual (what was lost by rounding)
lDiscrete /= 255; // remap → [0, 1]
}
CGContextSetStrokeColor(cgc, [CPColor colorWithWhite: lDiscrete alpha: 1]);
}
[optotypes strokeVLineAtX: ix y0: -s2 y1: s2];
}
}
- (void) drawStimulusInRect: (CGRect) dirtyRect forView: (FractView) fractView {
isGratingColor = [Settings isGratingColor];
isErrorDiffusion = [Settings gratingUseErrorDiffusion];
[self calculateForeBackColors];
if (isGratingColor) {
gColorFore = [Settings gratingForeColor]; gColorBack = [Settings gratingBackColor];
}
[self prepareDrawing];
if (!isGratingColor) {
CGContextSetFillColor(cgc, [CPColor colorWithWhite: [MiscLight devicegrayFromLuminance: 0.5] alpha: 1]);
CGContextFillRect(cgc, CGRectMake(-viewWidth2, +viewHeight2, viewWidth, -viewHeight));
}
switch(state) {
case kStateDrawBack: break;
case kStateDrawFore:
[self drawFixMark];
break;
case kStateDrawFore2:
if ([self isContrastG]) {
contrastMichelsonPercent = [MiscLight contrastMichelsonPercentFromLogCSWeber: stimStrengthInDeviceunits];
spatialFreqCPD = [Settings gratingCPD];
} else { // acuity_grating
contrastMichelsonPercent = [Settings gratingContrastMichelsonPercent];
spatialFreqCPD = [self freqFromThresholderunits: stimStrengthInThresholderUnits];
if ([Settings specialBcmOn]) {
spatialFreqCPD = specialBcmFreq;
}
}
periodInPixel = Math.max([MiscSpace periodInPixelFromSpatialFrequency: spatialFreqCPD], 2);
let dir = [alternativesGenerator currentAlternative];
if ([Settings isGratingMasked]) {
CGContextBeginPath(cgc);
const r = 0.5 * [MiscSpace pixelFromDegree: [Settings gratingDiaInDeg]];
CGContextAddEllipseInRect(cgc, CGRectMake(0 - r, 0 - r, 2 * r, 2 * r));
CGContextClosePath(cgc); CGContextClip(cgc);
}
[self gratingSineWithPeriodInPx: periodInPixel direction: dir contrast: contrastMichelsonPercent];
[self drawFixMark3];
trialInfoString = [self contrastComposeTrialInfoString];
break;
default: break;
}
[self embedInNoise];
[self drawTouchControls];
CGContextRestoreGState(cgc);
[super drawStimulusInRect: dirtyRect];
if ([self isContrastG]) {
[trialHistoryController setValue: contrastMichelsonPercent];
} else { // acuity_grating
[trialHistoryController setValue: spatialFreqCPD];
}
}
- (void) runStart { //console.info("FractControllerContrastLett>runStart");
nAlternatives = Math.min([Settings nAlternatives], 4);
nTrials = nAlternatives == 4 ? [Settings nTrials04] : [Settings nTrials02];
[super runStart];
[self setCurrentTestResultUnit: "MichelsonPercent"];
}
- (int) responseNumberFromChar: (CPString) keyChar { //console.info("FractControllerContrastE>responseNumberFromChar: ", keyChar);
switch (keyChar) {
case CPLeftArrowFunctionKey: return 4; // ⬅️
case CPRightArrowFunctionKey: return 4; // ➡️
case CPUpArrowFunctionKey: return 0; // ⬆️
case CPDownArrowFunctionKey: return 0; // ⬇️
case "1": return 6; // ↙️
case "2": return 0; // ⬇️
case "3": return 2; // ↘️
case "4": return 4; // ⬅️
case "5": return -1;
case "6": return 4; // ➡️
case "7": return 2; // ↖️
case "8": return 0; // ⬆️
case "9": return 6; // ↗️
}
return -2;// 0, 2, 4, 6: valid; -1: ignore; -2: invalid
}
- (CPString) contrastComposeTrialInfoString {
let s = "trial: " + iTrial + "/" + nTrials;
s += ", contrast: " + [Misc stringFromNumber: contrastMichelsonPercent decimals: 1 localised: YES] + "%";
s += ", frequency: " + [Misc stringFromNumber: spatialFreqCPD decimals: 2 localised: YES];
s += ", alternative: " + [alternativesGenerator currentAlternative];
return s;
}
- (CPString) contrastComposeResultString {
rangeLimitStatus = kRangeLimitOk;
// taking into account the result of final trial
stimStrengthInDeviceunits = [self stimDeviceunitsFromThresholderunits: [thresholder nextStim2apply]];
if ([self isContrastG]) {
contrastMichelsonPercent = [MiscLight contrastMichelsonPercentFromLogCSWeber: stimStrengthInDeviceunits];
spatialFreqCPD = [Settings gratingCPD];
} else { // acuity_grating
if (![Settings specialBcmOn]) {
spatialFreqCPD = [self freqFromThresholderunits: stimStrengthInThresholderUnits];
}
contrastMichelsonPercent = [Settings gratingContrastMichelsonPercent];
}
/* needs work for frequency sweep
if (contrastMichelsonPercent < 100 / 512) // 2 × 256
rangeLimitStatus = kRangeLimitValueAtFloor;
if (contrastMichelsonPercent >= 100)
rangeLimitStatus = kRangeLimitValueAtCeiling; */
let s = "Grating contrast: ";
s += [self rangeStatusIndicatorStringInverted: YES];
s += [Misc stringFromNumber: contrastMichelsonPercent decimals: 2 localised: YES];
s += "%, spatial frequency: ";
s += [Misc stringFromNumber: spatialFreqCPD decimals: 2 localised: YES];
s += " cpd";
return s;
}
- (CPString) contrastComposeExportString {
if ([[self parentController] runAborted]) return "";
let _exportString = [self generalComposeExportString];
_exportString += tab + "value" + tab + [Misc stringFromNumber: contrastMichelsonPercent decimals: 3 localised: YES];
_exportString += tab + "unit1" + tab + currentTestResultUnit
_exportString += tab + "distanceInCm" + tab + [Misc stringFromNumber: [Settings distanceInCM] decimals: 2 localised: YES];
_exportString += tab + "spatFreq" + tab + [Misc stringFromNumber: spatialFreqCPD decimals: 2 localised: YES];
_exportString += tab + "unit2" + tab + "cpd";
_exportString += tab + "nTrials" + tab + [Misc stringFromNumber: nTrials decimals: 0 localised: YES];
_exportString += tab + "rangeLimitStatus" + tab + rangeLimitStatus;
_exportString += tab + "crowding" + tab + 0; // does not apply, but let's not NaN this
if (isGratingColor) {
_exportString += tab + "colorForeBack" + tab + [gColorFore hexString] + tab + [gColorBack hexString];
if ([self isAcuityGrating]) {
_exportString += tab + "cpdMin" + tab + [Misc stringFromNumber: [Settings gratingCPDmin] decimals: 3 localised: YES];
_exportString += tab + "cpdMax" + tab + [Misc stringFromNumber: [Settings gratingCPDmax] decimals: 2 localised: YES];
}
}
return [self generalComposeExportStringFinalize: _exportString];
}
@end