bobleesj commited on
Commit
42eaeb1
·
verified ·
1 Parent(s): b1e9935

workshop: explicit rotation pass to DirectPtychography

Browse files
Files changed (1) hide show
  1. notebooks/berk_workshop_v1.ipynb +13 -11
notebooks/berk_workshop_v1.ipynb CHANGED
@@ -376,9 +376,11 @@
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",
@@ -392,7 +394,7 @@
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",
@@ -407,13 +409,13 @@
407
  "source": [
408
  "## Step 5 — Phase retrieval: `DirectPtychography` + optuna aberration fit\n",
409
  "\n",
410
- "Build `DirectPtychography` once, then **fit the C10, C12, phi12 aberrations\n",
411
- "from the data itself** with a small optuna loop (30 trials, ~5 s on T4). The\n",
412
- "fitted coefs go into the final reconstructions.\n",
413
  "\n",
414
- "Why fit per-dataset: SSB silently returns zeros when the aberrations are\n",
415
- "wrong — it needs the probe phase profile to deconvolve. Fitting on each\n",
416
- "dataset means the workshop works on YOUR data, no prior calibration assumed."
417
  ]
418
  },
419
  {
@@ -437,11 +439,11 @@
437
  " dset,\n",
438
  " energy=meta[\"voltage_kV\"] * 1e3, # 300 kV -> 300000 eV\n",
439
  " semiangle_cutoff=meta[\"probe_semiangle_mrad\"] * 1e-3, # 30 mrad -> 0.030 rad\n",
440
- " rotation_angle=None, # auto-estimate\n",
441
  " device=\"cuda\" if torch.cuda.is_available() else \"cpu\",\n",
442
  " verbose=True,\n",
443
  ")\n",
444
- "print(\"DirectPtychography built\")"
445
  ]
446
  },
447
  {
 
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
+ "best_rotation_deg = com_model.detector_rotation_deg\n",
380
+ "best_rotation_rad = np.deg2rad(best_rotation_deg)\n",
381
+ "print(f\"detector rotation = {best_rotation_deg:.2f}° ({best_rotation_rad:.4f} rad)\")\n",
382
  "\n",
383
+ "# Recompute curl-vs-angle for the plot.\n",
384
  "angles_deg = torch.arange(-89, 90, 1, device=com_model.device, dtype=torch.float32)\n",
385
  "com_t = com_model.origin_measured.view(*dset.shape[:2], 2)\n",
386
  "com_n = com_t - com_t.mean(dim=(0, 1))\n",
 
394
  "\n",
395
  "fig, ax = plt.subplots(figsize=(6, 3.5))\n",
396
  "ax.plot(angles, curl, lw=1.5)\n",
397
+ "ax.axvline(best_rotation_deg, color=\"red\", lw=1, label=f\"{best_rotation_deg:.1f}°\")\n",
398
  "ax.set_xlabel(\"rotation angle (deg)\")\n",
399
  "ax.set_ylabel(\"mean |curl(CoM)|\")\n",
400
  "ax.set_title(\"Scan-detector rotation from CoM curl\")\n",
 
409
  "source": [
410
  "## Step 5 — Phase retrieval: `DirectPtychography` + optuna aberration fit\n",
411
  "\n",
412
+ "Build `DirectPtychography` with the **rotation found in Step 4b** (`best_rotation_rad`),\n",
413
+ "then **fit C10, C12, phi12 from the data** with a 30-trial optuna loop (~5 s on T4).\n",
414
+ "The fitted aberration coefs go into the final reconstructions.\n",
415
  "\n",
416
+ "Why fit per-dataset: SSB silently returns zeros when the aberrations are wrong —\n",
417
+ "it needs the probe phase profile to deconvolve. Fitting on each dataset means the\n",
418
+ "workshop works on YOUR data, no prior calibration assumed."
419
  ]
420
  },
421
  {
 
439
  " dset,\n",
440
  " energy=meta[\"voltage_kV\"] * 1e3, # 300 kV -> 300000 eV\n",
441
  " semiangle_cutoff=meta[\"probe_semiangle_mrad\"] * 1e-3, # 30 mrad -> 0.030 rad\n",
442
+ " rotation_angle=best_rotation_rad, # from Step 4b CoM curl\n",
443
  " device=\"cuda\" if torch.cuda.is_available() else \"cpu\",\n",
444
  " verbose=True,\n",
445
  ")\n",
446
+ "print(f\"DirectPtychography built with rotation = {np.rad2deg(best_rotation_rad):.2f}°\")"
447
  ]
448
  },
449
  {