-
Notifications
You must be signed in to change notification settings - Fork 8
/
cell_anim.py
166 lines (142 loc) · 4.12 KB
/
cell_anim.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
#
# cell_anim.py - plot 2-D cells associated with PhysiCell .svg files
#
import sys
import xml.etree.ElementTree as ET
import numpy as np
import glob
import matplotlib.pyplot as plt
import math
from collections import deque
print(len(sys.argv))
if (len(sys.argv) < 3):
usage_str = "Usage: %s start end" % (sys.argv[0])
print(usage_str)
print("e.g.,")
eg_str = "%s 0 42" % (sys.argv[0])
print(eg_str)
sys.exit(1)
else:
startCount = int(sys.argv[1])
endCount = int(sys.argv[2])
d={} # dictionary to hold all (x,y) positions of cells
"""
--- for example ---
In [141]: d['cell1599'][0:3]
Out[141]:
array([[ 4900. , 4900. ],
[ 4934.17, 4487.91],
[ 4960.75, 4148.02]])
"""
count = -1
xlist = deque()
ylist = deque()
rlist = deque()
rgb_list = deque()
for fname in sorted(glob.glob('snapshot*.svg')):
#for fname in['snapshot00000000.svg', 'snapshot00000001.svg']:
#for fname in['snapshot00000000.svg']:
# print(fname)
count += 1
if count < startCount:
continue
if count > endCount:
break
print('\n---- ' + fname + ':')
tree=ET.parse(fname)
# print('--- root.tag, root.attrib ---')
root=tree.getroot()
# print('--- root.tag ---')
# print(root.tag)
# print('--- root.attrib ---')
# print(root.attrib)
# print('--- child.tag, child.attrib ---')
numChildren = 0
for child in root:
print(child.tag, child.attrib)
# print("width ",child.attrib['width'])
# print('attrib=',child.attrib)
# if (child.attrib['id'] == 'tissue'):
if ('id' in child.attrib.keys()):
# print('-------- found tissue!!')
tissue_parent = child
break
# print('------ search tissue')
for child in tissue_parent:
# print('attrib=',child.attrib)
if (child.attrib['id'] == 'cells'):
# print('-------- found cells!!')
cells_parent = child
break
numChildren += 1
num_cells = 0
# print('------ search cells')
for child in cells_parent:
# print(child.tag, child.attrib)
# print('attrib=',child.attrib)
for circle in child:
# circle.attrib={'cx': '1085.59','cy': '1225.24','fill': 'rgb(159,159,96)','r': '6.67717','stroke': 'rgb(159,159,96)','stroke-width': '0.5'}
# print(' --- cx,cy=',circle.attrib['cx'],circle.attrib['cy'])
xval = float(circle.attrib['cx'])
s = circle.attrib['fill']
rgb = list(map(int, s[4:-1].split(","))) # using Py3
rgb[:]=[x/255. for x in rgb]
rgb_list.append(rgb)
if (num_cells < 25):
print(s)
print(rgb)
# should we test for bogus x,y locations??
if (math.fabs(xval) > 10000.):
print("xval=",xval)
break
xlist.append(xval)
yval = float(circle.attrib['cy'])
if (math.fabs(yval) > 10000.):
print("xval=",xval)
break
ylist.append(yval)
rval = float(circle.attrib['r'])
rlist.append(rval)
# if (child.attrib['id'] in d.keys()):
# d[child.attrib['id']] = np.vstack((d[child.attrib['id']],
# [ float(circle.attrib['cx']), float(circle.attrib['cy']) ]))
# else:
# d[child.attrib['id']] = np.array( [ float(circle.attrib['cx']), float(circle.attrib['cy']) ])
break
# if (child.attrib['id'] == 'cells'):
# print('-------- found cells!!')
# tissue_child = child
num_cells += 1
print(fname,': num_cells= ',num_cells)
xvals=np.array(xlist)
yvals=np.array(ylist)
rvals=np.array(rlist)
rgbs=np.array(rgb_list)
print("xvals[0:5]=",xvals[0:5])
print("rvals[0:5]=",rvals[0:5])
print("rvals.min, max=",rvals.min(),rvals.max())
fig = plt.figure(figsize=(8,8))
ax = fig.gca()
ax.set_aspect("equal")
#ax.set_xticks([])
#ax.set_yticks([]);
#ax.set_xlim(0, 8); ax.set_ylim(0, 8)
#print 'dir(fig)=',dir(fig)
#fig.set_figwidth(8)
#fig.set_figheight(8)
plt.scatter(xvals,yvals, s=rvals*0.9, c=rgbs)
plt.xlim(0,2000) # TODO - get these values from width,height in .svg at top
plt.ylim(0,2000)
"""
for key in d.keys():
if (len(d[key].shape) == 2):
x = d[key][:,0]
y = d[key][:,1]
plt.plot(x,y)
else:
print(key, " has no x,y points")
# print(" d[",key,"].shape=", d[key].shape)
# print(" d[",key,"].size=", d[key].size)
# print( d[key])
"""
plt.show()