Commit beade2fa authored by Médéric Boquien's avatar Médéric Boquien
Browse files

Add a models manager that stores the fluxes, the physical properties, and all...

Add a models manager that stores the fluxes, the physical properties, and all the necessary information to compute the models. Actual use will come in forthcoming patches.
parent 95dcbb78
# -*- coding: utf-8 -*-
# Copyright (C) 2017 Universidad de Antofagasta
# Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt
# Author: Médéric Boquien
"""This class manages the handling of models in the code. It contains the
fluxes and the physical properties and all the necessary information to
compute them, such as the configuration, the observations, and the parameters
of the models.
import ctypes
from multiprocessing.sharedctypes import RawArray
from astropy.table import Table, Column
import numpy as np
from ..warehouse import SedWarehouse
class ModelsManager(object):
"""A ModelsManager contains the fluxes and the properties of all the
models. In addition it contains all the information to understand how the
models have been computed, what block of the grid of models they correspond
to with the ability to easily recompute a model, what the bands are, what
are the names of the properties, etc.
def __init__(self, conf, obs, params, iblock=0):
self.conf = conf
self.obs = obs
self.params = params
self.block = params.blocks[iblock]
self.propertiesnames = conf['analysis_params']['variables']
self.allpropertiesnames, self.massproportional = self._get_info()
self._fluxes_shape = (len(obs.bands), len(self.block))
self._props_shape = (len(self.propertiesnames), len(self.block))
# 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 = self._shared_array(self._fluxes_shape)
self._properties = self._shared_array(self._props_shape)
def fluxes(self):
"""Returns a shared array containing the fluxes of the models.
return np.ctypeslib.as_array(self._fluxes).reshape(self._fluxes_shape)
def properties(self):
"""Returns a shared array containing the properties of the models.
return np.ctypeslib.as_array(self._properties).reshape(self._props_shape)
def _get_info(self):
warehouse = SedWarehouse()
sed = warehouse.get_sed(self.conf['sed_modules'],
info = list(
return (info, sed.mass_proportional_info)
def save(self, filename):
"""Save the fluxes and properties of all the models into a table.
filename: str
Root of the filename where to save the data.
table = Table(np.vstack((self.fluxes,,
names=self.obs.bands + self.propertiesnames)
table.add_column(Column(np.arange(len(table)), name='id'), index=0)
table.write("out/{}.txt".format(filename), format='ascii.fixed_width',
def _shared_array(shape):
return RawArray(ctypes.c_double, int(np.product(shape)))
Supports Markdown
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