Skip to content

Batch processing

The idea in plain words

The pipeline page served one dinner: the analysis steps were the courses, and one time-lapse was the ingredients. Now a whole table of guests orders the same set menu, each bringing their own ingredients (one file each). The recipe does not change. What changes is that the kitchens light their ovens once and keep them hot, the waiter brings the plates one after another, and at the end writes one bill for the whole table.

In image terms: run the same pipeline over every time-lapse in a folder, start the workers once and reuse them, collect one row per file (file name and a few numbers), write one CSV for the folder, and draw a summary plot.

flowchart LR
    F["for each file<br/>in the folder"]:::script
    P["the pipeline<br/>(open, segment,<br/>track, count)"]:::fiji
    W["Wavu steps<br/>(workers stay on)"]:::worker
    E[("their own<br/>pixi envs")]:::env
    R["one row:<br/>file + numbers"]:::script
    C["one CSV +<br/>one plot"]:::script
    F --> P --> R
    P <--> W
    W -.- E
    R -- "next file" --> F
    R -- "at the end" --> C

    classDef fiji   fill:#e3f2fd,stroke:#1565c0,color:#0d47a1
    classDef worker fill:#e8f5e9,stroke:#2e7d32,color:#1b5e20
    classDef script fill:#fff3e0,stroke:#ef6c00,color:#e65100
    classDef env    fill:#f3e5f5,stroke:#6a1b9a,color:#4a148c

Before you start

  • Get the pipeline working on one file first: Pipeline in Fiji or Pipeline in Python. This page uses the same example folders (examples/segment_frames, examples/track_trackpy) and the same settings; they come with the Wavu repository (open it in VS Code).
  • A folder of time-lapses (.tif). To try it, make a folder and copy examples/data/demo_timelapse.tif into it two or three times under different names. Every copy gives the same numbers, which is an easy check.
  • An output folder, different from the input folder.

The code

In all three versions the per-file pipeline is the code of the pipeline page, unchanged: same steps, same parameters. The only additions are the loop over the files, the summary row, and closing each file's windows so memory does not fill up. Each file still gets its own -counts.csv and plots; the folder gets batch-summary.csv and batch-summary.png.

Open File › New › Script…, choose Language › Groovy, paste, and press Run. Steps 1 to 5 are those of the Groovy pipeline, inside a closure (analyse) that returns one row; the only change in them is that the plot and the movie are saved and closed instead of shown.

#@ File (label = "Folder of time-lapses (.tif)", style = "directory") inDir
#@ File (label = "segment_frames folder", style = "directory") segmentDir
#@ File (label = "Output folder", style = "directory") outDir
// Batch: the pipeline of "Pipeline in Fiji" for every .tif in a folder,
// one row per file, one CSV and one plot for the folder.
import fiji.plugin.trackmate.Logger
import fiji.plugin.trackmate.Model
import fiji.plugin.trackmate.Settings
import fiji.plugin.trackmate.TrackMate
import fiji.plugin.trackmate.detection.LabelImageDetectorFactory
import fiji.plugin.trackmate.tracking.jaqaman.SparseLAPTrackerFactory
import ij.IJ
import ij.ImagePlus
import ij.gui.Line
import ij.gui.OvalRoi
import ij.gui.Overlay
import ij.gui.Plot
import ij.measure.ResultsTable
import java.awt.Color

def wavu = new GroovyShell(this.class.classLoader).evaluate(
        new File(IJ.getDirectory("imagej"), "scripts/Plugins/Wavu/lib/wavu_lib.groovy.txt"))

// The pipeline for one file (steps 1-5 of the pipeline page). Returns one row.
def analyse = { File file ->
    // 1. Open the time-lapse (native Fiji).
    def imp = IJ.openImage(file.path)
    if (imp == null) throw new IOException("cannot open " + file.name)
    def cal = imp.getCalibration()
    double dt = cal.frameInterval > 0 ? cal.frameInterval : 1
    int nFrames = imp.getNFrames()

    // 2. Segment every frame (Wavu). The worker started for the first file is reused.
    Map seg = wavu.run(segmentDir, "script", [sigma: 2, threshold: "otsu", min_area: 30], [image: imp])
    def labels = seg.images.labels
    labels.setCalibration(cal.copy())

    // 3. Track (native Fiji: TrackMate).
    def model = new Model()
    model.setLogger(Logger.VOID_LOGGER)
    def settings = new Settings(labels)
    settings.detectorFactory = new LabelImageDetectorFactory()
    settings.detectorSettings = settings.detectorFactory.getDefaultSettings()
    settings.detectorSettings["TARGET_CHANNEL"] = 1
    settings.detectorSettings["SIMPLIFY_CONTOURS"] = false
    settings.trackerFactory = new SparseLAPTrackerFactory()
    settings.trackerSettings = settings.trackerFactory.getDefaultSettings()
    settings.trackerSettings["LINKING_MAX_DISTANCE"] = 10d
    settings.trackerSettings["ALLOW_GAP_CLOSING"] = true
    settings.trackerSettings["GAP_CLOSING_MAX_DISTANCE"] = 10d
    settings.trackerSettings["MAX_FRAME_GAP"] = 2
    settings.trackerSettings["ALLOW_TRACK_SPLITTING"] = true
    settings.trackerSettings["SPLITTING_MAX_DISTANCE"] = 10d
    def trackmate = new TrackMate(model, settings)
    if (!trackmate.checkInput() || !trackmate.process())
        throw new RuntimeException("TrackMate: " + trackmate.getErrorMessage())
    def tm = model.getTrackModel()
    def trackIds = tm.trackIDs(true)

    // 4. Tabulate: cells per frame, and one row per track.
    def counts = new ResultsTable()
    List<Integer> perFrame = []
    for (int t = 0; t < nFrames; t++) {
        int n = model.getSpots().getNSpots(t, true)
        perFrame << n
        counts.addRow()
        counts.addValue("frame", t)
        counts.addValue("time_min", t * dt)
        counts.addValue("cells", n)
    }
    def tracks = new ResultsTable()
    int divisions = 0
    trackIds.each { id ->
        def spots = tm.trackSpots(id)
        int splits = spots.count { s -> tm.edgesOf(s).count { e -> tm.getEdgeSource(e) == s } > 1 }
        divisions += splits
        def frames = spots.collect { it.getFeature("FRAME") as int }
        tracks.addRow()
        tracks.addValue("track", id)
        tracks.addValue("spots", spots.size())
        tracks.addValue("first_frame", frames.min())
        tracks.addValue("last_frame", frames.max())
        tracks.addValue("divisions", splits)
    }
    counts.save(new File(outDir, imp.getShortTitle() + "-counts.csv").path)
    tracks.save(new File(outDir, imp.getShortTitle() + "-tracks.csv").path)

    // 5a. Plot cells per frame (saved, not shown).
    def plot = new Plot("Cells per frame", "time (" + (cal.getTimeUnit() ?: "frame") + ")", "cells")
    plot.setColor(new Color(0x1565c0))
    plot.setLineWidth(2)
    double[] times = (0..<nFrames).collect { it * dt } as double[]
    plot.add("line", times, perFrame as double[])
    plot.add("circle", times, perFrame as double[])
    plot.setLimits(0, (nFrames - 1) * dt, 0, perFrame.max() + 5)
    IJ.saveAs(plot.getImagePlus(), "PNG", new File(outDir, imp.getShortTitle() + "-counts.png").path)

    // 5b. Tracks on the last frame (saved, not shown).
    double px = cal.pixelWidth
    def overlay = new Overlay()
    trackIds.each { id ->
        def colour = Color.getHSBColor((float) ((id * 0.618034) % 1), 0.9f, 1f)
        tm.trackEdges(id).each { e ->
            def a = tm.getEdgeSource(e), b = tm.getEdgeTarget(e)
            def line = new Line(a.getDoublePosition(0) / px, a.getDoublePosition(1) / px,
                                b.getDoublePosition(0) / px, b.getDoublePosition(1) / px)
            line.setStrokeColor(colour)
            overlay.add(line)
        }
        tm.trackSpots(id).each { s ->
            double r = s.getFeature("RADIUS") / px
            def ring = new OvalRoi(s.getDoublePosition(0) / px - r, s.getDoublePosition(1) / px - r, 2 * r, 2 * r)
            ring.setStrokeColor(colour)
            ring.setPosition(1, 1, (s.getFeature("FRAME") as int) + 1)
            overlay.add(ring)
        }
    }
    imp.setOverlay(overlay)
    IJ.run(imp, "Grays", "")
    imp.setT(nFrames)
    def last = new ImagePlus("last frame", imp.getProcessor().duplicate())
    last.setOverlay(new Overlay())
    overlay.each { roi -> if (roi.getTPosition() in [0, nFrames]) last.getOverlay().add(roi) }
    IJ.saveAs(last.flatten(), "PNG", new File(outDir, imp.getShortTitle() + "-tracks.png").path)
    imp.close()                                          // free the memory before the next file
    labels.close()

    return [frames: nFrames, times: times, perFrame: perFrame,
            tracks: trackIds.size(), divisions: divisions]
}

// The batch: loop, one row per file, one CSV and one plot for the folder.
def files = inDir.listFiles().findAll { it.name.toLowerCase().endsWith(".tif") }.sort { it.name }
def summary = new ResultsTable()
def all = new Plot("Cells per frame, all files", "time (min)", "cells")
try {
    files.eachWithIndex { File file, int i ->
        IJ.log("[${i + 1}/${files.size()}] ${file.name}")
        summary.addRow()
        summary.addValue("file", file.name)
        try {
            Map row = analyse(file)
            summary.addValue("frames", row.frames)
            summary.addValue("first_count", row.perFrame[0])
            summary.addValue("last_count", row.perFrame[-1])
            summary.addValue("mean_cells", (row.perFrame.sum() / row.frames) as double)
            summary.addValue("tracks", row.tracks)
            summary.addValue("divisions", row.divisions)
            summary.addValue("error", "")
            all.setColor(Color.getHSBColor((float) ((i * 0.618034) % 1), 0.8f, 0.8f))
            all.add("line", row.times, row.perFrame as double[])
        } catch (InterruptedException e) {
            throw e                                      // Esc: stop the whole batch
        } catch (Throwable e) {                          // also reader errors (Bio-Formats)
            IJ.log("  skipped ${file.name}: ${e}")          // one bad file: log it, go on
            summary.addValue("error", (e.message ?: e.toString()).readLines()[0])
        }
    }
} finally {
    wavu.stopAll()                                       // stop the worker once, at the end
}
summary.show("Batch summary")
summary.save(new File(outDir, "batch-summary.csv").path)
all.setLimitsToFit(false)
all.show()
IJ.saveAs(all.getImagePlus(), "PNG", new File(outDir, "batch-summary.png").path)

Open File › New › Script…, choose Language › IJ1 Macro, paste, and press Run. Steps 1 to 5 are those of the macro pipeline, unchanged, inside a for loop.

// Batch: the pipeline of "Pipeline in Fiji" (macro) for every .tif in a folder.
// From the command line:
//   ImageJ-win64.exe --console -batch batch_counting.ijm "<input folder>|<segment_frames>|<track_trackpy>|<output folder>"
arg = getArgument();
if (arg == "") {
    inDir = getDirectory("Choose the folder of time-lapses");
    segmentDir = getDirectory("Choose the segment_frames folder");
    trackDir = getDirectory("Choose the track_trackpy folder");
    outDir = getDirectory("Choose the output folder");
} else {
    a = split(arg, "|");
    inDir = a[0] + "/"; segmentDir = a[1]; trackDir = a[2]; outDir = a[3] + "/";
}
setBatchMode(true);
list = getFileList(inDir);
Table.create("Batch summary");

