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

More testing of CosmoSIS module #313

Merged
merged 41 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
b3dc69a
More testing of CosmoSIS module
marcpaterno Aug 28, 2023
813c0c6
Bugfix for and more testing of CosmoSIS module
marcpaterno Aug 29, 2023
d1ecccf
Remove extraneous import
marcpaterno Aug 29, 2023
d0c0346
FIx oddly-formatted concatenated strings
marcpaterno Aug 29, 2023
c0402d1
Specify "utf-8" encoding for all open calls
marcpaterno Aug 29, 2023
c798d58
Improve fixture name
marcpaterno Sep 1, 2023
8e0d578
Test CosmoSIS module with derived parameters
marcpaterno Sep 1, 2023
8a69250
Fix weird string concatenations
marcpaterno Sep 1, 2023
ea01567
Do not insert derived_param0 in data from sampler
marcpaterno Sep 4, 2023
aad088a
Fix documentation
marcpaterno Sep 4, 2023
7ee871d
Add len support for RequiredParameters
marcpaterno Sep 4, 2023
e225bfe
Improve error message on CosmoSIS misconfiguration
marcpaterno Sep 4, 2023
cecbe38
Improve error report due to a missing parameter
marcpaterno Sep 5, 2023
f30773f
Remove extraneous import, fix mypy typing error
marcpaterno Sep 5, 2023
a1c54a7
Improve docstring
marcpaterno Sep 5, 2023
a6d9942
If sacc_tracer is not found, raise an error
marcpaterno Sep 5, 2023
6a14e92
Remove unused import
marcpaterno Sep 5, 2023
9e732b0
Make get_data_vector, compute_theory_vector abstract
marcpaterno Sep 6, 2023
cf90a76
Fix spelling error
marcpaterno Sep 6, 2023
20030f9
Introduce StatisticUnreadError
marcpaterno Sep 6, 2023
acd29e7
Unpack tuple at call site
marcpaterno Sep 11, 2023
fc0bb03
FIx documentation
marcpaterno Sep 11, 2023
3d33ee5
Add GuardedStatistic
marcpaterno Sep 11, 2023
48fbca2
Make use of GuardedStatistic
marcpaterno Sep 11, 2023
98bc182
Add read method to GuardedStatistic
marcpaterno Sep 13, 2023
80e6f1e
Remove unneeded methods
marcpaterno Sep 13, 2023
7d2cc25
Move TrivialStatistic
marcpaterno Sep 15, 2023
ef17c05
More error checking in GaussFamily.read
marcpaterno Sep 15, 2023
4ce8213
Adjusting tests and fixtures; still not complete
marcpaterno Sep 15, 2023
b45dbb8
Fix string interpolation
marcpaterno Sep 17, 2023
3d3d984
Update use of mypy
marcpaterno Sep 17, 2023
6598d8a
Relax pylint rules, and apply everywhere
marcpaterno Sep 17, 2023
875acee
Add docstrings
marcpaterno Sep 17, 2023
6aee9e3
Fix pylint complaints in srd_sn examples
marcpaterno Sep 17, 2023
c390b58
Fix example so that it runs
marcpaterno Sep 17, 2023
a2d3600
WIP
marcpaterno Sep 17, 2023
c641382
Refactoring guided by pylint
marcpaterno Sep 18, 2023
fc56496
Improve comments
marcpaterno Sep 18, 2023
2acf0b8
Attempt to diagnose some statistic read failures
marcpaterno Sep 18, 2023
47523c9
Fix malformed SACC object (add tag to data points)
marcpaterno Sep 18, 2023
607dbff
Remove incorrect type hint
marcpaterno Sep 18, 2023
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
2 changes: 1 addition & 1 deletion examples/des_y1_3x2pt/numcosmo_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

Ncm.cfg_init()

with open(r"numcosmo_firecrown_model.yml", "r", encoding="utf8") as modelfile:
with open(r"numcosmo_firecrown_model.yml", "r", encoding="utf-8") as modelfile:
ncmodel = yaml.load(modelfile, Loader=yaml.Loader)

NcFirecrown = define_numcosmo_model(ncmodel)
Expand Down
2 changes: 1 addition & 1 deletion examples/des_y1_3x2pt/numcosmo_run_PT.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

Ncm.cfg_init()

with open(r"numcosmo_firecrown_model_PT.yml", "r", encoding="utf8") as modelfile:
with open(r"numcosmo_firecrown_model_PT.yml", "r", encoding="utf-8") as modelfile:
ncmodel = yaml.load(modelfile, Loader=yaml.Loader)

NcFirecrownPT = define_numcosmo_model(ncmodel)
Expand Down
2 changes: 1 addition & 1 deletion examples/srd_sn/numcosmo_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

Ncm.cfg_init()

with open(r"numcosmo_firecrown_model_snia.yml", "r", encoding="utf8") as modelfile:
with open(r"numcosmo_firecrown_model_snia.yml", "r", encoding="utf-8") as modelfile:
ncmodel = yaml.load(modelfile, Loader=yaml.Loader)

NcFirecrownSNIa = define_numcosmo_model(ncmodel)
Expand Down
25 changes: 15 additions & 10 deletions firecrown/connector/cosmosis/likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
from cosmosis.datablock import option_section
from cosmosis.datablock import names as section_names
import pyccl as ccl

from firecrown.connector.mapping import mapping_builder, MappingCosmoSIS
from firecrown.likelihood.gauss_family.gauss_family import GaussFamily
from firecrown.likelihood.gauss_family.statistic.two_point import TwoPoint
from firecrown.likelihood.likelihood import load_likelihood, Likelihood, NamedParameters
from firecrown.parameters import ParamsMap
Expand Down Expand Up @@ -105,16 +107,19 @@ def execute(self, sample: cosmosis.datablock) -> int:
self.tools.reset()

# Save concatenated data vector and inverse covariance to enable support
# for the CosmoSIS fisher sampler.
sample.put(
"data_vector", "firecrown_theory", self.likelihood.predicted_data_vector
)
sample.put(
"data_vector", "firecrown_data", self.likelihood.measured_data_vector
)
sample.put(
"data_vector", "firecrown_inverse_covariance", self.likelihood.inv_cov
)
# for the CosmoSIS Fisher sampler. This can only work for likelihoods
# that have these quantities. Currently, this is only GaussFamily.

if isinstance(self.likelihood, GaussFamily):
sample.put(
"data_vector", "firecrown_theory", self.likelihood.predicted_data_vector
)
sample.put(
"data_vector", "firecrown_data", self.likelihood.measured_data_vector
)
sample.put(
"data_vector", "firecrown_inverse_covariance", self.likelihood.inv_cov
)

# Write out theory and data vectors to the data block the ease
# debugging.
Expand Down
126 changes: 119 additions & 7 deletions tests/connector/cosmosis/test_cosmosis_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
As a unit test, what this can test is very limited.
This test do not invoke the `cosmosis` executable.
"""


from cosmosis.datablock import DataBlock
from os.path import expandvars
import yaml
import pytest
from firecrown.connector.cosmosis.likelihood import FirecrownLikelihood
import numpy as np
from cosmosis.datablock import DataBlock, option_section, names as section_names

from firecrown.likelihood.likelihood import NamedParameters
from firecrown.connector.cosmosis.likelihood import FirecrownLikelihood, extract_section


@pytest.fixture(name="minimal_module_config")
Expand All @@ -17,7 +20,7 @@ def fixture_minimal_module_config() -> DataBlock:
This is the minimal possible configuration."""
block = DataBlock()
block.put_string(
"module_options", "likelihood_source", "tests/likelihood/lkdir/lkscript.py"
option_section, "likelihood_source", "tests/likelihood/lkdir/lkscript.py"
)
return block

