workshop: drop parallax notebook (folded into consolidated v1)
Browse files
notebooks/berk_workshop_parallax_v1.ipynb
DELETED
|
@@ -1,213 +0,0 @@
|
|
| 1 |
-
{
|
| 2 |
-
"cells": [
|
| 3 |
-
{
|
| 4 |
-
"cell_type": "markdown",
|
| 5 |
-
"id": "d7f1098c",
|
| 6 |
-
"metadata": {},
|
| 7 |
-
"source": [
|
| 8 |
-
"# Workshop: parallax phase retrieval on real gold (Colab T4)\n",
|
| 9 |
-
"\n",
|
| 10 |
-
"[](https://colab.research.google.com/gist/bobleesj/96ea70cbb0d3f97b32d13c0e7a5448ec/berk_workshop_parallax_v1.ipynb)\n",
|
| 11 |
-
"\n",
|
| 12 |
-
"Single-shot phase retrieval via the **parallax** mode of `DirectPtychography`.\n",
|
| 13 |
-
"Same real-gold 4D-STEM data from Hugging Face. No iterative ptycho yet — just\n",
|
| 14 |
-
"one direct deconvolution that gives you a usable phase image in seconds.\n",
|
| 15 |
-
"\n",
|
| 16 |
-
"Two installs (same as the BF/DF/DPC workshop): `quantem.widget` (TestPyPI\n",
|
| 17 |
-
"prerelease) + `quantem` from the `berk-workshop` branch on `bobleesj/quantem`.\n",
|
| 18 |
-
"\n",
|
| 19 |
-
"**Total runtime: 2-3 minutes on Colab T4.**"
|
| 20 |
-
]
|
| 21 |
-
},
|
| 22 |
-
{
|
| 23 |
-
"cell_type": "code",
|
| 24 |
-
"execution_count": null,
|
| 25 |
-
"id": "27f47377",
|
| 26 |
-
"metadata": {},
|
| 27 |
-
"outputs": [],
|
| 28 |
-
"source": [
|
| 29 |
-
"!pip install -q --pre --extra-index-url https://test.pypi.org/simple/ quantem.widget huggingface_hub\n",
|
| 30 |
-
"!pip install -q git+https://github.com/bobleesj/quantem.git@berk-workshop"
|
| 31 |
-
]
|
| 32 |
-
},
|
| 33 |
-
{
|
| 34 |
-
"cell_type": "code",
|
| 35 |
-
"execution_count": null,
|
| 36 |
-
"id": "a3ec89be",
|
| 37 |
-
"metadata": {},
|
| 38 |
-
"outputs": [],
|
| 39 |
-
"source": [
|
| 40 |
-
"import quantem as em\n",
|
| 41 |
-
"import quantem.widget\n",
|
| 42 |
-
"import torch\n",
|
| 43 |
-
"\n",
|
| 44 |
-
"# cuDNN grid_sample currently fails on the parallax shift-origin step for these\n",
|
| 45 |
-
"# dimensions; disable to use the deterministic CUDA path (no perf impact at this\n",
|
| 46 |
-
"# data size). Tracked upstream.\n",
|
| 47 |
-
"torch.backends.cudnn.enabled = False\n",
|
| 48 |
-
"\n",
|
| 49 |
-
"print(\"quantem \", em.__version__)\n",
|
| 50 |
-
"print(\"quantem.widget \", quantem.widget.__version__)\n",
|
| 51 |
-
"print(\"torch \", torch.__version__, \"(cuDNN disabled)\")\n",
|
| 52 |
-
"print(\"cuda available:\", torch.cuda.is_available(),\n",
|
| 53 |
-
" torch.cuda.get_device_name(0) if torch.cuda.is_available() else \"(no GPU)\")"
|
| 54 |
-
]
|
| 55 |
-
},
|
| 56 |
-
{
|
| 57 |
-
"cell_type": "code",
|
| 58 |
-
"execution_count": null,
|
| 59 |
-
"id": "08f7a2a4",
|
| 60 |
-
"metadata": {},
|
| 61 |
-
"outputs": [],
|
| 62 |
-
"source": [
|
| 63 |
-
"import os, json\n",
|
| 64 |
-
"import numpy as np\n",
|
| 65 |
-
"from huggingface_hub import snapshot_download\n",
|
| 66 |
-
"\n",
|
| 67 |
-
"folder = snapshot_download(\"bobleesj/quantem-data\", repo_type=\"dataset\",\n",
|
| 68 |
-
" allow_patterns=[\"4dstem/gold_512_npy_bin8/*\"])\n",
|
| 69 |
-
"asset = os.path.join(folder, \"4dstem\", \"gold_512_npy_bin8\")\n",
|
| 70 |
-
"data = np.load(os.path.join(asset, \"data.npy\")).astype(np.float32) # float for DirectPtychography\n",
|
| 71 |
-
"data = np.ascontiguousarray(data)\n",
|
| 72 |
-
"meta = json.load(open(os.path.join(asset, \"meta.json\")))\n",
|
| 73 |
-
"\n",
|
| 74 |
-
"dset = em.core.datastructures.Dataset4dstem.from_array(\n",
|
| 75 |
-
" data, sampling=meta[\"sampling\"], units=meta[\"units\"], name=meta[\"name\"],\n",
|
| 76 |
-
")\n",
|
| 77 |
-
"print(f\"dataset: shape {dset.shape}, dtype {dset.array.dtype}\")\n",
|
| 78 |
-
"print(f\"sampling {meta['sampling']} {meta['units']}\")\n",
|
| 79 |
-
"print(f\"optics: {meta['voltage_kV']} kV, probe {meta['probe_semiangle_mrad']} mrad\")"
|
| 80 |
-
]
|
| 81 |
-
},
|
| 82 |
-
{
|
| 83 |
-
"cell_type": "markdown",
|
| 84 |
-
"id": "7cd2a2fc",
|
| 85 |
-
"metadata": {},
|
| 86 |
-
"source": [
|
| 87 |
-
"## Step 1 — Build the DirectPtychography object\n",
|
| 88 |
-
"\n",
|
| 89 |
-
"`DirectPtychography.from_dataset4d` runs CoM + origin fit + auto-rotation\n",
|
| 90 |
-
"estimate internally. Pass the optics in SI: energy in eV, semi-angle cutoff in\n",
|
| 91 |
-
"radians."
|
| 92 |
-
]
|
| 93 |
-
},
|
| 94 |
-
{
|
| 95 |
-
"cell_type": "code",
|
| 96 |
-
"execution_count": null,
|
| 97 |
-
"id": "8ef4082e",
|
| 98 |
-
"metadata": {},
|
| 99 |
-
"outputs": [],
|
| 100 |
-
"source": [
|
| 101 |
-
"from quantem.diffractive_imaging import DirectPtychography\n",
|
| 102 |
-
"\n",
|
| 103 |
-
"DEVICE = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
|
| 104 |
-
"\n",
|
| 105 |
-
"direct = DirectPtychography.from_dataset4d(\n",
|
| 106 |
-
" dset,\n",
|
| 107 |
-
" energy=meta[\"voltage_kV\"] * 1e3, # 300 kV -> 300000 eV\n",
|
| 108 |
-
" semiangle_cutoff=meta[\"probe_semiangle_mrad\"] * 1e-3, # 30 mrad -> 0.030 rad\n",
|
| 109 |
-
" rotation_angle=None, # auto-estimate\n",
|
| 110 |
-
" device=DEVICE,\n",
|
| 111 |
-
" verbose=True,\n",
|
| 112 |
-
")\n",
|
| 113 |
-
"print(f\"DirectPtychography built on {DEVICE}\")"
|
| 114 |
-
]
|
| 115 |
-
},
|
| 116 |
-
{
|
| 117 |
-
"cell_type": "markdown",
|
| 118 |
-
"id": "a56cb2e5",
|
| 119 |
-
"metadata": {},
|
| 120 |
-
"source": [
|
| 121 |
-
"## Step 2 — Reconstruct with the parallax kernel\n",
|
| 122 |
-
"\n",
|
| 123 |
-
"`deconvolution_kernel=\"parallax\"` runs the parallax-tilt approximation (a\n",
|
| 124 |
-
"weighted shift-and-add over virtual BF images). Fast — single forward pass,\n",
|
| 125 |
-
"no iterative loop."
|
| 126 |
-
]
|
| 127 |
-
},
|
| 128 |
-
{
|
| 129 |
-
"cell_type": "code",
|
| 130 |
-
"execution_count": null,
|
| 131 |
-
"id": "db340059",
|
| 132 |
-
"metadata": {},
|
| 133 |
-
"outputs": [],
|
| 134 |
-
"source": [
|
| 135 |
-
"direct.reconstruct(\n",
|
| 136 |
-
" deconvolution_kernel=\"parallax\",\n",
|
| 137 |
-
" parallax_flip_phase=False,\n",
|
| 138 |
-
" verbose=True,\n",
|
| 139 |
-
")\n",
|
| 140 |
-
"print(\"parallax recon done\")"
|
| 141 |
-
]
|
| 142 |
-
},
|
| 143 |
-
{
|
| 144 |
-
"cell_type": "markdown",
|
| 145 |
-
"id": "ef1cc477",
|
| 146 |
-
"metadata": {},
|
| 147 |
-
"source": [
|
| 148 |
-
"## Step 3 — Visualize the recovered phase\n",
|
| 149 |
-
"\n",
|
| 150 |
-
"The parallax phase image lives on the GPU; pull to CPU once for the widget."
|
| 151 |
-
]
|
| 152 |
-
},
|
| 153 |
-
{
|
| 154 |
-
"cell_type": "code",
|
| 155 |
-
"execution_count": null,
|
| 156 |
-
"id": "feb7fbfe",
|
| 157 |
-
"metadata": {},
|
| 158 |
-
"outputs": [],
|
| 159 |
-
"source": [
|
| 160 |
-
"phase = direct.corrected_bf # (scan_r, scan_c) float32 on cuda\n",
|
| 161 |
-
"print(f\"phase shape {tuple(phase.shape)}, device {phase.device}\")\n",
|
| 162 |
-
"print(f\"phase range [{phase.min().item():.3f}, {phase.max().item():.3f}]\")\n",
|
| 163 |
-
"\n",
|
| 164 |
-
"quantem.widget.Show2D(\n",
|
| 165 |
-
" phase.detach().cpu().numpy(),\n",
|
| 166 |
-
" title=\"Parallax phase (corrected_bf)\",\n",
|
| 167 |
-
" sampling=meta[\"sampling\"][:2],\n",
|
| 168 |
-
" units=meta[\"units\"][:2],\n",
|
| 169 |
-
" cmap=\"gray\",\n",
|
| 170 |
-
")"
|
| 171 |
-
]
|
| 172 |
-
},
|
| 173 |
-
{
|
| 174 |
-
"cell_type": "markdown",
|
| 175 |
-
"id": "478edc58",
|
| 176 |
-
"metadata": {},
|
| 177 |
-
"source": [
|
| 178 |
-
"## What you just did\n",
|
| 179 |
-
"\n",
|
| 180 |
-
"1. Loaded real gold 4D-STEM from Hugging Face → numpy-backed `Dataset4dstem`.\n",
|
| 181 |
-
"2. Built `DirectPtychography` with the gold's actual optics (300 kV, 30 mrad).\n",
|
| 182 |
-
"3. Ran a single-shot parallax reconstruction (no iterative loop).\n",
|
| 183 |
-
"4. Rendered `direct.corrected_bf` as an interactive `Show2D` widget.\n",
|
| 184 |
-
"\n",
|
| 185 |
-
"Single forward pass on the T4. Companion notebook `berk_workshop_v1.ipynb`\n",
|
| 186 |
-
"covers browse + BF/DF + DPC on the same data.\n",
|
| 187 |
-
"\n",
|
| 188 |
-
"## Try next\n",
|
| 189 |
-
"\n",
|
| 190 |
-
"- `parallax_flip_phase=True` if the phase comes out inverted.\n",
|
| 191 |
-
"- Swap to `gold_512_npy_bin4` for a finer (4× larger) detector.\n",
|
| 192 |
-
"- v2 will add iterative ptychography (`PtychoLite`) for higher-resolution phase."
|
| 193 |
-
]
|
| 194 |
-
}
|
| 195 |
-
],
|
| 196 |
-
"metadata": {
|
| 197 |
-
"accelerator": "GPU",
|
| 198 |
-
"colab": {
|
| 199 |
-
"gpuType": "T4",
|
| 200 |
-
"include_colab_link": true,
|
| 201 |
-
"provenance": []
|
| 202 |
-
},
|
| 203 |
-
"kernelspec": {
|
| 204 |
-
"display_name": "Python 3",
|
| 205 |
-
"name": "python3"
|
| 206 |
-
},
|
| 207 |
-
"language_info": {
|
| 208 |
-
"name": "python"
|
| 209 |
-
}
|
| 210 |
-
},
|
| 211 |
-
"nbformat": 4,
|
| 212 |
-
"nbformat_minor": 5
|
| 213 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|