def sun_inact_kin_sum_stats(data, fast=True, slow=True): def double_exp(t, tauh, taus, Ah, As, A0): return A0 + Ah*np.exp(-t/tauh) + As*np.exp(-t/taus) output_tf = [] output_ts = [] for d in data.split_periodic(11000, adjust=True, closed_intervals=False): d = d.trim_left(10000, adjust=True) current = d['ical.i_CaL'] time = d['engine.time'] index = np.argmax(np.abs(current)) # Set time zero to peak current current = current[index:] time = time[index:] t0 = time[0] time = [t-t0 for t in time] with warnings.catch_warnings(): warnings.simplefilter('error', OptimizeWarning) warnings.simplefilter('error', RuntimeWarning) try: current = [c/current[0] for c in current] if len(time)<=1 or len(current)<=1: raise Exception('Failed simulation') popt, _ = so.curve_fit(double_exp, time, current, p0=[10,200,0.5,0.5,0], bounds=(0., [np.inf, np.inf, 1.0, 1.0, 1.0]), max_nfev=1000) fit = [double_exp(t,popt[0],popt[1],popt[2],popt[3],popt[4]) for t in time] # Calculate r2 ss_res = np.sum((np.array(current)-np.array(fit))**2) ss_tot = np.sum((np.array(current)-np.mean(np.array(current)))**2) r2 = 1 - (ss_res / ss_tot) tauf = min(popt[0],popt[1]) taus = max(popt[0],popt[1]) if r2 > fit_threshold: if fast: output_tf = output_tf+[tauf] if slow: output_ts = output_ts+[taus] else: raise RuntimeWarning('scipy.optimize.curve_fit found a poor fit') except: if fast: output_tf = output_tf+[float('inf')] if slow: output_ts = output_ts+[float('inf')] output = output_tf+output_ts return output
def sun_rel_inact_sum_stats(data): output = [] for d in data.split_periodic(11000, adjust=True, closed_intervals=False): d = d.trim_left(10000, adjust=True) current = d['ical.i_CaL'] peak = max(current, key=abs) ss = current[-1] try: output = output + [1-ss/peak] except: output = output + [float('inf')] return output