
I am attempting to create 2D arrays which are offset copies of a given starting array. For example if I have a 2D array like this: array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) I would like to offset it by some amount in either or both the x and y dimension. Lets say that both the x and y offset would be 1. Then I would like to have an array like this: array([[5, 6, 0], [8, 9, 0], [0, 0, 0]]) Here I don't really care about the values which are now zero. The main point is that now I can compare the data values at any given (x,y) point with the values at the adjacent point (over one on each axis). This would be useful for the kinds of calculations we need to do. I just can't come up with a numeric way to do this. Does anyone have any ideas? Thanks alot, Mike Romberg (romberg@fsl.noaa.gov)

Mike, As was pointed out to me when I had a similar query, one way to do this is to define a class which inherits UserArray and refine indexing and slicing. I actually shifted by an offset of one in the opposite direction to what you seem to require. I had intended to generalize to arbitrary offsets, but haven't had the time yet. Anyway, you're welcome to grab my code at http://www.physics.ubc.ca/~mbelab/python/arrayone as a starting point for your class. There are still some issues and quirkiness with the code, but they're documented along with work-arounds, and suggestions for fixes have been made on this mailing list. Again, it's a matter of time... regards, Eric On Mon, Sep 17, 2001 at 05:20:06PM -0600, Mike Romberg wrote:
-- ******************************** Eric Nodwell Ph.D. candidate Department of Physics University of British Columbia tel: 604-822-5425 fax: 604-822-5324 nodwell@physics.ubc.ca

Mike Romberg wrote:
I am attempting to create 2D arrays which are offset copies of a given starting array. For example if I have a 2D array like this:
have any ideas?
This is not quite as clean as i would like, but this will work:
if b does not have to be the same shape as a, then it is really easy:
b = a[1:,1:]
-Chris -- Christopher Barker, Ph.D. ChrisHBarker@home.net --- --- --- http://members.home.net/barkerlohmann ---@@ -----@@ -----@@ ------@@@ ------@@@ ------@@@ Oil Spill Modeling ------ @ ------ @ ------ @ Water Resources Engineering ------- --------- -------- Coastal and Fluvial Hydrodynamics -------------------------------------- ------------------------------------------------------------------------

This will work: b=zeros ((3,3)) b[:2,:2] = b[:2,:2] + a[1:,1:] You need to know the size of a to use this scheme. ----- Original Message ----- From: "Chris Barker" <chrishbarker@home.net> To: "Mike Romberg" <romberg@fsl.noaa.gov> Cc: <numpy-discussion@lists.sourceforge.net> Sent: Monday, September 17, 2001 2:03 PM Subject: Re: [Numpy-discussion] Offset 2D arrays

How about this: b = 0*a b[:-1, :-1] = a[1:, 1:] Works for any shape and type of a. Konrad. -- ------------------------------------------------------------------------------- Konrad Hinsen | E-Mail: hinsen@cnrs-orleans.fr Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-2.38.25.56.24 Rue Charles Sadron | Fax: +33-2.38.63.15.17 45071 Orleans Cedex 2 | Deutsch/Esperanto/English/ France | Nederlands/Francais -------------------------------------------------------------------------------

Konrad's solution is MUCH more elegant. HLR ----- Original Message ----- From: "Konrad Hinsen" <hinsen@cnrs-orleans.fr> To: <roitblat@hawaii.edu> Cc: <chrishbarker@home.net>; <romberg@fsl.noaa.gov>; <numpy-discussion@lists.sourceforge.net> Sent: Tuesday, September 18, 2001 10:05 PM Subject: Re: [Numpy-discussion] Offset 2D arrays

" " == Herbert L Roitblat <roitblat@hawaii.edu> writes:
> Konrad's solution is MUCH more elegant. > Message ----- From: "Konrad Hinsen" <hinsen@cnrs-orleans.fr> [snip] >> How about this: >> >> b = 0*a b[:-1, :-1] = a[1:, 1:] >> I think it looks cleaner as well. I've managed to create a function (with the help of the tips on this list) which can offset a 2d array in either or both the x and y dimensions. I strongly suspect that someone who *gets* python and numeric slicing better than I, can come up with a cleaner approach. def getindicies(o, l): if o > 0: s1 = o; e1 = l; s2 = 0; e2 = l - o elif o < 0: s1 = 0; e1 = l + o; s2 = -o; e2 = l else: s1 = 0; e1 = l; s2 = 0; e2 = l return s1, e1, s2, e2 # return a 2d array whose dimensions match a with the data offset # controlled by x and y. def offset(a, x, y): sy1, ey1, sy2, ey2 = getindicies(y, a.shape[0]) sx1, ex1, sx2, ex2 = getindicies(x, a.shape[1]) b = zeros(a.shape) b[sy1:ey1,sx1:ex1] = a[sy2:ey2,sx2:ex2] return b a = array(((1, 2, 3), (4, 5, 6), (7, 8, 9))) # no offset print offset(a, 0, 0) # offset by 1 column in x print offset(a, 1, 0) # offset by 1 column (opposite dir) in x print offset(a, -1, 0) # offset by 2 columns in x print offset(a, 2, 0) # offset by 2 columns in y print offset(a, 0, 2) Thanks, Mike Romberg (romberg@fsl.noaa.gov)

Mike, As was pointed out to me when I had a similar query, one way to do this is to define a class which inherits UserArray and refine indexing and slicing. I actually shifted by an offset of one in the opposite direction to what you seem to require. I had intended to generalize to arbitrary offsets, but haven't had the time yet. Anyway, you're welcome to grab my code at http://www.physics.ubc.ca/~mbelab/python/arrayone as a starting point for your class. There are still some issues and quirkiness with the code, but they're documented along with work-arounds, and suggestions for fixes have been made on this mailing list. Again, it's a matter of time... regards, Eric On Mon, Sep 17, 2001 at 05:20:06PM -0600, Mike Romberg wrote:
-- ******************************** Eric Nodwell Ph.D. candidate Department of Physics University of British Columbia tel: 604-822-5425 fax: 604-822-5324 nodwell@physics.ubc.ca

Mike Romberg wrote:
I am attempting to create 2D arrays which are offset copies of a given starting array. For example if I have a 2D array like this:
have any ideas?
This is not quite as clean as i would like, but this will work:
if b does not have to be the same shape as a, then it is really easy:
b = a[1:,1:]
-Chris -- Christopher Barker, Ph.D. ChrisHBarker@home.net --- --- --- http://members.home.net/barkerlohmann ---@@ -----@@ -----@@ ------@@@ ------@@@ ------@@@ Oil Spill Modeling ------ @ ------ @ ------ @ Water Resources Engineering ------- --------- -------- Coastal and Fluvial Hydrodynamics -------------------------------------- ------------------------------------------------------------------------

This will work: b=zeros ((3,3)) b[:2,:2] = b[:2,:2] + a[1:,1:] You need to know the size of a to use this scheme. ----- Original Message ----- From: "Chris Barker" <chrishbarker@home.net> To: "Mike Romberg" <romberg@fsl.noaa.gov> Cc: <numpy-discussion@lists.sourceforge.net> Sent: Monday, September 17, 2001 2:03 PM Subject: Re: [Numpy-discussion] Offset 2D arrays

How about this: b = 0*a b[:-1, :-1] = a[1:, 1:] Works for any shape and type of a. Konrad. -- ------------------------------------------------------------------------------- Konrad Hinsen | E-Mail: hinsen@cnrs-orleans.fr Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-2.38.25.56.24 Rue Charles Sadron | Fax: +33-2.38.63.15.17 45071 Orleans Cedex 2 | Deutsch/Esperanto/English/ France | Nederlands/Francais -------------------------------------------------------------------------------

Konrad's solution is MUCH more elegant. HLR ----- Original Message ----- From: "Konrad Hinsen" <hinsen@cnrs-orleans.fr> To: <roitblat@hawaii.edu> Cc: <chrishbarker@home.net>; <romberg@fsl.noaa.gov>; <numpy-discussion@lists.sourceforge.net> Sent: Tuesday, September 18, 2001 10:05 PM Subject: Re: [Numpy-discussion] Offset 2D arrays

" " == Herbert L Roitblat <roitblat@hawaii.edu> writes:
> Konrad's solution is MUCH more elegant. > Message ----- From: "Konrad Hinsen" <hinsen@cnrs-orleans.fr> [snip] >> How about this: >> >> b = 0*a b[:-1, :-1] = a[1:, 1:] >> I think it looks cleaner as well. I've managed to create a function (with the help of the tips on this list) which can offset a 2d array in either or both the x and y dimensions. I strongly suspect that someone who *gets* python and numeric slicing better than I, can come up with a cleaner approach. def getindicies(o, l): if o > 0: s1 = o; e1 = l; s2 = 0; e2 = l - o elif o < 0: s1 = 0; e1 = l + o; s2 = -o; e2 = l else: s1 = 0; e1 = l; s2 = 0; e2 = l return s1, e1, s2, e2 # return a 2d array whose dimensions match a with the data offset # controlled by x and y. def offset(a, x, y): sy1, ey1, sy2, ey2 = getindicies(y, a.shape[0]) sx1, ex1, sx2, ex2 = getindicies(x, a.shape[1]) b = zeros(a.shape) b[sy1:ey1,sx1:ex1] = a[sy2:ey2,sx2:ex2] return b a = array(((1, 2, 3), (4, 5, 6), (7, 8, 9))) # no offset print offset(a, 0, 0) # offset by 1 column in x print offset(a, 1, 0) # offset by 1 column (opposite dir) in x print offset(a, -1, 0) # offset by 2 columns in x print offset(a, 2, 0) # offset by 2 columns in y print offset(a, 0, 2) Thanks, Mike Romberg (romberg@fsl.noaa.gov)
participants (5)
-
Chris Barker
-
Eric Nodwell
-
Herbert L. Roitblat
-
Konrad Hinsen
-
Mike Romberg