Commit 3ace8b67 authored by Médéric Boquien's avatar Médéric Boquien

Implement an _init_code function so the initialisation of the module is...

Implement an _init_code function so the initialisation of the module is separated from the processing of the SED. Because the initialisation can be slow, this should yield a speed-up when applying the module on different SED.
parent 2b42fef0
......@@ -25,6 +25,7 @@
### Optimised
- Prior to version 0.7.0, we needed to maintain the list of redshifts for all the computed models. Past 0.7.0 we just infer the redshift from a list unique redshifts. This means that we can now discard the list of redshifts for all the models and only keep the list of unique redshifts. This saves ~8 MB of memory for every 10⁶ models. the models should be computed slightly faster but it is in the measurement noise. (Médéric Boquien)
- The sfhfromfile module is now fully initialised when it is instantiated rather than doing so when processing the SED. This should be especially sensitive when processing different SED. (Médéric Boquien)
## 0.8.1 (2015-12-07)
### Fixed
......
......@@ -34,7 +34,7 @@ class SfhFromFile(CreationModule):
("filename", (
"str",
"Name of the file containing the SFH. The first column must be "
"the time in Myr, starting from 0 with a step of 1 Myr. The other
"the time in Myr, starting from 0 with a step of 1 Myr. The other "
"columns must contain the SFR in Msun/yr."
"[Msun/yr].",
None
......@@ -57,47 +57,46 @@ class SfhFromFile(CreationModule):
))
])
def process(self, sed):
"""Add the SFH read from the file.
Parameters
----------
sed: pcigale.sed.SED object
parameters: dictionary containing the parameters
"""
def _init_code(self):
filename = self.parameters['filename']
table = read_table(filename)
time_grid = table.columns[0].data.astype(np.int)
normalise = (self.parameters["normalise"].lower() == "true")
age = int(self.parameters['age'])
self.sfr_column_number = int(self.parameters['sfr_column'])
if time_grid[0] != 0:
table = read_table(filename)
self.sfr = table.columns[self.sfr_column_number].data.astype(np.float)
self.time_grid = table.columns[0].data.astype(np.int)
if self.time_grid[0] != 0:
raise Exception("The time grid must start from 0.")
if np.all(time_grid[1:]-time_grid[:-1] == 1) == False:
if np.all(self.time_grid[1:]-self.time_grid[:-1] == 1) == False:
raise Exception("The time step must be 1 Myr. Computed models will "
"be wrong.")
sfr_column_number = int(self.parameters['sfr_column'])
sfr = table.columns[sfr_column_number].data.astype(np.float)
age = int(self.parameters['age'])
normalise = (self.parameters["normalise"].lower() == "true")
# We cut the SFH to the desired age.
sfr = sfr[time_grid <= age]
time_grid = time_grid[time_grid <= age]
self.sfr = self.sfr[self.time_grid <= age]
self.time_grid = self.time_grid[self.time_grid <= age]
# Compute the galaxy mass and normalise the SFH to 1 solar mass
# produced if asked to.
sfr_integrated = np.sum(sfr) * 1e6
self.sfr_integrated = np.sum(self.sfr) * 1e6
if normalise:
sfr /= sfr_integrated
sfr_integrated = 1.
self.sfr /= self.sfr_integrated
self.sfr_integrated = 1.
def process(self, sed):
"""Add the SFH read from the file.
Parameters
----------
sed: pcigale.sed.SED object
parameters: dictionary containing the parameters
"""
sed.add_module(self.name, self.parameters)
sed.sfh = (time_grid, sfr)
sed.add_info("sfh.integrated", sfr_integrated, True)
sed.add_info("sfh.index", sfr_column_number)
sed.sfh = (self.time_grid, self.sfr)
sed.add_info("sfh.integrated", self.sfr_integrated, True)
sed.add_info("sfh.index", self.sfr_column_number)
# CreationModule to be returned by get_module
Module = SfhFromFile
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