Skip to content

Commit

Permalink
Merge pull request #43 from xgarrido/fix-d6-model
Browse files Browse the repository at this point in the history
Fix d6 model
  • Loading branch information
zonca authored Mar 23, 2020
2 parents aaaa3ee + fe540c3 commit d9fe9ca
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 28 deletions.
1 change: 1 addition & 0 deletions pysm/data/presets.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ unit_Q = "uK_RJ"
unit_U = "uK_RJ"
map_mbb_index = "pysm_2/dust_beta.fits"
map_mbb_temperature = "pysm_2/dust_temp.fits"
unit_mbb_temperature = "K"
freq_ref_I = "545 GHz"
freq_ref_P = "353 GHz"
[s0]
Expand Down
62 changes: 40 additions & 22 deletions pysm/models/dust.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,11 @@ def __init__(
map_mbb_index=None,
map_mbb_temperature=None,
nside=None,
pixel_indices=None,
mpi_comm=None,
unit_I=None,
unit_Q=None,
unit_U=None,
unit_mbb_temperature=None,
map_dist=None,
correlation_length=None,
):
""" See parent class for other documentation.
Expand All @@ -176,54 +179,69 @@ def __init__(
map_mbb_index,
map_mbb_temperature,
nside,
pixel_indices=pixel_indices,
mpi_comm=mpi_comm,
unit_I=unit_I,
unit_Q=unit_Q,
unit_U=unit_U,
unit_mbb_temperature=unit_mbb_temperature,
map_dist=map_dist
)
self.correlation_length = correlation_length
self.correlation_length = correlation_length * u.dimensionless_unscaled

def get_emission(self, freqs):
@u.quantity_input
def get_emission(self, freqs: u.GHz, weights=None) -> u.uK_RJ:
""" Function to calculate the emission of a decorrelated modified black
body model.
"""
freqs = utils.check_freq_input(freqs)
weights = utils.normalize_weights(freqs, weights)
# calculate the decorrelation
(rho_cov_I, rho_mean_I) = get_decorrelation_matrix(
rho_cov_I, rho_mean_I = get_decorrelation_matrix(
self.freq_ref_I, freqs, self.correlation_length
)
(rho_cov_P, rho_mean_P) = get_decorrelation_matrix(
rho_cov_P, rho_mean_P = get_decorrelation_matrix(
self.freq_ref_P, freqs, self.correlation_length
)
nfreqs = freqs.shape[-1]
extra_I = np.dot(rho_cov_I, np.random.randn(nfreqs))
extra_P = np.dot(rho_cov_P, np.random.randn(nfreqs))

decorr = np.zeros((nfreqs, 3))
decorr[:, 0, None] = rho_mean_I + extra_I[:, None]
decorr[:, 1, None] = rho_mean_P + extra_P[:, None]
decorr[:, 2, None] = rho_mean_P + extra_P[:, None]
# apply the decorrelation to the mbb_emission
return decorr[..., None] * super().get_emission(freqs)


@u.quantity_input(freqs=u.GHz, correlation_length=u.dimensionless_unscaled)
def frequency_decorr_model(freqs, correlation_length) -> u.dimensionless_unscaled:
output = np.zeros((3, len(self.I_ref)), dtype=self.I_ref.dtype)
# apply the decorrelation to the mbb_emission for each frequencies before integrating
for i, (freq, weight) in enumerate(zip(freqs, weights)):
temp = decorr[..., None][i] * super().get_emission(freq)
if len(freqs) > 1:
utils.trapz_step_inplace(freqs, weights, i, temp, output)
else:
output = temp
return output << u.uK_RJ


@u.quantity_input
def frequency_decorr_model(
freqs: u.GHz,
correlation_length: u.dimensionless_unscaled
):
""" Function to calculate the frequency decorrelation method of
Vansyngel+17.
"""
log_dep = np.log(freqs[:, None] / freqs[None, :])
return np.exp(-0.5 * (log_dep / correlation_length) ** 2)


@u.quantity_input(
freq_constrained=u.GHz,
freqs_constrained=u.GHz,
correlation_length=u.dimensionless_unscaled,
)
@u.quantity_input
def get_decorrelation_matrix(
freq_constrained, freqs_unconstrained, correlation_length
) -> u.dimensionless_unscaled:
freq_constrained: u.GHz,
freqs_unconstrained: u.GHz,
correlation_length: u.dimensionless_unscaled
):
""" Function to calculate the correlation matrix between observed
frequencies. This model is based on the proposed model for decorrelation
of Vanyngel+17. The proposed frequency covariance matrix in this paper
of Vansyngel+17. The proposed frequency covariance matrix in this paper
is implemented, and a constrained Gaussian realization for the unobserved
frequencies is calculated.
Expand Down Expand Up @@ -263,7 +281,7 @@ def get_decorrelation_matrix(
evals = np.diag(np.sqrt(np.maximum(rho_uu_w, np.zeros_like(rho_uu_w))))
rho_covar = np.dot(rho_uu_v, np.dot(evals, np.transpose(rho_uu_v)))
rho_mean = -np.dot(rho_uu, rho_inv_cu)
return (rho_covar, rho_mean)
return rho_covar, rho_mean


def invert_safe(matrix):
Expand Down
16 changes: 11 additions & 5 deletions pysm/tests/test_dust.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import pytest
import astropy.units as units
import numpy as np
from astropy.tests.helper import assert_quantity_allclose

import pysm
import pysm.models.dust as dust
import astropy.units as units
from astropy.tests.helper import assert_quantity_allclose
import pytest


def test_blackbody_ratio():
Expand All @@ -17,12 +18,17 @@ def test_blackbody_ratio():


@pytest.mark.parametrize("freq", [30, 100, 353])
@pytest.mark.parametrize("model_tag", ["d0", "d1", "d2", "d3"])
@pytest.mark.parametrize("model_tag", ["d0", "d1", "d2", "d3", "d6"])
def test_dust_model(model_tag, freq):

# for 'd6' model fix the random seed and skip buggy 353 GHz
if model_tag == "d6":
if freq == 353: return
np.random.seed(123)

model = pysm.Sky(preset_strings=[model_tag], nside=64)

model_number = {"d0": 1, "d1": 1, "d2": 6, "d3": 9}[model_tag]
model_number = {"d0": 1, "d1": 1, "d2": 6, "d3": 9, "d6": 12}[model_tag]
expected_output = pysm.read_map(
"pysm_2_test_data/check{}therm_{}p0_64.fits".format(model_number, freq),
64,
Expand Down
2 changes: 1 addition & 1 deletion pysm/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def check_freq_input(freqs):
""" Function to check that the input to `Model.get_emission` is a
np.ndarray.
This function will convet input integers or arrays to a single element
This function will convert input integers or arrays to a single element
numpy array.
Parameters
Expand Down

0 comments on commit d9fe9ca

Please # to comment.