def test_model_good(self): # compile on instantiation, override model name model = CmdStanModel(model_name='bern', stan_file=BERN_STAN) self.assertEqual(BERN_STAN, model.stan_file) self.assertTrue(model.exe_file.endswith(BERN_EXE.replace('\\', '/'))) self.assertEqual('bern', model.name) # default model name model = CmdStanModel(stan_file=BERN_STAN) self.assertEqual(BERN_BASENAME, model.name) # instantiate with existing exe model = CmdStanModel(stan_file=BERN_STAN, exe_file=BERN_EXE) self.assertEqual(BERN_STAN, model.stan_file) self.assertTrue(model.exe_file.endswith(BERN_EXE)) # instantiate with existing exe only - no model model2 = CmdStanModel(exe_file=BERN_EXE) self.assertEqual(BERN_EXE, model2.exe_file) with self.assertRaises(RuntimeError): model2.code() with self.assertRaises(RuntimeError): model2.compile() # instantiate, don't compile os.remove(BERN_EXE) model = CmdStanModel(stan_file=BERN_STAN, compile=False) self.assertEqual(BERN_STAN, model.stan_file) self.assertEqual(None, model.exe_file)
def test_model_compile_space(self): with tempfile.TemporaryDirectory( prefix="cmdstanpy_testfolder_" ) as tmp_path: path_with_space = os.path.join(tmp_path, "space in path") os.makedirs(path_with_space, exist_ok=True) bern_stan_new = os.path.join( path_with_space, os.path.split(BERN_STAN)[1] ) bern_exe_new = os.path.join( path_with_space, os.path.split(BERN_EXE)[1] ) shutil.copyfile(BERN_STAN, bern_stan_new) model = CmdStanModel(stan_file=bern_stan_new) old_exe_time = os.path.getmtime(model.exe_file) os.remove(bern_exe_new) model.compile() new_exe_time = os.path.getmtime(model.exe_file) self.assertTrue(new_exe_time > old_exe_time) # test compile with existing exe - timestamp on exe unchanged exe_time = os.path.getmtime(model.exe_file) model2 = CmdStanModel(stan_file=bern_stan_new) self.assertEqual(exe_time, os.path.getmtime(model2.exe_file))
def test_model_good(self): stan = os.path.join(DATAFILES_PATH, 'bernoulli.stan') exe = os.path.join(DATAFILES_PATH, 'bernoulli' + EXTENSION) # compile on instantiation model = CmdStanModel(stan_file=stan) self.assertEqual(stan, model.stan_file) self.assertTrue(model.exe_file.endswith(exe.replace('\\', '/'))) # instantiate with existing exe model = CmdStanModel(stan_file=stan, exe_file=exe) self.assertEqual(stan, model.stan_file) self.assertTrue(model.exe_file.endswith(exe)) # instantiate with existing exe only - no model exe = os.path.join(DATAFILES_PATH, 'bernoulli' + EXTENSION) model2 = CmdStanModel(exe_file=exe) self.assertEqual(exe, model2.exe_file) self.assertEqual('bernoulli', model2.name) with self.assertRaises(RuntimeError): model2.code() with self.assertRaises(RuntimeError): model2.compile() # instantiate, don't compile os.remove(exe) model = CmdStanModel(stan_file=stan, compile=False) self.assertEqual(stan, model.stan_file) self.assertEqual(None, model.exe_file)
def test_exe_only(self): model = CmdStanModel(stan_file=BERN_STAN) self.assertEqual(BERN_EXE, model.exe_file) exe_only = os.path.join(DATAFILES_PATH, 'exe_only') shutil.copyfile(model.exe_file, exe_only) model2 = CmdStanModel(exe_file=exe_only) with self.assertRaises(RuntimeError): model2.code() with self.assertRaises(RuntimeError): model2.compile() self.assertFalse(model2._fixed_param)
def test_model_good_no_source(self): stan = os.path.join(datafiles_path, 'bernoulli.stan') exe = os.path.join(datafiles_path, 'bernoulli' + EXTENSION) model = CmdStanModel(stan_file=stan) model2 = CmdStanModel(exe_file=exe) self.assertEqual(exe, model2.exe_file) self.assertEqual('bernoulli', model2.name) with self.assertRaises(RuntimeError): model2.code() with self.assertRaises(RuntimeError): model2.compile()
def test_model_pedantic(self): stan_file = os.path.join(DATAFILES_PATH, 'bernoulli_pedantic.stan') with LogCapture(level=logging.WARNING) as log: logging.getLogger() model = CmdStanModel(model_name='bern', stan_file=stan_file) model.compile(force=True, stanc_options={'warn-pedantic': True}) log.check_present( ( 'cmdstanpy', 'WARNING', StringComparison(r'(?s).*The parameter theta has no priors.*'), ) )
def test_model_info(self): model = CmdStanModel(stan_file=BERN_STAN, compile=False) model.compile(force=True) info_dict = model.exe_info() self.assertEqual(info_dict['STAN_THREADS'].lower(), 'false') if model.exe_file is not None and os.path.exists(model.exe_file): os.remove(model.exe_file) empty_dict = model.exe_info() self.assertEqual(len(empty_dict), 0) model_info = model.src_info() self.assertNotEqual(model_info, {}) self.assertIn('theta', model_info['parameters'])
def test_model_compile(self): model = CmdStanModel(stan_file=BERN_STAN) self.assertTrue(model.exe_file.endswith(BERN_EXE.replace('\\', '/'))) model = CmdStanModel(stan_file=BERN_STAN) self.assertTrue(model.exe_file.endswith(BERN_EXE.replace('\\', '/'))) old_exe_time = os.path.getmtime(model.exe_file) os.remove(BERN_EXE) model.compile() new_exe_time = os.path.getmtime(model.exe_file) self.assertTrue(new_exe_time > old_exe_time) # test compile with existing exe - timestamp on exe unchanged exe_time = os.path.getmtime(model.exe_file) model2 = CmdStanModel(stan_file=BERN_STAN) self.assertEqual(exe_time, os.path.getmtime(model2.exe_file))
def test_check_sampler_csv_thin(self): stan = os.path.join(DATAFILES_PATH, 'bernoulli.stan') bern_model = CmdStanModel(stan_file=stan) bern_model.compile() jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json') bern_fit = bern_model.sample( data=jdata, chains=1, parallel_chains=1, seed=12345, iter_sampling=490, iter_warmup=490, thin=7, max_treedepth=11, adapt_delta=0.98, ) csv_file = bern_fit.runset.csv_files[0] dict = check_sampler_csv( path=csv_file, is_fixed_param=False, iter_sampling=490, iter_warmup=490, thin=7, ) self.assertEqual(dict['num_samples'], 490) self.assertEqual(dict['thin'], 7) self.assertEqual(dict['draws_sampling'], 70) self.assertEqual(dict['seed'], 12345) self.assertEqual(dict['max_depth'], 11) self.assertEqual(dict['delta'], 0.98) with self.assertRaisesRegex(ValueError, 'config error'): check_sampler_csv( path=csv_file, is_fixed_param=False, iter_sampling=490, iter_warmup=490, thin=9, ) with self.assertRaisesRegex(ValueError, 'expected 490 draws, found 70'): check_sampler_csv( path=csv_file, is_fixed_param=False, iter_sampling=490, iter_warmup=490, )
def test_model_compile(self): stan = os.path.join(datafiles_path, 'bernoulli.stan') exe = os.path.join(datafiles_path, 'bernoulli' + EXTENSION) model = CmdStanModel(stan_file=stan) self.assertTrue(model.exe_file.endswith(exe.replace('\\', '/'))) model = CmdStanModel(stan_file=stan) self.assertTrue(model.exe_file.endswith(exe.replace('\\', '/'))) old_exe_time = os.path.getmtime(model.exe_file) os.remove(exe) model.compile() new_exe_time = os.path.getmtime(model.exe_file) self.assertTrue(new_exe_time > old_exe_time) # test compile with existing exe - timestamp on exe unchanged exe_time = os.path.getmtime(model.exe_file) model2 = CmdStanModel(stan_file=stan) self.assertEqual(exe_time, os.path.getmtime(model2.exe_file))
def test_check_sampler_csv_thin(self): stan = os.path.join(DATAFILES_PATH, 'bernoulli.stan') bern_model = CmdStanModel(stan_file=stan) bern_model.compile() jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json') bern_fit = bern_model.sample( data=jdata, chains=1, cores=1, seed=12345, sampling_iters=490, warmup_iters=490, thin=7, max_treedepth=11, adapt_delta=0.98, ) csv_file = bern_fit.runset.csv_files[0] dict = check_sampler_csv(csv_file) self.assertEqual(dict['num_samples'], 490) self.assertEqual(dict['thin'], 7) self.assertEqual(dict['draws'], 70) self.assertEqual(dict['seed'], 12345) self.assertEqual(dict['max_depth'], 11) self.assertEqual(dict['delta'], 0.98)
def test_compile_force(self): if os.path.exists(BERN_EXE): os.remove(BERN_EXE) model = CmdStanModel(stan_file=BERN_STAN, compile=False, cpp_options={}) self.assertIsNone(model.exe_file) model.compile(force=True) self.assertIsNotNone(model.exe_file) self.assertTrue(os.path.exists(model.exe_file)) info_dict = model.exe_info() print(f'info={info_dict}') self.assertEqual(info_dict['STAN_THREADS'].lower(), 'false') more_opts = {'STAN_THREADS': 'TRUE'} model.compile(force=True, cpp_options=more_opts) self.assertIsNotNone(model.exe_file) self.assertTrue(os.path.exists(model.exe_file)) info_dict2 = model.exe_info() print(f'info2={info_dict2}') self.assertEqual(info_dict2['STAN_THREADS'].lower(), 'true') override_opts = {'STAN_NO_RANGE_CHECKS': 'TRUE'} model.compile( force=True, cpp_options=override_opts, override_options=True ) info_dict3 = model.exe_info() print(f'info3={info_dict3}') self.assertEqual(info_dict3['STAN_THREADS'].lower(), 'false') # cmdstan#1056 # self.assertEqual(info_dict3['STAN_NO_RANGE_CHECKS'].lower(), 'true') model.compile(force=True, cpp_options=more_opts) info_dict4 = model.exe_info() self.assertEqual(info_dict4['STAN_THREADS'].lower(), 'true') # test compile='force' in constructor model2 = CmdStanModel(stan_file=BERN_STAN, compile='force') info_dict5 = model2.exe_info() self.assertEqual(info_dict5['STAN_THREADS'].lower(), 'false')