def mrtm_k2p(tac, dt, inputf1, k2p, linear_phase_start, linear_phase_end, fig, fig_name): if linear_phase_start is None: linear_phase_start = 0 if linear_phase_end is None: linear_phase_end = np.amax(dt) mft = kt.dt2mft(dt) mft = np.append(0, mft) dt_new = np.array([mft[:-1], mft[1:]]) tdur = kt.dt2tdur(dt_new) input_dt = kt.int2dt(inputf1,dt) tac = np.append(0, tac) input_dt = np.append(0, input_dt) tac_cum = np.cumsum((tac[:-1] + tac[1:]) / 2 * tdur) input_cum = np.cumsum((input_dt[:-1] + input_dt[1:]) / 2 * tdur) tac = tac[1:] input_dt = input_dt[1:] yy = tac xx = np.column_stack((input_cum + 1 / k2p * input_dt, tac_cum)) tt = np.logical_and(mft > linear_phase_start, mft < linear_phase_end) tt = tt[1:] mft = mft[1:] reg = LinearRegression(fit_intercept=False).fit(xx[tt, ], yy[tt]) bp = - reg.coef_[0]/reg.coef_[1] - 1 yyf = reg.predict(xx) if fig: plt.cla() plt.plot(mft, yy, '.') plt.plot(mft, yyf, 'r') if fig_name is not None: plt.savefig(fig_name) plt.show() kps = {'bp': bp} return kps
def mrtm(tac, dt, inputf1, linear_phase_start, linear_phase_end, fig): if linear_phase_start is None: linear_phase_start = 0 if linear_phase_end is None: linear_phase_end = np.amax(dt) mft = kt.dt2mft(dt) mft = np.append(0, mft) dt_new = np.array([mft[:-1], mft[1:]]) tdur = kt.dt2tdur(dt_new) input_dt = kt.int2dt(inputf1, dt) tac = np.append(0, tac) input_dt = np.append(0, input_dt) tac_cum = np.cumsum((tac[:-1] + tac[1:]) / 2 * tdur) input_cum = np.cumsum((input_dt[:-1] + input_dt[1:]) / 2 * tdur) tac = tac[1:] input_dt = input_dt[1:] yy = tac xx = np.column_stack((input_cum, tac_cum, input_dt)) tt = np.logical_and(mft > linear_phase_start, mft < linear_phase_end) tt = tt[1:] mft = mft[1:] reg = LinearRegression(fit_intercept=False).fit(xx[tt, ], yy[tt]) bp = -reg.coef_[0] / reg.coef_[1] - 1 k2p = reg.coef_[0] / reg.coef_[2] # for 1 TC r1 = reg.coef_[2] k2 = -reg.coef_[1] yyf = reg.predict(xx) if fig: plt.plot(mft, yy, '.') plt.plot(mft, yyf, 'r') plt.show() kps = {'bp': bp, 'k2p': k2p, 'r1': r1, 'k2': k2} return kps
def logan_ref_k2p(tac, dt, inputf1, k2p, linear_phase_start, linear_phase_end, fig): if linear_phase_start is None: linear_phase_start = 0 if linear_phase_end is None: linear_phase_end = np.amax(dt) mft = kt.dt2mft(dt) mft = np.append(0, mft) dt_new = np.array([mft[:-1], mft[1:]]) tdur = kt.dt2tdur(dt_new) input_dt = kt.int2dt(inputf1, dt) tac = np.append(0, tac) input_dt = np.append(0, input_dt) tac_cum = np.cumsum((tac[:-1] + tac[1:]) / 2 * tdur) input_cum = np.cumsum((input_dt[:-1] + input_dt[1:]) / 2 * tdur) tac = tac[1:] input_dt = input_dt[1:] yy = np.zeros(tac.shape) xx = np.zeros(tac.shape) mask = tac.nonzero() yy[mask] = tac_cum[mask] / tac[mask] xx[mask] = (input_cum[mask] + input_dt[mask] / k2p) / tac[mask] tt = np.logical_and(mft > linear_phase_start, mft < linear_phase_end) tt = tt[1:] dvr, inter, _, _, _ = linregress(xx[tt], yy[tt]) bp = dvr - 1 yyf = dvr * xx + inter if fig: plt.plot(xx, yy, '.') plt.plot(xx[tt], yyf[tt], 'r') plt.show() kps = {'bp': bp} return kps
def mrtm(tac, dt, inputf1, linear_phase_start, linear_phase_end, fig): if linear_phase_start is None: linear_phase_start = 0 if linear_phase_end is None: linear_phase_end = np.amax(dt) # fill the coffee break gap if kt.dt_has_gaps(dt): tac, dt = kt.tac_dt_fill_coffee_break(tac, dt) mft = kt.dt2mft(dt) mft = np.append(0, mft) dt_new = np.array([mft[:-1], mft[1:]]) tdur = kt.dt2tdur(dt_new) input_dt = kt.int2dt(inputf1, dt) tac = np.append(0, tac) input_dt = np.append(0, input_dt) # set negative values to zero tac[tac < 0] = 0.0 input_dt[input_dt < 0] = 0.0 tac_cum = np.cumsum((tac[:-1] + tac[1:]) / 2 * tdur) input_cum = np.cumsum((input_dt[:-1] + input_dt[1:]) / 2 * tdur) tac = tac[1:] input_dt = input_dt[1:] yy = tac xx = np.column_stack((input_cum, tac_cum, input_dt)) # find tt for the linear phase tt = np.logical_and(mft >= linear_phase_start, mft <= linear_phase_end) tt = tt[1:] # select tt for tac > 0 tt = np.logical_and(tt, tac > 0) # select tt for xx < inf, yy < inf infinf = 1e10 tt = np.logical_and(tt, np.all(xx < infinf, axis=-1)) tt = np.logical_and(tt, yy < infinf) mft = mft[1:] reg = LinearRegression(fit_intercept=False).fit(xx[tt, ], yy[tt]) bp = -reg.coef_[0] / reg.coef_[1] - 1 k2p = reg.coef_[0] / reg.coef_[2] # for 1 TC r1 = reg.coef_[2] k2 = -reg.coef_[1] yyf = reg.predict(xx) if fig: plt.plot(mft, yy, '.') plt.plot(mft, yyf, 'r') plt.show() kps = {'bp': bp, 'k2p': k2p, 'r1': r1, 'k2': k2} return kps
def logan_ref_k2p(tac, dt, inputf1, k2p, linear_phase_start, linear_phase_end, fig): if linear_phase_start is None: linear_phase_start = 0 if linear_phase_end is None: linear_phase_end = np.amax(dt) # fill the coffee break gap if kt.dt_has_gaps(dt): tac, dt = kt.tac_dt_fill_coffee_break(tac, dt) mft = kt.dt2mft(dt) mft = np.append(0, mft) dt_new = np.array([mft[:-1], mft[1:]]) tdur = kt.dt2tdur(dt_new) input_dt = kt.int2dt(inputf1, dt) tac = np.append(0, tac) input_dt = np.append(0, input_dt) # set negative values to zero tac[tac < 0] = 0.0 input_dt[input_dt < 0] = 0.0 tac_cum = np.cumsum((tac[:-1] + tac[1:]) / 2 * tdur) input_cum = np.cumsum((input_dt[:-1] + input_dt[1:]) / 2 * tdur) tac = tac[1:] input_dt = input_dt[1:] yy = tac_cum / (tac + 0.0000000000000001) xx = (input_cum + input_dt / k2p) / (tac + 0.0000000000000001) # find tt for the linear phase tt = np.logical_and(mft >= linear_phase_start, mft <= linear_phase_end) tt = tt[1:] # select tt for tac > 0 tt = np.logical_and(tt, tac > 0) # select tt for xx < inf, yy < inf infinf = 1e10 tt = np.logical_and(tt, xx < infinf) tt = np.logical_and(tt, yy < infinf) # do linear regression with selected tt xx = xx[tt] yy = yy[tt] dvr, inter, _, _, _ = linregress(xx, yy) bp = dvr - 1 yyf = dvr * xx + inter if fig: plt.plot(xx, yy, '.') plt.plot(xx[tt], yyf[tt], 'r') plt.show() kps = {'bp': bp} return kps
def exp_am(tac, dt, idx, fig): tac[tac < 0] = 0.0 mft = kt.dt2mft(dt) p0 = (0.1, 1, 0.1) # p, _ = curve_fit(exp_1_fun_t, mft[idx], tac[idx], p0=p0, bounds=(0.00000001, 2500)) p, _ = curve_fit(exp_1_fun_t, mft[idx], tac[idx], p0=p0) a0, a1, b1 = p t1 = np.arange(np.amax(dt)) tac1f = exp_1_fun_t(t1, a0, a1, b1) if fig: print(p) plt.plot(t1, tac1f, 'b', mft, tac, 'go') plt.show() return tac1f, p
def feng_srtm(tac, dt, w, fig): ts_te_w = (dt[0, ], dt[1, ], w) p0 = [3.90671734e+00, 4.34910151e+02, 9.22189828e+01, 1.35949657e-02, 4.56109635e-02, 4.53841116e-02, 4.54180443e-02, 7.71163349e-04] p, _ = curve_fit(feng_srtm_fun, ts_te_w, tac*w, p0) a0, a1, a2, a3, b0, b1, b2, b3 = p t1 = np.arange(np.amax(dt)) tac1f = feng_srtm_fun_t(t1, a0, a1, a2, a3, b0, b1, b2, b3) if fig: print(p) cp1f = feng_fun_t(t1, a0, a1, a2, b0, b1, b2) mft = kt.dt2mft(dt) plt.plot(t1, cp1f, 'r', t1, tac1f, 'b', mft, tac, 'go') plt.show() return tac1f, p
def exp_2(tac, dt, idx, w, fig): if w is None: w = np.ones_like(tac) ts_te_w = (dt[0, idx], dt[1, idx], w[idx]) p0 = (1, 1, 1, 0, 0) p, _ = curve_fit(exp_2_fun, ts_te_w, tac[idx]*w[idx], p0) a0, a1, a2, b1, b2 = p t1 = np.arange(np.amax(dt)) tac1f = exp_2_fun_t(t1, a0, a1, a2, b1, b2) if fig: print(p) mft = kt.dt2mft(dt) plt.plot(t1, tac1f, 'b', mft, tac, 'go') plt.show() return tac1f, p
def exp_1(tac, dt, idx, w, fig): tac[tac < 0] = 0.0 if w is None: w = np.ones_like(tac) ts_te_w = (dt[0, idx], dt[1, idx], w[idx]) p0 = (1000, 5000, 10) p, _ = curve_fit(exp_1_fun, ts_te_w, tac[idx] * w[idx], p0) a0, a1, b1 = p t1 = np.arange(np.amax(dt)) tac1f = exp_1_fun_t(t1, a0, a1, b1) if fig: print(p) mft = kt.dt2mft(dt) plt.plot(t1, tac1f, 'b', mft, tac, 'go') plt.show() return tac1f, p
def feng_srtm(tac, dt, w, fig): tac[tac < 0] = 0.0 if w is None: w = 1 ts_te_w = (dt[0, ], dt[1, ], w) # p0 = [3.90671734e+00, 4.34910151e+02, 9.22189828e+01, 1.35949657e-02, 4.56109635e-02, 4.53841116e-02, 4.54180443e-02, 7.71163349e-04] p0 = [1, 2, 3, 4, 0.1, 0.2, 0.3, 0.4] # p0 = [6.85579165e-05, -2.08643110e+01, -1.81889002e+02, 7.16906660e+00, 4.21217390e-04, 7.23514957e-02, 7.84986975e-02, 8.27340347e-02] p, _ = curve_fit(feng_srtm_fun, ts_te_w, tac * w, p0=p0) a0, a1, a2, a3, b0, b1, b2, b3 = p t1 = np.arange(np.amax(dt)) tac1f = feng_srtm_fun_t(t1, a0, a1, a2, a3, b0, b1, b2, b3) print(tac1f) if fig: print(p) cp1f = feng_fun_t(t1, a0, a1, a2, b0, b1, b2) mft = kt.dt2mft(dt) plt.plot(t1, cp1f, 'r', t1, tac1f, 'b', mft, tac, 'go') plt.show() return tac1f, p
'inputf1': ref.inputf1cubic, 'w': None, 'r1': 0.905, 'k2p': 0.00025, 'beta_lim': beta_lim, 'n_beta': n_beta, 'b': b } user_inputs_fill_gaps = {'b': b_fill_gaps, 'inputf1_dt': inputf1_dt_fill_gaps } km_outputs = ['R1', 'k2', 'BP'] tac = TAC(ref.tac, dt) for model_name in models: model_inputs = get_model_inputs(user_inputs, model_name) tac.run_model(model_name, model_inputs) # make tac.km_results.update(user_inputs_fill_gaps) km_inputs = tac.km_results model_km_inputs = get_model_inputs(km_inputs, model_name+ '_para2tac') tac.run_model_para2tac(model_name + '_para2tac', model_km_inputs) plt.plot(tac.mft, tac.tac, 'b*', label='tac') plt.plot(dt2mft(dt_fill_gaps), tac.km_results['tacf'], 'r', label='fit') plt.legend() plt.show()
def logan_ref(tac, dt, inputf1, linear_phase_start, linear_phase_end, fig): if linear_phase_start is None: linear_phase_start = 0 if linear_phase_end is None: linear_phase_end = np.amax(dt) # fill the coffee break gap if kt.dt_has_gaps(dt): tac, dt = kt.tac_dt_fill_coffee_break(tac, dt) mft = kt.dt2mft(dt) mft = np.append(0, mft) dt_new = np.array([mft[:-1], mft[1:]]) tdur = kt.dt2tdur(dt_new) input_dt = kt.int2dt(inputf1, dt) tac = np.append(0, tac) input_dt = np.append(0, input_dt) # set negative values to zero tac[tac < 0] = 0.0 input_dt[input_dt < 0] = 0.0 # calculate integration tac_cum = np.cumsum((tac[:-1] + tac[1:]) / 2 * tdur) input_cum = np.cumsum((input_dt[:-1] + input_dt[1:]) / 2 * tdur) # tac_cum and input_cum are calculated in such a way to match tac # # # debug # file_path = '/Users/Himiko/data/amsterdam_data_pib/transfer_162391_files_89f2cba1/' # savetxt(file_path + 'tac_cum.csv', tac_cum) # savetxt(file_path + 'input_cum.csv', input_cum) # # tac1 = kt.interpt1(mft, tac, dt) # input1 = kt.interpt1(mft, input_dt, dt) # tac_cum1 = np.cumsum(tac1) # input_cum1 = np.cumsum(input1) # tac_cum1_mft = tac_cum1[mft.astype(int)] # input_cum1_mft = input_cum1[mft.astype(int)] # savetxt(file_path + 'tac_cum1_mft.csv', tac_cum1_mft) # savetxt(file_path + 'input_cum1_mft.csv', input_cum1_mft) # # # debug tac = tac[1:] input_dt = input_dt[1:] # yy = np.zeros(tac.shape) # xx = np.zeros(tac.shape) # mask = tac.nonzero() # yy[mask] = tac_cum[mask] / tac[mask] # xx[mask] = input_cum[mask] / tac[mask] yy = tac_cum / (tac + 0.0000000000000001) # ADDED BY MY 20210616 xx = input_cum / (tac + 0.0000000000000001) # ADDED BY MY 20210616 # find tt for the linear phase tt = np.logical_and(mft >= linear_phase_start, mft <= linear_phase_end) tt = tt[1:] # select tt for tac > 0 tt = np.logical_and(tt, tac > 0) # select tt for xx < inf, yy < inf infinf = 1e10 tt = np.logical_and(tt, xx < infinf) tt = np.logical_and(tt, yy < infinf) # do linear regression with selected tt xx = xx[tt] yy = yy[tt] dvr, inter, _, _, _ = linregress(xx, yy) bp = dvr - 1 yyf = dvr * xx + inter if fig: plt.plot(xx, yy, '.') plt.plot(xx, yyf, 'r') plt.show() kps = {'bp': bp} return kps