1. Home
  2. Data & files

Data files · debugging

Setting Up a Debugger Session for a Numerical Script

Eigenfield editorial team ·

Setting up a debugger session well takes about five minutes and saves hours. The aim is a run that fails the same way every time, starts close to the problem, and leaves a record of what was learned. Debugging a failure that appears only now and then, from a cold start that takes ten minutes to reach the interesting part, is slow however good the tools are. This page lists the preparation steps in the order that works best. It pairs with breakpoints in scripts and sits in the Data and files collection.

Make the failure repeat

Start by finding the smallest input that still triggers the problem. If a script fails on a file of 2 million rows, try the first 1,000, then the last 1,000, then a random 1,000. A failure that survives shrinking is far easier to follow. A failure that vanishes at small sizes is itself informative: it suggests a limit, such as memory, an index overflow, or a tolerance that scales with the data.

Next remove randomness. Any random number generator in the script should be given a fixed seed for the session. Without that, a bug in a stochastic algorithm shows up on one run in five and the debugger session is wasted on a run where nothing went wrong. Record the seed that produced the failure; seeds from failing production runs are worth keeping.

import numpy as np
rng = np.random.default_rng(20240607)   # fixed seed for the session
x = rng.standard_normal(1000)

Choose the entry point

Debugging from the top of a long script means stepping through setup code that works. Wrap the suspect part in a function, load saved intermediate data, and start there. Saving an intermediate state to a file at the point just before the failure turns a ten-minute lead-in into a one-second one. Keep that file with the bug report, since it also lets someone else reproduce the failure.

Launching the debugger with arguments is usually a matter of passing them after the script name. For the standard Python debugger the form is python -m pdb script.py followed by the script’s own arguments. Whatever the environment, write the exact launch line in a note, because the next session will need it.

Decide what to watch

A watch is an expression the debugger evaluates at every stop. Choose a few that capture the health of the computation instead of raw values: the norm of the residual, the largest entry of a vector, the count of NaN values, the current iteration number. A residual that falls steadily and then jumps up by three orders of magnitude points to the iteration to examine.

For linear algebra, watch the condition of the matrix, not only its entries. If the condition number is near 10¹⁶, a solve loses all precision in double arithmetic and the errors that follow are rounding, not logic bugs.

Keep a log of the session

Write down each hypothesis and what the debugger showed. It sounds tedious, but it stops the circular pattern of testing the same idea twice. A useful entry has three parts: the claim, the check performed, and the result. When a fix is found, add the input and seed to the regression tests so the failure cannot return unnoticed.

When the debugger is not available

Sometimes the script runs on a remote machine or inside a scheduled job. In that case the session setup becomes a logging setup: write the same watches to a log file at intervals, run the shrunken input, and read the log afterwards. Compare logs from a good run and a bad run using the approach in comparing two files; the first line where they differ is where to look. For handling failures at run time instead of at the desk, see trapping errors in scripts.

Back to top