我是Numpy的ndarray类的子类,添加了一些元数据和其他方法.我试图按照指示这篇文章和那一个.但是,一些Numpy(或Scipy)函数返回基类"ndarray"而不是我的自定义子类.其他Numpy函数返回我的子类,我不知道差异的原因是什么.如何让所有numpy/scipy函数返回我的子类?这就是我做的:
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 = …Run Code Online (Sandbox Code Playgroud)