Algorithm Search⌘K

Algorithmadvanced

Cotton-Schwab CLEAN

Also called major-minor cycle CLEAN, Cotton-Schwab algorithm.

Run fast CLEAN minor cycles on an image with a cut beam, then subtract the model from the ungridded visibilities in a major cycle, which removes the errors the minor cycles built up.

Time
Θ(C m n + K (n + b)) worst · average · best
Space
Θ(m + n) extra
Where
C = the number of major cycles; K = the total number of minor steps; b = the number of pixels in the beam patch; m = the number of visibilities; n = the number of pixels
Properties
deterministicexact residual each major cyclegreedyiterativenot linearnot monotone misfitnot nonnegative
Origin
Fred R. Schwab, William D. Cotton, 1984

Cotton-Schwab CLEAN runs Hogbom CLEAN in short, cheap minor cycles on an image with only a small patch of the dirty beam, and between them runs a major cycle that predicts the visibilities of the model, subtracts them from the measured, ungridded visibilities and makes a new exact residual image. The minor cycles are fast but drift, because the cut beam and the gridding are approximate; each major cycle wipes the drift out. Hogbom CLEAN never goes back to the data, and multiscale CLEAN changes the components, not the cycles: in practice multiscale steps run as the minor cycles of this frame. Because the major cycle works on the visibilities, it is also where wide-field corrections such as W-projection enter.

In plain words

Cleaning a big picture one point at a time is slow if each step has to touch every pixel. So this method cheats a little. For a while it removes only the bright center of the known pattern around each point, which is fast. The picture it works on slowly goes wrong, because the far parts of the pattern were not removed. Then it stops and goes back to the raw measurements. It computes what its list of points should have measured, takes that away from the real measurements, and makes a fresh and exact leftover picture. Then it cheats again for a while. The fresh start wipes out the errors of the fast part.

The problem

An interferometer measures visibilities VjV_j at points (uj,vj)(u_j, v_j) of the uv plane, with weights wj>0w_j > 0 and W=∑jwjW = \sum_j w_j; see aperture synthesis. For a model MM on an image of nn pixels at directions (lq,mq)(l_q, m_q), the predicted visibilities are

(AM)j=∑qMq e−2πi(ujlq+vjmq).(AM)_j = \sum_q M_q\, e^{-2\pi i (u_j l_q + v_j m_q)} .

The dirty image of a set of visibilities XX is I(X)q=1W∑jwj Re(Xj e2πi(ujlq+vjmq))\mathcal I(X)_q = \frac1W \sum_j w_j\, \mathrm{Re}\big(X_j\, e^{2\pi i (u_j l_q + v_j m_q)}\big), and the dirty beam is B(Δ)=1W∑jwjcos⁡2π(ujΔl+vjΔm)B(\Delta) = \frac1W \sum_j w_j \cos 2\pi (u_j \Delta l + v_j \Delta m), so B(0)=1B(0) = 1. For a sky xx, I(Ax)=B⋆x\mathcal I(Ax) = B \star x, the ordinary (not periodic) convolution over pixel offsets.

The input is the list of visibilities, the pixel grid, a loop gain gg, a threshold τ\tau, a beam patch half-width PP and a cycle factor ff. The output is a model MM and the exact residual image I(V−AM)\mathcal I(V - AM).

The algorithm

  1. COTTON-SCHWAB-CLEAN(V, g, τ, P, f, C)
  2. 1M ← 0, V_r ← V
  3. 2for c ← 1 to C do ▷ major cycles
  4. 3R ← I(V_r) ▷ exact residual image
  5. 4if max_q |R_q| ≤ τ then break
  6. 5λ ← max(τ, f · max_q |R_q|)
  7. 6Hogbom steps on R until max_q |R_q| ≤ λ, ▷ the minor cycle
  8. subtracting only B(Δ) with |Δ| ≤ P, and adding the components to M
  9. 7V_r ← V - A M ▷ predict and subtract
  10. 8return M, I(V_r)

