473,405 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

numpy subclassing

3
I would like to ask you a quick question:
I am facing the opposite problem described here:
http://docs.scipy.org/doc/numpy/user...bclassing.html

In short, I want to create a class Dummy(np.ndarray) which returns a plain array whenever is sliced or viewed. I cannot figure out how.

I would really appreciate you help, here is a chunk of code, what is commented was an attempt which didn’t work.
Thanks a lot,
Jacopo

Expand|Select|Wrap|Line Numbers
  1. import numpy as np 
  2.  
  3. class Dummy(np.ndarray): 
  4.  
  5.     def __del__(self): 
  6.         print "__del__" 
  7.     def __new__(cls, array): 
  8.         print "__new__" 
  9.         obj=array.view(cls) 
  10.         return obj 
  11.     def __array_finalize__(self, obj): 
  12.         print "__array_finalize__" 
  13.         #self=self.view(np.ndarray) 
  14.         #self=np.asarray(self) 
  15.     def __repr__(self): 
  16.         return "%s"%np.asarray(self) 
  17.  
  18. p=Dummy(np.ones(5)) 
  19. print type(p)
Jul 23 '09 #1
2 3049
bvdet
2,851 Expert Mod 2GB
I don't know much about Numpy. If I understand you correctly, you want a slice of an instance of your subclass to return a list or tuple. Try defining a __getslice__() method.
Expand|Select|Wrap|Line Numbers
  1. import numpy as np
  2.  
  3. class N(np.ndarray):
  4.  
  5.     def __new__(cls, obj): 
  6.         return obj.view(cls) 
  7.  
  8.     def __getslice__(self,i=None,j=None):
  9.         return tuple(self.view()[i:j:])
  10.  
  11. p = N(np.ones(9))
  12. print p
  13. print p[1:3]
  14. print p[:3]
  15. print p[3:]
  16. print p[1:8:3]
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> [ 1.  1.  1.  1.  1.  1.  1.  1.  1.]
  2. (1.0, 1.0)
  3. (1.0, 1.0, 1.0)
  4. (1.0, 1.0, 1.0, 1.0, 1.0, 1.0)
  5. [ 1.  1.  1.]
It does not work for a slice with a stride. The __getslice__() method has been deprecated since version 2.0.
Jul 25 '09 #2
jpecci
3
thanks a lot. I think I will do something on these lines even if
I was hoping in something which didnt involve getslice() since it has been deprecated.
jacopo
Jul 26 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Gary Wessle | last post by:
Hi I am trying to install NumPy in my debian/testing linux 2.6.15-1-686. with no numpy for debian/testing, I am left alone, since the experimental version available by debian will result in a...
3
by: Iljya | last post by:
Hello, I need to pickle the type numpy.float32 but encounter an error when I try to do so. I am able to pickle the array itself, it is specifically the type that I cannot pickle. I am using:...
0
by: robert | last post by:
just a note - some speed comparisons : 0.60627370238398726 0.42836673376223189 0.36965815487747022 0.016557970357098384 0.15692469294117473 0.01951756438393204
2
by: robert | last post by:
in Gnuplot (Gnuplot.utils) the input array will be converted to a Numeric float array as shown below. When I insert a numpy array into Gnuplot like that below, numbers 7.44 are cast to 7.0 Why is...
2
by: Chris Smith | last post by:
Howdy, I'm a college student and for one of we are writing programs to numerically compute the parameters of antenna arrays. I decided to use Python to code up my programs. Up to now I haven't...
5
by: robert | last post by:
Turning algs for old NumPy modules into numpy code I suffer from this: Upon further processing of returns of numpy calculations, lots of data in an apps object tree will become elementary numpy...
3
by: Duncan Smith | last post by:
Hello, Since moving to numpy I've had a few problems with my existing code. It basically revolves around the numpy scalar types. e.g. ------------------------------------------------ array(,...
5
by: Marc Oldenhof | last post by:
Hello all, I'm pretty new to Python, but use it a lot lately. I'm getting a crazy error trying to do operations on a string list after importing numpy. Minimal example: Python 2.5.1...
2
by: Travis Oliphant | last post by:
I wanted to point anybody interested to a blog post that describes a useful pattern for having a NumPy array that points to the memory created by a different memory manager than the standard one...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.