Skip to content

Commit

Permalink
Merge pull request #299 from lbl-anp/masked-fitting
Browse files Browse the repository at this point in the history
add masked fitting functionality
  • Loading branch information
jvavrek authored Nov 9, 2021
2 parents 4f169e6 + 707f16a commit a115833
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 87 deletions.
33 changes: 25 additions & 8 deletions becquerel/core/fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ class Fitter:
Robinson, "Data reduction and error analysis for the physical sciences".
"""

def __init__(self, model, x=None, y=None, y_unc=None, dx=None, roi=None):
def __init__(self, model, x=None, y=None, y_unc=None, dx=None, roi=None, mask=None):
# Initialize
self._model = None
self._name = None
Expand All @@ -379,6 +379,7 @@ def __init__(self, model, x=None, y=None, y_unc=None, dx=None, roi=None):
self._y_unc = None
self._roi = None
self._roi_msk = None
self._mask = None
self._xmode = None
self._ymode = None
self.result = None
Expand All @@ -387,7 +388,9 @@ def __init__(self, model, x=None, y=None, y_unc=None, dx=None, roi=None):
self._make_model(model)
self.params = self.model.make_params()
# Set data
self.set_data(x=x, y=y, y_unc=y_unc, dx=dx, roi=roi, update_defaults=True)
self.set_data(
x=x, y=y, y_unc=y_unc, dx=dx, roi=roi, mask=mask, update_defaults=True
)

def __str__(self):
return (
Expand Down Expand Up @@ -453,23 +456,23 @@ def y_unc(self, y_unc):

@property
def x_roi(self):
return self.x[self.roi_msk]
return self.x[self.roi_msk & self.mask]

@property
def y_roi(self):
return self.y[self.roi_msk]
return self.y[self.roi_msk & self.mask]

@property
def y_unc_roi(self):
if self.y_unc is None:
return None
return self.y_unc[self.roi_msk]
return self.y_unc[self.roi_msk & self.mask]

@property
def dx_roi(self):
if self.dx is None:
return None
return self.dx[self.roi_msk]
return self.dx[self.roi_msk & self.mask]

@property
def roi(self):
Expand All @@ -482,6 +485,13 @@ def roi_msk(self):
else:
return self._roi_msk

@property
def mask(self):
if self._mask is None:
return np.ones_like(self.x, dtype=bool)
else:
return self._mask

@property
def xmode(self):
return self._xmode
Expand All @@ -494,7 +504,9 @@ def ymode(self):
def param_names(self):
return list(self.params.keys())

def set_data(self, y, x=None, y_unc=None, dx=None, roi=None, update_defaults=True):
def set_data(
self, y, x=None, y_unc=None, dx=None, roi=None, update_defaults=True, mask=None
):
# Set y data (skip if y is None)
if y is None:
return
Expand All @@ -513,6 +525,8 @@ def set_data(self, y, x=None, y_unc=None, dx=None, roi=None, update_defaults=Tru
self.y_unc = y_unc
# set deltax (bin width)
self.dx = dx
# fit mask
self._mask = mask

if roi is not None:
self.set_roi(*roi)
Expand Down Expand Up @@ -648,7 +662,10 @@ def fit(self, backend="lmfit", guess=None, limits=None):
# Perform the fit, weighted by 1/uncertainties.
weights = self.y_unc_roi ** -1.0
self.result = self.model.fit(
y_roi_norm, self.params, x=self.x_roi, weights=weights
y_roi_norm,
self.params,
x=self.x_roi,
weights=weights,
)

elif self.backend == "lmfit-pml":
Expand Down
Loading

0 comments on commit a115833

Please sign in to comment.