In [2]:
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
#from nifti import NiftiImage
import nibabel as nb
from scipy.interpolate import interp1d
from scipy import ndimage

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)


()

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


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-5deed7cbcf29> in <module>()
----> 1 Ua=sio.loadmat(filename)
      2 DT=Ua['TSo']
      3 DT.shape

NameError: name 'sio' is not defined

In [4]:
# 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/db554c18-e3eb-41e2-afad-7de1c92bf4a5/ArclightCombo/910-911/910-911psfdffkf179Smith0_4_60IC.nii

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


Out[5]:
(83, 44, 10, 179)

In [6]:
S=data.shape
S


Out[6]:
(83, 44, 10, 179)

Z-score


In [7]:
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 [8]:
for i in range(S[3]):
    Demean[:,:,:,i]=data[:,:,:,i]-np.mean(np.mean(np.mean(data[:,:,:,i],0),0),0)

In [9]:
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.5
    Tvar[i]=np.var(DT[i,:])
Dmaps[Dmaps<0]=0

Open Masks


In [10]:
# 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/db554c18-e3eb-41e2-afad-7de1c92bf4a5/ArclightCombo/910-911/910_911registration/910JFRCTransformedfullpsftrimmed.nii

In [11]:
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 [12]:
Dmaps.shape


Out[12]:
(83, 44, 10, 179)

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

In [14]:
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 [15]:
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(74) if Num[l]==I]
    if J!= []:
        CompMainName[i]=Names[np.array(J)][0]


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

In [16]:
J


Out[16]:
[31]

In [17]:
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(Time_fluoICA.T,(DT[:,i]/np.sqrt(np.var(DT[:,i]))-h*n+2),color=C/2)
            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)
            
                    
    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
