# Drag Force Particle Tracking Automatic

Tracks brightfield particles across multiple stage speeds and outputs centroid data for drag force analysis.

---

## Workflow Overview

1. **Load images** — Scans a main folder for speed subfolders (e.g. `0.1`, `0.15`, `0.2`, `0.25`). Each subfolder contains a TIFF image sequence for one stage speed. Images are converted to grayscale, cropped, and inverted.

2. **Set tracking parameters** — Configure the number of particles, bounding radius, mask radius, and initial ring radius guess in the setup cell.

3. **Click particle centers** — An interactive figure opens for each speed. Click the center of each particle in frame 0. Clicks are stored and used as the initial guess for tracking.

4. **Test run** — Tracks the first 10 frames per speed to verify settings before committing to a full run.

5. **Verify** — Overlays the fitted rings on frame 0 for each speed. If the circles look off, adjust click positions or tracking parameters and re-run the test.

6. **Full tracking** — Runs the centroid-fitting algorithm across all frames for every speed. Each frame uses the previous frame's result as the next initial guess.

7. **Save output** — Choose an output folder using the widget. Saves one CSV and one JSON metadata file per speed.

---

## Key Parameters (Setup Cell)

| Parameter | Description |
|---|---|
| `MAIN_FOLDER` | Path to the folder containing speed subfolders |
| `TIFF_PATTERN` | Glob pattern for images (default: `*.tiff`) |
| `crop_dimensions` | Pixel crop region applied to every frame |
| `num_particles` | Number of particles to track per frame |
| `particle_r_bound` | Search radius (px) around each particle centroid |
| `mask_r` | Masks bright pixels near the particle center (px) |
| `r0` | Initial guess for the bright ring radius (px) |

---

## Output Format

For each speed subfolder, two files are saved:

- **`{speed} mms.csv`** — No header. Each row is one frame. Columns are `cx0, cy0, r0, cx1, cy1, r1, cx2, cy2, r2, ...` (3 columns per particle).
- **`{speed} mms.json`** — Metadata: tracking parameters and initial click positions.

---

## Dependencies

`numpy`, `pandas`, `scipy`, `scikit-image`, `pims`, `trackpy`, `matplotlib`, `ipywidgets`, `ipympl`

Install missing packages with `pip install <package>`.

---

## Analysis

Load the output CSVs into `Drag_auto.ipynb` to select stationary/movement regions interactively and compute drag force vs. displacement per particle.
