예제 #1
0
def get_weight(dt, isotope='18F', a=1, t=None, r=None, tac=None):

    if isotope == '18F':
        half_life = 20.39 * 60
    elif isotope == '12C':
        half_life = 109.77 * 60
    else:
        print('not known yet')

    decay_const = np.log(2) / half_life
    tdur = kt.dt2tdur(dt)
    dcf = decay_const * tdur / (np.exp(-decay_const * dt[0, ]) -
                                np.exp(-decay_const * dt[1, ]))

    n_model = 5
    w_m = [None] * n_model
    i = 0
    w_m[i] = a * dcf * dcf * t / tdur / tdur
    i += 1
    w_m[i] = a * dcf * dcf * (t + 2 * r) / tdur / tdur
    i += 1
    w_m[i] = a
    i += 1
    w_m[i] = a * dcf * dcf
    i += 1
    w_m[i] = a * tac * tdur * dcf

    for i in range(n_model):
        w_m[i] = 1 / w_m[i]

    return w_m
예제 #2
0
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
예제 #3
0
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
예제 #4
0
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
예제 #5
0
def image_to_suvr_with_reference_tac(pet_image, dt, reference_regional_tac,
                                     selected_frame_index):
    tdur = dt2tdur(dt)
    image = np.multiply(pet_image[:, :, :, selected_frame_index],
                        tdur[selected_frame_index])
    ref = np.multiply(reference_regional_tac[selected_frame_index],
                      tdur[selected_frame_index])
    image_suvr = np.sum(image, axis=-1) / np.sum(ref)
    return image_suvr
예제 #6
0
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
예제 #7
0
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
예제 #8
0
파일: demo_tac.py 프로젝트: AMYPAD/NiftyPAD
tac = np.array([
    0.0612937319754616, 2.56733416202755, 13.2302628562565, 17.5212376697891,
    19.8554469827973, 21.2888518029975, 22.3164453083215, 21.9106339934451,
    21.6976991361882, 21.2760967663021, 21.0320027737135, 20.3356078483447,
    19.7789261620624, 18.7016942272702, 17.5381378584246, 16.6955628809652,
    15.8305514461795, 15.1466577726934, 14.2137260024906, 13.7598553636470,
    12.9989332998008, 11.8293518915139, 10.3648913297944, 8.93110113265639,
    7.50503727314817, 6.49229487908344, 5.83027369667342, 5.35683342894270,
    4.94744092837665, 4.79454967790696, 4.49907835991146
])

tac = TAC(tac, dt)
ref = Ref(ref, dt)
ref.interp_1()
w = dt2tdur(tac.dt)
w = w / np.amax(w)
beta_lim = [0.0100000 / 60, 0.300000 / 60]
n_beta = 40
b = basis.make_basis(ref.inputf1, dt, beta_lim=beta_lim, n_beta=n_beta, w=w)

# provide all user inputs in one dict here and later 'get_model_inputs' will select the needed ones
user_inputs = {
    'dt': dt,
    'inputf1': ref.inputf1,
    'w': w,
    'r1': 0.905,
    'k2p': 0.00025,
    'beta_lim': beta_lim,
    'n_beta': n_beta,
    'b': b,
예제 #9
0
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