Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cubespecfit/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .fitting import *
from .models import *
13 changes: 8 additions & 5 deletions cubespecfit/fitting.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
__all__ = ['fitspec_lmfit','bic','fitcube','fitcube_two_model']

import numpy as np
import matplotlib
from matplotlib import pyplot as plt
Expand All @@ -7,7 +9,7 @@
import logging
import warnings

from cubespecfit.wcs_helpers import _save_results
from .wcs_helpers import _save_results

# Suppress specific numpy warnings globally
warnings.filterwarnings('ignore', message='Mean of empty slice')
Expand Down Expand Up @@ -119,15 +121,13 @@ def constrained_residual(params, x, data, weights):
# Reconstruct model result
param_vals = [result.params[p].value for p in params_dict.keys()]
bestfit = model(wave, *param_vals)
chi2 = np.sum((flux - bestfit) ** 2 / err ** 2)

# Extract results - only varying parameters
varying_params = [p for p in params_dict.keys() if result.params[p].vary]
param_values = np.array([result.params[p].value for p in varying_params])
param_errors = np.array([result.params[p].stderr if result.params[p].stderr is not None
else np.nan for p in varying_params])

result_obj = result

else:
# Standard unconstrained fit
Expand All @@ -140,8 +140,11 @@ def constrained_residual(params, x, data, weights):
else np.nan for p in varying_params])

bestfit = result.best_fit
chi2 = result.chisqr
result_obj = result

# NOTE lmfit doesn't appear to return what we think of as the "standard" chisquared
#chi2 = result.chisqr
chi2 = np.sum((flux - bestfit) ** 2 / err ** 2)
result_obj = result

return param_values, param_errors, bestfit, chi2, result_obj

Expand Down
3 changes: 3 additions & 0 deletions cubespecfit/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
__all__ = ['ha_nii_model','ha_nii_broad_model','ha_nii_sii_model','sii_model','oiii_model',
'oiii_broad_model','oiii_hbeta_model','oiii_hbeta_broad_model','hbeta_model']

import numpy as np

# ------------------------------------------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions cubespecfit/wcs_helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
__all__ = ['_propagate_celestial_wcs','_save_results']

import os
import numpy as np
from astropy.io import fits
Expand Down