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

workshop: BF+DF single widget; Show4DSTEM width; drop cross-modality

Browse files
Files changed (1) hide show
  1. notebooks/berk_workshop_v1.ipynb +27 -226
notebooks/berk_workshop_v1.ipynb CHANGED
@@ -175,7 +175,9 @@
175
  " torch.from_numpy(data).to(\"cuda\" if torch.cuda.is_available() else \"cpu\"),\n",
176
  " sampling=meta[\"sampling\"], units=meta[\"units\"], name=meta[\"name\"],\n",
177
  ")\n",
178
- "quantem.widget.Show4DSTEM(dset_torch)"
 
 
179
  ]
180
  },
181
  {
@@ -183,11 +185,11 @@
183
  "id": "c1faa1b5",
184
  "metadata": {},
185
  "source": [
186
- "## Step 2 — Bright field (BF) via `get_virtual_image`\n",
187
  "\n",
188
- "Upstream `Dataset4dstem.get_virtual_image(mode=\"circle\", geometry=((cy, cx), r))`\n",
189
- "makes the aperture mask + per-scan-position sum + returns a `Dataset2d`. One\n",
190
- "line. (`Show2D` shown alone so contrast is not yoked to anything else.)"
191
  ]
192
  },
193
  {
@@ -220,78 +222,24 @@
220
  }
221
  ],
222
  "source": [
 
 
223
  "H, W = dset.shape[-2:]\n",
224
  "cy, cx = H / 2, W / 2 # hardcoded geometric center\n",
225
  "BF_RADIUS_PX = 6.0\n",
 
226
  "\n",
227
- "bf_ds = dset.get_virtual_image(\n",
228
- " mode=\"circle\",\n",
229
- " geometry=((cy, cx), BF_RADIUS_PX),\n",
230
- " name=\"BF\",\n",
231
- ")\n",
232
  "print(f\"BF range [{bf_ds.array.min():.1f}, {bf_ds.array.max():.1f}]\")\n",
233
- "\n",
234
- "quantem.widget.Show2D(\n",
235
- " bf_ds.array, title=\"Bright field\",\n",
236
- " sampling=meta[\"sampling\"][:2], units=meta[\"units\"][:2],\n",
237
- " cmap=\"gray\",\n",
238
- ")"
239
- ]
240
- },
241
- {
242
- "cell_type": "markdown",
243
- "id": "6cc22c3c",
244
- "metadata": {},
245
- "source": [
246
- "## Step 3 — Dark field (DF) via `get_virtual_image` (annular)\n",
247
- "\n",
248
- "Annular mask: everything between the BF radius and the detector edge."
249
- ]
250
- },
251
- {
252
- "cell_type": "code",
253
- "execution_count": 43,
254
- "id": "79e0c9fc",
255
- "metadata": {},
256
- "outputs": [
257
- {
258
- "name": "stdout",
259
- "output_type": "stream",
260
- "text": [
261
- "DF range [100550.0, 181611.0]\n"
262
- ]
263
- },
264
- {
265
- "data": {
266
- "application/vnd.jupyter.widget-view+json": {
267
- "model_id": "e3316a8cbb794108b8bea37e1c9e7707",
268
- "version_major": 2,
269
- "version_minor": 1
270
- },
271
- "text/plain": [
272
- "Show2D(512×512, cmap=gray)"
273
- ]
274
- },
275
- "execution_count": 43,
276
- "metadata": {},
277
- "output_type": "execute_result"
278
- }
279
- ],
280
- "source": [
281
- "import math\n",
282
- "R_MAX = math.hypot(cy, cx) # corner-of-detector radius\n",
283
- "\n",
284
- "df_ds = dset.get_virtual_image(\n",
285
- " mode=\"annular\",\n",
286
- " geometry=((cy, cx), (BF_RADIUS_PX, R_MAX)),\n",
287
- " name=\"DF\",\n",
288
- ")\n",
289
  "print(f\"DF range [{df_ds.array.min():.1f}, {df_ds.array.max():.1f}]\")\n",
290
  "\n",
291
  "quantem.widget.Show2D(\n",
292
- " df_ds.array, title=\"Dark field\",\n",
 
293
  " sampling=meta[\"sampling\"][:2], units=meta[\"units\"][:2],\n",
294
  " cmap=\"gray\",\n",
 
295
  ")"
296
  ]
297
  },
@@ -300,7 +248,7 @@
300
  "id": "50bcd0fe",
301
  "metadata": {},
302
  "source": [
303
- "## Step 4 — DPC via `CenterOfMassOriginModel`\n",
304
  "\n",
305
  "Per-scan-position centroid (CoM) on the GPU. Two-cell call, two interactive panels."
306
  ]
@@ -356,7 +304,7 @@
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",
@@ -407,7 +355,7 @@
407
  "id": "6c89c61d",
408
  "metadata": {},
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",
@@ -504,7 +452,7 @@
504
  "id": "5ffc4fd8",
505
  "metadata": {},
506
  "source": [
507
- "## Step 6 — All three kernels side by side\n",
508
  "\n",
509
  "`link_contrast=False` so every kernel gets its own min/max (the SSB output is\n",
510
  "~3 orders of magnitude smaller than ICOM)."
@@ -542,153 +490,6 @@
542
  ")"
543
  ]
544
  },
545
- {
546
- "cell_type": "markdown",
547
- "id": "c2d0e9b4",
548
- "metadata": {},
549
- "source": [
550
- "## Step 7 — Phase retrieval vs classic imaging\n",
551
- "\n",
552
- "The workshop punchline.\n",
553
- "\n",
554
- "- **BF / DF**: intensity contrast from inside / outside the BF disk. Limited\n",
555
- " by probe size; atomic-lattice fringes mostly washed out.\n",
556
- "- **ICOM, parallax, SSB**: deconvolution of the full CBED against the probe\n",
557
- " transfer function. Sharper contrast at the same dose.\n",
558
- "\n",
559
- "Each panel its own `Show2D` widget — independent contrast."
560
- ]
561
- },
562
- {
563
- "cell_type": "code",
564
- "execution_count": 48,
565
- "id": "5367fee4",
566
- "metadata": {},
567
- "outputs": [
568
- {
569
- "data": {
570
- "application/vnd.jupyter.widget-view+json": {
571
- "model_id": "440f0bb7f30640178f0b0e0112dd3858",
572
- "version_major": 2,
573
- "version_minor": 1
574
- },
575
- "text/plain": [
576
- "Show2D(512×512, cmap=gray)"
577
- ]
578
- },
579
- "execution_count": 48,
580
- "metadata": {},
581
- "output_type": "execute_result"
582
- }
583
- ],
584
- "source": [
585
- "quantem.widget.Show2D(bf_ds.array, title=\"BF — intensity inside disk\", sampling=meta[\"sampling\"][:2], units=meta[\"units\"][:2], cmap=\"gray\")"
586
- ]
587
- },
588
- {
589
- "cell_type": "code",
590
- "execution_count": 49,
591
- "id": "6058a9b6",
592
- "metadata": {},
593
- "outputs": [
594
- {
595
- "data": {
596
- "application/vnd.jupyter.widget-view+json": {
597
- "model_id": "985bbf76e0494a729d899da1b20e50b7",
598
- "version_major": 2,
599
- "version_minor": 1
600
- },
601
- "text/plain": [
602
- "Show2D(512×512, cmap=gray)"
603
- ]
604
- },
605
- "execution_count": 49,
606
- "metadata": {},
607
- "output_type": "execute_result"
608
- }
609
- ],
610
- "source": [
611
- "quantem.widget.Show2D(df_ds.array, title=\"DF — intensity outside disk\", sampling=meta[\"sampling\"][:2], units=meta[\"units\"][:2], cmap=\"gray\")"
612
- ]
613
- },
614
- {
615
- "cell_type": "code",
616
- "execution_count": 50,
617
- "id": "faf40b28",
618
- "metadata": {},
619
- "outputs": [
620
- {
621
- "data": {
622
- "application/vnd.jupyter.widget-view+json": {
623
- "model_id": "522b3f5c064a4549b637db985960c9b7",
624
- "version_major": 2,
625
- "version_minor": 1
626
- },
627
- "text/plain": [
628
- "Show2D(512×512, cmap=magma)"
629
- ]
630
- },
631
- "execution_count": 50,
632
- "metadata": {},
633
- "output_type": "execute_result"
634
- }
635
- ],
636
- "source": [
637
- "quantem.widget.Show2D(phases[\"icom\"], title=\"ICOM — first-moment phase\", sampling=meta[\"sampling\"][:2], units=meta[\"units\"][:2], cmap=\"gray\")"
638
- ]
639
- },
640
- {
641
- "cell_type": "code",
642
- "execution_count": 51,
643
- "id": "63197495",
644
- "metadata": {},
645
- "outputs": [
646
- {
647
- "data": {
648
- "application/vnd.jupyter.widget-view+json": {
649
- "model_id": "2970116ba0134cd88d32e9adfa3c3a43",
650
- "version_major": 2,
651
- "version_minor": 1
652
- },
653
- "text/plain": [
654
- "Show2D(512×512, cmap=gray)"
655
- ]
656
- },
657
- "execution_count": 51,
658
- "metadata": {},
659
- "output_type": "execute_result"
660
- }
661
- ],
662
- "source": [
663
- "quantem.widget.Show2D(phases[\"parallax\"], title=\"parallax — phase retrieval\", sampling=meta[\"sampling\"][:2], units=meta[\"units\"][:2], cmap=\"gray\")"
664
- ]
665
- },
666
- {
667
- "cell_type": "code",
668
- "execution_count": 52,
669
- "id": "52f7726c",
670
- "metadata": {},
671
- "outputs": [
672
- {
673
- "data": {
674
- "application/vnd.jupyter.widget-view+json": {
675
- "model_id": "7f9e0ed1104b43a58814e76ae234bd22",
676
- "version_major": 2,
677
- "version_minor": 1
678
- },
679
- "text/plain": [
680
- "Show2D(512×512, cmap=gray)"
681
- ]
682
- },
683
- "execution_count": 52,
684
- "metadata": {},
685
- "output_type": "execute_result"
686
- }
687
- ],
688
- "source": [
689
- "quantem.widget.Show2D(phases[\"ssb\"], title=\"SSB — phase retrieval\", sampling=meta[\"sampling\"][:2], units=meta[\"units\"][:2], cmap=\"gray\")"
690
- ]
691
- },
692
  {
693
  "cell_type": "markdown",
694
  "id": "33a873d6",
@@ -697,11 +498,11 @@
697
  "## What you just did\n",
698
  "\n",
699
  "1. Loaded real 4D-STEM gold from Hugging Face → torch GPU + numpy `Dataset4dstem`.\n",
700
- "2. Browsed it with `Show4DSTEM`.\n",
701
- "3. BF, DF: upstream `Dataset4dstem.get_virtual_image(mode=\"circle\"|\"annular\", ...)`, separate `Show2D` widgets.\n",
702
- "4. DPC: upstream `CenterOfMassOriginModel.from_dataset(..., device=\"cuda\").calculate_origin()`.\n",
703
- "5. Built `DirectPtychography`; ran a 30-trial optuna fit of C10/C12/phi12; swept three deconvolution kernels (ICOM, parallax, SSB).\n",
704
- "6. Compared all five modalities each in its own widget.\n",
705
  "\n",
706
  "| Method | What it uses |\n",
707
  "|---|---|\n",
@@ -710,13 +511,13 @@
710
  "| ICOM, parallax, SSB | full CBED at every scan position; aberration-corrected |\n",
711
  "\n",
712
  "Takeaway: phase retrieval (especially SSB once aberrations are fit) recovers\n",
713
- "contrast + resolution that BF/DF can not physically access at the same dose.\n",
714
  "\n",
715
  "## Try next\n",
716
  "\n",
717
  "- Swap to `gold_512_npy_bin4` for a 4× finer detector.\n",
718
- "- Increase optuna trials past 30 if the aberration loss isn't converging.\n",
719
- "- v2 will add iterative ptychography (`PtychoLite`) for the highest-resolution phase."
720
  ]
