Closed antnieszka closed 7 years ago
dtype in action:
x = Curve([[1, 2], [3, 4]], dtype=bool)
x
Curve([[ True, True],
[ True, True]], dtype=bool)
x = Curve([[1, 2], [3, 4]], dtype=float)
x
Curve([[ 1., 2.],
[ 3., 4.]])
x = Curve([[1.0, 0.2], [-3.3, 4.5]], dtype=int)
x
Curve([[ 1, 0],
[-3, 4]])
all I did in Curve class was:
def __new__(cls, input_array, dtype=float, **meta):
shape = np.shape(input_array)
logger.info('Creating Curve object of shape {0} metadata is: {1}'.format(shape, meta))
if shape[1] != 2 and shape[1] != 3:
logger.error('...')
obj = np.asarray(input_array, dtype=dtype).view(cls)
Another profit - this fails:
Curve([['a', 'b']]))
ValueError: could not convert string to float: 'a'
Unless:
Curve([['a', 'b']], dtype=str)
Curve([['a', 'b']],
dtype='<U1')
What about order
parameter (https://docs.scipy.org/doc/numpy/reference/generated/numpy.asarray.html) ?
I see it is also not passed to asarray
Shape is also omitted :/
Do we need to pass shape expilicitely ? To what methdo ?
No, I was just looking at functions like zeros, ones.
numpy.asarray
has no such parameter and we don't need it too.
So how about this as Curvenew?:
def __new__(cls, input_array, dtype=float, order='C' **meta):
and than object creation as follows:
obj = np.asarray(input_array, dtype=dtype, order=order).view(cls)
dtype=np.float
or dtype=float
?
Can you also add a comment why we use shape = np.shape(input_array)
instead of shape = input_array.shape
?
Check how does it work when for example input_array = 0
.
np.float
I think... also I have the default numpy float set to float64, but on other machines it could be float32. Maybe be default should be float32?
np.float
is good compomise
Probably because we don't know much about input_array and we don't know if it has the attribute .shape
.
e.g. np.shape('whatever')
returns ()
. So we avoid AttributeError
.
Good, mention it in a comment, otherwise this knowledge will be lost.
Resolved in #110
Giving other dtype values will also result in above effect - probably creation of Curve object is incorrect. Let's look at the class itself:
In the last line shown we have
np.assarray
which also casts data if we pass dtype to it. I'll go with 'bool' just to check the effect:obj = np.asarray(input_array, dtype=bool).view(cls)
result:So similar to other ndarray creation methods like:
Maybe Curve should support argument like dtype? Or we could have it default as float, and optionally set.