简体   繁体   English

使用__numpy_ufunc __()

[英]Use of __numpy_ufunc__()

I'm trying to use the __numpy_ufunc__() method explained here in the Numpy v1.11 docs , to override the behavior of numpy ufuncs on a subclass of ndarray , but it never seems to get called. 我试图使用__numpy_ufunc__()方法解释在numpy的1.11文档这里 ,覆盖上的一个子类numpy的ufuncs的行为ndarray ,但它似乎永远不会被调用。 Despite this use case being listed in the guide, I can't find any examples of anyone actually using __numpy_ufunc__() . 尽管指南中列出了这个用例,但我找不到任何实际使用__numpy_ufunc__()例子。 Has anyone tried this? 有没人试过这个? Here's a minimal example: 这是一个最小的例子:

# Check python version
import sys
print(sys.version)

3.5.1 |Continuum Analytics, Inc.| 3.5.1 | Continuum Analytics,Inc。| (default, Jun 15 2016, 15:32:45) (默认,2016年6月15日,15:32:45)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-1) [GCC 4.4.7 20120313(Red Hat 4.4.7-1)

# Check numpy version
import numpy as np
print(np.__version__)

1.11.2 1.11.2

# Subclass ndarray as discussed in 
# https://docs.scipy.org/doc/numpy/user/basics.subclassing.html
class Function(np.ndarray):

    # Create subclass object by view
    def __new__(cls):
        obj = np.asarray([1,2,3]).view(cls)
        return obj

    # I'm not even adding anything functionality yet
    def __array_finalize(self,obj): pass

    # Override ufuncs
    def __numpy_ufunc__(ufunc, method, i, inputs, **kwargs):
        print("In PF __numpy_ufunc__")
        # do other stuff here if I want to 
        # and probably need to return a value...

# Create two Functions
f1=Function()
f2=Function()

# Check that they are correctly initialized as Function objects
# not just ndarrays
print(type(f1),type(f2))

⟨class ' main .Function'⟩ ⟨class ' main .Function'⟩ ⟨class的主要 .Function'⟩⟨class的主要 .Function'⟩

# Add using operator
f1+f2

Function([2, 4, 6]) 功能([2,4,6])

# Add, explicitly demanding a numpy ufunc
np.add(f1,f2)

Function([2, 4, 6]) 功能([2,4,6])

Clearly, the subclassing works, and it's using numpy to add arrays behind the scenes. 很明显,子类化工作,并使用numpy在幕后添加数组。 I'm using a new enough version of numpy to employ the __numpy_ufunc__() feature (according to that docs page, it's new in v1.11). 我正在使用一个足够新的numpy版本来使用__numpy_ufunc__()功能(根据该文档页面,它是v1.11中的新功能)。 But this code never prints out "In PF __numpy_ufunc__" . 但是这段代码永远不会输出"In PF __numpy_ufunc__" What gives? 是什么赋予了?

This functionality has finally been released in Numpy 1.13 under a new name: 此功能最终在Numpy 1.13中以新名称发布:

__array_ufunc__ added __array_ufunc__补充道

This is the renamed and redesigned __numpy_ufunc__ . 这是重命名和重新设计的__numpy_ufunc__ Any class, ndarray subclass or not, can define this method or set it to None in order to override the behavior of NumPy's ufuncs. 任何类,ndarray子类是否可以定义此方法或将其设置为None以覆盖NumPy的ufuncs的行为。 This works quite similarly to Python's __mul__ and other binary operation routines. 这与Python的__mul__和其他二进制操作例程非常相似。 See the documentation for a more detailed description of the implementation and behavior of this new option. 有关此新选项的实现和行为的更详细说明,请参阅文档。 The API is provisional, we do not yet guarantee backward compatibility as modifications may be made pending feedback. API是临时的,我们尚不保证向后兼容性,因为可能会在等待反馈之前进行修改。 See the NEP and documentation for more details. 有关更多详细信息,请参阅NEP文档

This should resolve this issue. 这应该可以解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM