bobleesj commited on
Commit
b1e9935
·
verified ·
1 Parent(s): 5bc34fc

workshop: add Step 4b rotation curl plot

Browse files
Files changed (1) hide show
  1. notebooks/berk_workshop_v1.ipynb +49 -0
notebooks/berk_workshop_v1.ipynb CHANGED
@@ -351,6 +351,55 @@
351
  ")"
352
  ]
353
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
354
  {
355
  "cell_type": "markdown",
356
  "id": "6c89c61d",
 
351
  ")"
352
  ]
353
  },
354
+ {
355
+ "cell_type": "markdown",
356
+ "id": "e8050ac2",
357
+ "metadata": {},
358
+ "source": [
359
+ "## Step 4b — Scan-detector rotation from CoM curl\n",
360
+ "\n",
361
+ "`DirectPtychography` needs the rotation between the scan axes and the detector\n",
362
+ "axes. Find it by sweeping rotation angles and minimizing the curl of the\n",
363
+ "rotated CoM field. (Auto-estimated internally by `from_dataset4d` too — this\n",
364
+ "cell exposes the curve so you can see WHERE the minimum lands.)"
365
+ ]
366
+ },
367
+ {
368
+ "cell_type": "code",
369
+ "execution_count": null,
370
+ "id": "414e6538",
371
+ "metadata": {},
372
+ "outputs": [],
373
+ "source": [
374
+ "import matplotlib.pyplot as plt\n",
375
+ "\n",
376
+ "# Fit origin (needed by estimate_detector_rotation) + run estimate.\n",
377
+ "com_model.fit_origin_background()\n",
378
+ "com_model.estimate_detector_rotation()\n",
379
+ "print(f\"detector rotation = {com_model.detector_rotation_deg:.2f}°\")\n",
380
+ "\n",
381
+ "# Recompute the curl-vs-angle curve to plot it.\n",
382
+ "angles_deg = torch.arange(-89, 90, 1, device=com_model.device, dtype=torch.float32)\n",
383
+ "com_t = com_model.origin_measured.view(*dset.shape[:2], 2)\n",
384
+ "com_n = com_t - com_t.mean(dim=(0, 1))\n",
385
+ "rot_rad = torch.deg2rad(angles_deg)[:, None, None]\n",
386
+ "rx = torch.cos(rot_rad) * com_n[None, ..., 0] - torch.sin(rot_rad) * com_n[None, ..., 1]\n",
387
+ "ry = torch.sin(rot_rad) * com_n[None, ..., 0] + torch.cos(rot_rad) * com_n[None, ..., 1]\n",
388
+ "grad_xy = torch.gradient(rx, dim=-1)[0]\n",
389
+ "grad_yx = torch.gradient(ry, dim=-2)[0]\n",
390
+ "curl = torch.mean(torch.abs(grad_yx - grad_xy), dim=(-2, -1)).cpu().numpy()\n",
391
+ "angles = angles_deg.cpu().numpy()\n",
392
+ "\n",
393
+ "fig, ax = plt.subplots(figsize=(6, 3.5))\n",
394
+ "ax.plot(angles, curl, lw=1.5)\n",
395
+ "ax.axvline(com_model.detector_rotation_deg, color=\"red\", lw=1, label=f\"{com_model.detector_rotation_deg:.1f}°\")\n",
396
+ "ax.set_xlabel(\"rotation angle (deg)\")\n",
397
+ "ax.set_ylabel(\"mean |curl(CoM)|\")\n",
398
+ "ax.set_title(\"Scan-detector rotation from CoM curl\")\n",
399
+ "ax.legend()\n",
400
+ "plt.tight_layout(); plt.show()"
401
+ ]
402
+ },
403
  {
404
  "cell_type": "markdown",
405
  "id": "6c89c61d",