Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft module level defaults #35

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion fgspectra/frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import numpy as np
from scipy import constants
from .model import Model
from functools import wraps


T_CMB = 2.72548
Expand Down
22 changes: 20 additions & 2 deletions fgspectra/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import inspect
import yaml
import numpy as np
import importlib

class Model(ABC):
""" Abstract class for model definition
Expand All @@ -13,11 +14,28 @@ class Model(ABC):

If the `eval` method of a hypotetical ``Child`` class calls the `eval`
method of other `Model`s, it is likely that ``Child`` has also to override
`set_defaults`, `defaults` and `_get_repr`
`set_defaults`, `defaults` and `_get_repr`.
Note also that if you override `__init__`, you may want to allow to set the
defaults at construction time, including the module-level defaults
"""

def __init__(self, **kwargs):
""" You can set defaults at construction time """
"""You can set defaults at construction time using
1) the keyword arguments passed at construction time
2) if existing, the `defaults` dictionary defined globally in the module
of the class being constructed

1) overrides 2)
"""
self.set_defaults_init(**kwargs)

def set_defaults_init(self, **kwargs):
# As set_defaults, but also considers module-level defaults, if existing
try:
module_defaults = importlib.import_module(self.__module__).defaults
kwargs = {**module_defaults, **kwargs}
except AttributeError:
pass
self.set_defaults(**kwargs)

def set_defaults(self, **kwargs):
Expand Down
10 changes: 9 additions & 1 deletion fgspectra/power.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def __init__(self, filenames, **kwargs):
mode='constant', constant_values=0)

self._cl[i+(ell,)] = spec
self.set_defaults(**kwargs)
self.set_defaults_init(**kwargs)

def eval(self, ell=None, ell_0=None, amp=1.0):
"""Compute the power spectrum with the given ell and parameters."""
Expand Down Expand Up @@ -192,6 +192,10 @@ def __init__(self, *power_spectra, **kwargs):
self.n_comp = np.rint(-1 + np.sqrt(1 + 8 * len(power_spectra))) // 2
self.n_comp = int(self.n_comp)
assert (self.n_comp + 1) * self.n_comp // 2 == len(power_spectra)
try:
kwargs = {**defaults, **kwargs}
except NameError:
pass
self.set_defaults(**kwargs)

def set_defaults(self, **kwargs):
Expand Down Expand Up @@ -275,6 +279,10 @@ def __init__(self, *power_spectra, **kwargs):
self.n_comp = np.rint(-1 + np.sqrt(1 + 8 * len(power_spectra))) // 2
self.n_comp = int(self.n_comp)
assert (self.n_comp + 1) * self.n_comp // 2 == len(power_spectra)
try:
kwargs = {**defaults, **kwargs}
except NameError:
pass
self.set_defaults(**kwargs)

def set_defaults(self, **kwargs):
Expand Down
21 changes: 10 additions & 11 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def test_ksz():
assert fgp.kSZ_bat() is not None

def test_ACT_models():
fgp.defaults = {'ell': np.array([2000])}
# define the models from fgspectra
ksz = fgc.FactorizedCrossSpectrum(fgf.ConstantSED(), fgp.kSZ_bat())
cibp = fgc.FactorizedCrossSpectrum(fgf.ModifiedBlackBody(), fgp.PowerLaw())
Expand All @@ -25,8 +26,6 @@ def test_ACT_models():
tsz = fgc.FactorizedCrossSpectrum(fgf.ThermalSZ(), fgp.tSZ_150_bat())
cibc = fgc.FactorizedCrossSpectrum(fgf.CIB(), fgp.PowerLaw())

ells = np.array([2000])


par = {
'nu_0' : 150.0,
Expand Down Expand Up @@ -58,39 +57,39 @@ def test_ACT_models():

par['a_tSZ'] * tsz(
{'nu':fsz, 'nu_0': par['nu_0']},
{'ell':ells, 'ell_0':par['ell_0']}) ,
{'ell_0':par['ell_0']}) ,
par['a_kSZ'] * ksz(
{'nu':fsz},
{'ell':ells, 'ell_0':par['ell_0']}) ,
{'ell_0':par['ell_0']}) ,

par['a_p'] * cibp(
{'nu': fdust, 'nu_0':par['nu_0'], 'temp':par['T_d'], 'beta':par['beta_p']},
{'ell':ells, 'ell_0':par['ell_0'], 'alpha':2}),
{'ell_0':par['ell_0'], 'alpha':2}),

par['a_c'] * cibc(
{'nu': fdust, 'nu_0':par['nu_0'], 'temp':par['T_d'], 'beta':par['beta_c']},
{'ell':ells, 'ell_0':par['ell_0'], 'alpha':2 - par['n_CIBC']}),
{'ell_0':par['ell_0'], 'alpha':2 - par['n_CIBC']}),

tSZ_and_CIB(
{'kwseq': (
{'nu':fsz, 'nu_0':par['nu_0']},
{'nu': fdust, 'nu_0':par['nu_0'], 'temp':par['T_d'], 'beta':par['beta_c']}
)},
{'kwseq': (
{'ell':ells, 'ell_0':par['ell_0'],
{'ell_0':par['ell_0'],
'amp':par['a_tSZ']},
{'ell':ells, 'ell_0':par['ell_0'],
{'ell_0':par['ell_0'],
'alpha':2-par['n_CIBC'], 'amp':par['a_c']},
{'ell':ells, 'ell_0':par['ell_0'],
{'ell_0':par['ell_0'],
'amp': -par['xi'] * np.sqrt(par['a_tSZ'] * par['a_c'])}
)}),

par['a_s'] * radio(
{'nu': fsynch, 'nu_0':par['nu_0'], 'beta':-0.5 - 2},
{'ell':ells, 'ell_0':par['ell_0'], 'alpha':2}) ,
{'ell_0':par['ell_0'], 'alpha':2}) ,
par['a_g'] * cirrus(
{'nu': fdust, 'nu_0':par['nu_0'], 'beta':3.8 - 2},
{'ell':ells, 'ell_0':par['ell_0'], 'alpha':-0.7})
{'ell_0':par['ell_0'], 'alpha':-0.7})
)

"""
Expand Down