Commit 3e86383c authored by Yannick Roehlly's avatar Yannick Roehlly
Browse files

Move common.py contents to the module packages

For the creation and analysis modules, move the contents of the
common.py files to the __init__.py.
parent b517d0bb
......@@ -7,7 +7,7 @@ __version__ = "0.1-alpha"
import argparse
from .session.configuration import Configuration
from .analysis_modules.common import get_module as get_analysis_module
from .analysis_modules import get_module as get_analysis_module
def init(config):
......
# -*- coding: utf-8 -*-
# Copyright (C) 2012 Centre de données Astrophysiques de Marseille
# Copyright (C) 2012, 2013 Centre de données Astrophysiques de Marseille
# Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt
# Author: Yannick Roehlly <yannick.roehlly@oamp.fr>
from importlib import import_module
from collections import OrderedDict
class AnalysisModule(object):
"""Abstract class, the pCigale analysis modules are based on.
"""
# parameter_list is a ordered dictionary containing all the parameters
# used by the module. Each parameter name is associate to a tuple
# (variable type, description [string], default value). Each module must
# define its parameter list, unless it does not use any parameter. Using
# None means that there is no description, unit or default value. If None
# should be the default value, use the 'None' string instead.
parameter_list = OrderedDict()
def __init__(self, **kwargs):
"""Instantiate a analysis module
The module parameters values can be passed as keyword parameters.
"""
# parameters is a dictionary containing the actual values for each
# module parameter.
self.parameters = kwargs
def _process(self, data_file, column_list, creation_modules,
creation_modules_params, redshift_module,
redshift_configuration, parameters):
"""Do the actual analysis
This method is responsible for the fitting / analysis process
and must be implemented by each real module.
Parameters
----------
data_file : string
Name of the file containing the observations to be fitted.
column_list : array of strings
Names of the columns from the data file to use in the analysis.
creation_modules : array of strings
Names (in the right order) of the modules to use to build the SED.
creation_modules_params : array of array of dictionaries
Array containing all the possible combinations of configurations
for the creation_modules. Each 'inner' array has the same length as
the creation_modules array and contains the configuration dictionary
for the corresponding module.
redshift_module : string
Name of the module used to redshift the SED.
redshift_configuration : dictionary
Configuration dictionary for the module used to redshift the SED.
parameters : dictionary
Configuration for the module.
Returns
-------
The process results are saved to disk by the analysis module.
"""
raise NotImplementedError()
def process(self, data_file, column_list, creation_modules,
creation_modules_params, redshift_module,
redshift_configuration, parameters):
"""Process with the analysis
This method is responsible for checking the module parameters before
doing the actual processing (_process method). If a parameter is not
given but exists in the parameter_list with a default value, this
value is used.
Parameters
----------
data_file : string
Name of the file containing the observations to be fitted.
column_list : array of strings
Names of the columns from the data file to use in the analysis.
creation_modules : array of strings
Names (in the right order) of the modules to use to build the SED.
creation_modules_params : array of array of dictionaries
Array containing all the possible combinations of configurations
for the creation_modules. Each 'inner' array has the same length as
the creation_modules array and contains the configuration
dictionary for the corresponding module.
redshift_module : string
Name of the module used to redshift the SED.
redshift_configuration : dictionary
Configuration dictionary for the module used to redshift the SED.
parameters : dictionary
Configuration for the module.
Returns
-------
The process results are saved to disk by the analysis module
Raises
------
KeyError : when not all the needed parameters are given.
"""
# For parameters that are present on the parameter_list with a default
# value and that are not in the parameters dictionary, we add them
# with their default value.
for key in self.parameter_list:
if (not key in parameters) and (
self.parameter_list[key][2] is not None):
parameters[key] = self.parameter_list[key][2]
# If the keys of the parameters dictionary are different from the one
# of the parameter_list dictionary, we raises a KeyError. That means
# that a parameter is missing (and has no default value) or that an
# unexpected one was given.
if not set(parameters.keys()) == set(self.parameter_list.keys()):
missing_parameters = (set(self.parameter_list.keys())
- set(parameters.keys()))
unexpected_parameters = (set(parameters.keys())
- set(self.parameter_list.keys()))
message = ""
if missing_parameters:
message += ("Missing parameters: " +
", ".join(missing_parameters) +
".")
if unexpected_parameters:
message += ("Unexpected parameters: " +
", ".join(unexpected_parameters) +
".")
raise KeyError("The parameters passed are different from the "
"expected one." + message)
#We do the actual processing
self._process(data_file, column_list, creation_modules,
creation_modules_params, redshift_module,
redshift_configuration, parameters)
def get_module(module_name):
"""Return the main class of the module provided
Parameters
----------
module_name : string
The name of the module we want to get the class.
Returns
-------
module_class : class
"""
try:
module = import_module('.' + module_name, 'pcigale.analysis_modules')
return module.Module()
except ImportError:
print('Module ' + module_name + ' does not exists!')
raise
# -*- coding: utf-8 -*-
# Copyright (C) 2012, 2013 Centre de données Astrophysiques de Marseille
# Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt
# Author: Yannick Roehlly <yannick.roehlly@oamp.fr>
from importlib import import_module
from collections import OrderedDict
class AnalysisModule(object):
"""Abstract class, the pCigale analysis modules are based on.
"""
# parameter_list is a ordered dictionary containing all the parameters
# used by the module. Each parameter name is associate to a tuple
# (variable type, description [string], default value). Each module must
# define its parameter list, unless it does not use any parameter. Using
# None means that there is no description, unit or default value. If None
# should be the default value, use the 'None' string instead.
parameter_list = OrderedDict()
def __init__(self, **kwargs):
"""Instantiate a analysis module
The module parameters values can be passed as keyword parameters.
"""
# parameters is a dictionary containing the actual values for each
# module parameter.
self.parameters = kwargs
def _process(self, data_file, column_list, creation_modules,
creation_modules_params, redshift_module,
redshift_configuration, parameters):
"""Do the actual analysis
This method is responsible for the fitting / analysis process
and must be implemented by each real module.
Parameters
----------
data_file : string
Name of the file containing the observations to be fitted.
column_list : array of strings
Names of the columns from the data file to use in the analysis.
creation_modules : array of strings
Names (in the right order) of the modules to use to build the SED.
creation_modules_params : array of array of dictionaries
Array containing all the possible combinations of configurations
for the creation_modules. Each 'inner' array has the same length as
the creation_modules array and contains the configuration dictionary
for the corresponding module.
redshift_module : string
Name of the module used to redshift the SED.
redshift_configuration : dictionary
Configuration dictionary for the module used to redshift the SED.
parameters : dictionary
Configuration for the module.
Returns
-------
The process results are saved to disk by the analysis module.
"""
raise NotImplementedError()
def process(self, data_file, column_list, creation_modules,
creation_modules_params, redshift_module,
redshift_configuration, parameters):
"""Process with the analysis
This method is responsible for checking the module parameters before
doing the actual processing (_process method). If a parameter is not
given but exists in the parameter_list with a default value, this
value is used.
Parameters
----------
data_file : string
Name of the file containing the observations to be fitted.
column_list : array of strings
Names of the columns from the data file to use in the analysis.
creation_modules : array of strings
Names (in the right order) of the modules to use to build the SED.
creation_modules_params : array of array of dictionaries
Array containing all the possible combinations of configurations
for the creation_modules. Each 'inner' array has the same length as
the creation_modules array and contains the configuration
dictionary for the corresponding module.
redshift_module : string
Name of the module used to redshift the SED.
redshift_configuration : dictionary
Configuration dictionary for the module used to redshift the SED.
parameters : dictionary
Configuration for the module.
Returns
-------
The process results are saved to disk by the analysis module
Raises
------
KeyError : when not all the needed parameters are given.
"""
# For parameters that are present on the parameter_list with a default
# value and that are not in the parameters dictionary, we add them
# with their default value.
for key in self.parameter_list:
if (not key in parameters) and (
self.parameter_list[key][2] is not None):
parameters[key] = self.parameter_list[key][2]
# If the keys of the parameters dictionary are different from the one
# of the parameter_list dictionary, we raises a KeyError. That means
# that a parameter is missing (and has no default value) or that an
# unexpected one was given.
if not set(parameters.keys()) == set(self.parameter_list.keys()):
missing_parameters = (set(self.parameter_list.keys())
- set(parameters.keys()))
unexpected_parameters = (set(parameters.keys())
- set(self.parameter_list.keys()))
message = ""
if missing_parameters:
message += ("Missing parameters: " +
", ".join(missing_parameters) +
".")
if unexpected_parameters:
message += ("Unexpected parameters: " +
", ".join(unexpected_parameters) +
".")
raise KeyError("The parameters passed are different from the "
"expected one." + message)
#We do the actual processing
self._process(data_file, column_list, creation_modules,
creation_modules_params, redshift_module,
redshift_configuration, parameters)
def get_module(module_name):
"""Return the main class of the module provided
Parameters
----------
module_name : string
The name of the module we want to get the class.
Returns
-------
module_class : class
"""
try:
module = import_module('.' + module_name, 'pcigale.analysis_modules')
return module.Module()
except ImportError:
print('Module ' + module_name + ' does not exists!')
raise
......@@ -28,9 +28,9 @@ from copy import deepcopy
from scipy import stats
from progressbar import ProgressBar
from matplotlib import pyplot as plt
from . import common
from . import AnalysisModule
from ..warehouse import SedWarehouse
from ..creation_modules.common import get_module
from ..creation_modules import get_module
from ..data import Database
......@@ -45,7 +45,7 @@ PLOT_L_MIN = 91
PLOT_L_MAX = 1e6
class Module(common.AnalysisModule):
class Module(AnalysisModule):
"""psum analysis
TODO: Description of the PSUM method.
......
......@@ -18,12 +18,12 @@ from collections import OrderedDict
from datetime import datetime
from astropy.table import Table
from progressbar import ProgressBar
from . import common
from . import AnalysisModule
from ..warehouse import SedWarehouse
from ..data import Database
class Module(common.AnalysisModule):
class Module(AnalysisModule):
"""
This module saves a table containing all the parameters and desired fluxes
for all the computed models.
......
# -*- coding: utf-8 -*-
# Copyright (C) 2012 Centre de données Astrophysiques de Marseille
# Copyright (C) 2012, 2013 Centre de données Astrophysiques de Marseille
# Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt
# Author: Yannick Roehlly <yannick.roehlly@oamp.fr>
from .common import *
import os
import inspect
from importlib import import_module
from collections import OrderedDict
def complete_parameters(given_parameters, parameter_list):
"""Complete the given parameter list with the default values
Complete the given_parameters dictionary with missing parameters that have
a default value in the parameter_list. If a parameter from parameter_list
have no default value and is not present in given_parameters, raises an
error. If a parameter is present in given_parameters and not in
parameter_list, an exception is also raised.
Returns an ordered dictionary with the same key order as the parameter
list.
Parameters
----------
given_parameters : dictionary
Parameter dictionary used to configure the module.
parameter_list : OrderedDict
Parameter list from the module.
Returns
-------
parameters : OrderedDict
Ordered dictionary combining the given parameters with the default
values for the missing ones.
Raises
------
KeyError when the given parameters are different from the expected ones.
"""
# Complete the given parameters with default values when needed.
for key in parameter_list:
if (not key in given_parameters) and (
parameter_list[key][2] is not None):
given_parameters[key] = parameter_list[key][2]
# Check parameter consistency between the parameter list and the given
# parameters.
if not set(given_parameters.keys()) == set(parameter_list.keys()):
missing_parameters = (set(parameter_list.keys())
- set(given_parameters.keys()))
unexpected_parameters = (set(given_parameters.keys())
- set(parameter_list.keys()))
message = ""
if missing_parameters:
message += ("Missing parameters: " +
", ".join(missing_parameters) +
". ")
if unexpected_parameters:
message += ("Unexpected parameters: " +
", ".join(unexpected_parameters) +
". ")
raise KeyError("The parameters passed are different from the "
"expected one. " + message)
# We want the result to be ordered as the parameter_list of the module is.
result = OrderedDict()
for key in parameter_list.keys():
result[key] = given_parameters[key]
return result
class SEDCreationModule(object):
"""Abstract class, the pCigale SED creation modules are based on.
"""
# parameter_list is an ordered dictionary containing all the parameters
# used by the module. Each parameter name is associate to a tuple
# (variable type, description [string], default value). Each module must
# define its parameter list, unless it does not use any parameter. Using
# None means that there is no description or default value. If None should
# be the default value, use the 'None' string instead.
parameter_list = OrderedDict()
# out_parameter_list is an ordered dictionary containing all the SED
# parameters that are added to the SED info dictionary and for which a
# statistical analysis may be done. Each parameter name is associated with
# its description. In the SED info dictionary, the parameter name in
# is postfixed with a same postfix used in the module name, to allow
# the use of repeated modules.
out_parameter_list = OrderedDict()
# comments is the text that is used to comment the module section in
# the configuration file. For instance, it can be used to give special
# instructions for the configuration.
comments = ""
def __init__(self, name=None, blank=False, **kwargs):
"""Instantiate a SED creation module
A name can be given to the module. This can be useful when a same
module is used several times with different parameters in the SED
creation process.
The module parameters must be passed as keyword parameters. If a
parameter is not given but exists in the parameter_list with a default
value, this value is used. If a parameter is missing or if an
unexpected parameter is given, an error will be raised.
Parameters
----------
name : string
Name of the module.
blank : boolean
If true, return a non-parametrised module that will be used only
to query the module parameter list.
The module parameters must be given as keyword parameters.
Raises
------
KeyError : when not all the needed parameters are given or when an
unexpected parameter is given.
"""
# If a name is not given, we take if from the file in which the
# module class is coded.
self.name = name or os.path.basename(inspect.getfile(self))[:4]
# We want to postfix the various keys of the SED with the same
# postfix as the module name, if any.
if '.' in name:
self.postfix = "." + name.split(".", 1)
else:
self.postfix = ""
if not blank:
# Parameters given in constructor.
parameters = kwargs
# Complete the parameter dictionary and "export" it to the module
self.parameters = complete_parameters(parameters,
self.parameter_list)
# Run the initialisation code specific to the module.
self._init_code()
def _init_code(self):
"""Initialisation code specific to the module.
For instance, a module taking data in the database can use this method
to do so, only one time when the module instantiates.
"""
pass
def process(self, sed):
"""Process a SED object with the module
The SED object is updated during the process, one must take care of
copying it before, if needed.
Parameters
----------
sed : pcigale.sed.SED object
"""
raise NotImplementedError()
def get_module(name, **kwargs):
"""Get a SED creation module from its name
Parameters
----------
module_name : string
The name of the module we want to get the class. This name can be
prefixed by anything using a dot, then the part before the dot is
used to determine the module to load (e.g. 'dh2002.1' will return
the 'dh2002' module).
Returns
-------
a pcigale.creation_modules.Module instance
"""
# Determine the real module name by removing the dotted prefix.
module_name = name.split('.')[0]
try:
module = import_module("." + module_name, 'pcigale.creation_modules')
return module.Module(name=name, **kwargs)
except ImportError:
print('Module ' + module_name + ' does not exists!')
raise
......@@ -5,11 +5,11 @@
import numpy as np
from collections import OrderedDict
from . import common
from . import SEDCreationModule
from ..data import Database
class Module(common.SEDCreationModule):
class Module(SEDCreationModule):
"""Module computing the Star Formation History contribution bases on the
Bruzual and Charlot (2003) models.
"""
......
......@@ -6,10 +6,10 @@
import numpy as np
import scipy.constants as cst
from collections import OrderedDict
from . import common
from . import SEDCreationModule
class Module(common.SEDCreationModule):
class Module(SEDCreationModule):
"""
Module computing the infra-red re-emission corresponding to an amount of
attenuation using the Casey (2012) model.
......
# -*- coding: utf-8 -*-
# Copyright (C) 2012, 2013 Centre de données Astrophysiques de Marseille
# Licensed under the CeCILL-v2 licence - see Licence_CeCILL_V2-en.txt
# Author: Yannick Roehlly <yannick.roehlly@oamp.fr>
import os
import inspect
from importlib import import_module
from collections import OrderedDict
def complete_parameters(given_parameters, parameter_list):
"""Complete the given parameter list with the default values
Complete the given_parameters dictionary with missing parameters that have
a default value in the parameter_list. If a parameter from parameter_list
have no default value and is not present in given_parameters, raises an
error. If a parameter is present in given_parameters and not in
parameter_list, an exception is also raised.
Returns an ordered dictionary with the same key order as the parameter
list.
Parameters
----------
given_parameters : dictionary