Skip to content

Commit

Permalink
allow re-estimating model parameters to update compat checks
Browse files Browse the repository at this point in the history
  • Loading branch information
kecnry committed May 30, 2023
1 parent 4ecdae7 commit 8cd968c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
2 changes: 1 addition & 1 deletion jdaviz/components/tooltip.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const tooltips = {
'plugin-plot-options-mixed-state': 'Current values are mixed, click to sync at shown value',
'plugin-model-fitting-add-model': 'Create model component',
'plugin-model-fitting-param-fixed': 'Check the box to freeze parameter value',
'plugin-model-fitting-reestimate-all': 'Re-estimate initial values based on the current data/subset selection for all free parameters',
'plugin-model-fitting-reestimate-all': 'Re-estimate initial values based on the current data/subset selection for all free parameters based on current display units',
'plugin-model-fitting-reestimate': 'Re-estimate initial values based on the current data/subset selection for all free parameters in this component',
'plugin-unit-conversion-apply': 'Apply unit conversion',
'plugin-line-lists-load': 'Load list into "Loaded Lines" section of plugin',
Expand Down
34 changes: 24 additions & 10 deletions jdaviz/configs/default/plugins/model_fitting/model_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,23 +514,33 @@ def _initialize_model_component(self, model_comp, comp_label, poly_order=None):
new_model["compat_display_units"] = True # always compatible at time of creation
return new_model

def _on_global_display_unit_changed(self, msg):
axis = {'spectral': 'x', 'flux': 'y'}.get(msg.axis)
def _check_model_component_compat(self, axes=['x', 'y'], display_units=None):
if display_units is None:
display_units = [u.Unit(self._units[ax]) for ax in axes]

# update internal tracking of current units
self._units[axis] = str(msg.unit)
disp_physical_types = [unit.physical_type for unit in display_units]

# update validity of model components
disp_physical_type = msg.unit.physical_type
for model_index, comp_model in enumerate(self.component_models):
comp_unit = u.Unit(comp_model["initialized_display_units"][axis])
compat = comp_unit.physical_type == disp_physical_type
compat = True
for ax, ax_physical_type in zip(axes, disp_physical_types):
comp_unit = u.Unit(comp_model["initialized_display_units"][ax])
compat = comp_unit.physical_type == ax_physical_type
if not compat:
break
self.component_models[model_index]["compat_display_units"] = compat

# length hasn't changed, so we need to force the traitlet to update
self.send_state("component_models")
self._check_model_equation_invalid()

def _on_global_display_unit_changed(self, msg):
axis = {'spectral': 'x', 'flux': 'y'}.get(msg.axis)

# update internal tracking of current units
self._units[axis] = str(msg.unit)

self._check_model_component_compat([axis], [msg.unit])

def remove_model_component(self, model_component_label):
"""
Remove an existing model component.
Expand Down Expand Up @@ -660,6 +670,9 @@ def reestimate_model_parameters(self, model_component_label=None):
# length hasn't changed, so we need to force the traitlet to update
self.send_state("component_models")

# model units may have changed, need to re-check their compatibility with display units
self._check_model_component_compat()

# return user-friendly info on revised model
return self.get_model_component(model_component_label)

Expand Down Expand Up @@ -700,6 +713,7 @@ def _check_model_equation_invalid(self, event=None):
# includes an operator without a variable (ex: 'C+')
self.model_equation_invalid_msg = 'incomplete equation.'
return

components_not_existing = [comp for comp in self.equation_components
if comp not in self.model_components]
if len(components_not_existing):
Expand All @@ -716,13 +730,13 @@ def _check_model_equation_invalid(self, event=None):
msg = ("is currently disabled because it has"
" incompatible units with the current display units."
" Remove the component from the equation,"
" recreate the model component to have the new units,"
" re-estimate its free parameters to use the new units"
" or revert the display units.")
else:
msg = ("are currently disabled because they have"
" incompatible units with the current display units."
" Remove the components from the equation,"
" recreate the model components to have the new units,"
" re-estimate their free parameters to use the new units"
" or revert the display units.")
self.model_equation_invalid_msg = f'{", ".join(components_not_valid)} {msg}'
return
Expand Down
23 changes: 22 additions & 1 deletion jdaviz/configs/default/plugins/model_fitting/model_fitting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,35 @@
<v-row v-if="!item.compat_display_units">
<v-alert :type="componentInEquation(item.id) ? 'error' : 'warning'">
<b>{{ item.id }}</b> is inconsistent with the current display units so cannot be used in the model equation.
Create a new model component or re-estimate the free parameters based on the current display units.
<v-row
justify="end"
style="padding-top: 12px; padding-right: 2px"
>
<j-tooltip tipid='plugin-model-fitting-reestimate'>
<v-btn
tile
:elevation=0
x-small
dense
color="turquoise"
dark
style="padding-left: 8px; padding-right: 6px;"
@click="reestimate_model_parameters(item.id)">
<v-icon left small dense style="margin-right: 2px">mdi-restart</v-icon>
Re-estimate free parameters
</v-btn>
</j-tooltip>
</v-row>
</v-alert>
</v-row>
<v-row v-if="item.compat_display_units && !componentInEquation(item.id)">
<v-alert type="info">
<b>{{ item.id }}</b> model component not in equation
</v-alert>
</v-row>
<v-row justify="end"
<v-row v-if="item.compat_display_units"
justify="end"
style="padding-top: 12px; padding-right: 2px"
>
<j-tooltip tipid='plugin-model-fitting-reestimate'>
Expand Down

0 comments on commit 8cd968c

Please sign in to comment.