def test_show_console(self):
        stan = os.path.join(DATAFILES_PATH, 'bernoulli.stan')
        bern_model = CmdStanModel(stan_file=stan)
        jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')
        bern_fit = bern_model.sample(
            data=jdata,
            chains=4,
            parallel_chains=2,
            seed=12345,
            iter_sampling=100,
        )
        stan = os.path.join(DATAFILES_PATH, 'bernoulli_ppc.stan')
        model = CmdStanModel(stan_file=stan)

        sys_stdout = io.StringIO()
        with contextlib.redirect_stdout(sys_stdout):
            model.generate_quantities(
                data=jdata,
                mcmc_sample=bern_fit,
                show_console=True,
            )
        console = sys_stdout.getvalue()
        self.assertTrue('Chain [1] method = generate' in console)
        self.assertTrue('Chain [2] method = generate' in console)
        self.assertTrue('Chain [3] method = generate' in console)
        self.assertTrue('Chain [4] method = generate' in console)
    def test_no_xarray(self):
        with self.without_import('xarray', cmdstanpy.stanfit):
            with self.assertRaises(ImportError):
                # if this fails the testing framework is the problem
                import xarray as _  # noqa

            stan = os.path.join(DATAFILES_PATH, 'bernoulli.stan')
            bern_model = CmdStanModel(stan_file=stan)
            jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')
            bern_fit = bern_model.sample(
                data=jdata,
                chains=4,
                parallel_chains=2,
                seed=12345,
                iter_sampling=100,
            )
            stan = os.path.join(DATAFILES_PATH, 'bernoulli_ppc.stan')
            model = CmdStanModel(stan_file=stan)

            bern_gqs = model.generate_quantities(
                data=jdata, mcmc_sample=bern_fit
            )

            with self.assertRaises(RuntimeError):
                bern_gqs.draws_xr()
 def test_sample_plus_quantities_dedup(self):
     # fitted_params - model GQ block: y_rep is PPC of theta
     stan = os.path.join(DATAFILES_PATH, 'bernoulli_ppc.stan')
     model = CmdStanModel(stan_file=stan)
     jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')
     bern_fit = model.sample(
         data=jdata,
         chains=4,
         parallel_chains=2,
         seed=12345,
         iter_sampling=100,
     )
     # gq_model - y_rep[n] == y[n]
     stan = os.path.join(DATAFILES_PATH, 'bernoulli_ppc_dup.stan')
     model = CmdStanModel(stan_file=stan)
     bern_gqs = model.generate_quantities(data=jdata, mcmc_sample=bern_fit)
     # check that models have different y_rep values
     assert_raises(
         AssertionError,
         assert_array_equal,
         bern_fit.stan_variable(var='y_rep'),
         bern_gqs.stan_variable(var='y_rep'),
     )
     # check that stan_variable returns values from gq model
     with open(jdata) as fd:
         bern_data = json.load(fd)
     y_rep = bern_gqs.stan_variable(var='y_rep')
     for i in range(10):
         self.assertEqual(y_rep[0, i], bern_data['y'][i])
    def test_from_mcmc_sample_variables(self):
        stan = os.path.join(DATAFILES_PATH, 'bernoulli.stan')
        bern_model = CmdStanModel(stan_file=stan)
        jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')
        bern_fit = bern_model.sample(
            data=jdata,
            chains=4,
            parallel_chains=2,
            seed=12345,
            iter_sampling=100,
        )
        stan = os.path.join(DATAFILES_PATH, 'bernoulli_ppc.stan')
        model = CmdStanModel(stan_file=stan)

        bern_gqs = model.generate_quantities(data=jdata, mcmc_sample=bern_fit)

        theta = bern_gqs.stan_variable(var='theta')
        self.assertEqual(theta.shape, (400,))
        y_rep = bern_gqs.stan_variable(var='y_rep')
        self.assertEqual(y_rep.shape, (400, 10))
        with self.assertRaises(ValueError):
            bern_gqs.stan_variable(var='eta')
        with self.assertRaises(ValueError):
            bern_gqs.stan_variable(var='lp__')

        vars_dict = bern_gqs.stan_variables()
        var_names = list(
            bern_gqs.mcmc_sample.metadata.stan_vars_cols.keys()
        ) + list(bern_gqs.metadata.stan_vars_cols.keys())
        self.assertEqual(set(var_names), set(list(vars_dict.keys())))
示例#5
0
    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)
示例#6
0
    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_from_mcmc_sample(self):
        # fitted_params sample
        stan = os.path.join(DATAFILES_PATH, 'bernoulli.stan')
        bern_model = CmdStanModel(stan_file=stan)
        jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')
        bern_fit = bern_model.sample(
            data=jdata,
            chains=4,
            parallel_chains=2,
            seed=12345,
            iter_sampling=100,
        )
        # gq_model
        stan = os.path.join(DATAFILES_PATH, 'bernoulli_ppc.stan')
        model = CmdStanModel(stan_file=stan)

        bern_gqs = model.generate_quantities(data=jdata, mcmc_sample=bern_fit)

        self.assertEqual(
            bern_gqs.runset._args.method, Method.GENERATE_QUANTITIES
        )
        self.assertIn('CmdStanGQ: model=bernoulli_ppc', bern_gqs.__repr__())
        self.assertIn('method=generate_quantities', bern_gqs.__repr__())
        self.assertEqual(bern_gqs.runset.chains, 4)
        for i in range(bern_gqs.runset.chains):
            self.assertEqual(bern_gqs.runset._retcode(i), 0)
            csv_file = bern_gqs.runset.csv_files[i]
            self.assertTrue(os.path.exists(csv_file))
示例#8
0
    def test_model_file_does_not_exist(self):
        with self.assertRaises(ValueError):
            CmdStanModel(stan_file='xdlfkjx', exe_file='sdfndjsds')

        stan = os.path.join(DATAFILES_PATH, 'b')
        with self.assertRaises(ValueError):
            CmdStanModel(stan_file=stan)
示例#9
0
    def test_model_compile_includes(self):
        stan = os.path.join(datafiles_path, 'bernoulli_include.stan')
        exe = os.path.join(datafiles_path, 'bernoulli_include' + EXTENSION)
        if os.path.exists(exe):
            os.remove(exe)

        here = os.path.dirname(os.path.abspath(__file__))
        datafiles_abspath = os.path.join(here, 'data')
        include_paths = [datafiles_abspath]

        # test compile with explicit include paths
        model = CmdStanModel(stan_file=stan, include_paths=include_paths)
        self.assertEqual(stan, model.stan_file)
        self.assertTrue(model.exe_file.endswith(exe.replace('\\', '/')))

        # test compile - implicit include path is current dir
        os.remove(os.path.join(datafiles_path, 'bernoulli_include' + '.hpp'))
        os.remove(os.path.join(datafiles_path, 'bernoulli_include' + '.o'))
        os.remove(exe)
        model2 = CmdStanModel(stan_file=stan)
        self.assertEqual(model2.include_paths, include_paths)

        # already compiled
        model3 = CmdStanModel(stan_file=stan)
        self.assertTrue(model3.exe_file.endswith(exe.replace('\\', '/')))
示例#10
0
    def test_model_paths(self):
        # pylint: disable=unused-variable
        model = CmdStanModel(stan_file=BERN_STAN)  # instantiates exe
        self.assertTrue(os.path.exists(BERN_EXE))

        dotdot_stan = os.path.realpath(os.path.join('..', 'bernoulli.stan'))
        dotdot_exe = os.path.realpath(
            os.path.join('..', 'bernoulli' + EXTENSION))
        shutil.copyfile(BERN_STAN, dotdot_stan)
        shutil.copyfile(BERN_EXE, dotdot_exe)
        model1 = CmdStanModel(
            stan_file=os.path.join('..', 'bernoulli.stan'),
            exe_file=os.path.join('..', 'bernoulli' + EXTENSION),
        )
        self.assertEqual(model1.stan_file, dotdot_stan)
        self.assertEqual(model1.exe_file, dotdot_exe)
        os.remove(dotdot_stan)
        os.remove(dotdot_exe)

        tilde_stan = os.path.realpath(
            os.path.join(os.path.expanduser('~'), 'bernoulli.stan'))
        tilde_exe = os.path.realpath(
            os.path.join(os.path.expanduser('~'), 'bernoulli' + EXTENSION))
        shutil.copyfile(BERN_STAN, tilde_stan)
        shutil.copyfile(BERN_EXE, tilde_exe)
        model2 = CmdStanModel(
            stan_file=os.path.join('~', 'bernoulli.stan'),
            exe_file=os.path.join('~', 'bernoulli' + EXTENSION),
        )
        self.assertEqual(model2.stan_file, tilde_stan)
        self.assertEqual(model2.exe_file, tilde_exe)
        os.remove(tilde_stan)
        os.remove(tilde_exe)
示例#11
0
    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))
示例#12
0
    def test_stanc_options(self):
        opts = {
            'O': True,
            'allow_undefined': True,
            'use-opencl': True,
            'name': 'foo',
        }
        model = CmdStanModel(stan_file=BERN_STAN,
                             compile=False,
                             stanc_options=opts)
        stanc_opts = model.stanc_options
        self.assertTrue(stanc_opts['O'])
        self.assertTrue(stanc_opts['allow_undefined'])
        self.assertTrue(stanc_opts['use-opencl'])
        self.assertTrue(stanc_opts['name'] == 'foo')

        cpp_opts = model.cpp_options
        self.assertEqual(cpp_opts['STAN_OPENCL'], 'TRUE')

        with self.assertRaises(ValueError):
            bad_opts = {'X': True}
            model = CmdStanModel(stan_file=BERN_STAN,
                                 compile=False,
                                 stanc_options=bad_opts)
        with self.assertRaises(ValueError):
            bad_opts = {'include_paths': True}
            model = CmdStanModel(stan_file=BERN_STAN,
                                 compile=False,
                                 stanc_options=bad_opts)
        with self.assertRaises(ValueError):
            bad_opts = {'include_paths': 'lkjdf'}
            model = CmdStanModel(stan_file=BERN_STAN,
                                 compile=False,
                                 stanc_options=bad_opts)
示例#13
0
 def test_model_bad(self):
     with self.assertRaises(ValueError):
         CmdStanModel(stan_file=None, exe_file=None)
     with self.assertRaises(ValueError):
         CmdStanModel(model_name='bad')
     with self.assertRaises(ValueError):
         CmdStanModel(model_name='', stan_file=BERN_STAN)
     with self.assertRaises(ValueError):
         CmdStanModel(model_name='   ', stan_file=BERN_STAN)
示例#14
0
    def test_model_bad(self):
        with self.assertRaises(Exception):
            model = CmdStanModel(stan_file='xdlfkjx', exe_file='sdfndjsds')

        stan = os.path.join(datafiles_path, 'b')
        with self.assertRaises(Exception):
            model = CmdStanModel(stan_file=stan)

        stan = os.path.join(datafiles_path, 'bad_syntax.stan')
        with self.assertRaises(Exception):
            model = CmdStanModel(stan_file=stan)
示例#15
0
    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)
示例#16
0
    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_from_mcmc_sample_draws(self):
        stan = os.path.join(DATAFILES_PATH, 'bernoulli.stan')
        bern_model = CmdStanModel(stan_file=stan)
        jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')
        bern_fit = bern_model.sample(
            data=jdata,
            chains=4,
            parallel_chains=2,
            seed=12345,
            iter_sampling=100,
        )
        stan = os.path.join(DATAFILES_PATH, 'bernoulli_ppc.stan')
        model = CmdStanModel(stan_file=stan)

        bern_gqs = model.generate_quantities(data=jdata, mcmc_sample=bern_fit)

        self.assertEqual(bern_gqs.draws_pd().shape, (400, 10))
        self.assertEqual(
            bern_gqs.draws_pd(inc_sample=True).shape[1],
            bern_gqs.mcmc_sample.draws_pd().shape[1]
            + bern_gqs.draws_pd().shape[1],
        )
        row1_sample_pd = bern_fit.draws_pd().iloc[0]
        row1_gqs_pd = bern_gqs.draws_pd().iloc[0]
        self.assertTrue(
            np.array_equal(
                pd.concat((row1_sample_pd, row1_gqs_pd), axis=0).values,
                bern_gqs.draws_pd(inc_sample=True).iloc[0].values,
            )
        )
        # draws_xr
        xr_data = bern_gqs.draws_xr()
        self.assertEqual(xr_data.y_rep.dims, ('chain', 'draw', 'y_rep_dim_0'))
        self.assertEqual(xr_data.y_rep.values.shape, (4, 100, 10))

        xr_var = bern_gqs.draws_xr(vars='y_rep')
        self.assertEqual(xr_var.y_rep.dims, ('chain', 'draw', 'y_rep_dim_0'))
        self.assertEqual(xr_var.y_rep.values.shape, (4, 100, 10))

        xr_var = bern_gqs.draws_xr(vars=['y_rep'])
        self.assertEqual(xr_var.y_rep.dims, ('chain', 'draw', 'y_rep_dim_0'))
        self.assertEqual(xr_var.y_rep.values.shape, (4, 100, 10))

        xr_data_plus = bern_gqs.draws_xr(inc_sample=True)
        self.assertEqual(
            xr_data_plus.y_rep.dims, ('chain', 'draw', 'y_rep_dim_0')
        )
        self.assertEqual(xr_data_plus.y_rep.values.shape, (4, 100, 10))
        self.assertEqual(xr_data_plus.theta.dims, ('chain', 'draw'))
        self.assertEqual(xr_data_plus.theta.values.shape, (4, 100))
示例#18
0
    def test_exe_only(self):
        stan = os.path.join(DATAFILES_PATH, 'bernoulli.stan')
        bern_model = CmdStanModel(stan_file=stan)
        exe_only = os.path.join(DATAFILES_PATH, 'exe_only')
        shutil.copyfile(bern_model.exe_file, exe_only)
        os.chmod(exe_only, 0o755)

        bern2_model = CmdStanModel(exe_file=exe_only)
        jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')
        mle = bern2_model.optimize(data=jdata)
        self.assertEqual(mle.optimized_params_np[0],
                         mle.optimized_params_dict['lp__'])
        self.assertEqual(mle.optimized_params_np[1],
                         mle.optimized_params_dict['theta'])
示例#19
0
    def test_gen_quanties_mcmc_sample(self):
        stan = os.path.join(DATAFILES_PATH, 'bernoulli.stan')
        bern_model = CmdStanModel(stan_file=stan)

        jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')
        bern_fit = bern_model.sample(
            data=jdata,
            chains=4,
            parallel_chains=2,
            seed=12345,
            iter_sampling=100,
        )

        stan = os.path.join(DATAFILES_PATH, 'bernoulli_ppc.stan')
        model = CmdStanModel(stan_file=stan)

        bern_gqs = model.generate_quantities(data=jdata, mcmc_sample=bern_fit)
        self.assertEqual(
            bern_gqs.runset._args.method, Method.GENERATE_QUANTITIES
        )
        self.assertIn('CmdStanGQ: model=bernoulli_ppc', bern_gqs.__repr__())
        self.assertIn('method=generate_quantities', bern_gqs.__repr__())

        # check results - ouput files, quantities of interest, draws
        self.assertEqual(bern_gqs.runset.chains, 4)
        for i in range(bern_gqs.runset.chains):
            self.assertEqual(bern_gqs.runset._retcode(i), 0)
            csv_file = bern_gqs.runset.csv_files[i]
            self.assertTrue(os.path.exists(csv_file))
        column_names = [
            'y_rep[1]',
            'y_rep[2]',
            'y_rep[3]',
            'y_rep[4]',
            'y_rep[5]',
            'y_rep[6]',
            'y_rep[7]',
            'y_rep[8]',
            'y_rep[9]',
            'y_rep[10]',
        ]
        self.assertEqual(bern_gqs.column_names, tuple(column_names))
        self.assertEqual(bern_fit.draws_pd().shape, bern_gqs.mcmc_sample.shape)
        self.assertEqual(
            bern_gqs.sample_plus_quantities.shape[1],
            bern_gqs.mcmc_sample.shape[1]
            + bern_gqs.generated_quantities_pd.shape[1],
        )
