24 |
|
|
25 |
from types import StringType,IntType,FloatType,BooleanType,ListType,DictType |
from types import StringType,IntType,FloatType,BooleanType,ListType,DictType |
26 |
from sys import stdout |
from sys import stdout |
27 |
|
import numarray |
28 |
|
import operator |
29 |
import itertools |
import itertools |
30 |
# import modellib temporarily removed!!! |
# import modellib temporarily removed!!! |
31 |
|
|
484 |
elif isinstance(value, Link): |
elif isinstance(value, Link): |
485 |
value.toDom(document, val) |
value.toDom(document, val) |
486 |
param.appendChild(val) |
param.appendChild(val) |
487 |
|
elif isinstance(value, numarray.NumArray): |
488 |
|
shape = value.getshape() |
489 |
|
if isinstance(shape, tuple): |
490 |
|
size = reduce(operator.mul, shape) |
491 |
|
shape = ' '.join(map(str, shape)) |
492 |
|
else: |
493 |
|
size = shape |
494 |
|
shape = str(shape) |
495 |
|
|
496 |
|
arraytype = value.type() |
497 |
|
val.appendChild(dataNode(document, 'ArrayType', str(arraytype))) |
498 |
|
val.appendChild(dataNode(document, 'Shape', shape)) |
499 |
|
val.appendChild(dataNode(document, 'Data', ' '.join( |
500 |
|
[str(x) for x in numarray.reshape(value, size)]))) |
501 |
|
param.appendChild(val) |
502 |
elif isinstance(value,StringType): |
elif isinstance(value,StringType): |
503 |
param.appendChild(dataNode(document, 'Value', value)) |
param.appendChild(dataNode(document, 'Value', value)) |
504 |
else: |
else: |
513 |
""" |
""" |
514 |
Remove the empty nodes from the children of this node. |
Remove the empty nodes from the children of this node. |
515 |
""" |
""" |
516 |
return [x for x in node.childNodes |
ret = [] |
517 |
if not isinstance(x, minidom.Text) or x.nodeValue.strip()] |
for x in node.childNodes: |
518 |
|
if isinstance(x, minidom.Text): |
519 |
|
if x.nodeValue.strip(): |
520 |
|
ret.append(x) |
521 |
|
else: |
522 |
|
ret.append(x) |
523 |
|
return ret |
524 |
|
|
525 |
def _floatfromValue(doc): |
def _floatfromValue(doc): |
526 |
return float(doc.nodeValue.strip()) |
return float(doc.nodeValue.strip()) |
536 |
|
|
537 |
def _nonefromValue(doc): |
def _nonefromValue(doc): |
538 |
return None |
return None |
539 |
|
|
540 |
|
def _numarrayfromValue(doc): |
541 |
|
for node in doc: |
542 |
|
if node.tagName == 'ArrayType': |
543 |
|
arraytype = node.firstChild.nodeValue.strip() |
544 |
|
if node.tagName == 'Shape': |
545 |
|
shape = node.firstChild.nodeValue.strip() |
546 |
|
shape = [int(x) for x in shape.split()] |
547 |
|
if node.tagName == 'Data': |
548 |
|
data = node.firstChild.nodeValue.strip() |
549 |
|
data = [float(x) for x in data.split()] |
550 |
|
return numarray.reshape(numarray.array(data, type=getattr(numarray, arraytype)), |
551 |
|
shape) |
552 |
|
|
553 |
# Mapping from text types in the xml to methods used to process trees of that type |
# Mapping from text types in the xml to methods used to process trees of that type |
554 |
ptypemap = {"Simulation": Simulation.fromDom, |
ptypemap = {"Simulation": Simulation.fromDom, |
559 |
"int":_intfromValue, |
"int":_intfromValue, |
560 |
"str":_stringfromValue, |
"str":_stringfromValue, |
561 |
"bool":_boolfromValue, |
"bool":_boolfromValue, |
562 |
"NoneType":_nonefromValue |
"NoneType":_nonefromValue, |
563 |
} |
} |
564 |
|
|
565 |
# print doc.toxml() |
# print doc.toxml() |
576 |
|
|
577 |
if childnode.tagName == "Value": |
if childnode.tagName == "Value": |
578 |
nodes = _children(childnode) |
nodes = _children(childnode) |
579 |
pvalue = ptypemap[ptype](nodes[0]) |
if ptype == 'NumArray': |
580 |
|
pvalue = _numarrayfromValue(nodes) |
581 |
|
else: |
582 |
|
pvalue = ptypemap[ptype](nodes[0]) |
583 |
|
|
584 |
parameters[pname] = pvalue |
parameters[pname] = pvalue |
585 |
|
|