-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcell_tracks.py
138 lines (120 loc) · 3.42 KB
/
cell_tracks.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
#
# cell_tracks.py - plot 2-D cell tracks associated with PhysiCell .svg files
#
# Usage:
# python cell_tracks.py <max # of .svg frames>
#
# Dependencies include matplotlib and numpy. We recommend installing the Anaconda Python3 distribution.
#
# Examples (run from directory containing the .svg files):
# python cell_tracks.py 100
#
# Author: Randy Heiland
#
import sys
import xml.etree.ElementTree as ET
import numpy as np
import glob
import matplotlib.pyplot as plt
import math
#print(len(sys.argv))
if (len(sys.argv) < 2):
usage_str = "Usage: %s <max # svg frames>" % (sys.argv[0])
print(usage_str)
print("e.g.,")
eg_str = "%s 42" % (sys.argv[0])
print(eg_str)
exit(1)
else:
maxCount = int(sys.argv[1])
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 = 0
for fname in glob.glob('snapshot*.svg'):
#for fname in['snapshot00000000.svg', 'snapshot00000001.svg']:
#for fname in['snapshot00000000.svg']:
# print(fname)
count += 1
if count > maxCount:
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('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:
# print(' --- cx,cy=',circle.attrib['cx'],circle.attrib['cy'])
xval = float(circle.attrib['cx'])
# should we test for bogus x,y locations??
if (math.fabs(xval) > 10000.):
print("xval=",xval)
break
yval = float(circle.attrib['cy'])
if (math.fabs(yval) > 10000.):
print("xval=",xval)
break
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)
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)
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])
title_str = " max # SVG frames: " + str(maxCount)
plt.title(title_str)
plt.show()