Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending numpy arrays

I'm new to Python/numpy.

I'm trying to extend numpy.array to give it some functions that make it nice for representing images (e.g. convert to greyscale etc).

import numpy as np
import cv2
from support import *
import matplotlib.pyplot as plt

class Frame(np.array):
    def __init__(self):
        print "new frame"


f = Frame()

currently this gives me:

  File "o.py", line 6, in <module>
    class Frame(np.array):
TypeError: Error when calling the metaclass bases
    cannot create 'builtin_function_or_method' instances

I don't understand why this is an issue for Python?

like image 882
cjm2671 Avatar asked Jun 06 '26 09:06

cjm2671


1 Answers

You want to be subclassing np.ndarray, not np.array, but it's a little more complicated than just swapping on out for the other in your example. It's probably worth taking a look at the documentation: http://docs.scipy.org/doc/numpy/user/basics.subclassing.html

like image 177
JoshAdel Avatar answered Jun 08 '26 22:06

JoshAdel