Esempio n. 1
0
def _rate_eval_block(iblock, start, stop, nstates, data_input, name,
                     mcbs_alpha, mcbs_nsets, mcbs_acalpha, do_correl,
                     mcbs_enable):
    # Our rate estimator is a little more complex, so we've defined a custom evaluation block for it,
    # instead of just using the block evalutors that we've imported.
    results = []
    for istate in range(nstates):
        for jstate in range(nstates):
            if istate == jstate:
                continue
            kwargs = {'istate': istate, 'jstate': jstate}
            # Why are we sending in the total population dataset, instead of a sliced one?
            # It's a requirement of our estimator; we need to pull from any given i to j state in order to properly normalize
            # and avoid i to j rate constants which are affected by a third state k.
            # That is, we need the populations for both i and j, and it's easier to just send in the entire dataset.
            dataset = {
                'dataset': data_input['dataset'][:, istate, jstate],
                'pops': data_input['pops']
            }
            ci_res = mcbs_ci_correl(
                dataset,
                estimator=sequence_macro_flux_to_rate,
                alpha=mcbs_alpha,
                n_sets=mcbs_nsets,
                autocorrel_alpha=mcbs_acalpha,
                subsample=np.mean,
                do_correl=do_correl,
                mcbs_enable=mcbs_enable,
                estimator_kwargs=kwargs,
            )
            results.append(
                (name, iblock, istate, jstate, (start, stop) + ci_res))

    return results
Esempio n. 2
0
    def calc_evol_flux(self):
        westpa.rc.pstatus(
            'Calculating cumulative evolution of flux confidence intervals every {} iteration(s)'
            .format(self.evol_step))

        for itarget, (target_label,
                      target_fluxdata) in enumerate(self.fluxdata.items()):
            fluxes = target_fluxdata['flux']
            target_group = self.target_groups[target_label]
            iter_start = target_group['n_iter'][0]
            iter_stop = target_group['n_iter'][-1]
            iter_count = iter_stop - iter_start
            n_blocks = iter_count // self.evol_step
            if iter_count % self.evol_step > 0:
                n_blocks += 1

            cis = np.empty((n_blocks, ), dtype=ci_dtype)

            for iblock in range(n_blocks):
                block_iter_stop = min(
                    iter_start + (iblock + 1) * self.evol_step, iter_stop)
                istop = min((iblock + 1) * self.evol_step,
                            len(target_fluxdata['flux']))
                fluxes = target_fluxdata['flux'][:istop]

                # avg, ci_lb, ci_ub, correl_len = mclib.mcbs_ci_correl(fluxes, np.mean, self.alpha, self.n_sets,
                #                                                     autocorrel_alpha = self.autocorrel_alpha,
                #                                                     subsample=np.mean)
                avg, ci_lb, ci_ub, sterr, correl_len = mclib.mcbs_ci_correl(
                    {'dataset': fluxes},
                    estimator=(lambda stride, dataset: np.mean(dataset)),
                    alpha=self.alpha,
                    n_sets=self.n_sets,
                    autocorrel_alpha=self.autocorrel_alpha,
                    subsample=np.mean,
                    do_correl=self.do_correl,
                    mcbs_enable=self.mcbs_enable,
                )
                cis[iblock]['iter_start'] = iter_start
                cis[iblock]['iter_stop'] = block_iter_stop
                cis[iblock]['expected'], cis[iblock]['ci_lbound'], cis[iblock][
                    'ci_ubound'] = avg, ci_lb, ci_ub
                cis[iblock]['corr_len'] = correl_len
                cis[iblock]['sterr'] = sterr

                del fluxes

            cis_ds = target_group.create_dataset('flux_evolution', data=cis)
            cis_ds.attrs['iter_step'] = self.evol_step
            cis_ds.attrs['mcbs_alpha'] = self.alpha
            cis_ds.attrs['mcbs_autocorrel_alpha'] = self.autocorrel_alpha
            cis_ds.attrs['mcbs_n_sets'] = self.n_sets
Esempio n. 3
0
def _1D_eval_block(iblock, start, stop, nstates, data_input, name, mcbs_alpha, mcbs_nsets, mcbs_acalpha, do_correl, mcbs_enable, estimator_kwargs):
    # As our reweighting estimator is a weird function, we can't use the general mclib block.
    results = []
    for istate in range(nstates):
        # A little hack to make our estimator play nice, as jstate must be there.
        # For 1D datasets (state probabilities, etc), the argument isn't used in our estimator,
        # and so any variable which has the proper type is fine.
        estimator_kwargs.update(dict(istate=istate, jstate=istate, nstates=nstates))

        dataset = { 'indices' : np.array(list(range(start-1, stop-1)), dtype=np.uint16) }

        ci_res = mcbs_ci_correl(dataset,estimator=reweight_for_c,
                                alpha=mcbs_alpha,n_sets=mcbs_nsets,autocorrel_alpha=mcbs_acalpha,
                                subsample=(lambda x: x[0]), do_correl=do_correl, mcbs_enable=mcbs_enable, estimator_kwargs=estimator_kwargs)
        results.append((name, iblock, istate, (start,stop) + ci_res))

    return results
Esempio n. 4
0
def _2D_eval_block(iblock, start, stop, nstates, data_input, name, mcbs_alpha, mcbs_nsets, mcbs_acalpha, do_correl, mcbs_enable, estimator_kwargs):
    # As our reweighting estimator is a weird function, we can't use the general mclib block.
    results = []
    for istate in range(nstates):
        for jstate in range(nstates):
            if istate == jstate:
                continue
            estimator_kwargs.update(dict(istate=istate, jstate=jstate, nstates=nstates))

            dataset = { 'indices' : np.array(list(range(start-1, stop-1)), dtype=np.uint16) }

            ci_res = mcbs_ci_correl(dataset,estimator=reweight_for_c,
                                    alpha=mcbs_alpha,n_sets=mcbs_nsets,autocorrel_alpha=mcbs_acalpha,
                                    subsample=(lambda x: x[0]), do_correl=do_correl, mcbs_enable=mcbs_enable, estimator_kwargs=estimator_kwargs)
            results.append((name, iblock, istate, jstate, (start,stop) + ci_res))

    return results
