Skip to content

Commit 8a3df31

Browse files
committed
refactor: replace bare except: with specific exception types
Replace all 13 bare except: blocks in LSQ_fitting.py and 1 in triexp_fitting_algorithms.py with specific exception types: - Parallel fallbacks: except Exception - curve_fit/minimize failures: except (RuntimeError, ValueError) - Bayesian fit: except (RuntimeError, ValueError, TypeError) - Goodness-of-fit: except (IndexError, ValueError) - NNLS: except Exception Also fix pre-existing bug: raise(params.message) on line 668 was raising a string (TypeError), not an exception. Changed to raise RuntimeError(params.message). Fixes Bug #3 from bugs.md
1 parent be85ee3 commit 8a3df31

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

src/original/fitting/OGC_AmsterdamUMC/LSQ_fitting.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def parfun(i):
9494

9595
output = Parallel(n_jobs=njobs)(delayed(parfun)(i) for i in tqdm(range(len(dw_data)), position=0, leave=True))
9696
Dt, Fp, Dp = np.transpose(output)
97-
except:
97+
except Exception:
9898
# if fails, retry using single core
9999
single = True
100100
else:
@@ -147,7 +147,7 @@ def fit_segmented(bvalues, dw_data, bounds=([0, 0, 0.005],[0.005, 0.7, 0.2]), cu
147147
params, _ = curve_fit(lambda b, Dp: Fp * np.exp(-b * Dp), bvalues, dw_data_remaining, p0=(p0[2]), bounds=bounds2)
148148
Dp = params[0]
149149
return Dt, Fp, Dp
150-
except:
150+
except (RuntimeError, ValueError, FloatingPointError):
151151
# if fit fails, return zeros
152152
# print('segmented fit failed')
153153
return 0., 0., 0.
@@ -183,7 +183,7 @@ def parfun(i):
183183

184184
output = Parallel(n_jobs=njobs)(delayed(parfun)(i) for i in tqdm(range(len(dw_data)), position=0, leave=True))
185185
Dt, Fp, Dp, S0 = np.transpose(output)
186-
except:
186+
except Exception:
187187
single = True
188188
else:
189189
single = True
@@ -207,7 +207,7 @@ def parfun(i):
207207

208208
output = Parallel(n_jobs=njobs)(delayed(parfun)(i) for i in tqdm(range(len(dw_data)), position=0, leave=True))
209209
Dt, Fp, Dp = np.transpose(output)
210-
except:
210+
except Exception:
211211
single = True
212212
else:
213213
single = True
@@ -257,7 +257,7 @@ def fit_least_squares(bvalues, dw_data, S0_output=False, fitS0=True,
257257
return order(Dt, Fp, Dp, S0)
258258
else:
259259
return order(Dt, Fp, Dp)
260-
except:
260+
except (RuntimeError, ValueError, FloatingPointError):
261261
# if fit fails, then do a segmented fit instead
262262
# print('lsq fit failed, trying segmented')
263263
if S0_output:
@@ -297,7 +297,7 @@ def parfun(i):
297297

298298
output = Parallel(n_jobs=njobs)(delayed(parfun)(i) for i in tqdm(range(len(dw_data)), position=0, leave=True))
299299
Fp0, Dt, Fp1, Dp1, Fp2, Dp2 = np.transpose(output)
300-
except:
300+
except Exception:
301301
single = True
302302
else:
303303
single = True
@@ -357,7 +357,7 @@ def fit_least_squares_tri_exp(bvalues, dw_data, S0_output=False, fitS0=True,
357357
return Fp0, Dt, Fp1, Dp1, Fp2, Dp2
358358
else:
359359
return Dt, Fp1, Dp1, Fp2, Dp2
360-
except:
360+
except (RuntimeError, ValueError, FloatingPointError):
361361
# if fit fails, then do a segmented fit instead
362362
# print('lsq fit failed, trying segmented')
363363
if S0_output:
@@ -396,7 +396,7 @@ def parfun(i):
396396

397397
output = Parallel(n_jobs=njobs)(delayed(parfun)(i) for i in tqdm(range(len(dw_data)), position=0, leave=True))
398398
Dt, Fp, Dp, Fp0, Fp2, Dp2 = np.transpose(output)
399-
except:
399+
except Exception:
400400
# if fails, retry using single core
401401
single = True
402402
else:
@@ -463,7 +463,7 @@ def fit_segmented_tri_exp(bvalues, dw_data, bounds=([0, 0, 0, 0.005, 0, 0.06], [
463463
params, _ = curve_fit(lambda b, Dp: Fp2 * np.exp(-b * Dp), bvalueslow, dw_data, p0=(0.1), bounds=bounds1)
464464
Dp2 = params[0]
465465
return Fp0, Dt, Fp, Dp, Fp2, Dp2
466-
except:
466+
except (RuntimeError, ValueError, FloatingPointError, TypeError):
467467
# if fit fails, return zeros
468468
# print('segnetned fit failed')
469469
return 0., 0., 0., 0., 0., 0.
@@ -619,7 +619,7 @@ def parfun(i):
619619
output = Parallel(n_jobs=arg.jobs)(delayed(parfun)(i) for i in tqdm(range(len(dw_data)), position=0,
620620
leave=True))
621621
Dt_pred, Fp_pred, Dp_pred, S0_pred = np.transpose(output)
622-
except:
622+
except Exception:
623623
single = True
624624
else:
625625
single = True
@@ -665,14 +665,14 @@ def fit_bayesian(bvalues, dw_data, neg_log_prior, x0=[0.001, 0.2, 0.05, 1], fitS
665665
else:
666666
params = minimize(neg_log_posterior, x0=x0[:3], args=(bvalues, dw_data, neg_log_prior), bounds=bounds[:3])
667667
if not params.success:
668-
raise (params.message)
668+
raise RuntimeError(params.message)
669669
if fitS0:
670670
Dt, Fp, Dp, S0 = params.x[0], params.x[1], params.x[2], params.x[3]
671671
else:
672672
Dt, Fp, Dp = params.x[0], params.x[1], params.x[2]
673673
S0 = 1
674674
return order(Dt, Fp, Dp, S0)
675-
except:
675+
except (RuntimeError, ValueError, TypeError):
676676
# if fit fails, return regular lsq-fit result
677677
# print('a bayes fit fialed')
678678
return fit_least_squares(bvalues, dw_data, S0_output=True)
@@ -717,7 +717,7 @@ def goodness_of_fit(bvalues, Dt, Fp, Dp, S0, dw_data, Fp2=None, Dp2=None):
717717
adjusted_R2 = 1 - ((1 - R2) * (len(bvalues)) / (len(bvalues) - 6 - 1))
718718
R2[R2 < 0] = 0
719719
adjusted_R2[adjusted_R2 < 0] = 0
720-
except:
720+
except (IndexError, ValueError, TypeError):
721721
if Fp2 is None:
722722
datasim = ivim(bvalues, Dt, Fp, Dp, S0)
723723
else:

src/original/fitting/PV_MUMC/triexp_fitting_algorithms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def find_idx_nearest(array, value):
196196

197197
return Dpar, Fmv, Dmv, Dint, Fint, S0
198198

199-
except:
199+
except Exception:
200200
return 0, 0, 0, 0, 0, 0
201201

202202

0 commit comments

Comments
 (0)