In [1]:
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
from scipy import io
import scipy.io as sio
%matplotlib inline 
import pylab
import csv
from Tkinter import Tk
from tkFileDialog import askopenfilename
from tkFileDialog import askdirectory
import nibabel as nb
from scipy import io
import nibabel as nb
from scipy.interpolate import interp1d
from scipy import ndimage

In [2]:
from sklearn import linear_model

Open data


In [3]:
# from http://stackoverflow.com/questions/3579568/choosing-a-file-in-python-with-simple-dialog
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)


/media/sophie/1554f3a2-94a1-4cf8-ae79-8a6fef1b5f7e/FreeBehaviorPanNeuronalGCaMP6/100498series/100499_100500_100501ConcatenatedStacksM308Smith0_4_60TS.mat

In [4]:
Ua=sio.loadmat(filename)
DT=Ua['TSo']
DT.shape


Out[4]:
(21496, 308)

In [5]:
# from http://stackoverflow.com/questions/3579568/choosing-a-file-in-python-with-simple-dialog
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename2 = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename2)


/media/sophie/1554f3a2-94a1-4cf8-ae79-8a6fef1b5f7e/FreeBehaviorPanNeuronalGCaMP6/100498series/100499_100500_100501ConcatenatedStacksM308Smith0_4_60IC.nii

In [6]:
img1 = nb.load(filename2)
data = img1.get_data()
S=data.shape
S


Out[6]:
(166, 111, 10, 308)

In [7]:
Time_fluoICA=np.array(range(11603))*0.01

Z-score


In [8]:
Demean=np.zeros(S)
Dmaps=np.zeros(S)
Dvar=np.zeros(S)
Var=np.zeros(S[3])
D2=np.zeros([S[0],S[1],5,S[3]])
Tvar=np.zeros(S[3])

In [9]:
for i in range(S[3]):
    Demean[:,:,:,i]=data[:,:,:,i]-np.mean(np.mean(np.mean(data[:,:,:,i],0),0),0)

In [10]:
for i in range(S[3]):
    Dsq=np.reshape(Demean[:,:,:,i],S[0]*S[1]*S[2])
    Var[i]=np.sqrt(np.var(Dsq))
    Dvar=Demean[:,:,:,i]/Var[i]
    Dmaps[:,:,:,i]=Dvar-2
    Tvar[i]=np.var(DT[i,:])
Dmaps[Dmaps<0]=0

In [11]:
# from http://stackoverflow.com/questions/3579568/choosing-a-file-in-python-with-simple-dialog
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)


/media/sophie/1554f3a2-94a1-4cf8-ae79-8a6fef1b5f7e/FreeBehaviorPanNeuronalGCaMP6/100498series/Xk499_500_501.mat

In [12]:
Ua=sio.loadmat(filename)
Xk=Ua['Xk']

In [13]:
Xk.shape


Out[13]:
(21496, 2)

Open Masks


In [14]:
# from http://stackoverflow.com/questions/3579568/choosing-a-file-in-python-with-simple-dialog
from Tkinter import Tk
from tkFileDialog import askopenfilename

Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filenameM = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filenameM)
img1 = nb.load(filenameM)
Masks = img1.get_data()
Sm=Masks.shape
Masks=np.array(Masks)


/media/sophie/1554f3a2-94a1-4cf8-ae79-8a6fef1b5f7e/FreeBehaviorPanNeuronalGCaMP6/100498series/100498Registration/JFRC100498Transformedfullpsftrimmed.nii

In [15]:
filenameM='/home/sophie/RegionList'
with open(filenameM) as f:
    content = f.readlines()
Names=[Line.split('\t') for Line in content]
RegionName=[Names[i][0] for i in range(75)]
Num=[int(Names[i][2]) for i in range(75)]

Average in masks to sort components by brain region


In [16]:
Dmaps.shape


Out[16]:
(166, 111, 10, 308)

In [17]:
M=np.zeros((S[3],86))
Mapmean=np.zeros(S[3])
MMasks=np.zeros(86)

In [18]:
for i in range(S[3]):
    Mapmean[i]=np.mean(np.mean(np.mean(Dmaps[:,:,:,i])))
    for j in range(86):
        MMasks[j]=np.mean(np.mean(np.mean(Masks[:,:,:,j])))
        if MMasks[j]:
            M[i,j]=np.mean(np.mean(np.mean(Masks[:,:,:,j]*Dmaps[:,:,:,i])))/(MMasks[j]*Mapmean[i])

In [19]:
plt.plot(M[1,:])
plt.plot(M[100,:])
plt.plot(M[200,:])
plt.plot(M[298,:])


Out[19]:
[<matplotlib.lines.Line2D at 0x7fa6e292add0>]

In [20]:
J=[l for l in range(75) if Num[l]==I]


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-20-08623a2922f4> in <module>()
----> 1 J=[l for l in range(75) if Num[l]==I]

NameError: name 'I' is not defined

In [21]:
J


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-21-76b0b0b60246> in <module>()
----> 1 J

NameError: name 'J' is not defined

In [22]:
CompMainName=S[3]*['']
CompNameAdd=np.zeros((S[3],86))
for i in range(S[3]):
    Max=np.max(M[i,:])
    I=np.argmax(M[i,:])+1
    for j in range(86):
        J=[l for l in range(74) if Num[l]==(j+1)]
        if M[i,j]>0.2*Max:
            CompNameAdd[i,J]=1
    J=[l for l in range(75) if Num[l]==I]
    CompMainName[i]=Names[np.array(J)][0]


/usr/local/lib/python2.7/dist-packages/ipykernel/__main__.py:11: VisibleDeprecationWarning: converting an array with ndim > 0 to an index will result in an error in the future

In [23]:
Time_fluoICA.shape


Out[23]:
(11603,)

In [24]:
DT.shape


Out[24]:
(21496, 308)

In [25]:
pylab.rcParams['figure.figsize'] = (13, 2.5)

h=5
tot=0
GoodICAnat=np.zeros(S[3])

for l in range(74):
    Final_maps=np.zeros((S[0],S[1],3))
    Fmap=np.zeros((S[0],S[1],3))
    C=np.zeros(3)

    n=0
    for i in range(len(CompMainName)):                    
        Dmmv=np.mean(data[:,:,:,i],2) 
        Dmmv[Dmmv<0.2*np.max(np.max(np.max(Dmmv)))]=0
        C=np.squeeze(np.random.rand(3,1))
        labeled, nrobject=ndimage.label(Dmmv>0)
        
        if CompMainName[i]==Names[l][0] and (sum(CompNameAdd[i,:])<5) and nrobject<200:
            n=n+1            
            
            for k in range(3):
                Fmap[:,:,k]=0.7*Dmmv*C[k]/np.max(C)
            Final_maps=Final_maps+Fmap
            plt.plot((DT[:,i]/np.sqrt(np.var(DT[:,i]))-h*n+2),color=C/2)        
            tot=tot+1
            GoodICAnat[i]=1
            #print(i)
            for j in range(86):
                if CompNameAdd[i,j]==1:                
                    print(Names[np.array(j)][0])
            print(i)
            
    plt.plot(Xk[:,0]/np.std(Xk[:,0])+2,color=(1,0,0))   
    plt.plot(Xk[:,1]/np.std(Xk[:,1])+2,color=(0.5,0.5,0))
    #plt.plot(Xk[:,4]/np.std(Xk[:,4])+6,color=(1,0,1))
    #plt.plot(Xk[:,6]/np.std(Xk[:,6])+12,color=(0,1,0))
    #plt.plot(Xk[:,7]/np.std(Xk[:,7])+12,color=(0,0,1))
    #plt.plot(Time_fluoICA.T,2*Xk[:,2]/np.max(Xk[:,2])+1.5,color=(1,0,1))    
    if n!=0:
        print(Names[l][1])

        plt.show()
        FM=Final_maps/np.max(np.max(Final_maps))
        FM[FM<0.1]=0
        plt.imshow(FM,interpolation='none')
        plt.show()
        frame1 = plt.gca()
        frame1.axes.get_xaxis().set_visible(False)
        frame1.axes.get_yaxis().set_visible(False)


