Пример #1
0
    def __init__(self, objective, chain, folder=None, parent=None):
        QtWidgets.QDialog.__init__(self, parent)
        self.setupUi(self)

        self.objective = objective
        self.folder = folder
        self.chain = chain

        if folder is None:
            self.folder = os.getcwd()

        if self.chain is None:
            model_file_name, ok = QtWidgets.QFileDialog.getOpenFileName(
                self, "Select chain file"
            )
            if not ok:
                return
            self.folder = os.path.dirname(model_file_name)
            try:
                self.chain = load_chain(model_file_name)
            except Exception as e:
                # a chain load will go wrong quite often I'd expect
                self.chain = None
                print(repr(e))
                return

        self.chain = chain

        if len(self.chain.shape) == 3:
            steps, walkers, varys = self.chain.shape
            self.chain_size.setText(
                "steps: {}, walkers: {}, varys: {}".format(
                    steps, walkers, varys
                )
            )
        else:
            steps, temps, walkers, varys = self.chain.shape
            self.chain_size.setText(
                "steps: {}, temps: {}, walkers: {}, varys: {}".format(
                    steps, temps, walkers, varys
                )
            )

        self.total_samples.setText("Total samples: {}".format(steps * walkers))

        self.burn.setMaximum(steps - 1)
        self.thin.setMaximum(steps - 1)

        acfs = autocorrelation_chain(self.chain)
        time = integrated_time(acfs, tol=1, quiet=True)
        self.autocorrelation_time.setText(
            "Estimated Autocorrelation Time: {}".format(time)
        )
Пример #2
0
    def on_buttonBox_accepted(self):
        nthin = self.thin.value()
        nburn = self.burn.value()

        process_chain(self.objective, self.chain, nburn=nburn, nthin=nthin)

        # plot the Autocorrelation function of the chain
        acfs = autocorrelation_chain(self.chain, nburn=nburn, nthin=nthin)

        fig = Figure()
        FigureCanvas(fig)

        ax = fig.add_subplot(111)
        ax.plot(acfs)
        ax.set_ylabel('autocorrelation')
        ax.set_xlabel('step')
        fig.savefig(os.path.join(self.folder, 'steps-autocorrelation.png'))
Пример #3
0
    def recalculate(self):
        nthin = self.thin.value()
        nburn = self.burn.value()

        lchain = self.chain[nburn::nthin]

        if len(lchain.shape) == 3:
            steps, walkers, varys = lchain.shape
        else:
            steps, temps, walkers, varys = lchain.shape

        self.total_samples.setText('Total samples: {}'.format(steps * walkers))

        acfs = autocorrelation_chain(lchain)
        time = integrated_time(acfs, tol=1, quiet=True)
        self.autocorrelation_time.setText(
            'Estimated Autocorrelation Time: {}'.format(time))
        self.nplot.setMaximum(steps * walkers)
Пример #4
0
def _process_chain(objective, chain, nburn, nthin, folder=None):
    # processes the chain for the ProcessMCMCDialog
    if folder is None:
        folder = os.getcwd()

    process_chain(objective, chain, nburn=nburn, nthin=nthin)

    # plot the Autocorrelation function of the chain
    acfs = autocorrelation_chain(chain, nburn=nburn, nthin=nthin)

    fig = Figure()
    FigureCanvas(fig)

    ax = fig.add_subplot(111)
    ax.plot(acfs)
    ax.set_ylabel("autocorrelation")
    ax.set_xlabel("step")
    fig.savefig(os.path.join(folder, "steps-autocorrelation.png"))
Пример #5
0
    def test_mcmc(self):
        self.mcfitter.sample(steps=50, nthin=1, verbose=False)

        assert_equal(self.mcfitter.nvary, 2)

        # smoke test for corner plot
        self.mcfitter.objective.corner()

        # we're not doing Parallel Tempering here.
        assert_(self.mcfitter._ntemps == -1)
        assert_(isinstance(self.mcfitter.sampler, emcee.EnsembleSampler))

        # should be able to multithread
        mcfitter = CurveFitter(self.objective, nwalkers=50)
        res = mcfitter.sample(steps=33, nthin=2, verbose=False, pool=2)

        # check that the autocorrelation function at least runs
        acfs = mcfitter.acf(nburn=10)
        assert_equal(acfs.shape[-1], mcfitter.nvary)

        # check the standalone autocorrelation calculator
        acfs2 = autocorrelation_chain(mcfitter.chain, nburn=10)
        assert_equal(acfs, acfs2)

        # check integrated_time
        integrated_time(acfs2, tol=5)

        # check chain shape
        assert_equal(mcfitter.chain.shape, (33, 50, 2))
        # assert_equal(mcfitter._lastpos, mcfitter.chain[:, -1, :])
        assert_equal(res[0].chain.shape, (33, 50))

        # if the number of parameters changes there should be an Exception
        # raised
        from pytest import raises

        with raises(RuntimeError):
            self.p[0].vary = False
            self.mcfitter.sample(1)

        # can fix by making the sampler again
        self.mcfitter.make_sampler()
        self.mcfitter.sample(1)