From 81c8e94244f67cb7f4dd167ed6fab97594c3c2b0 Mon Sep 17 00:00:00 2001 From: Rob685 Date: Fri, 6 Mar 2020 14:35:18 -0800 Subject: [PATCH] Debugging --- orbitize/results.py | 5 +-- tests/test_results.py | 75 +++++++++++++++++++++++++------------------ 2 files changed, 44 insertions(+), 36 deletions(-) diff --git a/orbitize/results.py b/orbitize/results.py index 826a4903..92bce8f0 100644 --- a/orbitize/results.py +++ b/orbitize/results.py @@ -19,7 +19,6 @@ import orbitize.kepler as kepler import orbitize.system - # define modified color map for default use in orbit plots cmap = mpl.cm.Purples_r cmap = colors.LinearSegmentedColormap.from_list( @@ -136,7 +135,6 @@ def load_results(self, filename, append=False): """ hf = h5py.File(filename, 'r') # Opens file for reading # Load up each dataset from hdf5 file - # pdb.set_trace() #here to see what attributes this has. sampler_name = np.str(hf.attrs['sampler_name']) post = np.array(hf.get('post')) lnlike = np.array(hf.get('lnlike')) @@ -164,7 +162,6 @@ def load_results(self, filename, append=False): elif self.sampler_name != sampler_name: raise Exception( 'Unable to append file {} to Results object. sampler_name of object and file do not match'.format(filename)) - # if no tau reference epoch is set, use input file's value if self.tau_ref_epoch is None: self.tau_ref_epoch = tau_ref_epoch @@ -174,7 +171,7 @@ def load_results(self, filename, append=False): tau_ref_epoch, self.tau_ref_epoch)) if self.labels is None: self.labels = labels - elif self.labels != labels: + elif self.labels.any() != labels.any(): raise ValueError("Loaded data has parameter labels {} while Results object has already been initialized to {}.".format( labels, self.labels)) diff --git a/tests/test_results.py b/tests/test_results.py index c6b34ee4..a8c45daf 100644 --- a/tests/test_results.py +++ b/tests/test_results.py @@ -11,6 +11,7 @@ std_labels = ['sma1', 'ecc1', 'inc1', 'aop1', 'pan1', 'tau1', 'plx', 'mtot'] + def simulate_orbit_sampling(n_sim_orbits): """ Returns posterior array with n_sim_orbit samples for testing @@ -21,32 +22,33 @@ def simulate_orbit_sampling(n_sim_orbits): sma = 9.660 sma_err = 1.1 # not real ecc = 0.08 - ecc_err = 0.03 # not real + ecc_err = 0.03 # not real inc = np.radians(88.81) inc_err = np.radians(0.12) aop = np.radians(205.8) - aop_err = np.radians(20.0) # not real + aop_err = np.radians(20.0) # not real pan = np.radians(31.76) pan_err = np.radians(0.09) epp = 0.73 - epp_err = 0.20 # not real + epp_err = 0.20 # not real system_mass = 1.80 system_mass_err = 0.04 - plx=51.44 #mas - plx_err=0.12 #mas + plx = 51.44 # mas + plx_err = 0.12 # mas # Create some simulated orbit draws - sim_post = np.zeros((n_sim_orbits,n_params)) - sim_post[:,0]=np.random.normal(sma,sma_err,n_sim_orbits) - sim_post[:,1]=np.random.normal(ecc,ecc_err,n_sim_orbits) - sim_post[:,2]=np.random.normal(inc,inc_err,n_sim_orbits) - sim_post[:,3]=np.random.normal(aop,aop_err,n_sim_orbits) - sim_post[:,4]=np.random.normal(pan,pan_err,n_sim_orbits) - sim_post[:,5]=np.random.normal(epp,epp_err,n_sim_orbits) - sim_post[:,6]=np.random.normal(plx,plx_err,n_sim_orbits) - sim_post[:,7]=np.random.normal(system_mass,system_mass_err,n_sim_orbits) + sim_post = np.zeros((n_sim_orbits, n_params)) + sim_post[:, 0] = np.random.normal(sma, sma_err, n_sim_orbits) + sim_post[:, 1] = np.random.normal(ecc, ecc_err, n_sim_orbits) + sim_post[:, 2] = np.random.normal(inc, inc_err, n_sim_orbits) + sim_post[:, 3] = np.random.normal(aop, aop_err, n_sim_orbits) + sim_post[:, 4] = np.random.normal(pan, pan_err, n_sim_orbits) + sim_post[:, 5] = np.random.normal(epp, epp_err, n_sim_orbits) + sim_post[:, 6] = np.random.normal(plx, plx_err, n_sim_orbits) + sim_post[:, 7] = np.random.normal(system_mass, system_mass_err, n_sim_orbits) return sim_post + def test_init_and_add_samples(): """ Tests object creation and add_samples() with some simulated posterior samples @@ -54,34 +56,35 @@ def test_init_and_add_samples(): """ # Create object results_obj = results.Results( - sampler_name='testing', tau_ref_epoch=50000, - labels = std_labels + sampler_name='testing', tau_ref_epoch=50000, + labels=std_labels ) # Simulate some sample draws, assign random likelihoods n_orbit_draws1 = 1000 sim_post = simulate_orbit_sampling(n_orbit_draws1) sim_lnlike = np.random.uniform(size=n_orbit_draws1) # Test adding samples - results_obj.add_samples(sim_post,sim_lnlike, labels=std_labels) + results_obj.add_samples(sim_post, sim_lnlike, labels=std_labels) # Simulate some more sample draws n_orbit_draws2 = 2000 sim_post = simulate_orbit_sampling(n_orbit_draws2) sim_lnlike = np.random.uniform(size=n_orbit_draws2) # Test adding more samples - results_obj.add_samples(sim_post,sim_lnlike, labels=std_labels) + results_obj.add_samples(sim_post, sim_lnlike, labels=std_labels) # Check shape of results.post expected_length = n_orbit_draws1 + n_orbit_draws2 - assert results_obj.post.shape == (expected_length,8) + assert results_obj.post.shape == (expected_length, 8) assert results_obj.lnlike.shape == (expected_length,) assert results_obj.tau_ref_epoch == 50000 assert results_obj.labels == std_labels return results_obj + @pytest.fixture() def results_to_test(): results_obj = results.Results( - sampler_name='testing', tau_ref_epoch=50000, + sampler_name='testing', tau_ref_epoch=50000, labels=std_labels ) # Simulate some sample draws, assign random likelihoods @@ -89,16 +92,17 @@ def results_to_test(): sim_post = simulate_orbit_sampling(n_orbit_draws1) sim_lnlike = np.random.uniform(size=n_orbit_draws1) # Test adding samples - results_obj.add_samples(sim_post,sim_lnlike, labels=std_labels) + results_obj.add_samples(sim_post, sim_lnlike, labels=std_labels) # Simulate some more sample draws n_orbit_draws2 = 2000 sim_post = simulate_orbit_sampling(n_orbit_draws2) sim_lnlike = np.random.uniform(size=n_orbit_draws2) # Test adding more samples - results_obj.add_samples(sim_post,sim_lnlike, labels=std_labels) + results_obj.add_samples(sim_post, sim_lnlike, labels=std_labels) # Return object for testing return results_obj + def test_save_and_load_results(results_to_test, has_lnlike=True): """ Tests saving and reloading of a results object @@ -106,9 +110,9 @@ def test_save_and_load_results(results_to_test, has_lnlike=True): (e.g. OFTI doesn't output lnlike) """ results_to_save = results_to_test - if not has_lnlike: # manipulate object to remove lnlike (as in OFTI) - results_to_save.lnlike=None - save_filename='test_results.h5' + if not has_lnlike: # manipulate object to remove lnlike (as in OFTI) + results_to_save.lnlike = None + save_filename = 'test_results.h5' # Save to file results_to_save.save_results(save_filename) # Create new blank results object and load from file @@ -125,7 +129,7 @@ def test_save_and_load_results(results_to_test, has_lnlike=True): original_length = results_to_save.post.shape[0] expected_length = original_length * 2 assert loaded_results.post.shape == (expected_length, 8) - assert loaded_results.labels == std_labels + assert loaded_results.labels[0].tolist() == std_labels if has_lnlike: assert loaded_results.lnlike.shape == (expected_length,) @@ -135,6 +139,7 @@ def test_save_and_load_results(results_to_test, has_lnlike=True): # Clean up: Remove save file os.remove(save_filename) + def test_plot_corner(results_to_test): """ Tests plot_corner() with plotting simulated posterior samples @@ -142,26 +147,32 @@ def test_plot_corner(results_to_test): """ Figure1 = results_to_test.plot_corner() assert Figure1 is not None - Figure2 = results_to_test.plot_corner(param_list=['sma1','ecc1','inc1','mtot']) + Figure2 = results_to_test.plot_corner(param_list=['sma1', 'ecc1', 'inc1', 'mtot']) assert Figure2 is not None return Figure1, Figure2 + def test_plot_orbits(results_to_test): """ Tests plot_orbits() with simulated posterior samples """ - Figure1 = results_to_test.plot_orbits(num_orbits_to_plot=1,square_plot=True,show_colorbar=True) + Figure1 = results_to_test.plot_orbits( + num_orbits_to_plot=1, square_plot=True, show_colorbar=True) assert Figure1 is not None - Figure2 = results_to_test.plot_orbits(num_orbits_to_plot=1,square_plot=True,show_colorbar=False) + Figure2 = results_to_test.plot_orbits( + num_orbits_to_plot=1, square_plot=True, show_colorbar=False) assert Figure2 is not None - Figure3 = results_to_test.plot_orbits(num_orbits_to_plot=1,square_plot=False,show_colorbar=True) + Figure3 = results_to_test.plot_orbits( + num_orbits_to_plot=1, square_plot=False, show_colorbar=True) assert Figure3 is not None - Figure4 = results_to_test.plot_orbits(num_orbits_to_plot=1,square_plot=False,show_colorbar=False) + Figure4 = results_to_test.plot_orbits( + num_orbits_to_plot=1, square_plot=False, show_colorbar=False) assert Figure4 is not None - Figure5 = results_to_test.plot_orbits(num_orbits_to_plot=1,square_plot=False,cbar_param='ecc') + Figure5 = results_to_test.plot_orbits(num_orbits_to_plot=1, square_plot=False, cbar_param='ecc') assert Figure5 is not None return (Figure1, Figure2, Figure3, Figure4, Figure5) + if __name__ == "__main__": test_results = test_init_and_add_samples() test_save_and_load_results(test_results, has_lnlike=True)