def run(nstates, nsamples): # Load force data. from netCDF4 import Dataset ncfile = Dataset('rnase-h-d10a-trace47.nc', 'r') tau = 0.001 # 1 kHz obs_label = 'force / pN' time_units = 's' # seconds o_t = ncfile.variables['force'] # load trace # copy data o_t = o_t[:] O = [o_t] # form list of traces # Initialize MLHMM. print "Initializing MLHMM with " + str(nstates) + " states." estimator = bhmm.MLHMM(O, nstates) # Plot initial guess. plots.plot_state_assignments( estimator.hmm, None, O[0], time_units=time_units, obs_label=obs_label, tau=tau, pdf_filename='RNAseH_trace47-guess-stateassignments-nstates' + str(nstates) + '.pdf') # Fit HMM. mle = estimator.fit() # Plot. plots.plot_state_assignments( mle, mle.hidden_state_trajectories[0], o_t, time_units=time_units, obs_label=obs_label, tau=tau, pdf_filename='RNAseH_trace47-mlhmm-stateassignments-nstates' + str(nstates) + '.pdf') # Initialize BHMM, using MLHMM model as initial model. print "Initializing BHMM and running with " + str(nsamples) + " samples." sampler = bhmm.BHMM(O, nstates, initial_model=mle) # Sample models. bhmm_models = sampler.sample(nsamples=nsamples, save_hidden_state_trajectory=False) # Generate a sample saving a hidden state trajectory. final_models = sampler.sample(nsamples=1, save_hidden_state_trajectory=True) # Plot. model = final_models[0] s_t = model.hidden_state_trajectories[0] o_t = O[0] plots.plot_state_assignments( model, s_t, o_t, time_units=time_units, obs_label=obs_label, tau=tau, pdf_filename='RNAseH_trace47-bhmm-stateassignments-nstates' + str(nstates) + '.pdf') # write latex table with sample statistics conf = 0.95 sampled_hmm = bhmm.SampledGaussianHMM(mle, bhmm_models) generate_latex_table( sampled_hmm, conf=conf, dt=tau, time_unit='s', caption='Bayesian HMM parameter estimates for RNAse-H data.', outfile='rnase-h-bhmm-statistics-table.tex')
return table if __name__ == "__main__": # Set verbosity. bhmm.config.verbose = True # Make figures directory. directory = 'figures' if not os.path.exists(directory): os.makedirs(directory) # Generate data. nobservations = 100000 [true_hmm, O, S] = generate_synthetic_data(nobservations) # Analyze data. nstates = 3 [mle1, bhmm_models1] = analyze_data(O, nstates, nobservations=1000) [mle2, bhmm_models2] = analyze_data(O, nstates, nobservations=10000) [mle3, bhmm_models3] = analyze_data(O, nstates, nobservations=100000) # Write latex table with true and inferred sample statistics. conf = 0.95 # confidence interval sampled_hmm_models = [ bhmm.SampledGaussianHMM(mle1, bhmm_models1), bhmm.SampledGaussianHMM(mle2, bhmm_models2), bhmm.SampledGaussianHMM(mle3, bhmm_models3)] generate_latex_table(true_hmm, sampled_hmm_models, conf=conf, dt=1, time_unit='ms', outfile='synthetic-three-state-model-bhmm-statistics.tex')
def run(nstates, nsamples): # Create model. true_model = testsystems.force_spectroscopy_model() nstates = true_model.nstates tau = 0.001 # time interval per observation # Generate synthetic data. print "Generating synthetic data..." [O, S] = true_model.generate_synthetic_observation_trajectories( ntrajectories=1, length=50000) # DEBUG print "synthetic observation trajectories:" print O print "Total state visits, min_state, max_state:" print testsystems.total_state_visits(nstates, S) # Generate MLHMM. print "Generating MLHMM..." estimator = bhmm.MLHMM(O, nstates) print "Initial guess:" print str(estimator.hmm.output_model) print estimator.hmm.transition_matrix print estimator.hmm.stationary_distribution # Plot initial guess. s_t = None o_t = O[0] plots.plot_state_assignments( estimator.hmm, s_t, o_t, time_units='s', obs_label='force / pN', tau=tau, pdf_filename='synthetic-three-state-model-guess-nstates' + str(nstates) + '.pdf') print "Fitting HMM..." mle = estimator.fit() # Plot. s_t = mle.hidden_state_trajectories[0] import numpy as np o_t = O[0] plots.plot_state_assignments( mle, s_t, o_t, time_units='s', obs_label='force / pN', tau=tau, pdf_filename='synthetic-three-state-model-mlhmm-nstates' + str(nstates) + '.pdf') # Initialize BHMM with MLHMM model. print "Sampling models from BHMM..." sampler = bhmm.BHMM(O, nstates, initial_model=mle) bhmm_models = sampler.sample(nsamples=nsamples, save_hidden_state_trajectory=False) # Generate a sample saving a hidden state trajectory. final_models = sampler.sample(nsamples=1, save_hidden_state_trajectory=True) # Plot final BHMM sample. model = final_models[0] s_t = model.hidden_state_trajectories[0] o_t = O[0] plots.plot_state_assignments( model, s_t, o_t, time_units='s', obs_label='force / pN', tau=tau, pdf_filename='synthetic-three-state-model-bhmm-nstates' + str(nstates) + '.pdf') # write latex table with sample statistics conf = 0.95 sampled_hmm = bhmm.SampledGaussianHMM(mle, bhmm_models) generate_latex_table( sampled_hmm, conf=conf, dt=1, time_unit='step', caption= 'Bayesian HMM parameter estimates for synthetic three-state model.', outfile='synthetic-three-state-model-bhmm-statistics.tex')
def run(nstates, nsamples): # Load force data. from netCDF4 import Dataset ncfile = Dataset('fiber3-trace011.nc', 'r') tau = 0.001 # 1 kHz obs_label = 'force / pN' time_units = 's' # seconds o_t = ncfile.variables['force'] # load trace # force to make a copy because netCDF appears to cause problems nsubsample = 50 # subsampling rate o_t = o_t[::nsubsample] tau *= nsubsample # ------------------- O = [o_t] # form list of traces # Initialize MLHMM. print "Initializing MLHMM with " + str(nstates) + " states." estimator = bhmm.MLHMM(O, nstates) # Plot initial guess. plots.plot_state_assignments( estimator.hmm, None, O[0], time_units=time_units, obs_label=obs_label, tau=tau, pdf_filename='fiber3-trace11-guess-stateassignments-nstate' + str(nstates) + '.pdf') # Fit MLHMM mle = estimator.fit() # Plot. plots.plot_state_assignments( mle, mle.hidden_state_trajectories[0], o_t, time_units=time_units, obs_label=obs_label, tau=tau, pdf_filename='fiber3-trace11-mlhmm-stateassignments-nstate' + str(nstates) + '.pdf') # Initialize BHMM, using MLHMM model as initial model. print "Initializing BHMM and running with " + str(nsamples) + " samples." sampler = bhmm.BHMM(O, nstates, initial_model=mle) # Sample models. bhmm_models = sampler.sample(nsamples=nsamples, save_hidden_state_trajectory=False) # Generate a sample saving a hidden state trajectory. final_models = sampler.sample(nsamples=1, save_hidden_state_trajectory=True) # Plot. model = final_models[0] s_t = model.hidden_state_trajectories[0] o_t = O[0] plots.plot_state_assignments( model, s_t, o_t, time_units=time_units, obs_label=obs_label, tau=tau, pdf_filename='fiber3-trace11-bhmm-stateassignments-nstate' + str(nstates) + '.pdf') # write latex table with sample statistics conf = 0.95 sampled_hmm = bhmm.SampledGaussianHMM(mle, bhmm_models) generate_latex_table(sampled_hmm, conf=conf, dt=tau, time_unit='s', caption='BHMM model estimates for RNA hairpin data.', outfile='p5ab-bhmm-statistics-table' + str(nstates) + '.tex')