Skip to content

Commit

Permalink
Merge pull request #124 from Chamani8/g23
Browse files Browse the repository at this point in the history
Add option to fit sed with G23 only
  • Loading branch information
karllark authored Sep 12, 2024
2 parents 2c93737 + ed897cf commit e79245f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 38 deletions.
83 changes: 50 additions & 33 deletions measure_extinction/modeldata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -221,6 +221,9 @@ def dust_extinguished_sed(self, params, sed, 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
Expand All @@ -231,42 +234,56 @@ 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.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.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)

# 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]))

else:
raise ValueError(
"Incorrect input for fit_range argument in dust_extinguished_sed(). Available options are: g23, all"
)

return ext_sed

Expand Down
16 changes: 11 additions & 5 deletions measure_extinction/utils/fit_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -74,6 +74,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
Expand All @@ -86,7 +87,7 @@ 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
Expand All @@ -97,7 +98,12 @@ def lnlike(self, params, obsdata, modeldata):

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(
(
(
Expand Down Expand Up @@ -146,7 +152,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
Expand All @@ -170,7 +176,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):
"""
Expand Down

0 comments on commit e79245f

Please sign in to comment.