for (f = 0; f < list.length; f++) {
    if (!endsWith(toLowerCase(list[f]), ".tif")) continue;
    print("[" + (f + 1) + "/" + list.length + "] " + list[f]);
    timelapse = inDir + list[f];

    // 1. Open the time-lapse (native Fiji).
    open(timelapse);
    movie = getImageID();
    name = File.nameWithoutExtension;
    dt = Stack.getFrameInterval();
    if (dt == 0) dt = 1;
    Stack.getUnits(xu, yu, zu, timeUnit, vu);
    Stack.getDimensions(w, h, channels, slices, frames);

    // 2. Segment every frame (Wavu). The worker started for the first file is reused.
    run("Run Script (Macro)", "folder=[" + segmentDir + "] script=script params=[sigma=2 threshold=otsu min_area=30]");

    // 3. Track (Wavu: trackpy), given the detections table. Opens "tracks".
    run("Run Script (Macro)", "folder=[" + trackDir + "] script=script params=[search_range=15 memory=2] tables=[detections=" + name + "-detections]");

    // 4. Tabulate: cells per frame and number of tracks.
    n = Table.size("tracks");
    cells = newArray(frames);
    times = newArray(frames);
    nTracks = 0;
    for (i = 0; i < n; i++) {
        t = Table.get("frame", i, "tracks");
        cells[t] = cells[t] + 1;
        if (i == 0 || Table.get("particle", i, "tracks") != Table.get("particle", i - 1, "tracks"))
            nTracks++;
    }
    Table.create("Cells per frame");
    for (t = 0; t < frames; t++) {
        times[t] = t * dt;
        Table.set("frame", t, t);
        Table.set("time_" + timeUnit, t, times[t]);
        Table.set("cells", t, cells[t]);
    }
    Table.update;
    Table.save(outDir + name + "-counts.csv");

    // 5a. Plot cells per frame against time.
    Plot.create("Cells per frame", "time (" + timeUnit + ")", "cells");
    Plot.setColor("#1565c0");
    Plot.setLineWidth(2);
    Plot.add("line", times, cells);
    Plot.add("circle", times, cells);
    Array.getStatistics(cells, cmin, cmax, cmean);
    Plot.setLimits(0, (frames - 1) * dt, 0, cmax + 5);
    Plot.show();
    saveAs("PNG", outDir + name + "-counts.png");

    // 5b. Draw each track's path on the original, one colour per track.
    selectImage(movie);
    run("Grays");
    palette = newArray("#e6194b", "#3cb44b", "#ffe119", "#4363d8", "#f58231", "#911eb4",
                       "#46f0f0", "#f032e6", "#bcf60c", "#fabebe", "#008080", "#e6beff");
    Overlay.remove;
    for (i = 1; i < n; i++) {
        p = Table.get("particle", i, "tracks");
        if (Table.get("particle", i - 1, "tracks") != p) continue;
        setColor(palette[p % palette.length]);
        Overlay.drawLine(Table.get("x", i - 1, "tracks"), Table.get("y", i - 1, "tracks"),
                         Table.get("x", i, "tracks"), Table.get("y", i, "tracks"));
    }
    Overlay.show;
    Stack.setFrame(frames);
    run("Flatten", "slice");
    saveAs("PNG", outDir + name + "-tracks.png");

    // Batch: one row for this file, then close everything it opened.
    r = Table.size("Batch summary");
    Table.set("file", r, list[f], "Batch summary");
    Table.set("frames", r, frames, "Batch summary");
    Table.set("first_count", r, cells[0], "Batch summary");
    Table.set("last_count", r, cells[frames - 1], "Batch summary");
    Table.set("mean_cells", r, cmean, "Batch summary");
    Table.set("tracks", r, nTracks, "Batch summary");
    close("*");                                    // all images: movie, labels, plot, snapshot
    close(name + "-detections");
    close(name + "-count_per_frame");
    close("tracks");
    close("Cells per frame");
}

// The folder: one CSV and one summary plot.
Table.save(outDir + "batch-summary.csv", "Batch summary");
last = Table.getColumn("last_count", "Batch summary");
mean = Table.getColumn("mean_cells", "Batch summary");
x = Array.getSequence(last.length);
Plot.create("Batch summary", "file (0 = first)", "cells");
Plot.setColor("#1565c0");
Plot.add("circle", x, last);
Plot.setColor("#ef6c00");
Plot.add("circle", x, mean);
Plot.setLegend("final count\tmean per frame");
Plot.show();
saveAs("PNG", outDir + "batch-summary.png");
setBatchMode("exit and display");
run("Stop Worker");                                // stop both workers, once

Save this as batch_counting.py next to examples/pipelines/dag_counting.py, so it can import that file's folders and settings. analyse_one is the body of analyse() with one change: it is handed the two running workers instead of starting them, so they start once for the whole folder.

"""Batch: the dag_counting pipeline for every time-lapse in a folder.

    pixi run -e napari python examples/pipelines/batch_counting.py <folder> [<output folder>]
"""
import sys
from pathlib import Path

import pandas as pd
import tifffile
from matplotlib.figure import Figure

from dag_counting import FRAME_MIN, PIXEL_UM, SEGMENT, TRACK
from wavu.client import JobCancelled, Worker


