Skip to content

Commit

Permalink
Debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob685 committed Mar 6, 2020
1 parent 868d910 commit 81c8e94
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 36 deletions.
5 changes: 1 addition & 4 deletions orbitize/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -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
Expand All @@ -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))

Expand Down
75 changes: 43 additions & 32 deletions tests/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,94 +22,97 @@ 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
Returns results.Results object
"""
# 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
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)
# 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
has_lnlike: allows for tests with and without lnlike values
(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
Expand All @@ -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,)

Expand All @@ -135,33 +139,40 @@ 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
for all 8 parameters and for just four selected parameters
"""
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)
Expand Down

0 comments on commit 81c8e94

Please sign in to comment.