Commit c13de4ed authored by Yannick Roehlly's avatar Yannick Roehlly

Get rid of process / _process separation

The process method was used to complete and check the parameter
dictionary before launching the _process one defined in each module. Now
that the parameters are dealed with at instantiation, this separation is
no more useful.

Also, modify the attenuation modules to read the filter in the database
at instantiation.
parent 1be38e55
......@@ -66,15 +66,13 @@ class Module(common.SEDCreationModule):
self.ssp = database.get_ssp_bc03(imf, metallicity)
database.session.close_all()
def _process(self, sed, parameters):
def process(self, sed):
"""Add the convolution of a Bruzual and Charlot SSP to the SED
Parameters
----------
sed : pcigale.sed.SED
SED object.
parameters : dictionary
Dictionary containing the parameters
"""
......@@ -108,7 +106,7 @@ class Module(common.SEDCreationModule):
# Base name for adding information to the SED.
name = self.name or "bc03"
sed.add_module(name, parameters)
sed.add_module(name, self.parameters)
sed.add_info(name + "_imf", imf)
sed.add_info(name + "_metallicity", metallicity)
......
......@@ -142,21 +142,6 @@ class SEDCreationModule(object):
"""
pass
def _process(self, sed, parameters):
"""Do the actual processing of the module on a SED object
This method is called with an object and a complete parameters
dictionary. It is not meant to be called directly but through the
process method.
Parameters
----------
sed : pcigale.sed.SED object
parameters : dictionary of the module parameters
"""
raise NotImplementedError()
def process(self, sed):
"""Process a SED object with the module
......@@ -168,7 +153,7 @@ class SEDCreationModule(object):
sed : pcigale.sed.SED object
"""
self._process(sed, self.parameters)
raise NotImplementedError()
def get_module(name, **kwargs):
......
......@@ -48,30 +48,28 @@ class Module(common.SEDCreationModule):
self.dh2002 = database.get_dh2002_infrared_templates()
database.session.close_all()
def _process(self, sed, parameters):
def process(self, sed):
"""Add the IR re-emission contributions
Parameters
----------
sed : pcigale.sed.SED object
parameters : dictionary containing the parameters
"""
alpha = float(parameters["alpha"])
attenuation_value_names = parameters["attenuation_value_names"]
alpha = float(self.parameters["alpha"])
attenuation_value_names = self.parameters["attenuation_value_names"]
dh2002 = self.dh2002
ir_template = dh2002.get_template(alpha)
ir_template = self.dh2002.get_template(alpha)
# Base name for adding information to the SED.
name = self.name or 'dh2002'
sed.add_module(name, parameters)
sed.add_module(name, self.parameters)
sed.add_info(name + '_alpha', alpha)
for attenuation in attenuation_value_names:
sed.add_contribution(
name + '_' + attenuation,
dh2002.wavelength_grid,
self.dh2002.wavelength_grid,
sed.info[attenuation] * ir_template
)
......@@ -244,13 +244,20 @@ class Module(common.SEDCreationModule):
"NAME_FILTER": "Attenuation in the FILTER filter.",
}
def _process(self, sed, parameters):
def _init_code(self):
"""Get the filters from the database"""
self.filters = {}
base = Database()
for filter_name in self.parameters["filters"]:
self.filters[filter_name] = base.get_filter(filter_name)
base.close()
def process(self, sed):
"""Add the CCM dust attenuation to the SED.
Parameters
----------
sed : pcigale.sed.SED object
parameters : dictionary containing the parameters
"""
......@@ -258,25 +265,22 @@ class Module(common.SEDCreationModule):
name = self.name or 'dustatt_calzleit_'
wavelength = sed.wavelength_grid
ebvs_young = float(parameters["E_BVs_young"])
ebvs_old = float(parameters["E_BVs_old_factor"]) * ebvs_young
young_contrib = parameters["young_contribution_name"]
old_contrib = parameters["old_contribution_name"]
uv_bump_wavelength = float(parameters["uv_bump_wavelength"])
uv_bump_width = float(parameters["uv_bump_wavelength"])
uv_bump_amplitude = float(parameters["uv_bump_amplitude"])
powerlaw_slope = float(parameters["powerlaw_slope"])
filter_list = parameters["filters"]
ebvs_young = float(self.parameters["E_BVs_young"])
ebvs_old = float(self.parameters["E_BVs_old_factor"]) * ebvs_young
young_contrib = self.parameters["young_contribution_name"]
old_contrib = self.parameters["old_contribution_name"]
uv_bump_wavelength = float(self.parameters["uv_bump_wavelength"])
uv_bump_width = float(self.parameters["uv_bump_wavelength"])
uv_bump_amplitude = float(self.parameters["uv_bump_amplitude"])
powerlaw_slope = float(self.parameters["powerlaw_slope"])
filters = self.filters
# Fλ fluxes in each filter before attenuation.
flux_noatt = {}
base = Database()
for filter_name in filter_list:
filt = base.get_filter(filter_name)
for filter_name, filter_ in filters.items():
flux_noatt[filter_name] = sed.compute_fnu(
filt.trans_table,
filt.effective_wavelength)
base.close()
filter_.trans_table,
filter_.effective_wavelength)
# Compute attenuation curve
sel_attenuation = a_vs_ebv(wavelength, uv_bump_wavelength,
......@@ -293,7 +297,7 @@ class Module(common.SEDCreationModule):
# spectrum is negative).
attenuation_young = -1 * np.trapz(attenuation_spectrum, wavelength)
sed.add_module(name, parameters)
sed.add_module(name, self.parameters)
sed.add_info(name + "_E_BVs_young", ebvs_young)
sed.add_info(name + "_attenuation_young", attenuation_young)
sed.add_contribution(name + "_young", wavelength, attenuation_spectrum)
......@@ -310,7 +314,7 @@ class Module(common.SEDCreationModule):
sed.add_info(name + "_E_BVs_old", ebvs_old)
sed.add_info(name + "_E_BVs_old_factor",
parameters["E_BVs_old_factor"])
self.parameters["E_BVs_old_factor"])
sed.add_info(name + "_attenuation_old", attenuation_old)
sed.add_contribution(name + "_old",
wavelength, attenuation_spectrum)
......@@ -323,15 +327,13 @@ class Module(common.SEDCreationModule):
# Fλ fluxes in each filter after attenuation.
flux_att = {}
base = Database()
for filter_name in filter_list:
filt = base.get_filter(filter_name)
flux_att[filter_name] = sed.compute_fnu(filt.trans_table,
filt.effective_wavelength)
base.close()
for filter_name, filter_ in filters.items():
flux_att[filter_name] = sed.compute_fnu(
filter_.trans_table,
filter_.effective_wavelength)
# Attenuation in each filter
for filter_name in filter_list:
for filter_name in filters:
sed.add_info(name + "_" + filter_name,
-2.5 * np.log(flux_att[filter_name] /
flux_noatt[filter_name]))
......@@ -165,13 +165,20 @@ class Module(common.SEDCreationModule):
"NAME_FILTER": "Attenuation in the FILTER filter.",
}
def _process(self, sed, parameters):
def _init_code(self):
"""Get the filters from the database"""
self.filters = {}
base = Database()
for filter_name in self.parameters["filters"]:
self.filters[filter_name] = base.get_filter(filter_name)
base.close()
def _process(self, sed):
"""Add the CCM dust attenuation to the SED.
Parameters
----------
sed : pcigale.sed.SED object
parameters : dictionary containing the parameters
"""
......@@ -179,25 +186,22 @@ class Module(common.SEDCreationModule):
name = self.name or 'dustatt_powerlaw_'
wavelength = sed.wavelength_grid
av_young = float(parameters["Av_young"])
av_old = float(parameters["Av_old_factor"] * av_young)
young_contrib = parameters["young_contribution_name"]
old_contrib = parameters["old_contribution_name"]
uv_bump_wavelength = float(parameters["uv_bump_wavelength"])
uv_bump_width = float(parameters["uv_bump_wavelength"])
uv_bump_amplitude = float(parameters["uv_bump_amplitude"])
powerlaw_slope = float(parameters["powerlaw_slope"])
filter_list = parameters["filters"]
av_young = float(self.parameters["Av_young"])
av_old = float(self.parameters["Av_old_factor"] * av_young)
young_contrib = self.parameters["young_contribution_name"]
old_contrib = self.parameters["old_contribution_name"]
uv_bump_wavelength = float(self.parameters["uv_bump_wavelength"])
uv_bump_width = float(self.parameters["uv_bump_wavelength"])
uv_bump_amplitude = float(self.parameters["uv_bump_amplitude"])
powerlaw_slope = float(self.parameters["powerlaw_slope"])
filters = self.parameters["filters"]
# Fλ fluxes in each filter before attenuation.
flux_noatt = {}
base = Database()
for filter_name in filter_list:
filt = base.get_filter(filter_name)
for filter_name, filter_ in filters.items():
flux_noatt[filter_name] = sed.compute_fnu(
filt.trans_table,
filt.effective_wavelength)
base.close()
filter_.trans_table,
filter_.effective_wavelength)
# Compute attenuation curve
sel_attenuation = alambda_av(wavelength, powerlaw_slope,
......@@ -213,7 +217,7 @@ class Module(common.SEDCreationModule):
# spectrum is negative).
attenuation_young = -1 * np.trapz(attenuation_spectrum, wavelength)
sed.add_module(name, parameters)
sed.add_module(name, self.parameters)
sed.add_info(name + "_Av_young", av_young)
sed.add_info(name + "_attenuation_young", attenuation_young)
sed.add_contribution(name + "_young", wavelength, attenuation_spectrum)
......@@ -229,7 +233,7 @@ class Module(common.SEDCreationModule):
sed.add_info(name + "_Av_old", av_old)
sed.add_info(name + "_Av_old_factor",
parameters["Av_old_factor"])
self.parameters["Av_old_factor"])
sed.add_info(name + "_attenuation_old", attenuation_old)
sed.add_contribution(name + "_old",
wavelength, attenuation_spectrum)
......@@ -242,15 +246,13 @@ class Module(common.SEDCreationModule):
# Fλ fluxes in each filter after attenuation.
flux_att = {}
base = Database()
for filter_name in filter_list:
filt = base.get_filter(filter_name)
flux_att[filter_name] = sed.compute_fnu(filt.trans_table,
filt.effective_wavelength)
base.close()
for filter_name, filter_ in filters.items():
flux_att[filter_name] = sed.compute_fnu(
filter_.trans_table,
filter_.effective_wavelength)
# Attenuation in each filter
for filter_name in filter_list:
for filter_name in filters:
sed.add_info(name + "_" + filter_name,
-2.5 * np.log(flux_att[filter_name] /
flux_noatt[filter_name]))
......@@ -41,7 +41,7 @@ class Module(common.SEDCreationModule):
)
}
def _process(self, sed, parameters):
def process(self, sed):
"""Add the redshift + IGM attenuation effect to the SED
Parameters
......@@ -52,7 +52,7 @@ class Module(common.SEDCreationModule):
"""
if parameters['redshift'] == 0:
if self.parameters['redshift'] == 0:
# If redshift is 0, we do nothing
pass
else:
......@@ -60,14 +60,14 @@ class Module(common.SEDCreationModule):
lsstSed = lsst.Sed()
# First, we get the redshifted spectrum of the galaxy
wavelen, flambda = lsstSed.redshiftSED(parameters['redshift'],
parameters['dimming'],
wavelen, flambda = lsstSed.redshiftSED(self.parameters['redshift'],
self.parameters['dimming'],
sed.wavelength_grid,
sed.luminosity)
wavelen, red_l_lambda = lsstSed.addIGMattenuation(
parameters['redshift'],
parameters['rtau'],
self.parameters['redshift'],
self.parameters['rtau'],
wavelen=wavelen,
flambda=flambda
)
......@@ -86,10 +86,10 @@ class Module(common.SEDCreationModule):
# Base name for adding information to the SED.
name = self.name or 'igmattenuation'
sed.add_module(name, parameters)
sed.add_module(name, self.parameters)
sed.add_info(name + '_redshift', parameters['redshift'])
sed.add_info(name + '_rtau', parameters['rtau'])
sed.add_info(name + '_redshift', self.parameters['redshift'])
sed.add_info(name + '_rtau', self.parameters['rtau'])
sed.add_contribution(
name,
......
......@@ -40,25 +40,24 @@ class Module(common.SEDCreationModule):
)
}
def _process(self, sed, parameters):
def process(self, sed):
"""Add the spectrum from the file to the SED object
Parameters
----------
sed : pcigale.sed.SED object
parameters : dictionary containing the parameters
"""
filename = parameters['filename']
filename = self.parameters['filename']
table = atpy.Table(filename, verbose=False)
# Base name for adding information to the SED.
name = self.name or 'loadfile'
sed.add_module(name, parameters)
sed.add_module(name, self.parameters)
sed.add_contribution(
name + '_' + filename,
table[parameters['lambda_column']],
table[parameters['l_lambda_column']]
table[self.parameters['lambda_column']],
table[self.parameters['l_lambda_column']]
)
......@@ -113,15 +113,13 @@ class Module(common.SEDCreationModule):
self.ssp = database.get_ssp_m2005(imf, metallicity)
database.session.close_all()
def _process(self, sed, parameters):
def process(self, sed):
"""Add the convolution of a Maraston 2005 SSP to the SED
Parameters
----------
sed : pcigale.sed.SED
SED object.
parameters : dictionary
Dictionary containing the parameters
"""
......@@ -155,7 +153,7 @@ class Module(common.SEDCreationModule):
# Base name for adding information to the SED.
name = self.name or 'm2005_sfh'
sed.add_module(name, parameters)
sed.add_module(name, self.parameters)
sed.add_info('imf', imf)
sed.add_info('metallicity', metallicity)
......
......@@ -63,21 +63,20 @@ class Module(common.SEDCreationModule):
"burst_age": "Age of the late burst in Myr."
}
def _process(self, sed, parameters):
def process(self, sed):
"""Add a double decreasing exponential Star Formation History.
Parameters
----------
sed : pcigale.sed.SED object
parameters : dictionary containing the parameters
"""
tau_main = float(parameters["tau_main"])
tau_burst = float(parameters["tau_burst"])
f_burst = float(parameters["f_burst"])
age = int(parameters["age"])
burst_age = int(parameters["burst_age"])
tau_main = float(self.parameters["tau_main"])
tau_burst = float(self.parameters["tau_burst"])
f_burst = float(self.parameters["f_burst"])
age = int(self.parameters["age"])
burst_age = int(self.parameters["burst_age"])
# Time grid and age. If needed, the age is rounded to the inferior Myr
time_grid = np.arange(AGE_LAPSE, age + AGE_LAPSE, AGE_LAPSE)
......@@ -106,7 +105,7 @@ class Module(common.SEDCreationModule):
# Base name for adding information to the SED.
name = self.name or "sfh2exp"
sed.add_module(name, parameters)
sed.add_module(name, self.parameters)
# Add the sfh and the output parameters to the SED.
sed.sfh = (time_grid, sfr)
......
......@@ -44,7 +44,7 @@ class Module(common.SEDCreationModule):
)
}
def _process(self, sed, parameters):
def process(self, sed):
"""Add the SFH read from the file.
Parameters
......@@ -53,18 +53,18 @@ class Module(common.SEDCreationModule):
parameters : dictionary containing the parameters
"""
filename = parameters['filename']
filename = self.parameters['filename']
table = atpy.Table(filename, verbose=False)
time_column_name = table.columns.keys[0]
time_grid = table[time_column_name]
# -1 because Python indexes start to 0.
sfr_column_number = int(parameters['sfr_column']) - 1
sfr_column_number = int(self.parameters['sfr_column']) - 1
sfr_column_name = table.columns.keys[sfr_column_number]
sfr = table[sfr_column_name]
age = int(parameters['age'])
age = int(self.parameters['age'])
# We cut the SFH to the desired age.
sfr = sfr[time_grid <= age]
......@@ -76,7 +76,7 @@ class Module(common.SEDCreationModule):
# Base name for adding information to the SED.
name = self.name or 'loadfile'
sed.add_module(name, parameters)
sed.add_module(name, self.parameters)
sed.add_info(name + "_sfh", (time_grid, sfr))
sed.add_info(name + "_age", age)
sed.add_info(name + "_sfh_id", sfr_column_name)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment