Table Of Contents

Previous topic

smt.py

Next topic

sim.py

This Page

condeval.py

class ConditionEvaluator(object):

SIMO simulator condition evaluator:

>>> import numpy
>>> import datetime as dt
>>> from minimock import Mock
>>> from simo.simulation.condeval import ConditionEvaluator
>>> add_error = Mock('simulator.add_error')  # mock error logging method
>>> ce = ConditionEvaluator(add_error)

def evaluate(self, sim, tind, expr, level, main_level, depth, opmem, throughlevel=None):

Evaluate conditional expression. Values are retrieved from data matrix using simo.builder.matrix.handler.Handler instance.:

>>> tind = numpy.array([[0,0,0], [0,0,1], [0,0,2], [0,0,3], [0,0,4]])
>>> data = Mock('Data')
>>> data.get_tind.mock_returns = numpy.array([[0,0,0], [0,0,1], [0,0,2],
...                                           [0,0,3], [0,0,4]]), set([])
>>> data.get_value.mock_returns = numpy.arange(5, dtype=float), None
>>> ret = [dt.date(2000,1,1) for i in range(5)]
>>> data.get_date.mock_returns = numpy.array(ret, dtype=dt.date)
>>> data.get_object_map.mock_returns = numpy.array([False, False, False,
...                                                 False],
...                                                 dtype=bool)
>>> opmem = Mock('OperationMemory')
>>> opmem.since.mock_returns = numpy.array([0,11,5,10,19], dtype=int)
>>> opmem.times.mock_returns = numpy.array([1,0,1,2,1], dtype=int)
>>> timespan = Mock('Timespan')
>>> timespan.time_step = 1
>>> timespan.unit = 'year'

Import chainfuncs from builder

>>> from simo.builder.modelchain.chainfunc import *

Evaluate condition ‘comp_unit:Age gt 1.0’:

>>> expr = [('data', (1, 1, True)), ('value', 1.0), ('eq', gte)]
>>> ce.evaluate(data, tind, expr, 1, 1, 0, opmem, timespan)
Called Data.get_active()
Called Data.set_active(None)
Called Data.get_value(
    array([[0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 4]]),
    1)
array([False, False,  True,  True,  True], dtype=bool)

Evaluate condition ‘comp_unit:Age eq comp_unit:dummy_age’

>>> expr = [('data', (1, 1, True)),
...         ('data', (1, 1, True)),
...         ('eq', eee)]
>>> ce.evaluate(data, tind, expr, 1, 1, 0, opmem, timespan)
Called Data.get_active()
Called Data.set_active(None)
Called Data.get_value(
    array([[0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 4]]),
    1)
Called Data.set_active(None)
Called Data.get_value(
    array([[0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 4]]),
    1)
array([ True,  True,  True,  True,  True], dtype=bool)

Evaluate condition ‘comp_unit:thinning times_eq 1.0’

>>> expr = [('op', 1), ('value', 1.0), ('times', eee)]
>>> ce.evaluate(data, tind, expr, 1, 1, 0, opmem, timespan)
Called Data.get_active()
Called OperationMemory.times(
    array([[0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 4]]),
    1)
array([ True, False,  True, False,  True], dtype=bool)

Evaluate condition ‘comp_unit:thinning since_gt 10.0’

>>> expr = [('op', 1), ('value', 10), ('since', sgt)]
>>> ce.evaluate(data, tind, expr, 1, 1, 0, opmem, timespan) 
Called Data.get_active()
Called Data.get_date(
    array([[0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 4]]))
Called OperationMemory.since(
    array([[0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 4]]),
    1,
    array([...2000...12...31..., ...2000...12...31..., ...2000...12...31...,
           ...2000...12...31..., ...2000...12...31...], dtype=object),
    'year')
array([False,  True, False, False,  True], dtype=bool)

Evaluate condition ‘comp_unit:thinning since_gt 10.0 and comp_unit:thinning times_eq 1.0’