Expand All @@ -28,13 +31,92 @@ def fixture_defective_module_config() -> DataBlock:
parameter to configure a ParameterizedLikelihood."""
block = DataBlock()
block.put_string(
"module_options",
option_section,
"likelihood_source",
"tests/likelihood/lkdir/lk_needing_param.py",
expandvars("${FIRECROWN_DIR}/tests/likelihood/lkdir/lk_needing_param.py"),
)
return block


@pytest.fixture(name="minimal_config")
def fixture_minimal_config() -> DataBlock:
result = DataBlock()
result.put_string(
option_section,
"likelihood_source",
expandvars("${FIRECROWN_DIR}/tests/likelihood/lkdir/lkscript.py"),
)
return result


@pytest.fixture(name="config_with_derived_parameters")
def fixture_config_with_derived_parameters() -> DataBlock:
result = DataBlock()
result.put_string(
option_section,
"Likelihood_source",
expandvars(
"${FIRECROWN_DIR}/tests/likelihood/lkdir" "/lk_derived_parameter.py"
),
)
result.put_double("sample_parameters_sections", "derived_param0", 12.5)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not necessary to insert this parameter into the DataBlock provided by the sampler. It will be inserted by Firecrown.

return result


@pytest.fixture(name="minimal_firecrown_mod")
def fixture_minimal_firecrown_mod(minimal_config: DataBlock) -> FirecrownLikelihood:
return FirecrownLikelihood(minimal_config)


@pytest.fixture(name="firecrown_mod_with_derived_parameters")
def fixture_firecrown_mod_with_derived_parameters(
config_with_derived_parameters: DataBlock,
) -> FirecrownLikelihood:
return FirecrownLikelihood(config_with_derived_parameters)


@pytest.fixture(name="sample_with_cosmo")
def fixture_sample_with_cosmo() -> DataBlock:
"""Return a DataBlock that contains some cosmological parameters."""
result = DataBlock()
params = {
"h0": 0.83,
"omega_b": 0.22,
"omega_c": 0.120,
"omega_k": 0.0,
"omega_nu": 0.0,
"w": -1.0,
"wa": 0.0,
}
for name, val in params.items():
result.put_double("cosmological_parameters", name, val)
return result


@pytest.fixture(name="minimal_sample")
def fixture_minimal_sample(sample_with_cosmo: DataBlock) -> DataBlock:
with open("tests/distances.yml", encoding="utf-8") as stream:
rawdata = yaml.load(stream, yaml.CLoader)
sample = sample_with_cosmo
for section_name, section_data in rawdata.items():
for parameter_name, value in section_data.items():
sample.put(section_name, parameter_name, np.array(value))
return sample


def test_extract_section_gets_named_parameters(defective_module_config: DataBlock):
params = extract_section(defective_module_config, "module_options")
assert isinstance(params, NamedParameters)
assert params.get_string("likelihood_source") == expandvars(
"${FIRECROWN_DIR}/tests/likelihood/lkdir/lk_needing_param.py"
)


def test_extract_section_raises_on_missing_section(defective_module_config: DataBlock):
with pytest.raises(RuntimeError, match="Datablock section `xxx' does not exist"):
_ = extract_section(defective_module_config, "xxx")


def test_parameterless_module_construction(minimal_module_config):
"""Make sure we can create a CosmoSIS likelihood modules that does not
introduce any new parameters."""
Expand All @@ -47,3 +129,33 @@ def test_missing_required_parameter(defective_module_config):
failure."""
with pytest.raises(KeyError):
_ = FirecrownLikelihood(defective_module_config)


def test_initialize_minimal_module(minimal_firecrown_mod: FirecrownLikelihood):
assert isinstance(minimal_firecrown_mod, FirecrownLikelihood)


def test_execute_missing_cosmological_parameters(
minimal_firecrown_mod: FirecrownLikelihood,
):
no_cosmo_params = DataBlock()
with pytest.raises(
RuntimeError,
match="Datablock section " "`cosmological_parameters' does " "not exist.",
):
_ = minimal_firecrown_mod.execute(no_cosmo_params)


def test_execute_with_cosmo(
minimal_firecrown_mod: FirecrownLikelihood, minimal_sample: DataBlock
):
assert minimal_firecrown_mod.execute(minimal_sample) == 0
assert minimal_sample[section_names.likelihoods, "firecrown_like"] == -3.0


def test_execute_with_derived_parameters(
firecrown_mod_with_derived_parameters: FirecrownLikelihood,
minimal_sample: DataBlock,
):
assert firecrown_mod_with_derived_parameters.execute(minimal_sample) == 0
assert minimal_sample.get_double("derived_section", "derived_param0") == 1.0
4 changes: 2 additions & 2 deletions tests/connector/numcosmo/test_numcosmo_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ def test_model_save_load(tmp_path, nc_model):
"""Test saving and loading a NumCosmo model."""

