Commit d9281b94 authored by Médéric Boquien's avatar Médéric Boquien

Store the best results in a dictionary of SharedArray.

parent 6807a5e9
......@@ -278,15 +278,18 @@ def bestfit(oidx, obs):
corr_dz = compute_corr_dz(obs.redshift, obs.distance)
_, scaling = compute_chi2(fluxes[:, None], intprops[:, None],
extprops[:, None], obs, corr_dz,
gbl_conf['analysis_params']['lim_flag'])
gbl_results.best.properties[oidx, :] = [sed.info[k] for k in
gbl_results.best.propertiesnames]
iprop = [i for i, k in enumerate(gbl_results.best.propertiesnames)
if k in gbl_results.best.massproportional]
gbl_results.best.properties[oidx, iprop] *= scaling * corr_dz
gbl_results.best.fluxes[oidx, :] = fluxes * scaling
extprops[:, None], obs, corr_dz,
gbl_conf['analysis_params']['lim_flag'])
for band in gbl_results.best.flux:
gbl_results.best.flux[band].data[oidx] = sed.compute_fnu(band) * scaling
for prop in gbl_results.best.intprop:
gbl_results.best.intprop[prop].data[oidx] = sed.info[prop]
for prop in gbl_results.best.extprop:
gbl_results.best.extprop[prop].data[oidx] = sed.info[prop] * scaling \
* corr_dz
if gbl_conf['analysis_params']["save_best_sed"]:
sed.to_fits('out/{}'.format(obs.id), scaling)
......
......@@ -150,63 +150,60 @@ class BestResultsManager(object):
def __init__(self, models):
self.obs = models.obs
self.nbands = len(models.obs.bands)
self.nobs = len(models.obs)
self.nintprops = len(models.obs.intprops)
self.nextprops = len(models.obs.extprops)
self.propertiesnames = models.allpropertiesnames
self.massproportional = models.massproportional
self.nproperties = len(models.allpropertiesnames)
self._fluxes_shape = (self.nobs, self.nbands)
self._intprops_shape = (self.nobs, self.nintprops)
self._extprops_shape = (self.nobs, self.nextprops)
self._properties_shape = (self.nobs, self.nproperties)
nobs = len(models.obs)
# Arrays where we store the data related to the models. For memory
# efficiency reasons, we use RawArrays that will be passed in argument
# to the pool. Each worker will fill a part of the RawArrays. It is
# important that there is no conflict and that two different workers do
# not write on the same section.
self._fluxes = SharedArray(self._fluxes_shape)
self._intprops = SharedArray(self._intprops_shape)
self._extprops = SharedArray(self._extprops_shape)
self._properties = SharedArray(self._properties_shape)
self._chi2 = SharedArray(self.nobs)
self.flux = {band: SharedArray(nobs) for band in models.obs.bands}
allintpropnames = set(models.allpropertiesnames) - models.massproportional
allextpropnames = set(models.allpropertiesnames) - allintpropnames
self.intprop = {prop: SharedArray(nobs)
for prop in allintpropnames}
self.extprop = {prop: SharedArray(nobs)
for prop in allextpropnames}
self.chi2 = SharedArray(nobs)
self.scaling = SharedArray(nobs)
# We store the index as a float to work around python issue #10746
self._index = SharedArray(self.nobs)
self.index = SharedArray(nobs)
@property
def fluxes(self):
def flux(self):
"""Returns a shared array containing the fluxes of the best fit for
each observation.
"""
return self._fluxes.data
return self._flux
@flux.setter
def flux(self, flux):
self._flux = flux
@property
def intprops(self):
def intprop(self):
"""Returns a shared array containing the fluxes of the best fit for
each observation.
"""
return self._intprops.data
return self._intprop
@intprop.setter
def intprop(self, intprop):
self._intprop = intprop
@property
def extprops(self):
def extprop(self):
"""Returns a shared array containing the fluxes of the best fit for
each observation.
"""
return self._extprops.data
return self._extprop
@property
def properties(self):
"""Returns a shared array containing the physical properties of the
best fit for each observation.
"""
return self._properties.data
@extprop.setter
def extprop(self, extprop):
self._extprop = extprop
@property
def chi2(self):
......@@ -216,6 +213,10 @@ class BestResultsManager(object):
"""
return self._chi2.data
@chi2.setter
def chi2(self, chi2):
self._chi2 = chi2
@property
def index(self):
"""Returns a shared array containing the index of the best fit for each
......@@ -224,6 +225,10 @@ class BestResultsManager(object):
"""
return self._index.data
@index.setter
def index(self, index):
self._index = index
@staticmethod
def merge(results):
"""Merge a list of partial results computed on individual blocks of
......@@ -243,23 +248,21 @@ class BestResultsManager(object):
if len(results) == 1:
return results[0]
fluxes = np.array([result.fluxes for result in results])
properties = np.array([result.properties for result in results])
chi2 = np.array([result.chi2 for result in results])
index = np.array([result.index for result in results])
best = np.argmin([result.chi2 for result in results], axis=0)
merged = results[0]
merged._fluxes = SharedArray((merged.nobs, merged.nbands))
merged._properties = SharedArray((merged.nobs, merged.nproperties))
merged._chi2 = SharedArray(merged.nobs)
# We store the index as a float to work around python issue #10746
merged._index = SharedArray(merged.nobs)
for iobs, bestidx in enumerate(np.argmin(chi2, axis=0)):
merged.fluxes[iobs, :] = fluxes[bestidx, iobs, :]
merged.properties[iobs, :] = properties[bestidx, iobs, :]
merged.chi2[iobs] = chi2[bestidx, iobs]
merged.index[iobs] = index[bestidx, iobs]
for iobs, bestidx in enumerate(best):
for band in merged.flux:
merged.flux[band].data[iobs] = \
results[bestidx].flux[band].data[iobs]
for prop in merged.intprop:
merged.intprop[prop].data[iobs] = \
results[bestidx].intprop[prop].data[iobs]
for prop in merged.extprop:
merged.extprop[prop].data[iobs] = \
results[bestidx].extprop[prop].data[iobs]
merged.chi2[iobs] = results[bestidx].chi2[iobs]
merged.index[iobs] = results[bestidx].index[iobs]
return merged
......@@ -344,13 +347,17 @@ class ResultsManager(object):
table.add_column(Column(self.best.chi2 / (nobs - 1),
name="best.reduced_chi_square"))
for idx, name in enumerate(self.best.propertiesnames):
table.add_column(Column(self.best.properties[:, idx],
name="best."+name))
for prop in sorted(self.best.intprop):
table.add_column(Column(self.best.intprop[prop].data,
name="best."+prop))
for prop in sorted(self.best.extprop):
table.add_column(Column(self.best.extprop[prop].data,
name="best."+prop))
for band in self.obs.bands:
table.add_column(Column(self.best.flux[band].data,
name="best."+band, unit='mJy'))
for idx, name in enumerate(self.obs.bands):
table.add_column(Column(self.best.fluxes[:, idx],
name="best."+name, unit='mJy'))
table.write("out/{}.txt".format(filename), format='ascii.fixed_width',
delimiter=None)
......
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