>>> class Lexicon(object):
... def __init__(self):
... self.models = {}
... def get_variable_ind(self, level, variable, active=False):
... if level == 'comp_unit': return (0,1)
... elif level == 'stratum': return (1,1)
... else: return (2,1)
... def get_level_ind(self, level):
... if level == 'comp_unit': return 0
... elif level == 'stratum': return 1
... else: return 2
... def get_variable_name(self, level, variable):
... return 'VARNAME'
... def get_level_name(self, level):
... return 'LEVEL%d' % level
... def is_parent(self, child, parent):
... return False
... def add_model(self, mtype, mname):
... if mtype not in self.models:
... self.models[mtype] = set()
... self.models[mtype].add(mname)
>>> from simo.builder.modelbase.modelbase import ModelbaseDef
>>> tdf = open('../../simulator/xml/schemas/Typedefs_SIMO.xsd')
>>> typedef = tdf.read()
>>> tdf.close()
>>> sf = open('../../simulator/xml/schemas/aggregation_modelbase.xsd')
>>> schema = sf.read()
>>> sf.close()
>>> xml = u'''<aggregation_base xmlns="http://www.simo-project.org/simo"
... xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
... <model>
... <name>sum</name>
... <description>desc...</description>
... <implemented_at>simoaggr.py</implemented_at>
... <vector_implementation>true</vector_implementation>
... </model>
... </aggregation_base>'''
>>> mb = ModelbaseDef(typedef)
>>> try:
... mb.schema = schema
... except ValueError, e:
... print e
schema type and schema text must be passed
>>> mb.schema = ('aggregation', schema)
>>> try:
... mb.xml = ('aggregation', 'testxml', xml)
... except ValueError, e:
... print e
xml name, xml content, lexicon instance, model directory path and model
type must be passed
>>> mb.xml = ('testxml', xml, Lexicon(), '', 'aggregation')
>>> mb.xml['aggregation']['testxml'][:17]
u'<aggregation_base'
Aggregation model
Properties:
Parses the XML data into model instance attributes:
>>> am = mb.obj['aggregation']['testxml']['sum']
>>> am.name
'sum'
>>> am.language
'python'
Aggregation model parameters
Attributes:
- target_ind: model target indice (level, variable)
- through_level: model aggregation level
- operands: aggregation model operands
- condition: aggregation model condition
- model: aggregation model object
Extract aggregation model parameters defined in the model chain
>>> from minimock import Mock
>>> execfile('builder/modelbase/test/mocktask.py')
>>> from lxml import etree
>>> from simo.builder.modelbase.aggregationmodel import AggregationModelParam
>>> xml = u'''<aggregation>
... <target>BA</target>
... <level>stratum</level>
... <through_level>stratum</through_level>
... <operands>
... <operand>
... <variable>BA_u</variable>
... <level>stratum</level>
... </operand>
... <operand>
... <variable>B_BA</variable>
... <level>stratum</level>
... </operand>
... </operands>
... </aggregation>'''
>>> elem = etree.fromstring(xml)
>>> ns = ''
>>> model = Mock('AggregationModel')
>>> model.vector_implementation = False
>>> mp = AggregationModelParam(ns, elem, task, model)
>>> mp.target_ind
(2, 1)
>>> mp.through_level
2
>>> len(mp.operands)
2
Extract aggregation parameters with a condition
>>> xml = u'''<aggregation>
... <target>BA_L</target>
... <through_level>comp_unit</through_level>
... <operands>
... <operand>
... <variable>ba_m2</variable>
... <level>tree</level>
... </operand>
... <operand>
... <variable>n</variable>
... <level>tree</level>
... </operand>
... </operands>
... <condition>tree:d gt tree:d</condition>
... </aggregation>'''
>>> elem = etree.fromstring(xml)
>>> task.chain_level = 'tree'
>>> mp = AggregationModelParam(ns, elem, task, model)
>>> mp.target_ind
(3, 1)
>>> mp.through_level
1
>>> len(mp.operands)
2
>>> mp.condition
[('data', (3, 1, True)), ('data', (3, 1, True)),
('eq', <function gte at ...>)]
Extract aggregation model with target vector, i.e. multiple targets instead of a single variable:
>>> model.vector_implementation = True
>>> xml = u'''<aggregation>
... <target> BA N D_gM
... H_gM
... </target>
... <level>comp_unit</level>
... <operands>
... <operand>
... <value> 0 0 0 0 </value>
... </operand>
... </operands>
... </aggregation>'''
>>> elem = etree.fromstring(xml)
>>> task.chain_level = 'comp_unit'
>>> mp = AggregationModelParam(ns, elem, task, model)
>>> mp.target_ind
(1, [1, 1, 1, 1])
>>> mp.scalar
False
>>> task.validator.errors
set([])
Extract aggregation parameters with invalid values
>>> xml = u'''<aggregation>
... <target>BA</target>
... <level>stratum</level>
... <through_level>stratum</through_level>
... <operands>
... <operand>
... <variable>BA_u</variable>
... <level>stratum</level>
... </operand>
... <operand>
... <variable>B_BA</variable>
... <level>tree</level>
... </operand>
... </operands>
... </aggregation>'''
>>> elem = etree.fromstring(xml)
>>> task.chain_level = 'stratum'
>>> mp = AggregationModelParam(ns, elem, task, model)
>>> task.validator.errors
set(["aggregation operands at different levels for task 'test task'"])
>>> xml = u'''<aggregation>
... <target>JUST_THINNED</target>
... <operands>
... <operand>
... <value>0</value>
... </operand>
... </operands>
... </aggregation>'''
>>> elem = etree.fromstring(xml)
>>> task.chain_level = 'stratum'
>>> mp = AggregationModelParam(ns, elem, task, model)
>>> oper = mp.operands[0]
>>> oper.value
0.0
>>> oper.type
'value'
Process aggregation definition for model without vector implementation:
>>> xml = u'''<aggregation>
... <target> BA N D_gM H_gM </target>
... <level>comp_unit</level>
... <operands>
... <operand>
... <value> 0 0 0 0</value>
... </operand>
... </operands>
... </aggregation>'''
>>> elem = etree.fromstring(xml)
>>> task.chain_level = 'comp_unit'
>>> task.validator.errors = set([])
>>> model.vector_implementation = False
>>> model.name = 'mock_model'
>>> mp = AggregationModelParam(ns, elem, task, model)
>>> task.validator.errors
set(["multiple targets not allowed for aggregation model 'mock_model', model lacks vector implementation!"])
Process aggregation definition with unequal number of target variables and operands:
>>> xml = u'''<aggregation>
... <target> BA N D_gM H_gM </target>
... <level>comp_unit</level>
... <operands>
... <operand>
... <value> 0 0 0 0 0</value>
... </operand>
... </operands>
... </aggregation>'''
>>> elem = etree.fromstring(xml)
>>> task.chain_level = 'comp_unit'
>>> task.validator.errors = set([])
>>> model.vector_implementation = True
>>> mp = AggregationModelParam(ns, elem, task, model)
>>> task.validator.errors
set(['unequal value vector and target variable vector lengths!'])
Process invalid aggregation definition with mixed scalar and vector definitions:
>>> xml = u'''<aggregation>
... <target> BA N D_gM H_gM </target>
... <level>comp_unit</level>
... <operands>
... <operand>
... <value>0</value>
... </operand>
... </operands>
... </aggregation>'''
>>> elem = etree.fromstring(xml)
>>> task.chain_level = 'comp_unit'
>>> task.validator.errors = set([])
>>> mp = AggregationModelParam(ns, elem, task, model)
>>> task.validator.errors
set(['target variable and operands must be either both scalar or both vector!'])
Process aggregation definition with invalid evaluation and target levels:
>>> xml = u'''<aggregation>
... <target>BA</target>
... <level>comp_unit</level>
... <operands>
... <operand>
... <value>0</value>
... </operand>
... </operands>
... </aggregation>'''
>>> elem = etree.fromstring(xml)
>>> task.chain_level = 'stratum'
>>> task.path = '<my_task_path>'
>>> task.validator.errors = set([])
>>> mp = AggregationModelParam(ns, elem, task, model)
>>> task.validator.warnings
set(["invalid target level 'comp_unit' - parent of evaluation level 'stratum' in task '<my_task_path>'"])
Aggregation model operand
Attributes:
Extract aggregation model operand defined in a model chain:
>>> from simo.builder.modelbase.aggregationmodel import AggregationModelOperand
>>> xml = u'''<operand>
... <variable default="0.0">ba</variable>
... <weight default="1.0">n</weight>
... <level>tree</level>
... </operand>'''
>>> elem = etree.fromstring(xml)
>>> op = AggregationModelOperand(ns, elem, task.validator)
>>> op.type
'variable'
>>> op.variable
(3, 1)
>>> op.weight
(3, 1)
>>> op.default_value
0.0
>>> op.default_weight
1.0
Extract aggregation model operand with variable vector:
>>> xml = u'''<operand>
... <variable>d h n sp</variable>
... <weight> ba ba ba ba</weight>
... <level>tree</level>
... </operand>'''
>>> elem = etree.fromstring(xml)
>>> op = AggregationModelOperand(ns, elem, task.validator, False, 4)
>>> op.type
'variable'
>>> op.level
'tree'
>>> op.variable
(3, [1, 1, 1, 1])
>>> op.weight
(3, [1, 1, 1, 1])
Extract aggregation model operand with value vector:
>>> xml = u'''<operand>
... <value> 1 2
... 3 4 </value>
... </operand>'''
>>> elem = etree.fromstring(xml)
>>> task.validator.errors = set([])
>>> op = AggregationModelOperand(ns, elem, task.validator, False, 4)
>>> op.type
'value'
>>> op.value
array([ 1., 2., 3., 4.])
>>> task.validator.errors
set([])
Extract variable vector operand with inequal number of variable and weight variables:
>>> xml = u'''<operand>
... <variable>d h n sp</variable>
... <weight>ba ba ba</weight>
... <level>tree</level>
... </operand>'''
>>> elem = etree.fromstring(xml)
>>> task.validator.errors = set([])
>>> op = AggregationModelOperand(ns, elem, task.validator, False, 4)
>>> task.validator.errors
set(['variable vector and weight vector must contain equal number of variables!'])
Extract aggregation model operand with non-numerical values in value vector:
>>> xml = u'''<operand>
... <value>1 2 3 X</value>
... </operand>'''
>>> elem = etree.fromstring(xml)
>>> task.validator.errors = set([])
>>> op = AggregationModelOperand(ns, elem, task.validator, False, 4)
>>> task.validator.errors
set(["invalid value vector '1 2 3 X'"])