Esempio n. 5
0
    def calc_store_flux_data(self):
        westpa.rc.pstatus(
            'Calculating mean flux and confidence intervals for iterations [{},{})'
            .format(self.iter_range.iter_start, self.iter_range.iter_stop))

        fluxdata = extract_fluxes(self.iter_range.iter_start,
                                  self.iter_range.iter_stop, self.data_reader)

        # Create a group to store data in
        output_group = h5io.create_hdf5_group(self.output_h5file,
                                              'target_flux',
                                              replace=False,
                                              creating_program=self.prog)
        self.output_group = output_group
        output_group.attrs['version_code'] = self.output_format_version
        self.iter_range.record_data_iter_range(output_group)

        n_targets = len(fluxdata)
        index = np.empty((len(fluxdata), ), dtype=target_index_dtype)
        avg_fluxdata = np.empty((n_targets, ), dtype=ci_dtype)

        for itarget, (target_label,
                      target_fluxdata) in enumerate(fluxdata.items()):
            # Create group and index entry
            index[itarget]['target_label'] = str(target_label)
            target_group = output_group.create_group(
                'target_{}'.format(itarget))

            self.target_groups[target_label] = target_group

            # Store per-iteration values
            target_group['n_iter'] = target_fluxdata['n_iter']
            target_group['count'] = target_fluxdata['count']
            target_group['flux'] = target_fluxdata['flux']
            h5io.label_axes(target_group['flux'], ['n_iter'], units=['tau^-1'])

            # Calculate flux autocorrelation
            fluxes = target_fluxdata['flux']
            mean_flux = fluxes.mean()
            fmm = fluxes - mean_flux
            acorr = fftconvolve(fmm, fmm[::-1])
            acorr = acorr[len(acorr) // 2:]
            acorr /= acorr[0]
            acorr_ds = target_group.create_dataset('flux_autocorrel',
                                                   data=acorr)
            h5io.label_axes(acorr_ds, ['lag'], ['tau'])

            # Calculate overall averages and CIs
            #avg, lb_ci, ub_ci, correl_len = mclib.mcbs_ci_correl(fluxes, np.mean, self.alpha, self.n_sets,
            #                                                     autocorrel_alpha=self.autocorrel_alpha, subsample=np.mean)
            avg, lb_ci, ub_ci, sterr, correl_len = mclib.mcbs_ci_correl(
                {'dataset': fluxes},
                estimator=(lambda stride, dataset: np.mean(dataset)),
                alpha=self.alpha,
                n_sets=self.n_sets,
                autocorrel_alpha=self.autocorrel_alpha,
                subsample=np.mean,
                do_correl=self.do_correl,
                mcbs_enable=self.mcbs_enable)
            avg_fluxdata[itarget] = (self.iter_range.iter_start,
                                     self.iter_range.iter_stop, avg, lb_ci,
                                     ub_ci, sterr, correl_len)
            westpa.rc.pstatus('target {!r}:'.format(target_label))
            westpa.rc.pstatus(
                '  correlation length = {} tau'.format(correl_len))
            westpa.rc.pstatus(
                '  mean flux and CI   = {:e} ({:e},{:e}) tau^(-1)'.format(
                    avg, lb_ci, ub_ci))
            index[itarget]['mean_flux'] = avg
            index[itarget]['mean_flux_ci_lb'] = lb_ci
            index[itarget]['mean_flux_ci_ub'] = ub_ci
            index[itarget]['mean_flux_correl_len'] = correl_len

        # Write index and summary
        index_ds = output_group.create_dataset('index', data=index)
        index_ds.attrs['mcbs_alpha'] = self.alpha
        index_ds.attrs['mcbs_autocorrel_alpha'] = self.autocorrel_alpha
        index_ds.attrs['mcbs_n_sets'] = self.n_sets

        self.fluxdata = fluxdata
        self.output_h5file['avg_flux'] = avg_fluxdata
Esempio n. 6
0
k_uncorrel = mcbs_correltime(ds_uncorrel,0.05,n_sets)
k_correl = mcbs_correltime(ds_correl,0.05,n_sets)
print('  uncorrelated:    {}'.format(k_uncorrel))
print('  correlated:      {}'.format(k_correl))


print('naive MCBS:')
uncorrel_mean, uncorrel_lb, uncorrel_ub = mcbs_ci(ds_uncorrel, np.mean, 0.05, n_sets=n_sets, sort=np.msort)
correl_mean, correl_lb, correl_ub = mcbs_ci(ds_correl, np.mean, 0.05, n_sets=n_sets, sort=np.msort)
n_uncorrel_width = uncorrel_ub - uncorrel_lb
n_correl_width   = correl_ub - correl_lb
print('  uncorrelated:    {} ({},{})'.format(uncorrel_mean, uncorrel_lb,uncorrel_ub))
print('  correlated:      {} ({},{})'.format(correl_mean, correl_lb,correl_ub))
print('  width ratio c/u: {}'.format((n_correl_width/n_uncorrel_width)))

print('blocked MCBS:')
#uncorrel_mean, uncorrel_lb, uncorrel_ub = mcbs_ci(ds_uncorrel[::k_uncorrel+1], np.mean, 0.05, n_sets=1000, sort=np.msort)
#correl_mean, correl_lb, correl_ub = mcbs_ci(ds_correl[::k_correl+1], np.mean, 0.05, n_sets=1000, sort=np.msort)
uncorrel_mean, uncorrel_lb, uncorrel_ub, k_ = mcbs_ci_correl(ds_uncorrel, np.mean, 0.05, n_sets=n_sets, subsample=np.mean)
correl_mean, correl_lb, correl_ub, k_ = mcbs_ci_correl(ds_correl, np.mean, 0.05, n_sets=n_sets, subsample=np.mean)
b_uncorrel_width = uncorrel_ub - uncorrel_lb
b_correl_width   = correl_ub - correl_lb
print('  uncorrelated:    {} ({},{})'.format(uncorrel_mean, uncorrel_lb,uncorrel_ub))
print('  correlated:      {} ({},{})'.format(correl_mean, correl_lb,correl_ub))
print('  width ratio c/u: {}'.format((b_correl_width/b_uncorrel_width)))

print('width ratio blocked/naive:')
print('  uncorrelated:    {}'.format(b_uncorrel_width/n_uncorrel_width))
print('  correlated:      {}'.format(b_correl_width/n_correl_width))