-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariableWindowAnalysisFunc.py
268 lines (201 loc) · 11.2 KB
/
VariableWindowAnalysisFunc.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 16 14:31:49 2019
@author: thugwithyoyo
"""
import numpy as np
from PeriEventTraceFuncLib import *
from collections import defaultdict
# Paths to data in JSON formatted files
PathToBehavFile = '/home/thugwithyoyo/CaTransDecoding/CalciumImagingData/2018-12-10-11-37-56_unique_B.json'
PathToFluorFile = '/home/thugwithyoyo/CaTransDecoding/CalciumImagingData/2018-12-10-11-37-56_unique_C.json'
#PathToBehavFile = '/home/thugwithyoyo/CaTransDecoding/CalciumImagingData/2018-12-14-11-01-41_B.json'
#PathToFluorFile = '/home/thugwithyoyo/CaTransDecoding/CalciumImagingData/2018-12-14-11-01-41_C.json'
SavePath = '/home/thugwithyoyo/CaTransDecoding/Output/2018-12-10-11-37-56_unique_AccumulatingWindow'
# Define ParamsDict, the dictionary that contains the parameters for
# PLS decoding, Bootstrapping, Shuffle control processes.
# Peripheral target entry events
ParamsDict = defaultdict(dict)
ParamsDict['RefEventsList'] = ['M6T0_Entry_ts', 'M6T1_Entry_ts']
# Scalar values assigned to event types listed above.
ParamsDict['AssignedEventVals'] = [-1, 1]
# Set parameters for peri-event extraction
ParamsDict['BoundaryWindow'] = [-1., 2.]
ParamsDict['StepWidth'] = 0.1
ParamsDict['WindowWidth'] = 0.4
# Set parameters for PLS
ParamsDict['NumLatents'] = 5
# Set parameters for Monte Carlo estimation of confidence intervals
ParamsDict['NumRepetitions'] = 5
ParamsDict['ConfLevel'] = 0.95
# Specified anti-tolerance window, relative to target entry, for detecting and
# removing repeat entries that followed shortly after the initial entry.
ParamsDict['RelativeTolWindow'] = (0.0001, 2.5)
def VariableWindowAnalysisFunc(PathToBehavFile, PathToFluorFile, SavePath, ParamsDict):
# Pack into a dict the two lists above that specify info for reference
# events
RefEventsDict = {'RefEventsList' : ParamsDict['RefEventsList'],
'AssignedEventVals' : ParamsDict['AssignedEventVals']}
ArrayOfWindows_fw = CumulativeWindowGen(ParamsDict['BoundaryWindow'],
ParamsDict['StepWidth'],
'positive')
ArrayOfWindows_bw = CumulativeWindowGen(ParamsDict['BoundaryWindow'],
ParamsDict['StepWidth'],
'negative')
# Generate the unfiltered behavior dictionary.
BehavDict = BehavDictGen(PathToBehavFile)
# Detect rapid repeats within each event list.
EventFilters = RemoveRepeatTargetEntries(BehavDict,
RefEventsDict['RefEventsList'],
ParamsDict['RelativeTolWindow'])
# Remove repeat events
for ef in EventFilters:
BehavDict[ef] = BehavDict[ef][EventFilters[ef]]
# Generate the data frame of calcium transients.
CellFluorTraces_Frame = CellFluorTraces_FrameGen(PathToFluorFile)
# Grow window forwards from floor
(NumDomains_fw, _) = ArrayOfWindows_fw.shape
# Initialize an empty array to contain output dictionaries from the
# decoder cross-validation perfomance and monte carlo bootstrap routines
Performance_fw = np.empty((NumDomains_fw,), dtype=dict)
ConfInts_fw = np.empty((NumDomains_fw,), dtype=dict)
EventsShuffled_fw = np.empty((NumDomains_fw,), dtype=dict)
for i in np.arange(0, NumDomains_fw):
PeriEventExtractorDict = PeriEventExtractor_Trace(BehavDict,
CellFluorTraces_Frame, RefEventsDict,
ArrayOfWindows_fw[i])
# Generate a set of indices to test the inclusion portion of the performance code.
PEA_Array = PeriEventExtractorDict['PEA_Array']
(NumTotalTrials, NumTotalFeatures) = PEA_Array.shape
InclusionSet = np.random.randint(0, high=NumTotalTrials, size=(NumTotalTrials,))
Performance_fw[i] = PLS_DecoderPerformance(PeriEventExtractorDict,
ParamsDict['NumLatents'])
Performance_fw[i].update({'PeriEventDomain': ArrayOfWindows_fw[i]})
ConfInts_fw[i] = PLS_MonteCarlo(PeriEventExtractorDict,
ParamsDict['NumLatents'],
ParamsDict['NumRepetitions'],
ParamsDict['ConfLevel'])
ConfInts_fw[i].update({'PeriEventDomain': ArrayOfWindows_fw[i]})
EventsShuffled_fw[i] = PLS_Shuffle(PeriEventExtractorDict,
ParamsDict['NumLatents'],
ParamsDict['NumRepetitions'],
ParamsDict['ConfLevel'])
EventsShuffled_fw[i].update({'PeriEventDomain': ArrayOfWindows_fw[i]})
# Grow window backwards from ceiling
(NumDomains_bw, _) = ArrayOfWindows_bw.shape
# Initialize an empty array to contain output dictionaries from the
# decoder cross-validation routine.
Performance_bw = np.empty((NumDomains_bw,), dtype=dict)
ConfInts_bw = np.empty((NumDomains_bw,), dtype=dict)
EventsShuffled_bw = np.empty((NumDomains_bw,), dtype=dict)
for i in np.arange(0, NumDomains_bw):
PeriEventExtractorDict = PeriEventExtractor_Trace(BehavDict,
CellFluorTraces_Frame, RefEventsDict,
ArrayOfWindows_bw[i])
PeriEventExtractorDict.update({'PeriEventDomain': ArrayOfWindows_bw[i]})
# Generate a set of indices to test the inclusion portion of the performance code.
PEA_Array = PeriEventExtractorDict['PEA_Array']
(NumTotalTrials, NumTotalFeatures) = PEA_Array.shape
InclusionSet = np.random.randint(0, high=NumTotalTrials, size=(NumTotalTrials,))
Performance_bw[i] = PLS_DecoderPerformance(PeriEventExtractorDict,
ParamsDict['NumLatents'])
Performance_bw[i].update({'PeriEventDomain': ArrayOfWindows_bw[i]})
ConfInts_bw[i] = PLS_MonteCarlo(PeriEventExtractorDict,
ParamsDict['NumLatents'],
ParamsDict['NumRepetitions'],
ParamsDict['ConfLevel'])
ConfInts_bw[i].update({'PeriEventDomain': ArrayOfWindows_bw[i]})
EventsShuffled_bw[i] = PLS_Shuffle(PeriEventExtractorDict,
ParamsDict['NumLatents'],
ParamsDict['NumRepetitions'],
ParamsDict['ConfLevel'])
EventsShuffled_bw[i].update({'PeriEventDomain': ArrayOfWindows_bw[i]})
#####################################
######## Start shelving #########
#####################################
# Assemble save path
# get root directory of save path from path to calcium data
# SavePath is an arguement above. The following script requires it
exec(open('./ShelveWorkspaceScript.py').read())
VariableWindowAnalysisFunc(PathToBehavFile, PathToFluorFile, SavePath, ParamsDict)
RestoreFilePath = SavePath+'.dat'
exec(open('./RestoreShelvedWorkspaceScript.py').read())
#### Plot outcome measures #####
# Specify outcome measures to be plotted
PerformancePlotSpecDict = {'measure': 'performance',
'measure_median': 'performance_median',
'measure_CLs': 'performance_CLs',
'color':'blue'}
ShuffledPerformancePlotSpecDict = {'measure': 'performance_median',
'measure_median': 'performance_median',
'measure_CLs': 'performance_CLs',
'color':'lightblue'}
MutInfoPlotSpecDict = {'measure': 'mutual_info',
'measure_median': 'mutual_info_median',
'measure_CLs': 'mutual_info_CLs',
'color':'blue'}
ShuffledMutInfoPlotSpecDict = {'measure': 'mutual_info_median',
'measure_median': 'mutual_info_median',
'measure_CLs': 'mutual_info_CLs',
'color':'lightblue'}
# Plot performance dependence on increasing peri-event window span
fig1, axs1 = plt.subplots()
#fig1.suptitle(PerformancePlotSpecDict['measure'])
# Plot forward-going accumulation performance
PlotSpecDict = {'measure': 'performance',
'measure_median': 'performance_median',
'measure_CLs': 'performance_CLs',
'color':'blue'}
GenerateConfIntsPlot(ConfInts_fw, Performance_fw, PlotSpecDict,
axs1, 'fw')
PlotSpecDict = {'measure': 'performance_median',
'measure_median': 'performance_median',
'measure_CLs': 'performance_CLs',
'color':'lightblue'}
GenerateConfIntsPlot(EventsShuffled_fw, EventsShuffled_fw, PlotSpecDict,
axs1, 'fw')
# Plot backward-going accumulation performance
PlotSpecDict = {'measure': 'performance',
'measure_median': 'performance_median',
'measure_CLs': 'performance_CLs',
'color':'green'}
GenerateConfIntsPlot(ConfInts_bw, Performance_bw, PlotSpecDict,
axs1, 'bw')
PlotSpecDict = {'measure': 'performance_median',
'measure_median': 'performance_median',
'measure_CLs': 'performance_CLs',
'color':'lightgreen'}
GenerateConfIntsPlot(EventsShuffled_bw, EventsShuffled_bw, PlotSpecDict,
axs1, 'bw')
axs1.set_xbound(lower=ParamsDict['BoundaryWindow'][0], upper=ParamsDict['BoundaryWindow'][1])
axs1.set_ybound(lower=0.4, upper=1.)
# Plot mutual information dependence on increasing peri-event window span
fig2, axs2 = plt.subplots()
#fig2.suptitle(MutInfoPlotSpecDict['measure'])
PlotSpecDict = {'measure': 'mutual_info',
'measure_median': 'mutual_info_median',
'measure_CLs': 'mutual_info_CLs',
'color':'blue'}
GenerateConfIntsPlot(ConfInts_fw, Performance_fw, PlotSpecDict,
axs2, 'fw')
PlotSpecDict = {'measure': 'mutual_info_median',
'measure_median': 'mutual_info_median',
'measure_CLs': 'mutual_info_CLs',
'color':'lightblue'}
GenerateConfIntsPlot(EventsShuffled_fw, EventsShuffled_fw, PlotSpecDict,
axs2, 'fw')
PlotSpecDict = {'measure': 'mutual_info',
'measure_median': 'mutual_info_median',
'measure_CLs': 'mutual_info_CLs',
'color':'green'}
GenerateConfIntsPlot(ConfInts_bw, Performance_bw, PlotSpecDict,
axs2, 'bw')
PlotSpecDict = {'measure': 'mutual_info_median',
'measure_median': 'mutual_info_median',
'measure_CLs': 'mutual_info_CLs',
'color':'lightgreen'}
GenerateConfIntsPlot(EventsShuffled_bw, EventsShuffled_bw, PlotSpecDict,
axs2, 'bw')
axs2.set_xbound(lower=ParamsDict['BoundaryWindow'][0], upper=ParamsDict['BoundaryWindow'][1])
axs2.set_ybound(lower=0., upper=1.)