Skip to content

Commit b712d4b

Browse files
committed
Updated initialization
1 parent 17f2143 commit b712d4b

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

src/standardized/OGC_AmsterdamUMC_Bayesian_biexp.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class OGC_AmsterdamUMC_Bayesian_biexp(OsipiBase):
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
accepts_priors = True
3030

31-
def __init__(self, bvalues=None, bounds=([0, 0, 0.005, 0.7],[0.005, 0.7, 0.2, 1.3]), initial_guess=None, fitS0=True, thresholds=None, prior_in=None):
31+
def __init__(self, bvalues=None, thresholds=None, bounds=([0, 0, 0.005, 0.7],[0.005, 0.7, 0.2, 1.3]), initial_guess=None, fitS0=True, prior_in=None):
3232
"""
3333
Everything this algorithm requires should be implemented here.
3434
Number of segmentation thresholds, bounds, etc.
@@ -39,11 +39,11 @@ def __init__(self, bvalues=None, bounds=([0, 0, 0.005, 0.7],[0.005, 0.7, 0.2, 1.
3939
Args:
4040
datain is a 2D array with values of D, f, D* (and S0) that will form the prior.
4141
"""
42-
super(OGC_AmsterdamUMC_Bayesian_biexp, self).__init__(bvalues, thresholds, bounds, initial_guess) #, fitS0, prior_in)
42+
super(OGC_AmsterdamUMC_Bayesian_biexp, self).__init__(bvalues, bounds, initial_guess) #, fitS0, prior_in)
4343
self.OGC_algorithm = fit_bayesian
44-
self.initialize(bounds, initial_guess, fitS0, thresholds, prior_in)
44+
self.initialize(bounds, initial_guess, fitS0, prior_in)
4545

