diff --git a/test_autolens/aggregator/__init__.py b/test_autolens/aggregator/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/test_autolens/aggregator/conftest.py b/test_autolens/aggregator/conftest.py deleted file mode 100644 index ce295b2cb..000000000 --- a/test_autolens/aggregator/conftest.py +++ /dev/null @@ -1,92 +0,0 @@ -import os -from os import path -import pytest -import shutil - -from autoconf import conf -import autofit as af -import autolens as al -from autoconf.conf import with_config -from autofit.non_linear.samples import Sample - - -@pytest.fixture(autouse=True) -def set_test_mode(): - os.environ["PYAUTOFIT_TEST_MODE"] = "1" - yield - del os.environ["PYAUTOFIT_TEST_MODE"] - - -def clean(database_file): - database_sqlite = path.join(conf.instance.output_path, f"{database_file}.sqlite") - - if path.exists(database_sqlite): - os.remove(database_sqlite) - - result_path = path.join(conf.instance.output_path, database_file) - - if path.exists(result_path): - shutil.rmtree(result_path) - - -@with_config( - "general", - "output", - "samples_to_csv", - value=True, -) -def aggregator_from(database_file, analysis, model, samples): - result_path = path.join(conf.instance.output_path, database_file) - - clean(database_file=database_file) - - search = al.m.MockSearch( - samples=samples, result=al.m.MockResult(model=model, samples=samples) - ) - search.paths = af.DirectoryPaths(path_prefix=database_file) - search.fit(model=model, analysis=analysis) - - analysis.modify_before_fit(paths=search.paths, model=model) - analysis.visualize_before_fit(paths=search.paths, model=model) - - database_file = path.join(conf.instance.output_path, f"{database_file}.sqlite") - - agg = af.Aggregator.from_database(filename=database_file) - agg.add_directory(directory=result_path) - - return agg - - -@pytest.fixture(name="model") -def make_model(): - dataset_model = af.Model(al.DatasetModel) - dataset_model.background_sky_level = af.UniformPrior( - lower_limit=0.5, upper_limit=1.5 - ) - - return af.Collection( - dataset_model=dataset_model, - galaxies=af.Collection( - lens=af.Model(al.Galaxy, redshift=0.5, light=al.lp.Sersic), - source=af.Model(al.Galaxy, redshift=1.0, light=al.lp.Sersic), - ), - ) - - -@pytest.fixture(name="samples") -def make_samples(model): - parameters = [model.prior_count * [1.0], model.prior_count * [10.0]] - - sample_list = Sample.from_lists( - model=model, - parameter_lists=parameters, - log_likelihood_list=[1.0, 2.0], - log_prior_list=[0.0, 0.0], - weight_list=[0.0, 1.0], - ) - - return al.m.MockSamples( - model=model, - sample_list=sample_list, - prior_means=[1.0] * model.prior_count, - ) diff --git a/test_autolens/aggregator/test_aggregator_fit_imaging.py b/test_autolens/aggregator/test_aggregator_fit_imaging.py deleted file mode 100644 index 8dfacfd7a..000000000 --- a/test_autolens/aggregator/test_aggregator_fit_imaging.py +++ /dev/null @@ -1,138 +0,0 @@ -import autolens as al - -from test_autolens.aggregator.conftest import clean, aggregator_from - -database_file = "db_fit_imaging" - - -def test__fit_imaging_randomly_drawn_via_pdf_gen_from__analysis_single( - analysis_imaging_7x7, samples, model -): - agg = aggregator_from( - database_file=database_file, - analysis=analysis_imaging_7x7, - model=model, - samples=samples, - ) - - fit_agg = al.agg.FitImagingAgg(aggregator=agg) - fit_pdf_gen = fit_agg.randomly_drawn_via_pdf_gen_from(total_samples=2) - - i = 0 - - for fit_gen in fit_pdf_gen: - for fit_list in fit_gen: - i += 1 - - assert fit_list[0].tracer.galaxies[0].redshift == 0.5 - assert fit_list[0].tracer.galaxies[0].light.centre == (10.0, 10.0) - assert fit_list[0].tracer.galaxies[1].redshift == 1.0 - - assert fit_list[0].dataset_model.background_sky_level == 10.0 - - assert i == 2 - - clean(database_file=database_file) - - -# TODO : These need to use FactorGraphModel - -# def test__fit_imaging_randomly_drawn_via_pdf_gen_from__analysis_multi( -# analysis_imaging_7x7, samples, model -# ): -# agg = aggregator_from( -# database_file=database_file, -# analysis=analysis_imaging_7x7 + analysis_imaging_7x7, -# model=model, -# samples=samples, -# ) -# -# fit_agg = al.agg.FitImagingAgg(aggregator=agg) -# fit_pdf_gen = fit_agg.randomly_drawn_via_pdf_gen_from(total_samples=2) -# -# i = 0 -# -# for fit_gen in fit_pdf_gen: -# for fit_list in fit_gen: -# i += 1 -# -# assert fit_list[0].tracer.galaxies[0].redshift == 0.5 -# assert fit_list[0].tracer.galaxies[0].light.centre == (10.0, 10.0) -# assert fit_list[0].tracer.galaxies[1].redshift == 1.0 -# -# assert fit_list[1].tracer.galaxies[0].redshift == 0.5 -# assert fit_list[1].tracer.galaxies[0].light.centre == (10.0, 10.0) -# assert fit_list[1].tracer.galaxies[1].redshift == 1.0 -# -# assert i == 2 -# -# clean(database_file=database_file) - - -def test__fit_imaging_all_above_weight_gen(analysis_imaging_7x7, samples, model): - agg = aggregator_from( - database_file=database_file, - analysis=analysis_imaging_7x7, - model=model, - samples=samples, - ) - - fit_agg = al.agg.FitImagingAgg(aggregator=agg) - fit_pdf_gen = fit_agg.all_above_weight_gen_from(minimum_weight=-1.0) - - i = 0 - - for fit_gen in fit_pdf_gen: - for fit_list in fit_gen: - i += 1 - - if i == 1: - assert fit_list[0].tracer.galaxies[0].redshift == 0.5 - assert fit_list[0].tracer.galaxies[0].light.centre == (1.0, 1.0) - assert fit_list[0].tracer.galaxies[1].redshift == 1.0 - - if i == 2: - assert fit_list[0].tracer.galaxies[0].redshift == 0.5 - assert fit_list[0].tracer.galaxies[0].light.centre == (10.0, 10.0) - assert fit_list[0].tracer.galaxies[1].redshift == 1.0 - - assert i == 2 - - clean(database_file=database_file) - - -def test__fit_imaging__adapt_images( - analysis_imaging_7x7, samples, model, adapt_images_7x7 -): - agg = aggregator_from( - database_file=database_file, - analysis=analysis_imaging_7x7, - model=model, - samples=samples, - ) - - fit_agg = al.agg.FitImagingAgg(aggregator=agg) - fit_pdf_gen = fit_agg.randomly_drawn_via_pdf_gen_from(total_samples=2) - - i = 0 - - for fit_gen in fit_pdf_gen: - for fit_list in fit_gen: - i += 1 - assert ( - list(fit_list[0].adapt_images.galaxy_image_dict.values())[0] - == list(adapt_images_7x7.galaxy_name_image_dict.values())[0] - ).all() - - assert ( - list( - fit_list[0].adapt_images.galaxy_image_plane_mesh_grid_dict.values() - )[0] - == list( - adapt_images_7x7.galaxy_name_image_plane_mesh_grid_dict.values() - )[0] - ).all() - - assert i == 2 - - clean(database_file=database_file) diff --git a/test_autolens/aggregator/test_aggregator_fit_interferometer.py b/test_autolens/aggregator/test_aggregator_fit_interferometer.py deleted file mode 100644 index 003e010e2..000000000 --- a/test_autolens/aggregator/test_aggregator_fit_interferometer.py +++ /dev/null @@ -1,144 +0,0 @@ -import autolens as al - -from test_autolens.aggregator.conftest import clean, aggregator_from - -database_file = "db_fit_interferometer" - - -def test__fit_interferometer_randomly_drawn_via_pdf_gen_from( - analysis_interferometer_7, samples, model -): - agg = aggregator_from( - database_file=database_file, - analysis=analysis_interferometer_7, - model=model, - samples=samples, - ) - - fit_agg = al.agg.FitInterferometerAgg(aggregator=agg) - fit_pdf_gen = fit_agg.randomly_drawn_via_pdf_gen_from(total_samples=2) - - i = 0 - - for fit_gen in fit_pdf_gen: - for fit_list in fit_gen: - i += 1 - - assert fit_list[0].tracer.galaxies[0].redshift == 0.5 - assert fit_list[0].tracer.galaxies[0].light.centre == (10.0, 10.0) - assert fit_list[0].tracer.galaxies[1].redshift == 1.0 - - assert fit_list[0].dataset_model.background_sky_level == 10.0 - - assert i == 2 - - clean(database_file=database_file) - - -# TODO : These need to use FactorGraphModel - -# def test__fit_interferometer_randomly_drawn_via_pdf_gen_from__analysis_multi(analysis_interferometer_7, samples, model): -# -# agg = aggregator_from( -# database_file=database_file, -# analysis=analysis_interferometer_7 + analysis_interferometer_7, -# model=model, -# samples=samples, -# ) -# -# fit_agg = al.agg.FitInterferometerAgg(aggregator=agg) -# fit_pdf_gen = fit_agg.randomly_drawn_via_pdf_gen_from( -# total_samples=2 -# ) -# -# i = 0 -# -# for fit_gen in fit_pdf_gen: -# for fit_list in fit_gen: -# i += 1 -# -# assert fit_list[0].tracer.galaxies[0].redshift == 0.5 -# assert fit_list[0].tracer.galaxies[0].light.centre == (10.0, 10.0) -# assert fit_list[0].tracer.galaxies[1].redshift == 1.0 -# -# assert fit_list[1].tracer.galaxies[0].redshift == 0.5 -# assert fit_list[1].tracer.galaxies[0].light.centre == (10.0, 10.0) -# assert fit_list[1].tracer.galaxies[1].redshift == 1.0 -# -# assert i == 2 -# -# clean(database_file=database_file) - - -def test__fit_interferometer_all_above_weight_gen( - analysis_interferometer_7, samples, model -): - agg = aggregator_from( - database_file=database_file, - analysis=analysis_interferometer_7, - model=model, - samples=samples, - ) - - fit_agg = al.agg.FitInterferometerAgg(aggregator=agg) - fit_pdf_gen = fit_agg.all_above_weight_gen_from(minimum_weight=-1.0) - - i = 0 - - for fit_gen in fit_pdf_gen: - for fit_list in fit_gen: - i += 1 - - if i == 1: - assert fit_list[0].tracer.galaxies[0].redshift == 0.5 - assert fit_list[0].tracer.galaxies[0].light.centre == (1.0, 1.0) - assert fit_list[0].tracer.galaxies[1].redshift == 1.0 - - if i == 2: - assert fit_list[0].tracer.galaxies[0].redshift == 0.5 - assert fit_list[0].tracer.galaxies[0].light.centre == ( - 10.0, - 10.0, - ) - assert fit_list[0].tracer.galaxies[1].redshift == 1.0 - - assert i == 2 - - clean(database_file=database_file) - - -def test__fit_interferometer__adapt_images( - analysis_interferometer_7, samples, model, adapt_images_7x7 -): - agg = aggregator_from( - database_file=database_file, - analysis=analysis_interferometer_7, - model=model, - samples=samples, - ) - - fit_agg = al.agg.FitInterferometerAgg(aggregator=agg) - fit_pdf_gen = fit_agg.randomly_drawn_via_pdf_gen_from(total_samples=2) - - i = 0 - - for fit_gen in fit_pdf_gen: - for fit_list in fit_gen: - i += 1 - assert ( - list(fit_list[0].adapt_images.galaxy_image_dict.values())[0] - == list(adapt_images_7x7.galaxy_name_image_dict.values())[0] - ).all() - - assert ( - list( - fit_list[0].adapt_images.galaxy_image_plane_mesh_grid_dict.values() - )[0] - == list( - adapt_images_7x7.galaxy_name_image_plane_mesh_grid_dict.values() - )[0] - ).all() - - assert i == 2 - - clean(database_file=database_file) diff --git a/test_autolens/aggregator/test_aggregator_tracer.py b/test_autolens/aggregator/test_aggregator_tracer.py deleted file mode 100644 index 3f167d7d4..000000000 --- a/test_autolens/aggregator/test_aggregator_tracer.py +++ /dev/null @@ -1,77 +0,0 @@ -import autolens as al - -from test_autolens.aggregator.conftest import clean, aggregator_from - -database_file = "db_tracer" - - -def test__tracer_randomly_drawn_via_pdf_gen_from( - masked_imaging_7x7, - samples, - model, -): - analysis = al.AnalysisImaging(dataset=masked_imaging_7x7, use_jax=False) - - agg = aggregator_from( - database_file=database_file, - analysis=analysis, - model=model, - samples=samples, - ) - - tracer_agg = al.agg.TracerAgg(aggregator=agg) - tracer_pdf_gen = tracer_agg.randomly_drawn_via_pdf_gen_from(total_samples=2) - - i = 0 - - for tracer_gen in tracer_pdf_gen: - for tracer_list in tracer_gen: - i += 1 - - assert tracer_list[0].galaxies[0].redshift == 0.5 - assert tracer_list[0].galaxies[0].light.centre == (10.0, 10.0) - assert tracer_list[0].galaxies[1].redshift == 1.0 - - assert i == 2 - - clean(database_file=database_file) - - -def test__tracer_all_above_weight_gen(analysis_imaging_7x7, samples, model): - agg = aggregator_from( - database_file=database_file, - analysis=analysis_imaging_7x7, - model=model, - samples=samples, - ) - - tracer_agg = al.agg.TracerAgg(aggregator=agg) - tracer_pdf_gen = tracer_agg.all_above_weight_gen_from(minimum_weight=-1.0) - weight_pdf_gen = tracer_agg.weights_above_gen_from(minimum_weight=-1.0) - - i = 0 - - for tracer_gen, weight_gen in zip(tracer_pdf_gen, weight_pdf_gen): - for tracer_list in tracer_gen: - i += 1 - - if i == 1: - assert tracer_list[0].galaxies[0].redshift == 0.5 - assert tracer_list[0].galaxies[0].light.centre == (1.0, 1.0) - assert tracer_list[0].galaxies[1].redshift == 1.0 - - if i == 2: - assert tracer_list[0].galaxies[0].redshift == 0.5 - assert tracer_list[0].galaxies[0].light.centre == (10.0, 10.0) - assert tracer_list[0].galaxies[1].redshift == 1.0 - - for weight in weight_gen: - if i == 0: - assert weight == 0.0 - - if i == 1: - assert weight == 1.0 - - assert i == 2 - - clean(database_file=database_file) diff --git a/test_autolens/conftest.py b/test_autolens/conftest.py index 17da6b12c..16744c3d7 100644 --- a/test_autolens/conftest.py +++ b/test_autolens/conftest.py @@ -1,5 +1,6 @@ import os from os import path +from unittest.mock import MagicMock import pytest from matplotlib import pyplot @@ -28,6 +29,8 @@ def make_plot_patch(monkeypatch): plot_patch = PlotPatch() monkeypatch.setattr(pyplot, "savefig", plot_patch) monkeypatch.setattr(matplotlib.figure.Figure, "savefig", plot_patch) + monkeypatch.setattr(pyplot, "tight_layout", lambda *a, **kw: None) + monkeypatch.setattr(pyplot, "colorbar", lambda *a, **kw: MagicMock()) return plot_patch diff --git a/test_autolens/imaging/test_simulate_and_fit_imaging.py b/test_autolens/imaging/test_simulate_and_fit_imaging.py index 6b6fafb5d..2cc50e9be 100644 --- a/test_autolens/imaging/test_simulate_and_fit_imaging.py +++ b/test_autolens/imaging/test_simulate_and_fit_imaging.py @@ -618,7 +618,7 @@ def test__simulate_imaging_data_and_fit__linear_light_profiles_and_pixelization_ def test__simulate_imaging_data_and_fit__complex_fit_compare_mapping_matrix_sparse_operator(): - grid = al.Grid2D.uniform(shape_native=(21, 21), pixel_scales=0.1) + grid = al.Grid2D.uniform(shape_native=(15, 15), pixel_scales=0.1) psf = al.Convolver.from_gaussian( shape_native=(3, 3), pixel_scales=0.2, sigma=0.75, normalize=True diff --git a/test_autolens/lens/test_los.py b/test_autolens/lens/test_los.py deleted file mode 100644 index c8ec59731..000000000 --- a/test_autolens/lens/test_los.py +++ /dev/null @@ -1,437 +0,0 @@ -""" -Tests for the line-of-sight halo sampling module ``autolens.lens.los``. -""" - -import numpy as np -import pytest - -import autogalaxy as ag -from autogalaxy.cosmology import Planck15 - -from autolens.lens.los import ( - comoving_distance_mpc_from, - comoving_volume_mpc3_from, - los_planes_from, - negative_kappa_from, - number_of_halos_from, - sample_halo_masses, - sample_positions_in_circle, - sample_concentrations, - _mass_ratio_from_concentration_and_tau, - LOSSampler, -) - - -@pytest.fixture -def cosmology(): - return Planck15() - - -# -------------------------------------------------------------------------- -# Comoving distance / volume -# -------------------------------------------------------------------------- - - -class TestComovingDistance: - def test_zero_redshift(self, cosmology): - assert comoving_distance_mpc_from(0.0, cosmology) == pytest.approx( - 0.0, abs=1e-6 - ) - - def test_positive_redshift(self, cosmology): - d = comoving_distance_mpc_from(0.5, cosmology) - assert 1900.0 < d < 2000.0 - - def test_monotonic(self, cosmology): - d1 = comoving_distance_mpc_from(0.3, cosmology) - d2 = comoving_distance_mpc_from(0.6, cosmology) - d3 = comoving_distance_mpc_from(1.0, cosmology) - assert d1 < d2 < d3 - - -class TestComovingVolume: - def test_zero(self, cosmology): - assert comoving_volume_mpc3_from(0.0, cosmology) == pytest.approx( - 0.0, abs=1e-6 - ) - - def test_positive(self, cosmology): - v = comoving_volume_mpc3_from(0.5, cosmology) - assert v > 0 - - def test_monotonic(self, cosmology): - v1 = comoving_volume_mpc3_from(0.3, cosmology) - v2 = comoving_volume_mpc3_from(0.6, cosmology) - assert v2 > v1 - - -# -------------------------------------------------------------------------- -# Plane slicing -# -------------------------------------------------------------------------- - - -class TestLosPlanes: - def test_boundary_endpoints(self, cosmology): - boundaries, centres = los_planes_from( - z_lens=0.5, - z_source=1.0, - planes_before_lens=4, - planes_after_lens=4, - ) - assert boundaries[0] == pytest.approx(0.0) - assert boundaries[-1] == pytest.approx(1.0) - - def test_number_of_planes(self, cosmology): - boundaries, centres = los_planes_from( - z_lens=0.5, - z_source=1.0, - planes_before_lens=4, - planes_after_lens=4, - ) - n_planes = len(centres) - # 4 front + 5 back (back includes the lens plane itself) - assert n_planes == 9 - assert len(boundaries) == n_planes + 1 - - def test_centres_within_boundaries(self, cosmology): - boundaries, centres = los_planes_from( - z_lens=0.5, - z_source=1.0, - planes_before_lens=4, - planes_after_lens=4, - ) - assert np.all(centres > 0.0) - assert np.all(centres < 1.0) - - def test_boundaries_sorted(self, cosmology): - boundaries, centres = los_planes_from( - z_lens=0.5, - z_source=1.0, - planes_before_lens=4, - planes_after_lens=4, - ) - assert np.all(np.diff(boundaries) > 0) - assert np.all(np.diff(centres) > 0) - - def test_matches_los_pipes_slicing_bounder(self): - """ - Verify against the reference slicing_bounder function from los_pipes. - """ - z_lens = 0.5 - z_source = 1.0 - pls = [4, 4] - - gap_front = z_lens / (pls[0] + 1.0) - gap_back = (z_source - z_lens) / (pls[1] + 1.0) - z_front_ref = np.arange(1.5 * gap_front, z_lens, gap_front) - z_back_ref = np.arange( - z_lens + 0.5 * gap_back, z_source - 1.0 * gap_back, gap_back - ) - boundaries_ref = np.concatenate(([0.0], z_front_ref, z_back_ref, [z_source])) - - centres_front_ref = np.linspace(0.0, z_lens, pls[0] + 2)[1:-1] - centres_back_ref = np.linspace(z_lens, z_source, pls[1] + 2)[:-1] - centres_ref = np.concatenate((centres_front_ref, centres_back_ref)) - - boundaries, centres = los_planes_from( - z_lens=z_lens, - z_source=z_source, - planes_before_lens=pls[0], - planes_after_lens=pls[1], - ) - - np.testing.assert_allclose(boundaries, boundaries_ref, atol=1e-12) - np.testing.assert_allclose(centres, centres_ref, atol=1e-12) - - -# -------------------------------------------------------------------------- -# Mass ratio -# -------------------------------------------------------------------------- - - -class TestMassRatio: - def test_known_values(self): - """ - Compare against los_pipes scale_c function for c=10, tau from cti=100. - """ - from scipy.optimize import fsolve - - c = 10.0 - cti = 100.0 - delta_c = 200.0 / 3.0 * (c ** 3 / (np.log(1 + c) - c / (1 + c))) - tau = fsolve( - lambda t, dc, ct: ct / 3.0 * (t ** 3 / (np.log(1 + t) - t / (1 + t))) - dc, - 10.0, - args=(delta_c, cti), - )[0] - - ratio = _mass_ratio_from_concentration_and_tau(c, tau) - - tau2 = tau ** 2 - expected = ( - tau2 - / (tau2 + 1.0) ** 2 - * ((tau2 - 1.0) * np.log(tau) + tau * np.pi - (tau2 + 1.0)) - ) / (np.log(1 + c) - c / (1 + c)) - - assert ratio == pytest.approx(expected, rel=1e-10) - - def test_mass_ratio_less_than_one(self): - ratio = _mass_ratio_from_concentration_and_tau(10.0, 25.0) - assert 0 < ratio < 2.0 - - -# -------------------------------------------------------------------------- -# Sampling functions -# -------------------------------------------------------------------------- - - -class TestNumberOfHalos: - def test_positive(self): - n = number_of_halos_from( - A=-1.9, B=8.0, m_min=1e7, m_max=1e10, volume=1.0 - ) - assert n > 0 - - def test_scales_with_volume(self): - n1 = number_of_halos_from(A=-1.9, B=8.0, m_min=1e7, m_max=1e10, volume=1.0) - n2 = number_of_halos_from(A=-1.9, B=8.0, m_min=1e7, m_max=1e10, volume=2.0) - assert n2 == pytest.approx(2.0 * n1, rel=1e-10) - - -class TestSampleHaloMasses: - def test_within_range(self): - masses = sample_halo_masses( - n=1000, m_min=1e7, m_max=1e10, A=-1.9, B=8.0, seed=42 - ) - assert len(masses) == 1000 - assert np.all(masses >= 1e7) - assert np.all(masses <= 1e10) - - def test_reproducible(self): - m1 = sample_halo_masses(n=100, m_min=1e7, m_max=1e10, A=-1.9, B=8.0, seed=42) - m2 = sample_halo_masses(n=100, m_min=1e7, m_max=1e10, A=-1.9, B=8.0, seed=42) - np.testing.assert_array_equal(m1, m2) - - def test_empty(self): - masses = sample_halo_masses(n=0, m_min=1e7, m_max=1e10, A=-1.9, B=8.0) - assert len(masses) == 0 - - def test_skew_toward_low_mass(self): - masses = sample_halo_masses( - n=10000, m_min=1e7, m_max=1e10, A=-1.9, B=8.0, seed=42 - ) - median = np.median(masses) - assert median < 1e9 - - -class TestSamplePositions: - def test_within_radius(self): - pos = sample_positions_in_circle(n=1000, radius=5.0, seed=42) - r = np.sqrt(pos[:, 0] ** 2 + pos[:, 1] ** 2) - assert np.all(r <= 5.0) - - def test_shape(self): - pos = sample_positions_in_circle(n=100, radius=5.0, seed=42) - assert pos.shape == (100, 2) - - def test_empty(self): - pos = sample_positions_in_circle(n=0, radius=5.0) - assert pos.shape == (0, 2) - - -class TestSampleConcentrations: - def test_clipped(self): - c = sample_concentrations( - log10_masses=np.array([7.0, 8.0, 9.0, 10.0]), - A_mc=-3.0, - B_mc=40.0, - c_scatter=0.15, - seed=42, - ) - assert np.all(c >= 0.1) - assert np.all(c <= 200.0) - - def test_reasonable_values(self): - c = sample_concentrations( - log10_masses=np.full(1000, 9.0), - A_mc=-3.0, - B_mc=40.0, - c_scatter=0.15, - seed=42, - ) - median_c = np.median(c) - expected_mean = -3.0 * 9.0 + 40.0 - assert 0.5 * expected_mean < median_c < 2.0 * expected_mean - - -# -------------------------------------------------------------------------- -# Negative kappa -# -------------------------------------------------------------------------- - - -class TestNegativeKappa: - def test_returns_negative(self, cosmology): - kappa = negative_kappa_from( - z_centre=0.25, - comoving_volume_per_arcsec2=30.0, - A_mf=-1.9, - B_mf=8.0, - A_mc=-3.0, - B_mc=40.0, - m_min=1e7, - m_max=1e10, - z_source=1.0, - truncation_factor=100.0, - c_scatter=0.15, - cosmology=cosmology, - ) - assert kappa < 0.0 - - def test_magnitude_reasonable(self, cosmology): - kappa = negative_kappa_from( - z_centre=0.25, - comoving_volume_per_arcsec2=30.0, - A_mf=-1.9, - B_mf=8.0, - A_mc=-3.0, - B_mc=40.0, - m_min=1e7, - m_max=1e10, - z_source=1.0, - truncation_factor=100.0, - c_scatter=0.15, - cosmology=cosmology, - ) - assert -0.1 < kappa < 0.0 - - def test_scales_with_volume(self, cosmology): - kwargs = dict( - z_centre=0.25, - A_mf=-1.9, - B_mf=8.0, - A_mc=-3.0, - B_mc=40.0, - m_min=1e7, - m_max=1e10, - z_source=1.0, - truncation_factor=100.0, - c_scatter=0.15, - cosmology=cosmology, - ) - k1 = negative_kappa_from(comoving_volume_per_arcsec2=10.0, **kwargs) - k2 = negative_kappa_from(comoving_volume_per_arcsec2=20.0, **kwargs) - assert k2 == pytest.approx(2.0 * k1, rel=1e-6) - - -# -------------------------------------------------------------------------- -# LOSSampler -# -------------------------------------------------------------------------- - - -class TestLOSSampler: - """ - LOSSampler tests use [2,2] planes which produces 5 planes - (2 front + 3 back including the lens plane). - """ - - N_PLANES_2_2 = 5 - - def _make_sampler(self, cosmology, seed=42): - n = self.N_PLANES_2_2 - return LOSSampler( - z_lens=0.5, - z_source=1.0, - planes_before_lens=2, - planes_after_lens=2, - m_min=1e7, - m_max=1e10, - cone_radius_arcsec=3.0, - c_scatter=0.15, - truncation_factor=100.0, - cosmology=cosmology, - mass_function_coefficients=np.tile([-1.9, 8.0], (n, 1)), - mass_concentration_coefficients=np.tile([-3.0, 40.0], (n, 1)), - seed=seed, - ) - - def test_returns_galaxies(self, cosmology): - galaxies = self._make_sampler(cosmology).galaxies_from() - assert len(galaxies) > 0 - - has_halo = any( - hasattr(g, "mass") and isinstance(g.mass, ag.mp.NFWTruncatedSph) - for g in galaxies - ) - has_sheet = any( - hasattr(g, "mass_sheet") and isinstance(g.mass_sheet, ag.mp.MassSheet) - for g in galaxies - ) - assert has_halo - assert has_sheet - - def test_number_of_sheets_matches_planes(self, cosmology): - n_front = 3 - n_back = 2 - _, centres = los_planes_from( - z_lens=0.5, z_source=1.0, - planes_before_lens=n_front, planes_after_lens=n_back, - ) - n_planes = len(centres) - - sampler = LOSSampler( - z_lens=0.5, - z_source=1.0, - planes_before_lens=n_front, - planes_after_lens=n_back, - m_min=1e7, - m_max=1e10, - cone_radius_arcsec=3.0, - c_scatter=0.15, - truncation_factor=100.0, - cosmology=cosmology, - mass_function_coefficients=np.tile([-1.9, 8.0], (n_planes, 1)), - mass_concentration_coefficients=np.tile([-3.0, 40.0], (n_planes, 1)), - seed=42, - ) - galaxies = sampler.galaxies_from() - - n_sheets = sum( - 1 - for g in galaxies - if hasattr(g, "mass_sheet") - and isinstance(g.mass_sheet, ag.mp.MassSheet) - ) - assert n_sheets == n_planes - - def test_negative_kappa_sheets_are_negative(self, cosmology): - galaxies = self._make_sampler(cosmology).galaxies_from() - - for g in galaxies: - if hasattr(g, "mass_sheet") and isinstance( - g.mass_sheet, ag.mp.MassSheet - ): - assert g.mass_sheet.kappa < 0.0 - - def test_reproducible(self, cosmology): - g1 = self._make_sampler(cosmology, seed=123).galaxies_from() - g2 = self._make_sampler(cosmology, seed=123).galaxies_from() - assert len(g1) == len(g2) - - def test_halo_redshifts_match_plane_centres(self, cosmology): - galaxies = self._make_sampler(cosmology).galaxies_from() - - _, centres = los_planes_from( - z_lens=0.5, - z_source=1.0, - planes_before_lens=2, - planes_after_lens=2, - ) - - for g in galaxies: - found = any( - abs(g.redshift - c) < 1e-10 for c in centres - ) - assert found, ( - f"Galaxy redshift {g.redshift} does not match any plane centre" - ) diff --git a/test_autolens/point/triangles/test_solver.py b/test_autolens/point/triangles/test_solver.py index 2264afce5..245ed17a4 100644 --- a/test_autolens/point/triangles/test_solver.py +++ b/test_autolens/point/triangles/test_solver.py @@ -63,22 +63,3 @@ def test_trivial( assert coordinates[0] == pytest.approx(source_plane_coordinate, abs=1.0e-1) -def test_real_example_jax(grid, tracer): - - import jax.numpy as jnp - - jax_solver = PointSolver.for_grid( - grid=grid, pixel_scale_precision=0.001, xp=jnp, magnification_threshold=1e-8 - ) - - result = jax_solver.solve( - tracer=tracer, source_plane_coordinate=(0.07, 0.07), remove_infinities=True - ) - - assert len(result) == 5 - - result = jax_solver.solve( - tracer=tracer, source_plane_coordinate=(0.07, 0.07), remove_infinities=False - ) - - assert len(result) == 15