-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalciumImagingBehaviorAnalysis.py
More file actions
428 lines (361 loc) · 18.4 KB
/
CalciumImagingBehaviorAnalysis.py
File metadata and controls
428 lines (361 loc) · 18.4 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
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
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 28 13:33:38 2019
@author: Ryan Eaton
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
import json
import collections
def CinePlexFromMatJSON_parser(PathToBehavFile):
ScriptDir = os.getcwd()
with open(PathToBehavFile) as f:
BehavDataDict = json.load(f)
BehaviorTraces_Frame = pd.DataFrame.from_dict(BehavDataDict,
orient='columns')
# Generate an index list for later referencing
Indices = np.arange(0, BehaviorTraces_Frame.index.size)
# Define a dictionary of key name pairs for referencing columns in the dataframe.
#
# ### Description of nomenclature. ###
# Here "M6" and "M7" stand for Marker 6 (right hand) and Marker 7 (left hand)
# respectively. "T0", "T1" and "CT" stand for Target 0 (animal's right),
# Target 1 (animal's left) and Center Target respectively. Tags "in" and "out"
# refer to when the relevant marker is present or absent respectively within
# the relevant target. Finally, "CumLen" stands for cumulative length in the
# present target.
# ColumnNames = {
# 'Timestamp':'Timestamp',
# 'FrameNumber':'Frame_Number',
# 'M6_xCoord':'X1_1_pix',
# 'M6_yCoord':'Y1_1_pix',
# 'M7_xCoord':'X2_1_pix',
# 'M7_yCoord':'Y2_1_pix',
# 'M6T0_in':'EV1_1',
# 'M6T0_in_CumLen':'EV1_1_Track_Length_pix',
# 'M6T0_out':'EV1_2',
# 'M6T0_out_CumLen':'EV1_2_Track_Length_pix',
# 'M6T1_in':'EV1_3',
# 'M6T1_in_CumLen':'EV1_3_Track_Length_pix',
# 'M6T1_out':'EV1_4',
# 'M6T1_out_CumLen':'EV1_4_Track_Length_pix',
# 'M6CT_in':'EV1_5',
# 'M6CT_in_CumLen':'EV1_5_Track_Length_pix',
# 'M6CT_out':'EV1_6',
# 'M6CT_out_CumLen':'EV1_6_Track_Length_pix',
# 'M7T0_in':'EV1_7',
# 'M7T0_in_CumLen':'EV1_7_Track_Length_pix',
# 'M7T0_out':'EV1_8',
# 'M7T0_out_CumLen':'EV1_8_Track_Length_pix',
# 'M7T1_in':'EV1_9',
# 'M7T1_in_CumLen':'EV1_9_Track_Length_pix',
# 'M7T1_out':'EV1_10',
# 'M7T1_out_CumLen':'EV1_10_Track_Length_pix',
# 'M7CT_in':'EV1_11',
# 'M7CT_in_CumLen':'EV1_11_Track_Length_pix',
# 'M7CT_out':'EV1_12',
# 'M7CT_out_CumLen':'EV1_12_Track_Length_pix'
# }
# Samantha's New Column name style
ColumnNames = {
'Timestamp':'Timestamp',
'FrameNumber':'Frame_number',
'M6_xCoord':'RH_x_pos',
'M6_yCoord':'RH_y_pos',
'M7_xCoord':'LH_x_pos',
'M7_yCoord':'LH_y_pos',
# 'M6T0_in':'RH_zone1',
'M6T0_in':'RH_zone1_reachentry',
'M6T0_in_CumLen':'EV1_1_Track_Length_pix',
'M6T0_out':'EV1_2',
'M6T0_out_CumLen':'EV1_2_Track_Length_pix',
# 'M6T1_in':'RH_zone2',
'M6T1_in':'RH_zone2_reachentry',
'M6T1_in_CumLen':'EV1_3_Track_Length_pix',
'M6T1_out':'EV1_4',
'M6T1_out_CumLen':'EV1_4_Track_Length_pix',
# 'M6CT_in':'RH_homezone',
'M6CT_in':'RH_homezone_reachentry',
'M6CT_in_CumLen':'EV1_5_Track_Length_pix',
'M6CT_out':'EV1_6',
'M6CT_out_CumLen':'EV1_6_Track_Length_pix',
# 'M7T0_in':'LH_zone1',
'M7T0_in':'LH_zone1_reachentry',
'M7T0_in_CumLen':'EV1_7_Track_Length_pix',
'M7T0_out':'EV1_8',
'M7T0_out_CumLen':'EV1_8_Track_Length_pix',
# 'M7T1_in':'LH_zone2',
'M7T1_in':'LH_zone2_reachentry',
'M7T1_in_CumLen':'EV1_9_Track_Length_pix',
'M7T1_out':'EV1_10',
'M7T1_out_CumLen':'EV1_10_Track_Length_pix',
# 'M7CT_in':'LH_homezone',
'M7CT_in':'LH_homezone_reachentry',
'M7CT_in_CumLen':'EV1_11_Track_Length_pix',
'M7CT_out':'EV1_12',
'M7CT_out_CumLen':'EV1_12_Track_Length_pix'
}
# Assuming that the imported data has already been corrected for
# pause delay.
# Extract Marker 6 T0 entry events
Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['M6T0_in']])== 1))
M6T0_Entry_ind = Indices[Filt]
M6T0_Entry_ts = BehaviorTraces_Frame[ColumnNames['Timestamp']][M6T0_Entry_ind]
# Extract Marker 6 T0 exit events
Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['M6T0_in']])== -1))
M6T0_Exit_ind = Indices[Filt]
M6T0_Exit_ts = BehaviorTraces_Frame[ColumnNames['Timestamp']][M6T0_Exit_ind]
# Extract Marker 6 T1 entry events
Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['M6T1_in']])== 1))
M6T1_Entry_ind = Indices[Filt]
M6T1_Entry_ts = BehaviorTraces_Frame[ColumnNames['Timestamp']][M6T1_Entry_ind]
# Extract Marker 6 T1 exit events
Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['M6T1_in']])== -1))
M6T1_Exit_ind = Indices[Filt]
M6T1_Exit_ts = BehaviorTraces_Frame[ColumnNames['Timestamp']][M6T1_Exit_ind]
# Extract Marker 6 CT exit events
#Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['M6CT_out']])== 1))
#M6CT_Exit_ind = Indices[Filt]
#M6CT_Exit_ts = BehaviorTraces_Frame[ColumnNames['Timestamp']][M6CT_Exit_ind]
# Extract Marker 7 T0 entry events
Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['M7T0_in']])== 1))
M7T0_Entry_ind = Indices[Filt]
M7T0_Entry_ts = BehaviorTraces_Frame[ColumnNames['Timestamp']][M7T0_Entry_ind]
# Extract Marker 7 T0 exit events
Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['M7T0_in']])== -1))
M7T0_Exit_ind = Indices[Filt]
M7T0_Exit_ts = BehaviorTraces_Frame[ColumnNames['Timestamp']][M7T0_Exit_ind]
# Extract Marker 7 T1 entry events
Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['M7T1_in']])== 1))
M7T1_Entry_ind = Indices[Filt]
M7T1_Entry_ts = BehaviorTraces_Frame[ColumnNames['Timestamp']][M7T1_Entry_ind]
# Extract Marker 7 T1 entry events
Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['M7T1_in']])== -1))
M7T1_Exit_ind = Indices[Filt]
M7T1_Exit_ts = BehaviorTraces_Frame[ColumnNames['Timestamp']][M7T1_Exit_ind]
# Extract Marker 7 CT exit events
#Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['M7CT_out']])== 1))
#M7CT_Exit_ind = Indices[Filt]
#M7CT_Exit_ts = BehaviorTraces_Frame[ColumnNames['Timestamp']][M7CT_Exit_ind]
return {
'M6T0_Entry_ind' : M6T0_Entry_ind,
'M6T0_Entry_ts': M6T0_Entry_ts,
'M6T0_Exit_ind' : M6T0_Exit_ind,
'M6T0_Exit_ts' : M6T0_Exit_ts,
'M6T1_Entry_ind' : M6T1_Entry_ind,
'M6T1_Entry_ts' : M6T1_Entry_ts,
'M6T1_Exit_ind' : M6T1_Exit_ind,
'M6T1_Exit_ts' : M6T1_Exit_ts,
#'M6CT_Exit_ind' : M6CT_Exit_ind,
#'M6CT_Exit_ts' : M6CT_Exit_ts,
'M7T0_Entry_ind' : M7T0_Entry_ind,
'M7T0_Entry_ts' : M7T0_Entry_ts,
'M7T0_Exit_ind' : M7T0_Exit_ind,
'M7T0_Exit_ts' : M7T0_Exit_ts,
'M7T1_Entry_ind' : M7T1_Entry_ind,
'M7T1_Entry_ts' : M7T1_Entry_ts,
'M7T1_Exit_ind' : M7T1_Exit_ind,
'M7T1_Exit_ts' : M7T1_Exit_ts,
#'M7CT_Exit_ind' : M7CT_Exit_ind,
#'M7CT_Exit_ts' : M7CT_Exit_ts
}
def CinePlexCSV_parser(PathToFile):
# Function takes a CSV file of present/absent events, converts changes
# of these states into timestamps, and returns each of the relevant
# lists of entry and exit events in a dictionary for subsequent
# processing.
# Aquire path of the directory that contains this script. This is so
# the routine can navigate back after changing directories if need be.
ScriptDir = os.getcwd()
#PathToFile = ''
BehaviorTraces_Frame = pd.read_csv(PathToFile, header=0)
# try:
#
# BehaviorTraces_Frame = pd.read_csv(PathToFile, header=0)
#
# except:
#
# print('File: ' + PathToFile + 'generated a pd.read_csv error...\n')
#
# return BehaviorTraces_Frame
# Generate an index list for later referencing
Indices = np.arange(0, BehaviorTraces_Frame['#'].size)
# Define a dictionary of key name pairs for referencing columns in the dataframe.
#
# ### Description of nomenclature. ###
# Here "M6" and "M7" stand for Marker 6 (right hand) and Marker 7 (left hand)
# respectively. "T0", "T1" and "CT" stand for Target 0 (animal's right),
# Target 1 (animal's left) and Center Target respectively. Tags "in" and "out"
# refer to when the relevant marker is present or absent respectively within
# the relevant target. Finally, "CumLen" stands for cumulative length in the
# present target.
ColumnNames = {'IndexNumber':'#',
'Timestamp':'Timestamp',
'FrameNumber':'Frame_Number',
'M6_xCoord':'X1.1_pix',
'M6_yCoord':'Y1.1_pix',
'M7_xCoord':'X2.1_pix',
'M7_yCoord':'Y2.1_pix',
'M6T0_in':'EV1.1',
'M6T0_in_CumLen':'EV1.1_Track_Length_pix',
'M6T0_out':'EV1.2',
'M6T0_out_CumLen':'EV1.2_Track_Length_pix',
'M6T1_in':'EV1.3',
'M6T1_in_CumLen':'EV1.3_Track_Length_pix',
'M6T1_out':'EV1.4',
'M6T1_out_CumLen':'EV1.4_Track_Length_pix',
'M6CT_in':'EV1.5',
'M6CT_in_CumLen':'EV1.5_Track_Length_pix',
'M6CT_out':'EV1.6',
'M6CT_out_CumLen':'EV1.6_Track_Length_pix',
'M7T0_in':'EV1.7',
'M7T0_in_CumLen':'EV1.7_Track_Length_pix',
'M7T0_out':'EV1.8',
'M7T0_out_CumLen':'EV1.8_Track_Length_pix',
'M7T1_in':'EV1.9',
'M7T1_in_CumLen':'EV1.9_Track_Length_pix',
'M7T1_out':'EV1.10',
'M7T1_out_CumLen':'EV1.10_Track_Length_pix',
'M7CT_in':'EV1.11',
'M7CT_in_CumLen':'EV1.11_Track_Length_pix',
'M7CT_out':'EV1.12',
'M7CT_out_CumLen':'EV1.12_Track_Length_pix'}
# Identify "pre-pause" delay in timestamp record
Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['Timestamp']].values) >
1.1*(BehaviorTraces_Frame[ColumnNames['Timestamp']].values[-1] -
BehaviorTraces_Frame[ColumnNames['Timestamp']].values[-2])))
if np.sum(Filt) > 0:
StartIndex = Indices[Filt][0]
elif np.sum(Filt) == 0:
StartIndex = 0
# Subtract out pre-pause delay on startup
BehaviorTraces_Frame[ColumnNames['Timestamp']] = \
BehaviorTraces_Frame[ColumnNames['Timestamp']].values - \
BehaviorTraces_Frame[ColumnNames['Timestamp']].values[StartIndex]
# Remove rows from dataframe that correspond to the in-pause time.
RowsToKeepFilt = (BehaviorTraces_Frame[ColumnNames['Timestamp']].values >= 0)
BehaviorTraces_Frame = BehaviorTraces_Frame.iloc[RowsToKeepFilt,:]
# Generate a list of indices for the resulting filtered table.
Indices = np.arange(0, BehaviorTraces_Frame['#'].size)
# Reset indices of dataframe
BehaviorTraces_Frame.index = Indices
# Extract Marker 6 T0 entry events
Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['M6T0_in']])== 1))
M6T0_Entry_ind = Indices[Filt]
M6T0_Entry_ts = BehaviorTraces_Frame[ColumnNames['Timestamp']][M6T0_Entry_ind]
# Extract Marker 6 T0 exit events
Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['M6T0_out']])== 1))
M6T0_Exit_ind = Indices[Filt]
M6T0_Exit_ts = BehaviorTraces_Frame[ColumnNames['Timestamp']][M6T0_Exit_ind]
# Extract Marker 6 T1 entry events
Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['M6T1_in']])== 1))
M6T1_Entry_ind = Indices[Filt]
M6T1_Entry_ts = BehaviorTraces_Frame[ColumnNames['Timestamp']][M6T1_Entry_ind]
# Extract Marker 6 T1 exit events
Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['M6T1_out']])== 1))
M6T1_Exit_ind = Indices[Filt]
M6T1_Exit_ts = BehaviorTraces_Frame[ColumnNames['Timestamp']][M6T1_Exit_ind]
# Extract Marker 6 CT entry events
Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['M6CT_in']])== 1))
M6CT_Entry_ind = Indices[Filt]
M6CT_Entry_ts = BehaviorTraces_Frame[ColumnNames['Timestamp']][M6CT_Entry_ind]
# Extract Marker 6 CT exit events
Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['M6CT_out']])== 1))
M6CT_Exit_ind = Indices[Filt]
M6CT_Exit_ts = BehaviorTraces_Frame[ColumnNames['Timestamp']][M6CT_Exit_ind]
# Extract Marker 7 T0 entry events
Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['M7T0_in']])== 1))
M7T0_Entry_ind = Indices[Filt]
M7T0_Entry_ts = BehaviorTraces_Frame[ColumnNames['Timestamp']][M7T0_Entry_ind]
# Extract Marker 7 T0 exit events
Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['M7T0_out']])== 1))
M7T0_Exit_ind = Indices[Filt]
M7T0_Exit_ts = BehaviorTraces_Frame[ColumnNames['Timestamp']][M7T0_Exit_ind]
# Extract Marker 7 T1 entry events
Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['M7T1_in']])== 1))
M7T1_Entry_ind = Indices[Filt]
M7T1_Entry_ts = BehaviorTraces_Frame[ColumnNames['Timestamp']][M7T1_Entry_ind]
# Extract Marker 7 T1 exit events
Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['M7T1_out']])== 1))
M7T1_Exit_ind = Indices[Filt]
M7T1_Exit_ts = BehaviorTraces_Frame[ColumnNames['Timestamp']][M7T1_Exit_ind]
# Extract Marker 7 CT entry events
Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['M7CT_in']])== 1))
M7CT_Entry_ind = Indices[Filt]
M7CT_Entry_ts = BehaviorTraces_Frame[ColumnNames['Timestamp']][M7CT_Entry_ind]
# Extract Marker 7 CT exit events
Filt = np.hstack((False, np.diff(BehaviorTraces_Frame[ColumnNames['M7CT_out']])== 1))
M7CT_Exit_ind = Indices[Filt]
M7CT_Exit_ts = BehaviorTraces_Frame[ColumnNames['Timestamp']][M7CT_Exit_ind]
return {# Right hand enters right target
'M6T0_Entry_ind' : M6T0_Entry_ind,
'M6T0_Entry_ts': M6T0_Entry_ts,
# Right hand exits right target
'M6T0_Exit_ind' : M6T0_Exit_ind,
'M6T0_Exit_ts': M6T0_Exit_ts,
# Right hand enters left target
'M6T1_Entry_ind' : M6T1_Entry_ind,
'M6T1_Entry_ts' : M6T1_Entry_ts,
# Right hand exits left target
'M6T1_Exit_ind' : M6T1_Exit_ind,
'M6T1_Exit_ts' : M6T1_Exit_ts,
# Right hand enters center target
'M6CT_Entry_ind' : M6CT_Entry_ind,
'M6CT_Entry_ts' : M6CT_Entry_ts,
# Right hand exits center target
'M6CT_Exit_ind' : M6CT_Exit_ind,
'M6CT_Exit_ts' : M6CT_Exit_ts,
# Left hand enters right target
'M7T0_Entry_ind' : M7T0_Entry_ind,
'M7T0_Entry_ts' : M7T0_Entry_ts,
# Left hand exits right target
'M7T0_Exit_ind' : M7T0_Exit_ind,
'M7T0_Exit_ts' : M7T0_Exit_ts,
# Left hand enters left target
'M7T1_Entry_ind' : M7T1_Entry_ind,
'M7T1_Entry_ts' : M7T1_Entry_ts,
# Left hand exits left target
'M7T1_Exit_ind' : M7T1_Exit_ind,
'M7T1_Exit_ts' : M7T1_Exit_ts,
# Left hand enters center target
'M7CT_Entry_ind' : M7CT_Entry_ind,
'M7CT_Entry_ts' : M7CT_Entry_ts,
# Left hand exits center target
'M7CT_Exit_ind' : M7CT_Exit_ind,
'M7CT_Exit_ts' : M7CT_Exit_ts
}
def EventComparator(tlist1, tlist2, tol_window):
# This tests if each entry t_2 in tlist2 is within the range [t_1 + tol_lo, t_1 + tol_hi)
# for each entry in tlist1. Output is an object array of size
# tlist1.size having each field contain a list of indices that point to all entries
# in tlist2 that lie within the range. Note, for t + tol_lo to precede t, tol_lo must
# be negative-valued.
# Extract low tolerance and high tolerance boundaries from the tol_window tuple.
(tol_lo, tol_hi) = tol_window
# Generate index lists for subsequent iteration and filtering procedures.
tlist2_ind = np.arange(0, tlist2.size)
tlist1_ind = np.arange(0, tlist1.size)
# Pre-allocate array dimensions for output. Note that the datatype is an
# array of dicts.
output = np.empty((tlist1.size,), dtype=dict)
for i in tlist1_ind:
filt = (tlist2 >= tlist1[i] + tol_lo) & (tlist2 < tlist1[i] + tol_hi)
output[i]= {
'within_tol_ts' : tlist2[filt],
'within_tol_ind' :tlist2_ind[filt]
}
return output
def RemoveEventsInTolWindowFiltGen(EventComparatorOutput):
(nEvents,) = EventComparatorOutput.shape
KeepEvent = (np.ones((nEvents,), dtype=int) == 1)
for i in np.arange(0, nEvents):
KeepEvent[EventComparatorOutput[i]['within_tol_ind']] = False
return KeepEvent
def KeepOnlyEventsInTolWindowFiltGen(EventComparatorOutput):
(nEvents,) = EventComparatorOutput.shape
KeepEvent = (np.ones((nEvents,), dtype=int) == 0)
for i in np.arange(0, nEvents):
KeepEvent[EventComparatorOutput[i]['within_tol_ind']] = True
return KeepEvent