-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetSpatialDistribution.py
144 lines (124 loc) · 4.16 KB
/
getSpatialDistribution.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
import numpy as np
import sys
import os
import modelSetup
# setup data
try:
fileName = sys.argv[1]
except Exception:
raise Exception("Please enter path to log file")
try:
isImagedCells = bool(int(sys.argv[2]))
except Exception:
isImagedCells = False
try:
isPrintOutHubs = bool(int(sys.argv[3]))
except Exception:
isPrintOutHubs = False
try:
isInteractivePlot = bool(int(sys.argv[4]))
except Exception:
isInteractivePlot = False
if not isInteractivePlot:
import matplotlib
matplotlib.use('Agg') # Must be before importing matplotlib.pyplot or pylab! - Otherwise figures try to load but -X not necessarily on
import matplotlib.pyplot as plt
else:
import matplotlib.pyplot as plt
fileDir = os.path.dirname(fileName)
fileBase = os.path.basename(fileName)
if isImagedCells:
saveName = os.path.join(fileDir,'imagedSD_'+fileBase[:-4]+'.png')
else:
saveName = os.path.join(fileDir,'wholeSD_'+fileBase[:-4]+'.png')
# hubList (if have)
hubList = np.loadtxt(fileName,delimiter=',',dtype=int)
# get only imaged cells (if applicable)
if isImagedCells:
imagedCells = []
startName = "#imagedCells = "
with open(fileName,"r") as fi:
for ln in fi:
if ln.startswith(startName):
imagedCells = ln[len(startName):]
imagedCells = [int(x.strip()) for x in imagedCells.split(',')]
imagedHubs = []
startName = "#imagedHubs = "
with open(fileName,"r") as fi:
for ln in fi:
if ln.startswith(startName):
imagedHubs = ln[len(startName):]
imagedHubs = [int(x.strip()) for x in imagedHubs.split(',')]
#TODO: fix the index for those imaged cells only
hubList = imagedHubs
startName = "#silencedCell = "
with open(fileName,"r") as fi:
for ln in fi:
if ln.startswith(startName):
temp = ln[len(startName):]
silencedCell = [int(x.strip()) for x in temp.split(',')]
# get morphology info
startName = "#morphology = "
with open(fileName,"r") as fi:
for ln in fi:
if ln.startswith(startName):
temp = ln[len(startName):]
morphology = int(temp)
startName = "#species = "
with open(fileName,"r") as fi:
for ln in fi:
if ln.startswith(startName):
temp = ln[len(startName):]
species = int(temp)
startName = "#dthres = "
with open(fileName,"r") as fi:
for ln in fi:
if ln.startswith(startName):
temp = ln[len(startName):]
dthres = float(temp)
startName = "#isletsize = "
with open(fileName,"r") as fi:
for ln in fi:
if ln.startswith(startName):
temp = ln[len(startName):]
try:
isletsize = float(temp)
except Exception:
isletsize = None
if species==0:
#pathToCoupledMatrix = '../morphologies/mouse/CouplingMatrix-mouse40-3-175.dat'
#pathToCoupledMatrix = '../morphologies/mouse/CouplingMatrixMouse403.dat'
pathToMorphology = "../morphologies/mouse/Mouse 40-%d.txt"%morphology
elif species==1:
pathToMorphology = ""
elif species==2:
pathToMorphology = "../morphologies/cubic_lattice/cubic%d.txt"%morphology
######
## Import system setup files (.hoc files and system matrix)
######
#CoupledMatrix = np.loadtxt(pathToCoupledMatrix,delimiter=' ')#[-100:,-100:] #only taking last 100 cells
CoorData = np.loadtxt(pathToMorphology)
# process CoorData to be acceptable format in modelSetup.genCoupleMatrix()
CoorData = CoorData[CoorData[:,0]==11][:,1:4]
CoupledMatrix = modelSetup.genCoupleMatrix(CoorData,dthres,isletsize,True)
ncells = CoupledMatrix.shape[0]
if isImagedCells:
CoupledMatrix = CoupledMatrix[imagedCells,:][:,imagedCells]
numLinks = np.sum(CoupledMatrix+CoupledMatrix.T,1)
if isPrintOutHubs:
for i in hubList:
print("cell %d ; links %d"%(i,numLinks[i]))
######
## Plot the spatial connectivity distribution
######
plt.figure(1)
countNumLinks = np.histogram(numLinks, bins=np.arange(numLinks.min(), numLinks.max()+1)-0.5)[0]
labelname = "islet" if not isImagedCells else "imaged"
plt.plot(np.arange(numLinks.min(), numLinks.max()), countNumLinks, '-s',label=labelname)
plt.hist(numLinks[hubList],label='hubs')
plt.xlabel("#GJ to neighbouring cells")
plt.ylabel("#cells")
plt.legend()
plt.savefig(saveName)
if isInteractivePlot:
plt.show()