forked from rheiland/PhysiCell_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg2npy.py
168 lines (140 loc) · 4.71 KB
/
svg2npy.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
167
#
# svg2npy.py: convert svg (asccii) output files to numpy's "npy" (binary) formatted files.
#
# Input:
# SVG output files from PhysiCell: snapshot%08d.svg
#
# Output:
# xyzr_*.npy - cells' centers (x,y,z) and radii (r)
# rgb_*.npy - associated colors (RGB, normalized [0,1])
#
# Author: Randy Heiland
#
import sys
import glob
import os
import xml.etree.ElementTree as ET
import math
try:
import matplotlib
import matplotlib.colors as mplc
except:
print("\n---Error: cannot import matplotlib")
print("---Try: python -m pip install matplotlib")
print(join_our_list)
# print("---Consider installing Anaconda's Python 3 distribution.\n")
raise
try:
import numpy as np # if mpl was installed, numpy should have been too.
except:
print("\n---Error: cannot import numpy")
print("---Try: python -m pip install numpy\n")
print(join_our_list)
raise
from collections import deque
current_idx = 0
print("# args=",len(sys.argv)-1)
#for idx in range(len(sys.argv)):
use_defaults = True
show_nucleus = 0
current_idx = 0
step_value = 1
axes_min = 0.0
axes_max = 2000
#axes_max = 1000
print("current_idx=",current_idx)
def convert_svg():
global current_idx, axes_max, rvals, markers_size
# global vec_xyzr, vec_rgb
fname = "snapshot%08d.svg" % current_idx
if (os.path.isfile(fname) == False):
print("File does not exist: ",fname)
return
vec_xyzr = np.empty(shape=(0,4))
vec_rgb = np.empty(shape=(0,3))
xlist = deque()
ylist = deque()
rlist = deque()
rgb_list = deque()
# print('\n---- ' + fname + ':')
tree = ET.parse(fname)
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("keys=",child.attrib.keys())
# # if use_defaults and ('width' in child.attrib.keys()):
# axes_max = float(child.attrib['width'])
# print("--- found width --> axes_max =", axes_max)
# if child.text and "Current time" in child.text:
# svals = child.text.split()
# title_str = "(" + str(current_idx) + ") Current time: " + svals[2] + "d, " + svals[4] + "h, " + svals[7] + "m"
# title_str = "Current time: " + svals[2] + "d, " + svals[4] + "h, " + svals[7] + "m"
# 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')
cells_parent = None
for child in tissue_parent:
# print('attrib=',child.attrib)
if (child.attrib['id'] == 'cells'):
# print('-------- found cells, setting cells_parent')
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: # two circles in each child: outer + nucleus
# 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']
# print("s=",s)
# print("type(s)=",type(s))
if (s[0:3] == "rgb"): # if an rgb string, e.g. "rgb(175,175,80)"
rgb = list(map(int, s[4:-1].split(",")))
rgb[:]=[x/255. for x in rgb]
else: # otherwise, must be a color name
rgb_tuple = mplc.to_rgb(mplc.cnames[s]) # a tuple
rgb = [x for x in rgb_tuple]
# test for bogus x,y locations (rwh TODO: use max of domain?)
too_large_val = 10000.
if (math.fabs(xval) > too_large_val):
print("bogus xval=",xval)
break
yval = float(circle.attrib['cy'])
if (math.fabs(yval) > too_large_val):
print("bogus xval=",xval)
break
rval = float(circle.attrib['r'])
vec_xyzr = np.append(vec_xyzr, np.array([[xval,yval,0.0,rval]]), axis=0)
# print(xval,yval)
vec_rgb = np.append(vec_rgb, np.array([[rgb[0],rgb[1],rgb[2]]]), axis=0)
# print('rgb=',rgb[0],rgb[1],rgb[2])
# For .svg files with cells that *have* a nucleus, there will be a 2nd
if (show_nucleus == 0):
break
num_cells += 1
# if num_cells > 3: # for debugging
# print(fname,': num_cells= ',num_cells," --- debug exit.")
# sys.exit(1)
# break
print(fname,': num_cells= ',num_cells)
cell_file = "xyzr_%04d" % current_idx
rgb_file = "rgb_%04d" % current_idx
np.save(cell_file, vec_xyzr)
np.save(rgb_file, vec_rgb)
for current_idx in range(0,505):
convert_svg()