.. _operationmemory-py: ################## operationmemory.py ################## ****************************** class OperationMemory(object): ****************************** Class for managing operation history Attributes: - memory: two four dimensional numpy arrays with following dimensions, first storing the last date of operation and the second one the number of operations done: 1. iteration 2. branch 3. object 4. operation def __init__(self): =================== Initialize operation memory structure:: >>> from simo.simulation.caller.operationmemory import OperationMemory >>> m = OperationMemory() def _size_check(self, obj_row, opind): ====================================== Check that operation memory is large enough for given objects, resize memory if necessary:: >>> import numpy as np >>> from datetime import date as dt >>> obj_row = (0,2,3) >>> m._size_check(obj_row, 1) >>> m._dates.shape (1, 3, 4, 2) >>> m._times.shape (1, 3, 4, 2) >>> m._size_check(obj_row, 4) >>> m._dates.shape (1, 3, 4, 5) >>> m._times.shape (1, 3, 4, 5) def update(self, tind, opind, level, dates): ============================================ Update operation memory by adding operation defined by operation indice (opind) for the objects defined by target index (tind):: >>> tind = np.array([[0,0,1,0,0],[0,0,2,0,0],[0,1,1,0,0],[0,2,3,0,0]], dtype=int) >>> m.update(tind, 0, 1, dt(2005, 1, 1)) >>> m._dates.shape (1, 3, 4, 5) >>> m._times.shape (1, 3, 4, 5) >>> m._dates[0,0,:,0] # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE array([...1...1...1..., ...2005...1...1..., ...2005...1...1..., ...1...1...1...], dtype=object) >>> m._times[0,0,:,0] array([0, 1, 1, 0]) >>> tind = np.array([[0,0,2,0,0],[0,0,5,0,0],[0,4,7,0,0]], dtype=int) >>> m.update(tind, 0, 1, dt(2015, 1, 1)) >>> m._dates.shape (1, 5, 8, 5) >>> m._times.shape (1, 5, 8, 5) >>> m._dates[0,0,:,0] # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE array([...1...1...1..., ...2005...1...1..., ...2015...1...1..., ...1...1...1..., ...1...1...1..., ...2015...1...1..., ...1...1...1..., ...1...1...1...], dtype=object) >>> m._times[0,0,:,0] array([0, 1, 2, 0, 0, 1, 0, 0]) >>> m._level_map[1] set([0]) def reset(self): ================ Reset operation memory. Sets all operation memory values to zero. def since(self, tind, opind, dates, timeunit='year'): ===================================================== Get number of timesteps since operation was done last time relative to given date:: >>> tind = np.array([[0,0,1,0,0]], dtype=int) >>> m.since(tind, 0, dt(2005,1,1), 'year')[0] 0 >>> m.since(tind, 0, dt(2005,12,31), 'year')[0] 0 >>> m.since(tind, 0, dt(2006,1,1), 'year')[0] 1 >>> m.since(tind, 0, dt(2006,12,31), 'year')[0] 1 >>> m.since(tind, 0, dt(2005,1,1), 'month')[0] 0 >>> m.since(tind, 0, dt(2005,1,31), 'month')[0] 0 >>> m.since(tind, 0, dt(2005,2,1), 'month')[0] 1 >>> m.since(tind, 0, dt(2005,7,1), 'month')[0] 6 >>> m.since(tind, 0, dt(2005,12,31), 'month')[0] 11 >>> m.since(tind, 0, dt(2006,1,1), 'month')[0] 12 >>> m.since(tind, 0, dt(2006,12,31), 'month')[0] 23 >>> m._dates[0,0,1,0] = dt(2000, 3, 31) >>> m.since(tind, 0, dt(2000,4,1), 'year')[0] 0 >>> m.since(tind, 0, dt(2001,4,1), 'year')[0] 1 >>> m.since(tind, 0, dt(2000,4,1), 'month')[0] 1 >>> m.since(tind, 0, dt(2000,12,31), 'month')[0] 9 >>> m.since(tind, 0, dt(2001,1,1), 'month')[0] 10 >>> m.since(tind, 0, dt(2001,3,31), 'month')[0] 12 >>> m.since(tind, 0, dt(2001,4,1), 'month')[0] 13 >>> m.since(tind, 0, dt(2001,6,30), 'month')[0] 15 >>> m.since(tind, 0, dt(2000,4,1), 'day')[0] 0 >>> m.since(tind, 0, dt(2000,12,31), 'day')[0] 273 >>> m.since(tind, 0, dt(2001,1,1), 'day')[0] 274 >>> m.since(tind, 0, dt(2001,3,31), 'day')[0] 365 >>> m.since(tind, 0, dt(2001,4,1), 'day')[0] 365 >>> m.since(tind, 0, dt(2001,6,30), 'day')[0] 455 def times(self, tind, opind): ============================= Get number of times a given operation has been done:: >>> tind = np.array([[0,0,1,0,0],[0,0,2,0,0],[0,0,3,0,0],[0,0,5,0,0]], dtype=int) >>> m.times(tind, 0) array([1, 2, 0, 1]) >>> m.times(tind, 1) array([0, 0, 0, 0]) def add_branch(self, par_obj, new_obj, level): ============================================== Update operation memory after adding a new branch to simulation data matrix :: >>> par_obj = (0,0,1) >>> new_obj = (0,1,1) >>> m.add_branch(par_obj, new_obj, 1) >>> m._dates[0,(0,1),1] # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE array([[...2...3...31..., ...1...1...1..., ...1...1...1..., ...1...1...1..., ...1...1...1...], [...2...3...31..., ...1...1...1..., ...1...1...1..., ...1...1...1..., ...1...1...1...]], dtype=object) >>> m._times[0,(0,1),1] array([[1, 0, 0, 0, 0], [1, 0, 0, 0, 0]]) def del_objects(self, it, br, objs, level): =========================================== Delete objects from operation memory (reset given objects) :: >>> m.del_objects(0, 0, [0,1,2], 1) >>> m._dates[0,0,(0,1,2),0] # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE array([...1...1...1..., ...1...1...1..., ...1...1...1...], dtype=object) >>> m._times[0,0,(0,1,2),0] array([0, 0, 0])