with open(
tmp_path / r"numcosmo_firecrown_model.yml", "w", encoding="utf8"
tmp_path / r"numcosmo_firecrown_model.yml", "w", encoding="utf-8"
) as modelfile:
yaml.dump(nc_model, modelfile, Dumper=yaml.Dumper)

with open(
tmp_path / r"numcosmo_firecrown_model.yml", "r", encoding="utf8"
tmp_path / r"numcosmo_firecrown_model.yml", "r", encoding="utf-8"
) as modelfile:
model_copy = yaml.load(modelfile, Loader=yaml.Loader)

Expand Down
4 changes: 4 additions & 0 deletions tests/distances.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
distances:
d_m: [0.0, 30.599016773238837, 61.164969838209146, 91.69730457953247, 122.19546576621251, 152.65889770161618, 183.08704437377514, 213.47934960593844, 243.83525720727596, 274.1542111236613, 304.43565558844614, 334.67903527312603, 364.8837954378314, 395.04938208154766, 425.17524209196944, 455.26082339491893, 485.30557510322814, 515.3089476650088, 545.2703930112107, 575.1893647023988, 605.0653180746485, 634.8977103844911, 664.6860009528034, 694.4296513075827, 724.1281253255095, 753.780889372219, 783.3874124412047, 812.9471662912787, 842.4596255824987, 871.9242680104995, 901.3405744391514, 930.7080290314592, 960.0261193786538, 989.2943366273881, 1018.512175604976, 1047.6791349426137, 1076.7947171965068, 1105.8584289668547, 1134.8697810146282, 1163.8282883760776, 1192.7334704749217, 1221.5848512321663, 1250.381959173494, 1279.124327534182, 1307.811494361507, 1336.4430026145776, 1365.0184002615756, 1393.5372403743443, 1421.9990812203082, 1450.4034863516767, 1478.7500246919053, 1507.0382706193889, 1535.26780404836, 1563.4382105069606, 1591.549081212481, 1619.600013143741, 1647.5906091105878, 1675.52047782052, 1703.389233942409, 1731.1964981673086, 1758.9418972663686, 1786.6250641458269, 1814.2456378990812, 1841.8032638558682, 1869.297593628516, 1896.7282851553073, 1924.095002740947, 1951.3974170941488, 1978.6352053623566, 2005.808051163614, 2032.9156446155973, 2059.957682361837, 2086.9338675951485, 2113.843910078286, 2140.6875261618634, 2167.4644387995504, 2194.1743775605864, 2220.81707863964, 2247.3922848640364, 2273.8997456984016, 2300.339217246755, 2326.7104622520724, 2353.013250093383, 2379.247356780422, 2405.4125649458747, 2431.5086638352805, 2457.535449294613, 2483.492723755584, 2509.3802962187356, 2535.1979822343474, 2560.945603881203, 2586.622989743288, 2612.2299748844357, 2637.766400820986, 2663.2321154925203, 2688.6269732306846, 2713.9508347261917, 2739.203566994024, 2764.385043336902, 2789.495143307066]
h: [0.0003299327135551882, 0.00033028642429886265, 0.00033064689311028655, 0.0003310141692726998, 0.0003313883014348929, 0.00033176933760470134, 0.0003321573251426722, 0.00033255231075590697, 0.0003329543404920849, 0.0003333634597336719, 0.000333779713192319, 0.00033420314490345386, 0.00033463379822107017, 0.0003350717158127181, 0.0003355169396546992, 0.00033596951102746967, 0.0003364294705112553, 0.00033689685798188004, 0.00033737171260681264, 0.0003378540728414326, 0.000338343976425519, 0.0003388414603799634, 0.00033934656100371036, 0.00033985931387092586, 0.0003403797538283965, 0.0003409079149931599, 0.00034144383075036957, 0.00034198753375139245, 0.0003425390559121421, 0.00034309842841164797, 0.0003436656816908607, 0.0003442408454516933, 0.0003448239486562996, 0.0003454150195265885, 0.00034601408554397507, 0.00034662117344936585, 0.00034723630924338015, 0.0003478595181868038, 0.0003484908248012773, 0.0003491302528702132, 0.00034977782543994503, 0.0003504335648211034, 0.0003510974925902181, 0.0003517696295915456, 0.00035244999593911667, 0.00035313861101900494, 0.00035383549349181175, 0.0003545406612953651, 0.00035525413164763013, 0.0003559759210498276, 0.0003567060452897582, 0.0003574445194453269, 0.0003581913578882677, 0.00035894657428806097, 0.00035971018161604334, 0.000360482192149704, 0.0003612626174771633, 0.0003620514685018326, 0.00036284875544724657, 0.00036365448786206757, 0.00036446867462525617, 0.0003652913239514028, 0.00036612244339621805, 0.0003669620398621739, 0.00036781011960429515, 0.0003686666882360922, 0.00036953175073563445, 0.00037040531145175553, 0.00037128737411038935, 0.00037217794182102905, 0.0003730770170833059, 0.0003739846017936815, 0.000374900697252251, 0.0003758253041696485, 0.00037675842267405384, 0.0003777000523182926, 0.0003786501920870262, 0.0003796088404040263, 0.00038057599513952853, 0.0003815516536176613, 0.00038253581262394495, 0.0003835284684128543, 0.00038452961671544254, 0.0003855392527470207, 0.00038655737121488646, 0.0003875839663260999, 0.0003886190317953005, 0.0003896625608525598, 0.000390714546251268, 0.0003917749802760464, 0.00039284385475068433, 0.0003939211610460946, 0.00039500689008828416, 0.00039610103236633395, 0.0003972035779403871, 0.00039831451644963803, 0.00039943383712032053, 0.00040056152877369016, 0.0004016975798339982, 0.0004028419783364516]
z: [0.0, 0.010101010101010102, 0.020202020202020204, 0.030303030303030304, 0.04040404040404041, 0.05050505050505051, 0.06060606060606061, 0.07070707070707072, 0.08080808080808081, 0.09090909090909091, 0.10101010101010102, 0.11111111111111112, 0.12121212121212122, 0.13131313131313133, 0.14141414141414144, 0.15151515151515152, 0.16161616161616163, 0.17171717171717174, 0.18181818181818182, 0.19191919191919193, 0.20202020202020204, 0.21212121212121213, 0.22222222222222224, 0.23232323232323235, 0.24242424242424243, 0.25252525252525254, 0.26262626262626265, 0.27272727272727276, 0.2828282828282829, 0.29292929292929293, 0.30303030303030304, 0.31313131313131315, 0.32323232323232326, 0.33333333333333337, 0.3434343434343435, 0.3535353535353536, 0.36363636363636365, 0.37373737373737376, 0.38383838383838387, 0.393939393939394, 0.4040404040404041, 0.4141414141414142, 0.42424242424242425, 0.43434343434343436, 0.4444444444444445, 0.4545454545454546, 0.4646464646464647, 0.4747474747474748, 0.48484848484848486, 0.494949494949495, 0.5050505050505051, 0.5151515151515152, 0.5252525252525253, 0.5353535353535354, 0.5454545454545455, 0.5555555555555556, 0.5656565656565657, 0.5757575757575758, 0.5858585858585859, 0.595959595959596, 0.6060606060606061, 0.6161616161616162, 0.6262626262626263, 0.6363636363636365, 0.6464646464646465, 0.6565656565656566, 0.6666666666666667, 0.6767676767676768, 0.686868686868687, 0.696969696969697, 0.7070707070707072, 0.7171717171717172, 0.7272727272727273, 0.7373737373737375, 0.7474747474747475, 0.7575757575757577, 0.7676767676767677, 0.7777777777777778, 0.787878787878788, 0.797979797979798, 0.8080808080808082, 0.8181818181818182, 0.8282828282828284, 0.8383838383838385, 0.8484848484848485, 0.8585858585858587, 0.8686868686868687, 0.8787878787878789, 0.888888888888889, 0.8989898989898991, 0.9090909090909092, 0.9191919191919192, 0.9292929292929294, 0.9393939393939394, 0.9494949494949496, 0.9595959595959597, 0.9696969696969697, 0.9797979797979799, 0.98989898989899, 1.0]