.. _aggregationmodel-py: ################### aggregationmodel.py ################### :: >>> 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''' ... ... sum ... desc... ... simoaggr.py ... true ... ... ''' >>> 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 # doctest: +NORMALIZE_WHITESPACE 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'>> am = mb.obj['aggregation']['testxml']['sum'] >>> am.name 'sum' >>> am.language 'python' ************************************ class AggregationModelParam(object): ************************************ 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 def __init__(self, ns, elem, validator, model): =============================================== 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''' ... BA ... stratum ... stratum ... ... ... BA_u ... stratum ... ... ... B_BA ... stratum ... ... ... ''' >>> 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''' ... BA_L ... comp_unit ... ... ... ba_m2 ... tree ... ... ... n ... tree ... ... ... tree:d gt tree:d ... ''' >>> 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 # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE [('data', (3, 1, True)), ('data', (3, 1, True)), ('eq', )] Extract aggregation model with target vector, i.e. multiple targets instead of a single variable:: >>> model.vector_implementation = True >>> xml = u''' ... BA N D_gM ... H_gM ... ... comp_unit ... ... ... 0 0 0 0 ... ... ... ''' >>> 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''' ... BA ... stratum ... stratum ... ... ... BA_u ... stratum ... ... ... B_BA ... tree ... ... ... ''' >>> 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''' ... JUST_THINNED ... ... ... 0 ... ... ... ''' >>> 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''' ... BA N D_gM H_gM ... comp_unit ... ... ... 0 0 0 0 ... ... ... ''' >>> 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''' ... BA N D_gM H_gM ... comp_unit ... ... ... 0 0 0 0 0 ... ... ... ''' >>> 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''' ... BA N D_gM H_gM ... comp_unit ... ... ... 0 ... ... ... ''' >>> 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''' ... BA ... comp_unit ... ... ... 0 ... ... ... ''' >>> elem = etree.fromstring(xml) >>> task.chain_level = 'stratum' >>> 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 ''"]) ************************************** class AggregationModelOperand(object): ************************************** Aggregation model operand Attributes: - type: u'variable' or u'value' def __init__(self, ns, elem, validator): ======================================== Extract aggregation model operand defined in a model chain:: >>> from simo.builder.modelbase.aggregationmodel import AggregationModelOperand >>> xml = u''' ... ba ... n ... tree ... ''' >>> 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''' ... d h n sp ... ba ba ba ba ... tree ... ''' >>> 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''' ... 1 2 ... 3 4 ... ''' >>> 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''' ... d h n sp ... ba ba ba ... tree ... ''' >>> 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''' ... 1 2 3 X ... ''' >>> 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'"])