[SciPy-dev] scipy.stats._chk_asarray

Pierre GM pgmdevlist at gmail.com
Wed Jun 3 02:17:56 EDT 2009


On Jun 3, 2009, at 1:09 AM, josef.pktd at gmail.com wrote:
>
> Given my experience with views, I would prefer to limit them to very
> local usage, e.g. views on transposed arrays don't work,

??
 >>> m = np.matrix([1,2,3])
 >>> m.T
matrix([[1],
         [2],
         [3]])
 >>> m.T.view(np.ndarray)
array([[1],
        [2],
        [3]])

What case did you have in mind ?

>>> And what is the best way to check whether an array is a plain  
>>> ndarray
>>> and not a subclass instance?
>>
>> Er, why do you want to do that ?
>
> To get fast track for users that deliver already directly usable data,
> without special type handling. This will be more relevant for
> stats.models to handle recarrays and masked arrays, and ?

Mmh. We'll see.

>
> If someone gives me this decorator, I will use it, but I don't know
> how to write a decorator that works for all input and output cases,
> and doesn't screw up our documentation system.

Try that.

def keepthetype(func):
     def wrapped(*args, **kwargs):
         first = args[0]
         if isinstance(first, np.ndarray):
             output_type = type(first)
         else:
             output_type = np.ndarray
         output = func(*args, **kwargs)
         if isinstance(output, np.ndarray):
             return output.view(output_type)
         return output
     wrapped.__name__ = func.__name__
     wrapped.__dict__ = func.__dict__
     wrapped.__doc__ = func.__doc__
     return wrapped




More information about the SciPy-Dev mailing list