Skip to content
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
40 changes: 33 additions & 7 deletions sacc/tracer_uncertainty/nz.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class NZLinearUncertainty(
In this case to draw a sample of an n(z) you would generate a vector
of standard normal variables and apply:

n_i -> n_i + mean + matrix @ normal_vector
n_i -> n_i + matrix @ normal_vector
"""

storage_type = ONE_OBJECT_PER_TABLE
Expand All @@ -158,34 +158,60 @@ def __init__(
self,
name,
tracer_names,
mean,
matrix,
):
"""
Initialize the NZLinearUncertainty object.

The mean attribute of the parent class is set to zero, so
it doesn't matter if you use it.

Parameters
----------
tracer_names : list of str
List of tracer names to which the uncertainty applies.

mean: array-like
Intercept of the linear uncertainty model.
Can be interpreted as the mean of the parameters of the model.

matrix: array-like
Linear transformation matrix of the linear uncertainty model.
Includes the Cholesky factorization of the covariance matrix
as well as the linear transformation of the parameters.
"""

mean = np.zeros(len(matrix))

super().__init__(
name,
tracer_names,
mean,
matrix,
linear_transformation_type="linear_model"
)
)

@classmethod
def from_table(cls, table):
"""
Create an instance of one of the NZGaussianTracerUncertainty subclasses
from an astropy table.

This method is inherited by all subclasses, so the cls argument will be the
subclass that calls this method.

Parameters
----------
table: astropy.table.Table
An astropy table containing the uncertainty data.

Returns
-------
instance: NZGaussianTracerUncertainty
An instance of a NZGaussianTracerUncertainty subclass.
"""
# All the subclasses have the same table structure
transformation_type = table.meta["LINEAR_TRANSFORMATION_TYPE"]
cholesky = table[transformation_type]
tracer_names = [table.meta[f"TRACER_NAME_{i}"] for i in range(table.meta["N_TRACERS"])]
name = table.meta["SACCNAME"]
return cls(name, tracer_names, cholesky)


class NZShiftUncertainty(
Expand Down
5 changes: 2 additions & 3 deletions test/test_sacc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,9 +1206,8 @@ def test_nzlinear_uncertainty_saving():
s.add_tracer('NZ', 'source2', z, nz2)

tracer_names = ["source1", "source2"]
mean = [0.0, -0.001]
sigma = [0.01, 0.02]
linear = sacc.NZLinearUncertainty("linear", tracer_names, mean, sigma)
linear = sacc.NZLinearUncertainty("linear", tracer_names, sigma)
s.add_tracer_uncertainty_object(linear)


Expand All @@ -1223,7 +1222,7 @@ def test_nzlinear_uncertainty_saving():
assert linear2.name == "linear"
assert isinstance(linear2, sacc.NZLinearUncertainty)
assert linear2.tracer_names == tracer_names
assert np.allclose(linear2.mean, mean)
assert np.allclose(linear2.mean, 0)
assert np.allclose(linear2.linear_transformation, np.diag(sigma))

def test_equality_mismatched_covariance(filled_sacc):
Expand Down