Skip to content

Commit 9d211a0

Browse files
Fixed bugs
1 parent b872d4d commit 9d211a0

File tree

5 files changed

+50
-98
lines changed

5 files changed

+50
-98
lines changed

src/original/OGC_AmsterdamUMC/LSQ_fitting.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,20 @@ def fit_segmented_tri_exp(bvalues, dw_data, bounds=([0, 0, 0, 0.005, 0, 0.06], [
466466
return 0., 0., 0., 0., 0., 0.
467467

468468

469+
def neg_log_likelihood(p, bvalues, dw_data):
470+
"""
471+
This function determines the negative of the log of the likelihood of parameters p, given the data dw_data for the Bayesian fit
472+
:param p: 1D Array with the estimates of D, f, D* and (optionally) S0
473+
:param bvalues: 1D array with b-values
474+
:param dw_data: 1D Array diffusion-weighted data
475+
:returns: the log-likelihood of the parameters given the data
476+
"""
477+
if len(p) == 4:
478+
return 0.5 * (len(bvalues) + 1) * np.log(
479+
np.sum((ivim(bvalues, p[0], p[1], p[2], p[3]) - dw_data) ** 2)) # 0.5*sum simplified
480+
else:
481+
return 0.5 * (len(bvalues) + 1) * np.log(
482+
np.sum((ivim(bvalues, p[0], p[1], p[2], 1) - dw_data) ** 2)) # 0.5*sum simplified
469483
def empirical_neg_log_prior(Dt0, Fp0, Dp0, S00=None):
470484
"""
471485
This function determines the negative of the log of the empirical prior probability of the IVIM parameters
@@ -519,6 +533,18 @@ def neg_log_prior(p):
519533

520534
return neg_log_prior
521535

536+
def neg_log_posterior(p, bvalues, dw_data, neg_log_prior):
537+
"""
538+
This function determines the negative of the log of the likelihood of parameters p, given the prior likelihood and the data
539+
:param p: 1D Array with the estimates of D, f, D* and (optionally) S0
540+
:param bvalues: 1D array with b-values
541+
:param dw_data: 1D Array diffusion-weighted data
542+
:param neg_log_prior: prior likelihood function (created with empirical_neg_log_prior)
543+
:returns: the posterior probability given the data and the prior
544+
"""
545+
return neg_log_likelihood(p, bvalues, dw_data) + neg_log_prior(p)
546+
547+
522548
def flat_neg_log_prior(Dt_range, Fp_range, Dp_range, S0_range=None):
523549
"""
524550
This function determines the negative of the log of the empirical prior probability of the IVIM parameters
@@ -537,45 +563,19 @@ def neg_log_prior(p):
537563
if (Dp < Dt):
538564
return 1e8
539565
else:
540-
eps = 1e-8
541-
Dp_prior = stats.loguniform.pdf(Dp, Dp_range[0], scale=Dp_range[1]-Dp_range[0])
542-
Dt_prior = stats.loguniform.pdf(Dt, Dt_range[0], scale=Dt_range[1]-Dt_range[0])
543-
Fp_prior = stats.loguniform.pdf(Fp, Fp_range[0], scale=Fp_range[1]-Fp_range[0])
544566
# determine and return the prior for D, f and D* (and S0)
545567
if len(p) == 4:
546-
S0_prior = stats.loguniform.pdf(S0, S0_range[0], scale=S0_range[1] - S0_range[0])
547-
return -np.log(Dp_prior + eps) - np.log(Dt_prior + eps) - np.log(Fp_prior + eps) - np.log(
548-
S0_prior + eps)
568+
if Dt_range[0] < Dt < Dt_range[1] and Fp_range[0] < Fp < Fp_range[1] and Dp_range[0] < Dp < Dp_range[1]: # and S0_range[0] < S0 < S0_range[1]: << not sure whether this helps. Technically it should be here
569+
return 0
570+
else:
571+
return 1e8
549572
else:
550-
return -np.log(Dp_prior + eps) - np.log(Dt_prior + eps) - np.log(Fp_prior + eps)
573+
if Dt_range[0] < Dt < Dt_range[1] and Fp_range[0] < Fp < Fp_range[1] and Dp_range[0] < Dp < Dp_range[1]:
574+
return 0
575+
else:
576+
return 1e8
551577

552578
return neg_log_prior
553-
def neg_log_likelihood(p, bvalues, dw_data):
554-
"""
555-
This function determines the negative of the log of the likelihood of parameters p, given the data dw_data for the Bayesian fit
556-
:param p: 1D Array with the estimates of D, f, D* and (optionally) S0
557-
:param bvalues: 1D array with b-values
558-
:param dw_data: 1D Array diffusion-weighted data
559-
:returns: the log-likelihood of the parameters given the data
560-
"""
561-
if len(p) == 4:
562-
return 0.5 * (len(bvalues) + 1) * np.log(
563-
np.sum((ivim(bvalues, p[0], p[1], p[2], p[3]) - dw_data) ** 2)) # 0.5*sum simplified
564-
else:
565-
return 0.5 * (len(bvalues) + 1) * np.log(
566-
np.sum((ivim(bvalues, p[0], p[1], p[2], 1) - dw_data) ** 2)) # 0.5*sum simplified
567-
568-
569-
def neg_log_posterior(p, bvalues, dw_data, neg_log_prior):
570-
"""
571-
This function determines the negative of the log of the likelihood of parameters p, given the prior likelihood and the data
572-
:param p: 1D Array with the estimates of D, f, D* and (optionally) S0
573-
:param bvalues: 1D array with b-values
574-
:param dw_data: 1D Array diffusion-weighted data
575-
:param neg_log_prior: prior likelihood function (created with empirical_neg_log_prior)
576-
:returns: the posterior probability given the data and the prior
577-
"""
578-
return neg_log_likelihood(p, bvalues, dw_data) + neg_log_prior(p)
579579

580580

581581
def fit_bayesian_array(bvalues, dw_data, paramslsq, arg):
@@ -655,7 +655,7 @@ def fit_bayesian(bvalues, dw_data, neg_log_prior, x0=[0.001, 0.2, 0.05], fitS0=T
655655
'''
656656
try:
657657
# define fit bounds
658-
bounds = [(0, 0.005), (0, 0.7), (0.005, 0.2), (0, 2.5)]
658+
bounds = [(0, 0.005), (0, 1), (0, 2), (0, 2.5)]
659659
# Find the Maximum a posterior probability (MAP) by minimising the negative log of the posterior
660660
if fitS0:
661661
params = minimize(neg_log_posterior, x0=x0, args=(bvalues, dw_data, neg_log_prior), bounds=bounds)

src/standardized/OGC_AmsterdamUMC_Bayesian_biexp.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ def __init__(self, bvalues=None, bounds=([0, 0, 0.005, 0.7],[0.005, 0.7, 0.2, 1.
3737
"""
3838
super(OGC_AmsterdamUMC_Bayesian_biexp, self).__init__(bvalues, bounds,initial_guess,fitS0)
3939
if bounds is None:
40-
self.bounds=([0, 0, 0.005, 0.7],[0.005, 0.7, 0.2, 1.3])
40+
self.bounds=([0, 0, 0.005, 0.7],[0.005, 1.0, 0.2, 1.3])
4141
else:
4242
self.bounds=bounds
43-
self.neg_log_prior=flat_neg_log_prior([self.bounds[0][0],self.bounds[1][0]],[self.bounds[0][1],self.bounds[1][1]],[self.bounds[0][1],self.bounds[1][1]],[self.bounds[0][2],self.bounds[1][2]])
43+
self.neg_log_prior=flat_neg_log_prior([self.bounds[0][0],self.bounds[1][0]],[self.bounds[0][1],self.bounds[1][1]],[self.bounds[0][2],self.bounds[1][2]],[self.bounds[0][3],self.bounds[1][3]])
4444
self.OGC_algorithm = fit_bayesian
45-
self.bounds=bounds
46-
self.initial_guess=initial_guess
45+
if initial_guess is None:
46+
self.initial_guess = [0.001, 0.001, 0.01, 1]
47+
else:
48+
self.initial_guess = initial_guess
4749
self.fitS0=fitS0
4850

4951
def ivim_fit(self, signals, bvalues=None):

src/standardized/OGC_AmsterdamUMC_biexp.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,14 @@ def __init__(self, bvalues=None, bounds=([0, 0, 0.005, 0.7],[0.005, 0.7, 0.2, 1.
3737
"""
3838
super(OGC_AmsterdamUMC_biexp, self).__init__(bvalues, bounds,initial_guess,fitS0)
3939
self.OGC_algorithm = fit_least_squares
40-
self.bounds=bounds
41-
self.initial_guess=initial_guess
40+
if bounds is None:
41+
self.bounds=([0, 0, 0.005, 0.7],[0.005, 1.0, 0.2, 1.3])
42+
else:
43+
self.bounds=bounds
44+
if initial_guess is None:
45+
self.initial_guess = [0.001, 0.001, 0.01, 1]
46+
else:
47+
self.initial_guess = initial_guess
4248
self.fitS0=fitS0
4349

4450
def ivim_fit(self, signals, bvalues=None):

src/standardized/OGC_AmsterdamUMC_biexp_segmented.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class OGC_AmsterdamUMC_biexp_segmented(OsipiBase):
2727
required_initial_guess_optional = True
2828
accepted_dimensions = 1 # Not sure how to define this for the number of accepted dimensions. Perhaps like the thresholds, at least and at most?
2929

30-
def __init__(self, bvalues=None, thresholds=75, bounds=([0, 0, 0.005],[0.005, 0.7, 0.2]), initial_guess=[0.001, 0.01, 0.01,1]):
30+
def __init__(self, bvalues=None, thresholds=150, bounds=([0, 0, 0.005],[0.005, 0.7, 0.2]), initial_guess=[0.001, 0.01, 0.01,1]):
3131
"""
3232
Everything this algorithm requires should be implemented here.
3333
Number of segmentation thresholds, bounds, etc.

src/standardized/PV_MUMC_biexp.py

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)