def srtmb_asl(tac, dt, inputf1, beta_lim, n_beta, w, r1): b = basis.make_basis(inputf1, dt, beta_lim=beta_lim, n_beta=n_beta, w=w) n_beta = b['beta'].size ssq = np.zeros(n_beta) if b['w'] is None: b['w'] = 1 y = tac - r1*b['input'] for i in range(0,n_beta): theta = r1 theta = np.append(theta, np.dot(y, b['basis'][i, :])/np.dot(b['basis'][i, :], b['basis'][i, :])) # w won't make a difference here a = np.column_stack((b['input'], b['basis'][i, :])) tacf = np.dot(a, theta) res = (tac - tacf) * b['w'] # w works here ssq[i] = np.sum(res ** 2) i = np.argmin(ssq) theta = r1 theta = np.append(theta, np.dot(y, b['basis'][i, :]) / np.dot(b['basis'][i, :], b['basis'][i, :])) a = np.column_stack((b['input'], b['basis'][i, :])) tacf = np.dot(a, theta) theta = np.append(theta, b['beta'][i]) r1, k2, bp = kp.srtm_theta2kp(theta) kps = {'r1': r1, 'k2': k2, 'bp': bp, 'tacf': tacf} return kps
def srtmb_k2p(tac, dt, inputf1, beta_lim, n_beta, w, k2p): b = basis.make_basis(inputf1, dt, beta_lim=beta_lim, n_beta=n_beta, w=w, k2p=k2p) kps = srtmb_k2p_basis(tac, b) return kps
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, 'linear_phase_start': 500, 'linear_phase_end': None, 'fig': True }
5696, 5996, 6296], [5, 10, 15, 20, 25, 30, 40, 50, 60, 120, 180, 240, 300, 450, 600, 900, 1200, 1800, 2400, 3000, 3600, 5696, 5996, 6296, 6596]]) # ref cer_right_GM = np.array([0, 0, 0, 0, 592.221900000000, 2487.12200000000, 4458.12800000000, 5239.16900000000, 5655.47800000000, 6740.88200000000, 7361.56300000000, 7315.28400000000, 7499.59700000000, 7067.78900000000, 6663.79200000000, 5921.15200000000, 5184.79900000000, 4268.11900000000, 3431.98000000000, 2886.08500000000, 2421.19200000000, 1687.55500000000, 1538.81800000000, 1440.42100000000, 1439.46900000000]) cer_left_GM = np.array([0, 0, 0, 0, 915.895900000000, 3751.55300000000, 5377.27800000000, 5896.48700000000, 6752.62900000000, 7299.80200000000, 7566.03600000000, 7440.07100000000, 7539.30500000000, 7271.21300000000, 6646.04300000000, 6109.30200000000, 5246.95700000000, 4447.90400000000, 3464.82700000000, 2863.01500000000, 2445.84400000000, 1658.95300000000, 1525.59300000000, 1382.73300000000, 1363.93900000000]) cer_GM = cer_right_GM/2 + cer_left_GM/2 ref = Ref(cer_GM, dt) ref.interp_1cubic() beta_lim = [0.0100000/60, 0.300000/60] n_beta = 40 k2p = 0.00025 b = basis.make_basis(ref.inputf1cubic, dt, beta_lim=beta_lim, n_beta=n_beta, w=None, k2p=k2p) dt_fill_gaps = dt_fill_gaps(dt) b_fill_gaps = basis.make_basis(ref.inputf1cubic, dt_fill_gaps, beta_lim=beta_lim, n_beta=n_beta, w=None, k2p=k2p) inputf1_dt_fill_gaps = ref.inputf1cubic, dt_fill_gaps models = ['srtmb_basis', 'srtm', 'srtmb_k2p_basis'] user_inputs = {'dt': dt, 'inputf1': ref.inputf1cubic, 'w': None, 'r1': 0.905, 'k2p': 0.00025, 'beta_lim': beta_lim, 'n_beta': n_beta, 'b': b
def srtmb(tac, dt, inputf1, beta_lim, n_beta, w): tac[tac < 0] = 0.0 b = basis.make_basis(inputf1, dt, beta_lim=beta_lim, n_beta=n_beta, w=w) kps = srtmb_basis(tac, b) return kps
def kinetic_model(src, dst=None, params=None, model='srtmb_basis', w=None, r1=0.905, k2p=0.000250, beta_lim=None, n_beta=40, linear_phase_start=500, linear_phase_end=None, km_outputs=None, thr=0.1, fig=False): """ Args: src (Path or str): input patient directory or filename dst (Path or str): output directory (default: `src` directory) params (Path or str): config (relative to `src` directory) model (str): srtmb_basis, srtmb_k2p_basis, srtmb_asl_basis, logan_ref, logan_ref_k2p, mrtm, mrtm_k2p """ import nibabel as nib import numpy as np from niftypad import basis from niftypad.image_process.parametric_image import image_to_parametric from niftypad.models import get_model_inputs from niftypad.tac import Ref src_path = Path(src) if src_path.is_dir(): fpath = next(src_path.glob('*.nii')) else: fpath = src_path src_path = fpath.parent log.debug("file:%s", fpath) if dst is None: dst_path = src_path else: dst_path = Path(dst) assert dst_path.is_dir() meta = readers.find_meta(src_path, filter(None, [params, fpath.stem])) dt = np.asarray(meta['dt']) ref = np.asarray(meta['ref']) ref = Ref(ref, dt) ref.interp_1cubic() log.debug("looking for first `*.nii` file in %s", src_path) img = nib.load(fpath) # pet_image = img.get_fdata(dtype=np.float32) pet_image = np.asanyarray(img.dataobj) # basis functions if beta_lim is None: beta_lim = [0.01 / 60, 0.3 / 60] b = basis.make_basis(ref.inputf1cubic, dt, beta_lim=beta_lim, n_beta=n_beta, w=w, k2p=k2p) if km_outputs is None: km_outputs = ['R1', 'k2', 'BP'] user_inputs = { 'dt': dt, 'inputf1': ref.inputf1cubic, 'w': w, 'r1': r1, 'k2p': k2p, 'beta_lim': beta_lim, 'n_beta': n_beta, 'b': b, 'linear_phase_start': linear_phase_start, 'linear_phase_end': linear_phase_end, 'fig': fig } model_inputs = get_model_inputs(user_inputs, model) # log.debug("model_inputs:%s", model_inputs) parametric_images_dict, pet_image_fit = image_to_parametric(pet_image, dt, model, model_inputs, km_outputs, thr=thr) for kp in parametric_images_dict: nib.save(nib.Nifti1Image(parametric_images_dict[kp], img.affine), f"{dst_path / fpath.stem}_{model}_{kp}_{fpath.suffix}") nib.save(nib.Nifti1Image(pet_image_fit, img.affine), f"{dst_path / fpath.stem}_{model}_fit_{fpath.suffix}")