# Custom RT range + running averages

34 views

### 9:46 AM

May 22, 2012, 3:45:28 PM5/22/12
Hello Stimfit community,

I've been really bugged by two questions, and I thought maybe somebody knew how to solve this :/

(a) Is it possible to make Stimfit calculate a custom RT (e.g., 10-50% instead of the default 20-80%)?

(b) How can one create running averages (e.g., not an average of the entire data set, but averages of every six traces)?

### Ilya Flyamer

May 22, 2012, 3:52:52 PM5/22/12
Hi!

I'm quite new to stimfit, but I think both cases need python scripting, thow (b) can be done via this:
http://www.stimfit.org/doc/sphinx/howto/runningmean.html?highlight=running%20average
As I understand, this funcrion does, what you need, thow I don't know, whether it works for more than one trace... Try it. In any case, it should work for one trace at a time.

Ilya.

2012/5/22 9:46 AM

### Jose Guzman

May 23, 2012, 3:32:19 AM5/23/12
On 22/05/12 21:52, Ilya Flyamer wrote:
Hi!
(b) How can one create running averages (e.g., not an average of the entire data set, but averages of every six traces)?

The general strategy  is to define a Python function that operates on a given trace, and enter a list of traces as argument to that function. You can find an example in  http://www.stimfit.org/doc/sphinx/howto/amplitudes.html that calculates amplitudes on selected traces  in Stimfit.

I will give you a very basic example

import stf
import numpy as np

def get_mean(start, end, trace):
"""
computes the average of the trace between
start and end

Arguments:
start    -- starting points (in xunits)
end     -- end point (in xunits)
trace   -- zero-based index of the trace to calculate the mean

Returns:
mean value of the trace between start and end

Example:
>>> get_mean(0, 100, 0) # returns means between 0 and 100 in sweep 0
>>> [get_mean(0, 100, sweep) for sweep in range(0, 100, 6)] # returns a list
of averages of 100 sweeps every 6 trace
"""

# type checking
if type(trace) !=int:
return False

# check that sweep is not out of range
if trace>stf.get_size_channel():
print("Trace out of range")
return False

# transform time into sampling points
dt = stf.get_sampling_interval()
pstart = int(round(start/dt))
pend = int(round(end/dt))

# obtain average
sweep = stf.get_trace(trace, channel=-1)[pstart:pend]
avg = np.mean(sweep)
return(avg)

As you can see in the example, you can enter a list of traces as argument with a list comprehension, or
alternatively with a for loop:

>>> mylist = range(0,100,6)
>>> for sweep in mylist:
>>>    get_mean(0, 100, sweep)

Hope it helps!

Jose