c4dynamics.states.state.state.storeparams

c4dynamics.states.state.state.storeparams#

state.storeparams(params, t=-1.0)[source]#

Stores parameters.

Parameters are data attributes which are not part of the state vector. storeparams() is used to store the instantaneous parameters.

Parameters:
  • params (str or list of str) – Name or names of the parameters to store.

  • t (float or int, optional) – Time stamp for the stored state.

Note

1. Time t is an optional parameter with a default value of \(t = -1\). The time is always appended at the head of the array to store. However, if t is not given, default \(t = -1\) is stored instead.

2. The method storeparams() goes together with the method data() as input and output.

Examples

>>> s = c4d.state(x = 100, vx = 10)
>>> s.mass = 25
>>> s.storeparams('mass')
>>> s.data('mass')[1]   
[25]

Store with time stamp:

>>> s = c4d.state(x = 100, vx = 10)
>>> s.mass = 25
>>> s.storeparams('mass', t = 0.1)
>>> s.data('mass')
(array([0.1]), array([25.]))

Store multiple parameters:

>>> s = c4d.state(x = 100, vx = 10)
>>> s.x_std = 5
>>> s.vx_std = 10
>>> s.storeparams(['x_std', 'vx_std'])
>>> s.data('x_std')[1]   
[5]
>>> s.data('vx_std')[1] 
[10]

Objects classification:

>>> s = c4d.state(x = 25, y = 25, w = 20, h = 10)
>>> np.random.seed(44)
>>> for i in range(3):
...   s.X += 1
...   s.w, s.h = np.random.randint(0, 50, 2)
...   if s.w > 40 or s.h > 20:
...     s.class_id = 'truck'
...   else:
...     s.class_id = 'car'
...   s.store() # stores the state
...   s.storeparams('class_id') # store the class_id parameter
>>> print('   x    y    w    h    class')  
>>> print(np.hstack((s.data()[:, 1:].astype(int), np.atleast_2d(s.data('class_id')[1]).T)))  
x   y   w   h   class
26  26  20  35  truck
27  27  49  45  car
28  28  3   32  car

The morphospectra implements a custom method getdim to update the dimension parameter dim with respect to the position coordinates:

>>> import types
>>> #
>>> def getdim(s):
...   if s.X[2] != 0:
...     # z
...     s.dim = 3
...   elif s.X[1] != 0:
...     # y
...     s.dim = 2
...   elif s.X[0] != 0:
...     # x
...     s.dim = 1
...   else:
...     # none
...     s.dim = 0
>>> #
>>> morphospectra = c4d.state(x = 0, y = 0, z = 0)
>>> morphospectra.dim = 0
>>> morphospectra.getdim = types.MethodType(getdim, morphospectra)
>>> #
>>> for r in range(10):
...   morphospectra.X = np.random.choice([0, 1], 3)
...   morphospectra.getdim()
...   morphospectra.store()
...   morphospectra.storeparams('dim')
>>> #
>>> print('x y z  | dim')  
>>> print('------------')  
>>> for x, dim in zip(morphospectra.data().astype(int)[:, 1 : 4].tolist(), morphospectra.data('dim')[1].tolist()):  
...   print(*(x + [' | '] + [dim]))
x y z  | dim
------------
0 1 0  |  2
1 1 0  |  2
1 0 0  |  1
0 1 1  |  3
1 0 0  |  1
1 1 1  |  3
1 1 0  |  2
1 1 0  |  2
1 0 1  |  3
1 0 1  |  3