AME_R
LO_R
157
AME_R
159
AME_R
212
AME_R
LO_R
234
AME_R
LO_R
ME_R
278
accessory medulla
LO_R
101
LO_R
IB_R
105
LO_R
LOP_R
121
LO_R
ME_R
136
LO_R
ME_R
146
LO_R
LOP_R
ME_R
147
LO_R
LOP_R
ME_R
163
LO_R
ME_R
166
LO_R
LOP_R
ME_R
181
LO_R
ME_R
182
LO_R
LOP_R
ME_R
189
LO_R
LOP_R
210
LO_R
ME_R
211
LO_R
ME_R
214
LO_R
ME_R
226
LO_R
ME_R
237
LO_R
BU_R
ME_R
255
LO_R
LOP_R
268
LO_R
ME_R
288
lobula
BU_R
LOP_L
ME_L
168
LO_R
BU_R
ME_R
186
BU_R
193
BU_R
221
BU_R
ME_R
243
BU_R
ME_R
257
bulb
NO
PB
ATL_R
119
PB
EB
127
PB
140
PB
151
NO
PB
GA_R
247
protocerebral bridge
LH_R
SLP_R
45
LH_R
46
LH_R
MB_CA_R
77
LH_R
AL_R
GA_R
LH_L
109
LH_R
131
LH_R
EB
ME_R
MB_PED_L
142
lateral horn
SAD
GNG
PRW
AMMC_L
10
SAD
IPS_R
GNG
50
saddle
AMMC_R
IVLP_R
6
AMMC_R
17
SAD
AMMC_R
86
AMMC_R
AVLP_R
PVLP_R
IVLP_R
113
antennal mechanosensory and motor center
PB
IB_R
ATL_R
31
IB_R
IB_L
34
inferior bridge
ICL_R
IB_R
ATL_R
EB
44
PB
ATL_R
58
ATL_R
FB
108
ATL_R
188
antler
MB_PED_R
MB_VL_R
MB_CA_R
SCL_R
100
MB_PED_R
MB_ML_R
ME_R
296
pedunculus of adult mushroom body
MB_PED_R
MB_VL_R
MB_ML_R
48
MB_VL_R
SMP_R
GA_R
96
vertical lobe of adult mushroom body
CRE_R
MB_PED_R
MB_ML_R
GA_R
89
CRE_R
MB_VL_R
MB_ML_R
MB_ML_L
111
medial lobe of adult mushroom body
FLA_R
PRW
FLA_L
102
flange
LO_R
LOP_R
92
lobula plate
EB
190
EB
FB
207
ellipsoid body
LAL_R
AL_R
19
AL_R
AVLP_R
38
AL_R
AVLP_R
AOTU_R
39
AL_R
67
adult antennal lobe
ME_R
149
ME_R
160
ME_R
164
ATL_R
ME_R
AVLP_R
179
ME_R
184
LO_R
ME_R
192
LO_R
ME_R
196
AME_R
LO_R
ME_R
202
ME_R
206
LO_R
NO
LOP_R
ME_R
208
LO_R
ME_R
SPS_R
224
ME_R
AVLP_R
235
ME_R
238
ME_R
239
AME_R
ME_R
241
ME_R
242
ME_R
244
ME_R
SCL_L
246
IB_R
ME_R
249
MB_ML_R
ME_R
250
LO_R
ME_R
252
LO_R
ME_R
259
ME_R
260
LO_R
ME_R
GA_R
270
LO_R
ME_R
272
ME_R
275
ME_R
FB
279
ME_R
280
NO
ME_R
283
BU_R
ME_R
BU_L
284
LO_R
ME_R
285
ME_R
287
ME_R
290
ME_R
295
ME_R
299
ME_R
302
LO_R
MB_ML_R
EB
ME_R
303
LO_R
ME_R
305
medulla
EB
FB
SMP_R
SMP_L
118
fan-shaped body
LH_R
MB_VL_R
SLP_R
SIP_R
5
SLP_R
SIP_R
80
SLP_R
SCL_R
91
SLP_R
134
SLP_R
SMP_R
SCL_R
GA_R
135
superior lateral protocerebrum
CRE_R
SIP_R
CRE_L
SIP_L
14
superior intermediate protocerebrum
SMP_R
87
ATL_R
SMP_R
SCL_R
106
SMP_R
150
superior medial protocerebrum
AVLP_R
78
anterior ventrolateral protocerebrum
AVLP_R
PVLP_R
PLP_R
103
posterior ventrolateral protocerebrum
SAD
AMMC_R
IVLP_R
47
AMMC_R
IVLP_R
IVLP_L
55
IVLP_R
PLP_R
IPS_R
IVLP_L
117
wedge
IB_R
PLP_R
13
AME_R
PLP_R
123
posterior lateral protocerebrum
AOTU_R
203
ME_R
IVLP_R
AOTU_R
MB_VL_L
266
anterior optic tubercle
MB_PED_R
MB_CA_R
29
MB_PED_R
MB_CA_R
51
MB_CA_R
61
MB_CA_R
128
calyx of adult mushroom body
IB_R
SPS_R
27
IVLP_R
SPS_R
IPS_R
EPA_R
59
AME_R
SPS_R
81
superior posterior slope
IPS_R
65
SPS_R
IPS_R
98
PLP_R
IPS_R
139
AMMC_R
VES_R
SPS_R
IPS_R
201
SAD
IPS_R
273
inferior posterior slope
AME_R
GNG
CAN_L
94
GNG
LOP_L
126
SAD
GNG
CAN_L
130
GNG
MB_VL_L
MB_ML_L
144
SAD
MB_VL_R
GNG
AMMC_L
167
SAD
FLA_R
GNG
PRW
172
SAD
GNG
CAN_L
IPS_L
178
GNG
CAN_L
204
BU_R
EB
GNG
MB_PED_L
218
adult gnathal ganglion
PRW
CAN_L
VES_L
FLA_L
35
SAD
GNG
PRW
64
FLA_R
SLP_R
PRW
GA_R
73
prow
LO_R
LOP_R
ME_R
GA_R
258
gall
AME_L
LO_L
124
AME_L
LO_L
176
accessory medulla
LO_L
125
LO_L
LOP_L
141
LO_L
ME_L
153
AME_L
LO_L
154
LO_L
LOP_L
ME_L
173
LO_L
177
LO_L
ME_L
215
LO_L
LOP_L
ME_L
276
PB
LO_L
IB_L
ME_L
291
LO_L
LOP_L
ME_L
294
lobula
EB
BU_L
138
BU_L
230
bulb
LH_L
SLP_L
8
LH_L
PLP_L
MB_CA_L
43
LH_L
63
LH_R
LH_L
SCL_L
74
LH_L
107
lateral horn
SMP_R
CAN_L
110
SAD
CAN_L
137
CAN_L
183
IPS_R
GNG
CAN_L
IPS_L
292
cantle
AMMC_L
PVLP_L
4
LAL_L
AMMC_L
IVLP_L
9
AMMC_L
IVLP_L
12
CAN_L
AMMC_L
IPS_L
60
antennal mechanosensory and motor center
CAN_R
IB_R
IB_L
SPS_L
0
IB_L
ATL_L
72
inferior bridge
FB
ATL_L
114
PB
FB
ATL_L
264
antler
BU_R
ME_R
MB_PED_L
199
pedunculus of adult mushroom body
MB_VL_L
MB_ML_L
SIP_L
SCL_L
54
MB_PED_L
MB_VL_L
MB_CA_L
SCL_L
66
CRE_L
MB_VL_L
MB_ML_L
120
vertical lobe of adult mushroom body
CRE_L
MB_PED_L
MB_ML_L
90
ME_R
MB_ML_L
200
medial lobe of adult mushroom body
LO_L
LOP_L
152
LO_L
LOP_L
ME_L
169
LO_L
LOP_L
174
LO_L
LOP_L
197
LOP_L
274
LOP_L
289
AME_L
LOP_L
ME_L
301
LOP_L
ME_L
307
lobula plate
AL_L
AVLP_L
3
AL_R
AL_L
15
AL_R
VES_L
AL_L
28
LAL_L
AL_L
57
adult antennal lobe
LOP_L
ME_L
85
LO_L
ME_L
143
LO_L
ME_L
AVLP_L
PVLP_L
156
LO_L
ME_L
AVLP_L
158
ME_L
162
ME_L
195
ME_L
213
LO_L
LOP_L
ME_L
219
LO_L
ME_L
220
ME_L
222
LO_L
LOP_L
ME_L
227
LO_L
MB_PED_L
LOP_L
ME_L
228
ME_L
229
ME_L
231
ME_L
236
LO_L
MB_PED_L
ME_L
245
ME_L
253
ME_L
262
MB_PED_L
MB_ML_L
LOP_L
ME_L
265
NO
LOP_L
ME_L
269
AME_L
ME_L
271
LO_L
LOP_L
ME_L
277
LO_L
LH_L
ME_L
281
ME_L
282
LO_L
LOP_L
ME_L
286
MB_PED_R
LO_L
LOP_L
ME_L
298
ME_L
300
IPS_R
LO_L
ME_L
304
LOP_L
ME_L
306
medulla
SLP_L
SIP_L
33
MB_PED_R
GA_R
SLP_L
112
superior lateral protocerebrum
SMP_L
69
CRE_L
MB_VL_L
SMP_L
75
CAN_R
MB_ML_L
SMP_L
116
SMP_R
AME_L
IB_L
SMP_L
161
SLP_L
SMP_L
180
SMP_R
CRE_L
SMP_L
185
SIP_L
SMP_L
187
superior medial protocerebrum
AVLP_L
PVLP_L
56
AVLP_R
AVLP_L
PVLP_L
76
anterior ventrolateral protocerebrum
AVLP_L
PVLP_L
IVLP_L
PLP_L
115
posterior ventrolateral protocerebrum
AMMC_L
IVLP_L
23
wedge
PLP_L
21
PLP_L
IPS_L
25
posterior lateral protocerebrum
MB_CA_L
11
MB_CA_L
83
SLP_L
MB_CA_L
97
calyx of adult mushroom body
IB_L
SPS_L
36
superior posterior slope
SAD
GNG
IPS_L
30
CAN_L
SPS_L
IPS_L
40
CAN_L
IPS_L
41
IPS_L
68
inferior posterior slope
NO
LAL_L
GOR_L
EPA_L
42
epaulette

