11"""
22January 2022 by Paulien Voorter
3- p.voorter@maastrichtuniversity.nl
3+ p.voorter@maastrichtuniversity.nl
44https://www.github.com/paulienvoorter
55
66requirements:
2020def two_exp_noS0 (bvalues , Dpar , Fmv , Dmv ):
2121 """ bi-exponential IVIM function, and S0 set to 1"""
2222 return Fmv * np .exp (- bvalues * Dmv ) + (1 - Fmv ) * np .exp (- bvalues * Dpar )
23-
23+
2424def two_exp (bvalues , S0 , Dpar , Fmv , Dmv ):
2525 """ bi-exponential IVIM function"""
2626 return S0 * (Fmv * np .exp (- bvalues * Dmv ) + (1 - Fmv ) * np .exp (- bvalues * Dpar ))
27-
27+
2828
2929
3030def fit_least_squares_array (bvalues , dw_data , fitS0 = False , bounds = ([0.9 , 0.0001 , 0.0 , 0.0025 ], [1.1 , 0.0025 , 0.5 , 0.2 ]), cutoff = 200 ):
@@ -34,8 +34,8 @@ def fit_least_squares_array(bvalues, dw_data, fitS0=False, bounds=([0.9, 0.0001,
3434 is done on an array.
3535 :param bvalues: 1D Array with the b-values
3636 :param dw_data: 2D Array with diffusion-weighted signal in different voxels at different b-values
37- :param bounds: Array with fit bounds ([S0min, Dparmin, Fmvmin, Dmvmin],[S0max, Dparmax, Fmvmax, Dmvmax]).
38- :param cutoff: cutoff b-value used in step 1
37+ :param bounds: Array with fit bounds ([S0min, Dparmin, Fmvmin, Dmvmin],[S0max, Dparmax, Fmvmax, Dmvmax]).
38+ :param cutoff: cutoff b-value used in step 1
3939 :return Dpar: 1D Array with Dpar in each voxel
4040 :return Fmv: 1D Array with Fmv in each voxel
4141 :return Dmv: 1D Array with Dmv in each voxel
@@ -60,48 +60,48 @@ def fit_least_squares(bvalues, dw_data, S0_output=False, fitS0=False,
6060 is done on an array. It fits a single curve
6161 :param bvalues: 1D Array with the b-values
6262 :param dw_data: 1D Array with diffusion-weighted signal in different voxels at different b-values
63- :param S0_output: Boolean determining whether to output (often a dummy) variable S0;
63+ :param S0_output: Boolean determining whether to output (often a dummy) variable S0;
6464 :param fitS0: Boolean determining whether to fix S0 to 1;
65- :param bounds: Array with fit bounds ([S0min, Dparmin, Fmvmin, Dmvmin],[S0max, Dparmax, Fmvmax, Dmvmax]).
66- :param cutoff: cutoff b-value used in step 1
65+ :param bounds: Array with fit bounds ([S0min, Dparmin, Fmvmin, Dmvmin],[S0max, Dparmax, Fmvmax, Dmvmax]).
66+ :param cutoff: cutoff b-value used in step 1
6767 :return S0: optional 1D Array with S0 in each voxel
6868 :return Dpar: scalar with Dpar of the specific voxel
6969 :return Fmv: scalar with Fmv of the specific voxel
7070 :return Dmv: scalar with Dmv of the specific voxel
7171 """
72-
72+
7373 #try:
7474 def monofit (bvalues , Dpar , Fmv ):
7575 return (1 - Fmv )* np .exp (- bvalues * Dpar )
76-
76+
7777 high_b = bvalues [bvalues >= cutoff ]
7878 high_dw_data = dw_data [bvalues >= cutoff ]
7979 boundsmonoexp = ([bounds [0 ][1 ], bounds [0 ][2 ]], [bounds [1 ][1 ], bounds [1 ][2 ]])
80- params , _ = curve_fit (monofit , high_b , high_dw_data , p0 = [(bounds [1 ][1 ]- bounds [0 ][1 ])/ 2 ,(bounds [1 ][2 ]- bounds [0 ][2 ])/ 2 ], bounds = boundsmonoexp )
80+ params , _ = curve_fit (monofit , high_b , high_dw_data , p0 = [(bounds [1 ][1 ]+ bounds [0 ][1 ])/ 2 ,(bounds [1 ][2 ]+ bounds [0 ][2 ])/ 2 ], bounds = boundsmonoexp )
8181 Dpar1 = params [0 ]
8282 if not fitS0 :
8383 boundsupdated = ([bounds [0 ][2 ] , bounds [0 ][3 ] ],
84- [bounds [1 ][2 ] , bounds [1 ][3 ] ])
84+ [bounds [1 ][2 ] , bounds [1 ][3 ] ])
8585 params , _ = curve_fit (lambda b , Fmv , Dmv : two_exp_noS0 (b , Dpar1 , Fmv , Dmv ), bvalues , dw_data , p0 = [(bounds [0 ][2 ]+ bounds [1 ][2 ])/ 2 , (bounds [0 ][3 ]+ bounds [1 ][3 ])/ 2 ], bounds = boundsupdated )
8686 Fmv , Dmv = params [0 ], params [1 ]
8787 #when the fraction of a compartment equals zero (or very very small), the corresponding diffusivity is non-existing (=NaN)
8888 #if Fmv < 1e-4:
8989 # Dmv = float("NaN")
90-
91- else :
90+
91+ else :
9292 boundsupdated = ([bounds [0 ][0 ] , bounds [0 ][2 ] , bounds [0 ][3 ] ],
93- [bounds [1 ][0 ] , bounds [1 ][2 ] , bounds [1 ][3 ] ])
93+ [bounds [1 ][0 ] , bounds [1 ][2 ] , bounds [1 ][3 ] ])
9494 params , _ = curve_fit (lambda b , S0 , Fmv , Dmv : two_exp (b , S0 , Dpar1 , Fmv , Dmv ), bvalues , dw_data , p0 = [1 , (bounds [0 ][2 ]+ bounds [1 ][2 ])/ 2 , (bounds [0 ][3 ]+ bounds [1 ][3 ])/ 2 ], bounds = boundsupdated )
9595 S0 = params [0 ]
9696 Fmv , Dmv = params [1 ] , params [2 ]
9797 #when the fraction of a compartment equals zero (or very very small), the corresponding diffusivity is non-existing (=NaN)
9898 #if Fmv < 1e-4:
99- # Dmv = float("NaN")
100-
99+ # Dmv = float("NaN")
100+
101101 if S0_output :
102102 return Dpar1 , Fmv , Dmv , S0
103103 else :
104104 return Dpar1 , Fmv , Dmv
105105
106106
107-
107+
0 commit comments