示例#20
0
 def test_model_includes_explicit(self):
     if os.path.exists(BERN_EXE):
         os.remove(BERN_EXE)
     model = CmdStanModel(stan_file=BERN_STAN,
                          stanc_options={'include_paths': DATAFILES_PATH})
     self.assertEqual(BERN_STAN, model.stan_file)
     self.assertTrue(model.exe_file.endswith(BERN_EXE.replace('\\', '/')))
示例#21
0
    def test_optimize_good(self):
        stan = os.path.join(DATAFILES_PATH, 'bernoulli.stan')
        model = CmdStanModel(stan_file=stan)
        jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')
        jinit = os.path.join(DATAFILES_PATH, 'bernoulli.init.json')
        mle = model.optimize(
            data=jdata,
            seed=1239812093,
            inits=jinit,
            algorithm='BFGS',
            init_alpha=0.001,
            iter=100,
        )

        # test numpy output
        self.assertTrue(isinstance(mle.optimized_params_np, np.ndarray))
        self.assertAlmostEqual(mle.optimized_params_np[0], -5, places=2)
        self.assertAlmostEqual(mle.optimized_params_np[1], 0.2, places=3)

        # test pandas output
        self.assertEqual(mle.optimized_params_np[0],
                         mle.optimized_params_pd['lp__'][0])
        self.assertEqual(mle.optimized_params_np[1],
                         mle.optimized_params_pd['theta'][0])

        # test dict output
        self.assertEqual(mle.optimized_params_np[0],
                         mle.optimized_params_dict['lp__'])
        self.assertEqual(mle.optimized_params_np[1],
                         mle.optimized_params_dict['theta'])
示例#22
0
    def test_eight_schools(self):
        stan = os.path.join(DATAFILES_PATH, 'optimize', 'eight_schools.stan')
        rdata = os.path.join(DATAFILES_PATH, 'optimize',
                             'eight_schools.data.R')
        model = CmdStanModel(stan_file=stan)
        with self.assertRaises(RuntimeError):
            model.optimize(data=rdata, algorithm='LBFGS')

        mle = model.optimize(data=rdata,
                             algorithm='LBFGS',
                             require_converged=False)
        self.assertIn('CmdStanMLE: model=eight_schools', mle.__repr__())
        self.assertIn('method=optimize', mle.__repr__())
        self.assertFalse(mle.converged)
        with LogCapture() as log:
            self.assertEqual(mle.optimized_params_pd.shape, (1, 11))
        log.check_present((
            'cmdstanpy',
            'WARNING',
            'Invalid estimate, optimization failed to converge.',
        ))
        with LogCapture() as log:
            mle.stan_variable('tau')
        log.check_present((
            'cmdstanpy',
            'WARNING',
            'Invalid estimate, optimization failed to converge.',
        ))
示例#23
0
 def test_variable_bern(self):
     stan = os.path.join(DATAFILES_PATH, 'bernoulli.stan')
     jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')
     bern_model = CmdStanModel(stan_file=stan)
     bern_mle = bern_model.optimize(
         data=jdata,
         seed=1239812093,
         algorithm='LBFGS',
         init_alpha=0.001,
         iter=100,
         tol_obj=1e-12,
         tol_rel_obj=1e4,
         tol_grad=1e-8,
         tol_rel_grad=1e7,
         tol_param=1e-8,
         history_size=5,
     )
     self.assertEqual(1, len(bern_mle.metadata.stan_vars_dims))
     self.assertTrue('theta' in bern_mle.metadata.stan_vars_dims)
     self.assertEqual(bern_mle.metadata.stan_vars_dims['theta'], ())
     theta = bern_mle.stan_variable(var='theta')
     self.assertTrue(isinstance(theta, float))
     with self.assertRaises(ValueError):
         bern_mle.stan_variable(var='eta')
     with self.assertRaises(ValueError):
         bern_mle.stan_variable(var='lp__')
