@@ -123,6 +123,7 @@ def run_spock_from_args(
123
123
all_bic = np .zeros ((len (idxs ), max_breakpoints + 1 ), dtype = float )
124
124
all_n = np .zeros ((len (idxs ), max_breakpoints + 1 ), dtype = int )
125
125
all_sc = np .zeros ((len (idxs ), max_breakpoints + 1 ), dtype = bool )
126
+ msels = []
126
127
for i , idx in enumerate (idxs ):
127
128
fitted = False
128
129
try :
@@ -139,6 +140,7 @@ def run_spock_from_args(
139
140
tolerance = xthresh ,
140
141
verbose = verb > 2 ,
141
142
)
143
+ msels .append (msel )
142
144
all_sc [i , :] = np .array (
143
145
[
144
146
slope_check (summary ["slopes" ], verb )
@@ -226,6 +228,7 @@ def run_spock_from_args(
226
228
print (
227
229
f"Fit did not converge with descriptor index { idx } : { tags [idx ]} \n due to { m } "
228
230
)
231
+ msels .append (None )
229
232
230
233
# Done iterating over descriptors
231
234
best_n = np .zeros_like (idxs , dtype = int )
@@ -293,17 +296,21 @@ def run_spock_from_args(
293
296
f"Fitting volcano with { n } breakpoints and descriptor index { idx } : { tags [idx ]} , as determined from BIC."
294
297
)
295
298
descriptor = d [:, idx ].reshape (- 1 )
296
- xrange = 0.05 * (max (descriptor ) - min (descriptor ))
299
+ xthresh = 0.05 * (max (descriptor ) - min (descriptor ))
297
300
pw_fit = Fit (
298
301
descriptor ,
299
302
target ,
300
303
n_breakpoints = n ,
301
304
weights = weights ,
302
- max_iterations = 5000 ,
303
- tolerance = xrange ,
305
+ max_iterations = n_iter_helper ( fitted ) ,
306
+ tolerance = xthresh ,
304
307
)
305
308
if not pw_fit .best_muggeo :
306
- raise ConvergenceError ("The fitting process did not converge." )
309
+ # If for some reason the fit fails now, we use the preliminary fit instead
310
+ pw_fit .best_muggeo = msels [idx - 1 ].models [n - 1 ].best_muggeo
311
+ if not slope_check (pw_fit .get_results ()["slopes" ], verb ):
312
+ # If for some reason the fit switched the slopes, we use the preliminary fit instead
313
+ pw_fit .best_muggeo = msels [idx - 1 ].models [n - 1 ].best_muggeo
307
314
if verb > 2 :
308
315
pw_fit .summary ()
309
316
# Plot the data, fit, breakpoints and confidence intervals
0 commit comments