[Numpy-discussion] ndarray sub-classing and append function

2012-03-30 Thread Prashant Saxena
Hi,

I am sub-classing numpy.ndarry for vector array representation. The append 
function is like this:

    def append(self, other):
       self = numpy.append(self, [other], axis=0)

Example:
vary = VectorArray([v1, v2])
#vary = numpy.append(vary, [v1], axis=0)
vary.append(v1)

The commented syntax (numpy syntax) is working but "vary.append(v1)" is not 
working.

Any help?

Cheers

Prashant___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] numpy/numexpr performance (particle simulation)

2009-06-29 Thread Prashant Saxena
Hi,

I am doing a little test using numpy and numexpr to do a particle simulation. I 
never used either of them much and this is the first time I have to go deeper. 
Here is the code:

import numpy as np
import numexpr as nexpr

class Particle( object ):

def __init__( self, id ):
self.position = [0.0, 0.0, 0.0]
self.color = [0.5, 0.5, 0.5]
self.direction = [0.0, 0.0, 0.0]
self..id = id
self.lifeSpan = 0
self.type = 0

class Emitter( object ):

def __init__( self ):
self.particles = np.empty([], dtype=Particle())
self.currentParticleId = 0
self.numParticles = 1000
self.emissionRate = 10
self.position = [0.0, 0.0, 0.0]
self.rotation = [0.0, 0.0, 0.0]

# Add a single particle
def addParticle( self ):
"""
Add a single particle in the emitter.
"""
if self.currentParticleId < self.numParticles:
self.particles = np.append( self.particles, Particle( 
self..currentParticleId ) )
self.currentParticleId+=1   


###
Problem 1:
self.particles = np.empty([], dtype=Particle())
In "Emitter" class, how do I initialize a numpy array of "Particle" type?

Problem 2:
If problem 1 can be solved, is it possible to use numexpr to alter the position 
of each particle's position
in emitter.particles array. For example, multiply x,y and z of each particle by 
using three random values. In other words

self.particles[0].position*= random(1.0)
self.particles[1].position*= random(2.0)
self.particles[2].position*= random(3.0)

If problem 1 cannot be solved then may be I could use something like this using 
simple list:

a = [Particle(0), Particle(1)]

How do I modify the position of each particle in array"a" using numexpr?

I would be using 1-5 million particles where each particle's attribute postion, 
rotation and color will be modified using some complex
equations. The above code is just the begining hence any tips for performance 
would be greatly apperciated.

Regards

Prashant


  Yahoo! recommends that you upgrade to the new and safer Internet Explorer 
8. http://downloads.yahoo.com/in/internetexplorer/___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] image processing using numpy-scipy?

2009-02-27 Thread Prashant Saxena
Well,

I came up with a slightly different approach.

Get the first row on the image.
It would be something like this:
[1,1,1,0,0,0,1,1,0,0,1]
1 = white, 0 = black.
Run the floodfill on [0],[6] and [10] pixel
if floodfill area is smaller then given area then paint that area black
if floodfill area is larger then given area then copy that area to another 
image of same dimension and paint this area also black here.
This would save tons of per pixel iteration. Next row would surely be having 
less number of white pixels to do the iteration.

Now I need an efficient floodfill algorithm for above logic and I hope this 
will work.

Prashant





From: Zachary Pincus 
To: Discussion of Numerical Python 
Sent: Friday, 27 February, 2009 11:41:43 PM
Subject: Re: [Numpy-discussion] image processing using numpy-scipy?

Hello,

>> This a little wiered problem. I am having a black and white image.  
>> (black
>> background)
>> Entire image is filled with noisy white patterns of different size  
>> and
>> shape. I need to fill the
>> white patches if there area is more then given one. Logically this  
>> could
>> possible to use a quickfill algorithm
>> for every pixel and if the filled area generated by that pixel is  
>> more then
>> given area(in pixel) then paint that
>> patch (filled area) with black.
>>
>> I have read the docs of PIL but there is no function for this. Can  
>> I use
>> numpy-scipy for the matter?
>
> Sure, there are a couple of options.  First, look at scipy.ndimage if
> there is anything there you can use (i.e. maybe binary dilation is
> sufficient).
>
> Otherwise, I've got some connected component code at:
>
> http://mentat.za.net/source/connected_components.tar.bz2
>
> (The repository is at http://mentat.za.net/hg/ccomp if you prefer)

I think that scipy.ndimage.label also provides connected-component  
labeling, with arbitrary connectivity. But this problem could also be  
solvable via dilation / erosion, if the area constraint is loose --  
e.g. if you want to quash all blobs smaller than, say, 5x5 pixels, you  
could just erode for 5 iterations, and then dilate the eroded image  
back for '-1' iterations (which causes the ndimage algorithm to  
iterate until no pixels change), using a mask of the original image  
(so that no pixels outside of the original blobs are turned on). This  
basically means that any object that survives the original erosion  
will be dilated back to its initial size. (Similar tricks can also be  
used to find / kill objects touching the edges -- use a non-zero  
constant out-of-bounds value and dilate an all-zeros array for -1  
iterations, using the original array as the mask. Then only objects  
touching the edge will get filled in...)

Alternately, if you need a very stringent area threshold -- e.g.  
remove all objects with five or fewer total pixels, regardless of  
their configuration -- then the connected-components approach is  
required. Below is a stab at it, though note that there's a slow step  
that I can't right now figure out how to avoid, short of coding just  
that in C or with cython or something...

Zach



import numpy
import scipy.ndimage as ndimage

_4_connected = numpy.array([[0, 1, 0],
 [1, 1, 1],
 [0, 1, 0]], dtype=bool)

def kill_small(binary_image, min_size, structure=_4_connected):
   label_image, num_objects = ndimage.label(binary_image, structure)
   # Label 0 is the background...
   object_labels = numpy.arange(1, num_objects+1)
   # Get the area of each labeled object by summing up the pixel  
values in the
   # binary image. (Divide the binary image by its max val to ensure  
that the image
   # consists of 1s and 0s, so that the sum equals the area in pixels.)
   areas = numpy.array(nd.sum(binary_image / binary_image.max(),  
label_image, object_labels))
   big_objects = object_labels[areas >= min_size]
   # This part will be pretty slow! But I can't think right now how to  
speed it up.
   # (If there are more big objects than small objects, it would be  
faster to
   # reverse this process and xor out the small objects from the  
binary image,
   # rather than the below, which or's up a new image of just the  
large objects.)
   big_object_image = numpy.zeros(binary_image.shape, dtype=bool)
   for bo in big_objects:
 big_object_image |= label_image == bo
   return big_object_image
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy..org/mailman/listinfo/numpy-discussion



  Add more friends to your messenger and enjoy! Go to 
http://messenger.yahoo.com/invite/___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] image processing using numpy-scipy?

2009-02-27 Thread Prashant Saxena
Hi,

This a little wiered problem. I am having a black and white image. (black 
background)
Entire image is filled with noisy white patterns of different size and shape. I 
need to fill the
white patches if there area is more then given one. Logically this could 
possible to use a quickfill algorithm
for every pixel and if the filled area generated by that pixel is more then 
given area(in pixel) then paint that
patch (filled area) with black.

I have read the docs of PIL but there is no function for this. Can I use 
numpy-scipy for the matter?
The image size is 1K.

Any help would be greatly apperciated.

Regards
Prashant



  Connect with friends all over the world. Get Yahoo! India Messenger at 
http://in.messenger.yahoo.com/?wm=n/___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] replace array values efficiently

2009-01-08 Thread Prashant Saxena
Hi,

I am new to numpy and getting my hands on slowly. 

How do you replace integers from strings in an integer array. (1D)

For example:

array = {1,1,1,2,3,3,4}

replace 1 with "apple"
replace 2 with "cheery"
replace 3 with "mango"
replace 4 with "banana"

I know the general solution, but I am looking for an efficient way, supported 
by numpy/scipy to do this kind of conversion as fast as possible.

Thanks

Prashant



  Add more friends to your messenger and enjoy! Go to 
http://messenger.yahoo.com/invite/___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] array not appending

2008-12-18 Thread Prashant Saxena

ST = np.empty((), dtype=np.float32)
ST = np.append(ST, 10.0)

This works, is it proper way to do so?

One more prob

ST.size returns 2.

Why? I have added only one element.

Prashant





From: Gael Varoquaux 
To: Discussion of Numerical Python 
Sent: Thursday, 18 December, 2008 4:20:49 PM
Subject: Re: [Numpy-discussion] array not appending

On Thu, Dec 18, 2008 at 04:19:20PM +0530, Prashant Saxena wrote:
>How do I solve this?

If you want appending in place you have to use a python list. If you don't
need modification in place, np.append returns an array with the appended
number.

Gaël
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy..org/mailman/listinfo/numpy-discussion



  Add more friends to your messenger and enjoy! Go to 
http://messenger.yahoo.com/invite/___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] array not appending

2008-12-18 Thread Prashant Saxena
How do I solve this?

Thanks
Prashant





