From 10abc5f74fae74f84b4f31b07eb083e8a8e2afcf Mon Sep 17 00:00:00 2001 From: Chamani Gunasekera Date: Tue, 10 Sep 2024 10:44:28 -0400 Subject: [PATCH 1/3] Add option to fit sed with G23 only --- measure_extinction/modeldata.py | 75 +++++++++++++++------------ measure_extinction/utils/fit_model.py | 10 ++-- 2 files changed, 48 insertions(+), 37 deletions(-) diff --git a/measure_extinction/modeldata.py b/measure_extinction/modeldata.py index f0fcd2f..f61b378 100644 --- a/measure_extinction/modeldata.py +++ b/measure_extinction/modeldata.py @@ -209,7 +209,7 @@ def stellar_sed(self, params, velocity=None): return sed - def dust_extinguished_sed(self, params, sed, velocity=0.0): + def dust_extinguished_sed(self, params, sed, fit_range="all", velocity=0.0): """ Dust extinguished sed given the extinction parameters @@ -231,42 +231,51 @@ def dust_extinguished_sed(self, params, sed, velocity=0.0): """ Rv = params[1] g23mod = G23(Rv=Rv) - optnir_axav_x = np.flip(1.0 / (np.arange(0.35, 30.0, 0.1) * u.micron)) - optnir_axav_y = g23mod(optnir_axav_x) - - # updated F04 C1-C2 correlation - C1 = 2.18 - 2.91 * params[2] # create the extinguished sed ext_sed = {} - for cspec in self.fluxes.keys(): - # get the dust extinguished SED (account for the - # systemic velocity of the galaxy [opposite regular sense]) - shifted_waves = (1.0 - velocity / 2.998e5) * self.waves[cspec] - axav = _curve_F99_method( - shifted_waves, - Rv, - C1, - params[2], - params[3], - params[4], - params[5], - params[6], - optnir_axav_x.value, - optnir_axav_y, - [0.033, 11.0], - "FM90_G23_measure_extinction", - ) - ext_sed[cspec] = sed[cspec] * (10 ** (-0.4 * axav * params[0])) + if fit_range == "g23": + for cspec in self.fluxes.keys(): + shifted_waves = (1.0 - velocity / 2.998e5) * self.waves[cspec] + axav = g23mod(shifted_waves) + ext_sed[cspec] = sed[cspec] * (10 ** (-0.4 * axav * params[0])) + + elif fit_range == "all": + optnir_axav_x = np.flip(1.0 / (np.arange(0.35, 30.0, 0.1) * u.micron)) + optnir_axav_y = g23mod(optnir_axav_x) + + # updated F04 C1-C2 correlation + C1 = 2.18 - 2.91 * params[2] + + for cspec in self.fluxes.keys(): + # get the dust extinguished SED (account for the + # systemic velocity of the galaxy [opposite regular sense]) + shifted_waves = (1.0 - velocity / 2.998e5) * self.waves[cspec] + axav = _curve_F99_method( + shifted_waves, + Rv, + C1, + params[2], #C2 + params[3], #C3 + params[4], #C4 + xo=params[5], #xo + gamma=params[6], #gamma + optnir_axav_x=optnir_axav_x.value, + optnir_axav_y=optnir_axav_y, + valid_x_range=[0.033, 11.0], + model_name="FM90_G23_measure_extinction", + ) + + ext_sed[cspec] = sed[cspec] * (10 ** (-0.4 * axav * params[0])) - # # create the extinguished sed - # ext_sed = {} - # for cspec in self.fluxes.keys(): - # # get the dust extinguished SED (account for the - # # systemic velocity of the galaxy [opposite regular sense]) - # shifted_waves = (1.0 - velocity / 2.998e5) * self.waves[cspec] - # axav = g23mod(shifted_waves) - # ext_sed[cspec] = sed[cspec] * (10 ** (-0.4 * axav * params[0])) + # # create the extinguished sed + # ext_sed = {} + # for cspec in self.fluxes.keys(): + # # get the dust extinguished SED (account for the + # # systemic velocity of the galaxy [opposite regular sense]) + # shifted_waves = (1.0 - velocity / 2.998e5) * self.waves[cspec] + # axav = g23mod(shifted_waves) + # ext_sed[cspec] = sed[cspec] * (10 ** (-0.4 * axav * params[0])) return ext_sed diff --git a/measure_extinction/utils/fit_model.py b/measure_extinction/utils/fit_model.py index 6967d77..82af967 100755 --- a/measure_extinction/utils/fit_model.py +++ b/measure_extinction/utils/fit_model.py @@ -64,7 +64,7 @@ def __init__( self.weights = weights self.velocities = np.array([stellar_velocity, 0.0]) - def lnlike(self, params, obsdata, modeldata): + def lnlike(self, params, obsdata, modeldata, fit_range="all"): """ Compute the natural log of the likelihood that the data fits the model. @@ -73,6 +73,7 @@ def lnlike(self, params, obsdata, modeldata): ---------- params : floats parameters of the model + params = [logT, logg, logZ, Av, Rv, C2, C3, C4, x0, gamma, HI_gal, HI_mw] obsdata : StarData object observed data for a reddened star @@ -85,7 +86,8 @@ def lnlike(self, params, obsdata, modeldata): # dust_extinguished sed ext_modsed = modeldata.dust_extinguished_sed( - params[3:10], modsed, velocity=self.velocities[0] + params[3:10], modsed, fit_range=fit_range, + velocity=self.velocities[0] ) # hi_abs sed @@ -143,7 +145,7 @@ def lnprior(self, params): return lnp @staticmethod - def lnprob(params, obsdata, modeldata, fitinfo): + def lnprob(params, obsdata, modeldata, fitinfo, fit_range="all"): """ Compute the natural log of the probability @@ -167,7 +169,7 @@ def lnprob(params, obsdata, modeldata, fitinfo): if lnp == lnp_bignnum: return lnp else: - return lnp + fitinfo.lnlike(params, obsdata, modeldata) + return lnp + fitinfo.lnlike(params, obsdata, modeldata, fit_range=fit_range) def check_param_limits(self, params): """ From 394088592aaa5bd00e62c8dcfd19b0955993d302 Mon Sep 17 00:00:00 2001 From: Chamani Gunasekera Date: Tue, 10 Sep 2024 10:47:11 -0400 Subject: [PATCH 2/3] Include error message for mismatched BANDs --- measure_extinction/utils/fit_model.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/measure_extinction/utils/fit_model.py b/measure_extinction/utils/fit_model.py index 82af967..cbb6724 100755 --- a/measure_extinction/utils/fit_model.py +++ b/measure_extinction/utils/fit_model.py @@ -98,7 +98,10 @@ def lnlike(self, params, obsdata, modeldata, fit_range="all"): lnl = 0.0 for cspec in hi_ext_modsed.keys(): - gvals = (self.weights[cspec] > 0) & (np.isfinite(hi_ext_modsed[cspec])) + try: + gvals = (self.weights[cspec] > 0) & (np.isfinite(hi_ext_modsed[cspec])) + except ValueError: + raise ValueError("Oops! The model data and reddened star data did not match.\n Hint: Make sure that the BAND name in the .dat files match.") chiarr = np.square( ( obsdata.data[cspec].fluxes[gvals].value From ed897cfb57adf4dcde1d29de3b74262180bf3073 Mon Sep 17 00:00:00 2001 From: Chamani Gunasekera Date: Thu, 12 Sep 2024 16:11:26 -0400 Subject: [PATCH 3/3] Fix codestyle errors and add else --- measure_extinction/modeldata.py | 22 +++++++++++++++------- measure_extinction/utils/fit_model.py | 7 ++++--- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/measure_extinction/modeldata.py b/measure_extinction/modeldata.py index f61b378..b2a1483 100644 --- a/measure_extinction/modeldata.py +++ b/measure_extinction/modeldata.py @@ -221,6 +221,9 @@ def dust_extinguished_sed(self, params, sed, fit_range="all", velocity=0.0): sed : dict fluxes for each spectral piece + fit_range : string, optional + keyword to toggle SED fitting to be done with G23 only or to also include curve_F99_method + velocity : float, optional velocity of dust @@ -234,13 +237,13 @@ def dust_extinguished_sed(self, params, sed, fit_range="all", velocity=0.0): # create the extinguished sed ext_sed = {} - if fit_range == "g23": + if fit_range.lower() == "g23": for cspec in self.fluxes.keys(): shifted_waves = (1.0 - velocity / 2.998e5) * self.waves[cspec] axav = g23mod(shifted_waves) ext_sed[cspec] = sed[cspec] * (10 ** (-0.4 * axav * params[0])) - elif fit_range == "all": + elif fit_range.lower() == "all": optnir_axav_x = np.flip(1.0 / (np.arange(0.35, 30.0, 0.1) * u.micron)) optnir_axav_y = g23mod(optnir_axav_x) @@ -255,11 +258,11 @@ def dust_extinguished_sed(self, params, sed, fit_range="all", velocity=0.0): shifted_waves, Rv, C1, - params[2], #C2 - params[3], #C3 - params[4], #C4 - xo=params[5], #xo - gamma=params[6], #gamma + params[2], # C2 + params[3], # C3 + params[4], # C4 + xo=params[5], # xo + gamma=params[6], # gamma optnir_axav_x=optnir_axav_x.value, optnir_axav_y=optnir_axav_y, valid_x_range=[0.033, 11.0], @@ -277,6 +280,11 @@ def dust_extinguished_sed(self, params, sed, fit_range="all", velocity=0.0): # axav = g23mod(shifted_waves) # ext_sed[cspec] = sed[cspec] * (10 ** (-0.4 * axav * params[0])) + else: + raise ValueError( + "Incorrect input for fit_range argument in dust_extinguished_sed(). Available options are: g23, all" + ) + return ext_sed def dust_extinguished_sed_FM04(self, params, sed, velocity=0.0): diff --git a/measure_extinction/utils/fit_model.py b/measure_extinction/utils/fit_model.py index cbb6724..993b49c 100755 --- a/measure_extinction/utils/fit_model.py +++ b/measure_extinction/utils/fit_model.py @@ -86,8 +86,7 @@ def lnlike(self, params, obsdata, modeldata, fit_range="all"): # dust_extinguished sed ext_modsed = modeldata.dust_extinguished_sed( - params[3:10], modsed, fit_range=fit_range, - velocity=self.velocities[0] + params[3:10], modsed, fit_range=fit_range, velocity=self.velocities[0] ) # hi_abs sed @@ -101,7 +100,9 @@ def lnlike(self, params, obsdata, modeldata, fit_range="all"): try: gvals = (self.weights[cspec] > 0) & (np.isfinite(hi_ext_modsed[cspec])) except ValueError: - raise ValueError("Oops! The model data and reddened star data did not match.\n Hint: Make sure that the BAND name in the .dat files match.") + raise ValueError( + "Oops! The model data and reddened star data did not match.\n Hint: Make sure that the BAND name in the .dat files match." + ) chiarr = np.square( ( obsdata.data[cspec].fluxes[gvals].value