|
12 | 12 | multi-plane :class:`autolens.Tracer`. |
13 | 13 | """ |
14 | 14 |
|
| 15 | +import warnings |
| 16 | + |
15 | 17 | import numpy as np |
16 | | -from scipy.integrate import quad |
| 18 | +from scipy.integrate import quad, IntegrationWarning |
17 | 19 | from scipy.interpolate import interp1d |
18 | 20 | from typing import List, Optional, Tuple |
19 | 21 |
|
20 | 22 | import autogalaxy as ag |
21 | 23 | from autogalaxy.cosmology import Planck15 |
22 | 24 |
|
| 25 | +from autoconf.test_mode import is_test_mode |
| 26 | + |
| 27 | +# Number of LOS halos retained per plane when ``PYAUTO_TEST_MODE`` is active. |
| 28 | +# Capping the population keeps the multi-plane ray-tracing and per-galaxy |
| 29 | +# plotting paths exercised (so regressions still surface) while collapsing the |
| 30 | +# downstream cost from ~1100 halos to a few dozen. See ``LOSSampler.galaxies_from``. |
| 31 | +_TEST_MODE_MAX_HALOS_PER_PLANE = 3 |
| 32 | + |
23 | 33 |
|
24 | 34 | def comoving_distance_mpc_from(z, cosmology): |
25 | 35 | """ |
@@ -241,6 +251,8 @@ def negative_kappa_from( |
241 | 251 | truncation_factor, |
242 | 252 | c_scatter, |
243 | 253 | cosmology, |
| 254 | + quad_limit=50, |
| 255 | + quad_epsrel=1.49e-8, |
244 | 256 | ): |
245 | 257 | """ |
246 | 258 | Compute the negative convergence sheet for a single LOS plane. |
@@ -278,6 +290,16 @@ def negative_kappa_from( |
278 | 290 | Log-normal scatter in concentration (sigma in dex, e.g. 0.15). |
279 | 291 | cosmology |
280 | 292 | A ``LensingCosmology`` instance. |
| 293 | + quad_limit |
| 294 | + Maximum number of adaptive subintervals for both the inner |
| 295 | + (concentration) and outer (mass) ``scipy.integrate.quad`` calls. |
| 296 | + Defaults to scipy's own default of ``50``. ``LOSSampler.galaxies_from`` |
| 297 | + lowers this under ``PYAUTO_TEST_MODE`` to make the double integral cheap |
| 298 | + while still exercising the full integrand (the inner ``fsolve`` is the |
| 299 | + dominant cost, so fewer subintervals is a ~50x speed-up). |
| 300 | + quad_epsrel |
| 301 | + Relative error tolerance passed to both ``quad`` calls. Defaults to |
| 302 | + scipy's own default of ``1.49e-8``; loosened under test mode. |
281 | 303 |
|
282 | 304 | Returns |
283 | 305 | ------- |
@@ -316,13 +338,20 @@ def _integrand_mass(m): |
316 | 338 | lgc_hi = lgc_centre + 4.0 * c_scatter |
317 | 339 |
|
318 | 340 | c_integral = quad( |
319 | | - _integrand_concentration, lgc_lo, lgc_hi, args=(m, lgc_centre) |
| 341 | + _integrand_concentration, |
| 342 | + lgc_lo, |
| 343 | + lgc_hi, |
| 344 | + args=(m, lgc_centre), |
| 345 | + limit=quad_limit, |
| 346 | + epsrel=quad_epsrel, |
320 | 347 | )[0] |
321 | 348 |
|
322 | 349 | dndm = 10 ** B_mf * m ** A_mf |
323 | 350 | return dndm * m * c_integral |
324 | 351 |
|
325 | | - mass_integral = quad(_integrand_mass, m_min, m_max)[0] |
| 352 | + mass_integral = quad( |
| 353 | + _integrand_mass, m_min, m_max, limit=quad_limit, epsrel=quad_epsrel |
| 354 | + )[0] |
326 | 355 |
|
327 | 356 | kappa = mass_integral * comoving_volume_per_arcsec2 / sigma_cr_mpc2 |
328 | 357 |
|
@@ -601,6 +630,17 @@ def galaxies_from(self) -> List[ag.Galaxy]: |
601 | 630 | cosmology = self.cosmology |
602 | 631 | rng = np.random.RandomState(self.seed) |
603 | 632 |
|
| 633 | + # ``PYAUTO_TEST_MODE`` (integration tests / workspace smoke runs) makes |
| 634 | + # the full LOS population prohibitively slow: a science run samples |
| 635 | + # ~1100 halos (driving multi-plane ray tracing to ~90s) and the |
| 636 | + # per-plane negative-kappa double integral costs ~3.8s/plane. Under test |
| 637 | + # mode we cap the halos per plane and loosen the kappa integral, which |
| 638 | + # keeps both code paths exercised while collapsing the runtime so the |
| 639 | + # los_halos simulators finish well under the per-script timeout cap. |
| 640 | + test_mode = is_test_mode() |
| 641 | + quad_limit = 1 if test_mode else 50 |
| 642 | + quad_epsrel = 0.1 if test_mode else 1.49e-8 |
| 643 | + |
604 | 644 | boundaries, centres = los_planes_from( |
605 | 645 | z_lens=self.z_lens, |
606 | 646 | z_source=self.z_source, |
@@ -682,6 +722,9 @@ def galaxies_from(self) -> List[ag.Galaxy]: |
682 | 722 | ) |
683 | 723 | n_halos = rng.poisson(n_bar) |
684 | 724 |
|
| 725 | + if test_mode: |
| 726 | + n_halos = min(n_halos, _TEST_MODE_MAX_HALOS_PER_PLANE) |
| 727 | + |
685 | 728 | if n_halos > 0: |
686 | 729 | masses = sample_halo_masses( |
687 | 730 | n=n_halos, |
@@ -718,20 +761,30 @@ def galaxies_from(self) -> List[ag.Galaxy]: |
718 | 761 | ag.Galaxy(redshift=z_cen, mass=halo) |
719 | 762 | ) |
720 | 763 |
|
721 | | - kappa_neg = negative_kappa_from( |
722 | | - z_centre=z_cen, |
723 | | - comoving_volume_per_arcsec2=vol_depth, |
724 | | - A_mf=mf_coeffs[i, 0], |
725 | | - B_mf=mf_coeffs[i, 1], |
726 | | - A_mc=mc_coeffs[i, 0], |
727 | | - B_mc=mc_coeffs[i, 1], |
728 | | - m_min=self.m_min, |
729 | | - m_max=self.m_max, |
730 | | - z_source=self.z_source, |
731 | | - truncation_factor=self.truncation_factor, |
732 | | - c_scatter=self.c_scatter, |
733 | | - cosmology=cosmology, |
734 | | - ) |
| 764 | + with warnings.catch_warnings(): |
| 765 | + # Under test mode the deliberately low ``quad_limit`` makes |
| 766 | + # scipy emit a (harmless, expected) max-subdivisions warning per |
| 767 | + # integral; silence it so smoke-run output stays clean. Full |
| 768 | + # accuracy runs (quad_limit=50) never trip it. |
| 769 | + if test_mode: |
| 770 | + warnings.simplefilter("ignore", IntegrationWarning) |
| 771 | + |
| 772 | + kappa_neg = negative_kappa_from( |
| 773 | + z_centre=z_cen, |
| 774 | + comoving_volume_per_arcsec2=vol_depth, |
| 775 | + A_mf=mf_coeffs[i, 0], |
| 776 | + B_mf=mf_coeffs[i, 1], |
| 777 | + A_mc=mc_coeffs[i, 0], |
| 778 | + B_mc=mc_coeffs[i, 1], |
| 779 | + m_min=self.m_min, |
| 780 | + m_max=self.m_max, |
| 781 | + z_source=self.z_source, |
| 782 | + truncation_factor=self.truncation_factor, |
| 783 | + c_scatter=self.c_scatter, |
| 784 | + cosmology=cosmology, |
| 785 | + quad_limit=quad_limit, |
| 786 | + quad_epsrel=quad_epsrel, |
| 787 | + ) |
735 | 788 | galaxies.append( |
736 | 789 | ag.Galaxy( |
737 | 790 | redshift=z_cen, |
|
0 commit comments