Line L6 is lines L3 to L7 of Hogbom CLEAN, with the subtraction cut to the patch: Rq←Rq−c B(q−p)R_q \leftarrow R_q - c\,B(q - p) only for ∣q−p∣≤P|q - p| \le P in each coordinate. Line L7 is the step that names the method: it goes back to the measured, ungridded visibilities, so nothing that the minor cycle got wrong survives into the next residual image.

The cycle factor ff decides how far a minor cycle may go. The rule of Clark (1980) is to set ff to the largest beam value outside the patch: a sidelobe outside the patch can then hide at most a fraction ff of a peak. The code uses this rule by default. Production imagers grid the visibilities and use the FFT in lines L3 and L7, which costs much less than the direct sums here; the gridding error is one more thing that line L7 removes, because the prediction is subtracted from the visibilities and not from an image.

Example

Take a 16×1616 \times 16 image with pixel size 1/161/16 in direction cosine, and four point sources of flux 1.01.0, 0.70.7, 0.50.5 and 0.40.4 (total 2.62.6). The array has 100 random baselines with ∣u∣,∣v∣≤6|u|, |v| \le 6, each with its conjugate, so m=200m = 200. The dirty peak is 1.01541.0154 and the misfit Q(M)=1W∑jwj∣Vj−(AM)j∣2Q(M) = \frac1W \sum_j w_j |V_j - (AM)_j|^2 starts at 2.10522.1052. The beam has B(0,±1)=0.2699B(0, \pm 1) = 0.2699 and B(±1,0)=0.3646B(\pm 1, 0) = 0.3646, and its largest value outside a patch of half-width P=3P = 3 is 0.22530.2253, which is the cycle factor. With g=0.2g = 0.2 and at most 150 minor steps per cycle:

major cycle minor steps QQ after exact max⁡q∣Rq∣\max_q \lvert R_q \rvert after model flux
1 31 0.0721 0.1505 2.2702
2 43 0.0213 0.0615 2.5374
3 81 0.0074 0.0363 2.5955
4 55 0.0040 0.0201 2.6317
6 69 0.0015 0.0070 2.6148
8 150 0.0008 0.0042 2.6079

Now run one minor cycle of 150 steps with the same patch and no major cycle. The minor cycle believes the residual peak is 0.10160.1016, but the exact residual has peak 0.33820.3382, and the misfit is still 0.46160.4616. The drift came from the sidelobes outside the patch. With the full beam as patch, 150 steps of one cycle bring QQ to 10−810^{-8}, which is Hogbom CLEAN.

Correctness

Three facts are proved, and one is not claimed.

  • Exact residual. Line L3 makes R=I(V−AM)R = \mathcal I(V - AM), and this equals D−B⋆MD - B \star M. So every major cycle starts from the true residual, whatever the minor cycles did.
  • Bounded drift. During a minor cycle that adds the components ΔM\Delta M, the image RR it keeps differs from the exact residual by at most βP∥ΔM∥1\beta_P \|\Delta M\|_1 at each pixel, where βP\beta_P is the largest beam value outside the patch.
  • When a minor step helps. If the drift at the chosen pixel is at most ee, the step lowers the true misfit as long as the peak it sees exceeds 2e/(2−g)2e/(2 - g). With the full beam as patch, e=0e = 0, and the minor cycle is Hogbom CLEAN on the exact convolution, with the proved descent.
  • Not claimed: convergence for a cut beam. If a minor cycle goes too far, the true misfit can rise. On the data of the example with a patch of half-width 1, no cycle factor and 150 minor steps per cycle, the misfit after 1, 2, 4 and 8 major cycles is 0.6400.640, 0.3280.328, 0.3100.310 and 1.0361.036. With Clark's factor for that patch, 0.26720.2672, it is 0.1290.129, 0.01750.0175, 0.00160.0016 and 0.00030.0003. Clark's rule is a heuristic that works in practice, not a theorem.

The full proof is below.

Proof: Each major cycle of Cotton-Schwab CLEAN restores the exact residual, and the drift of a minor cycle is bounded by the sidelobes outside the patchfull

Setting. The image has nn pixels at directions (lq,mq)(l_q, m_q). The data are visibilities VjV_j at (uj,vj)(u_j, v_j) with weights wj>0w_j > 0, W=∑jwjW = \sum_j w_j. For a real model MM, (AM)j=∑qMqajq(AM)_j = \sum_q M_q a_{jq} with ajq=e−2πi(ujlq+vjmq)a_{jq} = e^{-2\pi i (u_j l_q + v_j m_q)}, and for complex values XX the image is

I(X)q=1W∑jwj Re(Xj ajq‾).\mathcal I(X)_q = \frac1W \sum_j w_j\, \mathrm{Re}\big(X_j\, \overline{a_{jq}}\big).

The beam is B(q−r)=1W∑jwjcos⁡2π(uj(lq−lr)+vj(mq−mr))B(q - r) = \frac1W \sum_j w_j \cos 2\pi\big(u_j (l_q - l_r) + v_j (m_q - m_r)\big), which depends only on the offset between two pixels of a regular grid; B(0)=1B(0) = 1. The misfit is Q(M)=1W∑jwj∣Vj−(AM)j∣2Q(M) = \frac1W \sum_j w_j |V_j - (AM)_j|^2. For a patch half-width PP, let BP(Δ)=B(Δ)B_P(\Delta) = B(\Delta) when ∣Δ∣≤P|\Delta| \le P in each coordinate and 00 otherwise, and βP=max⁡Δ∣B(Δ)−BP(Δ)∣\beta_P = \max_\Delta |B(\Delta) - B_P(\Delta)|, the largest beam value outside the patch.

Claim 1 (the dirty image is a convolution). For every real image xx, I(Ax)q=∑rB(q−r) xr\mathcal I(Ax)_q = \sum_r B(q - r)\, x_r.

Proof. I(Ax)q=1W∑jwjRe∑rxrajrajq‾=∑rxr1W∑jwjRe e2πi(uj(lq−lr)+vj(mq−mr))\mathcal I(Ax)_q = \frac1W \sum_j w_j \mathrm{Re} \sum_r x_r a_{jr} \overline{a_{jq}} = \sum_r x_r \frac1W \sum_j w_j \mathrm{Re}\, e^{2\pi i (u_j (l_q - l_r) + v_j (m_q - m_r))}, and the inner sum is B(q−r)B(q - r). □\square

Claim 2 (each major cycle starts from the exact residual). After line L7, Vr=V−AMV_r = V - AM, so line L3 gives R=I(V−AM)=D−B⋆MR = \mathcal I(V - AM) = D - B \star M, with D=I(V)D = \mathcal I(V) and ⋆\star the convolution of Claim 1. The same holds for the image returned by line L8.

Proof. I\mathcal I is linear over real scalars, and Claim 1 gives I(AM)=B⋆M\mathcal I(AM) = B \star M. Lines L1 and L7 are the only lines that set VrV_r, and both make Vr=V−AMV_r = V - AM for the current MM. □\square

So the state that a major cycle passes on is the model MM alone; whatever error the minor cycle made in its image is dropped.

Claim 3 (the drift of a minor cycle). Let a minor cycle start from the exact residual RstartR^{\mathrm{start}} and add the components ΔM\Delta M (the sum of its steps). Let R~\tilde R be the image it keeps and R=I(V−A(M+ΔM))R = \mathcal I(V - A(M + \Delta M)) the exact residual. Then at every pixel

∣R~q−Rq∣≤βP ∥ΔM∥1.|\tilde R_q - R_q| \le \beta_P\, \|\Delta M\|_1 .

Proof. Each step at pixel pp with flux cc subtracts c BP(⋅−p)c\,B_P(\cdot - p) from R~\tilde R, so R~=Rstart−BP⋆ΔM\tilde R = R^{\mathrm{start}} - B_P \star \Delta M. By Claims 1 and 2, R=Rstart−B⋆ΔMR = R^{\mathrm{start}} - B \star \Delta M. Hence R~q−Rq=∑r(B−BP)(q−r) ΔMr\tilde R_q - R_q = \sum_r (B - B_P)(q - r)\, \Delta M_r, and each term is at most βP∣ΔMr∣\beta_P |\Delta M_r|. □\square

Claim 4 (the cost of one component). For every pixel pp and real cc, with R=I(V−AM)R = \mathcal I(V - AM),

Q(M+c δp)=Q(M)−2c Rp+c2.Q(M + c\,\delta_p) = Q(M) - 2c\, R_p + c^2 .

Proof. With X=V−AMX = V - AM, Q(M+cδp)=1W∑jwj∣Xj−c ajp∣2Q(M + c\delta_p) = \frac1W \sum_j w_j |X_j - c\, a_{jp}|^2. Since ∣ajp∣=1|a_{jp}| = 1, this is Q(M)−2cW∑jwjRe(Xjajp‾)+c2W∑jwj=Q(M)−2cRp+c2Q(M) - \frac{2c}{W} \sum_j w_j \mathrm{Re}(X_j \overline{a_{jp}}) + \frac{c^2}{W} \sum_j w_j = Q(M) - 2cR_p + c^2. □\square

Claim 5 (when a minor step lowers the true misfit). Suppose a minor step sees the value R~p\tilde R_p at its chosen pixel, takes c=gR~pc = g \tilde R_p with 0<g<20 < g < 2, and the drift there is ∣R~p−Rp∣≤e|\tilde R_p - R_p| \le e. Then

Q(M)−Q(M+c δp)≥g ∣R~p∣ ((2−g)∣R~p∣−2e),Q(M) - Q(M + c\,\delta_p) \ge g\, |\tilde R_p|\, \big((2 - g)|\tilde R_p| - 2e\big),

which is positive when ∣R~p∣>2e/(2−g)|\tilde R_p| > 2e / (2 - g).

Proof. By Claim 4 the drop is 2cRp−c2=gR~p(2Rp−gR~p)2cR_p - c^2 = g \tilde R_p (2R_p - g \tilde R_p). Write Rp=R~p+ϵR_p = \tilde R_p + \epsilon with ∣ϵ∣≤e|\epsilon| \le e. Then R~pRp=R~p2+R~pϵ≥R~p2−∣R~p∣e\tilde R_p R_p = \tilde R_p^2 + \tilde R_p \epsilon \ge \tilde R_p^2 - |\tilde R_p| e, so the drop is at least g(2R~p2−2∣R~p∣e−gR~p2)g(2\tilde R_p^2 - 2|\tilde R_p| e - g \tilde R_p^2). □\square

Combined with Claim 3, a minor step is safe while its peak exceeds 2βP∥ΔM∥1/(2−g)2\beta_P \|\Delta M\|_1 / (2 - g). This is the reason to stop a minor cycle early. Clark's rule, which stops at βP\beta_P times the starting peak, follows the same idea but does not bound ∥ΔM∥1\|\Delta M\|_1, so it is not a proof of descent.

Claim 6 (the full patch). If the patch holds the whole beam (βP=0\beta_P = 0), the minor cycle is Hogbom CLEAN on the exact non-periodic convolution, every step lowers QQ by (2g−g2)Rp2(2g - g^2) R_p^2, and for 0<g<20 < g < 2 and τ=0\tau = 0, max⁡q∣Rq∣→0\max_q |R_q| \to 0 and Q(M)Q(M) tends to its least value.

Proof. By Claim 3 the kept image is exact, so Claim 4 with c=gRpc = gR_p gives the drop. As on the Hogbom page, the drops have a finite sum and line L3 of the minor cycle takes the largest ∣Rq∣|R_q|, so max⁡q∣Rq∣→0\max_q |R_q| \to 0. Now Q(M)=Q(0)−2⟨D,M⟩+⟨M,HM⟩Q(M) = Q(0) - 2\langle D, M\rangle + \langle M, HM\rangle with Hqr=B(q−r)H_{qr} = B(q - r), and ⟨M,HM⟩=1W∑jwj∣(AM)j∣2≥0\langle M, HM \rangle = \frac1W \sum_j w_j |(AM)_j|^2 \ge 0, so HH is positive semidefinite, and R=D−HMR = D - HM is minus half the gradient. A convex quadratic that is bounded below satisfies Q(M)−min⁡Q=⟨R,H+R⟩≤∥R∥2/λQ(M) - \min Q = \langle R, H^{+} R\rangle \le \|R\|^2 / \lambda, with λ\lambda the smallest nonzero eigenvalue of HH, and the right side tends to 00. □\square

What is not claimed. For βP>0\beta_P > 0 the method can raise QQ when the minor cycles go too far, and the page shows a run where it does. No convergence theorem is given for a cut beam with Clark's rule.

Complexity

With mm visibilities and nn pixels, the direct sums of lines L3 and L7 take Θ(mn)\Theta(mn) time per major cycle. A minor step takes Θ(n)\Theta(n) for the peak search and Θ(b)\Theta(b) for the patch, where b=(2P+1)2b = (2P + 1)^2. So CC major cycles and KK minor steps take Θ(Cmn+K(n+b))\Theta(Cmn + K(n + b)). Imagers replace the direct sums by gridding and the FFT, or by the nonuniform FFT, which cost Θ(ms2+nlog⁡n)\Theta(m s^2 + n \log n) per cycle with a kernel of s×ss \times s cells. Because a major cycle is expensive, the art is in doing as few as possible: Clark's cycle factor lets each minor cycle go as far as it safely can.

Properties

  • The residual is exact after each major cycle. Errors of the minor cycle do not build up across cycles.
  • Model on the visibilities. The model is subtracted from the data themselves, so gridding errors, a beam that changes over the field and the ww term can be handled in the prediction step.
  • The same answer as Hogbom CLEAN in the limit of a full patch.
  • Not monotone in general. A minor cycle with a cut beam can raise the misfit; see the correctness section.
  • The usual CLEAN behavior. Greedy, nonlinear, and no noise model; the threshold τ\tau guards against fitting the noise.

Variants and improvements

  • Clark CLEAN. Major cycles on images: the residual image is recomputed by an FFT convolution of the model with the full beam, not from the visibilities. It removes the drift of the cut beam but not gridding errors.
  • Multiscale minor cycles. The minor cycle is multiscale CLEAN in place of point CLEAN.
  • Wide-field prediction. Line L7 predicts with a ww correction, such as W-projection, so that sources far from the field center are subtracted correctly.
  • Self-calibration. Between major cycles, the model visibilities are used to solve for antenna gain errors; this was the setting of Schwab (1984).

When to use it

Use Cotton-Schwab CLEAN for any real interferometer data set: it is the standard frame of radio imaging software. Use it when the data are not on a grid, when the field is wide, or when calibration is still being refined. Use plain Hogbom CLEAN only for a small image with a known beam, or for teaching.

Pitfalls

  • Minor cycles that go too far. With a small patch and a cycle factor below the outside sidelobe level, the minor cycle cleans sidelobes that it never subtracted, and the true misfit can rise. Use Clark's rule or a larger patch.
  • Too many major cycles. Each one costs a full pass over the visibilities. A cycle factor that is too high wastes them.
  • A model that is not on the image grid. The prediction in line L7 must use the same pixel positions and the same sign convention as the imaging in line L3, or the residual will not go down.
  • A threshold below the noise. As for every CLEAN, the components then fit the noise.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

cotton_schwab_clean.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/cotton-schwab-clean/. The authors are listed in prov/R1NZQT.jsonl.
"""Cotton-Schwab CLEAN with exact major cycles.

The image is n x n, img[i][j], row i from the top. Pixel (i, j) sits at the
direction cosines l = (j - n/2) dl and m = (n/2 - i) dl. A visibility is a
tuple (u, v, w, V) with weight w > 0, and the measurement equation is exact:

    V = sum over pixels of x[i][j] exp(-2 pi i (u l + v m)).

The dirty image is (1/W) sum w Re(V exp(2 pi i (u l + v m))), W = sum w. The
beam is stored as a dict from pixel offsets (di, dj) to values; the dirty
image is exactly the (non-periodic) convolution of the sky with it. The minor
cycle subtracts only a patch of the beam, so its residual drifts; each major
cycle recomputes the residual from the visibilities, which removes the drift.
"""

import cmath
import math


def predict(model, vis, dl):
    """Model visibilities: sum_p M_p exp(-2 pi i (u l_p + v m_p)) for each (u, v) of vis."""
    n = len(model)
    comps = [((j - n / 2) * dl, (n / 2 - i) * dl, model[i][j]) for i in range(n) for j in range(n) if model[i][j] != 0]
    out = []
    for u, v, _, _ in vis:
        out.append(sum(f * cmath.exp(-2j * math.pi * (u * l + v * m)) for l, m, f in comps))
    return out


def dirty_from(vis, values, n, dl):
    """The dirty image (1/W) sum_j w_j Re(values_j exp(2 pi i (u_j l + v_j m)))."""
    total = sum(w for _, _, w, _ in vis)
    img = [[0.0] * n for _ in range(n)]
    for (u, v, w, _), val in zip(vis, values):
        if w == 0:
            continue
        eu = [cmath.exp(2j * math.pi * u * (j - n / 2) * dl) for j in range(n)]
        for i in range(n):
            ev = cmath.exp(2j * math.pi * v * (n / 2 - i) * dl) * val * w / total
            row = img[i]
            for j in range(n):
                row[j] += (ev * eu[j]).real
    return img


def dirty_beam(vis, n, dl):
    """The beam B(di, dj) = (1/W) sum w cos(2 pi (u dj dl - v di dl)) for |di|, |dj| < n."""
    total = sum(w for _, _, w, _ in vis)
    beam = {}
    for di in range(-(n - 1), n):
        for dj in range(-(n - 1), n):
            beam[(di, dj)] = sum(w * math.cos(2 * math.pi * (u * dj - v * di) * dl) for u, v, w, _ in vis) / total
    return beam


def misfit(vis, model, dl):
    """Q(M) = (1/W) sum w |V - (A M)|^2, the misfit to the measured visibilities."""
    total = sum(w for _, _, w, _ in vis)
    pred = predict(model, vis, dl)
    return sum(w * abs(val - p) ** 2 for (_, _, w, val), p in zip(vis, pred)) / total


def minor_cycle(r, model, beam, patch, gain, limit, max_iter):
    """Hogbom steps on the image r with the beam cut to |di|, |dj| <= patch, until max |r| <= limit."""
    n = len(r)
    b0 = beam[(0, 0)]
    steps = 0
    while steps < max_iter:
        i0, j0 = max(((i, j) for i in range(n) for j in range(n)), key=lambda p: abs(r[p[0]][p[1]]))
        peak = r[i0][j0]
        if abs(peak) <= limit:
            break
        c = gain * peak / b0
        model[i0][j0] += c
        for i in range(max(0, i0 - patch), min(n, i0 + patch + 1)):
            for j in range(max(0, j0 - patch), min(n, j0 + patch + 1)):
                r[i][j] -= c * beam[(i - i0, j - j0)]
        steps += 1
    return steps


def outside_sidelobe(beam, patch):
    """The largest |B| outside the patch |di|, |dj| <= patch (0 when the patch holds the whole beam)."""
    return max((abs(b) for (di, dj), b in beam.items() if max(abs(di), abs(dj)) > patch), default=0.0)


