Skip to content

Commit

Permalink
Add image-rendering md cells instead of displaying images from python…
Browse files Browse the repository at this point in the history
…-cell output (#268)

* add image-rendering cells
* add image paths
* dont show plots on saving
  • Loading branch information
avivajpeyi authored Aug 4, 2023
1 parent 857f31a commit dfded31
Show file tree
Hide file tree
Showing 17 changed files with 339 additions and 178 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ theano_cache
*.txt
src/tess_atlas/notebook_controllers/templates/*.ipybn
src/tess_atlas/notebook_controllers/templates/*.md
/src/tess_atlas/notebook_controllers/templates/toi_125_files/
2 changes: 1 addition & 1 deletion paper/figures/make_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ def plot_toi_phase_subplots(tic_entry, planet_transit_model):
n = tic_entry.planet_count
fig, axes = plt.subplots(n,1, figsize=(6.5, 3*n), sharex=True)
kwgs.update(dict(
save=False, legend=2, annotate_with_period=False, default_fs=20, legend_fs = 16
legend=2, annotate_with_period=False, default_fs=20, legend_fs = 16
))
for i in range(tic_entry.planet_count):
ax = axes[i]
Expand Down
2 changes: 1 addition & 1 deletion src/tess_atlas/analysis/model_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
logger = logging.getLogger(LOGGER_NAME)


def sample_prior(model: Model, size: Optional[int] = 10000):
def sample_prior(model: Model, size: Optional[int] = 50000):
varnames = get_untransformed_varnames(model)
try:
samples = draw_values([model[v] for v in varnames], size=size)
Expand Down
2 changes: 1 addition & 1 deletion src/tess_atlas/data/inference_data_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def get_optimized_init_params(
stellar_params,
period_params,
theta=None,
verbose=False,
verbose=True,
return_all=False,
quick=False,
):
Expand Down
3 changes: 3 additions & 0 deletions src/tess_atlas/data/tic_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ def save_data(
self.optimized_params.save_data(self.outdir)
logger.info(f"Saved data in {self.outdir}")

def __repr__(self):
return f"<TICEntry[{self.toi_number}]>"


def _get_tic_id_from_table(tic_data: pd.DataFrame) -> int:
return int(tic_data[TIC_ID].iloc[0])
145 changes: 109 additions & 36 deletions src/tess_atlas/notebook_controllers/templates/toi_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@
# extension: .py
# format_name: light
# format_version: '1.5'
# jupytext_version: 1.14.5
# jupytext_version: 1.14.7
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---

# + tags=["exe", "hide-cell"]
try:
from tess_atlas.utils import notebook_initalisations
except ModuleNotFoundError:
# !pip install git+https://github.com/dfm/tess-atlas.git@main
from tess_atlas.utils import notebook_initalisations
# ! pip install tess-atlas -q

from tess_atlas.utils import notebook_initalisations

notebook_initalisations()

Expand Down Expand Up @@ -63,7 +61,7 @@
#
# To get going, we'll add some _magic_, import some packages, and run some setup steps.

# + pycharm={"name": "#%%\n"} tags=["def", "hide-cell"]
# + pycharm={"name": "#%%\n"} tags=["def", "hide-cell", "remove-output"]
# %load_ext autoreload
# %load_ext memory_profiler
# %load_ext autotime
Expand Down Expand Up @@ -102,16 +100,16 @@
plot_raw_lightcurve,
)

# + pycharm={"name": "#%%\n"} tags=["exe", "hide-cell"]

TOI_NUMBER = {{{TOINUMBER}}}
# TOI_NUMBER = {{{TOINUMBER}}}
TOI_NUMBER = 103
logger = get_notebook_logger(outdir=f"toi_{TOI_NUMBER}_files")

# + tags=["exe", "hide-cell"]
import theano

from tess_atlas.utils import tabulate_global_environ_vars

logger.info("Logging some settings for future reference")
logger.info("GLOBAL ENVS:\n" + tabulate_global_environ_vars())
logger.info(f"THEANO Config:\n{theano.config}")

Expand Down Expand Up @@ -150,11 +148,22 @@
# Plot of the lightcurve:


# + pycharm={"name": "#%%\n"} tags=["exe"]
plot_lightcurve(tic_entry)
# + tags=["exe", "hide-cell"]
plot_raw_lightcurve(tic_entry)
plot_raw_lightcurve(tic_entry, zoom_in=True)
# + pycharm={"name": "#%%\n"} tags=["exe", "remove-output"]
plot_lightcurve(tic_entry, save=True)

# Some diagnostics
plot_raw_lightcurve(tic_entry, save=True)
plot_raw_lightcurve(tic_entry, zoom_in=True, save=True)
# -
# ![](toi_{{{TOINUMBER}}}_files/flux_vs_time.png)

# + [markdown] tags=["hide-cell"]
# Diagnostic plots of the raw lightcurve (not applying sigma clipping/other cleaning methods to remove outliers). Some things to consider:
# - Do the initial fits from ExoFOP match the transits if visible?
# - If this is marked as a single-transit event, is there only 1 transit visible?
#
# ![](toi_{{{TOINUMBER}}}_files/diagnostic_raw_lc_plot.png)
# ![](toi_{{{TOINUMBER}}}_files/diagnostic_raw_lc_plot_zoom.png)
# -

# ## Fitting transit parameters
Expand Down Expand Up @@ -209,18 +218,23 @@
# ### Optimizing the initial point for sampling
# We help out the sampler we try to find an optimized set of initial parameters to begin sampling from.

# + tags=["exe"]
# + tags=["exe", "remove-output"]
if tic_entry.optimized_params is None:
init_params = get_optimized_init_params(planet_transit_model, **params)
tic_entry.save_data(optimized_params=init_params)
else:
init_params = tic_entry.optimized_params.to_dict()

# + tags=["exe"]
# sanity check that none of the right hand column have nans!
test_model(planet_transit_model, init_params, show_summary=True)
# -

# Below are plots of our initial model and priors.
#
# ### Initial model fit

# + pycharm={"name": "#%%\n"} tags=["exe"]
# + pycharm={"name": "#%%\n"} tags=["exe", "remove-output"]
initial_lc_models = (
compute_variable(
model=planet_transit_model,
Expand All @@ -229,23 +243,55 @@
)
* 1e3
)
plot_lightcurve(tic_entry, initial_lc_models)
plot_lightcurve(
tic_entry, initial_lc_models, save="lightcurve_with_initial_guess.png"
)
plot_lightcurve(
tic_entry,
initial_lc_models,
zoom_in=True,
save="lightcurve_with_initial_guess_zoom.png",
)
# -

# + tags=["exe"]
# <!-- Show LC plot with initial guess -->
# ![](toi_{{{TOINUMBER}}}_files/lightcurve_with_initial_guess.png)

# + [markdown] tags=["hide-cell"]
# <!-- Show zoomed in LC plot with initial guess -->
# ![](toi_{{{TOINUMBER}}}_files/lightcurve_with_initial_guess_zoom.png)

# + tags=["exe", "remove-output"]
params = dict(
tic_entry=tic_entry, model=planet_transit_model, initial_params=init_params
)
plot_phase(**params, thumbnail=True)
plot_phase(**params, plot_all_datapoints=True)
plot_phase(**params, save="phase_initial.png")
plot_phase(
**params, plot_all_datapoints=True, save="phase_initial_all_datapoints.png"
)
# -

# + tags=["exe"]
# <!-- SHOW PHASE PLOT -->
# <img src="toi_{{{TOINUMBER}}}_files/phase_initial.png" style="width:450px;"/>
#

# + [markdown] tags=["hide-cell"]
# Diagnostic phase plot
# <img src="toi_{{{TOINUMBER}}}_files/phase_initial_all_datapoints.png" style="width:450px;"/>
# -

# ### Histograms of Priors

# + tags=["exe", "remove-output"]
prior_samples = sample_prior(planet_transit_model)
if prior_samples:
plot_priors(tic_entry, prior_samples, init_params)
plot_priors(tic_entry, prior_samples, init_params, save=True)


# -

# ![](toi_{{{TOINUMBER}}}_files/priors.png)

#
# ### Sampling
# With the model and priors defined, we can begin sampling.
Expand All @@ -255,7 +301,7 @@
def run_inference(model) -> InferenceData:
np.random.seed(TOI_NUMBER)
with model:
sampling_kwargs = dict(tune=2000, draws=2000, chains=2, cores=2)
sampling_kwargs = dict(tune=200, draws=200, chains=1, cores=1)
logger.info(f"Run sampler with kwargs: {sampling_kwargs}")
inference_data = pmx.sample(
**sampling_kwargs, start=init_params, return_inferencedata=True
Expand All @@ -280,8 +326,11 @@ def run_inference(model) -> InferenceData:

# + pycharm={"name": "#%%\n"} tags=["exe"]
summary(inference_data)
# + tags=["exe", "hide-cell"]
plot_inference_trace(tic_entry)
# + tags=["exe", "remove-output"]
plot_inference_trace(tic_entry, save=True)

# + [markdown] tags=["hide-cell"]
# ![](toi_{{{TOINUMBER}}}_files/diagnostic_trace_plot.png)
# -

# ## Results
Expand All @@ -290,18 +339,28 @@ def run_inference(model) -> InferenceData:
# ### Posterior plots
# Below are plots of the posterior probability distributions and the best-fitting light-curve model.

# + pycharm={"name": "#%%\n"} tags=["exe"]
plot_posteriors(tic_entry, inference_data, initial_params=init_params)
# + pycharm={"name": "#%%\n"} tags=["exe"]
# + pycharm={"name": "#%%\n"} tags=["exe", "remove-output"]
plot_posteriors(
tic_entry, inference_data, initial_params=init_params, save=True
)
# -
# <!-- SHOW POSTERIOR PLOT -->
# ![](toi_{{{TOINUMBER}}}_files/posteriors.png)

# + pycharm={"name": "#%%\n"} tags=["exe", "remove-output"]
# %%memit
plot_phase(
tic_entry,
planet_transit_model,
inference_data,
initial_params=init_params,
save=True,
)
# -

# <!-- SHOW PHASE PLOT -->
# <img src="toi_{{{TOINUMBER}}}_files/phase_plot.png" style="width:450px;"/>

# ### Eccentricity post-processing
#
# As discussed above, we fit this model assuming a circular orbit which speeds things up for a few reasons:
Expand All @@ -323,24 +382,38 @@ def run_inference(model) -> InferenceData:
star = tic_entry.stellar_data
star

# + pycharm={"name": "#%%\n"} tags=["exe"]
# + tags=["remove-input"]
if star.density_data_present:
ecc_samples = calculate_eccentricity_weights(tic_entry, inference_data)
ecc_samples.to_csv(
os.path.join(tic_entry.outdir, "eccentricity_samples.csv"), index=False
logger.info(
"Stellar data present for TIC. Continuing with eccentricity calculations."
)
plot_eccentricity_posteriors(tic_entry, ecc_samples)
else:
logger.info(
"Stellar data not present for TIC. Skipping eccentricity calculations."
)

# + pycharm={"name": "#%%\n"} tags=["exe", "remove-output"]
if star.density_data_present:
ecc_samples = calculate_eccentricity_weights(tic_entry, inference_data)
ecc_samples.to_csv(
os.path.join(tic_entry.outdir, "eccentricity_samples.csv"), index=False
)
plot_eccentricity_posteriors(tic_entry, ecc_samples, save=True)
# -
# <!-- SHOW ECC POSTERIORS -->
# ![](toi_{{{TOINUMBER}}}_files/eccentricity_posteriors.png)

# ### Diagnostics
# Finally, we also generate some diagnostic plots.

# + pycharm={"name": "#%%\n"} tags=["exe", "hide-cell"]
plot_diagnostics(tic_entry, planet_transit_model, init_params)
# + pycharm={"name": "#%%\n"} tags=["exe", "remove-output"]
plot_diagnostics(tic_entry, planet_transit_model, init_params, save=True)
# + [markdown] tags=["hide-cell"]
# <!-- SHOW DIAGNOSTICS -->
#
# ![](toi_{{{TOINUMBER}}}_files/diagnostic_flux_vs_time_zoom.png)
# -

# ## Citations
#
# We hope this has been helpful! The TESS-Atlas was built using exoplanet, PyMC3, lightkurve, starry, celerite2, ExoFOP, and Sphinx.
Expand Down
Loading

0 comments on commit dfded31

Please sign in to comment.