def recovery_time(v, t=None, p=0.90): """ Time to ``p*100%`` recovery from first local extremum to initial value. Intended for action potential duration and the like. :param array_like t: Time, defaults to [0, 1, 2, ...]. Examples: >>> recovery_time([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 0]) 11.8... >>> recovery_time([10, 0, 20], [100, 101, 102]) 101.45 >>> recovery_time([10, 0, 20], p=0.5) 1.25 """ if t is None: t = np.arange(len(v)) assert 0 <= p <= 1 ex = extrema.extrema(v)[:3] # first three extrema including initial value # We'll interpolate in a monotone slice of (v, t) start, end = ex.index[1], 1 + ex.index[2] vi, ti = v[start:end], t[start:end] vp = p * ex.value[0] + (1 - p) * ex.value[1] # recovery criterion if ex.curv[1] < 0: # recovering from a maximum vi = vi[::-1] # interp requires vi to be increasing, ti = ti[::-1] # so reverse vi and ti return np.interp(vp, vi, ti) # interpolate t as function of v [sic]
def ph2agg(ph, tol=1e-3): """Phenotype aggregation.""" e = extrema(ph["V"], withend=False) peak = e[e.curv < 0] trough = e[e.curv > 0] if len(peak.index) >= 2: t0, t1 = ph["t"].squeeze()[peak.index[-2:]] period = t1 - t0 amplitude = peak.value[-1] - trough.value[-1] if amplitude < tol: period = amplitude = 0.0 else: period = amplitude = 0.0 return np.rec.fromrecords([(period, amplitude)], names=["period", "amplitude"])