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

Fixing known test issues and small test refactoring. #88

Merged
merged 34 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
bc7e0c4
Fixing covariance real base tests.
mattkwiecien Apr 21, 2023
1d7cde6
Adding teardown/setup modules
mattkwiecien Apr 22, 2023
5ebb4e2
Updating tests to use separate test subdirectories.
mattkwiecien Apr 22, 2023
49e69c9
Updating covariance calculator test, covariance clusters tests.
mattkwiecien Apr 22, 2023
032e190
Adding conf files for each test so we dont get directory collisions.
mattkwiecien Apr 22, 2023
52f19c1
Minimal builder changes
mattkwiecien Apr 22, 2023
6b136d1
refactored mpi tests
mattkwiecien Apr 23, 2023
a152628
Refactored fourier SSC test
mattkwiecien Apr 23, 2023
35f82cc
Adding race condition fix in the fourier ssc test
mattkwiecien Apr 23, 2023
9053ae2
Changes for tests.
mattkwiecien Apr 24, 2023
7be70e8
cov namaster tests passing
mattkwiecien Apr 24, 2023
c9b98ac
Additional changes, unifying temp directory.
mattkwiecien Apr 24, 2023
2d78380
Updating fssc, gfsky and IO tests to use tmp directory
mattkwiecien Apr 24, 2023
9e77079
Updating fg nmt to use temp directory
mattkwiecien Apr 24, 2023
a502f60
Updated fg nmt and fourier base.
mattkwiecien Apr 24, 2023
2d5ca0c
Removing un-needed conf files.
mattkwiecien Apr 24, 2023
3052fbb
Finished last changes
mattkwiecien Apr 24, 2023
f78ccb0
All tests passing
mattkwiecien Apr 25, 2023
eb58f3d
Adding pytest-retry to dependencies
mattkwiecien Apr 25, 2023
f9bcbdf
Small MPI test fixes.
mattkwiecien Apr 25, 2023
74a8328
Adding debug logging to mpich step.
mattkwiecien Apr 25, 2023
1ec4da7
Python 3.9 for pytest-retry
mattkwiecien Apr 25, 2023
a942fc6
Changing retry library to rerunfailures (official one) and reverting …
mattkwiecien Apr 25, 2023
f88ef87
Re-adding tests/openmpi
mattkwiecien Apr 25, 2023
ef1d95c
Explicitly call mpich mpiexec to avoid confusion with open mpi.
mattkwiecien Apr 25, 2023
34697f6
Attemptng to purge openmpi before running mpich tests.
mattkwiecien Apr 25, 2023
9ff3ac0
Trying to call mpiexec.mpich directly.
mattkwiecien Apr 25, 2023
8ae8063
Swapping order to ensure the issue is a mpich/openmpi mismatch
mattkwiecien Apr 25, 2023
275e521
Specifying a working mpich version.
mattkwiecien Apr 25, 2023
72b4037
Updating --no-bin to be --no-cache-dir now that pip is 23.1
mattkwiecien Apr 25, 2023
384be2e
Re-adding tests to build now that mpich is working
mattkwiecien Apr 25, 2023
498e2cc
Removing debug code
mattkwiecien Apr 25, 2023
5a08a28
Adding google doc string for clobber argument.
mattkwiecien Apr 25, 2023
694375b
Adding clobber docstring details.
mattkwiecien Apr 26, 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
32 changes: 16 additions & 16 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,24 @@ jobs:
# For coveralls
pip install -U pytest-cov coveralls

- name: Test
run: |
python3 -m pytest -vv --ignore=tests/test_mpi.py --cov=tjpcov
# - name: Test
# run: |
# python3 -m pytest -vv --ignore=tests/test_mpi.py --cov=tjpcov

- name: Test mpi (openmpi)
run: |
# Install openmpi
sudo apt install -y openmpi-bin libopenmpi-dev
# Install mpi4py. Not included in requirements as one
# can run the code in serial
pip install -U mpi4py
# - name: Test mpi (openmpi)
# run: |
# # Install openmpi
# sudo apt install -y openmpi-bin libopenmpi-dev
# # Install mpi4py. Not included in requirements as one
# # can run the code in serial
# pip install -U mpi4py

# Run MPI test (just the computation of blocks)
mpiexec -n 2 python3 -m pytest -vv tests/test_mpi.py --cov=tjpcov --cov-append
# # Run MPI test (just the computation of blocks)
# mpiexec -n 2 python3 -m pytest -vv tests/test_mpi.py --cov=tjpcov --cov-append

# Uninstall openmpi
sudo apt remove -y openmpi-bin libopenmpi-dev
pip uninstall -y mpi4py
# # Uninstall openmpi
# sudo apt remove -y openmpi-bin libopenmpi-dev
# pip uninstall -y mpi4py

- name: Test mpi (mpich)
run: |
Expand All @@ -82,7 +82,7 @@ jobs:
pip install -U mpi4py --no-binary :all:

# Run MPI tests
mpiexec -n 2 python3 -m pytest -vv tests/test_mpi.py --cov=tjpcov --cov-append
mpiexec -n 2 python3 -m pytest -vv tests/test_mpi.py --cov=tjpcov --cov-append -s --full-trace
mattkwiecien marked this conversation as resolved.
Show resolved Hide resolved

# Uninstall mpich
sudo apt remove -y mpich libmpich-dev
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies = [
"Jinja2",
"pyyaml",
"pytest",
"pytest-retry",
"pyccl>=2.5.0",
"sacc>=0.7",
"camb",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ tjpcov:
DESwl__0: mask_DESwl0
DESwl__1: mask_DESwl1

outdir: ./tests/benchmarks/32_DES_tjpcov_bm/tjpcov_tmp/
outdir: ./tests/tmp/

# Ngal & sigma_e are not needed in nmt if the coupled noise is provided
# through the cache or sacc file. These are here to test that one can also
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ tjpcov:
DESwl__0: mask_DESwl0
DESwl__1: mask_DESwl1

outdir: ./tests/benchmarks/32_DES_tjpcov_bm/tjpcov_tmp/
outdir: ./tests/tmp/

# Survey params:
# 5 lens bins
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tjpcov:
DESwl__0: mask_DESwl0
DESwl__1: mask_DESwl1

outdir: ./tests/benchmarks/32_DES_tjpcov_bm/tjpcov_tmp/
outdir: ./tests/tmp/

# linear bias for lenses constant for redshift bin (example notebook)
bias_DESgc__0: 1.48
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ tjpcov:
DESwl__0: mask_DESwl0
DESwl__1: mask_DESwl1

outdir: ./tests/benchmarks/32_DES_tjpcov_bm/tjpcov_tmp/
outdir: ./tests/tmp/

# linear bias for lenses constant for redshift bin (example notebook)
bias_DESgc__0: 1.48
Expand Down
2 changes: 1 addition & 1 deletion tests/data/conf_covariance_ssc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ tjpcov:
DESwl__0: mask_DESwl0
DESwl__1: mask_DESwl1

outdir: ./tests/tmp
outdir: ./tests/tmp/

# Survey params:
# 5 lens bins
Expand Down
147 changes: 71 additions & 76 deletions tests/test_covariance_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,22 @@
from tjpcov.covariance_builder import CovarianceBuilder
from scipy.linalg import block_diag

input_yml = "./tests/data/conf_covariance_builder_minimal.yaml"
input_sacc = sacc.Sacc.load_fits(
"./tests/benchmarks/32_DES_tjpcov_bm/cls_cov.fits"
)
outdir = "tests/tmp/"
INPUT_YML = "./tests/data/conf_covariance_builder_minimal.yaml"
OUTDIR = "tests/tmp/"

# Create temporal folder
os.makedirs(outdir, exist_ok=True)

def setup_module():
os.makedirs(OUTDIR, exist_ok=True)


def teardown_module():
shutil.rmtree(OUTDIR)


def mock_sacc():
return sacc.Sacc.load_fits(
"./tests/benchmarks/32_DES_tjpcov_bm/cls_cov.fits"
)


def get_covariance_block(tracer_comb1, tracer_comb2, **kwargs):
Expand Down Expand Up @@ -46,6 +54,11 @@ def _get_covariance_block_for_sacc(
)


@pytest.fixture()
def mock_builder():
return CovarianceBuilderTester(INPUT_YML)


def get_nmt_bin(lmax=95):
bpw_edges = np.array(
[0, 6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, 78, 84, 90, 96]
Expand All @@ -58,44 +71,34 @@ def get_nmt_bin(lmax=95):
return nmt.NmtBin.from_edges(bpw_edges[:-1], bpw_edges[1:])


def clean_tmp():
if os.path.isdir(outdir):
shutil.rmtree(outdir)
os.makedirs(outdir)


# Cleaning the tmp dir before running and after running the tests
@pytest.fixture(autouse=True)
def run_clean_tmp():
clean_tmp()


def test_smoke():
CovarianceBuilderTester(input_yml)
CovarianceBuilderTester(INPUT_YML)


def test_nuisance_config():
cb = CovarianceBuilderTester(input_yml)
assert cb.bias_lens == {"DESgc__0": 1.48}
assert cb.IA is None
def test_nuisance_config(mock_builder):
assert mock_builder.bias_lens == {"DESgc__0": 1.48}
assert mock_builder.IA is None
Ngal = 26 * 3600 / (np.pi / 180) ** 2
assert cb.Ngal == {"DESgc__0": Ngal, "DESwl__0": Ngal, "DESwl__1": Ngal}
assert cb.sigma_e == {"DESwl__0": 0.26, "DESwl__1": 0.26}
assert mock_builder.Ngal == {
"DESgc__0": Ngal,
"DESwl__0": Ngal,
"DESwl__1": Ngal,
}
assert mock_builder.sigma_e == {"DESwl__0": 0.26, "DESwl__1": 0.26}


# Tested also in tests/test_mpi.py
def test_split_tasks_by_rank():
cb = CovarianceBuilderTester(input_yml)
def test_split_tasks_by_rank(mock_builder):
tasks = list(range(100))
tasks_splitted = list(cb._split_tasks_by_rank(tasks))
tasks_splitted = list(mock_builder._split_tasks_by_rank(tasks))

assert tasks == tasks_splitted

# Fake a mpi process with size = 2 and rank = 2
cb.size = 2
cb.rank = 1
mock_builder.size = 2
mock_builder.rank = 1

tasks_splitted = list(cb._split_tasks_by_rank(tasks))
tasks_splitted = list(mock_builder._split_tasks_by_rank(tasks))

assert tasks[1::2] == tasks_splitted

Expand All @@ -113,7 +116,7 @@ def _get_covariance_block_for_sacc(
):
return get_covariance_block(tracer_comb1, tracer_comb2, **kwargs)

cb = CovarianceBuilderTester(input_yml)
cb = CovarianceBuilderTester(INPUT_YML)
blocks, tracers_blocks = cb._compute_all_blocks()
nblocks = len(cb.get_list_of_tracers_for_cov())
assert nblocks == len(blocks)
Expand All @@ -122,11 +125,10 @@ def _get_covariance_block_for_sacc(
assert np.all(bi == get_covariance_block(trs[0], trs[1]))


def test_get_cosmology():
def test_get_cosmology(mock_builder):
# Check that it reads the parameters from the yaml file
cb = CovarianceBuilderTester(input_yml)
config = cb.config.copy()
assert isinstance(cb.get_cosmology(), ccl.Cosmology)
config = mock_builder.config.copy()
assert isinstance(mock_builder.get_cosmology(), ccl.Cosmology)

# Check that it uses the cosmology if given
cosmo = ccl.CosmologyVanillaLCDM()
Expand All @@ -140,7 +142,7 @@ def test_get_cosmology():
assert isinstance(cb.get_cosmology(), ccl.Cosmology)

# Check it reads pickles too
fname = os.path.join(outdir, "cosmos_desy1.pkl")
fname = os.path.join(OUTDIR, "cosmos_desy1.pkl")
with open(fname, "wb") as ff:
pickle.dump(cosmo, ff)

Expand All @@ -156,10 +158,9 @@ def test_get_cosmology():
cb.get_cosmology()


def test_get_covariance_block_not_implemented():
def test_get_covariance_block_not_implemented(mock_builder):
with pytest.raises(NotImplementedError):
cb = CovarianceBuilderTester(input_yml)
cb.get_covariance_block([], [])
mock_builder.get_covariance_block([], [])


def test_get_covariance():
Expand All @@ -186,7 +187,7 @@ def _get_covariance_block_for_sacc(
):
return get_covariance_block(tracer_comb1, tracer_comb2, **kwargs)

cb = CovarianceBuilderTester(input_yml)
cb = CovarianceBuilderTester(INPUT_YML)
blocks, tracers_blocks = cb._compute_all_blocks()
cov = cb.get_covariance()
cov2 = build_matrix_from_blocks(blocks[::-1], tracers_blocks[::-1])
Expand Down Expand Up @@ -214,7 +215,7 @@ def _get_covariance_block_for_sacc(
return get_covariance_block(tracer_comb1, tracer_comb2, **kwargs)

# Test it return 0's when the data types are not those of the class
cb = CovarianceBuilderTester(input_yml)
cb = CovarianceBuilderTester(INPUT_YML)
trs_cov = cb.get_list_of_tracers_for_cov()[0]
cov = cb.get_covariance_block_for_sacc(*trs_cov)
assert not np.any(cov)
Expand Down Expand Up @@ -251,93 +252,88 @@ def get_tracer_comb_data_types(self, tracer_comb):
elif tracer_comb == trs_cov[1]:
return ["cl"]

cb = CovarianceBuilderTester(input_yml)
cb = CovarianceBuilderTester(INPUT_YML)
cov = cb.get_covariance_block_for_sacc(*trs_cov)
cov2 = get_covariance_block(*trs_cov)
assert np.all(cov == cov2)


def test_get_list_of_tracers_for_cov():
cb = CovarianceBuilderTester(input_yml)
trs_cov = cb.get_list_of_tracers_for_cov()
def test_get_list_of_tracers_for_cov(mock_builder):
trs_cov = mock_builder.get_list_of_tracers_for_cov()

# Test all tracers
trs_cov2 = []
tracers = cb.io.get_sacc_file().get_tracer_combinations()
tracers = mock_builder.io.get_sacc_file().get_tracer_combinations()
for i, trs1 in enumerate(tracers):
for trs2 in tracers[i:]:
trs_cov2.append((trs1, trs2))

assert trs_cov == trs_cov2


def test_get_mask_names_dict():
def test_get_mask_names_dict(mock_builder):
tracer_names = {1: "DESwl__0", 2: "DESgc__0", 3: "DESwl__1", 4: "DESwl__1"}
cb = CovarianceBuilderTester(input_yml)
mn = cb.get_mask_names_dict(tracer_names)
mn = mock_builder.get_mask_names_dict(tracer_names)

assert isinstance(mn, dict)
for i, mni in mn.items():
tni = tracer_names[i]
assert mni == cb.config["tjpcov"]["mask_names"][tni]
assert mni == mock_builder.config["tjpcov"]["mask_names"][tni]


def test_get_masks_dict():
def test_get_masks_dict(mock_builder):
tracer_names = {1: "DESwl__0", 2: "DESgc__0", 3: "DESwl__1", 4: "DESwl__1"}
cb = CovarianceBuilderTester(input_yml)
m = cb.get_masks_dict(tracer_names)
m = mock_builder.get_masks_dict(tracer_names)

assert isinstance(m, dict)
for i, mni in m.items():
tni = tracer_names[i]
assert np.all(
mni == hp.read_map(cb.config["tjpcov"]["mask_file"][tni])
mni == hp.read_map(mock_builder.config["tjpcov"]["mask_file"][tni])
)

mi = np.arange(100)
cache = {f"m{i+1}": mi + i for i in range(4)}
m = cb.get_masks_dict(tracer_names, cache)
m = mock_builder.get_masks_dict(tracer_names, cache)
for i in range(4):
assert np.all(m[i + 1] == mi + i)


def test_get_nbpw():
cb = CovarianceBuilderTester(input_yml)
assert cb.get_nbpw() == 16
def test_get_nbpw(mock_builder):
assert mock_builder.get_nbpw() == 16


def test_get_tracers_spin_dict():
def test_get_tracers_spin_dict(mock_builder):
tracer_names = {1: "DESwl__0", 2: "DESgc__0", 3: "DESwl__1", 4: "DESwl__1"}
cb = CovarianceBuilderTester(input_yml)
s = cb.get_tracers_spin_dict(tracer_names)
s = mock_builder.get_tracers_spin_dict(tracer_names)

assert s == {1: 2, 2: 0, 3: 2, 4: 2}


def test_get_tracer_comb_spin():
def test_get_tracer_comb_spin(mock_builder):
tracer_comb = ["DESwl__0", "DESgc__0"]
cb = CovarianceBuilderTester(input_yml)
assert cb.get_tracer_comb_spin(tracer_comb) == (2, 0)

assert mock_builder.get_tracer_comb_spin(tracer_comb) == (2, 0)

def test_get_tracer_comb_data_types():
cb = CovarianceBuilderTester(input_yml)

def test_get_tracer_comb_data_types(mock_builder):
tracer_comb = ["DESgc__0", "DESgc__0"]
assert cb.get_tracer_comb_data_types(tracer_comb) == ["cl_00"]
assert mock_builder.get_tracer_comb_data_types(tracer_comb) == ["cl_00"]

tracer_comb = ["DESgc__0", "DESwl__0"]
assert cb.get_tracer_comb_data_types(tracer_comb) == ["cl_0e", "cl_0b"]
assert mock_builder.get_tracer_comb_data_types(tracer_comb) == [
"cl_0e",
"cl_0b",
]

tracer_comb = ["DESwl__0", "DESwl__0"]
assert cb.get_tracer_comb_data_types(tracer_comb) == [
assert mock_builder.get_tracer_comb_data_types(tracer_comb) == [
"cl_ee",
"cl_eb",
"cl_bb",
]

tracer_comb = ["DESwl__0", "DESwl__1"]
assert cb.get_tracer_comb_data_types(tracer_comb) == [
assert mock_builder.get_tracer_comb_data_types(tracer_comb) == [
"cl_ee",
"cl_eb",
"cl_be",
Expand All @@ -346,7 +342,6 @@ def test_get_tracer_comb_data_types():


@pytest.mark.parametrize("tr", ["DESwl__0", "DESgc__0"])
def test_get_tracer_nmaps(tr):
cb = CovarianceBuilderTester(input_yml)
def test_get_tracer_nmaps(tr, mock_builder):
nmap = 2 if tr == "DESwl__0" else 1
assert cb.get_tracer_nmaps(tr) == nmap
assert mock_builder.get_tracer_nmaps(tr) == nmap
Loading