2

I have an array of numbers each corresponding to an event and the times when the events occurred. For example,

ev=[0, 14, 23, 53, 3]

time=[0, 0.4, 0.75, 0.9, 1.1]

Imagine ev vs. time to be a (right-continuous) step function which changes values at the values in the time array. Now by resampling, I mean defining a new array of time values and looking up the values of ev function at these times. I want to resample the variable ev under an evenly spaced time array. For example, if t1 is an evenly spaced array, ev1 is the corresponding event list I need.

t1=[0, 0.2, 0.4, 0.6, 0.8, 1, 1.2]

ev1=[0, 0, 14, 14, 23, 53, 3]

Is it possible to do such a resampling of an event array in Python? Is there a direct command? Thanks.

4
  • What method are you using to decide on the ev1 values? A typical approach to this would be to use an interpolation algorithm, like scipy.interp1d(), but with the values you give, I'm not sure that would do what you want. Commented Jan 30, 2014 at 17:42
  • @tom10: To clarify, imagine ev vs. time to be a (right-continuous) step function which changes values at the values in the time array. Now by resampling, I mean defining a new array of time values and looking up the values of ev function at these times. Commented Jan 30, 2014 at 17:44
  • @tom10: Not sure interpd will work. given this is a step function. Commented Jan 30, 2014 at 17:46
  • I think then that interp1d with kind="zero" would work for you. Commented Jan 30, 2014 at 17:53

2 Answers 2

4

You can use np.searchsorted with side='right to find the index of the last item in time that is smaller than your new timings, and then use it to fetch values from the ev array:

>>> np.take(ev, np.searchsorted(time, t1, side='right')-1)
array([ 0,  0, 14, 14, 23, 53,  3])

If you first convert ev to a numpy array, fancy indecing may be more readable:

>>> ev = np.array(ev)
>>> idx = np.searchsorted(time, t1, side='right')-1
>>> ev[idx]
array([ 0,  0, 14, 14, 23, 53,  3])
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. A minor problem with this though. When the dimensions of ev is (5,), it works well. However when they are (5,1) or (1.5), it throws up an error saying "object too deep for desired array". How do I properly reshape the array for using this command?
If you replace time with time.ravel() in the call to searchsorted it should get rid of that error.
2

I'm sure there's a slick sorting way to do this in pure numpy, but here's a pandas way anyhow.

>>> ev = [0, 14, 23, 53, 3]
>>> time = [0, 0.4, 0.75, 0.9, 1.1]
>>> ser = pd.Series(ev, index=time)
>>> ser
0.00     0
0.40    14
0.75    23
0.90    53
1.10     3
dtype: int64
>>> ser.reindex(np.arange(0, 1.4, 0.2), method='ffill')
0.0     0
0.2     0
0.4    14
0.6    14
0.8    23
1.0    53
1.2     3
dtype: int64

2 Comments

Thanks. How do I get the two variables ev1 and t1 from the two columns of this output?
@Bravo use .T method to transpose the 2D array or zip(*that_list).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.