一些Numpy函数返回ndarray而不是我的子类 [英] some Numpy functions return ndarray instead of my subclass

查看:110
本文介绍了一些Numpy函数返回ndarray而不是我的子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Numpy的ndarray类子类化,并添加了一些元数据和其他方法.我正在尝试按照本文

I am subclassing Numpy's ndarray class, adding some meta-data and additional methods. I'm trying to follow the instructions in this article and that one. However, some Numpy (or Scipy) functions return the base class "ndarray" instead of my custom subclass. Other Numpy functions DO return my subclass, and I don't know what's the reason for the difference. How can I make all the numpy/scipy functions return my subclass? here's what I did:

class Signal(np.ndarray):
    def __new__(cls, filename):
        #print "In __new__" #TEMP DEBUG
        ret = np.fromfile(filename, dtype = np.int32)
        ret = ret.view(cls) # convert to my class, i.e. Signal
        ret.parse_filename(filename)
        return ret

    def __array_finalize__(self, obj):
        #print "in __array_finalize__" #TEMP DEBUG
        if obj is None: return # shouldn't actually happen.
        # copy meta-fields from source, if it has them (otherwise put None's)
        self.filename = getattr(obj, "filename", None)
        self.folder = getattr(obj, "folder", None)
        self.label = getattr(obj, "label", None)
        self.date = getattr(obj, "date", None)
        self.time = getattr(obj, "time", None)
        #etc

以下是一些用法示例:

这些工作符合预期-

>>> s = Signal(filename)
>>> s2 = s[10:20]
>>> type (s2)
<class '__main__.Signal'>
>>> s3 = s + 17
>>> type (s3)
<class '__main__.Signal'>
>>> s4 = np.sqrt(s)
>>> type(s4)
<class '__main__.Signal'>

但是,这些呢?

>>> s5 = log10(s)
>>> type(s5)
<type 'numpy.ndarray'>
>>> s6 = np.fft.fft(s)
>>> type(s6)
<type 'numpy.ndarray'>

查看fftlog10的代码,我看到它们使用了asarray(),这会剥离子类并返回一个ndarray,以解释其行为.因此,我的问题不是为什么,从技术上讲,这会发生",而是一个设计问题-我应该如何编写代码以免发生这种情况?

looking into the code of fft and log10 I can see that they use asarray(), which strips the subclass and returns an ndarray, explaining the behavior. Therefore, my question isn't "why, technically, this happens" but more a design question - how should I write my code so this doesn't happen?

p.s.我既是Python的新手,还是Stack Overflow的新手,所以请原谅任何明显的错误或不当之处.

p.s. I'm a newbie both at Python and here on Stack Overflow, so please excuse any obvious mistakes or inappropriateness...

谢谢, 盖伊.

推荐答案

我不确定fft,但是np.log10 http://docs.scipy.org/doc/numpy/reference/ufuncs.html#output-type-determination

I am not sure about fft, but np.log10 is a ufunc. The following page explains how the output type of a ufunc is determined: http://docs.scipy.org/doc/numpy/reference/ufuncs.html#output-type-determination

尽管fft总是返回ndarray也不会令我感到惊讶(我没有看过源代码,但是FFT显然不适合ufunc的定义).如果是这种情况,您始终可以编写自己的包装器,然后调用该包装器.

It wouldn't surprise me if fft always returned an ndarray though (I haven't looked at the source code, but the FFT clearly doesn't fit the definition of a ufunc). If that's the case, you can always write your own wrapper and call that instead.

这篇关于一些Numpy函数返回ndarray而不是我的子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