DataMedSci / beprof

Beam Profile Analysing Tools
MIT License
2 stars 2 forks source link

Curve object creation #116

Closed antnieszka closed 7 years ago

antnieszka commented 7 years ago
>>> x = Curve([[1, 2], [3, 4]], dtype=float)
>>> x
Curve([[1, 2],
       [3, 4]])
>>> x.dtype
dtype('int64')

Giving other dtype values will also result in above effect - probably creation of Curve object is incorrect. Let's look at the class itself:

class Curve(np.ndarray):
    def __new__(cls, input_array, **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('...')
            raise IndexError('...')
        obj = np.asarray(input_array).view(cls)
...

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:

>>> x = Curve([[1, 2], [3, 4]])
>>> x
Curve([[ True,  True],
       [ True,  True]], dtype=bool)

So similar to other ndarray creation methods like:

>>> np.ones(5, dtype=bool)
array([ True,  True,  True,  True,  True], dtype=bool)

Maybe Curve should support argument like dtype? Or we could have it default as float, and optionally set.

antnieszka commented 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)
antnieszka commented 7 years ago

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')
grzanka commented 7 years ago

What about order parameter (https://docs.scipy.org/doc/numpy/reference/generated/numpy.asarray.html) ? I see it is also not passed to asarray

antnieszka commented 7 years ago

Shape is also omitted :/

grzanka commented 7 years ago

Do we need to pass shape expilicitely ? To what methdo ?

antnieszka commented 7 years ago

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):

antnieszka commented 7 years ago

and than object creation as follows: obj = np.asarray(input_array, dtype=dtype, order=order).view(cls)

grzanka commented 7 years ago

dtype=np.float or dtype=float ?

grzanka commented 7 years ago

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.

antnieszka commented 7 years ago

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?

grzanka commented 7 years ago

np.float is good compomise

antnieszka commented 7 years ago

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.

grzanka commented 7 years ago

Good, mention it in a comment, otherwise this knowledge will be lost.

antnieszka commented 7 years ago

Resolved in #110