>>> expr = [('op', 1), ('value', 10), ('since', sgt),\
...         ('op', 1), ('value', 1), ('times', eee),
...         ('group', and_)]
>>> ce.evaluate(data, tind, expr, 1, 1, 0, opmem, timespan) 
Called Data.get_active()
Called Data.get_date(
    array([[0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 4]]))
Called OperationMemory.since(
    array([[0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 4]]),
    1,
    array([...2000...12...31..., ...2000...12...31..., ...2000...12...31...,
           ...2000...12...31..., ...2000...12...31...], dtype=object),
    'year')
Called OperationMemory.times(
    array([[0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 4]]),
    1)
array([False, False, False, False,  True], dtype=bool)

Evaluate condition ‘comp_unit:thinning since_gt 10.0 or comp_unit:thinning times_eq 1.0’

>>> expr = [('op', 1), ('value', 10), ('since', sgt),
...         ('op', 1), ('value', 1), ('times', eee),
...         ('group', or_)]
>>> ce.evaluate(data, tind, expr, 1, 1, 0, opmem, timespan) 
Called Data.get_active()
Called Data.get_date(
    array([[0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 4]]))
Called OperationMemory.since(
    array([[0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 4]]),
    1,
    array([...2000...12...31..., ...2000...12...31..., ...2000...12...31...,
           ...2000...12...31..., ...2000...12...31...], dtype=object),
    'year')
Called OperationMemory.times(
    array([[0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 4]]),
    1)
array([ True,  True,  True, False,  True], dtype=bool)

Evaluate condition ‘comp_unit exists’

>>> expr = [('level', 1), None, ('ex', ext)]
>>> tind = numpy.array([[0,0,1,0,0],
...                     [0,0,2,0,0],
...                     [0,0,3,0,0],
...                     [0,0,6,0,0]], dtype=int)
>>> ce.evaluate(data, tind, expr, 1, 1, 0, opmem, timespan)
Called Data.get_active()
Called Data.set_active(None)
Called Data.get_object_map(
    1,
    array([[0, 0, 1, 0, 0],
       [0, 0, 2, 0, 0],
       [0, 0, 3, 0, 0],
       [0, 0, 6, 0, 0]]))
array([False, False, False, False], dtype=bool)

Evaluate condition ‘stratum:BA not exists’

>>> expr = [('data', (1,1,True)), None, ('ex', n_ext)]
>>> ce.evaluate(data, tind, expr, 1, 1, 0, opmem, timespan)
Called Data.get_active()
Called Data.set_active(None)
Called Data.get_exists(
    array([[0, 0, 1, 0, 0],
       [0, 0, 2, 0, 0],
       [0, 0, 3, 0, 0],
       [0, 0, 6, 0, 0]]),
    1)
False

Evaluate condition ‘tree:d gt self:d’

>>> expr = [('data', (2, 1, True)),
...         ('data', (2, 1, False)),
...         ('eq', gte)]
>>> ce.evaluate(data, tind, expr, 1, 1, 0, opmem, timespan)
Called Data.get_active()
Called Data.get_tind(1, 0, 2, None, True, None)
Called Data.get_active()
Called Data.get_value(
    array([[0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 4]]),
    1)
Called Data.get_tind(1, 0, 2, None, False, None)
Called Data.get_active()
Called Data.get_value(
    array([[0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 4]]),
    1)
array([False, False, False, False, False], dtype=bool)

Evaluate condition ‘tree:d ex’

>>> expr = [('data', (2, 1, True)),
...         None,
...         ('ex', ext)]
>>> ce.evaluate(data, tind, expr, 1, 1, 0, opmem, timespan)
Called Data.get_active()
Called Data.get_tind(1, 0, 2, None, True, None)
Called Data.get_active()
Called Data.get_exists(
    array([[0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 4]]),
    1)

Evaluate condition ‘function:<mock_func>’

>>> tind = numpy.array([[0, 0, 0, 0, 0], [0, 0, 1, 0, 0]])
>>> expr = [('function', Mock('mock_func'))]
>>> str(ce.evaluate(data, tind, expr, 1, 1, 0, opmem, timespan))
Called Data.get_active()
Called mock_func(2)
'None'