721
  }
722
  ],
 
175
  " torch.from_numpy(data).to(\"cuda\" if torch.cuda.is_available() else \"cpu\"),\n",
176
  " sampling=meta[\"sampling\"], units=meta[\"units\"], name=meta[\"name\"],\n",
177
  ")\n",
178
+ "w = quantem.widget.Show4DSTEM(dset_torch)\n",
179
+ "w.layout.width = \"1000px\" # ipywidgets layout — best-effort; widget JS may override\n",
180
+ "w"
181
  ]
182
  },
183
  {
 
185
  "id": "c1faa1b5",
186
  "metadata": {},
187
  "source": [
188
+ "## Step 2 — Bright field + Dark field, side by side (independent contrast)\n",
189
  "\n",
190
+ "`Dataset4dstem.get_virtual_image(mode=\"circle\"|\"annular\", ...)` makes the\n",
191
+ "aperture masks + per-scan-position sum. One `Show2D` widget with both panels;\n",
192
+ "`link_contrast=False` so each panel scales to its own min/max."
193
  ]
194
  },
195
  {
 
222
  }
223
  ],
224
  "source": [
225
+ "import math\n",
226
+ "\n",
227
  "H, W = dset.shape[-2:]\n",
228
  "cy, cx = H / 2, W / 2 # hardcoded geometric center\n",
229
  "BF_RADIUS_PX = 6.0\n",
230
+ "R_MAX = math.hypot(cy, cx) # corner-of-detector radius\n",
231
  "\n",
232
+ "bf_ds = dset.get_virtual_image(mode=\"circle\", geometry=((cy, cx), BF_RADIUS_PX), name=\"BF\")\n",
233
+ "df_ds = dset.get_virtual_image(mode=\"annular\", geometry=((cy, cx), (BF_RADIUS_PX, R_MAX)), name=\"DF\")\n",
 
 
 
234
  "print(f\"BF range [{bf_ds.array.min():.1f}, {bf_ds.array.max():.1f}]\")\n",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
235
  "print(f\"DF range [{df_ds.array.min():.1f}, {df_ds.array.max():.1f}]\")\n",
236
  "\n",
237
  "quantem.widget.Show2D(\n",
238
+ " [bf_ds.array, df_ds.array],\n",
239
+ " labels=[\"Bright field\", \"Dark field\"],\n",
240
  " sampling=meta[\"sampling\"][:2], units=meta[\"units\"][:2],\n",
241
  " cmap=\"gray\",\n",
242
+ " link_contrast=False,\n",
243
  ")"
244
  ]
245
  },
 
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
  ]
 
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",
 
355
  "id": "6c89c61d",
356
  "metadata": {},
357
  "source": [
358
+ "## Step 4 — Phase retrieval: `DirectPtychography` + optuna aberration fit\n",
359
  "\n",
360
  "Build `DirectPtychography` with the **rotation found in Step 4b** (`best_rotation_rad`),\n",
361
  "then **fit C10, C12, phi12 from the data** with a 30-trial optuna loop (~5 s on T4).\n",
 
452
  "id": "5ffc4fd8",
453
  "metadata": {},
454
  "source": [
455
+ "## Step 5 — All three kernels side by side\n",
456
  "\n",
457
  "`link_contrast=False` so every kernel gets its own min/max (the SSB output is\n",
458
  "~3 orders of magnitude smaller than ICOM)."
 
490
  ")"
491
  ]
492
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
493
  {
494
  "cell_type": "markdown",
495
  "id": "33a873d6",
 
498
  "## What you just did\n",
499
  "\n",
500
  "1. Loaded real 4D-STEM gold from Hugging Face → torch GPU + numpy `Dataset4dstem`.\n",
501
+ "2. Browsed with `Show4DSTEM`.\n",
502
+ "3. BF + DF in one `Show2D` widget (independent contrast) via `Dataset4dstem.get_virtual_image`.\n",
503
+ "4. DPC via upstream `CenterOfMassOriginModel.calculate_origin()`.\n",
504
+ "5. Found scan-detector rotation by minimizing CoM curl (Step 3b).\n",
505
+ "6. Built `DirectPtychography` with that rotation; ran a 30-trial optuna fit of C10/C12/phi12; swept ICOM / parallax / SSB.\n",
506
  "\n",
507
  "| Method | What it uses |\n",
508
  "|---|---|\n",
 
511
  "| ICOM, parallax, SSB | full CBED at every scan position; aberration-corrected |\n",
512
  "\n",
513
  "Takeaway: phase retrieval (especially SSB once aberrations are fit) recovers\n",
514
+ "contrast + resolution that BF/DF can not access at the same dose.\n",
515
  "\n",
516
  "## Try next\n",
517
  "\n",
518
  "- Swap to `gold_512_npy_bin4` for a 4× finer detector.\n",
519
+ "- Increase optuna trials past 30 if aberration loss isn't converging.\n",
520
+ "- v2 will add iterative ptycho (`PtychoLite`) for the highest-resolution phase."
521
  ]
522
  }
523
  ],