IVLP_R
91
accessory medulla
LO_R
LOP_R
CAN_L
9
LO_R
AL_R
ME_R
MB_CA_R
99
LO_R
LOP_R
106
LO_R
BU_R
EB
ME_R
109
LO_R
LOP_L
120
LO_R
122
lobula
NO
PB
FB
45
NO
PB
58
nodulus
BU_R
SLP_R
SIP_R
SCL_R
152
bulb
PB
ATL_R
28
PB
FB
ATL_L
100
PB
ATL_R
ATL_L
127
protocerebral bridge
LH_R
32
LH_R
54
LH_R
AL_R
MB_CA_R
75
LH_R
88
LH_R
124
LH_R
MB_CA_R
SCL_R
165
LH_R
168
LH_R
SCL_R
172
LH_R
MB_VL_R
SLP_R
MB_CA_R
177
lateral horn
SAD
CAN_R
GNG
IPS_L
2
SAD
GNG
CAN_L
4
SAD
IPS_R
GNG
CAN_L
29
SAD
IPS_R
GNG
55
SAD
CAN_R
GNG
89
saddle
SAD
CAN_R
IPS_R
GNG
59
CAN_R
GNG
CAN_L
AL_L
102
cantle
MB_VL_R
175
vertical lobe of adult mushroom body
MB_VL_R
FLA_R
PRW
30
FLA_R
GNG
AMMC_L
93
LH_R
FLA_R
MB_CA_R
LO_L
119
flange
BU_R
LOP_R
SMP_R
71
lobula plate
LAL_R
AL_R
GA_R
44
CRE_R
MB_ML_R
AL_R
MB_ML_L
103
adult antennal lobe
ME_R
0
medulla
SLP_R
SMP_R
SLP_L
84
SLP_R
SMP_R
GOR_R
ATL_L
110
SLP_R
112
SLP_R
SIP_R
LH_L
ME_L
145
SLP_R
154
SLP_R
155
SLP_R
SCL_R
156
SLP_R
163
SLP_R
CAN_L
166
ATL_R
SLP_R
SIP_R
167
SLP_R
170
BU_R
SLP_R
SCL_R
174
superior lateral protocerebrum
SLP_R
SMP_R
SLP_L
SMP_L
62
SLP_R
SMP_R
129
superior medial protocerebrum
SLP_R
AVLP_R
SCL_R
159
anterior ventrolateral protocerebrum
SIP_R
AOTU_R
150
SLP_R
SIP_R
AOTU_R
162
SLP_R
SIP_R
AOTU_R
ME_L
164
MB_VL_R
SIP_R
AOTU_R
169
SIP_R
AOTU_R
LH_L
ME_L
171
SIP_R
AOTU_R
178
anterior optic tubercle
ICL_R
GOR_R
CAN_L
78
gorget
MB_CA_R
12
MB_CA_R
14
MB_CA_R
17
MB_CA_R
18
MB_CA_R
26
MB_CA_R
LO_L
MB_CA_L
114
MB_CA_R
121
MB_CA_R
133
AL_R
MB_CA_R
LH_L
AL_L
153
SLP_R
MB_CA_R
SCL_R
SLP_L
161
calyx of adult mushroom body
SAD
IPS_R
GNG
40
SAD
FLA_R
IPS_R
GNG
61
inferior posterior slope
SLP_R
AVLP_R
GOR_R
SCL_R
143
superior clamp
GNG
1
GOR_R
GNG
16
GNG
IPS_L
19
GNG
LOP_L
21
GNG
25
GNG
37
SAD
GNG
IPS_L
38
GNG
41
SAD
IPS_R
GNG
47
GNG
LOP_L
48
SAD
GOR_R
GNG
50
IPS_R
GNG
57
SAD
GNG
IPS_L
65
GNG
69
SAD
VES_R
GNG
73
IPS_R
GNG
79
IPS_R
GNG
AL_L
86
SAD
GNG
IPS_L
94
SAD
GNG
CAN_L
IPS_L
96
SAD
GNG
97
PB
GNG
104
SAD
GNG
CAN_L
105
GNG
116
SAD
GNG
AMMC_L
MB_VL_L
132
SAD
GNG
140
adult gnathal ganglion
PRW
CAN_L
FLA_L
87
SLP_R
PRW
ME_L
148
prow
LO_L
35
LO_L
MB_CA_L
52
CAN_R
LO_L
FLA_L
125
lobula
LH_L
53
LH_L
LAL_L
PLP_L
80
SLP_R
LH_L
134
lateral horn
LAL_L
68
BU_L
LAL_L
CRE_L
AL_L
85
lateral accessory lobe
SAD
GNG
CAN_L
7
GNG
CAN_L
FLA_L
IPS_L
8
CAN_L
10
SAD
GNG
CAN_L
IPS_L
15
SAD
GNG
PRW
CAN_L
23
CAN_L
27
SAD
IPS_R
GNG
CAN_L
39
SAD
CAN_R
GNG
CAN_L
42
CAN_L
46
SAD
GNG
CAN_L
FLA_L
51
CAN_L
FLA_L
64
SAD
GNG
CAN_L
70
NO
SAD
GNG
CAN_L
82
cantle
MB_VL_L
90
vertical lobe of adult mushroom body
FLA_R
CAN_L
FLA_L
22
FLA_R
FLA_L
33
GNG
PRW
CAN_L
FLA_L
34
SLP_R
FLA_L
AL_L
MB_CA_L
135
flange
LOP_L
20
LOP_L
ME_L
SLP_L
95
lobula plate
LAL_L
AL_L
31
AL_L
67
FLA_R
EB
AL_L
130
SLP_R
BU_L
AL_L
146
AMMC_R
SLP_R
AOTU_R
AL_L
149
SLP_R
SIP_R
AL_L
158
adult antennal lobe
LH_L
ME_L
141
medulla
SLP_R
SMP_R
SLP_L
SMP_L
81
superior lateral protocerebrum
AVLP_R
AVLP_L
117
anterior ventrolateral protocerebrum
MB_CA_L
13
MB_CA_L
36
MB_CA_L
49
MB_CA_L
56
MB_CA_L
66
MB_CA_L
72
MB_CA_L
76
MB_PED_L
MB_CA_L
83
MB_CA_L
131
calyx of adult mushroom body
Looked at the components maps and time series and remove all the components which are localized on the edge of the brain and with activity unlike GCaMP6 transients.

In [25]:


In [26]:
for idx in BadICs:
    GoodICAnat[idx] = 0.0

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


In [27]:
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'}

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','']

Tozip=range(len(SmallRegionsSorted))
SmallRegionsDic=dict(zip(SmallRegionsSorted,Tozip))

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

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}

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

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

NewOrder=np.argsort(SmallRegion)

SmallRegion[NewOrder]


Out[27]:
array([ 0,  1,  2,  2,  2,  2,  2,  2,  2,  3,  3,  3,  4,  5,  5,  5,  5,
        6, 12, 12, 13, 14, 14, 14, 14, 14, 14, 14, 14, 18, 18, 18, 18, 19,
       19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
       21, 21, 21, 21, 21, 21, 21, 21, 21, 24, 25, 28, 28, 29, 30, 31, 31,
       31, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 34, 34, 34, 34,
       34, 34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 40, 40, 47, 47, 47, 47,
       48, 48, 48, 50, 53, 53, 53, 56, 56, 57, 64, 64, 68, 68, 68, 68, 68,
       69, 69, 69, 70, 70, 70, 70, 71, 71, 71, 72, 72, 72, 73, 73, 73, 73,
       73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 73, 74, 74, 74,
       74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74, 74,
       74, 74, 74, 74, 74, 74, 74, 74, 74])

In [28]:
LargerRegionIndToName = {v: k for k, v in LargerRegionInd.iteritems()}

In [29]:
LargerRegionI


Out[29]:
array([ 1, 12, 11, 11, 11,  2, 11, 11, 11,  1, 11, 11,  5,  5,  5, 11, 12,
        5,  5, 12,  1, 12, 11, 11, 12, 12,  5, 11,  8, 11, 11,  4,  6, 11,
       11,  1,  5, 12, 12, 11,  3, 12, 11,  3,  4,  8, 11, 12, 12,  5, 12,
       11,  1,  6,  6, 11,  5, 12,  8, 11,  7,  3,  7,  8, 11, 12,  5,  4,
        9, 12, 11,  1,  5, 12,  7,  6,  5,  1,  3, 12,  6,  7, 11,  5,  7,
        9, 12, 11,  6, 11,  5,  1, 12, 11, 12,  1, 12, 12, 11,  1,  8, 12,
       11,  4, 12, 12,  1,  1,  7,  1,  7,  9,  7,  6,  5,  1, 12,  2,  5,
       11,  1,  5,  1,  4,  6,  1, 11,  8, 11,  7,  4,  5, 12,  5,  6, 11,
        4,  7, 12,  6, 12,  1, 11, 10,  3,  7,  4,  8, 11,  4,  2,  5,  8,
        5,  7,  7,  7, 10,  4,  2,  2,  5,  2,  7,  2,  6,  7,  7,  6,  2,
        7,  2,  6,  2,  7,  5,  6,  6,  2])

In [30]:
GoodICAnat


Out[30]:
array([ 0.,  1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  1.,
        1.,  1.,  0.,  1.,  1.,  1.,  1.,  0.,  1.,  0.,  0.,  0.,  1.,
        1.,  0.,  0.,  0.,  1.,  1.,  1.,  0.,  0.,  1.,  1.,  1.,  1.,
        0.,  0.,  1.,  0.,  0.,  1.,  1.,  0.,  1.,  1.,  1.,  1.,  0.,
        1.,  1.,  1.,  0.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,
        1.,  1.,  1.,  0.,  1.,  0.,  1.,  1.,  1.,  0.,  1.,  1.,  0.,
        0.,  1.,  1.,  0.,  0.,  1.,  0.,  1.,  1.,  0.,  1.,  0.,  1.,
        0.,  0.,  0.,  1.,  0.,  1.,  1.,  0.,  1.,  1.,  0.,  0.,  1.,
        1.,  1.,  1.,  0.,  0.,  1.,  1.,  0.,  0.,  0.,  1.,  0.,  1.,
        1.,  0.,  0.,  1.,  1.,  1.,  0.,  1.,  1.,  0.,  1.,  0.,  0.,
        1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  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.,  0.,  0.,  1.,
        0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  1.,  0.])

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

h=5
tot=0
NumberInLargeRegion=np.zeros(13)

for l in range(1,13):
    print(LargerRegionIndToName[l])
    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 LargerRegionI[i]==l:          
            if GoodICAnat[i]==1:
                for k in range(3):
                    Fmap[:,:,k]=0.7*Dmmv*C[k]/np.max(C)
                Final_maps=Final_maps+Fmap
                #plt.plot(Time_fluoICA.T,(DT[:,i]/np.sqrt(np.var(DT[:,i]))-h*n+2),color=C/2)
                plt.plot((DT[:,i]/np.sqrt(np.var(DT[:,i]))-h*n+2),color=C/2)
                tot=tot+1
                print(i)
                n=n+1 
                    
    if n!=0:

        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)
        
    NumberInLargeRegion[l]=n


OL
9
35
52
71
99
106
109
120
122
125
VLNP
117
VMNP
AL
31
44
67
103
130
MB
12
13
14
17
18
26
36
49
56
66
72
76
83
90
114
121
131
133
LH
32
53
54
75
80
88
124
165
168
172
177
SNP
110
CX
45
58
100
127
LX
85
INP
PENP
2
30
GNG
1
16
19
21
25
37
38
41
47
48
50
57
65
69
73
79
86
94
96
97
104
105
116
132
140

In [32]:
# Output number of component per region
np.savetxt('/'.join(filename.split('/')[:-1])+'/NumberInLargeRegions.txt',NumberInLargeRegion)

In [ ]: