Skip to content

perf: reduce unit test suite runtime by removing redundant visualization and reducing grid sizes #321

@Jammy2211

Description

@Jammy2211

Overview

Profile and optimize the PyAutoGalaxy unit test suite to reduce total runtime by 25-35%. The source code is stable, so this only touches test code and test configuration. Follows the same approach proven on PyAutoFit (61s→41s).

Plan

  • Remove redundant visualize_before_fit() calls from aggregator test fixtures — these duplicate work already done by search.fit() and no aggregator test asserts on visualization outputs
  • Disable FITS/CSV file output in test visualization config — real disk I/O that no test depends on
  • Reduce interferometer simulate-and-fit grid sizes from 51×51 to 31×31 where assertions are grid-size-independent
Detailed implementation plan

Affected Repositories

  • PyAutoGalaxy (primary)

Work Classification

Library

Branch Survey

Repository Current Branch Dirty?
./PyAutoGalaxy main minor (CLAUDE.md, docs, scrap.py)

Suggested branch: feature/unit-test-profiling

Implementation Steps

  1. Remove visualize_before_fit() line from 4 files:

    • test_autogalaxy/aggregator/conftest.py:51
    • test_autogalaxy/aggregator/ellipse/conftest.py:51
    • test_autogalaxy/aggregator/imaging/test_aggregator_fit_imaging.py:33
    • test_autogalaxy/aggregator/ellipse/test_aggregator_fit_ellipse.py:33
  2. Disable FITS/CSV flags in test_autogalaxy/config/visualize.yaml:

    • fits_fit, fits_galaxy_images, fits_model_galaxy_images → false
    • fits_dirty_images → false
    • csv_reconstruction → false
  3. Reduce grid sizes in test_autogalaxy/interferometer/test_simulate_and_fit_interferometer.py:

    • test__perfect_fit: 51×51→31×31 grid, 7→3 UV wavelengths, 7×7→5×5 mesh
    • test__linear_light_profiles_agree: 51×51→31×31 grid
  4. Run full test suite to verify all 852 tests pass

Key Files

  • test_autogalaxy/aggregator/conftest.py — redundant viz call
  • test_autogalaxy/aggregator/ellipse/conftest.py — redundant viz call
  • test_autogalaxy/aggregator/imaging/test_aggregator_fit_imaging.py — redundant viz call
  • test_autogalaxy/aggregator/ellipse/test_aggregator_fit_ellipse.py — redundant viz call
  • test_autogalaxy/config/visualize.yaml — FITS/CSV output flags
  • test_autogalaxy/interferometer/test_simulate_and_fit_interferometer.py — grid sizes

Original Prompt

Click to expand starting prompt

Profile and speed up unit tests in @PyAutoGalaxy.

Goal

Reduce the test suite runtime by identifying and optimizing slow tests. The source code is stable, so safe changes to test parameters are acceptable — but prompt the user before changing any numerical assertion values.

Approach (proven on PyAutoFit)

  1. Profile: Run pytest <test_dir> --durations=0 -q to get wall-clock times for all tests
  2. Identify: Find the top 30+ slowest tests and read their source to understand bottlenecks
  3. Categorize: Group by cause — large data arrays, excessive iterations/max_steps, expensive fixtures, real sampler initialization (Dynesty)
  4. Optimize (in order of impact):
    • Reduce n_obs, data array sizes, range(N) loop counts where tests use more data than needed for their assertion tolerances
    • Reduce max_steps, n_iters, maxcall values in EP/sampler tests that validate plumbing not convergence
    • Reduce number_of_steps in grid search fixtures to shrink the job count (e.g. 10->5 gives 25 jobs instead of 100)
    • Adjust query/assertion values if data range changes (e.g. if range(100)->range(50), update assertions that referenced value 50 to value 25)
    • Leave tests with inherent sampler initialization overhead (DynestyStatic with maxcall<=5) as-is
  5. Verify: Run full suite, confirm all tests pass, compare before/after total time

What NOT to change

  • Don't change numerical reference values in assertions without user approval
  • Don't change fixture scoping to module/session — this conflicts with function-scoped set_config_path autouse fixtures
  • Don't mock real sampler tests — they test actual sampling behavior
  • Don't change test_messages.py::test_normal_simplex — the slowness is numerical quadrature integration

Known pattern: FactorValue hash collision

If you encounter a KeyError: <GaussianPrior id=0> in AbstractJacobian.grad(), this is a known bug where FactorValue (id=0) hash-collides with a GaussianPrior that gets id=0. This was fixed in PyAutoFit PR #1182 — check that the fix is present in the installed autofit version.

Target

25-35% reduction in total test runtime. On PyAutoFit this took 61s -> 41s.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions