Table Of Contents

Previous topic

brancher.py

Next topic

ind2id.py

This Page

handler.py

>>> from minimock import Mock

class Handler(object):

Matrix handler class for accessing data during simulation. Keeps the data matrix and the object linking information consistent.

def __init__(self, iterations, lexicon, attributes, mcdepth, mainlevel, branchgroups, noopbranch, separate_branching_groups, sim, objcount=50, branchcount=1, branch_limit=None):

Initialize a data handler taking the lexicon with the hierarchy description; object index to data hierarchy level ordinal and object index to data lineage information mapping ; and number of attributes as input:

>>> hr = {}
>>> hr[0] = {'ordinal':0, 'lineage':set([0]), 'parent': None}
>>> hr[1] = {'ordinal':1, 'lineage':set([0]), 'parent': 0}
>>> hr[2] = {'ordinal':2, 'lineage':set([0]), 'parent': 1}
>>> lexicon = Mock('Lexicon')
>>> sim = Mock('simulator')
>>> lexicon.hierarchy = hr
>>> lexicon.get_variable_name.mock_returns = "VARNAME"
>>> lexicon.get_level_name.mock_returns = "LEVNAME"
>>> from simo.matrix.handler import Handler

>>> bgroups = {}
>>> h = Handler(1, lexicon, 10, 1, 1, bgroups, False, False, sim, 0)

>>> bgroups = {'group1': [[(None, {'task1': ['branch1']})], True]}
>>> h = Handler(1, lexicon, 10, 1, 1, bgroups, False, False, sim, 0)

def add_objects(self, iteration, branch, level, num, parlev=None, parind=None, obj_ids=None):

Add number of objects to given branch and level in the data matrix. Optionally take the parent level and object indices and a list of object ids as input. Return a target index (similar as from get_tind) of the created object indices:

>>> obj_ids = [('SIMULATION', 'SIMULATION')]
>>> h.add_objects(0, 0, 0, 1, None, None, obj_ids) 
array([[0, 0, 0, 0, 0]]...)
>>> obj_ids = [('UID001', 'UID001'), ('UID002', 'UID002'),
...            ('UID003', 'UID003')]
>>> h.add_objects(0, 0, 1, 3, 0, 0, obj_ids) 
array([[0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 2, 0, 0]]...)
>>> h.linkage.links[(0,0)][(0,0)]
{1: [0, 1, 2]}
>>> h.matrix.omap
array([[[[ True, False, False],
         [ True,  True,  True],
         [False, False, False]]]], dtype=bool)
>>> h.matrix.emap
array([[[[[ True, False, False]],

         [[ True,  True,  True]],

         [[False, False, False]]]]], dtype=bool)
>>> h.add_objects(0, 0, 1, 2, 0, 0) 
array([[0, 0, 3, 0, 0],
       [0, 0, 4, 0, 0]]...)
>>> h.matrix.omap
array([[[[ True, False, False, False, False],
         [ True,  True,  True,  True,  True],
         [False, False, False, False, False]]]], dtype=bool)
>>> h.matrix.emap
array([[[[[ True, False, False, False, False]],

         [[ True,  True,  True,  True,  True]],

         [[False, False, False, False, False]]]]], dtype=bool)
>>> h.linkage.links[(0,0)][(0,0)]
{1: [0, 1, 2, 3, 4]}
>>> h.add_objects(0, 0, 2, 1, 1, 3) 
array([[0, 0, 0, 0, 0]]...)
>>> h.linkage.links[(0,0)][(0,0)]
{1: [0, 1, 2, 3, 4], 2: [0]}
>>> h.add_objects(0, 0, 2, 2, 1, 4) 
array([[0, 0, 1, 0, 0],
       [0, 0, 2, 0, 0]]...)
>>> h.linkage.links[(0,0)][(1,4)]
{0: [0], 2: [1, 2]}
>>> h.linkage.links[(0,0)][(1,2)]
{0: [0]}
>>> h.matrix.emap
array([[[[[ True, False, False, False, False]],

         [[ True,  True,  True,  True,  True]],

         [[ True,  True,  True, False, False]]]]], dtype=bool)

def add_branch(self, tind):

Add a new branch to the data matrix by copying values from “parent” branch:

>>> import numpy
>>> tind = numpy.array([[0,0,4,0,0]], dtype=int)
>>> h.add_branch(tind) 
array([[0, 1, 4, 0, 0]]...)
>>> h.matrix.omap[0,:2,:,:]
array([[[ True, False, False, False, False],
        [ True,  True,  True,  True,  True],
        [ True,  True,  True, False, False]],

       [[ True, False, False, False, False],
        [False, False, False, False,  True],
        [ True,  True, False, False, False]]], dtype=bool)
>>> h.matrix.emap[0,:2,:,0,:]
array([[[ True, False, False, False, False],
        [ True,  True,  True,  True,  True],
        [ True,  True,  True, False, False]],

       [[ True, False, False, False, False],
        [False, False, False, False,  True],
        [ True,  True, False, False, False]]], dtype=bool)

>>> keys = h.linkage.links[(0,1)].keys()
>>> keys.sort()
>>> keys
[(0, 0), (1, 4), (2, 0), (2, 1)]

>>> li = h.linkage.links[(0,1)]
>>> li[(2, 0)]
{0: [0], 1: [4]}
>>> li[(0, 0)]
{1: [4], 2: [0, 1]}
>>> li[(2, 1)]
{0: [0], 1: [4]}
>>> li[(1, 4)]
{0: [0], 2: [0, 1]}

>>> keys = h.ind2id.ids[(0,1)].keys()
>>> keys.sort()
>>> keys
[(0, 0), (1, 4), (2, 0), (2, 1)]

>>> ind = h.ind2id.ids[(0,1)]
>>> ind[(2, 0)]
('SIMULATION-1-0', 'SIMULATION-1-0')
>>> ind[(0, 0)]
('simulation', 'simulation')
>>> ind[(2, 1)]
('SIMULATION-1-1', 'SIMULATION-1-1')
>>> ind[(1, 4)]
('SIMULATION-1', 'SIMULATION-1')

>>> tind = numpy.array([[0,0,0,0,0],[0,0,3,0,0]], dtype=int)
>>> h.add_branch(tind) 
array([[0, 1, 0, 0, 0],
       [0, 1, 3, 0, 0]]...)
>>> h.matrix.omap[0,:2,:,:]
array([[[ True, False, False, False, False],
        [ True,  True,  True,  True,  True],
        [ True,  True,  True, False, False]],

       [[ True, False, False, False, False],
        [ True, False, False,  True,  True],
        [ True,  True,  True, False, False]]], dtype=bool)
>>> h.matrix.emap[0,:2,:,0,:]
array([[[ True, False, False, False, False],
        [ True,  True,  True,  True,  True],
        [ True,  True,  True, False, False]],

       [[ True, False, False, False, False],
        [ True, False, False,  True,  True],
        [ True,  True,  True, False, False]]], dtype=bool)

>>> keys = h.linkage.links[(0,1)].keys()
>>> keys.sort()
>>> keys
[(0, 0), (1, 0), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2)]

>>> li = h.linkage.links[(0,1)]
>>> li[(1, 3)]
{0: [0], 2: [2]}
>>> li[(2, 1)]
{0: [0], 1: [4]}
>>> li[(2, 0)]
{0: [0], 1: [4]}
>>> li[(0, 0)]
{1: [0, 3, 4], 2: [0, 1, 2]}
>>> li[(1, 4)]
{0: [0], 2: [0, 1]}
>>> li[(2, 2)]
{0: [0], 1: [3]}
>>> li[(1, 0)]
{0: [0]}

>>> keys = h.ind2id.ids[(0,1)].keys()
>>> keys.sort()
>>> keys
[(0, 0), (1, 0), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2)]

>>> ind = h.ind2id.ids[(0,1)]
>>> ind[(1, 3)]
('SIMULATION-0', 'SIMULATION-0')
>>> ind[(2, 1)]
('SIMULATION-1-1', 'SIMULATION-1-1')
>>> ind[(2, 0)]
('SIMULATION-1-0', 'SIMULATION-1-0')
>>> ind[(0, 0)]
('simulation', 'simulation')
>>> ind[(1, 4)]
('SIMULATION-1', 'SIMULATION-1')
>>> ind[(2, 2)]
('SIMULATION-0-0', 'SIMULATION-0-0')
>>> ind[(1, 0)]
('UID001', 'UID001')

def add_iteration(self, fromit):

>>> h2 = Handler(4, lexicon, 10, 1, 1, bgroups, False, False, sim, 0)
>>> obj_ids = [('SIMULATION', 'SIMULATION')]
>>> h2.add_objects(0, 0, 0, 1, None, None, obj_ids) 
array([[0, 0, 0, 0, 0]]...)
>>> obj_ids = [('UID001', 'UID001'), ('UID002', 'UID002'),
...            ('UID003', 'UID003')]
>>> h2.add_objects(0, 0, 1, 3, 0, 0, obj_ids) 
array([[0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 2, 0, 0]]...)
>>> h2.add_iteration(0)
>>> h2.matrix.omap
array([[[[ True, False, False],
         [ True,  True,  True],
         [False, False, False]]],


       [[[ True, False, False],
         [ True,  True,  True],
         [False, False, False]]],


       [[[ True, False, False],
         [ True,  True,  True],
         [False, False, False]]],


       [[[ True, False, False],
         [ True,  True,  True],
         [False, False, False]]]], dtype=bool)
>>> h2.linkage.links[(2,0)][(0,0)]
{1: [0, 1, 2]}
>>> h2.ind2id.ids[(2,0)][(0,0)]
('SIMULATION', 'SIMULATION')
>>> h2.ind2id.ids[(3,0)][(1,2)]
('UID003', 'UID003')

def set_value(self, level, tind, attrs, values, modelname=None, indicatechange=False):

Store attribute value(s) into the data matrix on the given level using the provided target index (tind) combinations. Each target index consists of branch and object indices. Attributes (attrs) and values are iterable objects. The lenght of tind and values must be equal. Furthermore, the length of each item in values must match the lengt of attrs:

