-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtiScore.cpp
More file actions
363 lines (321 loc) · 12.8 KB
/
tiScore.cpp
File metadata and controls
363 lines (321 loc) · 12.8 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
#include <vector>
#include <random>
#include <numeric>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <Python.h>
using namespace std;
namespace py = pybind11;
#include "tcHeader.h"
PYBIND11_MAKE_OPAQUE(std::vector<double>);
/// Returns the mean dfbf[ frame# ] for the specified shuffle[ trial# ]
/// using data[ trial# ][ frame# ]. Operates on data for a single cell.
vector< double > aveOfTrials(
const vector< vector< double > >& data,
const vector< unsigned int >& shuff,
unsigned int numFrames )
{
unsigned int numTrials = data.size();
vector< double > aveBin( numFrames, 0.0 );
assert( numTrials == data.size() && numTrials > 0 );
for ( unsigned int ii = 0; ii < numTrials; ++ii ) {
vector< double >::iterator aptr = aveBin.begin();
auto dd = data[ii].begin();
for ( unsigned int bb = shuff[ii]; bb < shuff[ii] + numFrames; bb++, aptr++) {
*aptr += *( dd + bb%numFrames );
}
}
for( vector< double >::iterator aptr = aveBin.begin(); aptr != aveBin.end(); aptr++ ) {
*aptr /= numFrames;
}
return aveBin;
}
/// returns binned data for a single trial.
vector< double > binner( const vector< double >& orig, unsigned int binFrames )
{
if (binFrames <= 1 || binFrames >= orig.size()/2 ) return orig;
unsigned int numRet = orig.size() / binFrames;
vector< double > ret( numRet, 0.0 );
for ( unsigned int idx = 0; idx < numRet; idx++ ) {
for ( unsigned int bf = 0; bf < binFrames; bf++ )
ret[idx] += orig[ idx * binFrames + bf ];
}
return ret;
}
/// returns binned data for a single cell, all trials.
vector< vector< double > > binnedCellData( const vector< vector< double > >& data, unsigned int numFrames, unsigned int binFrames )
{
unsigned int numTrials = data.size();
vector< vector< double > > ret( numTrials );
for( unsigned int trialIdx = 0; trialIdx < numTrials; trialIdx++ ) {
ret[ trialIdx ] = binner( data[ trialIdx ], binFrames );
}
return ret;
}
/// Return the average tuning curve of the cell. Uses peeling to find all
/// significant peaks separated by at least ap.minPeakSep seconds.
void tuningCurve( const vector< vector< double >>& data, const vector< vector< unsigned int > >& shuff, unsigned int numFrames, const AnalysisParams& ap, CellScore& cs )
{
unsigned int numTrials = data.size();
vector< vector< double >> binnedCellData_ = binnedCellData( data, numFrames, ap.binFrames );
unsigned int numBins = binnedCellData_[0].size();
vector< unsigned int > zeros( numTrials, 0 );
cs.meanTrace = aveOfTrials( binnedCellData_, zeros, numBins );
// Pre-compute all shuffled mean traces once; reused for every peak.
vector< vector< double > > shuffMeans( ap.numShuffle );
for ( unsigned int ii = 0; ii < ap.numShuffle; ii++ )
shuffMeans[ii] = aveOfTrials( binnedCellData_, shuff[ii], numBins );
unsigned int minSepBins = (unsigned int)round( ap.minPeakSep / (ap.frameDt * ap.binFrames) );
if ( minSepBins < 1 ) minSepBins = 1;
// Dip threshold: mean + dipSdev * sdev of the mean trace.
double dipThresh;
{
double sum = 0.0, sumSq = 0.0;
for ( auto v : cs.meanTrace ) { sum += v; sumSq += v * v; }
double mn = sum / numBins;
double sd = sqrt( sumSq / numBins - mn * mn );
dipThresh = mn + ap.dipSdev * sd;
}
// Availability mask: bins set to false are inside a previously accepted
// peak window and are excluded from future searches.
vector< bool > available( numBins, true );
bool firstPeak = true;
while ( true ) {
// Find the highest available bin in the original mean trace.
unsigned int pkBin = numBins; // sentinel
double pkVal = -1e30;
for ( unsigned int bb = 0; bb < numBins; bb++ ) {
if ( available[bb] && cs.meanTrace[bb] > pkVal ) {
pkVal = cs.meanTrace[bb];
pkBin = bb;
}
}
if ( pkBin == numBins || pkVal <= 0.0 ) break;
// Adjacent bin for two-bin significance test.
unsigned int adjBin;
if ( pkBin == 0 ) adjBin = 1;
else if ( pkBin == numBins - 1 ) adjBin = numBins - 2;
else adjBin = (cs.meanTrace[pkBin-1] > cs.meanTrace[pkBin+1]) ? pkBin-1 : pkBin+1;
unsigned int numOK = 0;
for ( unsigned int ii = 0; ii < ap.numShuffle; ii++ )
numOK += ( cs.meanTrace[pkBin] > shuffMeans[ii][pkBin] ) &&
( cs.meanTrace[adjBin] > shuffMeans[ii][adjBin] );
if ( (numOK * 100) <= (99 * ap.numShuffle) ) break;
// Dip check: for 2nd+ peaks the trace must drop below dipThresh for
// at least dipFrames consecutive bins between this and the nearest accepted peak.
unsigned int lo = (pkBin >= minSepBins) ? pkBin - minSepBins : 0;
unsigned int hi = min( pkBin + minSepBins, numBins - 1 );
if ( !firstPeak ) {
unsigned int nearest = cs.allPkIndices[0];
unsigned int minDist = (pkBin > nearest) ? pkBin - nearest : nearest - pkBin;
for ( auto prevIdx : cs.allPkIndices ) {
unsigned int d = (pkBin > prevIdx) ? pkBin - prevIdx : prevIdx - pkBin;
if ( d < minDist ) { minDist = d; nearest = prevIdx; }
}
if ( !hasDipBetween( cs.meanTrace, nearest, pkBin, dipThresh, ap.dipFrames ) ) {
for ( unsigned int bb = lo; bb <= hi; bb++ ) available[bb] = false;
continue;
}
}
double pval = 1.0 - (double)numOK / ap.numShuffle;
cs.allPkIndices.push_back( pkBin );
cs.allPkScores.push_back( cs.meanTrace[pkBin] );
cs.allPkPvalues.push_back( pval );
if ( firstPeak ) {
cs.meanPkIdx = pkBin;
cs.meanScore = cs.meanTrace[pkBin];
cs.sigMean = true;
firstPeak = false;
}
// Mark the separation window as unavailable.
for ( unsigned int bb = lo; bb <= hi; bb++ )
available[bb] = false;
}
if ( firstPeak ) {
cs.meanPkIdx = (unsigned int)(
max_element( cs.meanTrace.begin(), cs.meanTrace.end() ) - cs.meanTrace.begin() );
cs.meanScore = cs.meanTrace[cs.meanPkIdx];
cs.sigMean = false;
}
}
/// args: data[ frame# ][ trial# ][ cell# ]
/// Pass-back arg: ret[ cell# ][ trial# ][ frame# ]
/// Fills in only the data in the window around the stimuli, from
/// CS_ONSET_FRAME - CIRC_PAD to US_ONSET_FRAME + CIRC_PAD.
void reorderData( const double* data, unsigned int numCells, unsigned int numTrials, unsigned int numFrames, vector< vector< vector< double >>>& ret, const AnalysisParams& ap )
{
ret.clear();
ret.resize( numCells );
unsigned int ncxnt = numCells * numTrials;
unsigned int nf = ap.circShuffleFrames;
for ( unsigned int cell = 0; cell < numCells; cell++ ) {
vector< vector< double > >& rc = ret[cell];
rc.resize( numTrials );
for( unsigned int tt = 0; tt < numTrials; tt++ ) {
vector< double >& rct = rc[ tt ];
rct.resize( nf, 0.0 );
for( unsigned int ff = 0; ff < nf; ++ff ) {
rct[ff] = data[ cell + tt * numCells + (ff + ap.csOnsetFrame - ap.circPad) * ncxnt];
}
}
}
}
// Returns threshold for sig peak for data[trial#][frame#]. The
// data has already been sub-selected for a given cell.
double findCellThresh(
const vector< vector< double > >& data, unsigned int numFrames, double transientThresh )
{
double sum = 0.0;
double sq = 0.0;
for( auto trialD = data.begin(); trialD != data.end(); ++trialD ) {
for( auto frameD = trialD->begin(); frameD != trialD->end(); ++frameD ) {
sum += *frameD;
sq += *frameD * *frameD;
}
}
double numSamples = numFrames * data.size(); //numFrames * numTrials
double mean = sum/numSamples;
return mean + transientThresh * sqrt( sq / numSamples - mean*mean );
}
/// Returns time stamps of transients trial# ][ transient# ].
// given the reordered dfbf data[ trial# ][ frame# ]
// A given trial may have zero or more transients.
vector< vector< unsigned int > > findTransients(
const vector< vector< double > >& data, double cellThresh, unsigned int numFrames )
{
unsigned int numTrials = data.size();
vector< vector< unsigned int > > ret ( numTrials );
vector< vector< unsigned int > >::iterator trialT = ret.begin();
for( auto trialD = data.begin(); trialD != data.end(); ++trialD, ++trialT ) {
trialT->clear();
double lastFrame = 0.0;
bool refractory = 0;
for( unsigned int ii = 0; ii < numFrames; ++ii ) {
double frameD = (*trialD)[ii];
// Don't permit another transient till signal goes < cellThresh
if (frameD > cellThresh && frameD > lastFrame && !refractory) {
trialT->push_back( ii );
refractory = 1;
} else {
refractory = (frameD > cellThresh);
}
lastFrame = frameD;
}
}
return ret;
}
// Returns transient rates for the current
double findAveTransientRate( const vector< vector< unsigned int > >& transients, double trialDuration, unsigned int numTrials )
{
double ret = 0.0;
for( auto trial: transients ) {
for ( auto bin: trial ) {
ret += bin;
}
}
return ret/ ( trialDuration * numTrials );
}
/// Returns temporal information for the current cell.
double findTemporalInformation( const vector< vector< unsigned int > >& transients, double aveTransientRate, const vector< unsigned int >& shuff, unsigned int numFrames )
{
double ret = 0.0;
unsigned int numTrials = transients.size();
vector< double > binAve( numFrames, 0.0 );
for( unsigned int trialIdx = 0; trialIdx < numTrials; ++trialIdx ) {
for ( unsigned int bin: transients[trialIdx] ) {
unsigned int b = ( bin + shuff[trialIdx] ) % numFrames;
binAve[b] += 1.0;
}
}
for ( auto bb: binAve ) {
if ( bb > 0.0 ) {
ret += bb * log2( bb / aveTransientRate );
}
}
if ( aveTransientRate > 0.0 )
return ret / ( aveTransientRate * numFrames );
return 0.0;
}
// Returns the specified percentile value of shuffled TIs, for current cell
double percentileTI( const vector< vector< unsigned int > >& transients, double aveTransientRate, const AnalysisParams& ap, double percentile )
{
std::random_device dev;
// std::mt19937 rng(dev());
std::mt19937 rng( 1234 );
std::uniform_int_distribution<std::mt19937::result_type> shuffler(0, ap.circShuffleFrames );
unsigned int numTrials = transients.size();
assert( numTrials > 0 );
// allocate tiRank[iter#], for sorting.
vector< double > tiRank( ap.numShuffle );
for ( unsigned int iter = 0; iter < ap.numShuffle; ++iter ) {
vector< unsigned int > shuff( numTrials );
for ( unsigned int tt = 0; tt < numTrials; ++tt ) {
shuff[tt] = shuffler( rng );
}
tiRank[iter] = findTemporalInformation( transients, aveTransientRate, shuff, ap.circShuffleFrames );
}
unsigned int percIdx = (percentile * ap.numShuffle ) / 100;
sort( tiRank.begin(), tiRank.end() );
return tiRank[percIdx];
}
// Fraction of trials in which there was >= 1 transient. frac[ cell# ]
double fracTrialsFired( const vector< vector< unsigned int > >& transients )
{
assert( transients.size() > 0 );
double ret = 0.0;
for( auto tt : transients ) {
ret += (tt.size() > 0 );
}
return ret / transients.size();
}
/// Computes TI score for a given neuron. zero means it fails a criterion.
// Should really return a lot of stuff: the mean bin vector, the
// baseTI, the PTI, the frac fired, the percentile bin height.
// Suggest return a 2-D vector or a small struct.
CellScore cellTIscore( const vector< vector< double > >& data, const AnalysisParams& ap, const TiAnalysisParams& tip )
{
CellScore cs;
unsigned int numTrials = data.size();
assert( numTrials > 0 );
unsigned int numFrames = data[0].size();
vector< unsigned int > nonShuff( numTrials, 0 );
vector< double > aveOfTrials_ = aveOfTrials( data, nonShuff, ap.circShuffleFrames );
double cellThresh = findCellThresh( data, ap.circShuffleFrames, tip.transientThresh );
vector< vector< unsigned int > > transients =
findTransients( data, cellThresh, ap.circShuffleFrames );
double trialDuration = tip.frameDt * ap.circShuffleFrames;
double aveTransientRate = findAveTransientRate( transients, trialDuration, numTrials );
cs.baseScore = findTemporalInformation( transients, aveTransientRate, nonShuff, ap.circShuffleFrames );
cs.percentileScore = percentileTI( transients, aveTransientRate, ap, tip.tiPercentile );
cs.fracTrialsFired = fracTrialsFired( transients );
// Fill up the RNGs
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> shuffler(0, numFrames / ap.binFrames );
vector< vector< unsigned int > > binShuff( ap.numShuffle );
for ( unsigned int ii = 0; ii < ap.numShuffle; ii++ ) {
binShuff[ii].resize( numTrials );
for ( unsigned int jj = 0; jj < numTrials; jj++ ) {
binShuff[ii][jj] = shuffler( rng );
}
}
tuningCurve( data, binShuff, numFrames, ap, cs );
cs.sigBootstrap = ( cs.baseScore > cs.percentileScore ) && (cs.fracTrialsFired > tip.fracTrialsFiredThresh );
return cs;
}
// Returns the reliability index.
vector< CellScore > tiScore( py::array_t<double> xs, const AnalysisParams& ap, const TiAnalysisParams& tip )
{
py::buffer_info info = xs.request();
auto data = static_cast< double* >( info.ptr);
// unsigned int numDat = info.itemsize;
vector< vector< vector< double >>> reorderedData;
reorderData( data, ap.numCells, ap.numTrials, ap.numFrames, reorderedData, ap );
vector< CellScore > ret( ap.numCells );
for( unsigned int cellIdx = 0; cellIdx < ap.numCells; cellIdx++ ) {
ret[cellIdx] = cellTIscore( reorderedData[cellIdx], ap, tip );
}
return ret;
}