From: Christopher B. <Chr...@no...> - 2006-06-01 19:05:08
|
I'm trying to get the (x,y) coords for all the points in a grid, bound by xmin, xmax, ymin, ymax. This list comprehension does it fine: Points = [(x,y) for x in xrange(minx, maxx) for y in xrange(miny, maxy)] But I can't think at the moment how to do it with numpy. Any ideas? Thanks, -Chris -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chr...@no... |
From: Robert K. <rob...@gm...> - 2006-06-01 19:20:10
|
Christopher Barker wrote: > > I'm trying to get the (x,y) coords for all the points in a grid, bound > by xmin, xmax, ymin, ymax. > > This list comprehension does it fine: > > Points = [(x,y) for x in xrange(minx, maxx) for y in xrange(miny, maxy)] > > But I can't think at the moment how to do it with numpy. Any ideas? In [4]: x, y = mgrid[0:10, 5:15] In [5]: x Out[5]: array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3, 3, 3, 3, 3], [4, 4, 4, 4, 4, 4, 4, 4, 4, 4], [5, 5, 5, 5, 5, 5, 5, 5, 5, 5], [6, 6, 6, 6, 6, 6, 6, 6, 6, 6], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]]) In [6]: y Out[6]: array([[ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]]) In [8]: points = column_stack((x.ravel(), y.ravel())) In [9]: points Out[9]: array([[ 0, 5], [ 0, 6], [ 0, 7], [ 0, 8], [ 0, 9], [ 0, 10], ... -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco |
From: Sasha <nd...@ma...> - 2006-06-01 19:26:11
|
>>> mgrid[0:10, 5:15].reshape(2,100).transpose() array([[ 0, 5], [ 0, 6], [ 0, 7], [ 0, 8], ...]) On 6/1/06, Robert Kern <rob...@gm...> wrote: > Christopher Barker wrote: > > > > I'm trying to get the (x,y) coords for all the points in a grid, bound > > by xmin, xmax, ymin, ymax. > > > > This list comprehension does it fine: > > > > Points = [(x,y) for x in xrange(minx, maxx) for y in xrange(miny, maxy)] > > > > But I can't think at the moment how to do it with numpy. Any ideas? > > In [4]: x, y = mgrid[0:10, 5:15] > > In [5]: x > Out[5]: > array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], > [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], > [2, 2, 2, 2, 2, 2, 2, 2, 2, 2], > [3, 3, 3, 3, 3, 3, 3, 3, 3, 3], > [4, 4, 4, 4, 4, 4, 4, 4, 4, 4], > [5, 5, 5, 5, 5, 5, 5, 5, 5, 5], > [6, 6, 6, 6, 6, 6, 6, 6, 6, 6], > [7, 7, 7, 7, 7, 7, 7, 7, 7, 7], > [8, 8, 8, 8, 8, 8, 8, 8, 8, 8], > [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]]) > > In [6]: y > Out[6]: > array([[ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], > [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], > [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], > [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], > [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], > [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], > [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], > [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], > [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], > [ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]]) > > In [8]: points = column_stack((x.ravel(), y.ravel())) > > In [9]: points > Out[9]: > array([[ 0, 5], > [ 0, 6], > [ 0, 7], > [ 0, 8], > [ 0, 9], > [ 0, 10], > ... > > -- > Robert Kern > > "I have come to believe that the whole world is an enigma, a harmless enigma > that is made terrible by our own mad attempt to interpret it as though it had > an underlying truth." > -- Umberto Eco > > > > ------------------------------------------------------- > All the advantages of Linux Managed Hosting--Without the Cost and Risk! > Fully trained technicians. The highest number of Red Hat certifications in > the hosting industry. Fanatical Support. Click to learn more > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642 > _______________________________________________ > Numpy-discussion mailing list > Num...@li... > https://lists.sourceforge.net/lists/listinfo/numpy-discussion > |
From: Charlie M. <cw...@gm...> - 2006-06-01 20:08:08
|
Here's my crack at it. pts = mgrid[minx:maxx,miny:maxy].transpose() pts.reshape(pts.size/2, 2) #pts is good to go On 6/1/06, Christopher Barker <Chr...@no...> wrote: > > I'm trying to get the (x,y) coords for all the points in a grid, bound > by xmin, xmax, ymin, ymax. > > This list comprehension does it fine: > > Points = [(x,y) for x in xrange(minx, maxx) for y in xrange(miny, maxy)] > > But I can't think at the moment how to do it with numpy. Any ideas? > > Thanks, > > -Chris > > > -- > Christopher Barker, Ph.D. > Oceanographer > > NOAA/OR&R/HAZMAT (206) 526-6959 voice > 7600 Sand Point Way NE (206) 526-6329 fax > Seattle, WA 98115 (206) 526-6317 main reception > > Chr...@no... > > > ------------------------------------------------------- > All the advantages of Linux Managed Hosting--Without the Cost and Risk! > Fully trained technicians. The highest number of Red Hat certifications in > the hosting industry. Fanatical Support. Click to learn more > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642 > _______________________________________________ > Numpy-discussion mailing list > Num...@li... > https://lists.sourceforge.net/lists/listinfo/numpy-discussion > |
From: Robert K. <rob...@gm...> - 2006-06-01 20:13:44
|
Charlie Moad wrote: > Here's my crack at it. > > pts = mgrid[minx:maxx,miny:maxy].transpose() > pts.reshape(pts.size/2, 2) > #pts is good to go Well, if we're going for terseness: points = mgrid[minx:maxx, miny:maxy].reshape(2, -1).transpose() -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco |
From: Christopher B. <Chr...@no...> - 2006-06-01 23:51:49
|
Robert Kern wrote: > points = mgrid[minx:maxx, miny:maxy].reshape(2, -1).transpose() As I need Numeric and numarray compatibility at this point, it seems the best I could come up with is below. I'm guessing the list comprehension may well be faster! -Chris #!/usr/bin/env python #import numpy as N #import Numeric as N import numarray as N Spacing = 2.0 minx = 0 maxx = 5 miny = 20 maxy = 22 print "minx", minx print "miny", miny print "maxx", maxx print "maxy", maxy ## # The nifty, terse, numpy way ## points = mgrid[minx:maxx, miny:maxy].reshape(2, -1).transpose() ## The Numeric and numarray way: x = N.arange(minx, maxx+Spacing, Spacing) # makeing sure to get the last point y = N.arange(miny, maxy+Spacing, Spacing) # an extra is OK points = N.zeros((len(y), len(x), 2), N.Float) x.shape = (1,-1) y.shape = (-1,1) points[:,:,0] += x points[:,:,1] += y points.shape = (-1,2) print points -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chr...@no... |
From: Robert K. <rob...@gm...> - 2006-06-02 00:16:59
|
Christopher Barker wrote: > Robert Kern wrote: > >>points = mgrid[minx:maxx, miny:maxy].reshape(2, -1).transpose() > > As I need Numeric and numarray compatibility at this point, it seems the > best I could come up with is below. Ah. It might help if you said that up front. (Untested, but what I usually did in the bad old days before I used scipy): x = arange(minx, maxx+step, step) # oy. y = arange(miny, maxy+step, step) nx = len(x) ny = len(y) x = repeat(x, ny) y = concatenate([y] * nx) points = transpose([x, y]) -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco |
From: Rob H. <ro...@ho...> - 2006-06-02 05:31:43
Attachments:
timer.py
|
Christopher Barker wrote: > x = N.arange(minx, maxx+Spacing, Spacing) # makeing sure to get the last > point > y = N.arange(miny, maxy+Spacing, Spacing) # an extra is OK > points = N.zeros((len(y), len(x), 2), N.Float) > x.shape = (1,-1) > y.shape = (-1,1) > points[:,:,0] += x > points[:,:,1] += y > points.shape = (-1,2) > > print points How about something like: >>> k=Numeric.repeat(range(0,5+1),Numeric.ones(6)*7) >>> l=Numeric.resize(range(0,6+1),[42]) >>> zone=Numeric.concatenate((k[:,Numeric.NewAxis],l[:,Numeric.NewAxis]),axis=1) >>> zone array([[0, 0], [0, 1], [0, 2], ... [5, 4], [5, 5], [5, 6]]) This is the same speed as Robert Kern's solution for large arrays, a bit slower for small arrays. Both are a little faster than yours. Rob -- Rob W.W. Hooft || ro...@ho... || http://www.hooft.net/people/rob/ |
From: Christopher B. <Chr...@no...> - 2006-06-02 16:57:29
Attachments:
junk.py
|
Robert Kern wrote: >> As I need Numeric and numarray compatibility at this point, it seems the > Ah. It might help if you said that up front. Yes, it would, but that would mean accepting that I need to keep backward compatibility -- I'm still hoping! > x = arange(minx, maxx+step, step) # oy. > y = arange(miny, maxy+step, step) > > nx = len(x) > ny = len(y) > > x = repeat(x, ny) > y = concatenate([y] * nx) > points = transpose([x, y]) Somehow I never think to use repeat. And why use repeat for x and concatenate for y? Rob Hooft wrote: > How about something like: > > >>> k=Numeric.repeat(range(0,5+1),Numeric.ones(6)*7) > >>> l=Numeric.resize(range(0,6+1),[42]) > >>> > zone=Numeric.concatenate((k[:,Numeric.NewAxis],l[:,Numeric.NewAxis]),axis=1) > This is the same speed as Robert Kern's solution for large arrays, a bit > slower for small arrays. Both are a little faster than yours. Did you time them? And yours only handles integers. This is my timing: For small arrays: Using numpy The Numpy way took: 0.020000 seconds My way took: 0.010000 seconds Robert's way took: 0.020000 seconds Using Numeric My way took: 0.010000 seconds Robert's way took: 0.020000 seconds Using numarray My way took: 0.070000 seconds Robert's way took: 0.120000 seconds Number of X: 4 Number of Y: 3 So my way was faster with all three packages for small arrays. For Medium arrays ( the size I'm most likely to be using ): Using numpy The Numpy way took: 0.120000 seconds My way took: 0.040000 seconds Robert's way took: 0.030000 seconds Using Numeric My way took: 0.040000 seconds Robert's way took: 0.030000 seconds Using numarray My way took: 0.090000 seconds Robert's way took: 1.070000 seconds Number of X: 21 Number of Y: 41 Now we're getting close, with mine faster with numarray, but Robert's faster with Numeric and numpy. For Large arrays: (still not very big, but as big as I'm likely to need) Using numpy The Numpy way took: 4.200000 seconds My way took: 0.660000 seconds Robert's way took: 0.340000 seconds Using Numeric My way took: 0.590000 seconds Robert's way took: 0.500000 seconds Using numarray My way took: 0.390000 seconds Robert's way took: 20.340000 seconds Number of X: 201 Number of Y: 241 So Robert's way still is faster with Numeric and numpy, but Much slower with numarray. As it's close with numpy and Numeric, but mine is much faster with numarray, I think I'll stick with mine. I note that while the numpy way, using mgrid(), is nice and clean to write, it is slower across the board. Perhaps mgrid(0 could use some optimization. This is exactly why I had suggested that one thing I wanted for numpy was an as-easy-to-use-as-possible C/C++ API. It would be nice to be able to write as many as possible of these kinds of utility functions in C as we could. In case anyone is interested, I'm using this to draw a grid of dots on the screen for my wxPython FloatCanvas. Every time the image is changed or moved or zoomed, I need to re-calculate the points before drawing them, so it's nice to have it fast. I've enclosed my test code. -Chris -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chr...@no... |
From: Robert K. <rob...@gm...> - 2006-06-02 18:35:54
|
Christopher Barker wrote: > Robert Kern wrote: >> x = repeat(x, ny) >> y = concatenate([y] * nx) >> points = transpose([x, y]) > > Somehow I never think to use repeat. And why use repeat for x and > concatenate for y? I guess you could use repeat() on y[newaxis] and then flatten it. y = repeat(y[newaxis], nx).ravel() > Using numpy > The Numpy way took: 0.020000 seconds > My way took: 0.010000 seconds > Robert's way took: 0.020000 seconds > Using Numeric > My way took: 0.010000 seconds > Robert's way took: 0.020000 seconds > Using numarray > My way took: 0.070000 seconds > Robert's way took: 0.120000 seconds > Number of X: 4 > Number of Y: 3 Those timings look real funny. I presume you are using a UNIX and time.clock(). Don't do that. It's a very poor timer on UNIX. Use time.time() on UNIX and time.clock() on Windows(). Even better, please use timeit.py instead. Tim Peters did a lot of work to make timeit.py do the right thing. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco |
From: Rob H. <ro...@ho...> - 2006-06-02 21:06:34
|
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Christopher Barker wrote: | Did you time them? And yours only handles integers. Yes I did, check the attachment of my previous message for a python module to time the three, with completely different results from yours (I'm using Numeric). The attachment also contains a floatified version of my demonstration. Rob - -- Rob W.W. Hooft || ro...@ho... || http://www.hooft.net/people/rob/ -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.3 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFEgKhRH7J/Cv8rb3QRAlk1AJ4vyt1F1Lr54sGMjHkp1hGXzcowJwCeMD5O CqkaDTpKOdDrAy7+v3Py7kw= =jnqb -----END PGP SIGNATURE----- |
From: Charlie M. <cw...@gm...> - 2006-06-01 23:51:53
|
That reshape should be "resize". Sorry. > Here's my crack at it. > > pts = mgrid[minx:maxx,miny:maxy].transpose() > pts.reshape(pts.size/2, 2) > #pts is good to go > > On 6/1/06, Christopher Barker <Chr...@no...> wrote: > > > > I'm trying to get the (x,y) coords for all the points in a grid, bound > > by xmin, xmax, ymin, ymax. > > > > This list comprehension does it fine: > > > > Points = [(x,y) for x in xrange(minx, maxx) for y in xrange(miny, maxy)] > > > > But I can't think at the moment how to do it with numpy. Any ideas? > > > > Thanks, > > > > -Chris > > > > > > -- > > Christopher Barker, Ph.D. > > Oceanographer > > > > NOAA/OR&R/HAZMAT (206) 526-6959 voice > > 7600 Sand Point Way NE (206) 526-6329 fax > > Seattle, WA 98115 (206) 526-6317 main reception > > > > Chr...@no... > > > > > > ------------------------------------------------------- > > All the advantages of Linux Managed Hosting--Without the Cost and Risk! > > Fully trained technicians. The highest number of Red Hat certifications in > > the hosting industry. Fanatical Support. Click to learn more > > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=107521&bid=248729&dat=121642 > > _______________________________________________ > > Numpy-discussion mailing list > > Num...@li... > > https://lists.sourceforge.net/lists/listinfo/numpy-discussion > > > |
From: Christopher B. <Chr...@no...> - 2006-06-01 22:18:52
|
> Charlie Moad wrote: >> pts = mgrid[minx:maxx,miny:maxy].transpose() >> pts.reshape(pts.size/2, 2) Thanks everyone -- yet another reason to dump support for the older num* packages. -Chris -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chr...@no... |
From: Christopher B. <Chr...@no...> - 2006-06-02 22:09:39
|
Rob Hooft wrote: > Christopher Barker wrote: > | Did you time them? And yours only handles integers. > > Yes I did, check the attachment of my previous message for a python > module to time the three, Sorry about that, I don't notice that. > with completely different results from yours > (I'm using Numeric). I ran it and got similar results to mine. Frankly, for the size problems I'm dealing with, they are all about the same, except for under Numarray, where mine is fastest, your second, and Robert third -- by a wide margin! Another reason I'm glad numpy is built on the Numeric code: Using numarray My way took: 0.394555 seconds Robert's way took: 20.590545 seconds Rob's way took: 4.802346 seconds Number of X: 201 Number of Y: 241 Using Numeric My way took: 0.593319 seconds Robert's way took: 0.523235 seconds Rob's way took: 0.579756 seconds Robert's way has a pretty decent edge under numpy: Using numpy My way took: 0.686741 seconds Robert's way took: 0.357887 seconds Rob's way took: 0.796977 seconds And I'm using time(), rather than clock() now, though it dint' really change anything. I suppose I should figure out timeit.py Thanks for all your help on this, -Chris -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chr...@no... |