def setup_registered_units():
    """
    Function to setup predefined units.
    This should run once when the module
    is imported.
    """
    global FORMATTED_UNITS
    # Load up configurations from yaml file
    with open(DEFAULT_FLUX_UNITS_CONFIGS, 'r') as yamlfile:
        cfg = yaml.load(yamlfile)

    # Define and add new units to astropy
    for new_unit_key in cfg["new_units"]:
        new_unit = cfg["new_units"][new_unit_key]
        try:
            u.Unit(new_unit["name"])
        except ValueError:
            if "base" in new_unit:
                new_astropy_unit = u.def_unit(new_unit["name"], u.Unit(new_unit["base"]))
            else:
                new_astropy_unit = u.def_unit(new_unit["name"])
            register_new_unit(new_astropy_unit)

    new_physical_types = [
        [(u.Jy / u.degree ** 2), 'SFD_over_solid_angle'],
        [(u.Jy / u.pix), 'SFD_over_pix']
    ]

    for model_unit, name in new_physical_types:
        try:
            u.def_physical_type(model_unit, name)
        except ValueError:
            continue

    FORMATTED_UNITS = cfg["formatted_units"]
Ejemplo n.º 2
0
def setup_registered_units():
    """
    Function to setup predefined units.
    This should run once when the module
    is imported.
    """
    global FORMATTED_UNITS
    # Load up configurations from yaml file
    with open(DEFAULT_FLUX_UNITS_CONFIGS, 'r') as yamlfile:
        cfg = yaml.load(yamlfile)

    # Define and add new units to astropy
    for new_unit_key in cfg["new_units"]:
        new_unit = cfg["new_units"][new_unit_key]
        try:
            u.Unit(new_unit["name"])
        except ValueError:
            if "base" in new_unit:
                new_astropy_unit = u.def_unit(new_unit["name"],
                                              u.Unit(new_unit["base"]))
            else:
                new_astropy_unit = u.def_unit(new_unit["name"])
            register_new_unit(new_astropy_unit)

    new_physical_types = [[(u.Jy / u.degree**2), 'SFD_over_solid_angle'],
                          [(u.Jy / u.pix), 'SFD_over_pix']]

    for model_unit, name in new_physical_types:
        try:
            u.def_physical_type(model_unit, name)
        except ValueError:
            continue

    FORMATTED_UNITS = cfg["formatted_units"]
Ejemplo n.º 3
0
 def _define_new_physical_types():
     """
     Two new types of units defined:
         - SFD_over_solid_angle = spectral_flux_density / (degree^2)
         - SFD_over_pix = spectral_flux_density / pixel
     """
     new_physical_types = [
         [(u.Jy / u.degree ** 2), 'SFD_over_solid_angle'],
         [(u.Jy / u.pix), 'SFD_over_pix']
     ]
     for model_unit, name in new_physical_types:
         try:
             u.def_physical_type(model_unit, name)
         except ValueError:
             continue
Ejemplo n.º 4
0
import astropy.units as u
import numpy as np
from astropy import log

from .utils import sed_conversion, validate_data_table

__all__ = [
    "normal_prior",
    "uniform_prior",
    "log_uniform_prior",
    "get_sampler",
    "run_sampler",
]

# Define phsyical types used in plot and utils.validate_data_table
u.def_physical_type(u.erg / u.cm**2 / u.s, "flux")
u.def_physical_type(u.Unit("1/(s cm2 erg)"), "differential flux")
u.def_physical_type(u.Unit("1/(s erg)"), "differential power")
u.def_physical_type(u.Unit("1/TeV"), "differential energy")
u.def_physical_type(u.Unit("1/cm3"), "number density")
u.def_physical_type(u.Unit("1/(eV cm3)"), "differential number density")

# Prior functions


def uniform_prior(value, umin, umax):
    """Uniform prior distribution.
    """
    if umin <= value <= umax:
        return 0.0
    else:
Ejemplo n.º 5
0
import numpy as np
from astropy import units as u
from .. import six

u.def_physical_type(u.cm ** 2 / u.g, 'area per unit mass')
u.def_physical_type(u.erg / u.cm ** 2 / u.s, 'flux')


def validate_physical_type(name, value, physical_type):
    if physical_type is not None:
        if not isinstance(value, u.Quantity):
            raise TypeError("{0} should be given as a Quantity object".format(name))
        if isinstance(physical_type, six.string_types):
            if value.unit.physical_type != physical_type:
                raise TypeError("{0} should be given in units of {1}".format(name, physical_type))
        else:
            if not value.unit.physical_type in physical_type:
                raise TypeError("{0} should be given in units of {1}".format(name, ', '.join(physical_type)))


def validate_scalar(name, value, domain=None, physical_type=None):

    validate_physical_type(name, value, physical_type)

    if not physical_type:
        if np.isscalar(value) or not np.isreal(value):
            raise TypeError("{0} should be a scalar floating point value".format(name))

    if domain == 'positive':
        if value < 0.:
            raise ValueError("{0} should be positive".format(name))
Ejemplo n.º 6
0
# -*- coding: utf-8 -*-
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
import numpy as np
from astropy import log
import astropy
import astropy.units as u

from .utils import validate_data_table, sed_conversion

__all__ = ["normal_prior", "uniform_prior", "get_sampler", "run_sampler"]

# Define phsyical types used in plot and utils.validate_data_table
u.def_physical_type(u.erg / u.cm ** 2 / u.s, 'flux')
u.def_physical_type(u.Unit('1/(s cm2 erg)'), 'differential flux')
u.def_physical_type(u.Unit('1/(s erg)'), 'differential power')
u.def_physical_type(u.Unit('1/TeV'), 'differential energy')
u.def_physical_type(u.Unit('1/cm3'), 'number density')

# Prior functions


def uniform_prior(value, umin, umax):
    """Uniform prior distribution.
    """
    if umin <= value <= umax:
        return 0.0
    else:
        return - np.inf
Ejemplo n.º 7
0
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
import numpy as np
from astropy import log
import astropy.units as u
import warnings

from .utils import validate_data_table, sed_conversion

__all__ = [
    "normal_prior", "uniform_prior", "log_uniform_prior", "get_sampler",
    "run_sampler"
]

# Define phsyical types used in plot and utils.validate_data_table
u.def_physical_type(u.erg / u.cm**2 / u.s, 'flux')
u.def_physical_type(u.Unit('1/(s cm2 erg)'), 'differential flux')
u.def_physical_type(u.Unit('1/(s erg)'), 'differential power')
u.def_physical_type(u.Unit('1/TeV'), 'differential energy')
u.def_physical_type(u.Unit('1/cm3'), 'number density')
u.def_physical_type(u.Unit('1/(eV cm3)'), 'differential number density')

# Prior functions


def uniform_prior(value, umin, umax):
    """Uniform prior distribution.
    """
    if umin <= value <= umax:
        return 0.0
    else: