Skip to content

Commit

Permalink
refactor the selection of models using minimum_match
Browse files Browse the repository at this point in the history
  • Loading branch information
teuben committed Jun 27, 2024
1 parent 4fffd29 commit 6f8f8f7
Showing 1 changed file with 11 additions and 23 deletions.
34 changes: 11 additions & 23 deletions src/dysh/spectra/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,34 +254,22 @@ def baseline(spectrum, order, exclude=None, **kwargs):
"""
kwargs_opts = {
#'show': False,
"model": "polynomial",
"model": "chebyshev",
"fitter": LinearLSQFitter(calc_uncertainties=True),
"fix_exclude": False,
"exclude_action": "replace", # {'replace','append',None}
"exclude_action": "replace", # {'replace','append', None}
}
kwargs_opts.update(kwargs)

# @todo allow minimum match here; need a tool for this, e.g.
# model = minmatch(kwargs_opts["model"], _valid_models)
# if model = None:
# raise ValueError()
# elif model == "polynomial"
# ...
_valid_models = ["polynomial", "chebyshev", "legendre", "hermite"]
model = minimum_string_match(kwargs_opts["model"], _valid_models)
available_models = { "chebyshev" : Chebyshev1D,
"hermite" : Hermite1D,
"legendre" : Legendre1D,
"polynomial" : Polynomial1D,
}
model = minimum_string_match(kwargs_opts["model"], list(available_models.keys()))
if model == None:
raise ValueError(f'Unrecognized input model {kwargs["model"]}. Must be one of {_valid_models}')
elif model == "polynomial":
model = Polynomial1D(degree=order)
elif model == "chebyshev":
model = Chebyshev1D(degree=order)
elif model == "legendre":
model = Legendre1D(degree=order)
elif model == "hermite":
model = Hermite1D(degree=order)
else:
# should never get here, unless we someday allow user to input an astropy.model
raise ValueError(f'Unrecognized input model {kwargs["model"]}. Must be one of {_valid_models}')
raise ValueError(f'Unrecognized input model {kwargs["model"]}. Must be one of {list(available_models.keys())}')
selected_model = available_models[model](degree=order)

_valid_exclude_actions = ["replace", "append", None]
if kwargs_opts["exclude_action"] not in _valid_exclude_actions:
Expand All @@ -306,7 +294,7 @@ def baseline(spectrum, order, exclude=None, **kwargs):
# exist (they will be a list of SpectralRegions or None)
regionlist = p._exclude_regions
print(f"EXCLUDING {regionlist}")
return fit_continuum(spectrum=p, model=model, fitter=fitter, exclude_regions=regionlist)
return fit_continuum(spectrum=p, model=selected_model, fitter=fitter, exclude_regions=regionlist)


def mean_tsys(calon, caloff, tcal, mode=0, fedge=0.1, nedge=None):
Expand Down

1 comment on commit 6f8f8f7

@mpound
Copy link
Collaborator

@mpound mpound commented on 6f8f8f7 Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

Please sign in to comment.