def analyse_one(movie_path, seg, trk):
    """Steps 1-4 of dag_counting.analyse(), with workers that are already running."""
    movie = tifffile.imread(movie_path)
    res = seg.run("script", {"sigma": 2.0, "threshold": "otsu", "min_area": 30},
                  {"image": movie}, meta={"image": {"pixel_um": PIXEL_UM}})
    detections = res.outputs["detections"].value
    res = trk.run("script", {"search_range": 10.0, "memory": 2},
                  {"detections": detections})
    tracks = res.outputs["tracks"].value
    counts = (detections.groupby("frame").size()
              .reindex(range(len(movie)), fill_value=0).rename("cells").reset_index())
    counts["minutes"] = counts["frame"] * FRAME_MIN
    per_track = tracks.groupby("particle")["frame"].agg(start="min", end="max", length="size")
    row = {
        "frames": len(movie),
        "first_count": int(counts["cells"].iloc[0]),
        "last_count": int(counts["cells"].iloc[-1]),
        "mean_cells": float(counts["cells"].mean()),
        "tracks": int(tracks["particle"].nunique()),
        "new_tracks": int((per_track["start"] > 0).sum()),
    }
    return counts, row


def main(folder: Path, out: Path):
    out.mkdir(parents=True, exist_ok=True)
    files = sorted(folder.glob("*.tif"))
    rows, curves = [], {}
    with Worker.start(SEGMENT, on_log=print) as seg, Worker.start(TRACK, on_log=print) as trk:
        for i, path in enumerate(files, 1):
            print(f"[{i}/{len(files)}] {path.name}")
            try:
                counts, row = analyse_one(path, seg, trk)
            except JobCancelled:
                raise                                   # cancelled: stop the whole batch
            except Exception as e:                      # one bad file: log it, go on
                print(f"  skipped {path.name}: {e}")
                rows.append({"file": path.name, "error": str(e).split("\n")[0]})
                continue
            counts.to_csv(out / f"{path.stem}-counts.csv", index=False)
            rows.append({"file": path.name, **row})
            curves[path.stem] = counts

    summary = pd.DataFrame(rows)
    summary.to_csv(out / "batch-summary.csv", index=False)
    print(summary.to_string(index=False))

    fig = Figure(figsize=(6, 3.5), layout="constrained")
    ax = fig.add_subplot()
    for name, counts in curves.items():
        ax.plot(counts["minutes"], counts["cells"], label=name)
    ax.set_xlabel("time (min)")
    ax.set_ylabel("cells")
    ax.set_title("Cells per frame, all files")
    ax.legend(fontsize="small")
    fig.savefig(out / "batch-summary.png", dpi=120)


if __name__ == "__main__":
    main(Path(sys.argv[1]), Path(sys.argv[2]) if len(sys.argv) > 2 else Path("batch_out"))

What you get

In the output folder:

  • for each file, the same files as on the pipeline page: <name>-counts.csv, <name>-counts.png and <name>-tracks.png (Fiji), or <name>-counts.csv (Python);
  • for the folder, batch-summary.csv with one row per file, and batch-summary.png.
column meaning
file the file name
frames number of frames
first_count, last_count cells in the first and last frame
mean_cells mean cells per frame
tracks number of tracks
divisions Groovy only: TrackMate's track splits
new_tracks Python only: tracks that start after frame 0
error Groovy and Python: why a file was skipped

The summary plot shows every file's cells-per-frame curve on one axis (Groovy, Python), or the final and mean counts per file (macro). With copies of the demo movie, every row and every curve is the same: 25 cells at the start, 29 at the end.

Tips

  • Only the first file is slow. The first Wavu call starts the worker (and, the very first time, installs its kitchenware). Every later call reuses it. That is why the workers are stopped after the loop, not inside it.
  • One bad file should not stop the batch. The Groovy and Python versions catch the error, log the file name, add a row with an error column, and go on. An ImageJ macro cannot catch errors: a failed Wavu step stops the whole macro. The Log shows the last file it started; move that file out of the folder, or fix it, and run again.
  • Stopping. Press Esc in Fiji (or Ctrl+C in a terminal for Python). The running Wavu step is cancelled and the batch stops instead of going on to the next file. If a macro is stopped halfway, end the workers with Plugins › Wavu › Stop Worker.
  • Memory. Close every image and table a file opened before the next one (imp.close(), close("*")). In Python, the arrays are freed when the next file replaces them. Otherwise memory grows with every file.
  • Check before you trust. Open one or two per-file plots and track snapshots by eye. A summary table hides a bad segmentation very well.
  • When to switch to large data. Batch processing is for many files that each fit in memory (roughly 1–2 GB each). If a single file is larger, use stream analysis. For thousands of files or many hours of work, run the Python version in a terminal without a viewer, or on a cluster.

In one sentence

Write the pipeline for one file, wrap it in a loop, let the workers start once and serve every file, keep going past bad files, and collect one row per file into one CSV and one plot for the folder.

Next: Large data