示例#24
0
 def test_instantiate(self):
     stan = os.path.join(DATAFILES_PATH, 'variational',
                         'eta_should_be_big.stan')
     model = CmdStanModel(stan_file=stan)
     no_data = {}
     args = VariationalArgs(algorithm='meanfield')
     cmdstan_args = CmdStanArgs(
         model_name=model.name,
         model_exe=model.exe_file,
         chain_ids=None,
         data=no_data,
         method_args=args,
     )
     runset = RunSet(args=cmdstan_args, chains=1)
     runset._csv_files = [
         os.path.join(DATAFILES_PATH, 'variational', 'eta_big_output.csv')
     ]
     variational = CmdStanVB(runset)
     self.assertIn('CmdStanVB: model=eta_should_be_big',
                   variational.__repr__())
     self.assertIn('method=variational', variational.__repr__())
     self.assertEqual(
         variational.column_names,
         ('lp__', 'log_p__', 'log_g__', 'mu[1]', 'mu[2]'),
     )
     self.assertAlmostEqual(variational.variational_params_dict['mu[1]'],
                            31.0299,
                            places=2)
     self.assertAlmostEqual(variational.variational_params_dict['mu[2]'],
                            28.8141,
                            places=2)
     self.assertEqual(variational.variational_sample.shape, (1000, 5))
示例#25
0
    def test_set_mle_attrs(self):
        stan = os.path.join(datafiles_path, 'optimize', 'rosenbrock.stan')
        model = CmdStanModel(stan_file=stan)
        no_data = {}
        args = OptimizeArgs(algorithm='Newton')
        cmdstan_args = CmdStanArgs(
            model_name=model.name,
            model_exe=model.exe_file,
            chain_ids=None,
            data=no_data,
            method_args=args,
        )
        runset = RunSet(args=cmdstan_args, chains=1)
        mle = CmdStanMLE(runset)
        self.assertIn('CmdStanMLE: model=rosenbrock', mle.__repr__())
        self.assertIn('method=optimize', mle.__repr__())

        self.assertEqual(mle._column_names, ())
        self.assertEqual(mle._mle, {})

        output = os.path.join(datafiles_path, 'optimize', 'rosenbrock_mle.csv')
        mle._set_mle_attrs(output)
        self.assertEqual(mle.column_names, ('lp__', 'x', 'y'))
        self.assertAlmostEqual(mle.optimized_params_dict['x'], 1, places=3)
        self.assertAlmostEqual(mle.optimized_params_dict['y'], 1, places=3)
示例#26
0
 def test_model_includes_implicit(self):
     stan = os.path.join(DATAFILES_PATH, 'bernoulli_include.stan')
     exe = os.path.join(DATAFILES_PATH, 'bernoulli_include' + EXTENSION)
     if os.path.exists(exe):
         os.remove(exe)
     model2 = CmdStanModel(stan_file=stan)
     self.assertTrue(model2.exe_file.endswith(exe.replace('\\', '/')))
示例#27
0
    def test_read_progress(self):
        model = CmdStanModel(stan_file=BERN_STAN, compile=False)

        proc_mock = Mock()
        proc_mock.poll.side_effect = [None, None, 'finish']
        stan_output1 = 'Iteration: 12100 / 31000 [ 39%]  (Warmup)'
        stan_output2 = 'Iteration: 14000 / 31000 [ 45%]  (Warmup)'
        pbar = tqdm.tqdm(desc='Chain 1 - warmup', position=1, total=1)

        proc_mock.stdout.readline.side_effect = [
            stan_output1.encode('utf-8'),
            stan_output2.encode('utf-8'),
        ]

        with LogCapture() as log:
            result = model._read_progress(proc=proc_mock, pbar=pbar, idx=0)
            self.assertEqual([], log.actual())
            self.assertEqual(31000, pbar.total)

            # Expect progress bar output to be something like this:
            # 'Chain 1 -   done:  45%|████▌     | 14000/31000'
            # --------

            self.assertIn('Chain 1 -   done:  45%', str(pbar))
            self.assertIn('14000/31000', str(pbar))

            # Check Stan's output is returned
            output = result.decode('utf-8')
            self.assertIn(stan_output1, output)
            self.assertIn(stan_output2, output)