def cotton_schwab_clean(vis, n, dl, patch, gain=0.1, threshold=0.0, cycle_factor=None, max_major=10, max_minor=500):
    """Deconvolve visibilities. Returns (model, residual image, number of major cycles, minor steps per cycle).

    A minor cycle stops when its peak falls to cycle_factor times the peak at the
    start of the cycle; by default the factor is the largest beam value outside
    the patch, as in Clark's rule. The residual image returned is exact: it is
    computed from V - A M.
    """
    beam = dirty_beam(vis, n, dl)
    if cycle_factor is None:
        cycle_factor = outside_sidelobe(beam, patch)
    model = [[0.0] * n for _ in range(n)]  # @line L1
    data = [val for _, _, _, val in vis]
    res_vis = list(data)
    steps = []
    for _ in range(max_major):  # @line L2
        r = dirty_from(vis, res_vis, n, dl)  # @line L3
        peak = max(abs(x) for row in r for x in row)
        if peak <= threshold:  # @line L4
            break
        limit = max(threshold, cycle_factor * peak)  # @line L5
        steps.append(minor_cycle(r, model, beam, patch, gain, limit, max_minor))  # @line L6
        pred = predict(model, vis, dl)  # @line L7
        res_vis = [d - p for d, p in zip(data, pred)]
    r = dirty_from(vis, res_vis, n, dl)  # @line L8
    return model, r, len(steps), steps
test_cotton_schwab_clean.py 109 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/cotton-schwab-clean/. The authors are listed in prov/R1NZQT.jsonl.

import random

from cotton_schwab_clean import (
    cotton_schwab_clean,
    dirty_beam,
    dirty_from,
    minor_cycle,
    misfit,
    outside_sidelobe,
    predict,
)


def make_vis(seed, n, pairs, sources, umax=6.0):
    """Random uv points in conjugate pairs, and the exact visibilities of a sky of point sources."""
    rng = random.Random(seed)
    dl = 1.0 / 16
    sky = [[0.0] * n for _ in range(n)]
    for i, j, f in sources:
        sky[i][j] = f
    uv = []
    for _ in range(pairs):
        u, v = rng.uniform(-umax, umax), rng.uniform(-umax, umax)
        uv += [(u, v, 1.0, 0), (-u, -v, 1.0, 0)]
    vals = predict(sky, uv, dl)
    return sky, [(u, v, w, val) for (u, v, w, _), val in zip(uv, vals)], dl


PAGE_SOURCES = ((4, 5, 1.0), (10, 11, 0.7), (6, 12, 0.5), (12, 3, 0.4))


def max_abs(img):
    return max(abs(x) for row in img for x in row)


def diff(a, b):
    return max(abs(x - y) for ra, rb in zip(a, b) for x, y in zip(ra, rb))


def test_dirty_image_is_the_beam_convolution():
    n = 10
    sky, vis, dl = make_vis(3, n, 40, ((2, 3, 1.0), (7, 6, -0.5), (5, 5, 0.8)))
    beam = dirty_beam(vis, n, dl)
    assert abs(beam[(0, 0)] - 1.0) < 1e-12
    dirty = dirty_from(vis, [val for _, _, _, val in vis], n, dl)
    conv = [[sum(beam[(i - a, j - b)] * sky[a][b] for a in range(n) for b in range(n)) for j in range(n)] for i in range(n)]
    assert diff(dirty, conv) < 1e-10


def test_full_patch_minor_cycle_is_exact():
    n = 10
    sky, vis, dl = make_vis(5, n, 40, ((2, 3, 1.0), (7, 6, 0.6)))
    beam = dirty_beam(vis, n, dl)
    r = dirty_from(vis, [val for _, _, _, val in vis], n, dl)
    model = [[0.0] * n for _ in range(n)]
    minor_cycle(r, model, beam, n - 1, 0.2, 0.0, 40)
    exact = dirty_from(vis, [val - p for (_, _, _, val), p in zip(vis, predict(model, vis, dl))], n, dl)
    assert diff(r, exact) < 1e-10


