# Drag Force Particle Tracking

Two-notebook pipeline for measuring drag force on brightfield particles in a viscous fluid.

1. **`Drag Force Particle Tracking Automatic.ipynb`** — Track particle centroids from TIFF image sequences
2. **`Drag_auto.ipynb`** — Load tracking output, define movement regions, and compute drag force vs. displacement

---

## Part 1 — Particle Tracking

### Workflow

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

| 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.

---

## Part 2 — Drag Force Analysis

### Workflow

1. **Load CSVs** — Reads all `*.csv` files from a folder (output from Part 1). Automatically detects the number of particles from the column count.

2. **Set regions (slider cell)** — For each speed, an interactive plot shows the x-position traces for all particles over time. Use the three sliders to define:
   - **Stationary** — frames where the stage is not moving (baseline)
   - **Movement 1** — first direction of stage movement
   - **Movement 2** — second direction of stage movement (return)

   Click **Confirm All & Continue** when all speeds are set.

3. **Analyze** — Computes the mean displacement of each particle relative to its stationary baseline and the expected Stokes drag force at each speed. Fits a linear spring constant `k` (pN/µm) per particle per direction through the origin.

### Key Parameters

| Parameter | Description |
|---|---|
| `folder` | Path to folder containing `{speed} mms.csv` files |
| `PIXELS_PER_MICRON` | Pixel-to-micron conversion factor |
| `ETA` | Fluid viscosity (Pa·s); default: `0.001` (water) |
| `R` | Particle radius (m); default: `1e-6` (1 µm) |
| `TRIM` | Frames trimmed from each edge of a region to remove transients |

### Output Plot

- **Color = particle** (each particle gets a unique color)
- **Filled circles** = Direction 1, **open circles** = Direction 2
- **Solid fit line** = Direction 1, **dotted fit line** = Direction 2
- Separate origin-constrained fit per particle per direction with 95% confidence interval shading
- Printed spring constant `k ± σ` (pN/µm) for each particle and direction

---

## Dependencies

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

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