Evaluate condition ‘function:random_number_0_1’

>>> expr = [('function', functions['random_number_0_1'])]
>>> result = ce.evaluate(data, tind, expr, 1, 1, 0, opmem, timespan)
Called Data.get_active()
>>> for i in result: 0.0 <= i <= 1.0
True
True
>>> result 
array([ ...,  ...])

Evaluate condition ‘function:random_number_0_1 gt 0.0’

>>> expr = [('function', functions['random_number_0_1']),
...         ('value', 0.0),
...         ('eq', gte)]
>>> ce.evaluate(data, tind, expr, 1, 1, 0, opmem, timespan)
Called Data.get_active()
array([ True,  True], dtype=bool)

Evaluate nested logical conditions - ‘(comp_unit:SC eq 1 and comp_unit:PEAT eq 2) and (comp_unit:TS gt 1100 and comp_unit:MAIN_SP eq 3)’

>>> tind = numpy.array([[0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 2, 0, 0]])
>>> data2 = Mock('Data')
>>> vals = [(numpy.array([1.,1.,2.], dtype=float), None),
...         (numpy.array([2.,2.,1.], dtype=float), None),
...         (numpy.array([1200.,1200.,1000.], dtype=float), None),
...         (numpy.array([3.,2.,5.], dtype=float), None)]
>>> data2.get_value.mock_returns_iter = iter(vals)
>>> data2.get_tind.mock_returns = (None, set([]),)
>>> expr = [('data', (1, 'SC', True)),
...         ('value', 1.0),
...         ('eq', eee),
...         ('data', (1, 'PEAT', True)),
...         ('value', 2.0),
...         ('eq', eee),
...         ('group', and_),
...         ('data', (1, 'TS', True)),
...         ('value', 1100.0),
...         ('eq', gte),
...         ('data', (1, 'MAIN_SP', True)),
...         ('value', 3.0),
...         ('eq', eee),
...         ('group', and_),
...         ('group', and_)]
>>> ce.evaluate(data2, tind, expr, 1, 1, 0, opmem, timespan)
Called Data.get_active()
Called Data.set_active(None)
Called Data.get_value(
    array([[0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 2, 0, 0]]),
    'SC')
Called Data.set_active(None)
Called Data.get_value(
    array([[0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 2, 0, 0]]),
    'PEAT')
Called Data.set_active(None)
Called Data.get_value(
    array([[0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 2, 0, 0]]),
    'TS')
Called Data.set_active(None)
Called Data.get_value(
    array([[0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 2, 0, 0]]),
    'MAIN_SP')
array([ True, False, False], dtype=bool)

Evaluate nested logical conditions - ‘comp_unit:SC eq 1 and comp_unit:PEAT eq 2 and comp_unit:TS gt 1100 and comp_unit:MAIN_SP eq 3’

>>> data2 = Mock('Data')
>>> vals = [(numpy.array([1.,1.,2.], dtype=float), None),
...         (numpy.array([2.,2.,1.], dtype=float), None),
...         (numpy.array([1200.,1200.,1000.], dtype=float), None),
...         (numpy.array([3.,2.,5.], dtype=float), None)]
>>> data2.get_value.mock_returns_iter = iter(vals)
>>> data2.get_tind.mock_returns = (None, set([]),)
>>> expr = [('data', (1, 'SC', True)),
...         ('value', 1.0),
...         ('eq', eee),
...         ('data', (1, 'PEAT', True)),
...         ('value', 2.0),
...         ('eq', eee),
...         ('group', and_),
...         ('data', (1, 'TS', True)),
...         ('value', 1100.0),
...         ('eq', gte),
...         ('group', and_),
...         ('data', (1, 'MAIN_SP', True)),
...         ('value', 3.0),
...         ('eq', eee),
...         ('group', and_)]
>>> ce.evaluate(data2, tind, expr, 1, 1, 0, opmem, timespan)
Called Data.get_active()
Called Data.set_active(None)
Called Data.get_value(
    array([[0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 2, 0, 0]]),
    'SC')
