Skip to content

Commit 18f3c63

Browse files
committed
a bit of cleanup after code review
1 parent 4231cea commit 18f3c63

3 files changed

Lines changed: 40 additions & 15 deletions

File tree

py/dynesty/bounding.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,35 @@
4545
# these are the exitsing bounds implemented here
4646

4747

48+
def _slogdet_checked(matrix):
49+
"""
50+
Compute the log-determinant and check that it's positive.
51+
52+
Parameters
53+
----------
54+
matrix : array_like
55+
Matrix to compute determinant for.
56+
57+
Returns
58+
-------
59+
detln : float
60+
Natural log of the absolute value of the determinant.
61+
62+
Raises
63+
------
64+
ValueError
65+
If the determinant is not positive.
66+
"""
67+
detsign, detln = linalg.slogdet(matrix)
68+
if detsign <= 0:
69+
raise ValueError(
70+
f"Invalid ellipsoid: covariance matrix has non-positive determinant "
71+
f"(sign={detsign}). This may indicate degenerate live points or "
72+
f"numerical instability. Matrix shape: {matrix.shape}"
73+
)
74+
return detln
75+
76+
4877
class Bound:
4978
"""
5079
This is is master class for the all the bounds listing
@@ -729,8 +758,7 @@ def __init__(self, ndim, cov=None):
729758
self.axes = lalg.sqrtm(self.cov)
730759
self.axes_inv = lalg.pinvh(self.axes)
731760

732-
detsign, detln = linalg.slogdet(self.am)
733-
assert detsign > 0
761+
detln = _slogdet_checked(self.am)
734762
self.logvol = logvol_prefactor(self.ndim) - 0.5 * detln
735763
self.funit = 1
736764
self.ctrs = [] # placeholder
@@ -922,8 +950,7 @@ def update(self,
922950
self.ctrs = points
923951

924952
# Compute volume.
925-
detsign, detln = linalg.slogdet(self.am)
926-
assert detsign > 0
953+
detln = _slogdet_checked(self.am)
927954
self.logvol = (logvol_prefactor(self.ndim) - 0.5 * detln)
928955

929956
# Estimate the volume and fractional overlap with the unit cube
@@ -997,8 +1024,7 @@ def __init__(self, ndim, cov=None):
9971024
self.axes = lalg.sqrtm(self.cov)
9981025
self.axes_inv = lalg.pinvh(self.axes)
9991026

1000-
detsign, detln = linalg.slogdet(self.am)
1001-
assert detsign > 0
1027+
detln = _slogdet_checked(self.am)
10021028
self.logvol = self.ndim * np.log(2.) - 0.5 * detln
10031029
self.funit = 1
10041030
self.ctrs = []
@@ -1190,8 +1216,7 @@ def update(self,
11901216
self.axes *= hsmax
11911217
self.axes_inv /= hsmax
11921218

1193-
detsign, detln = linalg.slogdet(self.am)
1194-
assert detsign > 0
1219+
detln = _slogdet_checked(self.am)
11951220
self.logvol = (self.ndim * np.log(2.) - 0.5 * detln)
11961221

11971222
# Estimate the volume and fractional overlap with the unit cube

py/dynesty/plotting.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ def traceplot(results,
660660
try:
661661
ids = connect_highlight[0]
662662
ids = connect_highlight
663-
except:
663+
except Exception:
664664
ids = rstate.choice(uid, size=connect_highlight, replace=False)
665665

666666
# Determine plotting bounds for marginalized 1-D posteriors.
@@ -1015,7 +1015,7 @@ def cornerpoints(results,
10151015
for j, y in enumerate(samples[:-1]):
10161016
try:
10171017
ax = axes[i, j]
1018-
except:
1018+
except Exception:
10191019
ax = axes
10201020
# Setup axes.
10211021
if span is not None:
@@ -1682,7 +1682,7 @@ def boundplot(results,
16821682
r = -(nlive + i)
16831683
uidx = samples_id[r]
16841684
live_u[uidx] = samples[r]
1685-
except:
1685+
except Exception:
16861686
# In the dynamic sampling case, we will show the live points used
16871687
# during the batch associated with a particular iteration/bound.
16881688
batch = results['samples_batch'][it] # select batch
@@ -1735,7 +1735,7 @@ def boundplot(results,
17351735
r = -(nlive + i)
17361736
uidx = samples_id[r]
17371737
live_u[uidx] = samples[r]
1738-
except:
1738+
except Exception:
17391739
raise ValueError("Live point tracking currently not "
17401740
"implemented for dynamic sampling results.")
17411741
# Draw samples.
@@ -2003,7 +2003,7 @@ def cornerbound(results,
20032003
r = -(nlive + i)
20042004
uidx = samples_id[r]
20052005
live_u[uidx] = samples[r]
2006-
except:
2006+
except Exception:
20072007
# In the dynamic sampling case, we will show the live points used
20082008
# during the batch associated with a particular iteration/bound.
20092009
if it is not None:
@@ -2314,7 +2314,7 @@ def _hist2d(x,
23142314
for i, v0 in enumerate(levels):
23152315
try:
23162316
V[i] = Hflat[sm <= v0][-1]
2317-
except:
2317+
except Exception:
23182318
V[i] = Hflat[0]
23192319
V.sort()
23202320
m = (np.diff(V) == 0)

py/dynesty/sampler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def _initialize_live_points(live_points,
228228
"log-likelihood.")
229229
else:
230230
# If we found nothing after many attempts, raise the alarm.
231-
warnings.warn(f"After {n_attempts} attempts, we cound not "
231+
warnings.warn(f"After {n_attempts} attempts, we could not "
232232
f"find at least {min_npoints} points "
233233
"that have a valid log-likelihood! "
234234
"The initial sampling is very inefficient!")

0 commit comments

Comments
 (0)