示例#28
0
    def test_bernoulli_bad(self):
        stan = os.path.join(DATAFILES_PATH, 'bernoulli.stan')
        bern_model = CmdStanModel(stan_file=stan)

        with self.assertRaisesRegex(RuntimeError, 'variable does not exist'):
            bern_model.sample(chains=2, cores=2, seed=12345, iter_sampling=100)

        with self.assertRaisesRegex(RuntimeError, 'variable does not exist'):
            bern_model.sample(
                data={'foo': 1},
                chains=2,
                cores=2,
                seed=12345,
                iter_sampling=100,
            )
        if platform.system() != 'Windows':
            jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')
            dirname1 = 'tmp1' + str(time())
            os.mkdir(dirname1, mode=644)
            dirname2 = 'tmp2' + str(time())
            path = os.path.join(dirname1, dirname2)
            with self.assertRaisesRegex(ValueError,
                                        'invalid path for output files'):
                bern_model.sample(data=jdata, chains=1, output_dir=path)
            os.rmdir(dirname1)
示例#29
0
    def test_multi_proc(self):
        logistic_stan = os.path.join(DATAFILES_PATH, 'logistic.stan')
        logistic_model = CmdStanModel(stan_file=logistic_stan)
        logistic_data = os.path.join(DATAFILES_PATH, 'logistic.data.R')

        with LogCapture() as log:
            logging.getLogger()
            logistic_model.sample(data=logistic_data, chains=4, cores=1)
        log.check_present(
            ('cmdstanpy', 'INFO', 'finish chain 1'),
            ('cmdstanpy', 'INFO', 'start chain 2'),
        )
        with LogCapture() as log:
            logging.getLogger()
            logistic_model.sample(data=logistic_data, chains=4, cores=2)
        if cpu_count() >= 4:
            # finish chains 1, 2 before starting chains 3, 4
            log.check_present(
                ('cmdstanpy', 'INFO', 'finish chain 1'),
                ('cmdstanpy', 'INFO', 'start chain 4'),
            )
        if cpu_count() >= 4:
            with LogCapture() as log:
                logging.getLogger()
                logistic_model.sample(data=logistic_data, chains=4, cores=4)
                log.check_present(
                    ('cmdstanpy', 'INFO', 'start chain 4'),
                    ('cmdstanpy', 'INFO', 'finish chain 1'),
                )
示例#30
0
    def test_dont_save_warmup(self):
        stan = os.path.join(DATAFILES_PATH, 'bernoulli.stan')
        jdata = os.path.join(DATAFILES_PATH, 'bernoulli.data.json')

        bern_model = CmdStanModel(stan_file=stan)
        bern_fit = bern_model.sample(
            data=jdata,
            chains=2,
            seed=12345,
            iter_warmup=200,
            iter_sampling=100,
            save_warmup=False,
        )
        self.assertEqual(bern_fit.column_names, tuple(BERNOULLI_COLS))
        self.assertEqual(bern_fit.num_draws, 100)
        self.assertEqual(bern_fit.draws().shape, (100, 2, len(BERNOULLI_COLS)))
        with LogCapture() as log:
            self.assertEqual(
                bern_fit.draws(inc_warmup=True).shape,
                (100, 2, len(BERNOULLI_COLS)),
            )
        log.check_present((
            'cmdstanpy',
            'WARNING',
            'draws from warmup iterations not available,'
            ' must run sampler with "save_warmup=True".',
        ))