@@ -57,28 +57,13 @@ def __init__(self, bvalues=None, thresholds=None, bounds=None, initial_guess=Non
5757 self .fit_segmented = fit_segmented
5858
5959 def initialize (self , bounds = None , initial_guess = None , fitS0 = True , prior_in = None , thresholds = None ):
60-
61-
62- if bounds is None :
63- print ('warning, no bounds were defined, so default bounds are used of [0, 0, 0.005, 0.7],[0.005, 1.0, 0.2, 1.3]' )
64- self .bounds = ([0 , 0 , 0.005 , 0.7 ],[0.005 , 1.0 , 0.2 , 1.3 ])
65- else :
66- self .bounds = ([self .bounds ["D" ][0 ], self .bounds ["f" ][0 ], self .bounds ["Dp" ][0 ], self .bounds ["S0" ][0 ]],
67- [self .bounds ["D" ][1 ], self .bounds ["f" ][1 ], self .bounds ["Dp" ][1 ], self .bounds ["S0" ][1 ]])
68- if initial_guess is None :
69- print ('warning, no initial guesses were defined, so default bounds are used of [0.001, 0.001, 0.01, 1]' )
70- self .initial_guess = [0.001 , 0.001 , 0.01 , 1 ]
71- else :
72- self .initial_guess = [self .initial_guess ["D" ], self .initial_guess ["f" ], self .initial_guess ["Dp" ], self .initial_guess ["S0" ]]
73- self .use_initial_guess = True
74- self .use_bounds = True
75- if thresholds is None :
76- print ('warning, no bounds were defined, so default bounds are used of [0, 0, 0.005, 0.7],[0.005, 1.0, 0.2, 1.3]' )
77- thresholds = 150
60+ self .use_initial_guess = {"f" : True , "D" : True , "Dp" : True , "S0" : True }
61+ self .use_bounds = {"f" : True , "D" : True , "Dp" : True , "S0" : True }
7862 self .thresholds = thresholds
63+
7964 if prior_in is None :
8065 print ('using a flat prior between bounds' )
81- 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 ]])
66+ self .neg_log_prior = flat_neg_log_prior ([self .bounds ["D" ][0 ],self .bounds ["D" ][ 1 ]],[self .bounds ["f" ][ 0 ],self .bounds ["f" ][1 ]],[self .bounds ["Dp" ][ 0 ],self .bounds ["Dp" ][ 1 ]],[self .bounds ["S0" ][ 0 ],self .bounds ["S0" ][ 1 ]])
8267 else :
8368 print ('warning, bounds are not used, as a prior is used instead' )
8469 if len (prior_in ) == 4 :
@@ -97,15 +82,20 @@ def ivim_fit(self, signals, bvalues, initial_guess=None, **kwargs):
9782 Returns:
9883 _type_: _description_
9984 """
85+ bounds = ([self .bounds ["D" ][0 ], self .bounds ["f" ][0 ], self .bounds ["Dp" ][0 ], self .bounds ["S0" ][0 ]],
86+ [self .bounds ["D" ][1 ], self .bounds ["f" ][1 ], self .bounds ["Dp" ][1 ], self .bounds ["S0" ][1 ]])
87+
88+ initial_guess = [self .initial_guess ["D" ], self .initial_guess ["f" ], self .initial_guess ["Dp" ], self .initial_guess ["S0" ]]
89+
10090 bvalues = np .array (bvalues )
10191
10292 epsilon = 0.000001
103- fit_results = fit_segmented (bvalues , signals , bounds = self . bounds , cutoff = self .thresholds , p0 = self . initial_guess )
93+ fit_results = fit_segmented (bvalues , signals , bounds = bounds , cutoff = self .thresholds , p0 = initial_guess )
10494 fit_results = np .array (fit_results + (1 ,))
10595 for i in range (4 ):
106- if fit_results [i ] < self . bounds [0 ][i ] : fit_results [0 ] = self . bounds [0 ][i ]+ epsilon
107- if fit_results [i ] > self . bounds [1 ][i ] : fit_results [0 ] = self . bounds [1 ][i ]- epsilon
108- fit_results = self .OGC_algorithm (bvalues , signals , self .neg_log_prior , x0 = fit_results , fitS0 = self .fitS0 , bounds = self . bounds )
96+ if fit_results [i ] < bounds [0 ][i ] : fit_results [0 ] = bounds [0 ][i ]+ epsilon
97+ if fit_results [i ] > bounds [1 ][i ] : fit_results [0 ] = bounds [1 ][i ]- epsilon
98+ fit_results = self .OGC_algorithm (bvalues , signals , self .neg_log_prior , x0 = fit_results , fitS0 = self .fitS0 , bounds = bounds )
10999
110100 results = {}
111101 results ["D" ] = fit_results [0 ]
@@ -122,6 +112,11 @@ def ivim_fit_full_volume(self, signals, bvalues, njobs=4, **kwargs):
122112 Returns:
123113 _type_: _description_
124114 """
115+ bounds = ([self .bounds ["D" ][0 ], self .bounds ["f" ][0 ], self .bounds ["Dp" ][0 ], self .bounds ["S0" ][0 ]],
116+ [self .bounds ["D" ][1 ], self .bounds ["f" ][1 ], self .bounds ["Dp" ][1 ], self .bounds ["S0" ][1 ]])
117+
118+ initial_guess = [self .initial_guess ["D" ], self .initial_guess ["f" ], self .initial_guess ["Dp" ], self .initial_guess ["S0" ]]
119+
125120 # normalize signals
126121 # Get index of b=0
127122 shape = np .shape (signals )
@@ -140,19 +135,19 @@ def ivim_fit_full_volume(self, signals, bvalues, njobs=4, **kwargs):
140135 bvalues = np .array (bvalues )
141136
142137 epsilon = 0.000001
143- fit_results = np .array (fit_segmented_array (bvalues , signals , bounds = self . bounds , cutoff = self .thresholds , p0 = self . initial_guess ))
138+ fit_results = np .array (fit_segmented_array (bvalues , signals , bounds = bounds , cutoff = self .thresholds , p0 = initial_guess ))
144139 #fit_results=np.array(fit_results+(1,))
145140 # Loop over parameters (rows)
146141
147142 for i in range (4 ):
148143 if i == 3 :
149144 fit_results [i ] = np .random .normal (1 ,0.2 ,np .shape (fit_results [i ]))
150145 else :
151- below = fit_results [i ] < self . bounds [0 ][i ]
152- above = fit_results [i ] > self . bounds [1 ][i ]
146+ below = fit_results [i ] < bounds [0 ][i ]
147+ above = fit_results [i ] > bounds [1 ][i ]
153148
154- fit_results [i , below ] = self . bounds [0 ][i ] + epsilon
155- fit_results [i , above ] = self . bounds [1 ][i ] - epsilon
149+ fit_results [i , below ] = bounds [0 ][i ] + epsilon
150+ fit_results [i , above ] = bounds [1 ][i ] - epsilon
156151 self .jobs = njobs
157152 fit_results = self .OGC_algorithm_array (bvalues , signals ,fit_results , self )
158153
0 commit comments