46-
def initialize(self, bounds=([0, 0, 0.005, 0.7],[0.005, 0.7, 0.2, 1.3]), initial_guess=None, fitS0=True, thresholds=None, prior_in=None):
46+
def initialize(self, bounds=([0, 0, 0.005, 0.7],[0.005, 0.7, 0.2, 1.3]), initial_guess=None, fitS0=True, prior_in=None):
4747
if bounds is None:
4848
self.bounds=([0, 0, 0.005, 0.7],[0.005, 1, 0.2, 1.3])
4949
else:
@@ -61,7 +61,7 @@ def initialize(self, bounds=([0, 0, 0.005, 0.7],[0.005, 0.7, 0.2, 1.3]), initial
6161
self.initial_guess = initial_guess
6262
self.fitS0=fitS0
6363

64-
def ivim_fit(self, signals, bvalues, bounds=None, initial_guess=None, fitS0=True, thresholds=None, prior_in=None, **kwargs):
64+
def ivim_fit(self, signals, bvalues, initial_guess=None, **kwargs):
6565
"""Perform the IVIM fit
6666
6767
Args:
@@ -71,9 +71,10 @@ def ivim_fit(self, signals, bvalues, bounds=None, initial_guess=None, fitS0=True
7171
Returns:
7272
_type_: _description_
7373
"""
74-
self.initialize(bounds, initial_guess, fitS0, thresholds, prior_in)
74+
if initial_guess is not None and len(initial_guess) == 4:
75+
self.initial_guess = initial_guess
7576
bvalues=np.array(bvalues)
76-
fit_results = fit_segmented(bvalues, signals,bounds=self.bounds,cutoff=150,p0=self.initial_guess)
77+
fit_results = fit_segmented(bvalues, signals, bounds=self.bounds, cutoff=150, p0=self.initial_guess)
7778
fit_results=fit_results+(1,)
7879
fit_results = self.OGC_algorithm(bvalues, signals, self.neg_log_prior, x0=fit_results, fitS0=self.fitS0)
7980

src/standardized/OGC_AmsterdamUMC_biexp.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,16 @@ class OGC_AmsterdamUMC_biexp(OsipiBase):
2626
required_initial_guess = False
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?
29-
accepts_priors = True
3029

31-
def __init__(self, bvalues=None, bounds=([0, 0, 0.005, 0.7],[0.005, 0.7, 0.2, 1.3]), initial_guess=None, fitS0=False, thresholds=None):
30+
def __init__(self, bvalues=None, thresholds=None, bounds=([0, 0, 0.005, 0.7],[0.005, 0.7, 0.2, 1.3]), initial_guess=None, fitS0=False):
3231
"""
3332
Everything this algorithm requires should be implemented here.
3433
Number of segmentation thresholds, bounds, etc.
3534
3635
Our OsipiBase object could contain functions that compare the inputs with
3736
the requirements.
3837
"""
39-
super(OGC_AmsterdamUMC_biexp, self).__init__(bvalues, bounds,initial_guess,fitS0)
38+
super(OGC_AmsterdamUMC_biexp, self).__init__(bvalues, bounds, initial_guess, fitS0)
4039
self.OGC_algorithm = fit_least_squares
4140
self.initialize(bounds, initial_guess, fitS0)
4241

@@ -51,7 +50,7 @@ def initialize(self, bounds, initial_guess, fitS0):
5150
self.initial_guess = initial_guess
5251
self.fitS0=fitS0
5352

54-
def ivim_fit(self, signals, bvalues, bounds=None, initial_guess=None, fitS0=False, **kwargs):
53+
def ivim_fit(self, signals, bvalues, initial_guess=None, **kwargs):
5554
"""Perform the IVIM fit
5655
5756
Args:
@@ -61,7 +60,8 @@ def ivim_fit(self, signals, bvalues, bounds=None, initial_guess=None, fitS0=Fals
6160
Returns:
6261
_type_: _description_
6362
"""
64-
self.initialize(bounds, initial_guess, fitS0)
63+
if initial_guess is not None and len(initial_guess) == 4:
64+
self.initial_guess = initial_guess
6565
bvalues=np.array(bvalues)
6666
fit_results = self.OGC_algorithm(bvalues, signals, p0=self.initial_guess, bounds=self.bounds, fitS0=self.fitS0)
6767

src/standardized/OGC_AmsterdamUMC_biexp_segmented.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class OGC_AmsterdamUMC_biexp_segmented(OsipiBase):
2626
required_initial_guess = False
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?
29-
accepts_priors = True
3029

3130
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]):
3231
"""
@@ -55,7 +54,7 @@ def initialize(self, bounds, initial_guess, thresholds=300):
5554
else:
5655
self.thresholds = thresholds
5756

58-
def ivim_fit(self, signals, bvalues, bounds=None, initial_guess=None, thresholds=None, **kwargs):
57+
def ivim_fit(self, signals, bvalues, initial_guess=None, **kwargs):
5958
"""Perform the IVIM fit
6059
6160
Args:
@@ -65,6 +64,8 @@ def ivim_fit(self, signals, bvalues, bounds=None, initial_guess=None, thresholds
6564
Returns:
6665
_type_: _description_
6766
"""
67+
if initial_guess is not None and len(initial_guess) == 4:
68+
self.initial_guess = initial_guess
6869
bvalues=np.array(bvalues)
6970
fit_results = self.OGC_algorithm(bvalues, signals, bounds=self.bounds, cutoff=self.thresholds, p0=self.initial_guess)
7071

tests/IVIMmodels/unit_tests/test_ivim_synthetic.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@ def test_generated(ivim_algorithm, ivim_data, SNR, rtol, atol, fit_count, rician
2525
Dp = data["Dp"]
2626
fit = OsipiBase(algorithm=ivim_algorithm)
2727
# here try a prior, but it's not seeing it, why?
28-
# if hasattr(fit, "accepts_priors") and fit.accepts_priors:
29-
prior = [np.repeat(D, 10)+np.random.normal(0,D/3,np.shape(np.repeat(D, 10))), np.repeat(f, 10)+np.random.normal(0,f/3,np.shape(np.repeat(D, 10))), np.repeat(Dp, 10)+np.random.normal(0,Dp/3,np.shape(np.repeat(D, 10))),np.repeat(np.ones_like(Dp), 10)+np.random.normal(0,1/3,np.shape(np.repeat(D, 10)))] # D, f, D*
30-
# fit.initialize(prior_in=prior)
28+
if hasattr(fit, "accepts_priors") and fit.accepts_priors:
29+
prior = [np.random.normal(D, D/3, 10), np.random.normal(f, f/3, 10), np.random.normal(Dp, Dp/3, 10), np.random.normal(1, 1/3, 10)]
30+
# prior = [np.repeat(D, 10)+np.random.normal(0,D/3,np.shape(np.repeat(D, 10))), np.repeat(f, 10)+np.random.normal(0,f/3,np.shape(np.repeat(D, 10))), np.repeat(Dp, 10)+np.random.normal(0,Dp/3,np.shape(np.repeat(D, 10))),np.repeat(np.ones_like(Dp), 10)+np.random.normal(0,1/3,np.shape(np.repeat(D, 10)))] # D, f, D*
31+
fit.initialize(prior_in=prior)
3132
time_delta = datetime.timedelta()
3233
for idx in range(fit_count):
3334
# if "data" not in data:
3435
signal = gd.ivim_signal(D, Dp, f, S0, bvals, SNR, rician_noise)
3536
# else:
3637
# signal = data["data"]
3738
start_time = datetime.datetime.now()
38-
[f_fit, Dp_fit, D_fit] = fit.osipi_fit(signal, bvals, prior_in=prior)
39+
[f_fit, Dp_fit, D_fit] = fit.osipi_fit(signal, bvals) #, prior_in=prior
3940
time_delta += datetime.datetime.now() - start_time
4041
if save_file:
4142
save_results(save_file, ivim_algorithm, name, SNR, idx, [f, Dp, D], [f_fit, Dp_fit, D_fit])

0 commit comments

Comments
 (0)