22 Tests for the Pathfinder method.
33"""
44
5+ import contextlib
6+ from io import StringIO
57from pathlib import Path
68
79import numpy as np
@@ -129,6 +131,26 @@ def test_pathfinder_init_sampling():
129131 assert fit .draws ().shape == (1000 , 4 , 9 )
130132
131133
134+ def test_inits_for_pathfinder ():
135+ stan = DATAFILES_PATH / 'bernoulli.stan'
136+ bern_model = cmdstanpy .CmdStanModel (stan_file = stan )
137+ jdata = str (DATAFILES_PATH / 'bernoulli.data.json' )
138+ bern_model .pathfinder (
139+ jdata , inits = [{"theta" : 0.1 }, {"theta" : 0.9 }], num_paths = 2
140+ )
141+
142+ # second path is initialized too large!
143+ with contextlib .redirect_stdout (StringIO ()) as captured :
144+ bern_model .pathfinder (
145+ jdata ,
146+ inits = [{"theta" : 0.1 }, {"theta" : 1.1 }],
147+ num_paths = 2 ,
148+ show_console = True ,
149+ )
150+
151+ assert "Bounded variable is 1.1" in captured .getvalue ()
152+
153+
132154def test_pathfinder_no_psis ():
133155 stan = DATAFILES_PATH / 'bernoulli.stan'
134156 bern_model = cmdstanpy .CmdStanModel (stan_file = stan )
@@ -152,3 +174,20 @@ def test_pathfinder_no_lp_calc():
152174 n_lp_nan = np .sum (np .isnan (pathfinder .method_variables ()['lp__' ]))
153175 assert n_lp_nan < 4000 # some lp still calculated during pathfinder
154176 assert n_lp_nan > 3000 # but most are not
177+
178+
179+ def test_pathfinder_threads ():
180+ stan = DATAFILES_PATH / 'bernoulli.stan'
181+ bern_model = cmdstanpy .CmdStanModel (stan_file = stan )
182+ jdata = str (DATAFILES_PATH / 'bernoulli.data.json' )
183+
184+ bern_model .pathfinder (data = jdata , num_threads = 1 )
185+
186+ with pytest .raises (ValueError , match = "STAN_THREADS" ):
187+ bern_model .pathfinder (data = jdata , num_threads = 4 )
188+
189+ bern_model = cmdstanpy .CmdStanModel (
190+ stan_file = stan , cpp_options = {'STAN_THREADS' : True }, force_compile = True
191+ )
192+ pathfinder = bern_model .pathfinder (data = jdata , num_threads = 4 )
193+ assert pathfinder .draws ().shape == (1000 , 3 )
0 commit comments