Skip to content

Commit

Permalink
Merge pull request #86 from sblunt/expand-orbit-plots
Browse files Browse the repository at this point in the history
Expand orbit plots
  • Loading branch information
Sarah Blunt authored Jan 3, 2019
2 parents 5b7d268 + e70fdf5 commit 6e8a5e7
Show file tree
Hide file tree
Showing 9 changed files with 363 additions and 144 deletions.
40 changes: 23 additions & 17 deletions docs/tutorials/MCMC_tutorial.ipynb

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions docs/tutorials/MCMC_vs_OFTI.ipynb

Large diffs are not rendered by default.

20 changes: 14 additions & 6 deletions docs/tutorials/Modifying_Priors.ipynb

Large diffs are not rendered by default.

52 changes: 29 additions & 23 deletions docs/tutorials/OFTI_tutorial.ipynb

Large diffs are not rendered by default.

286 changes: 216 additions & 70 deletions docs/tutorials/Plotting_tutorial.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion orbitize/priors.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def compute_lnprob(self, element_array):
lnprob = -0.5*np.log(2.*np.pi*self.sigma) - 0.5*((element_array - self.mu) / self.sigma)**2

if self.no_negatives:

bad_samples = np.where(element_array < 0)[0]
lnprob[bad_samples] = -np.inf

