Skip to content

Commit

Permalink
Display Gaussian1D results in GUI
Browse files Browse the repository at this point in the history
and some bug fixes
  • Loading branch information
pllim committed Jun 28, 2022
1 parent 0b4a770 commit 99fd9df
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions docs/imviz/plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ an interactively selected region. A typical workflow is as follows:
However, if NaN exists in data, it will be treated as 0.

When calculation is complete, a plot would show the radial profile
of the background subtracted data and the photometry results are displayed under the
:guilabel:`CALCULATE` button.
of the background subtracted data and the photometry and model fitting (if requested)
results are displayed under the :guilabel:`CALCULATE` button.

.. figure:: img/imviz_radial_profile.png
:alt: Imviz radial profile plot.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
from astropy import units as u
from astropy.modeling.fitting import LevMarLSQFitter
from astropy.modeling import Parameter
from astropy.modeling.models import Gaussian1D
from astropy.table import QTable
from astropy.time import Time
Expand Down Expand Up @@ -68,6 +69,7 @@ def __init__(self, *args, **kwargs):
self._fig = bqplot.Figure()
self.plot_types = ["Curve of Growth", "Radial Profile", "Radial Profile (Raw)"]
self.current_plot_type = self.plot_types[0]
self._fitted_model_name = 'phot_radial_profile'

def reset_results(self):
self.result_available = False
Expand Down Expand Up @@ -223,6 +225,11 @@ def vue_do_aper_phot(self, *args, **kwargs):
data = self._selected_data
reg = self._selected_subset

# Reset last fitted model
fit_model = None
if self._fitted_model_name in self.app.fitted_models:
del self.app.fitted_models[self._fitted_model_name]

try:
comp = data.get_component(data.main_components[0])
try:
Expand Down Expand Up @@ -373,7 +380,7 @@ def vue_do_aper_phot(self, *args, **kwargs):
self.hub.broadcast(SnackbarMessage(
f"Radial profile fitting: {msg}", color='warning', sender=self))
y_fit = fit_model(x_data)
self.app.fitted_models['phot_radial_profile'] = fit_model
self.app.fitted_models[self._fitted_model_name] = fit_model
bqplot_fit = bqplot.Lines(x=x_data, y=y_fit, marker=None,
scales={'x': line_x_sc, 'y': line_y_sc},
colors='magenta', line_style='dashed')
Expand Down Expand Up @@ -410,6 +417,15 @@ def vue_do_aper_phot(self, *args, **kwargs):
f'{x:.4e} ({phot_table["aperture_sum_counts_err"][0]:.4e})'})
else:
tmp.append({'function': key, 'result': str(x)})
# Also display fit results
if fit_model is not None and isinstance(fit_model, Gaussian1D):
model_name = fit_model.__class__.__name__
for param in ('fwhm', 'mean', 'amplitude'):
p_val = getattr(fit_model, param)
if isinstance(p_val, Parameter):
p_val = p_val.value
tmp.append({'function': f'{model_name}_{param}',
'result': f'{p_val:.4e}'})
self.results = tmp
self.result_available = True
self.radial_plot = self._fig
Expand Down
6 changes: 3 additions & 3 deletions notebooks/concepts/imviz_simple_aper_phot.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
"id": "b45fcdfe",
"metadata": {},
"source": [
"If you fitted Gaussian to radial profile, you can get it back out like this."
"If you fitted Gaussian to radial profile, you can get it back out like this. If it does not exist, you will get `None`."
]
},
{
Expand All @@ -205,14 +205,14 @@
"metadata": {},
"outputs": [],
"source": [
"my_gaussian = imviz.app.fitted_models['phot_radial_profile']\n",
"my_gaussian = imviz.app.fitted_models.get('phot_radial_profile', None)\n",
"my_gaussian"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1e546682",
"id": "b7c59fef",
"metadata": {},
"outputs": [],
"source": []
Expand Down

0 comments on commit 99fd9df

Please sign in to comment.