Called Data.set_active(None)
Called Data.get_value(
    array([[0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 2, 0, 0]]),
    'PEAT')
Called Data.set_active(None)
Called Data.get_value(
    array([[0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 2, 0, 0]]),
    'TS')
Called Data.set_active(None)
Called Data.get_value(
    array([[0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 2, 0, 0]]),
    'MAIN_SP')
array([ True, False, False], dtype=bool)

Evaluate nested logical conditions - ‘((comp_unit:SC eq 1 and comp_unit:PEAT eq 2) and comp_unit:TS gt 1100) and comp_unit:MAIN_SP eq 3’

>>> data2 = Mock('Data')
>>> vals = [(numpy.array([1.,1.,2.], dtype=float), None),
...         (numpy.array([2.,2.,1.], dtype=float), None),
...         (numpy.array([1200.,1200.,1000.], dtype=float), None),
...         (numpy.array([3.,2.,5.], dtype=float), None)]
>>> data2.get_value.mock_returns_iter = iter(vals)
>>> data2.get_tind.mock_returns = (None, set([]),)
>>> expr = [('data', (1, 'SC', True)),
...         ('value', 1.0),
...         ('eq', eee),
...         ('data', (1, 'PEAT', True)),
...         ('value', 2.0),
...         ('eq', eee),
...         ('group', and_),
...         ('data', (1, 'TS', True)),
...         ('value', 1100.0),
...         ('eq', gte),
...         ('group', and_),
...         ('data', (1, 'MAIN_SP', True)),
...         ('value', 3.0),
...         ('eq', eee),
...         ('group', and_)]
>>> ce.evaluate(data2, tind, expr, 1, 1, 0, opmem, timespan)
Called Data.get_active()
Called Data.set_active(None)
Called Data.get_value(
    array([[0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 2, 0, 0]]),
    'SC')
Called Data.set_active(None)
Called Data.get_value(
    array([[0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 2, 0, 0]]),
    'PEAT')
Called Data.set_active(None)
Called Data.get_value(
    array([[0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 2, 0, 0]]),
    'TS')
Called Data.set_active(None)
Called Data.get_value(
    array([[0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 2, 0, 0]]),
    'MAIN_SP')
array([ True, False, False], dtype=bool)

Evaluate condition so that data handler returns errors

>>> tind = numpy.array([[0,0,0], [0,0,1], [0,0,2], [0,0,3], [0,0,4]])
>>> tind1 = numpy.array([[0, 0, 0, 0, 1], [0, 0, 1, 1, 2]], dtype=int)
>>> tind2 = numpy.array([[0, 0, 0, 0, 1], [0, 0, 1, 1, 2],
...                      [0, 0, 2, 2, 3], [0, 0, 3, 3, 4]], dtype=int)
>>> err = [(numpy.arange(5, dtype=float), ([('error message', [0])],
...         tind1, [0])),
...        (numpy.arange(5, dtype=float), ([('error message', [1,3])],
...         tind2, [1,3]))]
>>> data = Mock('Data')
>>> data.get_value.mock_returns_iter = iter(err)
>>> data.get_tind.mock_returns = numpy.array([[0,0,0], [0,0,1], [0,0,2],
...                                           [0,0,3], [0,0,4]]), set([])
>>> expr = [('data', (1, 1, True)),
...         ('data', (1, 1, True)),
...         ('eq', eee)]
>>> ce.evaluate(data, tind, expr, 1, 1, 0, opmem, timespan)
...     
Called Data.get_active()
Called Data.set_active(None)
Called Data.get_value(
    array([[0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 4]]),
    1)
Called Data.set_active(None)
Called Data.get_value(
    array([[0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 4]]),
    1)
Called simulator.add_error(
    'error message when evaluating condition',
    None,
    array([[0, 0, 0, 0, 1]]))
Called simulator.add_error(
    'error message when evaluating condition',
    None,
    array([[0, 0, 1, 1, 2],
       [0, 0, 3, 3, 4]]))
array([False, False,  True, False,  True], dtype=bool)