Skip to content

Commit cca6577

Browse files
committed
Added initialization and updating of the weights following http://dx.doi.org/10.1016/j.neuroimage.2013.05.028 equation (7)
1 parent fb36fa0 commit cca6577

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/original/TF_reference/segmented_IVIMfit.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ def segmented_IVIM_fit(bvalues, dw_data, b_cutoff = 200, bounds=([0.0001, 0.0, 0
4343
return D, f, Dp
4444

4545

46-
def d_fit_iterative_wls(bvalues_D, log_signal, max_iter=500, tolerance= 1e-6):
46+
def d_fit_iterative_wls(bvalues_D, log_signal, max_iter=50):
4747
"""
4848
Function to calculate D using an iterative wlls on the log(signal)
49+
weights for the wlls are initialized from the predicted signal of a lls as described in
50+
http://dx.doi.org/10.1016/j.neuroimage.2013.05.028 equation (7)
4951
5052
Parameters:
5153
log_signal: the log() of the signal above the threshold for segmented fitting
@@ -54,25 +56,23 @@ def d_fit_iterative_wls(bvalues_D, log_signal, max_iter=500, tolerance= 1e-6):
5456
5557
max_iter: the maximum number of iterations for WLS
5658
57-
tolerance: the tolerance for early stopping of the WLS
5859
"""
5960

60-
bvals = sm.add_constant(bvalues_D)
61-
weights = np.ones_like(log_signal)
61+
bvals = sm.add_constant(-bvalues_D)
62+
# First do a LLS to initialize the weights
63+
beta_lls = np.linalg.lstsq(bvals, log_signal, rcond=None)[0]
64+
# initialize the weights based on the predicted signals W=diag(exp(2*X*Beta_lls))
65+
init_weights = np.exp(2*bvals@beta_lls)
66+
weights = init_weights
6267

6368
for i in range(max_iter):
6469
# Weighted linear regression
6570
model = sm.WLS(log_signal, bvals, weights=weights)
6671
results = model.fit()
67-
signal_pred = results.predict(bvals)
68-
residuals = log_signal - signal_pred
69-
new_weights = 1 / np.maximum(residuals ** 2, 1e-10)
70-
71-
if np.linalg.norm(new_weights - weights) < tolerance:
72-
break
72+
# The weights for the next iteration are based on the predicted signal of this iteration
73+
new_weights = np.exp(2*bvals@results.params)
7374
weights = new_weights
7475

75-
ln_b0_intercept, D_neg = results.params
76+
ln_b0_intercept, D = results.params
7677
b0_intercept = np.exp(ln_b0_intercept)
77-
D = -D_neg
7878
return D, b0_intercept

0 commit comments

Comments
 (0)