Skip to content

Commit

Permalink
Remove debugging prints, add comment for context
Browse files Browse the repository at this point in the history
  • Loading branch information
rosteen committed Sep 11, 2024
1 parent f735123 commit 5dd22fa
Showing 1 changed file with 2 additions and 22 deletions.
24 changes: 2 additions & 22 deletions jdaviz/configs/default/plugins/model_fitting/model_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ def _param_units(self, param, model_type=None):
elif param == "scale" and model_type == "BlackBody":
return str("")

#print(f"returning y units from _param_units: {self._units['y']}")
return self._units["y"] if param in y_params else self._units["x"]

def _update_parameters_from_fit(self):
Expand Down Expand Up @@ -465,24 +464,20 @@ def _initialize_model_component(self, model_comp, comp_label, poly_order=None):
"bounds": {"scale": (0.0, None)}}

initial_values = {}
#print("Initializing values")
for param_name in get_model_parameters(model_cls, new_model["model_kwargs"]):
# access the default value from the model class itself
default_param = getattr(model_cls, param_name, _EmptyParam(0))
default_units = self._param_units(param_name,
model_type=new_model["model_type"])
#print(f"default units: {default_units}")

if default_param.unit is None:
# then the model parameter accepts unitless, but we want
# to pass with appropriate default units
#print("default_param.unit is None")
initial_val = u.Quantity(default_param.value, default_units)
else:
# then the model parameter has default units. We want to pass
# with jdaviz default units (based on x/y units) but need to
# convert the default parameter unit to these units
#print(f"Got to this else, converting {default_param.quantity} to {default_units}")
initial_val = default_param.quantity.to(default_units)

initial_values[param_name] = initial_val
Expand All @@ -498,21 +493,17 @@ def _initialize_model_component(self, model_comp, comp_label, poly_order=None):
masked_spectrum = self._apply_subset_masks(self.dataset.selected_spectrum,
self.spectral_subset)
mask = masked_spectrum.mask
#print(f"Initial values: {initial_values}")
initialized_model = initialize(
MODELS[model_comp](name=comp_label,
**initial_values,
**new_model.get("model_kwargs", {})),
masked_spectrum.spectral_axis[~mask] if mask is not None else masked_spectrum.spectral_axis, # noqa
masked_spectrum.flux[~mask] if mask is not None else masked_spectrum.flux)

#print(initialized_model)

# need to loop over parameters again as the initializer may have overridden
# the original default value. However, if we toggled cube_fit, we may need to override
for param_name in get_model_parameters(model_cls, new_model["model_kwargs"]):
param_quant = getattr(initialized_model, param_name)
#print(f"Setting param_quant {param_name} {param_quant.value} {param_quant.unit}")
new_model["parameters"].append({"name": param_name,
"value": param_quant.value,
"unit": str(param_quant.unit),
Expand All @@ -522,8 +513,6 @@ def _initialize_model_component(self, model_comp, comp_label, poly_order=None):
new_model["Initialized"] = True
new_model["initialized_display_units"] = self._units.copy()

#print(f"initialized_display_units: {new_model['initialized_display_units']}")

new_model["compat_display_units"] = True # always compatible at time of creation
return new_model

Expand Down Expand Up @@ -559,13 +548,11 @@ def _on_global_display_unit_changed(self, msg):
uc = self.app._jdaviz_helper.plugins['Unit Conversion']

if msg.unit != uc._obj.sb_unit_selected:
print(f"Uh oh, flux units: {msg.unit}, forcing to {uc._obj.sb_unit_selected}")
self._units[axis] = uc._obj.sb_unit_selected
self._check_model_component_compat([axis], [u.Unit(uc._obj.sb_unit_selected)])
return

# update internal tracking of current units
print(f"Updating {axis} units to {msg.unit}")
self._units[axis] = str(msg.unit)

self._check_model_component_compat([axis], [msg.unit])
Expand Down Expand Up @@ -796,14 +783,9 @@ def _handle_toggle_cube_fit(self, event={}):
self._update_viewer_filters(event=event)
uc = self.app._jdaviz_helper.plugins['Unit Conversion']

print("Toggled cube fit")
print(self._units['y'])
print(uc._obj.sb_unit_selected, uc.flux_unit.selected)
if event.get('new'):
print(f"Uh oh, flux units: {self._units['y']}, forcing to {uc._obj.sb_unit_selected}")
self._units['y'] = uc._obj.sb_unit_selected
else:
print("Changing y units back to flux")
self._units['y'] = uc.flux_unit.selected


Expand Down Expand Up @@ -954,9 +936,7 @@ def _fit_model_to_cube(self, add_data):

uc = self.app._jdaviz_helper.plugins['Unit Conversion']
if spec.flux.unit != uc._obj.sb_unit_selected:
print(f"Converting cube units from {spec.flux.unit} to {uc._obj.sb_unit_selected}")
spec = spec.with_flux_unit(uc._obj.sb_unit_selected)
print(f"Fitting to {spec}")

snackbar_message = SnackbarMessage(
"Fitting model to cube...",
Expand All @@ -968,8 +948,8 @@ def _fit_model_to_cube(self, add_data):

# Apply masks from selected spectral subset
spec = self._apply_subset_masks(spec, self.spectral_subset)
print(f"Fitting with {models_to_fit}")
# Also mask out NaNs for fitting
# Also mask out NaNs for fitting. Simply adding filter_non_finite to the cube fit
# didn't work out of the box, so doing this for now.
if spec.mask is None:
spec.mask = np.isnan(spec.flux)
else:
Expand Down

0 comments on commit 5dd22fa

Please sign in to comment.