5 Replies - 1323 Views - Last Post: 16 August 2015 - 08:51 PM Rate Topic: -----

#1 toll_booth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-June 15

"Thresholds" in numpy/scipy

Posted 27 July 2015 - 03:36 PM

I want to use Numpy/Scipy to help me find the number of times on an 1-D array that cross a given threshold in an ascending direction. It's an easy problem with Python arrays: Given 1-D array X and threshold value c, the script would look like this:

crossings = []
for i in range(1, len(X)):
    if X[i-1] < c and X[i] >= c:
        crossings.append(i)

Note that since all I really need for my script are the number of times this happens, I could just initialize "[I]crossings[/I]" to 0 and increment it by 1 every time the "if" statement returns True.

The problem is that this is a very slow process. What array method can I use in numpy or scipy to do this at a faster rate?

Is This A Good Question/Topic? 0
  • +

Replies To: "Thresholds" in numpy/scipy

#2 ndc85430  Icon User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 704
  • View blog
  • Posts: 2,881
  • Joined: 13-June 14

Re: "Thresholds" in numpy/scipy

Posted 29 July 2015 - 09:43 AM

This is known as root finding. Have a look at the functions in scipy.optimize.
Was This Post Helpful? 0
  • +
  • -

#3 toll_booth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-June 15

Re: "Thresholds" in numpy/scipy

Posted 30 July 2015 - 01:52 PM

View Postndc85430, on 29 July 2015 - 09:43 AM, said:

This is known as root finding. Have a look at the functions in scipy.optimize.


I found this: http://docs.scipy.or...ize.brentq.html

I am familiar with the process of root-finding. However:

1. This function finds *a* root. I need ALL the ascending roots in the interval.
2. I only want roots in the ascending direction; that is, roots between a and b such that f(a)<0 and f(b)>0.
Was This Post Helpful? 0
  • +
  • -

#4 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 351
  • Posts: 1,159
  • Joined: 27-December 13

Re: "Thresholds" in numpy/scipy

Posted 01 August 2015 - 05:19 AM

Maybe this is far-out, but give it a thought.
I have heard and read many times, that 'slow' Python is not a problem because you can always write a small code in C or C++ for such time critical code parts.

Personally, I don't code in C and have only remote knowledge of C++.
But if ever I was to try it, then your four lines of Python code looks like a surmountable challegne.

You would need to code, compile and import as a module to Python - then you are supposed to see really fast execution.

This post has been edited by DK3250: 01 August 2015 - 05:19 AM

Was This Post Helpful? 0
  • +
  • -

#5 toll_booth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-June 15

Re: "Thresholds" in numpy/scipy

Posted 04 August 2015 - 04:54 PM

Here is what I wound up doing:

crossings = np.logical_and(V[:-1]<Omega, Omega<V[1:])
spikeCount = np.sum(crossings)

where Omega is the threshold and V is the array. Not entirely sure how that indexing works, particularly the -1, but hey, it gets the job done.
Was This Post Helpful? 0
  • +
  • -

#6 umuber  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 12-December 13

Re: "Thresholds" in numpy/scipy

Posted 16 August 2015 - 08:51 PM

View Posttoll_booth, on 04 August 2015 - 04:54 PM, said:

Here is what I wound up doing:

crossings = np.logical_and(V[:-1]<Omega, Omega<V[1:])
spikeCount = np.sum(crossings)

where Omega is the threshold and V is the array. Not entirely sure how that indexing works, particularly the -1, but hey, it gets the job done.

Basically, V[:-1] is the array V without the last element, and V[1:] is the array V without the first element. Be aware that your algorithm won't work if V[n] == Omega.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1