def test_minor_cycle_drift_is_bounded_by_the_outside_sidelobe():
    n = 12
    sky, vis, dl = make_vis(7, n, 50, ((3, 3, 1.0), (8, 9, 0.8), (5, 10, 0.5)))
    beam = dirty_beam(vis, n, dl)
    patch = 2
    far = outside_sidelobe(beam, patch)
    assert 0.0 < far < 1.0
    r = dirty_from(vis, [val for _, _, _, val in vis], n, dl)
    model = [[0.0] * n for _ in range(n)]
    minor_cycle(r, model, beam, patch, 0.2, 0.0, 30)
    exact = dirty_from(vis, [val - p for (_, _, _, val), p in zip(vis, predict(model, vis, dl))], n, dl)
    l1 = sum(abs(x) for row in model for x in row)
    assert diff(r, exact) <= far * l1 + 1e-12
    assert diff(r, exact) > 1e-3


def test_major_cycles_remove_the_drift():
    n = 16
    sky, vis, dl = make_vis(1, n, 100, PAGE_SOURCES)
    q0 = misfit(vis, [[0.0] * n for _ in range(n)], dl)
    one, _, _, _ = cotton_schwab_clean(vis, n, dl, 3, gain=0.2, cycle_factor=0.0, max_major=1, max_minor=150)
    many, res, cycles, steps = cotton_schwab_clean(vis, n, dl, 3, gain=0.2, max_major=8, max_minor=150)
    q_one, q_many = misfit(vis, one, dl), misfit(vis, many, dl)
    assert cycles == 8 and len(steps) == 8
    assert q_many < 1e-3 * q0
    assert q_many < 0.01 * q_one
    exact = dirty_from(vis, [val - p for (_, _, _, val), p in zip(vis, predict(many, vis, dl))], n, dl)
    assert diff(res, exact) < 1e-12
    assert max_abs(res) < 0.01


def test_clark_cycle_factor_versus_minor_cycles_that_go_too_far():
    n = 16
    sky, vis, dl = make_vis(1, n, 100, PAGE_SOURCES)
    deep, _, _, _ = cotton_schwab_clean(vis, n, dl, 1, gain=0.2, cycle_factor=0.0, max_major=8, max_minor=150)
    clark, _, _, _ = cotton_schwab_clean(vis, n, dl, 1, gain=0.2, max_major=8, max_minor=150)
    assert abs(misfit(vis, deep, dl) - 1.036) < 1e-3
    assert misfit(vis, clark, dl) < 1e-3


def test_threshold_stops_before_any_cycle():
    n = 8
    sky, vis, dl = make_vis(2, n, 20, ((3, 3, 1.0),))
    model, res, cycles, steps = cotton_schwab_clean(vis, n, dl, 2, threshold=10.0)
    assert cycles == 0 and steps == []
    assert max_abs(model) == 0.0

History

Hogbom (1974) cleaned the dirty image with the full beam. Clark (1980) made this faster with minor cycles that use a small patch of the beam and only the brightest pixels, and major cycles that recompute the residual image with the FFT. Schwab (1984), in a paper on self-calibration, moved the major cycle to the visibilities: subtract the model from the ungridded data and image the result again. The method carries his name and that of W. D. Cotton. This form became the standard frame of radio imaging, and modern imagers such as WSClean are built around it.

References

  1. F. R. Schwab, Relaxing the isoplanatism assumption in self-calibration; applications to low-frequency radio interferometry, The Astronomical Journal 89, 1984, the article from page 1076 (the CLEAN variant whose major cycles subtract the model from the ungridded visibilities)
  2. Clark, An efficient implementation of the algorithm CLEAN, 1980, the article (minor cycles with a beam patch and the brightest pixels, major cycles with the FFT)
  3. A. Richard Thompson, James M. Moran and George W. Swenson Jr., Interferometry and Synthesis in Radio Astronomy, third edition, Springer, 2017, Chapter 11, Further Imaging Techniques (the CLEAN variants)
  4. A. R. Offringa et al., WSClean, an implementation of a fast, generic wide-field imager for radio astronomy, Monthly Notices of the Royal Astronomical Society 444(1), 2014, pages 606 to 619 (a modern imager built on major and minor cycles)

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.average
  • a full proof of time.best
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example