-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGRFSpikeController.m
More file actions
executable file
·389 lines (347 loc) · 15.5 KB
/
GRFSpikeController.m
File metadata and controls
executable file
·389 lines (347 loc) · 15.5 KB
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
//
// GRFSpikeController.m
// GaborRFMap
//
// Window with summary information about behavioral performance.
//
// Copyright (c) 2006. All rights reserved.
//
#define NoGlobals
#import "GRF.h"
#import "GRFMapStimTable.h"
#import "UtilityFunctions.h"
#import "GRFSpikeController.h"
#define kContentHeightPix ((kPlotHeightPix + kMarginPix) * kNumSpikes + kMarginPix)
#define kContentWidthPix ((kPlotHeightPix + kMarginPix) * (kNumPlots + 1) + kMarginPix)
#define kMarginPix 10
#define kPlotHeightPix 250
#define kPlotWidthPix 250
#define kSpikeLatencyMS 50
@implementation GRFSpikeController
- (void)checkParams;
{
long spikeIndex, plotIndex;
BOOL dirty = NO;
MapParams *pPtr, *pParam[kNumSpikes][kNumPlots] = {
{&mapSettings[0].directionDeg, &mapSettings[0].spatialFreqCPD, &mapSettings[0].sigmaDeg,
&mapSettings[0].contrastPC},
{&mapSettings[1].directionDeg, &mapSettings[1].spatialFreqCPD, &mapSettings[1].sigmaDeg,
&mapSettings[1].contrastPC}
};
if (mapSettings[0].azimuthDeg.n == 0) { // not initialized yet
return;
}
for (spikeIndex = 0; spikeIndex < kNumSpikes; spikeIndex++) {
pPtr = &mapSettings[spikeIndex].azimuthDeg;
if (pPtr->n != [heatMaps[spikeIndex] plotXPoints] ||
pPtr->maxValue != [heatMaps[spikeIndex] xMaxValue] ||
pPtr->minValue != [heatMaps[spikeIndex] xMinValue]) {
[heatMaps[spikeIndex] setPlotXPoints:pPtr->n];
[heatMaps[spikeIndex] setXMaxValue:pPtr->n - 1];
[heatMaps[spikeIndex] setXMinValue:0];
[self changeHMAxis:heatMaps[spikeIndex] withSettings:*pPtr labelArray:xHMAxisLabels[spikeIndex]];
dirty = YES;
}
pPtr = &mapSettings[spikeIndex].elevationDeg;
if (pPtr->n != [heatMaps[spikeIndex] plotYPoints] ||
pPtr->maxValue != [heatMaps[spikeIndex] yMaxValue] ||
pPtr->minValue != [heatMaps[spikeIndex] yMinValue]) {
[heatMaps[spikeIndex] setPlotYPoints:pPtr->n];
[heatMaps[spikeIndex] setYMaxValue:pPtr->n - 1];
[heatMaps[spikeIndex] setYMinValue:0];
[self changeHMAxis:heatMaps[spikeIndex] withSettings:*pPtr labelArray:yHMAxisLabels[spikeIndex]];
dirty = YES;
}
for (plotIndex = 0; plotIndex < kNumPlots; plotIndex++) {
pPtr = pParam[spikeIndex][plotIndex];
if (pPtr->n != [ratePlots[spikeIndex][plotIndex] points] ||
pPtr->maxValue != [ratePlots[spikeIndex][plotIndex] xMaxDisplayValue] ||
pPtr->minValue != [ratePlots[spikeIndex][plotIndex] xMinDisplayValue]) {
[self changeXAxis:ratePlots[spikeIndex][plotIndex] withSettings:*pPtr
labelArray:xAxisLabels[spikeIndex][plotIndex]];
dirty = YES;
}
}
}
if (dirty) {
[self reset:[NSData data] eventTime:[NSNumber numberWithLong:0]];
}
}
- (void)changeHMAxis:(LLHeatMapView *)plot withSettings:(MapParams)params labelArray:(NSMutableArray *)labels;
{
long index;
double stimValue;
NSString *string;
[labels removeAllObjects];
for (index = 0; index < params.n; index++) {
if (params.n < 2) {
stimValue = params.minValue;
}
else {
stimValue = params.minValue + index * (params.maxValue - params.minValue) / (params.n - 1);
}
string = [NSString stringWithFormat:@"%.*f", [LLTextUtil precisionForValue:stimValue significantDigits:2],
stimValue];
if ((params.n >= 6) && ((index % 2) == (params.n % 2))) {
[labels addObject:@""];
}
else {
[labels addObject:string];
}
}
}
- (void)changeXAxis:(LLPlotView *)plot withSettings:(MapParams)params labelArray:(NSMutableArray *)labels;
{
long index;
double stimValue;
NSString *string;
[plot setPoints:params.n];
[plot setXMin:0 xMax:params.n - 1];
[labels removeAllObjects];
for (index = 0; index < params.n; index++) {
if (params.n < 2) {
stimValue = params.minValue;
}
else {
stimValue = params.minValue + index * (params.maxValue - params.minValue) / (params.n - 1);
}
string = [NSString stringWithFormat:@"%.*f", [LLTextUtil precisionForValue:stimValue significantDigits:2],
stimValue];
if ((params.n >= 6) && ((index % 2) == (params.n % 2))) {
[labels addObject:@""];
}
else {
[labels addObject:string];
}
}
}
- (void)dataParam:(NSData *)eventData eventTime:(NSNumber *)eventTime;
{
DataParam *pParam = (DataParam *)[eventData bytes];
if (strcmp((char *)&pParam->dataName, "spike0") == 0) {
spikePeriodMS = pParam->timing;
}
}
- (void)dealloc;
{
long spikeIndex, plotIndex;
for (spikeIndex = 0; spikeIndex < kNumSpikes; spikeIndex++) {
[xHMAxisLabels[spikeIndex] dealloc];
[yHMAxisLabels[spikeIndex] dealloc];
for (plotIndex = 0; plotIndex < kNumPlots; plotIndex++) {
[xAxisLabels[spikeIndex][plotIndex] release];
}
[stimDescs[spikeIndex] release];
[stimTimes[spikeIndex] release];
}
[super dealloc];
}
- (id) init;
{
if ((self = [super initWithWindowNibName:@"GRFSpikeController" defaults:[task defaults]]) != nil) {
spikePeriodMS = 1.0;
}
return self;
}
- (void)mouseDown:(NSEvent *)theEvent;
{
long spikeIndex, plotIndex;
for (spikeIndex = 0; spikeIndex < kNumSpikes; spikeIndex++) {
for (plotIndex = 0; plotIndex < kNumPlots; plotIndex++) {
[ratePlots[spikeIndex][plotIndex] mouseDown:theEvent];
}
}
}
- (void) windowDidLoad;
{
long index, eleIndex, aziIndex, spikeIndex, plotIndex;
LLViewScale *plotScaling, *hmScaling;
NSMutableArray *theArray;
NSString *plotLabels[] = {@"Direction (deg)", @"Spatial Freq. (CPD)", @"Sigma (deg)", @" Contrast (%)"};
// NSColor *redColor = [NSColor colorWithCalibratedRed:1.0 green:0.0 blue:0.0 alpha:1.0];
// NSColor *blueColor = [NSColor colorWithCalibratedRed:0.0 green:0.0 blue:1.0 alpha:1.0];
NSColor *grayColor = [NSColor colorWithCalibratedRed:0.7 green:0.7 blue:0.7 alpha:1.0];
NSColor *greenColor = [NSColor colorWithCalibratedRed:0.0 green:0.7 blue:0.0 alpha:1.0];
NSColor *brownColor = [NSColor colorWithCalibratedRed:0.6 green:0.4 blue:0.2 alpha:1.0];
NSColor *plotColors[] = {greenColor, brownColor, grayColor};
// NSColor *plotColors[] = {greenColor, brownColor};
[super windowDidLoad];
for (index = 0; index < kNumSpikes; index++) {
stimDescs[index] = [[NSMutableArray alloc] init];
stimTimes[index] = [[NSMutableArray alloc] init];
}
documentView = [scrollView documentView];
[documentView setFrame:NSMakeRect(0, 0, kContentWidthPix, kContentHeightPix)];
[super setBaseMaxContentSize:NSMakeSize(kContentWidthPix, kContentHeightPix)];
highlightColor = [NSColor colorWithDeviceRed:0.85 green:0.85 blue:0.85 alpha:1.0];
// Initialize the spike rate plots
plotScaling = [[[LLViewScale alloc] init] autorelease];
hmScaling = [[[LLViewScale alloc] init] autorelease];
for (spikeIndex = 0; spikeIndex < kNumSpikes; spikeIndex++) {
heatMaps[spikeIndex] = [[[LLHeatMapView alloc] initWithFrame:NSMakeRect(kMarginPix,
kMarginPix + (1 - spikeIndex) * (kMarginPix + kPlotHeightPix),
kPlotWidthPix, kPlotHeightPix)] autorelease];
heatMapRates[spikeIndex] = [[[NSMutableArray alloc] init] autorelease];
for (aziIndex = 0; aziIndex < kMaxMapValues; aziIndex++ ) {
theArray = [[[NSMutableArray alloc] init] autorelease];
for (eleIndex = 0; eleIndex < kMaxMapValues; eleIndex++) {
[theArray addObject:[[[LLNormDist alloc] init] autorelease]];
}
[heatMapRates[spikeIndex] addObject:theArray];
}
[heatMaps[spikeIndex] setPlotValues:heatMapRates[spikeIndex]];
xHMAxisLabels[spikeIndex] = [[NSMutableArray alloc] init];
[heatMaps[spikeIndex] setXAxisTickLabels:xHMAxisLabels[spikeIndex]];
yHMAxisLabels[spikeIndex] = [[NSMutableArray alloc] init];
[heatMaps[spikeIndex] setYAxisTickLabels:yHMAxisLabels[spikeIndex]];
[heatMaps[spikeIndex] setHighlightXRangeColor:highlightColor];
[heatMaps[spikeIndex] setXAxisLabel:[NSString stringWithFormat:@"Spike %ld Azimuth", spikeIndex]];
[heatMaps[spikeIndex] setYAxisLabel:[NSString stringWithFormat:@"Spike %ld Elevation", spikeIndex]];
[documentView addSubview:heatMaps[spikeIndex]];
for (plotIndex = 0; plotIndex < kNumPlots; plotIndex++) {
ratePlots[spikeIndex][plotIndex] = [[[LLPlotView alloc] initWithFrame:NSMakeRect(
kMarginPix + (plotIndex + 1) * (kMarginPix + kPlotHeightPix),
kMarginPix + (1 - spikeIndex) * (kMarginPix + kPlotHeightPix),
kPlotWidthPix, kPlotHeightPix) scaling:plotScaling] autorelease];
xAxisLabels[spikeIndex][plotIndex] = [[NSMutableArray alloc] init];
[ratePlots[spikeIndex][plotIndex] setXAxisTickLabels:xAxisLabels[spikeIndex][plotIndex]];
[ratePlots[spikeIndex][plotIndex] setHighlightXRangeColor:highlightColor];
[ratePlots[spikeIndex][plotIndex] setXAxisLabel:
[NSString stringWithFormat:@"Spike %ld %@", spikeIndex, plotLabels[plotIndex]]];
rates[spikeIndex][plotIndex] = [[[NSMutableArray alloc] init] autorelease];
for (index = 0; index < kMaxMapValues; index++) {
[rates[spikeIndex][plotIndex] addObject:[[[LLNormDist alloc] init] autorelease]];
}
[ratePlots[spikeIndex][plotIndex] addPlot:rates[spikeIndex][plotIndex] plotColor:plotColors[spikeIndex]];
[documentView addSubview:ratePlots[spikeIndex][plotIndex]];
}
}
[self checkParams];
}
- (void)interstimMS:(NSData *)eventData eventTime:(NSNumber *)eventTime;
{
[eventData getBytes:&interstimDurMS];
}
- (void)map0Settings:(NSData *)eventData eventTime:(NSNumber *)eventTime;
{
[eventData getBytes:&mapSettings[0]]; // don't checkParams - wait for map1Settings
}
- (void)map1Settings:(NSData *)eventData eventTime:(NSNumber *)eventTime;
{
[eventData getBytes:&mapSettings[1]];
[self checkParams];
}
- (void)reset:(NSData *)eventData eventTime:(NSNumber *)eventTime;
{
long spikeIndex, plotIndex, aziIndex;
for (spikeIndex = 0; spikeIndex < kNumSpikes; spikeIndex++) {
[[heatMaps[spikeIndex] scale] setHeight:10]; // Reset scaling as well
for (aziIndex = 0; aziIndex < kMaxMapValues; aziIndex++ ) {
[[heatMapRates[spikeIndex] objectAtIndex:aziIndex] makeObjectsPerformSelector:@selector(clear)];
}
for (plotIndex = 0; plotIndex < kNumPlots; plotIndex++) {
[rates[spikeIndex][plotIndex] makeObjectsPerformSelector:@selector(clear)];
[[ratePlots[spikeIndex][plotIndex] scale] setHeight:10]; // Reset scaling as well
}
}
[[[self window] contentView] setNeedsDisplay:YES];
}
- (void)spike:(NSData *)eventData eventTime:(NSNumber *)eventTime;
{
TimestampData spikeData;
[eventData getBytes:&spikeData];
if (spikeData.time >= 0 && spikeData.time < kMaxSpikeMS) {
trialSpikes[spikeData.channel][spikeData.time]++;
}
}
- (void)mapStimDurationMS:(NSData *)eventData eventTime:(NSNumber *)eventTime;
{
[eventData getBytes:&stimDurMS];
}
- (void)stimulus:(NSData *)eventData eventTime:(NSNumber *)eventTime;
{
StimDesc stimDesc;
[eventData getBytes:&stimDesc];
if (stimDesc.gaborIndex != kMapGabor0 && stimDesc.gaborIndex != kMapGabor1) {
return;
}
[stimDescs[stimDesc.gaborIndex - kMapGabor0]
addObject:[NSValue valueWithBytes:&stimDesc objCType:@encode(StimDesc)]];
[stimTimes[stimDesc.gaborIndex - kMapGabor0] addObject:eventTime];
}
- (void)trial:(NSData *)eventData eventTime:(NSNumber *)eventTime;
{
long index, bin;
[eventData getBytes:&trial];
trialStartTime = [eventTime longValue];
NSLog(@"trialStartTime %ld", trialStartTime);
for (index = 0; index < kNumSpikes; index++) {
[stimDescs[index] removeAllObjects];
[stimTimes[index] removeAllObjects];
for (bin = 0; bin < kMaxSpikeMS; bin++) {
trialSpikes[index][bin] = 0;
}
}
}
- (void)trialEnd:(NSData *)eventData eventTime:(NSNumber *)eventTime;
{
long eotCode, startTimeMS, stopTimeMS, index, aziIndex, eleIndex, spikeIndex, spikeCount, plotIndex, bin, minN;
float meanRate, maxRate, minRate;
StimDesc stimDesc;
NSNumber *number;
NSValue *value;
NSArray *eleArray;
NSEnumerator *stimEnumerator, *timeEnumerator;
long *pIndices[kNumPlots] = {&stimDesc.directionIndex, &stimDesc.spatialFreqIndex,
&stimDesc.sigmaIndex, &stimDesc.contrastIndex};
// Update only for correct trials
// Think about if there is any reason to update for other trials.
// Maybe separate superimposed histograms for failed trials
[eventData getBytes:&eotCode];
// if (((eotCode != kEOTCorrect) && (eotCode != kEOTFailed)) || (trial.catchTrial == YES)) {
// return;
// }
for (spikeIndex = 0; spikeIndex < kNumSpikes; spikeIndex++) {
stimEnumerator = [stimDescs[spikeIndex] objectEnumerator];
timeEnumerator = [stimTimes[spikeIndex] objectEnumerator];
while (value = [stimEnumerator nextObject]) { // For each stimulus
[value getValue:&stimDesc];
number = [timeEnumerator nextObject];
startTimeMS = [number longValue] - trialStartTime + kSpikeLatencyMS;
stopTimeMS = startTimeMS + stimDurMS;
if (startTimeMS < 0 || stopTimeMS >= kMaxSpikeMS) {
continue;
}
for (bin = startTimeMS, spikeCount = 0; bin < stopTimeMS; bin++) {
spikeCount += trialSpikes[spikeIndex][bin];
}
for (plotIndex = 0; plotIndex < kNumPlots; plotIndex++) {
[[rates[spikeIndex][plotIndex] objectAtIndex:*pIndices[plotIndex]]
addValue:spikeCount * 1000.0 / stimDurMS];
for (index = 0, minN = LONG_MAX; index < kNumSpikes; index++) {
minN = MIN(minN, [[rates[spikeIndex][plotIndex] objectAtIndex:index] n]);
}
[ratePlots[spikeIndex][plotIndex] setTitle:[NSString stringWithFormat:@"(n >= %ld)", minN]];
[ratePlots[spikeIndex][plotIndex] setNeedsDisplay:YES];
}
[[[heatMapRates[spikeIndex] objectAtIndex:stimDesc.azimuthIndex] objectAtIndex:stimDesc.elevationIndex]
addValue:spikeCount * 1000.0 / stimDurMS];
minN = LONG_MAX;
minRate = FLT_MAX;
maxRate = FLT_MIN;
for (aziIndex = 0; aziIndex < mapSettings[spikeIndex].azimuthDeg.n; aziIndex++) {
eleArray = [heatMapRates[spikeIndex] objectAtIndex:aziIndex];
for (eleIndex = 0; eleIndex < mapSettings[spikeIndex].elevationDeg.n; eleIndex++) {
minN = MIN(minN, [[eleArray objectAtIndex:eleIndex] n]);
meanRate = [[eleArray objectAtIndex:eleIndex] mean];
minRate = MIN(minRate, meanRate);
maxRate = MAX(maxRate, meanRate);
}
}
[heatMaps[spikeIndex] setTitle:[NSString stringWithFormat:@"Rates %.1f-%.1f (n >= %ld)",
minRate, maxRate, minN]];
[heatMaps[spikeIndex] setNeedsDisplay:YES];
}
}
}
@end