>>> epsilon = 0.000001
>>> tind = numpy.array([[0,0,0],[0,1,0]], dtype=int)
>>> h.set_value(0, tind, (0,), [(10.1, 20.1)])
>>> h.matrix.mx[0,0,0,0,0] - 10.1 < epsilon
True
>>> tind = numpy.array([[0,0,0],[0,0,1],[0,0,2],[0,0,3],[0,0,4],[0,1,0],
...                     [0,1,1],[0,1,2],[0,1,3],[0,1,4]], dtype=int)
>>> h.set_value(1, tind, (0,1),
...             [[1.1, 2.1, 2.2, 2.3, 2.4,1.1, 2.1, 2.2, 2.3, 2.4],
...              [5.0, 5.1, 5.2, 5.3, 5.4,5.0, 5.1, 5.2, 5.3, 5.4]])
>>> h.matrix.mx[0,0,1,2,1] - 5.2 < epsilon
True
>>> tind=numpy.array([[0,1,0],[0,1,1],[0,1,2],[0,1,3],[0,1,4]],dtype=int)
>>> h.set_value(1, tind, (0,), [[3.1, 3.1, 3.2, 3.3, 3.4]])
>>> h.matrix.mx[0,1,1,2,0] - 3.2 < epsilon
True
>>> tind = numpy.array([[0,0,0],[0,0,1],[0,0,2]], dtype=int)
>>> h.set_value(2, tind, (0,), [[.5, .6, .7]])
>>> h.matrix.mx[0,0,2,1,0] - .6 < epsilon
True
>>> h.set_value(2, tind, 0, [.5, .6, .7])
>>> h.matrix.mx[0,0,2,1,0] - .6 < epsilon
True

def del_objects(self, iteration, branch, level, objs):

Delete objects from data by nullifying data and removing the relationship links:

>>> h.del_objects(0, 0, 1, [0, 1])
>>> h.matrix.mx[0,0,1,0,:] 
... 
array([ ...], dtype=float32)
>>> h.matrix.omap[0,0,1,1]
False
>>> h.matrix.omap[0,0,1,2]
True
>>> h.linkage.links[(0,0)][(0,0)]
{1: [2, 3, 4], 2: [0, 1, 2]}
>>> h.del_objects(0, 0, 1, [2, 3])
>>> h.add_objects(0, 0, 1, 4, 0, 0) 
array([[0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 2, 0, 0],
       [0, 0, 3, 0, 0]]...)
>>> h.del_objects(0, 0, 1, [0, 1])
>>> h.set_value(1, numpy.array([[0,0,2,0,0],[0,0,3,0,0]]), [0,1],
...             [[2.2,2.3], [5.2,5.3]])

def get_tind(self, level, depthind, datalevel=None, throughlevel=None, vals4all=True, order=None):

Get index of target branches and objects for the specified data level (evaluation level in model chain) and the model chain depth. If datalevel differs from the (evaluation) level, each branch object index pair is augmented with the start and stop indices that map the rows in the data matrix returned by get_value to the objects at the (evaluation) level. Setting the throughlevel attribute forces the collection of relatives determined by the datalevel attribute to be collected via the through level; e.g. for a stand-stratum-tree hierarchy; for each stratum all the trees of a stand are returned set as data objects if the level == stratum, datalevel == tree and throughlevel == stand. If throughlevel is defined, the values are either for all data-level objects, or for self (this can happen only in aggregation model conditions). Parameter valuelevel can be defined in situations when datalevel differs from level, but the actual values should be from a level other than datalevel. The order of the targets can be forced using order -parameter, but this requires that the indices of all targets are known beforehand.

Dimensions:

    1. dimension: iteration
    1. dimension: branch
    1. dimension: object index at the evaluation level
    1. dimension: start index of data belonging to the object in the result data matrix of get_value
    1. dimension: stop index of data belonging to the object in the result data matrix of get_value
>>> h.matrix.emap[:,:2,:,:,:]
array([[[[[ True, False, False, False, False]],

         [[False, False,  True,  True,  True]],

         [[ True,  True,  True, False, False]]],


        [[[ True, False, False, False, False]],

         [[ True, False, False,  True,  True]],

         [[ True,  True,  True, False, False]]]]], dtype=bool)
>>> h.get_tind(0, 0) 
(array([[0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0]]...), set([]))
>>> h.get_tind(1, 0) 
(array([[0, 0, 2, 0, 0],
       [0, 0, 3, 0, 0],
       [0, 0, 4, 0, 0],
       [0, 1, 0, 0, 0],
       [0, 1, 3, 0, 0],
       [0, 1, 4, 0, 0]]...), set([]))
>>> h.get_tind(0, 0, 1) 
(array([[0, 0, 0, 0, 3],
       [0, 1, 0, 3, 6]]...), set([]))
>>> h.get_tind(1, 0, 0) 
(array([[0, 0, 2, 0, 1],
       [0, 0, 3, 1, 2],
       [0, 0, 4, 2, 3],
       [0, 1, 0, 3, 4],
       [0, 1, 3, 4, 5],
       [0, 1, 4, 5, 6]]...), set([]))
>>> tind = numpy.array([[0,0,2,0,0]], dtype=int)
>>> h.add_branch(tind) 
array([[0, 1, 2, 0, 0]]...)
>>> h.matrix.omap[0,:2,:,:]
array([[[ True, False, False, False, False],
        [False, False,  True,  True,  True],
        [ True,  True,  True, False, False]],

       [[ True, False, False, False, False],
        [ True, False,  True,  True,  True],
        [ True,  True,  True, False, False]]], dtype=bool)
>>> tind = numpy.array([[0,1,0,0,0]], dtype=int)
>>> h.add_branch(tind) 
array([[0, 0, 0, 0, 0]]...)
>>> h.matrix.omap[0,:2,:,:]
array([[[ True, False, False, False, False],
        [ True, False,  True,  True,  True],
        [ True,  True,  True, False, False]],

       [[ True, False, False, False, False],
        [ True, False,  True,  True,  True],
        [ True,  True,  True, False, False]]], dtype=bool)
>>> h.get_tind(0,0) 
(array([[0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0]]...), set([]))
>>> h.get_tind(1,0) 
(array([[0, 0, 0, 0, 0],
       [0, 0, 2, 0, 0],
       [0, 0, 3, 0, 0],
       [0, 0, 4, 0, 0],
       [0, 1, 0, 0, 0],
       [0, 1, 2, 0, 0],
       [0, 1, 3, 0, 0],
       [0, 1, 4, 0, 0]]...), set([]))
>>> h.get_tind(0, 0, 1) 
(array([[0, 0, 0, 0, 4],
       [0, 1, 0, 4, 8]]...), set([]))
>>> h.get_tind(1, 0, 0) 
(array([[0, 0, 0, 0, 1],
       [0, 0, 2, 1, 2],
       [0, 0, 3, 2, 3],
       [0, 0, 4, 3, 4],
       [0, 1, 0, 4, 5],
       [0, 1, 2, 5, 6],
       [0, 1, 3, 6, 7],
       [0, 1, 4, 7, 8]]...), set([]))

Get target index using throughlevel value:

>>> h.get_tind(1, 0, 2, 0) 
(array([[ 0,  0,  0,  0,  3],
       [ 0,  0,  2,  3,  6],
       [ 0,  0,  3,  6,  9],
       [ 0,  0,  4,  9, 12],
       [ 0,  1,  0, 12, 15],
       [ 0,  1,  2, 15, 18],
       [ 0,  1,  3, 18, 21],
       [ 0,  1,  4, 21, 24]]...), set([]))
>>> h.active_dataobjects 
array([[0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 1, 0],
       [0, 1, 1],
       [0, 1, 2],
       [0, 1, 0],
       [0, 1, 1],
       [0, 1, 2],
       [0, 1, 0],
       [0, 1, 1],
       [0, 1, 2],
       [0, 1, 0],
       [0, 1, 1],
       [0, 1, 2]]...)
>>> h.get_tind(1, 0, 1, 0) 
(array([[ 0,  0,  0,  0,  4],
       [ 0,  0,  2,  4,  8],
       [ 0,  0,  3,  8, 12],
       [ 0,  0,  4, 12, 16],
       [ 0,  1,  0, 16, 20],
       [ 0,  1,  2, 20, 24],
       [ 0,  1,  3, 24, 28],
       [ 0,  1,  4, 28, 32]]...), set([]))
>>> h.active_dataobjects 
array([[0, 0, 0],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 4],
       [0, 0, 0],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 4],
       [0, 0, 0],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 4],
       [0, 0, 0],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 4],
       [0, 1, 0],
       [0, 1, 2],
       [0, 1, 3],
       [0, 1, 4],
       [0, 1, 0],
       [0, 1, 2],
       [0, 1, 3],
       [0, 1, 4],
       [0, 1, 0],
       [0, 1, 2],
       [0, 1, 3],
       [0, 1, 4],
       [0, 1, 0],
       [0, 1, 2],
       [0, 1, 3],
       [0, 1, 4]]...)
>>> h.get_tind(2, 0, 2, 0) 
(array([[ 0,  0,  0,  0,  3],
       [ 0,  0,  1,  3,  6],
       [ 0,  0,  2,  6,  9],
       [ 0,  1,  0,  9, 12],
       [ 0,  1,  1, 12, 15],
       [ 0,  1,  2, 15, 18]]...), set([]))
>>> h.active_dataobjects 
array([[0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 0],
       [0, 0, 1],
       [0, 0, 2],
       [0, 1, 0],
       [0, 1, 1],
       [0, 1, 2],
       [0, 1, 0],
       [0, 1, 1],
       [0, 1, 2],
       [0, 1, 0],
       [0, 1, 1],
       [0, 1, 2]]...)

Get target index using throughlevel and so that vals4all attribute is False:

>>> h.get_tind(1, 0, 2, 0, False) 
(array([[ 0,  0,  0,  0,  3],
       [ 0,  0,  2,  3,  6],
       [ 0,  0,  3,  6,  9],
       [ 0,  0,  4,  9, 12],
       [ 0,  1,  0, 12, 15],
       [ 0,  1,  2, 15, 18],
       [ 0,  1,  3, 18, 21],
       [ 0,  1,  4, 21, 24]]...), set([]))
