I can't figure out how to use the CArray trait. Why does this class
from traits.api import HasTraits, CArray, Float,Int
import numpy as np
class Coordinate3D(HasTraits):
coordinate = CArray(Float(), shape=(1,3) )
def _coordinate_default(self):
return np.array([1.,2.,3.])
apparently not use my _name_default() method?
In [152]: c=Coordinate3D()
In [153]: c.coordinate
Out[153]: np.array([[ 0., 0., 0.]])
I would have expected np.array([1,2,3])! The _name_default() seems to work with Int
class A(HasTraits):
a=Int
def _a_default(self):
return 2
In [163]: a=A()
In [164]: a.a
Out[164]: 2
So what I am doing wrong here? Also, I can't assign values:
In [181]: c.coordinate=[1,2,3]
TraitError: The 'coordinate' trait of a Coordinate3D instance must be an array of
float64 values with shape (1, 3), but a value of array([ 1., 2., 3.]) <type
'numpy.ndarray'> was specified.
Same error message with
In [182]: c.coordinate=np.array([1,2,3])