Hi everyone I was wondering if there is a more optimal way to write what follows: I am studying waves, so I have an array of wave crests positions, Xcrest and the positions of the ZeroCrossings, Xzeros. The goal is to find between which Xzeros my xcrest are. XXX1=XCrest CrestZerosNeighbour=np.zeros([len(XCrest),2], dtype='d') for nn in range(len(Xzeros)-1): X1=Xzeros[nn] X2=Xzeros[nn+1] indexxx1=np.where((X1<=XXX1) & (XXX1 < X2)) try: CrestZerosNeighbour[indexxx1[0]]=np.array([X1,X2]) except: pass Someone has an idea? in the spirit of (numpy.ma.masked_outside) which does exactly the opposite I want: it masks an array outside an interval. I would like to mask everything except the interval that contains my value. I do this operation a large number of times , and a loop is time consuming. thanks Xavier -- « Quand le gouvernement viole les droits du peuple, l'insurrection est, pour le peuple et pour chaque portion du peuple, le plus sacré des droits et le plus indispensable des devoirs » Déclaration des droits de l'homme et du citoyen, article 35, 1793
Excerpts from Xavier Barthelemy's message of mar. déc. 06 06:53:09 +0100 2011:
Hi everyone
I was wondering if there is a more optimal way to write what follows: I am studying waves, so I have an array of wave crests positions, Xcrest and the positions of the ZeroCrossings, Xzeros.
The goal is to find between which Xzeros my xcrest are.
XXX1=XCrest CrestZerosNeighbour=np.zeros([len(XCrest),2], dtype='d') for nn in range(len(Xzeros)-1): X1=Xzeros[nn] X2=Xzeros[nn+1] indexxx1=np.where((X1<=XXX1) & (XXX1 < X2)) try: CrestZerosNeighbour[indexxx1[0]]=np.array([X1,X2]) except: pass
Someone has an idea? in the spirit of (numpy.ma.masked_outside) which does exactly the opposite I want: it masks an array outside an interval. I would like to mask everything except the interval that contains my value. I do this operation a large number of times , and a loop is time consuming.
Hi, My first idea would be to write a function in C or Fortran that return Xzeros index (instead of values). Algorithms may be optimized according to the inputs: if the XCrest are Xzeros sorted, if len(Xcreast) >> len(Xzeros) using dichotomy... But I would be interested to see a solution with masked array too. --
ok let me be more precise I have an Z array which is the elevation from this I extract a discrete array of Zero Crossing, and another discrete array of Crests. len(crest) is different than len(Xzeros). I have a threshold method to detect my "valid" crests, and sometimes there are 2 crests between two zero-crossing (grouping effect) Crest and Zeros are 2 different arrays, with positions. example: Zeros=[1,2,3,4] Arrays=[1.5,1.7,3.5] and yes arrays can be sorted. not a problm with this. Xavier 2011/12/6 David Froger <david.froger@gmail.com>
Excerpts from Xavier Barthelemy's message of mar. déc. 06 06:53:09 +0100 2011:
Hi everyone
I was wondering if there is a more optimal way to write what follows: I am studying waves, so I have an array of wave crests positions, Xcrest and the positions of the ZeroCrossings, Xzeros.
The goal is to find between which Xzeros my xcrest are.
XXX1=XCrest CrestZerosNeighbour=np.zeros([len(XCrest),2], dtype='d') for nn in range(len(Xzeros)-1): X1=Xzeros[nn] X2=Xzeros[nn+1] indexxx1=np.where((X1<=XXX1) & (XXX1 < X2)) try: CrestZerosNeighbour[indexxx1[0]]=np.array([X1,X2]) except: pass
Someone has an idea? in the spirit of (numpy.ma.masked_outside) which does exactly the opposite I want: it masks an array outside an interval. I would like to mask everything except the interval that contains my value. I do this operation a large number of times , and a loop is time consuming.
Hi,
My first idea would be to write a function in C or Fortran that return Xzeros index (instead of values). Algorithms may be optimized according to the inputs: if the XCrest are Xzeros sorted, if len(Xcreast) >> len(Xzeros) using dichotomy... But I would be interested to see a solution with masked array too.
-- _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- « Quand le gouvernement viole les droits du peuple, l'insurrection est, pour le peuple et pour chaque portion du peuple, le plus sacré des droits et le plus indispensable des devoirs » Déclaration des droits de l'homme et du citoyen, article 35, 1793
Excerpts from Xavier Barthelemy's message of mar. déc. 06 08:51:22 +0100 2011:
ok let me be more precise
I have an Z array which is the elevation from this I extract a discrete array of Zero Crossing, and another discrete array of Crests. len(crest) is different than len(Xzeros). I have a threshold method to detect my "valid" crests, and sometimes there are 2 crests between two zero-crossing (grouping effect)
Crest and Zeros are 2 different arrays, with positions. example: Zeros=[1,2,3,4] Arrays=[1.5,1.7,3.5]
Thanks for the precision. My suggestion was to consider the alternative of rewriting the critical time consuming part of the code (the function that take XCrest and Xzeros as input and return CrestZerosNeighbour) in C or Fortran, this function si then wrapped into Python using Swig, or F2py, or Cython, or Weave. I think this is a typical case where this is usefull. Advantage is that in the C or Fortran function, you can code directly the algorithm you want and have an maximal optimization. Drawback is that you loose the simplicity of pure Python, you need to manage 2 languages and a tool to connect them... --
Yes I understood what you said. I know these tools, and I am using them. I was just wandering if someone has a more one-liner-pythonic way to do it. I don't think it's worth importing a new fortran module. Thanks anyway :) Xavier 2011/12/6 David Froger <david.froger@gmail.com>
Excerpts from Xavier Barthelemy's message of mar. déc. 06 08:51:22 +0100 2011:
ok let me be more precise
I have an Z array which is the elevation from this I extract a discrete array of Zero Crossing, and another discrete array of Crests. len(crest) is different than len(Xzeros). I have a threshold method to detect my "valid" crests, and sometimes there are 2 crests between two zero-crossing (grouping effect)
Crest and Zeros are 2 different arrays, with positions. example: Zeros=[1,2,3,4] Arrays=[1.5,1.7,3.5]
Thanks for the precision. My suggestion was to consider the alternative of rewriting the critical time consuming part of the code (the function that take XCrest and Xzeros as input and return CrestZerosNeighbour) in C or Fortran, this function si then wrapped into Python using Swig, or F2py, or Cython, or Weave. I think this is a typical case where this is usefull. Advantage is that in the C or Fortran function, you can code directly the algorithm you want and have an maximal optimization. Drawback is that you loose the simplicity of pure Python, you need to manage 2 languages and a tool to connect them... -- _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- « Quand le gouvernement viole les droits du peuple, l'insurrection est, pour le peuple et pour chaque portion du peuple, le plus sacré des droits et le plus indispensable des devoirs » Déclaration des droits de l'homme et du citoyen, article 35, 1793
On Tue, Dec 6, 2011 at 2:51 AM, Xavier Barthelemy <xabart@gmail.com> wrote:
ok let me be more precise
I have an Z array which is the elevation from this I extract a discrete array of Zero Crossing, and another discrete array of Crests. len(crest) is different than len(Xzeros). I have a threshold method to detect my "valid" crests, and sometimes there are 2 crests between two zero-crossing (grouping effect)
Crest and Zeros are 2 different arrays, with positions. example: Zeros=[1,2,3,4] Arrays=[1.5,1.7,3.5]
and yes arrays can be sorted. not a problm with this.
Xavier
I may be oversimplifying this, but does searchsorted do what you want?
In [314]: xzeros=[1,2,3,4]; xcrests=[1.5,1.7,3.5] In [315]: np.searchsorted(xzeros, xcrests) Out[315]: array([1, 1, 3]) This returns the indexes of xzeros to the left of xcrests. -Tony
Actually this can be a good idea. i didn't thought using he sorting. i'll try thanks for yours ideas Xavier 2011/12/7 Tony Yu <tsyu80@gmail.com>
On Tue, Dec 6, 2011 at 2:51 AM, Xavier Barthelemy <xabart@gmail.com>wrote:
ok let me be more precise
I have an Z array which is the elevation from this I extract a discrete array of Zero Crossing, and another discrete array of Crests. len(crest) is different than len(Xzeros). I have a threshold method to detect my "valid" crests, and sometimes there are 2 crests between two zero-crossing (grouping effect)
Crest and Zeros are 2 different arrays, with positions. example: Zeros=[1,2,3,4] Arrays=[1.5,1.7,3.5]
and yes arrays can be sorted. not a problm with this.
Xavier
I may be oversimplifying this, but does searchsorted do what you want?
In [314]: xzeros=[1,2,3,4]; xcrests=[1.5,1.7,3.5]
In [315]: np.searchsorted(xzeros, xcrests) Out[315]: array([1, 1, 3])
This returns the indexes of xzeros to the left of xcrests.
-Tony
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- « Quand le gouvernement viole les droits du peuple, l'insurrection est, pour le peuple et pour chaque portion du peuple, le plus sacré des droits et le plus indispensable des devoirs » Déclaration des droits de l'homme et du citoyen, article 35, 1793
participants (3)
-
David Froger -
Tony Yu -
Xavier Barthelemy