In [27]:
sum(GoodICAnat)-5-9


Out[27]:
237.0

In [41]:
# from http://stackoverflow.com/questions/3579568/choosing-a-file-in-python-with-simple-dialog
from Tkinter import Tk
from tkFileDialog import askopenfilename

Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filenamet = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filenamet)
nimt=nb.load(filenamet)
Dtemp=np.squeeze(nimt.get_data())
Dtemp.shape


/media/sophie2/100148/100148ss2it30/AVG_100148ss2onc500regcpsf.nii
Out[41]:
(181, 109, 9)

Reorder by larger sub-regions (~ presumed stimulus to motor)


In [57]:
LargerRegionsDic={'':'','AME_R':'OL','LO_R':'OL','NO':'CX','BU_R':'CX','PB':'CX','LH_R':'LH','LAL_R':'LX','SAD':'PENP'
               ,'CAN_R':'PENP','AMMC_R':'PENP','ICL_R':'INP','VES_R':'VMNP','IB_R':'INP','ATL_R':'INP','CRE_R':'INP'
               ,'MB_PED_R':'MB','MB_VL_R':'MB','MB_ML_R':'MB','FLA_R':'PENP','LOP_R':'OL','EB':'CX','AL_R':'AL',
                'ME_R':'OL','FB':'CX','SLP_R':'SNP','SIP_R':'SNP','SMP_R':'SNP','AVLP_R':'VLNP','PVLP_R':'VLNP',
                'IVLP_R':'VLNP','PLP_R':'VLNP','AOTU_R':'VLNP','GOR_R':'VMNP','MB_CA_R':'MB','SPS_R':'VMNP',
                'IPS_R':'VMNP','SCL_R':'INP','EPA_R':'VMNP','GNG':'GNG','PRW':'PENP','GA_R':'LX','AME_L':'OL'
                ,'LO_L':'OL','BU_L':'CX','LH_L':'LH','LAL_L':'LX','CAN_L':'PENP','AMMC_L':'PENP','ICL_L':'INP',
                'VES_L':'VMNP','IB_L':'INP','ATL_L':'INP','CRE_L':'INP','MB_PED_L':'MB','MB_VL_L':'MB',
                'MB_ML_L':'MB','FLA_L':'PENP','LOP_L':'OL','AL_L':'AL','ME_L':'OL','SLP_L':'SNP','SIP_L':'SNP',
                'SMP_L':'SNP','AVLP_L':'VLNP','PVLP_L':'VLNP','IVLP_L':'VLNP','PLP_L':'VLNP','AOTU_L':'VLNP',
                'GOR_L':'VMNP','MB_CA_L':'MB','SPS_L':'VMNP','IPS_L':'VMNP','SCL_L':'INP','EPA_L':'VMNP','GA_L':'LX'}

In [58]:
SmallRegionsSorted=['ME_L','ME_R','LO_R','LO_L','LOP_R','LOP_L','AME_R','AME_L',
                  'PLP_R','PLP_L','PVLP_R','PVLP_L','AVLP_R','AVLP_L','AOTU_R','AOTU_L','IVLP_R','IVLP_L',
                  'AL_R','AL_L',
                  'MB_CA_R','MB_CA_L','MB_PED_R','MB_PED_L','MB_VL_R','MB_VL_L','MB_ML_R','MB_ML_L',
                  'SMP_R','SMP_L','SIP_R','SLP_L','SLP_R','SIP_L',
                  'LH_R','LH_L',                  
                  'CRE_R','CRE_L','ICL_R','ICL_L','SCL_R','SCL_L','IB_R','IB_L','ATL_R','ATL_L',
                  'EB','PB','NO','FB',
                  'BU_R','BU_L','LAL_R','LAL_L','GA_R','GA_L',
                  'GOR_R','GOR_L','EPA_R','EPA_L','VES_R','VES_L','SPS_R','SPS_L','IPS_R','IPS_L',
                  'AMMC_R','AMMC_L','SAD','FLA_R','FLA_L','PRW','CAN_R','CAN_L',
                  'GNG','']

In [59]:
Tozip=range(len(SmallRegionsSorted))
SmallRegionsDic=dict(zip(SmallRegionsSorted,Tozip))

In [60]:
LargerRegion=[LargerRegionsDic[CompMainName[i]] for i in range(S[3])]

In [61]:
LargerRegionInd={ 'OL':1,'VLNP':2,'VMNP':3,'AL':4,'MB':5,'LH':6,'SNP':7,'CX':8,'LX':9,'INP':10,'PENP':11,'GNG':12,'':13}

In [62]:
LargerRegionI=np.array([LargerRegionInd[LargerRegion[i]] for i in range(S[3])])

In [63]:
SmallRegion=np.array([SmallRegionsDic[CompMainName[i]] for i in range(S[3])])

In [64]:
NewOrder=np.argsort(SmallRegion)

In [65]:
SmallRegion[NewOrder]


Out[65]:
array([ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
        0,  0,  0,  0,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
        1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  2,
        2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
        2,  2,  2,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  4,  4,  4,  4,
        4,  4,  4,  4,  5,  5,  5,  5,  5,  5,  7,  7,  7, 10, 11, 12, 12,
       12, 13, 13, 14, 16, 16, 16, 17, 17, 17, 17, 18, 18, 19, 19, 19, 19,
       19, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 22, 23, 23, 24, 24,
       25, 25, 25, 25, 26, 26, 26, 26, 27, 27, 28, 28, 28, 28, 28, 29, 29,
       29, 29, 29, 30, 31, 31, 32, 32, 34, 34, 35, 35, 35, 44, 44, 45, 45,
       45, 45, 46, 46, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 48,
       48, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 53, 54, 54, 54, 54, 54,
       54, 55, 55, 55, 55, 55, 57, 57, 58, 58, 60, 61, 62, 62, 63, 63, 64,
       64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 65, 65, 65, 65, 66, 66,
       66, 66, 66, 66, 67, 67, 67, 68, 69, 69, 69, 69, 69, 70, 70, 70, 70,
       70, 71, 71, 71, 71, 71, 71, 71, 71, 71, 72, 72, 72, 72, 72, 72, 72,
       73, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74])

Last pruning by hand


In [66]:
%%javascript
IPython.OutputArea.auto_scroll_threshold =4000;



In [67]:
if S[2]>5:
    Nstack=5
    Int100=[(i+1)*100/Nstack for i in range(Nstack)]
    Percs=np.percentile(range(S[2]),Int100)
    Indices=np.split(range(S[2]),Percs)
    D1=np.zeros([S[0],S[1],Nstack])
    Dmean=np.squeeze(data[:,:,range(Nstack),2])
    for i in range(Nstack):
        Vmean=np.mean(Dtemp[:,:,Indices[i]],2)
        Dmean[:,:,i]=Vmean
else:
    Nstack=S[2]
    D1=np.zeros([S[0],S[1],S[2]])
    Dmean=data[:,:,range(S[2])]  
    Dmean=np.squeeze(Dtemp[:,:,:])

In [68]:
plt.imshow(Dmean[:,:,1],cmap=plt.cm.gray)


Out[68]:
<matplotlib.image.AxesImage at 0x7f16b6c89dd0>

In [69]:
my_cmap=plt.cm.jet
my_cmap.set_bad(alpha=0)
Good_ICs=np.zeros(S[3])
Label_ICs=[]
pylab.rcParams['figure.figsize'] = (15, 2.5)

In [81]:
algorithm = linear_model.LinearRegression()

In [162]:
Sxk=Xk.shape

In [163]:
Sxk


Out[163]:
(11603, 8)

In [164]:
X=np.zeros((11603, 6))

In [165]:
X[:,0]=(Xk[:,0]-np.mean(Xk[:,0]))/np.std(Xk[:,0])
X[:,1]=(Xk[:,1]-np.mean(Xk[:,1]))/np.std(Xk[:,1])
X[:,2]=(Xk[:,3]-np.mean(Xk[:,3]))/np.std(Xk[:,3])
X[:,3]=(Xk[:,4]-np.mean(Xk[:,4]))/np.std(Xk[:,4])
X[:,4]=(Xk[:,6]-np.mean(Xk[:,6]))/np.std(Xk[:,6])
X[:,5]=(Xk[:,7]-np.mean(Xk[:,7]))/np.std(Xk[:,7])

In [310]:
#plt.plot(X[:,0])
#plt.plot(X[:,1])
#plt.plot(X[:,2])
plt.plot(X[:,3])


Out[310]:
[<matplotlib.lines.Line2D at 0x7f16aef223d0>]

In [167]:
Rsq=np.zeros((4,S[3]))
Betas=np.zeros((4,S[3]))

In [169]:
for j in range(S[3]):

    a=''
    if S[2]>5:
        for i in range(Nstack):
            V=Dmaps[:,:,Indices[i],j]
            D1[:,:,i]=np.max(V,2)
        D2[:,:,:,j]=D1
        D1[D1==0]=np.nan
           
    else:
        for i in range(S[2]):
            V=Dmaps[:,:,i,Order[j]]
            D1[:,:,i]=V 

    if (CompMainName[j] != '') and (LargerRegionI[j]!=1) and (LargerRegionI[j]==1 or LargerRegionI[j]==1
                                                             
                                                             
                                                             ):
        print(j)
        print(CompMainName[j])
        for i in range(Nstack):
            plt.subplot(1,5,i+1)
            plt.imshow(Dmean[:,:,i],cmap=plt.cm.gray)
            plt.imshow(D1[:,:,i], cmap=my_cmap,interpolation='none')
            frame1 = plt.gca()
            frame1.axes.get_xaxis().set_visible(False)
            frame1.axes.get_yaxis().set_visible(False)
        
        plt.show()
        
        model = algorithm.fit(X, DT[:,j])
        betas = model.coef_
        rsq = model.score(X,DT[:,j])
        print('left:',betas[0],'right:',betas[1],'walk:',betas[2],'groom:',betas[3])
        print(rsq)
        plt.plot(Time_fluoICA.T,2*DT[:,j]+1.5)
        plt.plot(Time_fluoICA.T,X[:,0],color=(1,0,0))   
        plt.plot(Time_fluoICA.T,X[:,1],color=(1,0,0))
        plt.show()
        a=raw_input()
    
    Label_ICs.append(a)
    if Label_ICs[j]!='':
        Good_ICs[j]=1

In [170]:
Dmaps.shape


Out[170]:
(181, 109, 9, 300)

In [171]:
fn=open('/home/sophie/Desktop/100148GoodICs150.txt','w')
for i in range(S[3]):
    if Good_ICs[i]:
        print>>fn, i
        print>>fn, CompMainName[i]
        print>>fn, Good_ICs[i]

In [172]:
if len(Label_ICs)<S[3]:
    for j in range(S[3]-len(Label_ICs)):
      Label_ICs.append('')

In [147]:
G=Good_ICs.tolist();

In [148]:
len(Good_ICs)


Out[148]:
300

Plot all components for turning left, right, walking, and grooming


In [174]:
Rsq=np.zeros((1,S[3]))
Betas=np.zeros((6,S[3]))
for j in range(S[3]):
    model = algorithm.fit(X, DT[:,j])
    Betas[:,j] = model.coef_
    Rsq[:,j] = model.score(X,DT[:,j])

In [175]:
RsqUni=np.zeros((6,S[3]))
BetaUni=np.zeros((6,S[3]))
Sx=X.shape
for k in range(6):
    for j in range(S[3]):
        model = algorithm.fit(np.reshape(X[:,k],(Sx[0],1)), DT[:,j])
        BetaUni[k,j] = model.coef_
        RsqUni[k,j] = model.score(np.reshape(X[:,k],(Sx[0],1)),DT[:,j])

In [336]:
RsqUni.shape


Out[336]:
(6, 300)

In [176]:
plt.plot(RsqUni[0,:])


Out[176]:
[<matplotlib.lines.Line2D at 0x7f16bc058390>]

In [295]:
GoodICo=Good_ICs[NewOrder]
D2o=D2[:,:,:,NewOrder]
LargerRegionIo=LargerRegionI[NewOrder]
Ind=np.array(range(S[3]))
Indexo=Ind[NewOrder]
DTo=DT[:,NewOrder]

In [351]:
import random

In [453]:
del Final_map
del Fmaps

In [454]:
if S[2]>5:
    Final_map=np.zeros([S[0],S[1],5,3])
    Fmaps=np.zeros([S[0],S[1],5,3])
else:
    Final_map=np.zeros([S[0],S[1],3]) 
    Fmaps=np.zeros([S[0],S[1],3])    
C=np.zeros([S[3],3])
C1=np.zeros([6,3])
C1[0][:]=(1,0,0)
C1[1][:]=(0,1,0)
C1[2][:]=(0,0,1)
C1[3][:]=(0.8,0.8,0)
C1[4][:]=(0,1,1)
C1[5][:]=(1,0,1)
S1=DT.shape

In [455]:
random.uniform(0,1)


Out[455]:
0.6169860564661207

In [457]:
C=np.zeros((S[3],3))
i=0
for j in range(S[3]):  
    if Betas[1,j]>0.85*np.max(Betas[1,:]):
    #if 1>0.1:
        #C[j,:]=C1[i%6][:]
        C[j,2]=1
        C[j,1]=random.uniform(0,1)
        #C[j,2]=1
        for k in range(3):           
            M=np.max(np.squeeze(np.reshape(D2[:,:,:,j],S[0]*S[1]*5)))
            Fmaps[:,:,:,k]=0.8*D2[:,:,:,j]*C[j,k]/M
        Final_map=Final_map+Fmaps
        J=j
        #print(Indexo[j])
        i=i+1

In [462]:
pylab.rcParams['figure.figsize'] = (19, 6)
C2=np.zeros(3)

Df=np.zeros([S[0],S[1],5,3]) 
  
for i in range(3):
    Df[:,:,:,i]=Final_map[:,:,:,i]+Dmean/16
    Df=Df/(np.max(np.max(Df)))
if S[2]>5:
    N=Nstack
else:
    N=S[2]
for i in range(N):
    #if Good_ICs[j]:
        plt.subplot(1,N,i+1)
        plt.imshow(Df[:,:,i],cmap=plt.cm.gray)
        plt.imshow(Df[:,:,i,:],cmap=my_cmap,interpolation='none')
        frame1 = plt.gca()
        frame1.axes.get_xaxis().set_visible(False)
        frame1.axes.get_yaxis().set_visible(False)
plt.tight_layout(pad=0,w_pad=0,h_pad=0)


Plot all components together


In [150]:
if S[2]>5:
    Final_map=np.zeros([S[0],S[1],5,3])
    Fmaps=np.zeros([S[0],S[1],5,3])
else:
    Final_map=np.zeros([S[0],S[1],3]) 
    Fmaps=np.zeros([S[0],S[1],3])    
C=np.zeros([S[3],3])

In [151]:
C1=np.zeros([6,3])
C1[0][:]=(1,0,0)
C1[1][:]=(0,1,0)
C1[2][:]=(0,0,1)
C1[3][:]=(0.8,0.8,0)
C1[4][:]=(0,1,1)
C1[5][:]=(1,0,1)
S1=DT.shape

In [152]:
GoodICo=Good_ICs[NewOrder]
D2o=D2[:,:,:,NewOrder]
LargerRegionIo=LargerRegionI[NewOrder]
Ind=np.array(range(S[3]))
Indexo=Ind[NewOrder]
DTo=DT[:,NewOrder]

In [153]:
C=np.zeros((S[3],3))
i=0
for j in range(S[3]):  
    if LargerRegionIo[j]<12 and GoodICo[j]:
        C[j,:]=C1[i%6][:]
        for k in range(3):           
            M=np.max(np.squeeze(np.reshape(D2o[:,:,:,j],S[0]*S[1]*5)))
            Fmaps[:,:,:,k]=0.6*D2o[:,:,:,j]*C[j,k]/M
        Final_map=Final_map+Fmaps
        #print(Indexo[j])
        i=i+1

In [154]:
pylab.rcParams['figure.figsize'] = (15, 6)
C2=np.zeros(3)

Df=np.zeros([S[0],S[1],5,3]) 
  
for i in range(3):
    Df[:,:,:,i]=Final_map[:,:,:,i]+Dmean/16
    Df=Df/(np.max(np.max(Df)))
if S[2]>5:
    N=Nstack
else:
    N=S[2]
for i in range(N):
    #if Good_ICs[j]:
        plt.subplot(1,N,i+1)
        plt.imshow(Dmean[:,:,i],cmap=plt.cm.gray)
        plt.imshow(Df[:,:,i,:],cmap=my_cmap,interpolation='none')
        frame1 = plt.gca()
        frame1.axes.get_xaxis().set_visible(False)
        frame1.axes.get_yaxis().set_visible(False)
plt.tight_layout(pad=0,w_pad=0,h_pad=0)



In [123]:
pylab.rcParams['figure.figsize'] = (10, 15)
h=0.5
i=0

for j in range(S[3]):
    if GoodICo[j]:
        plt.plot(Time_fluoICA,(DTo[:,j]+h*i),color=C[j,:]) 
        i=i+1
plt.xlim([np.min(Time_fluoICA),np.max(Time_fluoICA)])
plt.ylim([-0.5,h*i])
frame1 = plt.gca()
frame1.axes.get_yaxis().set_visible(False)
plt.show()



In [124]:
k=0
J=np.zeros(len(GoodICo[GoodICo==1]))
for j in range(len(GoodICo)):
    if GoodICo[j]:
        print(k)
        print([CompMainName[Indexo[j]]])
        J[k]=j
        k=k+1


0
['ME_L']
1
['ME_L']
2
['ME_L']
3
['ME_L']
4
['ME_L']
5
['ME_L']
6
['ME_R']
7
['ME_R']
8
['LO_R']
9
['LO_R']
10
['LO_R']
11
['LO_R']
12
['LO_L']
13
['LO_L']
14
['LO_L']
15
['LOP_L']
16
['AVLP_R']
17
['AVLP_L']
18
['IVLP_R']
19
['IVLP_R']
20
['IVLP_L']
21
['AL_L']
22
['MB_VL_R']
23
['SMP_R']
24
['EB']
25
['PB']
26
['PB']
27
['PB']
28
['PB']
29
['PB']
30
['PB']
31
['SPS_R']
32
['SPS_L']
33
['IPS_R']
34
['IPS_R']
35
['IPS_R']
36
['IPS_R']
37
['IPS_R']
38
['IPS_R']
39
['IPS_R']
40
['IPS_L']
41
['IPS_L']
42
['SAD']
43
['FLA_L']
44
['PRW']
45
['PRW']
46
['PRW']
47
['GNG']
48
['']
49
['']
50
['']
51
['']
52
['']
53
['']
54
['']
55
['']
56
['']
57
['']

In [125]:
Sets=[range(10),range(10,12),range(12,17),range(17,20),20,range(21,23),range(23,25),25]

In [126]:
pylab.rcParams['figure.figsize'] = (12, 6)

for i in range(len(Sets)):
    
    Final_map2=np.zeros([S[0],S[1],3]) 
    Fmaps2=np.zeros([S[0],S[1],3]) 
    Final_map3=np.zeros([S[0],S[1],5,3]) 
    Fmaps3=np.zeros([S[0],S[1],5,3])     
     
    if type(Sets[i])==list:
        for j in np.array(Sets[i]):
            C=np.zeros((S[3],3))
            C[j,:]=C1[j%6][:]
            
            for k in range(3):           
                M=np.max(np.squeeze(np.reshape(D2o[:,:,:,J[j]],S[0]*S[1]*5)))
                Fmaps2[:,:,k]=0.9*np.mean(D2o[:,:,:,J[j]],2)*C[j,k]/M
                M=np.max(np.squeeze(np.reshape(D2o[:,:,:,J[j]],S[0]*S[1],5)))
                Fmaps3[:,:,:,k]=0.9*D2o[:,:,:,J[j]]*C[j,k]/M                
            Final_map2=Final_map2+Fmaps2
            Final_map3=Final_map3+Fmaps3            
                
    else:
        j=Sets[i]
        C[j,:]=C1[j%6][:]
        for k in range(3):           
            M=np.max(np.squeeze(np.reshape(D2o[:,:,:,J[j]],S[0]*S[1]*5)))
            Fmaps2[:,:,k]=0.8*np.mean(D2o[:,:,:,J[j]],2)*C[j,k]/M
        Final_map2=Final_map2+Fmaps2
                
    Df=np.zeros([S[0],S[1],3]) 
  
    for l in range(3):
        Df[:,:,l]=Final_map2[:,:,l]+np.mean(Dmean,2)/16
    MM=np.max(np.max(Df))

    Rotated=ndimage.rotate(Df[:,:,:]/MM,-90)
    a=plt.imshow(Rotated,cmap=my_cmap,interpolation='none')
    frame1 = plt.gca()
    frame1.axes.get_xaxis().set_visible(False)
    frame1.axes.get_yaxis().set_visible(False)

    plt.show()


/usr/local/lib/python2.7/dist-packages/ipykernel/__main__.py:16: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
/usr/local/lib/python2.7/dist-packages/ipykernel/__main__.py:17: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
/usr/local/lib/python2.7/dist-packages/ipykernel/__main__.py:18: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-126-721c891f5728> in <module>()
     16                 M=np.max(np.squeeze(np.reshape(D2o[:,:,:,J[j]],S[0]*S[1]*5)))
     17                 Fmaps2[:,:,k]=0.9*np.mean(D2o[:,:,:,J[j]],2)*C[j,k]/M
---> 18                 M=np.max(np.squeeze(np.reshape(D2o[:,:,:,J[j]],S[0]*S[1],5)))
     19                 Fmaps3[:,:,:,k]=0.9*D2o[:,:,:,J[j]]*C[j,k]/M
     20             Final_map2=Final_map2+Fmaps2

/usr/local/lib/python2.7/dist-packages/numpy/core/fromnumeric.pyc in reshape(a, newshape, order)
    222     except AttributeError:
    223         return _wrapit(a, 'reshape', newshape, order=order)
--> 224     return reshape(newshape, order=order)
    225 
    226 

ValueError: total size of new array must be unchanged

In [ ]: