1111# code adapted from MAtlab code by Eric Schrauben: https://github.com/schrau24/XCAT-ERIC
1212# This code generates a 4D IVIM phantom as nifti file
1313
14- def phantom (bvalue , noise , TR = 3000 , TE = 40 , motion = False , rician = False , interleaved = False ):
14+ def phantom (bvalue , noise , TR = 3000 , TE = 40 , motion = False , rician = False , interleaved = False , T1T2 = True ):
1515 np .random .seed (42 )
1616 if motion :
1717 states = range (1 ,21 )
@@ -26,7 +26,7 @@ def phantom(bvalue, noise, TR=3000, TE=40, motion=False, rician=False, interleav
2626 XCAT = XCAT [- 1 :0 :- 2 ,- 1 :0 :- 2 ,10 :160 :4 ]
2727
2828 D , f , Ds = contrast_curve_calc ()
29- S , Dim , fim , Dpim , legend = XCAT_to_MR_DCE (XCAT , TR , TE , bvalue , D , f , Ds )
29+ S , Dim , fim , Dpim , legend = XCAT_to_MR_DCE (XCAT , TR , TE , bvalue , D , f , Ds , T1T2 = T1T2 )
3030 if state == 1 :
3131 Dim_out = Dim
3232 fim_out = fim
@@ -187,7 +187,7 @@ def contrast_curve_calc():
187187 return D , f , Ds
188188
189189
190- def XCAT_to_MR_DCE (XCAT , TR , TE , bvalue , D , f , Ds , b0 = 3 , ivim_cont = True ):
190+ def XCAT_to_MR_DCE (XCAT , TR , TE , bvalue , D , f , Ds , b0 = 3 , ivim_cont = True , T1T2 = True ):
191191 ###########################################################################################
192192 # This script converts XCAT tissue values to MR contrast based on the SSFP signal equation.
193193 # Christopher W. Roy 2018-12-04 # fetal.xcmr@gmail.com
@@ -363,7 +363,6 @@ def XCAT_to_MR_DCE(XCAT, TR, TE, bvalue, D, f, Ds, b0=3, ivim_cont = True):
363363 else :
364364 T1 = Tissue [iTissue , 2 ]
365365 T2 = Tissue [iTissue , 3 ]
366-
367366 if ivim_cont and not np .isnan ([D [iTissue ], f [iTissue ], Ds [iTissue ]]).any ():
368367 # note we are assuming blood fraction has the same T1 as tissue fraction here for simplicity. Can be changed in future.
369368 Dtemp = D [iTissue ]
@@ -374,8 +373,11 @@ def XCAT_to_MR_DCE(XCAT, TR, TE, bvalue, D, f, Ds, b0=3, ivim_cont = True):
374373 ftemp = np .random .rand (1 )* 0.5
375374 Dstemp = 5e-3 + np .random .rand (1 )* 1e-1
376375 S0 = ivim (bvalue ,Dtemp ,ftemp ,Dstemp )
377- if T1 > 0 or T2 > 0 :
378- MR = MR + np .tile (np .expand_dims (XCAT == iTissue ,3 ),len (S0 )) * S0 * (1 - 2 * np .exp (- (TR - TE / 2 ) / T1 ) + np .exp (- TR / T1 )) * np .exp (- TE / T2 )
376+ if T1T2 :
377+ if T1 > 0 or T2 > 0 :
378+ MR = MR + np .tile (np .expand_dims (XCAT == iTissue ,3 ),len (S0 )) * S0 * (1 - 2 * np .exp (- (TR - TE / 2 ) / T1 ) + np .exp (- TR / T1 )) * np .exp (- TE / T2 )
379+ else :
380+ MR = MR + np .tile (np .expand_dims (XCAT == iTissue ,3 ),len (S0 )) * S0
379381 Dim = Dim + (XCAT == iTissue ) * Dtemp
380382 fim = fim + (XCAT == iTissue ) * ftemp
381383 Dpim = Dpim + (XCAT == iTissue ) * Dstemp
@@ -415,6 +417,7 @@ def parse_bvalues_file(file_path):
415417 parser .add_argument ("-n" , "--noise" , type = float , default = 0.0005 , help = "Noise" )
416418 parser .add_argument ("-m" , "--motion" , action = "store_true" , help = "Motion flag" )
417419 parser .add_argument ("-i" , "--interleaved" , action = "store_true" , help = "Interleaved flag" )
420+ parser .add_argument ("-u" , "--T1T2" , action = "store_true" , help = "weight signal with T1T2" ) # note, set this to zero when generating test data for unit testing!
418421 args = parser .parse_args ()
419422
420423 if args .bvalues_file and args .bvalue :
@@ -432,10 +435,11 @@ def parse_bvalues_file(file_path):
432435 noise = args .noise
433436 motion = args .motion
434437 interleaved = args .interleaved
438+ T1T2 = args .T1T2
435439 download_data ()
436440 for key , bvalue in bvalues .items ():
437441 bvalue = np .array (bvalue )
438- sig , XCAT , Dim , fim , Dpim , legend = phantom (bvalue , noise , motion = motion , interleaved = interleaved )
442+ sig , XCAT , Dim , fim , Dpim , legend = phantom (bvalue , noise , motion = motion , interleaved = interleaved , T1T2 = T1T2 )
439443 # sig = np.flip(sig,axis=0)
440444 # sig = np.flip(sig,axis=1)
441445 res = np .eye (4 )
0 commit comments