/
transientlyImm.py
executable file
·215 lines (192 loc) · 8.24 KB
/
transientlyImm.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
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 25 13:12:36 2018
plots for transiently immobilized animal.
@author: monika
"""
# standard modules
import numpy as np
import matplotlib.pylab as plt
import h5py
# custom modules
import dataHandler as dh
import makePlots as mp
import dimReduction as dr
# multicolor
key = 'BrainScanner20180427_120545_MS'
# transiently immobilized
#key = 'BrainScanner20180329_152141_MS'
# moving
#key = 'BrainScanner20180327_152059_MS/'
folder ='AML70_chip/{}'.format(key)
folder ='OtherDatasets/{}'.format(key)
print folder
# data parameters
dataPars = {'medianWindow':5, # smooth eigenworms with gauss filter of that size, must be odd
'gaussWindow':5, # sgauss window for angle velocity derivative. must be odd
'rotate':True, # rotate Eigenworms using previously calculated rotation matrix
'windowGCamp': 5 # gauss window for red and green channel
}
# analysis parameters
pars ={'nCompPCA':10, # no of PCA components
'PCAtimewarp':True, #timewarp so behaviors are equally represented
'trainingCut': 0.7, # what fraction of data to use for training
'trainingType': 'middle', # simple, random or middle.select random or consecutive data for training. Middle is a testset in the middle
'linReg': 'simple', # ordinary or ransac least squares
'trainingSample': 1, # take only samples that are at least n apart to have independence. 4sec = gcamp_=->24 apart
'useRank': 0, # use the rank transformed version of neural data for all analyses
'useDeconv': 0, # use the rank transformed version of neural data for all analyses
'useClust': 0, # use the clustered neurons transformed version of neural data for all analyses
}
# data dictionary
dataSets = {}
dataSets[key] = dh.loadData(folder, dataPars, ew=1)
keyList = dataSets.keys()
# results dictionary
resultDict = {}
for kindex, key in enumerate(keyList):
resultDict[key] = {}
behaviors = ['AngleVelocity','Eigenworm3']
createIndicesTest = 1#True
overview = 1#False
pca = 1#False
hierclust = False
linreg = False
lasso = 1
elasticnet = 1#True
positionweights = 1#True
resultsPredictionOverview = 1
###############################################
#
# create training and test set indices
#
##############################################
if createIndicesTest:
for kindex, key in enumerate(keyList):
resultDict[key] = {'Training':{}}
for label in behaviors:
train, test = dr.createTrainingTestIndices(dataSets[key], pars, label=label)
resultDict[key]['Training'][label] = {'Train':train }
resultDict[key]['Training'][label]['Test']=test
print "Done generating trainingsets"
###############################################
#
# some generic data checking plots
#
##############################################
if overview:
#mp.plotBehaviorNeuronCorrs(dataSets, keyList, behaviors)
#mp.plotBehaviorOrderedNeurons(dataSets, keyList, behaviors)
#mp.plotVelocityTurns(dataSets, keyList)
mp.plotDataOverview(dataSets, keyList)
#mp.plotNeurons3D(dataSets, keyList, threed = False)
#mp.plotExampleCenterlines(dataSets, keyList, folder)
plt.show()
###############################################
#
# run PCA and store results
#
##############################################
#%%
if pca:
print 'running PCA'
for kindex, key in enumerate(keyList):
resultDict[key]['PCA'] = dr.runPCANormal(dataSets[key], pars, whichPC=0)
# overview of data ordered by PCA
mp.plotDataOverview2(dataSets, keyList, resultDict)
# overview of PCA results and weights
mp.plotPCAresults(dataSets, resultDict, keyList, pars)
plt.show()
mp.plotPCAresults3D(dataSets, resultDict, keyList, pars, col = 'time')
plt.show()
# color by before and after
colorBy = np.zeros(dataSets[key]['Neurons']['Activity'].shape[1])
colorBy[:int(dataSets[key]['Neurons']['Activity'].shape[1]/2.)] = 1
mp.plotPCAresults3D(dataSets, resultDict, keyList, pars, col = 'Immobilization', colorBy = colorBy)
plt.show()
plt.show(block=True)
resultDict['PCA'] = {}
resultDict['PCA2'] = {}
# run PCA on each half
half1 = np.arange(0,1680)
half2 = np.arange(1680,dataSets[key]['Neurons']['Activity'].shape[1])
resultDict[key]['PCAHalf1'] = dr.runPCANormal(dataSets[key], pars, whichPC=0, testset = half1)
resultDict[key]['PCAHalf2'] = dr.runPCANormal(dataSets[key], pars, whichPC=0, testset = half2)
mp. plotPCAresults(dataSets, resultDict, keyList, pars, flag = 'PCAHalf1', testset=half1)
mp. plotPCAresults(dataSets, resultDict, keyList, pars, flag = 'PCAHalf2', testset=half2)
plt.show()
#%%
###############################################
#
# linear regression using LASSO
#
##############################################
if lasso:
print "Performing LASSO.",
for kindex, key in enumerate(keyList):
print key
splits = resultDict[key]['Training']
resultDict[key]['LASSO'] = dr.runLasso(dataSets[key], pars, splits, plot=1, behaviors = behaviors)
# calculate how much more neurons contribute
tmpDict = dr.scoreModelProgression(dataSets[key], resultDict[key],splits, pars, fitmethod = 'LASSO', behaviors = behaviors)
for tmpKey in tmpDict.keys():
resultDict[key]['LASSO'][tmpKey].update(tmpDict[tmpKey])
tmpDict = dr.reorganizeLinModel(dataSets[key], resultDict[key], splits, pars, fitmethod = 'LASSO', behaviors = behaviors)
for tmpKey in tmpDict.keys():
resultDict[key]['LASSO'][tmpKey]=tmpDict[tmpKey]
mp.plotLinearModelResults(dataSets, resultDict, keyList, pars, fitmethod='LASSO', behaviors = behaviors, random = pars['trainingType'])
plt.show()
# overview of LASSO results and weights
#mp.plotPCAresults(dataSets, resultDict, keyList, pars, flag = 'LASSO')
#plt.show()
# plot 3D trajectory of SVM
#mp.plotPCAresults3D(dataSets, resultDict, keyList, pars, col = 'etho', flag = 'LASSO')
plt.show()
#%%
###############################################
#
# linear regression using elastic Net
#
##############################################
if elasticnet:
for kindex, key in enumerate(keyList):
print 'Running Elastic Net', key
splits = resultDict[key]['Training']
resultDict[key]['ElasticNet'] = dr.runElasticNet(dataSets[key], pars,splits, plot=1, behaviors = behaviors)
# calculate how much more neurons contribute
tmpDict = dr.scoreModelProgression(dataSets[key], resultDict[key], splits,pars, fitmethod = 'ElasticNet', behaviors = behaviors, )
for tmpKey in tmpDict.keys():
resultDict[key]['ElasticNet'][tmpKey].update(tmpDict[tmpKey])
mp.plotLinearModelResults(dataSets, resultDict, keyList, pars, fitmethod='ElasticNet', behaviors = behaviors,random = pars['trainingType'])
plt.show()
#%%
###############################################
#
# overlay neuron projections with relevant neurons
#
##############################################
if positionweights:
for kindex, key in enumerate(keyList):
print 'plotting linear model weights on positions', key
mp.plotWeightLocations(dataSets, resultDict, keyList, fitmethod='ElasticNet')
plt.show()
#%%
###############################################
#
# plot the number of neurons and scatter plot of predictions fo velocity and turns
#
##############################################
if resultsPredictionOverview:
fitmethod = 'ElasticNet'
mp.plotLinearModelScatter(dataSets, resultDict, keyList, pars, fitmethod=fitmethod, behaviors = ['AngleVelocity', 'Eigenworm3'], random = 'none')
# collect the relevant number of neurons
noNeur = []
for key in keyList:
noNeur.append([resultDict[key][fitmethod]['AngleVelocity']['noNeurons'], resultDict[key][fitmethod]['Eigenworm3']['noNeurons']])
noNeur = np.array(noNeur)
plt.figure()
plt.bar([1,2], np.mean(noNeur, axis=0),yerr=np.std(noNeur, axis=0) )
plt.scatter(np.ones(len(noNeur[:,0]))+0.5, noNeur[:,0])
plt.scatter(np.ones(len(noNeur[:,0]))+1.5, noNeur[:,1])
plt.xticks([1,2], ['velocity', 'Turns'])
plt.show()