>>> h.active_dataobjects 
array([[0, 0, 0],
       [0, 0, 0],
       [0, 0, 0],
       [0, 0, 2],
       [0, 0, 2],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 3],
       [0, 0, 3],
       [0, 0, 4],
       [0, 0, 4],
       [0, 0, 4],
       [0, 1, 0],
       [0, 1, 0],
       [0, 1, 0],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 3],
       [0, 1, 3],
       [0, 1, 3],
       [0, 1, 4],
       [0, 1, 4],
       [0, 1, 4]]...)
>>> h.get_tind(1, 0, 1, 0, False) 
(array([[ 0,  0,  0,  0,  4],
       [ 0,  0,  2,  4,  8],
       [ 0,  0,  3,  8, 12],
       [ 0,  0,  4, 12, 16],
       [ 0,  1,  0, 16, 20],
       [ 0,  1,  2, 20, 24],
       [ 0,  1,  3, 24, 28],
       [ 0,  1,  4, 28, 32]]...), set([]))
>>> h.active_dataobjects 
array([[0, 0, 0],
       [0, 0, 0],
       [0, 0, 0],
       [0, 0, 0],
       [0, 0, 2],
       [0, 0, 2],
       [0, 0, 2],
       [0, 0, 2],
       [0, 0, 3],
       [0, 0, 3],
       [0, 0, 3],
       [0, 0, 3],
       [0, 0, 4],
       [0, 0, 4],
       [0, 0, 4],
       [0, 0, 4],
       [0, 1, 0],
       [0, 1, 0],
       [0, 1, 0],
       [0, 1, 0],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 3],
       [0, 1, 3],
       [0, 1, 3],
       [0, 1, 3],
       [0, 1, 4],
       [0, 1, 4],
       [0, 1, 4],
       [0, 1, 4]]...)
>>> h.get_tind(2, 0, 2, 0, False) 
(array([[ 0,  0,  0,  0,  3],
       [ 0,  0,  1,  3,  6],
       [ 0,  0,  2,  6,  9],
       [ 0,  1,  0,  9, 12],
       [ 0,  1,  1, 12, 15],
       [ 0,  1,  2, 15, 18]]...), set([]))
>>> h.active_dataobjects 
array([[0, 0, 0],
       [0, 0, 0],
       [0, 0, 0],
       [0, 0, 1],
       [0, 0, 1],
       [0, 0, 1],
       [0, 0, 2],
       [0, 0, 2],
       [0, 0, 2],
       [0, 1, 0],
       [0, 1, 0],
       [0, 1, 0],
       [0, 1, 1],
       [0, 1, 1],
       [0, 1, 1],
       [0, 1, 2],
       [0, 1, 2],
       [0, 1, 2]]...)

Get target index, but with a predefined order:

>>> order = numpy.array([[0,0,1], [0,0,0]], dtype=int)
>>> h.get_tind(0, 0, None, None, True, None, order) 
(array([[0, 0, 1, 0, 0],
       [0, 0, 0, 0, 0]]...), set([]))
>>> order = numpy.array([[0,1,4],[0,1,3],[0,1,2],[0,1,0],[0,0,4],
...                      [0,0,3],[0,0,2],[0,0,0]], dtype=int)
>>> h.get_tind(1, 0, 2, None, True, None, order) 
(array([[0, 1, 4, 0, 2],
       [0, 1, 3, 2, 3],
       [0, 1, 2, 0, 0],
       [0, 1, 0, 0, 0],
       [0, 0, 4, 3, 5],
       [0, 0, 3, 0, 0],
       [0, 0, 2, 0, 0],
       [0, 0, 0, 0, 0]]...), set([2, 3, 5, 6, 7]))
>>> h.active_dataobjects 
array([[0, 1, 0],
       [0, 1, 1],
       [0, 1, 2],
       [0, 0, 1],
       [0, 0, 2]]...)

def get_value(self, tind, attrs, ignore_missing=False):

Get attribute value(s) from the data matrix. If any of the target objects are missing values and ignore_missing is False, return also error messages, an error object target index (for error logging), and a set of object indices that were missing values

The return value is (if some values are missing): (values, ([error_messages], error_tind, removable_objects)) And if no values are missing: (values, None)

Get values so that all objects have missing values (and all target objects will be removed)

>>> from numpy import array, float32, NaN
>>> EPSILON = 0.00001
>>> tind, toremove = h.get_tind(0, 0)
>>> vals, err = h.get_value(tind, [0,1])  
Called Lexicon.get_variable_name(0, 1)
Called Lexicon.get_level_name(0)
>>> expected = array([[10.1,  20.1], [NaN, NaN]], dtype=float32)
>>> expected - vals < EPSILON
array([[ True,  True],
       [False, False]], dtype=bool)
>>> err[0]  
[("value for variable 'VARNAME' at level 'LEVNAME' not found",
array([0, 1]))]
>>> err[1] 
array([[0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0]]...)
>>> err[2]
set([0, 1])

Get values so that no values are missing

>>> tind, toremove = h.get_tind(1, 0)
>>> vals, err = h.get_value(tind, 0)
>>> expected = array([3.1, 2.2, 2.3, 2.4, 3.1, 2.2, 3.3, 3.4],
...                  dtype=float32)
>>> expected - vals < EPSILON 
array([ True,  True,  True,  True,  True,  True,  True,  True],
      dtype=bool)
>>> err

>>> vals, err = h.get_value(tind, [0,1])
>>> expected = array([[ 3.1,  2.2,  2.3,  2.4,  3.1,  2.2,  3.3,  3.4],
...                   [ 5. ,  5.2,  5.3,  5.4,  5. ,  5.2,  5.3,  5.4]],
...                  dtype=float32)
>>> expected - vals < EPSILON 
array([[ True,  True,  True,  True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True,  True,  True,  True]],
       dtype=bool)
>>> err

>>> tind, toremove = h.get_tind(0, 0, 1)
>>> vals, err = h.get_value(tind, 1)
>>> expected = array([ 5. ,  5.2,  5.3,  5.4,  5. ,  5.2,  5.3,  5.4],
...                  dtype=float32)
>>> expected -vals < EPSILON 
array([ True,  True,  True,  True,  True,  True,  True,  True],
      dtype=bool)
>>> err

>>> vals, err = h.get_value(tind, [0,1])
>>> expected = array([[ 3.1,  2.2,  2.3,  2.4,  3.1,  2.2,  3.3,  3.4],
...                   [ 5. ,  5.2,  5.3,  5.4,  5. ,  5.2,  5.3,  5.4]],
...                   dtype=float32)
>>> expected - vals < EPSILON 
array([[ True,  True,  True,  True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True,  True,  True,  True]],
       dtype=bool)
>>> err

Get values from child level so that some values are missing

>>> tind, toremove = h.get_tind(1, 0)
>>> h.matrix.mx[0,0,1,(0,3),0] = numpy.NaN
>>> h.matrix.mx[0,0,1,0,1] = numpy.NaN
>>> vals, err = h.get_value(tind, [0,1])
Called Lexicon.get_variable_name(1, 0)
Called Lexicon.get_level_name(1)
Called Lexicon.get_variable_name(1, 1)
Called Lexicon.get_level_name(1)
>>> expected = array([[ NaN,  2.2,  NaN,  2.4,  3.1,  2.2,  3.3,  3.4],
...                   [ NaN,  5.2,  5.3,  5.4,  5. ,  5.2,  5.3,  5.4]],
...                  dtype=float32)
>>> expected - vals < EPSILON 
array([[False,  True, False,  True,  True,  True,  True,  True],
       [False,  True,  True,  True,  True,  True,  True,  True]],
       dtype=bool)
>>> for e in err[0]: print e  
("value for variable 'VARNAME' at level 'LEVNAME' not found",
array([0, 2]))
("value for variable 'VARNAME' at level 'LEVNAME' not found",
array([0]))
>>> err[1] 
array([[0, 0, 0, 0, 0],
       [0, 0, 2, 0, 0],
       [0, 0, 3, 0, 0],
       [0, 0, 4, 0, 0],
       [0, 1, 0, 0, 0],
       [0, 1, 2, 0, 0],
       [0, 1, 3, 0, 0],
       [0, 1, 4, 0, 0]]...)
>>> err[2]
set([0, 2])
>>> vals, err = h.get_value(tind, 0)
Called Lexicon.get_variable_name(1, 0)
Called Lexicon.get_level_name(1)
>>> expected = array([ NaN,  2.2,  NaN,  2.4,  3.1,  2.2,  3.3,  3.4],
...                  dtype=float32)
>>> expected - vals < EPSILON 
array([False,  True, False,  True,  True,  True,  True,  True],
      dtype=bool)
>>> for e in err[0]: print e  
("value for variable 'VARNAME' at level 'LEVNAME' not found",
array([0, 2]))
>>> err[1] 
array([[0, 0, 0, 0, 0],
       [0, 0, 2, 0, 0],
       [0, 0, 3, 0, 0],
       [0, 0, 4, 0, 0],
       [0, 1, 0, 0, 0],
       [0, 1, 2, 0, 0],
       [0, 1, 3, 0, 0],
       [0, 1, 4, 0, 0]]...)
>>> err[2]
set([0, 2])

Get values with empty tind

>>> etind = numpy.zeros((0,0), dtype=int)
>>> vals, err = h.get_value(etind, 0)

def get_exists(self, tind, attrs):

Get attribute value(s) from the data matrix. If the target object has a value, return True(s) and return False(s) for those which are missing a value

>>> exts = h.get_exists(tind, [0,1])
>>> exts  
array([[False,  True, False,  True,  True,  True,  True,  True],
       [False,  True,  True,  True,  True,  True,  True,  True]],
dtype=bool)

>>> exts = h.get_exists(tind, 0)
>>> exts  
array([False,  True, False,  True,  True,  True,  True,  True], dtype=bool)

>>> exts = h.get_exists(tind, (0,))
>>> exts  
array([[False,  True, False,  True,  True,  True,  True,  True]], dtype=bool)

def set_date(self, tind, dates, set_init_date=False):

Store date(s) into the date matrix and if set_init_date is True, set the initial dates also

>>> import datetime
>>> tind = numpy.array([[0,0,0],[0,0,1]], dtype=int)

>>> h.set_date(tind, numpy.array([datetime.date(2000,1,1),
...            datetime.date(2002,2,2)], dtype=datetime.date))
>>> h.matrix.dmx[:]  
array([...2000...1...1..., ...2002...1...1...1...,
       ...1...1...1..., ...1...1...1...], dtype=object)
>>> tind = numpy.array([[0,2,3],[0,3,4]], dtype=int)
>>> dates = numpy.array([datetime.date(2003,3,3),
...                      datetime.date(2005,5,5)], dtype=datetime.date)

>>> h.set_date(tind, dates)
>>> h.matrix.dmx[:]  
array([...2000...1...1..., ...2002...2...2..., ...1...1...1...,
       ...2003...3...3..., ...2005...5...5...], dtype=object)
>>> h.matrix.init_dmx[:]  
array([...1...1...1..., ...1...1...1..., ...1...1...1..., ...1...1...1...,
       ...1...1...1...], dtype=object)

>>> h.set_date(tind, dates, True)
>>> h.matrix.init_dmx[:]  
array([...1...1...1..., ...1...1...1..., ...1...1...1...,
       ...2003...3...3..., ...2005...5...5...], dtype=object)

def get_date(self, tind):

Get date(s) from the date matrix:

>>> tind = numpy.array([[0,0,0],[0,1,0],[0,2,0]], dtype=int)
>>> h.get_date(tind) 
array([...2000...1...1..., ...2000...1...1...,
       ...2000...1...1...], dtype=object)

>>> tind = numpy.array([[0,0,0],[0,0,1],[0,0,2],[0,0,3]], dtype=int)
>>> h.get_date(tind) 
array([...2000...1...1..., ...2002...2...2..., ...1...1...1...,
       ...2003...3...3...], dtype=object)

def get_init_date(self, tind):

>>> h.get_init_date(tind) 
array([...1...1...1..., ...1...1...1..., ...1...1...1...,
       ...2003...3...3...], dtype=object)
>>> tind = numpy.array([[0,0,0],[0,0,1],[0,0,2],[0,0,3]], dtype=int)
>>> h.get_init_date(tind) 
array([...1...1...1..., ...1...1...1..., ...1...1...1...,
       ...2003...3...3...], dtype=object)

Get initial date(s) from the date matrix

def get_id(self, level, tind):

Get id of objects defined in target index:

>>> tind = numpy.array([[0,0,2,0,0]], dtype=int)
>>> h.get_id(1, tind)
[('SIMULATION-4', 'SIMULATION-4')]

def get_parent(self, level, tind, parentlevel, filterby=None):

Get parent target index for objects in the target index

>>> tind = numpy.array([[0,0,0,0,0],
...                     [0,0,1,0,0],
...                     [0,0,2,0,0]], dtype=int)
>>> h.get_parent(2, tind, 1) 
(array([[0, 0, 4, 0, 0],
       [0, 0, 4, 0, 0]]...), set([0]))

>>> h.get_parent(2, tind[(1,),:], 1) 
(array([[0, 0, 4, 0, 0]]...), set([]))

>>> h.get_parent(2, tind, 0) 
(array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]]...), set([]))

>>> tind = numpy.array([[0,0,0,0,0],
...                     [0,0,2,0,0],
...                     [0,0,3,0,0],
...                     [0,0,4,0,0]], dtype=int)
>>> h.get_parent(1, tind, 0) 
(array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]]...), set([]))

def get_children(self, iteration, branch, obj, level, childlevel):

Get the children of an object from child level in given iteration and branch

>>> h.get_children(0, 1, 0, 0, 1)
[0, 2, 3, 4]
>>> h.get_children(0, 1, 0, 0, 2)
[0, 1, 2]
>>> h.get_children(0, 1, 3, 1, 2)
[2]
>>> h.get_children(0, 1, 4, 1, 2)
[0, 1]

def get_object_map(self, level, tind):

Get object mapping for targets on given level

>>> h.get_object_map(2, tind)
array([ True,  True, False, False], dtype=bool)

def block(self, level, depth, tind, block_children=False):

Block evaluation of objects at given model chain depth level and all levels below the given depth level (prevents evaluation of blocked objects):

>>> tind = numpy.array([[0,0,0,0,0]], dtype=int)
>>> h.matrix.emap[0,0,0,:,:]
array([[ True, False, False, False, False]], dtype=bool)
>>> h.block(0, 0, tind)
>>> h.matrix.emap[0,0,0,:,:]
array([[False, False, False, False, False]], dtype=bool)
>>> tind = numpy.array([[0,0,3,0,0],[0,0,4,0,0]], dtype=int)
>>> h.matrix.emap[0,0,1,:,:]
array([[ True, False,  True,  True,  True]], dtype=bool)
>>> h.block(1, 0, tind)
>>> h.matrix.emap[0,0,1,:,:]
array([[ True, False,  True, False, False]], dtype=bool)
>>> h.matrix.emap[0,0,2,:,:]
array([[ True,  True,  True, False, False]], dtype=bool)
>>> h.block(1, 0, tind, True)
>>> h.matrix.emap[0,0,1,:,:]
array([[ True, False,  True, False, False]], dtype=bool)
>>> h.matrix.emap[0,0,2,:,:]
array([[ True, False, False, False, False]], dtype=bool)
>>> h.block(0, 0, numpy.array([[0,0,0,0,0]], dtype=int), True)
>>> h.matrix.emap[0,0,1:,:,:]
array([[[False, False, False, False, False]],

       [[False, False, False, False, False]]], dtype=bool)

def release(self, level, depth, tind, release_children=False release_all=False):

Release objects at given level and model chain depth for evaluation. If level is not defined, release all levels. If depth is not defined release objects from all depths and if target index is not defined, release all objects.:

>>> h.matrix.omap[0,0,0,:]
array([ True, False, False, False, False], dtype=bool)
>>> h.release(0, 0, None)
>>> h.matrix.emap[0,0,0,:,:]
array([[ True, False, False, False, False]], dtype=bool)
>>> tind = numpy.array([[0,0,2,0,0]], dtype=int)
>>> h.release(1, 0, tind)
>>> h.matrix.emap[0,0,1,:,:]
array([[False, False,  True, False, False]], dtype=bool)
>>> tind = numpy.array([[0,0,4,0,0]], dtype=int)
>>> h.release(1, 0, tind, True)
>>> h.matrix.emap[0,0,1:,:,:]
array([[[False, False,  True, False,  True]],

       [[False,  True,  True, False, False]]], dtype=bool)
>>> h.release(None, None, None, False, True)
>>> h.matrix.emap[0,0,1:,:,:]
array([[[ True, False,  True,  True,  True]],

       [[ True,  True,  True, False, False]]], dtype=bool)
>>> h.matrix.omap[0,0,1:,:,]
array([[ True, False,  True,  True,  True],
       [ True,  True,  True, False, False]], dtype=bool)

def block_children(self, depth, level, childlevel):

Find the blocked objects from ‘level’ and block all relatives from lower levels until childlevel

>>> h.block(1, 0, numpy.array([[0,0,4,0,0]], dtype=int))
>>> h.matrix.emap[0,0,2,0,:]
array([ True,  True,  True, False, False], dtype=bool)
>>> h.block_children(0, 1, [2, ]) 
{2: array([[0, 0, 1],
       [0, 0, 2]]...)}
>>> h.matrix.emap[0,0,2,0,:]
array([ True, False, False, False, False], dtype=bool)

Block simulation level and the all it’s children

>>> h.block(0, 0, numpy.array([[0,0,0,0,0]], dtype=int))
>>> bl = h.block_children(0, 0, [1, 2])
>>> bl[1] 
array([[0, 0, 0],
       [0, 0, 2],
       [0, 0, 3]]...)
>>> bl[2] 
array([[0, 0, 0]]...)

def del_branch(self, tind, bg_ind):

Delete branches defined in the target index:

>>> tind = numpy.array([[0,1,4,0,0]], dtype=int)
>>> links = h.linkage.links[(0,1)].keys()
>>> links.sort()
>>> links
[(0, 0), (1, 0), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2)]
>>> h.linkage.links[(0,1)][(2,0)].keys()
[0, 1]
>>> h.linkage.links[(0,1)][(2,1)].keys()
[0, 1]
>>> ids = h.ind2id.ids[(0,1)].keys()
>>> ids.sort()
>>> ids
[(0, 0), (1, 0), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2)]
>>> h.del_branch(tind, 0)
>>> links = h.linkage.links[(0,1)].keys()
>>> links.sort()
>>> links
[(0, 0), (1, 0), (1, 2), (1, 3), (2, 2)]
>>> ids = h.ind2id.ids[(0,1)].keys()
>>> ids.sort()
>>> ids
[(0, 0), (1, 0), (1, 2), (1, 3), (2, 2)]

def _squeeze_objects(self, iteration, branch, level, num):

Find empty space for number of objects in given iteration, branch and level. Increase matrix size to make space if needed.

def _copy_matrices(self, iteration, frombranch, tobranch, level, fromobj, toobj):

Copy values in all matrices from parent branch/object to target branch/object

def _find_empty_branches(self, iteration, level, objs):

Find empty branches for objects on given level. Resize matrices in branch dimension if necessary.:

>>> h.matrix.omap[0,:4,1,:]
array([[ True, False,  True,  True,  True],
       [ True, False,  True,  True, False],
       [False, False, False, False, False],
       [False, False, False, False, False]], dtype=bool)
>>> h._find_empty_branches([0,0,0,0,0], 1, [0,1,2,3,4])
array([2, 0, 2, 2, 1])

Find empty branches when object list includes non-unique indices (new branches for one object with different parent branches):

>>> h._find_empty_branches([0,0,0,0], 1, [0,4,4,4])
array([2, 1, 2, 3])

Find empty branches with non-unique objects and so that matrix dimensions have to be modified:

>>> h.matrix.omap[0,:,1,0] = True
>>> h._find_empty_branches([0,0,0], 1, [0,0,0])
array([11, 12, 13])