-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExperiment.py
343 lines (294 loc) · 13.9 KB
/
Experiment.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#-------------------------------------------------------------------#
# Copyright 2012-2013 Margriet Palm #
# #
# CC3DSimUtils is free software; you can redistribute #
# it and/or modify it under the terms of the GNU General Public #
# License as published by the Free Software Foundation; either #
# version 3 of the License, or (at your option) any later version. #
# #
# CC3DSimUtils is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
# General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with CC3DSimUtils; if not, write to the Free Software #
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA #
# 02110-1301 USA #
# #
#-------------------------------------------------------------------#
import xml.dom.minidom as xdm
class Experiment:
""" The class Experiments holds the data for a CC3D experiment as an xml object. This objects is created from a template xml file. Elements that are already present in the template can be changed or removed and new elements can be added. When the xml is written to file all unchanged content of the template, including commented xml, is written to the new file.
"""
def __init__(self,templatefile):
""" Instantiate experiment based on a given template
:param templatefile: path to xml template
"""
self.template = xdm.parse(templatefile)
self.cc3d = self.template.getElementsByTagName("CompuCell3D")[0]
self.potts = self.template.getElementsByTagName("Potts")[0]
self.plugins = self.template.getElementsByTagName("Plugin")
self.steppables = self.template.getElementsByTagName("Steppable")
#-- Private functions --#
def _setNodeValue(self,node,value):
""" Set value of an element.
:param node: xml element
:param value: value of xml element
"""
if len(node.childNodes) == 0:
child = self.template.createTextNode(str(value))
node.appendChild(child)
else:
node.childNodes[0].nodeValue = str(value)
def _getElement(self,name,parent):
""" Get element
:param name: element name
:param parent: parent node
:return: xml element
"""
if (len(parent.getElementsByTagName(name))==0):
element = self.template.createElement(name)
parent.appendChild(element)
else:
element = parent.getElementsByTagName(name)[0]
return element
def _getPluginByName(self,name):
""" Get CC3D plugin element by plugin name
:param name: name of the plugin
:return: xml element with plugin
"""
for plugin in self.plugins:
if plugin.getAttribute("Name") == name:
return plugin
plugin = self.template.createElement(name)
self.cc3d.appendChild(plugin)
return plugin
def _getSteppableByType(self,type):
""" Get CC3D steppable element by steppable type
:param type: type of the steppable
:return: xml element with steppable specification
"""
for steppable in self.steppables:
if steppable.getAttribute("Type") == type:
return steppable
steppable = self.template.createElement("Steppable")
self._setAttribute("Type",type,steppable)
self.cc3d.appendChild(steppable)
return steppable
def _setAttribute(self,name,value,parent):
""" Set attribute value of an xml element
:param name: attribute name
:param value: attribute value
:param parent: parent node
"""
if not parent.hasAttribute(name):
parent.setAttributeNode(self.template.createAttribute(str(name)))
parent.setAttribute(name,str(value))
def setPottsProperty(self,tagname,attributes={},value=None):
""" General function to set a parameter in the Potts element
:param tagname: name of the element
:param attributes: dictionary with attribute names as key and attribute values as values.
:param value: element value
"""
tag = self._getElement(tagname,self.potts)
for key,val in attributes.iteritems():
self._setAttribute(key, str(val), tag)
if value:
self._setNodeValue(tag,str(value))
def deletePottsProperty(self,tagname):
""" Remove element from the Potts element
:param tagname: name of element
"""
self.potts.removeChild(tagname)
def setGenericPlugin(self,name,elements=[]):
""" Set generic plugin. If the plugin is already in the template, it is updated. If not, the plugin is added to the model. This function does not support multi-level xml elements.
:param name: plugin name
:param elements: list of elements described by a dictionary: {'name':name,'value':val,'attributes':{}}
"""
# element = {'name':name,'value':val,'attributes':{}}
plugin = self._getPluginByName(name)
for e in elements:
element = self._getElement(e['name'], plugin)
if 'value' in e:
self._setNodeValue(element, e['value'])
if 'attributes' in e:
for key,val in e['attributes'].iteritems():
self._setAttribute(key, val, element)
def deletePlugin(self,name):
""" Delete plugin
:param name: plugin name
"""
self.cc3d.removeChild(self._getPluginByName(name))
def setGenericSteppable(self,type,freq=None,elements=[]):
""" Set generic steppable. If the steppable is already in the template, it is updated. If not, the plugin is added to the model. This function does not support multi-level xml elements.
:param type: steppable type
:param freq: frequency
:param elements: list of elements described by a dictionary with keys name, value and attributes
"""
# element = {'name':name,'value':val,'attributes':{}}
steppable = self._getSteppableByType(type)
if freq is not None:
steppable.setAttribute("Frequency",str(freq))
for e in elements:
element = self._getElement(e['name'], steppable)
if 'value' in e:
#~ if len(e['value'] > 0):
self._setNodeValue(element, str(e['value']))
if 'attributes' in e:
for key,val in e['attributes'].iteritems():
self._setAttribute(key, val, element)
def deleteSteppable(self,type):
""" Delete steppable
:param type: steppable type
"""
self.cc3d.removeChild(self._getSteppableByType(type))
#-- Functions for specific settings, plugins, etc --#
def setMultiCore(self,threads=1,cores=1):
""" Set number of threads and cores for a simulation
:param threads: number of threads per core
:param cores: number of cores
"""
vpu = self._getElement("VirtualProcessingUnits",self._getElement("Metadata",self.cc3d))
vpu.setAttribute("ThreadsPerVPU",str(threads))
self._setNodeValue(vpu,cores)
def setDebugFrequencyInMeta(self,freq):
""" Set debug frequency
:param frequency: frequency
"""
debug = self._getElement("DebugOutputFrequency",self._getElement("Metadata",self.cc3d))
self._setNodeValue(debug,freq)
def setSeed(self,seed):
""" Set simulation seed
:param seed: random seed
"""
self.setPottsProperty("RandomSeed",value=seed)
def setMCS(self,mcs):
""" Set number of Monte Carlo steps
:param mcs: number of MCS (note that mcs+1 appears in the xml)
"""
self.setPottsProperty("Steps",value=mcs+1)
def setTemp(self,T):
""" Set temperature tag
:param T: temperature
"""
self.template.getElementsByTagName("Temperature")[0].childNodes[0].nodeValue = str(T)
def setMotility(self,celltype,T):
""" Edit motility parameters per cell type
:param celltype: name of the cell type
:param T: motility
"""
mot = self._getElement("CellMotility", self.potts)
mottypes = mot.getElementsByTagName("MotilityParameters")
typeFound = False
for e in mottypes:
if e.getAttribute("CellType") == celltype:
e.setAttribute("Motility",str(T))
typeFound = True
break
if not typeFound:
element = self.template.createElement("MotilityParameters")
element.setAttribute("CellType",str(celltype))
element.setAttribute("Motility",str(T))
mot.appendChild(element)
def addStatistics(self,freq,basename):
""" Edit statistics save options
:param freq: save frequency
:param basename: basename for files (full path)
"""
func = self._getElement("EnergyFunctionCalculator",self.potts)
self._setAttribute("Type","Statistics",func)
out = self._getElement("OutputFileName",func)
self._setAttribute("Frequency", freq, out)
self._setNodeValue(out, basename)
def setVolume(self,celltype,vol,lam):
""" Set volume per cell type
:param celltype: cell type name
:param vol: target volume
:param lam: lambda volume
"""
steppable = self._getPluginByName("VolumeFlex")
elements = steppable.getElementsByTagName("VolumeEnergyParameters")
typeFound = False
for e in elements:
if celltype == e.getAttribute("CellType"):
self._setAttribute("TargetVolume",vol,e)
self._setAttribute("LambdaVolume",str(lam),e)
typeFound = True
break
if not typeFound:
element = self.template.createElement("VolumeEnergyParameters")
self._setAttribute("CellType",str(celltype),element)
self._setAttribute("TargetVolume",str(vol),element)
self._setAttribute("LambdaVolume",str(lam),element)
steppable.appendChild(element)
def setContact(self,_type1,_type2,J):
""" Edit contact energy
:param type1: name of the first cell type
:param type2: name of the second cell type
:param J: contact energy between type1 and type2
"""
plugin = self._getPluginByName("Contact")
energy = plugin.getElementsByTagName("Energy")
eFound = False
for rule in energy:
type1 = rule.getAttribute("Type1")
type2 = rule.getAttribute("Type2")
if ((type1 == _type1) and (type2 == _type2)) or ((type1 == _type2) and (type2 == _type1)):
rule.childNodes[0].nodeValue = str(J)
eFound = True
break
if not eFound:
element = self.template.createElement("Energy")
self._setAttribute("Type1",_type1,element)
self._setAttribute("Type2",_type2,element)
self._setNodeValue(element,J)
plugin.appendChild(element)
def setSecretion(self,celltype,s,solver="FastDiffusionSolver2DFE"):
""" Set secretion coefficient for specific cell type
:param celltype: name of the cell type
:param s: secretion coefficient
:param solver: solver name
"""
plugin = self._getSteppableByType("FastDiffusionSolver2DFE")
elements = plugin.getElementsByTagName("Secretion")
typeFound = False
for e in elements:
if e.getAttribute("Type") == celltype:
self._setNodeValue(e,str(s))
typeFound = True
break
if not typeFound:
sdata = plugin.getElementsByTagName("SecretionData")[0]
element = self.template.createElement("Secretion")
element.setAttribute("Type",str(celltype))
self._setNodeValue(element,str(s))
sdata.appendChild(element)
def setChemotaxis(self,celltype,towards,lam):
""" Set chemotaxis for cell type
:param celltype: name of the cell type
:param towards: cell type towards chemotaxis occurs
:param lam: chemotactic strength
"""
plugin = self._getPluginByName("Chemotaxis")
elements = plugin.getElementsByTagName("ChemotaxisByType")
typeFound = False
for e in elements:
if e.getAttribute("Type") == celltype:
e.setAttribute("ChemotactTowards",str(towards))
e.setAttribute("Lambda",str(lam))
typeFound = True
break
if not typeFound:
element = self.template.createElement("ChemotaxisByType")
element.setAttribute("Type",str(celltype))
e.setAttribute("ChemotactTowards",str(towards))
e.setAttribute("Lambda",str(lam))
plugin.getElementsByTagName("ChemicalField")[0].appendChild(element)
def write(self,filename):
""" Save xml to file
:param filename: filename of new xml
"""
f = open(filename,'w')
self.template.writexml(f)
f.close()