# "Thresholds" in numpy/scipy

Page 1 of 1

## 5 Replies - 1408 Views - Last Post: 16 August 2015 - 08:51 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=379368&amp;s=b05f7ca2348bd020261ef3606070ac58&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 toll_booth

Reputation: 0
• 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

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

Reputation: 797
• Posts: 3,255
• 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.

### #3 toll_booth

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

## Re: "Thresholds" in numpy/scipy

Posted 30 July 2015 - 01:52 PM

ndc85430, 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.

### #4 DK3250

• Pythonian

Reputation: 402
• Posts: 1,297
• 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

### #5 toll_booth

Reputation: 0
• 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.

### #6 umuber

Reputation: 0
• Posts: 16
• Joined: 12-December 13

## Re: "Thresholds" in numpy/scipy

Posted 16 August 2015 - 08:51 PM

toll_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.