Skip to content

Commit

Permalink
AV: improve plot_fit()
Browse files Browse the repository at this point in the history
- make sure posteriors are loaded in plot_fit()
- share axes in plot_fit() for easier interactive exploration of plot
  • Loading branch information
avigan committed Nov 9, 2024
1 parent 1548073 commit cae90f0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
30 changes: 17 additions & 13 deletions ForMoSA/plotting/plotting_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,9 @@ def plot_corner(self, levels_sig=[0.997, 0.95, 0.68], bins=100, quantiles=(0.16,
'''
print('ForMoSA - Corner plot')

# make sure posteriors are loaded
self._get_posteriors()


fig = corner.corner(self.posterior_to_plot[burn_in:],
weights=self.weights[burn_in:],
labels=self.posteriors_names,
Expand Down Expand Up @@ -392,6 +392,7 @@ def plot_chains(self, figsize=(7,15)):
'''
print('ForMoSA - Posteriors chains for each parameter')

# make sure posteriors are loaded
self._get_posteriors()

col = int(len(self.posterior_to_plot[0][:])/2)+int(len(self.posterior_to_plot[0][:])%2)
Expand Down Expand Up @@ -430,6 +431,7 @@ def plot_radar(self, ranges, label='', quantiles=[0.16, 0.5, 0.84]):
'''
print('ForMoSA - Radar plot')

# make sure posteriors are loaded
self._get_posteriors()

list_posteriors = []
Expand Down Expand Up @@ -467,7 +469,8 @@ def _get_spectra(self,theta,return_model=False):
planet transmission, star fluxes, systematics
- ck list(floats): list scaling factor(s)
'''
# Get the posteriors

# make sure posteriors are loaded
self._get_posteriors()

# Create a list for each spectra (obs and mod) for each observation + scaling factors
Expand Down Expand Up @@ -589,6 +592,8 @@ def get_FULL_spectra(self, theta, grid_used = 'original', wavelengths=[], N_poin
- flx_final (array): Flux array of the full model
- ck (float): Scaling factor of the full model
'''

# make sure posteriors are loaded
self._get_posteriors()

if len(wavelengths)==0:
Expand Down Expand Up @@ -674,31 +679,31 @@ def plot_fit(self, figsize=(10, 5), uncert='no', trans='no', logx='no', logy='no
- axr (object) : matplotlib axes objects, residuals
- axr2 (object) : matplotlib axes objects, right side density histogram
'''

# make sure posteriors are loaded
self._get_posteriors()

print('ForMoSA - Best fit and residuals plot')

fig = plt.figure(figsize=figsize)
fig.tight_layout()
size = (7,11)
ax = plt.subplot2grid(size, (0, 0),rowspan=5 ,colspan=10)
axr= plt.subplot2grid(size, (5, 0),rowspan=2 ,colspan=10)
axr2= plt.subplot2grid(size, (5, 10),rowspan=2 ,colspan=1)

ax = plt.subplot2grid(size, (0, 0), rowspan=5, colspan=10)
axr = plt.subplot2grid(size, (5, 0), rowspan=2, colspan=10, sharex=ax)
axr2 = plt.subplot2grid(size, (5, 10), rowspan=2, colspan=1, sharey=axr)

spectra, ck = self._get_spectra(self.theta_best)
iobs_spectro = 0
iobs_photo = 0


# Scale or not in absolute flux
if norm != 'yes':
if len(spectra[0][0]) != 0:
ck = np.full(len(spectra[0][0]), 1)
else:
ck = np.full(len(spectra[0][4]), 1)


for indobs, obs in enumerate(sorted(glob.glob(self.global_params.main_observation_path))):

if self.global_params.use_lsqr[indobs] == 'True':
# If we used the lsq function, it means that our data is contaminated by the starlight difraction
# so the model is the sum of the planet model + the estimated stellar contribution
Expand All @@ -722,8 +727,6 @@ def plot_fit(self, figsize=(10, 5), uncert='no', trans='no', logx='no', logy='no
ax.plot(spectra[indobs][0], star_flx, c='b')
ax.plot(spectra[indobs][0], model, c='r')



residuals = spectra[indobs][3] - spectra[indobs][1]
sigma_res = np.nanstd(residuals) # Replace np.std by np.nanstd if nans are in the array to ignore them
axr.plot(spectra[indobs][0], residuals/sigma_res, c=self.color_out, alpha=0.8)
Expand All @@ -742,7 +745,6 @@ def plot_fit(self, figsize=(10, 5), uncert='no', trans='no', logx='no', logy='no
ax.plot(spectra[0][0], np.empty(len(spectra[0][0]))*np.nan, c='r', label='Planetary model')
iobs_spectro = -1


if len(spectra[indobs][4]) != 0:
iobs_photo += 1
iobs_spectro += 1
Expand Down Expand Up @@ -779,11 +781,14 @@ def plot_fit(self, figsize=(10, 5), uncert='no', trans='no', logx='no', logy='no
if logx == 'yes':
ax.set_xscale('log')
axr.set_xscale('log')

# Set xlog-scale
if logy == 'yes':
ax.set_yscale('log')

# Remove the xticks from the first ax
ax.set_xticks([])

# Labels
axr.set_xlabel(r'Wavelength (µm)')
if norm != 'yes':
Expand All @@ -796,7 +801,6 @@ def plot_fit(self, figsize=(10, 5), uncert='no', trans='no', logx='no', logy='no
ax.legend(frameon=False)
axr.legend(frameon=False)


# define the data as global
self.spectra = spectra

Expand Down
3 changes: 2 additions & 1 deletion RELEASE_NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ Comments:

_ _ _

08/11/2024
09/11/2024

Arthur Vigan

Expand All @@ -470,3 +470,4 @@ Comments:
- Fix import to allow importation of plotting module outside of the ForMoSA package context
- Update .gitignore file with more general patterns for a python package
- Remove files that should not have been committed
- Small improvements to plot_fit()

0 comments on commit cae90f0

Please sign in to comment.