def HighFlowSingleInletTwoCompartmentGadoxetateAnd2DSPGRModel( xData2DArray, Ve, Kbh, Khe): try: exceptionHandler.modelFunctionInfoLogger() funcName = 'HighFlowSingleInletTwoCompartmentGadoxetateAnd2DSPGRMode' t = xData2DArray[:, 0] signalAIF = xData2DArray[:, 1] # SPGR model parameters TR = 3.78 / 1000 # Repetition time of dynamic SPGR sequence in seconds dt = 16 #temporal resolution in sec t0 = 5 * dt # Duration of baseline scans FA = 15 #degrees r1 = 5.9 # Hz/mM R10a = 1 / 1.500 # Hz R10t = 1 / 0.800 # Hz # Precontrast signal Sa_baseline = np.mean(signalAIF[0:int(t0 / t[1]) - 1]) # Convert to concentrations R1a = [ Parallel(n_jobs=4)(delayed(fsolve)(spgr2d_func, x0=0, args=(FA, TR, R10a, Sa_baseline, signalAIF[p])) for p in np.arange(0, len(t))) ] R1a = np.squeeze(R1a) concAIF = (R1a - R10a) / r1 c_if = concAIF Th = (1 - Ve) / kbh ce = c_if ct = Ve * ce + Khe * Th * tools.expconv(Th, t, ce, funcName) # Convert to signal St_rel = tools.spgr2d_func_inv(r1, FA, TR, R10t, ct) return ( St_rel ) #Returns tissue signal relative to the baseline St/St_baseline except ZeroDivisionError as zde: exceptionHandler.handleDivByZeroException(zde) except Exception as e: exceptionHandler.handleGeneralException(e)
def DualInletTwoCompartmentGadoxetateAnd2DSPGRModel(xData2DArray, Fa, Ve, Fp, Kbh, Khe): try: exceptionHandler.modelFunctionInfoLogger() funcName = 'DualInletTwoCompartmentGadoxetateAnd2DSPGRModel' t = xData2DArray[:, 0] signalAIF = xData2DArray[:, 1] signalVIF = xData2DArray[:, 2] fv = 1 - Fa # Get SPGR model parameters TR = 3.78 / 1000 # Repetition time of dynamic SPGR sequence in seconds dt = 16 #temporal resolution in sec t0 = 5 * dt # Duration of baseline scans FA = 15 #degrees r1 = 5.9 # Hz/mM R10a = 1 / 1.500 # Hz R10v = 1 / 1.500 # Hz R10t = 1 / 0.800 # Hz # Precontrast signal Sa_baseline = np.mean(signalAIF[0:int(t0 / t[1]) - 1]) Sv_baseline = np.mean(signalVIF[0:int(t0 / t[1]) - 1]) # Convert to concentrations R1a = [ Parallel(n_jobs=4)(delayed(fsolve)(spgr2d_func, x0=0, args=(FA, TR, R10a, Sa_baseline, signalAIF[p])) for p in np.arange(0, len(t))) ] R1v = [ Parallel(n_jobs=4)(delayed(fsolve)(spgr2d_func, x0=0, args=(FA, TR, R10v, Sv_baseline, signalVIF[p])) for p in np.arange(0, len(t))) ] R1a = np.squeeze(R1a) R1v = np.squeeze(R1v) concAIF = (R1a - R10a) / r1 concVIF = (R1v - R10v) / r1 c_if = Fp * (Fa * concAIF + fv * concVIF) Th = (1 - Ve) / Kbh Te = Ve / (Fp + Khe) alpha = np.sqrt(((1 / Te + 1 / Th) / 2)**2 - 1 / (Te * Th)) beta = (1 / Th - 1 / Te) / 2 gamma = (1 / Th + 1 / Te) / 2 # conc = (Ve + Khe(1+Kbh/(vh(1/Tb-1/Th)))exp(-t/Th)-kbhkhe/(vh(1/Tb-1/Th))exp(-t/Tb))*exp(-gamma.t)(cosh(alpha.t)+beta/gamma sinh(alpha.t))*Fp/Ve (Fa concAIF(t)+fv concVIF(t)) # Let ce(t) = exp(-gamma.t)(cosh(alpha.t)+beta/gamma sinh(alpha.t))*c_if(t) then # conc = (Ve + Khe(1+Kbh/(vh(1/Tb-1/Th)))exp(-t/Th)-kbhkhe/(vh(1/Tb-1/Th))exp(-t/Tb))*ce(t) Tc1 = 1 / (gamma - alpha) Tc2 = 1 / (gamma + alpha) ce = (1 / (2 * Ve)) * ( (1 + beta / alpha) * Tc1 * tools.expconv(Tc1, t, c_if, funcName) + (1 - beta / alpha) * Tc2 * tools.expconv(Tc2, t, c_if, funcName)) ct = Ve * ce + Khe * Th * tools.expconv(Th, t, ce, funcName) # Convert to signal St_rel = tools.spgr2d_func_inv(r1, FA, TR, R10t, ct) return ( St_rel ) #Returns tissue signal relative to the baseline St/St_baseline except ZeroDivisionError as zde: exceptionHandler.handleDivByZeroException(zde) except Exception as e: exceptionHandler.handleGeneralException(e)
def HighFlowSingleInletGadoxetate2DSPGR_Rat(xData2DArray, Ve, Kbh, Khe, constantsString): """This function contains the algorithm for calculating how MR signal from a 2D scan varies with time using the High Flow Single Inlet Two Compartment Gadoxetate Model model. Input Parameters ---------------- xData2DArray - time and AIF concentration 1D arrays stacked into one 2D array. Ve - Plasma Volume Fraction (decimal fraction). Khe - Hepatocyte Uptake Rate (mL/min/mL) Kbh - Biliary Efflux Rate (mL/min/mL) constantsString - String representation of a dictionary of constant name:value pairs used to convert concentrations predicted by this model to MR signal values. Returns ------- St_rel - list of calculated MR signals at each of the time points in array 'time'. """ try: exceptionHandler.modelFunctionInfoLogger() t = xData2DArray[:, 0] Sa = xData2DArray[:, 1] # Unpack SPGR model constants from # a string representation of a dictionary # of constants and their values constantsDict = eval(constantsString) TR, baseline, FA, r1, R10a, R10t = \ float(constantsDict['TR']), \ int(constantsDict['baseline']),\ float(constantsDict['FA']), float(constantsDict['r1']), \ float(constantsDict['R10a']), float(constantsDict['R10t']) # Convert to concentrations # n_jobs set to 1 to turn off parallel processing # because parallel processing caused a segmentation # fault in the compiled version of this application. # This is not a problem in the uncompiled script R1a = [ Parallel(n_jobs=1)(delayed(fsolve)(tools.spgr2d_func, x0=0, args=(r1, FA, TR, R10a, baseline, Sa[p])) for p in np.arange(0, len(t))) ] R1a = np.squeeze(R1a) ca = (R1a - R10a) / r1 # Correct for spleen Ve ve_spleen = 0.43 ce = ca / ve_spleen if Kbh != 0: Th = (1 - Ve) / Kbh ct = Ve * ce + Khe * Th * tools.expconv( Th, t, ce, 'HighFlowSingleInletGadoxetate2DSPGR_Rat') else: ct = Ve * ce + Khe * tools.integrate(ce, t) # Convert to signal St_rel = tools.spgr2d_func_inv(r1, FA, TR, R10t, ct) #Return tissue signal relative to the baseline St/St_baseline return (St_rel) except ZeroDivisionError as zde: exceptionHandler.handleDivByZeroException(zde) except Exception as e: exceptionHandler.handleGeneralException(e)
def Model_Function_Template(xData2DArray, param1, param2, param3, param4, param5, constantsString): """This function contains the algorithm for calculating how MR signal varies with time. Input Parameters ---------------- xData2DArray - time and AIF signal (and VIR signal if dual inlet model) 1D arrays stacked into one 2D array. param1 - model parameter. param2 - model parameter. param3 - model parameter. param4 - model parameter. param5 - model parameter. constantsString - String representation of a dictionary of constant name:value pairs used to convert concentrations predicted by this model to MR signal values. Returns ------- St_rel - list of calculated MR signals at each of the time points in array 'time'. """ try: exceptionHandler.modelFunctionInfoLogger #please leave times = xData2DArray[:, 0] signalAIF = xData2DArray[:, 1] #Uncheck the next line of code if the model is dual inlet #and there is a VIF #signalVIF = xData2DArray[:,2] # Unpack SPGR model constants from # a string representation of a dictionary # of constants and their values. # If constants are added/removed from the Model Library XML # file, this section must be updated accordingly constantsDict = eval(constantsString) TR, baseline, FA, r1, R10a, R10t = \ float(constantsDict['TR']), \ int(constantsDict['baseline']),\ float(constantsDict['FA']), float(constantsDict['r1']), \ float(constantsDict['R10a']), float(constantsDict['R10t']) # Convert AIF MR signals to concentrations # n_jobs set to 1 to turn off parallel processing # because parallel processing caused a segmentation # fault in the compiled version of this application. # This is not a problem in the uncompiled script R1a = [ Parallel(n_jobs=1)(delayed(fsolve)(tools.spgr2d_func, x0=0, args=(r1, FA, TR, R10a, baseline, signalAIF[p])) for p in np.arange(0, len(times))) ] R1a = np.squeeze(R1a) ca = (R1a - R10a) / r1 ########################################### # # Add code here to calculate concentration, ct # ############################################## # Convert to signal St_rel = tools.spgr2d_func_inv(r1, FA, TR, R10t, ct) #Return tissue signal relative to the baseline St/St_baseline return (St_rel) #please leave the next 4 lines of code except ZeroDivisionError as zde: exceptionHandler.handleDivByZeroException(zde) except Exception as e: exceptionHandler.handleGeneralException(e)