-1

To be more clear, let me reword my question that I initially asked below.

I have a series of data points that rise and fall in amplitude as someone takes a step and then another step. Zero is when the foot is off the ground.

A simple example would look like this:

 data_array = (0,0,0,10,20,50,40,30,10,0,0,0,0,0,10,20,50,40,30,10,0,0)

I determine when each step starts and stop by recording the starts (all the indexes that start) in one array and the stops in another array.

starts = (4 15)

stops = (9 20)

THE QUESTION: Now, I want to slice the actual data for each step out of the initial array and them in columns.

[Note: That, if necessary, we know the number of steps taken by the amount of data in the starts or stops arrays.]

10 10

20 20

50 50

40 40

30 30

10 10

I can not figure out how to use those starts and stops indices to slice the initial array. OR, I did not find a filter function to slice out the steps.

BTW (2nd Edit) Here is some of the code I am using if it helps:

sigma = 5

threshold = 30

above_threshold = gaussian_filter(Fz, sigma=sigma) > threshold

#---INDEX ALL STATE CHANGES---

ind = np.where(np.diff(above_threshold))[0] + 1

print ind

ORIGINAL QUESTION

In this type of array:

data_array = (0,0,0,10,20,50,40,30,10,0,0)

I determine when values are above 20 and below 20. I return those indexes as starts (going above 20) and stops (going below 20) by making the following:

startstop = np.vstack((ind[::2], ind[1::2])).T   
starts1=np.vstack((ind[::2])).T
stops1=np.vstack((ind[1::2])).T

Can someone point me in the right direction using numpy (or not) so that I can extract all those values in data_array using one of these arrays (startstop, starts1, stops1) to get this:

new_array = (50,40,30)

Thanks, Scott

Scott
  • 79
  • 3
  • 8

4 Answers4

1

I don't understand the way you are trying to go about it, but can you use filter?:

>>> data_array = (0,0,0,10,20,50,40,30,10,0)
>>> filter(lambda x: x>20,data_array)

(50, 40, 30)

Works with numpy arrays too.

Josiah
  • 3,266
  • 24
  • 24
  • Filter is appealing now. I am currently using sigma = 5 threshold = 30 above_threshold = gaussian_filter(Fz, sigma=sigma) > threshold #arrray with True/Falso pending values above/below filter #---INDEX ALL STATE CHANGES--- ind = np.where(np.diff(above_threshold))[0] + 1 print ind – Scott May 30 '12 at 03:36
  • Sorry, new to stackoverflow, not sure how to format my reply better. hope it makes sense. – Scott May 30 '12 at 03:37
1

take() will do what you wants, i think.

data_array = np.array([0,0,0,10,20,50,40,30,10,0])


b = data_array.take([1,2,3])
print b

output:

[ 0  0 10]

read more: take() more on take

fhtuft
  • 966
  • 5
  • 8
  • Thanks, take may come in handy later. But, I need to automate this one, not enter 1,2,3. – Scott May 30 '12 at 03:35
  • You can replace the array [1,2,3]. with an array you made that has the indexes of the elements you want. as this: b = data_array.take(starts1) . Thought that was your question? – fhtuft May 30 '12 at 10:15
  • starts1 only contains the starting index (one element), i have the end of the range in stops1. I could make a new array with all the indexes between each start and stop. Then pass them in. Seems expensive though? – Scott May 30 '12 at 14:37
1

Your question is not perfectly clear, but if you are asking how to slice a Python list or iterable, use the built-in slice function:

>>> data_array = (0,0,0,10,20,50,40,30,10,0)
>>> data_array[slice(5,8)]
(50, 40, 30)
>>> new_list=list(data_array[slice(5,8)])
>>> new_list
[50, 40, 30]
dawg
  • 98,345
  • 23
  • 131
  • 206
  • I reworded my question above. You are right, not very clear. Could I use your suggestion without putting in the 5, 8. I tried that previously, but could not pass in variables to automate the code and to take out multiple slices. – Scott May 30 '12 at 03:34
  • @Scott: well, yes. You can do `data_array[slice(start,stop)]` and the slice built-in is less picky than the slice syntax of `l[8:10]` as an example. – dawg May 30 '12 at 05:17
  • Makes sense, thanks. Seems my major issue was that I was really trying to keep everything in a 2D numpy array that initially had the data. I flattened it into a list and works. But, I'll want to rebuild the numpy arrays for math done on each segment. – Scott May 30 '12 at 06:28
1

Python lists are defined with square brackets, and we want to generate a list of lists (where each piece contains one of your defined segments). Since computers start counting at 0, your "4th element is the start" translates to array index = 3.

One quirk is that to query the 4th to 9th element we'd use data_array[3:9]: this slice notation gives you every element starting at the first specified, up to (but not including) the last. The rest is a list comprehension that goes over any arbitrary number of step segments.

starts = [4, 15]
stops = [9, 20]
data_array = [0,0,0,10,20,50,40,30,10,0,0,0,0,0,10,20,50,40,30,10,0,0]
segments = [ data_array[starts[i] - 1: stops[i]  ]  for i in range( len(starts) ) ]

returns

>>> segments
[[10, 20, 50, 40, 30, 10], [10, 20, 50, 40, 30, 10]]

Each set of steps (segment) can be accessed individually:

>>>segments[0]
[10, 20, 50, 40, 30, 10]

EDIT: Alternately... if you need to use numpy array, then try: segments = array([ data_array[starts[i] - 1: stops[i] ] for i in range( len(starts) ) ])

I tried working in additional modules to get the array directly (using itertools.islice with itertools.chain, numpy.fromiter, etc). But even if the fancier solution worked, I'm not sure it'd offer significant speed advantages over converting to an array, and it would be a lot less concise. See: How do I build a numpy array from a generator?

Community
  • 1
  • 1
abought
  • 2,652
  • 1
  • 18
  • 13
  • This works if I flatter my numpy array into a 1D list. But, then I'll have to rebuild new numpy arrays to take advantage of mathematical functions to run on each segment. Any suggestions? – Scott May 30 '12 at 06:25
  • Depending on the size of your data, you might be able to get away with converting it to an array at the end: segments = array [ list comprehension ] ). Alternately, you could get a bit fancy and combine additional features/modules. I'll edit my post to include an alternate solution using numpy. – abought May 30 '12 at 14:30