bobleesj commited on
Commit
f3f7a23
·
verified ·
1 Parent(s): 9b71168

workshop: pdset.preprocess for DPC+rotation; pdset ready for v2 ptycho

Browse files
Files changed (1) hide show
  1. notebooks/berk_workshop_v1.ipynb +19 -65
notebooks/berk_workshop_v1.ipynb CHANGED
@@ -248,9 +248,15 @@
248
  "id": "50bcd0fe",
249
  "metadata": {},
250
  "source": [
251
- "## Step 3 — DPC via `CenterOfMassOriginModel`\n",
252
  "\n",
253
- "Per-scan-position centroid (CoM) on the GPU. Two-cell call, two interactive panels."
 
 
 
 
 
 
254
  ]
255
  },
256
  {
@@ -285,69 +291,17 @@
285
  }
286
  ],
287
  "source": [
288
- "from quantem.diffractive_imaging import CenterOfMassOriginModel\n",
289
- "\n",
290
- "com_model = CenterOfMassOriginModel.from_dataset(dset, device=\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
291
- "com_model.calculate_origin()\n",
292
- "com = com_model.origin_measured.view(*dset.shape[:2], 2).cpu().numpy()\n",
293
- "\n",
294
- "quantem.widget.Show2D(\n",
295
- " [com[..., 0], com[..., 1]],\n",
296
- " labels=[\"CoM row (qx)\", \"CoM col (qy)\"],\n",
297
- " sampling=meta[\"sampling\"][:2], units=meta[\"units\"][:2],\n",
298
- " cmap=\"RdBu_r\", link_contrast=False,\n",
299
- ")"
300
- ]
301
- },
302
- {
303
- "cell_type": "markdown",
304
- "id": "e8050ac2",
305
- "metadata": {},
306
- "source": [
307
- "## Step 3b — Scan-detector rotation from CoM curl\n",
308
- "\n",
309
- "`DirectPtychography` needs the rotation between the scan axes and the detector\n",
310
- "axes. Find it by sweeping rotation angles and minimizing the curl of the\n",
311
- "rotated CoM field. (Auto-estimated internally by `from_dataset4d` too — this\n",
312
- "cell exposes the curve so you can see WHERE the minimum lands.)"
313
- ]
314
- },
315
- {
316
- "cell_type": "code",
317
- "execution_count": null,
318
- "id": "414e6538",
319
- "metadata": {},
320
- "outputs": [],
321
- "source": [
322
- "import matplotlib.pyplot as plt\n",
323
- "\n",
324
- "# Fit origin (needed by estimate_detector_rotation) + run estimate.\n",
325
- "com_model.fit_origin_background()\n",
326
- "com_model.estimate_detector_rotation()\n",
327
- "best_rotation_deg = com_model.detector_rotation_deg\n",
328
- "best_rotation_rad = np.deg2rad(best_rotation_deg)\n",
329
- "print(f\"detector rotation = {best_rotation_deg:.2f}° ({best_rotation_rad:.4f} rad)\")\n",
330
- "\n",
331
- "# Recompute curl-vs-angle for the plot.\n",
332
- "angles_deg = torch.arange(-89, 90, 1, device=com_model.device, dtype=torch.float32)\n",
333
- "com_t = com_model.origin_measured.view(*dset.shape[:2], 2)\n",
334
- "com_n = com_t - com_t.mean(dim=(0, 1))\n",
335
- "rot_rad = torch.deg2rad(angles_deg)[:, None, None]\n",
336
- "rx = torch.cos(rot_rad) * com_n[None, ..., 0] - torch.sin(rot_rad) * com_n[None, ..., 1]\n",
337
- "ry = torch.sin(rot_rad) * com_n[None, ..., 0] + torch.cos(rot_rad) * com_n[None, ..., 1]\n",
338
- "grad_xy = torch.gradient(rx, dim=-1)[0]\n",
339
- "grad_yx = torch.gradient(ry, dim=-2)[0]\n",
340
- "curl = torch.mean(torch.abs(grad_yx - grad_xy), dim=(-2, -1)).cpu().numpy()\n",
341
- "angles = angles_deg.cpu().numpy()\n",
342
- "\n",
343
- "fig, ax = plt.subplots(figsize=(6, 3.5))\n",
344
- "ax.plot(angles, curl, lw=1.5)\n",
345
- "ax.axvline(best_rotation_deg, color=\"red\", lw=1, label=f\"{best_rotation_deg:.1f}°\")\n",
346
- "ax.set_xlabel(\"rotation angle (deg)\")\n",
347
- "ax.set_ylabel(\"mean |curl(CoM)|\")\n",
348
- "ax.set_title(\"Scan-detector rotation from CoM curl\")\n",
349
- "ax.legend()\n",
350
- "plt.tight_layout(); plt.show()"
351
  ]
352
  },
353
  {
 
248
  "id": "50bcd0fe",
249
  "metadata": {},
250
  "source": [
251
+ "## Step 3 — DPC + scan-detector rotation in one upstream call\n",
252
  "\n",
253
+ "`PtychographyDatasetRaster.preprocess(plot_rotation=True, plot_com=True)` runs\n",
254
+ "CoM, fits the origin background, finds the scan-detector rotation by curl\n",
255
+ "minimization, **and plots both** — all in one upstream call. Bonus: the `pdset`\n",
256
+ "this returns is ready for iterative ptycho later (v2).\n",
257
+ "\n",
258
+ "Takes ~30 s on T4 (the intensity normalization step is the bulk; we use the\n",
259
+ "plots + the rotation now)."
260
  ]
261
  },
262
  {
 
291
  }
292
  ],
293
  "source": [
294
+ "from quantem.diffractive_imaging import PtychographyDatasetRaster\n",
295
+ "\n",
296
+ "pdset = PtychographyDatasetRaster.from_dataset4dstem(dset)\n",
297
+ "pdset.preprocess(\n",
298
+ " com_fit_function=\"constant\",\n",
299
+ " plot_rotation=True,\n",
300
+ " plot_com=True,\n",
301
+ " probe_energy=meta[\"voltage_kV\"] * 1e3,\n",
302
+ ")\n",
303
+ "best_rotation_rad = pdset.com_rotation_rad\n",
304
+ "print(f\"detector rotation = {np.rad2deg(best_rotation_rad):.2f}° ({best_rotation_rad:.4f} rad)\")"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305
  ]
306
  },
307
  {