Expand Down
78 changes: 65 additions & 13 deletions orbitize/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import matplotlib.colors as colors
import corner
import orbitize.kepler as kepler
import orbitize.system
import h5py
from astropy.io import fits
from astropy.time import Time
Expand Down Expand Up @@ -40,7 +41,7 @@ class Results(object):
[parallax, total mass]
where 1 corresponds to the first orbiting object, 2 corresponds
to the second, etc. If stellar mass
to the second, etc.
Written: Henry Ngo, Sarah Blunt, 2018
"""
Expand Down Expand Up @@ -269,10 +270,12 @@ def plot_corner(self, param_list=[], **corner_kwargs):
figure = corner.corner(samples, **corner_kwargs)
return figure


def plot_orbits(self, parallax=None, total_mass=None, object_mass=0,
object_to_plot=1, start_mjd=51544.,
num_orbits_to_plot=100, num_epochs_to_plot=100,
square_plot=True, show_colorbar=True, cmap=cmap):
square_plot=True, show_colorbar=True, cmap=cmap,
sep_pa_color='lightgrey', sep_pa_end_year=2025.0):
"""
Plots one orbital period for a select number of fitted orbits
for a given object, with line segments colored according to time
Expand All @@ -297,9 +300,15 @@ def plot_orbits(self, parallax=None, total_mass=None, object_mass=0,
show_colorbar (Boolean): Displays colorbar to the right of the plot [True]
cmap (matplotlib.cm.ColorMap): color map to use for making orbit tracks
(default: modified Purples_r)
sep_pa_color (string): any valid matplotlib color string, used to set the
color of the orbit tracks in the Sep/PA panels (default: 'lightgrey').
sep_pa_end_year (float): decimal year specifying when to stop plotting orbit
tracks in the Sep/PA panels (default: 2025.0).
Return:
``matplotlib.pyplot.Figure``: the orbit plot if input is valid, None otherwise
``matplotlib.pyplot.Figure``: the orbit plot if input is valid, ``None`` otherwise
(written): Henry Ngo, Sarah Blunt, 2018
"""
Expand Down Expand Up @@ -349,6 +358,7 @@ def plot_orbits(self, parallax=None, total_mass=None, object_mass=0,
period = period.to(u.day).value
# Create an epochs array to plot num_epochs_to_plot points over one orbital period
epochs[i,:] = np.linspace(start_mjd, float(start_mjd+period[orb_ind]), num_epochs_to_plot)

# Calculate ra/dec offsets for all epochs of this orbit
raoff0, deoff0, _ = kepler.calc_orbit(
epochs[i,:], sma[orb_ind], ecc[orb_ind], inc[orb_ind], aop[orb_ind], pan[orb_ind],
Expand All @@ -367,7 +377,9 @@ def plot_orbits(self, parallax=None, total_mass=None, object_mass=0,
)

# Create figure for orbit plots
fig, ax = plt.subplots()
fig = plt.figure(figsize=(14,6))

ax = plt.subplot2grid((2, 14), (0, 0), rowspan=2, colspan=6)

# Plot each orbit (each segment between two points coloured using colormap)
for i in np.arange(num_orbits_to_plot):
Expand All @@ -379,7 +391,7 @@ def plot_orbits(self, parallax=None, total_mass=None, object_mass=0,
lc.set_array(epochs[i,:])
ax.add_collection(lc)

# Modify the axes
# modify the axes
if square_plot:
adjustable_param='datalim'
else:
Expand All @@ -390,14 +402,54 @@ def plot_orbits(self, parallax=None, total_mass=None, object_mass=0,
ax.locator_params(axis='x', nbins=6)
ax.locator_params(axis='y', nbins=6)

# Add colorbar
# add colorbar
if show_colorbar:
sm = mpl.cm.ScalarMappable(cmap=cmap, norm=norm_yr)
sm.set_array([]) # magic? (just needs to *not* be None)
cbar = fig.colorbar(sm, format='%g')
# Alternative implementation example for right-hand colorbar
# fig.subplots_adjust(right=0.8)
# cbar_ax = fig.add_axes([0.825, 0.15, 0.05, 0.7]) # xpos, ypos, width, height, in fraction of figure size
# cbar = mpl.colorbar.ColorbarBase(cbar_ax, cmap=cmap, norm=norm_yr, orientation='vertical')
cbar_ax = fig.add_axes([0.47, 0.15, 0.015, 0.7]) # xpos, ypos, width, height, in fraction of figure size
cbar = mpl.colorbar.ColorbarBase(cbar_ax, cmap=cmap, norm=norm_yr, orientation='vertical')

# plot sep/PA zoom-in panels
ax1 = plt.subplot2grid((2, 14), (0, 9), colspan=6)
ax2 = plt.subplot2grid((2, 14), (1, 9), colspan=6)
ax2.set_ylabel('PA [$^{{\\circ}}$]')
ax1.set_ylabel('$\\rho$ [mas]')
ax2.set_xlabel('Epoch')

epochs_seppa = np.zeros((num_orbits_to_plot, num_epochs_to_plot))

for i in np.arange(num_orbits_to_plot):

orb_ind = choose[i]

epochs_seppa[i,:] = np.linspace(
start_mjd,
Time(sep_pa_end_year, format='decimalyear').mjd,
num_epochs_to_plot
)

# Calculate ra/dec offsets for all epochs of this orbit
raoff0, deoff0, _ = kepler.calc_orbit(
epochs_seppa[i,:], sma[orb_ind], ecc[orb_ind], inc[orb_ind], aop[orb_ind], pan[orb_ind],
tau[orb_ind], plx[orb_ind], mtot[orb_ind], mass=mplanet[orb_ind]
)

raoff[i,:] = raoff0
deoff[i,:] = deoff0

yr_epochs = Time(epochs_seppa[i,:],format='mjd').decimalyear
plot_epochs = np.where(yr_epochs <= sep_pa_end_year)[0]
yr_epochs = yr_epochs[plot_epochs]

seps, pas = orbitize.system.radec2seppa(raoff[i,:], deoff[i,:])

plt.sca(ax1)
plt.plot(yr_epochs, seps, color=sep_pa_color)

plt.sca(ax2)
plt.plot(yr_epochs, pas, color=sep_pa_color)

ax1.locator_params(axis='x', nbins=6)
ax1.locator_params(axis='y', nbins=6)
ax2.locator_params(axis='x', nbins=6)
ax2.locator_params(axis='y', nbins=6)

return fig
2 changes: 1 addition & 1 deletion orbitize/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Sampler(ABC):
def __init__(self, system, like='chi2_lnlike'):
self.system = system

# check if likliehood fuction is a string of a function
# check if `like` is a string or a function
if callable(like):
self.lnlike = like
else:
Expand Down
1 change: 1 addition & 0 deletions tests/test_priors.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,4 @@ def test_compute_lnprob():
test_draw_samples()



0 comments on commit 6e8a5e7

Please sign in to comment.