@@ -17,8 +17,7 @@ def __init__(self, corr=.95, prior_win=10):
1717 self .ndim = 3
1818 self .mean = np .linspace (- 1 , 1 , self .ndim )
1919 self .cov = np .identity (self .ndim )
20- self .cov [self .cov ==
21- 0 ] = corr
20+ self .cov [self .cov == 0 ] = corr
2221 self .cov_inv = linalg .inv (self .cov )
2322 self .lnorm = - 0.5 * (np .log (2 * np .pi ) * self .ndim +
2423 np .log (linalg .det (self .cov )))
@@ -39,13 +38,14 @@ def prior_transform(self, u):
3938def test_static_proposal_stats ():
4039 rstate = get_rstate ()
4140 g = Gaussian ()
42- sampler = dynesty .NestedSampler (g .loglikelihood ,
43- g .prior_transform ,
44- g .ndim ,
45- nlive = nlive ,
46- rstate = rstate ,
47- sample = 'rwalk' ,
48- blob = False ) # blob=False to isolate proposal_stats
41+ sampler = dynesty .NestedSampler (
42+ g .loglikelihood ,
43+ g .prior_transform ,
44+ g .ndim ,
45+ nlive = nlive ,
46+ rstate = rstate ,
47+ sample = 'rwalk' ,
48+ blob = False ) # blob=False to isolate proposal_stats
4949 sampler .run_nested (print_progress = printing , maxiter = 100 )
5050 res = sampler .results
5151 assert 'proposal_stats' in res
@@ -61,13 +61,14 @@ def test_static_proposal_stats():
6161def test_dynamic_proposal_stats ():
6262 rstate = get_rstate ()
6363 g = Gaussian ()
64- sampler = dynesty .DynamicNestedSampler (g .loglikelihood ,
65- g .prior_transform ,
66- g .ndim ,
67- nlive = nlive ,
68- rstate = rstate ,
69- sample = 'unif' ,
70- blob = False ) # blob=False to isolate proposal_stats
64+ sampler = dynesty .DynamicNestedSampler (
65+ g .loglikelihood ,
66+ g .prior_transform ,
67+ g .ndim ,
68+ nlive = nlive ,
69+ rstate = rstate ,
70+ sample = 'unif' ,
71+ blob = False ) # blob=False to isolate proposal_stats
7172 sampler .run_nested (print_progress = printing , dlogz_init = 1 , maxiter_init = 100 )
7273 res = sampler .results
7374 assert 'proposal_stats' in res
@@ -84,16 +85,17 @@ def test_proposal_stats_length_consistency():
8485 """Test that proposal_stats array has same length as samples array"""
8586 rstate = get_rstate ()
8687 g = Gaussian ()
87- sampler = dynesty .NestedSampler (g .loglikelihood ,
88- g .prior_transform ,
89- g .ndim ,
90- nlive = nlive // 10 , # smaller for faster test
91- rstate = rstate ,
92- sample = 'rwalk' ,
93- blob = False )
88+ sampler = dynesty .NestedSampler (
89+ g .loglikelihood ,
90+ g .prior_transform ,
91+ g .ndim ,
92+ nlive = nlive // 10 , # smaller for faster test
93+ rstate = rstate ,
94+ sample = 'rwalk' ,
95+ blob = False )
9496 sampler .run_nested (print_progress = printing , maxiter = 50 )
9597 res = sampler .results
96-
98+
9799 # Check length consistency
98100 assert len (res ['proposal_stats' ]) == len (res .samples )
99101
@@ -102,30 +104,32 @@ def test_proposal_stats_different_samplers():
102104 """Test proposal_stats with different sampling methods"""
103105 rstate = get_rstate ()
104106 g = Gaussian ()
105-
107+
106108 # Test different sampling methods
107109 sampling_methods = ['unif' , 'rwalk' , 'slice' , 'rslice' ]
108-
110+
109111 for sample_method in sampling_methods :
110- sampler = dynesty .NestedSampler (g .loglikelihood ,
111- g .prior_transform ,
112- g .ndim ,
113- nlive = nlive // 10 , # smaller for faster test
114- rstate = rstate ,
115- sample = sample_method ,
116- blob = False )
112+ sampler = dynesty .NestedSampler (
113+ g .loglikelihood ,
114+ g .prior_transform ,
115+ g .ndim ,
116+ nlive = nlive // 10 , # smaller for faster test
117+ rstate = rstate ,
118+ sample = sample_method ,
119+ blob = False )
117120 sampler .run_nested (print_progress = printing , maxiter = 30 )
118121 res = sampler .results
119-
122+
120123 # Basic checks
121124 assert 'proposal_stats' in res
122125 assert res ['proposal_stats' ] is not None
123126 assert len (res ['proposal_stats' ]) == len (res ['logl' ])
124-
127+
125128 # Check that we have some non-None proposal_stats
126- non_none_count = sum (1 for ps in res ['proposal_stats' ] if ps is not None )
129+ non_none_count = sum (1 for ps in res ['proposal_stats' ]
130+ if ps is not None )
127131 assert non_none_count > 0 , f"No proposal_stats found for { sample_method } "
128-
132+
129133 # Check structure of non-None entries
130134 for ps in res ['proposal_stats' ]:
131135 if ps is not None :
@@ -139,42 +143,42 @@ def test_proposal_stats_nested_vs_dynamic():
139143 """Test proposal_stats consistency between nested and dynamic nested samplers"""
140144 rstate = get_rstate ()
141145 g = Gaussian ()
142-
146+
143147 # Test with nested sampler
144148 sampler_nested = dynesty .NestedSampler (g .loglikelihood ,
145- g .prior_transform ,
146- g .ndim ,
147- nlive = nlive // 10 ,
148- rstate = rstate ,
149- sample = 'rwalk' ,
150- blob = False )
149+ g .prior_transform ,
150+ g .ndim ,
151+ nlive = nlive // 10 ,
152+ rstate = rstate ,
153+ sample = 'rwalk' ,
154+ blob = False )
151155 sampler_nested .run_nested (print_progress = printing , maxiter = 30 )
152156 res_nested = sampler_nested .results
153-
157+
154158 # Test with dynamic nested sampler
155159 rstate = get_rstate () # Reset to same state
156160 sampler_dynamic = dynesty .DynamicNestedSampler (g .loglikelihood ,
157- g .prior_transform ,
158- g .ndim ,
159- nlive = nlive // 10 ,
160- rstate = rstate ,
161- sample = 'rwalk' ,
162- blob = False )
163- sampler_dynamic .run_nested (print_progress = printing ,
164- dlogz_init = 1 ,
165- maxiter_init = 30 )
161+ g .prior_transform ,
162+ g .ndim ,
163+ nlive = nlive // 10 ,
164+ rstate = rstate ,
165+ sample = 'rwalk' ,
166+ blob = False )
167+ sampler_dynamic .run_nested (print_progress = printing ,
168+ dlogz_init = 1 ,
169+ maxiter_init = 30 )
166170 res_dynamic = sampler_dynamic .results
167-
171+
168172 # Both should have proposal_stats
169173 assert 'proposal_stats' in res_nested
170174 assert 'proposal_stats' in res_dynamic
171175 assert res_nested ['proposal_stats' ] is not None
172176 assert res_dynamic ['proposal_stats' ] is not None
173-
177+
174178 # Both should have consistent lengths
175179 assert len (res_nested ['proposal_stats' ]) == len (res_nested ['logl' ])
176180 assert len (res_dynamic ['proposal_stats' ]) == len (res_dynamic ['logl' ])
177-
181+
178182 # Both should have some non-None proposal_stats
179183 assert any (ps is not None for ps in res_nested ['proposal_stats' ])
180184 assert any (ps is not None for ps in res_dynamic ['proposal_stats' ])
@@ -187,67 +191,74 @@ def test_proposal_stats_content_validity():
187191 sampler = dynesty .NestedSampler (g .loglikelihood ,
188192 g .prior_transform ,
189193 g .ndim ,
190- nlive = nlive // 10 ,
194+ nlive = nlive // 10 ,
191195 rstate = rstate ,
192196 sample = 'rwalk' ,
193197 blob = False )
194198 sampler .run_nested (print_progress = printing , maxiter = 30 )
195199 res = sampler .results
196-
200+
197201 # Check content of proposal_stats
198202 for i , ps in enumerate (res ['proposal_stats' ]):
199203 if ps is not None :
200204 # Should be a dictionary
201205 assert isinstance (ps , dict ), f"proposal_stats[{ i } ] is not a dict"
202-
206+
203207 # Should contain n_proposals
204208 assert 'n_proposals' in ps , f"proposal_stats[{ i } ] missing 'n_proposals'"
205-
209+
206210 # n_proposals should be a positive integer
207211 n_prop = ps ['n_proposals' ]
208- assert isinstance (n_prop , (int , np .integer )), f"n_proposals is not an integer at index { i } "
212+ assert isinstance (
213+ n_prop ,
214+ (int ,
215+ np .integer )), f"n_proposals is not an integer at index { i } "
209216 assert n_prop > 0 , f"n_proposals should be positive at index { i } , got { n_prop } "
210-
217+
211218 # If there are other keys, they should have reasonable values
212219 for key , value in ps .items ():
213220 if key != 'n_proposals' :
214221 # Any additional stats should be numeric and finite
215222 if isinstance (value , (int , float , np .number )):
216- assert np .isfinite (value ), f"Non-finite value in proposal_stats[{ i } ]['{ key } ']"
223+ assert np .isfinite (
224+ value
225+ ), f"Non-finite value in proposal_stats[{ i } ]['{ key } ']"
217226
218227
219228def test_proposal_stats_with_blob ():
220229 """Test that proposal_stats work correctly when blob is also enabled"""
230+
221231 def loglikelihood_with_blob (x ):
222232 logl = - 0.5 * np .sum (x ** 2 )
223233 blob = {'param_sum' : np .sum (x ), 'param_max' : np .max (x )}
224234 return logl , blob
225-
235+
226236 def prior_transform (u ):
227237 return 4 * (u - 0.5 ) # uniform from -2 to 2
228-
238+
229239 rstate = get_rstate ()
230- sampler = dynesty .NestedSampler (loglikelihood_with_blob ,
231- prior_transform ,
232- 3 , # ndim
233- nlive = nlive // 10 ,
234- rstate = rstate ,
235- sample = 'rwalk' ,
236- blob = True )
240+ sampler = dynesty .NestedSampler (
241+ loglikelihood_with_blob ,
242+ prior_transform ,
243+ 3 , # ndim
244+ nlive = nlive // 10 ,
245+ rstate = rstate ,
246+ sample = 'rwalk' ,
247+ blob = True )
237248 sampler .run_nested (print_progress = printing , maxiter = 30 )
238249 res = sampler .results
239-
250+
240251 # Should have both blob and proposal_stats
241252 assert 'blob' in res
242253 assert 'proposal_stats' in res
243254 assert res ['blob' ] is not None
244255 assert res ['proposal_stats' ] is not None
245-
256+
246257 # Both arrays should have same length as samples
247258 assert len (res ['blob' ]) == len (res .samples )
248259 assert len (res ['proposal_stats' ]) == len (res .samples )
249260 assert len (res ['proposal_stats' ]) == len (res ['logl' ])
250-
261+
251262 # Check that both contain valid data
252263 assert any (b is not None for b in res ['blob' ])
253264 assert any (ps is not None for ps in res ['proposal_stats' ])
0 commit comments