From: Gael Varoquaux 
To: Discussion of Numerical Python 
Sent: Thursday, 18 December, 2008 4:03:34 PM
Subject: Re: [Numpy-discussion] array not appending

On Thu, Dec 18, 2008 at 03:52:23PM +0530, Prashant Saxena wrote:
>In [43]: ST = np.empty([], dtype=np.float32)

>In [44]: np.append(ST, 10.0)
>Out[44]: array([  3.8603e-38,   1.e+01])

>In [45]: np.append(ST, 10.0)
>Out[45]: array([  3.8603e-38,   1.e+01])

>In [46]: print ST
>3.8602707e-038

>What's wrong here?

Nothing. If you look at the documentation, np.append does not modify
in place the array.:

'''
Returns
---
out : ndarray
A copy of `arr` with `values` appended to `axis`.  Note that `append`
does not occur in-place: a new array is allocated and filled.
'''

Modification in place is not possible with the numpy model of an array.

Gaël
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion



  Add more friends to your messenger and enjoy! Go to 
http://messenger.yahoo.com/invite/___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] array not appending

2008-12-18 Thread Prashant Saxena
Hi,

This is copied from ipython console. 

In [42]: import numpy as np

In [43]: ST = np.empty([], dtype=np.float32)

In [44]: np..append(ST, 10.0)
Out[44]: array([  3.8603e-38,   1.e+01])

In [45]: np.append(ST, 10.0)
Out[45]: array([  3.8603e-38,   1.e+01])

In [46]: print ST
3.8602707e-038

What's wrong here?

win XP 32
numpy 1.2.1
python 2.5.2


Prashant



  Bollywood news, movie reviews, film trailers and more! Go to 
http://in.movies.yahoo.com/___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] numpy fileIO

2008-10-16 Thread Prashant Saxena
I have just started doing my tests using numpy and hdpy( 
http://h5py.alfven..org/ ).
Hope either of these would give me what I need.

Prashant



- Original Message 
From: Francesc Alted <[EMAIL PROTECTED]>
To: Discussion of Numerical Python 
Sent: Thursday, 16 October, 2008 10:03:22 PM
Subject: Re: [Numpy-discussion] numpy fileIO

A Thursday 16 October 2008, Prashant Saxena escrigué:
> Hi,
>
> I have never used numpy in my python applications until now. I am
> writing a python/openGL based tool to manipulate 3d geometrical
> data(meshes, curve, points etc.) I would be using numpy to store
> these objects(vertices, edges, faces, index etc.) at run time. One of
> my major concern is numpy's file IO capabilities. Geometrical objects
> would be stored in a structures, for example a logical structure to
> store a mesh goes like this:
>
> struct(
> name(string)
> vertex(numpy.float16)[x, y, z]
> normal(numpy.float16)[x, y, z]
> edges(numpy.int)
> st(2d numpy.float16)[u, v]
> index(numpy.int)
> )
>
> There would be different structures for curve, patch, points and rest
> of the primitives. I am sure numpy users must have encounered similar
> scenerio where you need to dump this data to a file and read it back.
> In my case, a general assumption of nearly 50-150 megs of data would
> be considered as normal size. Before I go deep into coding It would
> be great if numpy user can share their expreience for the task.
>
> I am also open for unconventional or off the route methods, provided
> they can do the job.(C/c++, 3rd party modules etc.) Here is the
> summery of IO operations I would be working on:
>
> 1. Write different structures to a file.
> 2. Read data back from file.
> 3. if structure can be tagged(int or string) then read a particular
> structure using tag, from file.

Your structure seems like can be expressed through a record array [1].  
If that is the case, just try the included tofile()/fromfile() 
utilities included in numpy for saving/retrieving them.

If you need more functionality for recarray I/O, you can have a look at 
pytables [2], which allows you to access directly the recarrays on disk 
row by row (even if compressed) or use complex expressions to quickly 
select interesting parts, among other niceties.

If your data structures can't be fit in recarray structures directly, 
you may want to try to decompose them in smaller constructs and relate 
them in file through pointers (indices to other constructs).

[1] http://www.scipy.org/RecordArrays
[2] http://www.pytables.org

Hope that helps,

-- 
Francesc Alted
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion



  Add more friends to your messenger and enjoy! Go to 
http://messenger.yahoo.com/invite/___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] numpy fileIO

2008-10-16 Thread Prashant Saxena
I am seeing all the OSS for this purpose but I would stick to use pure python 
and the scene graph I am developing for the application. I already did some 
test using pyOpenGL/python/wx.GLcanvas and a large data set of roughly 4000+ 
objects consisting nearly 1 million polygons using display lists and results 
were more then satisfactory. Although it was done quickly and preformence can 
be improved further by code optimizations. numpy can play a crucial role
in my application and I have to make sure before pinning down any decisions 
regarding tools and techniques I would be using.   

Prashant



- Original Message 
From: Nadav Horesh <[EMAIL PROTECTED]>
To: Discussion of Numerical Python 
Sent: Thursday, 16 October, 2008 8:13:38 PM
Subject: Re: [Numpy-discussion] numpy fileIO

Did you consider VTK?
I've used it a *little*: Probably it contains all the structures you need, 
along with c++ routines for I/O, manipulation and
(OpenGL) display, and a python interface.

  Nadav.

-הודעה מקורית-
מאת: [EMAIL PROTECTED] בשם Prashant Saxena
נשלח: ה 16-אוקטובר-08 13:12
אל: numpy-discussion@scipy.org
נושא: [Numpy-discussion] numpy fileIO

Hi,

I have never used numpy in my python applications until now. I am writing a 
python/openGL based tool to manipulate 3d geometrical data(meshes, curve, 
points etc.) I would be using numpy to store these objects(vertices, edges, 
faces, index etc.) at run time. One of my major concern is numpy's file IO 
capabilities. Geometrical objects would be stored in a structures, for example 
a logical structure to store a mesh goes like this:

struct(
name(string)
vertex(numpy.float16)[x, y, z]
normal(numpy.float16)[x, y, z]
edges(numpy.int)
st(2d numpy.float16)[u, v]
index(numpy.int)
)

There would be different structures for curve, patch, points and rest of the 
primitives. I am sure numpy users must have encounered similar scenerio where
you need to dump this data to a file and read it back. In my case, a general 
assumption of nearly 50-150 megs of data would be considered as normal size.
Before I go deep into coding It would be great if numpy user can share their 
expreience for the task.

I am also open for unconventional or off the route methods, provided they can 
do the job.(C/c++, 3rd party modules etc.)
Here is the summery of IO operations I would be working on:

1. Write different structures to a file.
2. Read data back from file.
3. if structure can be tagged(int or string) then read a particular structure 
using tag, from file.

Hope to here from numpy users soon :-)

Regards

Prashant



  Add more friends to your messenger and enjoy! Go to 
http://messenger.yahoo.com/invite/


  Unlimited freedom, unlimited storage. Get it now, on 
http://help.yahoo.com/l/in/yahoo/mail/yahoomail/tools/tools-08.html/___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] numpy fileIO

2008-10-16 Thread Prashant Saxena
Hi,

I have never used numpy in my python applications until now. I am writing a 
python/openGL based tool to manipulate 3d geometrical data(meshes, curve, 
points etc.) I would be using numpy to store these objects(vertices, edges, 
faces, index etc.) at run time. One of my major concern is numpy's file IO 
capabilities. Geometrical objects would be stored in a structures, for example 
a logical structure to store a mesh goes like this:

struct(
name(string)
vertex(numpy.float16)[x, y, z]
normal(numpy.float16)[x, y, z]
edges(numpy.int)
st(2d numpy.float16)[u, v]
index(numpy.int)
)

There would be different structures for curve, patch, points and rest of the 
primitives. I am sure numpy users must have encounered similar scenerio where
you need to dump this data to a file and read it back. In my case, a general 
assumption of nearly 50-150 megs of data would be considered as normal size.
Before I go deep into coding It would be great if numpy user can share their 
expreience for the task.

I am also open for unconventional or off the route methods, provided they can 
do the job.(C/c++, 3rd party modules etc.)
Here is the summery of IO operations I would be working on:

1. Write different structures to a file.
2. Read data back from file.
3. if structure can be tagged(int or string) then read a particular structure 
using tag, from file.

Hope to here from numpy users soon :-)

Regards

Prashant



  Add more friends to your messenger and enjoy! Go to 
http://messenger.yahoo.com/invite/___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] first post, simple questions

2008-08-21 Thread Prashant Saxena
Hi,

numpy rocks!!!

import numpy
linsp = numpy.linspace
red = linsp(0, 255, 50)
green = linsp(125, 150, 50)
blue = linsp(175, 255, 50)

array's elements are float. How do I convert them into integer?

I need to build a new array from red, green, blue. like this:

[[ red[0], green[0], blue[0] ],
 [ red[1], green[1], blue[1] ],
 [ red[2], green[3], blue[3] ],
 
 
 [ red[49], green[49], blue[49] ],
]

how do I create such an array?

Are there any issues regarding compilation of simple python scripts using numpy 
functions and cython?

Thanks



  ___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion