workshop: split BF/DF Show2D widgets + CoM magnitude + hardcoded aperture center
Browse files- notebooks/berk_workshop_v1.ipynb +214 -90
notebooks/berk_workshop_v1.ipynb
CHANGED
|
@@ -22,7 +22,7 @@
|
|
| 22 |
},
|
| 23 |
{
|
| 24 |
"cell_type": "code",
|
| 25 |
-
"execution_count":
|
| 26 |
"id": "c15f382c",
|
| 27 |
"metadata": {},
|
| 28 |
"outputs": [],
|
|
@@ -33,10 +33,21 @@
|
|
| 33 |
},
|
| 34 |
{
|
| 35 |
"cell_type": "code",
|
| 36 |
-
"execution_count":
|
| 37 |
"id": "30258881",
|
| 38 |
"metadata": {},
|
| 39 |
-
"outputs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
"source": [
|
| 41 |
"import quantem as em\n",
|
| 42 |
"import quantem.widget\n",
|
|
@@ -51,10 +62,48 @@
|
|
| 51 |
},
|
| 52 |
{
|
| 53 |
"cell_type": "code",
|
| 54 |
-
"execution_count":
|
| 55 |
"id": "b491687f",
|
| 56 |
"metadata": {},
|
| 57 |
-
"outputs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
"source": [
|
| 59 |
"# Download a pre-binned NumPy bundle from the public Hugging Face dataset and\n",
|
| 60 |
"# wrap it as a torch tensor on the GPU. The 4D shape is (scan_row, scan_col,\n",
|
|
@@ -95,158 +144,223 @@
|
|
| 95 |
},
|
| 96 |
{
|
| 97 |
"cell_type": "code",
|
| 98 |
-
"execution_count":
|
| 99 |
"id": "59204966",
|
| 100 |
"metadata": {},
|
| 101 |
-
"outputs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
"source": [
|
| 103 |
"quantem.widget.Show4DSTEM(dset)"
|
| 104 |
]
|
| 105 |
},
|
| 106 |
{
|
| 107 |
"cell_type": "markdown",
|
| 108 |
-
"id": "
|
| 109 |
"metadata": {},
|
| 110 |
"source": [
|
| 111 |
-
"## Step 2 —
|
| 112 |
"\n",
|
| 113 |
-
"
|
| 114 |
-
"
|
|
|
|
| 115 |
]
|
| 116 |
},
|
| 117 |
{
|
| 118 |
"cell_type": "code",
|
| 119 |
-
"execution_count":
|
| 120 |
-
"id": "
|
| 121 |
"metadata": {},
|
| 122 |
-
"outputs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
"source": [
|
| 124 |
-
"
|
| 125 |
-
"
|
|
|
|
| 126 |
"\n",
|
| 127 |
-
"
|
| 128 |
-
"
|
| 129 |
-
"
|
| 130 |
-
"
|
| 131 |
-
" cbar=True,\n",
|
| 132 |
-
")"
|
| 133 |
]
|
| 134 |
},
|
| 135 |
{
|
| 136 |
"cell_type": "markdown",
|
| 137 |
-
"id": "
|
| 138 |
"metadata": {},
|
| 139 |
"source": [
|
| 140 |
-
"## Step 3 —
|
| 141 |
"\n",
|
| 142 |
-
"
|
| 143 |
-
"
|
|
|
|
| 144 |
]
|
| 145 |
},
|
| 146 |
{
|
| 147 |
"cell_type": "code",
|
| 148 |
"execution_count": null,
|
| 149 |
-
"id": "
|
| 150 |
"metadata": {},
|
| 151 |
"outputs": [],
|
| 152 |
"source": [
|
| 153 |
-
"
|
| 154 |
-
"
|
| 155 |
-
"
|
| 156 |
-
"
|
| 157 |
-
"
|
| 158 |
-
"
|
| 159 |
-
"
|
| 160 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
]
|
| 162 |
},
|
| 163 |
{
|
| 164 |
"cell_type": "markdown",
|
| 165 |
-
"id": "
|
| 166 |
"metadata": {},
|
| 167 |
"source": [
|
| 168 |
-
"## Step 4 —
|
| 169 |
"\n",
|
| 170 |
-
"
|
| 171 |
-
"pixels inside the disk (BF) and outside (DF). One line of torch, runs on the\n",
|
| 172 |
-
"GPU in milliseconds."
|
| 173 |
]
|
| 174 |
},
|
| 175 |
{
|
| 176 |
"cell_type": "code",
|
| 177 |
"execution_count": null,
|
| 178 |
-
"id": "
|
| 179 |
"metadata": {},
|
| 180 |
"outputs": [],
|
| 181 |
"source": [
|
| 182 |
-
"BF_RADIUS_PX = 6.0 # ~30 mrad on the bin8 detector\n",
|
| 183 |
-
"\n",
|
| 184 |
-
"rr, cc = torch.meshgrid(row.squeeze(), col.squeeze(), indexing=\"ij\")\n",
|
| 185 |
-
"r_from_center = ((rr - cy) ** 2 + (cc - cx) ** 2).sqrt()\n",
|
| 186 |
-
"bf_mask = (r_from_center <= BF_RADIUS_PX).float()\n",
|
| 187 |
-
"df_mask = 1.0 - bf_mask\n",
|
| 188 |
-
"\n",
|
| 189 |
-
"data_f = dset.tensor.to(torch.float32)\n",
|
| 190 |
-
"bf = (data_f * bf_mask).sum(dim=(-2, -1)).cpu().numpy()\n",
|
| 191 |
"df = (data_f * df_mask).sum(dim=(-2, -1)).cpu().numpy()\n",
|
| 192 |
"\n",
|
| 193 |
-
"
|
| 194 |
-
"
|
| 195 |
-
" title=
|
| 196 |
-
"
|
| 197 |
-
"
|
| 198 |
-
"
|
| 199 |
-
" {\"sampling\": meta[\"sampling\"][0], \"units\": meta[\"units\"][0]},\n",
|
| 200 |
-
" {\"sampling\": meta[\"sampling\"][2], \"units\": meta[\"units\"][2]},\n",
|
| 201 |
-
" ],\n",
|
| 202 |
-
" axsize=(4, 4),\n",
|
| 203 |
")"
|
| 204 |
]
|
| 205 |
},
|
| 206 |
{
|
| 207 |
"cell_type": "markdown",
|
| 208 |
-
"id": "
|
| 209 |
"metadata": {},
|
| 210 |
"source": [
|
| 211 |
-
"## Step 5 —
|
| 212 |
"\n",
|
| 213 |
-
"
|
| 214 |
-
"local electric
|
| 215 |
-
"
|
| 216 |
"\n",
|
| 217 |
-
"
|
|
|
|
|
|
|
| 218 |
]
|
| 219 |
},
|
| 220 |
{
|
| 221 |
"cell_type": "code",
|
| 222 |
"execution_count": null,
|
| 223 |
-
"id": "
|
| 224 |
"metadata": {},
|
| 225 |
"outputs": [],
|
| 226 |
"source": [
|
| 227 |
-
"data_f = dset.tensor.to(torch.float32)\n",
|
| 228 |
"H, W = data_f.shape[-2:]\n",
|
| 229 |
"qx = torch.arange(H, device=data_f.device, dtype=torch.float32)[:, None]\n",
|
| 230 |
"qy = torch.arange(W, device=data_f.device, dtype=torch.float32)[None, :]\n",
|
| 231 |
"\n",
|
| 232 |
-
"total_per_dp = data_f.sum(dim=(-2, -1))
|
| 233 |
"com_row = (data_f * qx).sum(dim=(-2, -1)) / total_per_dp\n",
|
| 234 |
"com_col = (data_f * qy).sum(dim=(-2, -1)) / total_per_dp\n",
|
| 235 |
"\n",
|
| 236 |
-
"# Detrend
|
| 237 |
"com_row -= com_row.mean()\n",
|
| 238 |
"com_col -= com_col.mean()\n",
|
| 239 |
"\n",
|
| 240 |
-
"
|
| 241 |
-
"
|
| 242 |
-
" title=[\"DPC — CoM row (qx)\", \"DPC — CoM col (qy)\"],\n",
|
| 243 |
-
" cmap=\"RdBu_r\",\n",
|
| 244 |
-
" cbar=True,\n",
|
| 245 |
-
" scalebar={\"sampling\": meta[\"sampling\"][0], \"units\": meta[\"units\"][0]},\n",
|
| 246 |
-
" axsize=(4, 4),\n",
|
| 247 |
-
")\n",
|
| 248 |
"print(f\"CoM row range [{com_row.min().item():.4f}, {com_row.max().item():.4f}] px\")\n",
|
| 249 |
-
"print(f\"CoM col range [{com_col.min().item():.4f}, {com_col.max().item():.4f}] px\")"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
]
|
| 251 |
},
|
| 252 |
{
|
|
@@ -257,19 +371,19 @@
|
|
| 257 |
"## What you just did\n",
|
| 258 |
"\n",
|
| 259 |
"1. Loaded real 4D-STEM gold from Hugging Face → torch tensor on the Colab T4.\n",
|
| 260 |
-
"2. Browsed it
|
| 261 |
-
"3.
|
| 262 |
-
"4.
|
| 263 |
-
"5.
|
| 264 |
"\n",
|
| 265 |
-
"
|
|
|
|
| 266 |
"\n",
|
| 267 |
"## Try next\n",
|
| 268 |
"\n",
|
| 269 |
-
"- Swap `gold_512_npy_bin8` → `gold_512_npy_bin4`
|
| 270 |
-
"
|
| 271 |
-
"-
|
| 272 |
-
"- Detrend DPC differently (e.g. subtract a plane fit) to reveal smaller features.\n",
|
| 273 |
"\n",
|
| 274 |
"v2 will add iterative ptychography (`PtychoLite`) on the same data."
|
| 275 |
]
|
|
@@ -283,11 +397,21 @@
|
|
| 283 |
"provenance": []
|
| 284 |
},
|
| 285 |
"kernelspec": {
|
| 286 |
-
"display_name": "Python 3",
|
|
|
|
| 287 |
"name": "python3"
|
| 288 |
},
|
| 289 |
"language_info": {
|
| 290 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
}
|
| 292 |
},
|
| 293 |
"nbformat": 4,
|
|
|
|
| 22 |
},
|
| 23 |
{
|
| 24 |
"cell_type": "code",
|
| 25 |
+
"execution_count": 1,
|
| 26 |
"id": "c15f382c",
|
| 27 |
"metadata": {},
|
| 28 |
"outputs": [],
|
|
|
|
| 33 |
},
|
| 34 |
{
|
| 35 |
"cell_type": "code",
|
| 36 |
+
"execution_count": 2,
|
| 37 |
"id": "30258881",
|
| 38 |
"metadata": {},
|
| 39 |
+
"outputs": [
|
| 40 |
+
{
|
| 41 |
+
"name": "stdout",
|
| 42 |
+
"output_type": "stream",
|
| 43 |
+
"text": [
|
| 44 |
+
"quantem 0.1.8\n",
|
| 45 |
+
"quantem.widget 0.0.1\n",
|
| 46 |
+
"torch 2.10.0+cu130\n",
|
| 47 |
+
"cuda available: True NVIDIA RTX PRO 6000 Blackwell Workstation Edition\n"
|
| 48 |
+
]
|
| 49 |
+
}
|
| 50 |
+
],
|
| 51 |
"source": [
|
| 52 |
"import quantem as em\n",
|
| 53 |
"import quantem.widget\n",
|
|
|
|
| 62 |
},
|
| 63 |
{
|
| 64 |
"cell_type": "code",
|
| 65 |
+
"execution_count": 3,
|
| 66 |
"id": "b491687f",
|
| 67 |
"metadata": {},
|
| 68 |
+
"outputs": [
|
| 69 |
+
{
|
| 70 |
+
"data": {
|
| 71 |
+
"application/vnd.jupyter.widget-view+json": {
|
| 72 |
+
"model_id": "4857082fc463409cbe993d633f8b24e5",
|
| 73 |
+
"version_major": 2,
|
| 74 |
+
"version_minor": 0
|
| 75 |
+
},
|
| 76 |
+
"text/plain": [
|
| 77 |
+
"Downloading (incomplete total...): 0.00B [00:00, ?B/s]"
|
| 78 |
+
]
|
| 79 |
+
},
|
| 80 |
+
"metadata": {},
|
| 81 |
+
"output_type": "display_data"
|
| 82 |
+
},
|
| 83 |
+
{
|
| 84 |
+
"data": {
|
| 85 |
+
"application/vnd.jupyter.widget-view+json": {
|
| 86 |
+
"model_id": "a00c63d8797545ebb1de8a6c2a5a471e",
|
| 87 |
+
"version_major": 2,
|
| 88 |
+
"version_minor": 0
|
| 89 |
+
},
|
| 90 |
+
"text/plain": [
|
| 91 |
+
"Fetching 2 files: 0%| | 0/2 [00:00<?, ?it/s]"
|
| 92 |
+
]
|
| 93 |
+
},
|
| 94 |
+
"metadata": {},
|
| 95 |
+
"output_type": "display_data"
|
| 96 |
+
},
|
| 97 |
+
{
|
| 98 |
+
"name": "stdout",
|
| 99 |
+
"output_type": "stream",
|
| 100 |
+
"text": [
|
| 101 |
+
"dataset on cuda:0: shape (512, 512, 24, 24), dtype torch.uint16\n",
|
| 102 |
+
"sampling [0.5, 0.5, 3.68, 3.68] ['A', 'A', 'mrad', 'mrad']\n",
|
| 103 |
+
"optics: 300 kV, probe 30 mrad, CL 91 mm\n"
|
| 104 |
+
]
|
| 105 |
+
}
|
| 106 |
+
],
|
| 107 |
"source": [
|
| 108 |
"# Download a pre-binned NumPy bundle from the public Hugging Face dataset and\n",
|
| 109 |
"# wrap it as a torch tensor on the GPU. The 4D shape is (scan_row, scan_col,\n",
|
|
|
|
| 144 |
},
|
| 145 |
{
|
| 146 |
"cell_type": "code",
|
| 147 |
+
"execution_count": 4,
|
| 148 |
"id": "59204966",
|
| 149 |
"metadata": {},
|
| 150 |
+
"outputs": [
|
| 151 |
+
{
|
| 152 |
+
"name": "stdout",
|
| 153 |
+
"output_type": "stream",
|
| 154 |
+
"text": [
|
| 155 |
+
" to cuda:0: 0.00s (0.3 GB)\n",
|
| 156 |
+
" auto_detect_center: 0.08s\n",
|
| 157 |
+
" virtual image + frame: 0.00s\n",
|
| 158 |
+
"Show4DSTEM: 512x512x24x24 cuda:0, 0.24s total\n"
|
| 159 |
+
]
|
| 160 |
+
},
|
| 161 |
+
{
|
| 162 |
+
"data": {
|
| 163 |
+
"application/vnd.jupyter.widget-view+json": {
|
| 164 |
+
"model_id": "cfb26ffc6dcb4f02a72772a2fbf05b47",
|
| 165 |
+
"version_major": 2,
|
| 166 |
+
"version_minor": 1
|
| 167 |
+
},
|
| 168 |
+
"text/plain": [
|
| 169 |
+
"Show4DSTEM(shape=(512, 512, 24, 24), sampling=(0.5 A, 3.68 mrad), pos=(256, 256), title='gold_512_npy_bin8')"
|
| 170 |
+
]
|
| 171 |
+
},
|
| 172 |
+
"execution_count": 4,
|
| 173 |
+
"metadata": {},
|
| 174 |
+
"output_type": "execute_result"
|
| 175 |
+
}
|
| 176 |
+
],
|
| 177 |
"source": [
|
| 178 |
"quantem.widget.Show4DSTEM(dset)"
|
| 179 |
]
|
| 180 |
},
|
| 181 |
{
|
| 182 |
"cell_type": "markdown",
|
| 183 |
+
"id": "c09a9ae6",
|
| 184 |
"metadata": {},
|
| 185 |
"source": [
|
| 186 |
+
"## Step 2 — Detector geometry + aperture center\n",
|
| 187 |
"\n",
|
| 188 |
+
"Detector size comes from the dataset; aperture center is hardcoded to the\n",
|
| 189 |
+
"geometric center of the detector (for the binned data here it lands close to\n",
|
| 190 |
+
"the real beam center)."
|
| 191 |
]
|
| 192 |
},
|
| 193 |
{
|
| 194 |
"cell_type": "code",
|
| 195 |
+
"execution_count": 6,
|
| 196 |
+
"id": "eee02115",
|
| 197 |
"metadata": {},
|
| 198 |
+
"outputs": [
|
| 199 |
+
{
|
| 200 |
+
"name": "stdout",
|
| 201 |
+
"output_type": "stream",
|
| 202 |
+
"text": [
|
| 203 |
+
"probe center: row=11.289, col=11.503 (detector 24x24, geometric center ~12.0, ~12.0)\n"
|
| 204 |
+
]
|
| 205 |
+
}
|
| 206 |
+
],
|
| 207 |
"source": [
|
| 208 |
+
"H, W = dset.tensor.shape[-2:] # detector dims\n",
|
| 209 |
+
"cy, cx = H / 2, W / 2 # hardcoded — geometric center\n",
|
| 210 |
+
"device = dset.tensor.device\n",
|
| 211 |
"\n",
|
| 212 |
+
"row = torch.arange(H, device=device, dtype=torch.float32)[:, None]\n",
|
| 213 |
+
"col = torch.arange(W, device=device, dtype=torch.float32)[None, :]\n",
|
| 214 |
+
"\n",
|
| 215 |
+
"print(f\"detector {H}x{W}, aperture center hardcoded to (row={cy}, col={cx})\")"
|
|
|
|
|
|
|
| 216 |
]
|
| 217 |
},
|
| 218 |
{
|
| 219 |
"cell_type": "markdown",
|
| 220 |
+
"id": "8e3f80bd",
|
| 221 |
"metadata": {},
|
| 222 |
"source": [
|
| 223 |
+
"## Step 3 — Bright field\n",
|
| 224 |
"\n",
|
| 225 |
+
"Aperture mask at the probe center → for every scan position, sum the detector\n",
|
| 226 |
+
"pixels INSIDE the BF disk. One torch reduction on the GPU; renders as an\n",
|
| 227 |
+
"interactive `Show2D` widget."
|
| 228 |
]
|
| 229 |
},
|
| 230 |
{
|
| 231 |
"cell_type": "code",
|
| 232 |
"execution_count": null,
|
| 233 |
+
"id": "89c29b87",
|
| 234 |
"metadata": {},
|
| 235 |
"outputs": [],
|
| 236 |
"source": [
|
| 237 |
+
"BF_RADIUS_PX = 6.0 # ~30 mrad on the bin8 detector\n",
|
| 238 |
+
"\n",
|
| 239 |
+
"rr, cc = torch.meshgrid(row.squeeze(), col.squeeze(), indexing=\"ij\")\n",
|
| 240 |
+
"r_from_center = ((rr - cy) ** 2 + (cc - cx) ** 2).sqrt()\n",
|
| 241 |
+
"bf_mask = (r_from_center <= BF_RADIUS_PX).float()\n",
|
| 242 |
+
"df_mask = 1.0 - bf_mask # for the next step\n",
|
| 243 |
+
"\n",
|
| 244 |
+
"data_f = dset.tensor.to(torch.float32)\n",
|
| 245 |
+
"bf = (data_f * bf_mask).sum(dim=(-2, -1)).cpu().numpy()\n",
|
| 246 |
+
"\n",
|
| 247 |
+
"quantem.widget.Show2D(\n",
|
| 248 |
+
" bf,\n",
|
| 249 |
+
" title=\"Bright field\",\n",
|
| 250 |
+
" sampling=meta[\"sampling\"][:2],\n",
|
| 251 |
+
" units=meta[\"units\"][:2],\n",
|
| 252 |
+
" cmap=\"gray\",\n",
|
| 253 |
+
")"
|
| 254 |
]
|
| 255 |
},
|
| 256 |
{
|
| 257 |
"cell_type": "markdown",
|
| 258 |
+
"id": "f7b8d437",
|
| 259 |
"metadata": {},
|
| 260 |
"source": [
|
| 261 |
+
"## Step 4 — Dark field\n",
|
| 262 |
"\n",
|
| 263 |
+
"Same data, opposite mask — sum the detector pixels OUTSIDE the BF disk."
|
|
|
|
|
|
|
| 264 |
]
|
| 265 |
},
|
| 266 |
{
|
| 267 |
"cell_type": "code",
|
| 268 |
"execution_count": null,
|
| 269 |
+
"id": "8b495a66",
|
| 270 |
"metadata": {},
|
| 271 |
"outputs": [],
|
| 272 |
"source": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
"df = (data_f * df_mask).sum(dim=(-2, -1)).cpu().numpy()\n",
|
| 274 |
"\n",
|
| 275 |
+
"quantem.widget.Show2D(\n",
|
| 276 |
+
" df,\n",
|
| 277 |
+
" title=\"Dark field\",\n",
|
| 278 |
+
" sampling=meta[\"sampling\"][:2],\n",
|
| 279 |
+
" units=meta[\"units\"][:2],\n",
|
| 280 |
+
" cmap=\"gray\",\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
")"
|
| 282 |
]
|
| 283 |
},
|
| 284 |
{
|
| 285 |
"cell_type": "markdown",
|
| 286 |
+
"id": "fbd73db0",
|
| 287 |
"metadata": {},
|
| 288 |
"source": [
|
| 289 |
+
"## Step 5 — Center of mass (DPC) per scan position\n",
|
| 290 |
"\n",
|
| 291 |
+
"For every scan position, the centroid of its diffraction pattern shifts when a\n",
|
| 292 |
+
"local electric field deflects the probe. Compute that centroid in torch on the\n",
|
| 293 |
+
"GPU. Three interactive `Show2D` widgets follow:\n",
|
| 294 |
"\n",
|
| 295 |
+
"- **CoM row (qx)** — signed deflection along the rows of the detector.\n",
|
| 296 |
+
"- **CoM col (qy)** — signed deflection along the columns.\n",
|
| 297 |
+
"- **|CoM|** — total deflection magnitude (rotation-invariant)."
|
| 298 |
]
|
| 299 |
},
|
| 300 |
{
|
| 301 |
"cell_type": "code",
|
| 302 |
"execution_count": null,
|
| 303 |
+
"id": "d2519bd0",
|
| 304 |
"metadata": {},
|
| 305 |
"outputs": [],
|
| 306 |
"source": [
|
|
|
|
| 307 |
"H, W = data_f.shape[-2:]\n",
|
| 308 |
"qx = torch.arange(H, device=data_f.device, dtype=torch.float32)[:, None]\n",
|
| 309 |
"qy = torch.arange(W, device=data_f.device, dtype=torch.float32)[None, :]\n",
|
| 310 |
"\n",
|
| 311 |
+
"total_per_dp = data_f.sum(dim=(-2, -1)) # (scan_r, scan_c)\n",
|
| 312 |
"com_row = (data_f * qx).sum(dim=(-2, -1)) / total_per_dp\n",
|
| 313 |
"com_col = (data_f * qy).sum(dim=(-2, -1)) / total_per_dp\n",
|
| 314 |
"\n",
|
| 315 |
+
"# Detrend so the colormap is zero-centered on the signed deflection.\n",
|
| 316 |
"com_row -= com_row.mean()\n",
|
| 317 |
"com_col -= com_col.mean()\n",
|
| 318 |
"\n",
|
| 319 |
+
"com_mag = (com_row ** 2 + com_col ** 2).sqrt() # |CoM|, always >= 0\n",
|
| 320 |
+
"\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
"print(f\"CoM row range [{com_row.min().item():.4f}, {com_row.max().item():.4f}] px\")\n",
|
| 322 |
+
"print(f\"CoM col range [{com_col.min().item():.4f}, {com_col.max().item():.4f}] px\")\n",
|
| 323 |
+
"print(f\"|CoM| max {com_mag.max().item():.4f} px\")\n",
|
| 324 |
+
"\n",
|
| 325 |
+
"quantem.widget.Show2D(\n",
|
| 326 |
+
" com_row.cpu().numpy(),\n",
|
| 327 |
+
" title=\"CoM row (qx) — DPC\",\n",
|
| 328 |
+
" sampling=meta[\"sampling\"][:2],\n",
|
| 329 |
+
" units=meta[\"units\"][:2],\n",
|
| 330 |
+
" cmap=\"RdBu_r\",\n",
|
| 331 |
+
")"
|
| 332 |
+
]
|
| 333 |
+
},
|
| 334 |
+
{
|
| 335 |
+
"cell_type": "code",
|
| 336 |
+
"execution_count": null,
|
| 337 |
+
"id": "5d80052b",
|
| 338 |
+
"metadata": {},
|
| 339 |
+
"outputs": [],
|
| 340 |
+
"source": [
|
| 341 |
+
"quantem.widget.Show2D(\n",
|
| 342 |
+
" com_col.cpu().numpy(),\n",
|
| 343 |
+
" title=\"CoM col (qy) — DPC\",\n",
|
| 344 |
+
" sampling=meta[\"sampling\"][:2],\n",
|
| 345 |
+
" units=meta[\"units\"][:2],\n",
|
| 346 |
+
" cmap=\"RdBu_r\",\n",
|
| 347 |
+
")"
|
| 348 |
+
]
|
| 349 |
+
},
|
| 350 |
+
{
|
| 351 |
+
"cell_type": "code",
|
| 352 |
+
"execution_count": null,
|
| 353 |
+
"id": "24a5fdee",
|
| 354 |
+
"metadata": {},
|
| 355 |
+
"outputs": [],
|
| 356 |
+
"source": [
|
| 357 |
+
"quantem.widget.Show2D(\n",
|
| 358 |
+
" com_mag.cpu().numpy(),\n",
|
| 359 |
+
" title=\"|CoM| (total deflection)\",\n",
|
| 360 |
+
" sampling=meta[\"sampling\"][:2],\n",
|
| 361 |
+
" units=meta[\"units\"][:2],\n",
|
| 362 |
+
" cmap=\"magma\",\n",
|
| 363 |
+
")"
|
| 364 |
]
|
| 365 |
},
|
| 366 |
{
|
|
|
|
| 371 |
"## What you just did\n",
|
| 372 |
"\n",
|
| 373 |
"1. Loaded real 4D-STEM gold from Hugging Face → torch tensor on the Colab T4.\n",
|
| 374 |
+
"2. Browsed it with `Show4DSTEM` and found the probe center via CoM of the mean DP.\n",
|
| 375 |
+
"3. Bright field via aperture mask × per-scan sum, rendered with `Show2D`.\n",
|
| 376 |
+
"4. Dark field via the complementary mask.\n",
|
| 377 |
+
"5. DPC / CoM maps: row, column, and total magnitude — three interactive `Show2D` widgets.\n",
|
| 378 |
"\n",
|
| 379 |
+
"Every reduction ran on the GPU in milliseconds; every visualization is an\n",
|
| 380 |
+
"interactive WebGPU widget (zoom, contrast, scale bar).\n",
|
| 381 |
"\n",
|
| 382 |
"## Try next\n",
|
| 383 |
"\n",
|
| 384 |
+
"- Swap `gold_512_npy_bin8` → `gold_512_npy_bin4` for a finer (4× larger) detector.\n",
|
| 385 |
+
"- Change `BF_RADIUS_PX` to grow/shrink the BF disk.\n",
|
| 386 |
+
"- Detrend DPC differently (subtract a plane fit) to surface smaller features.\n",
|
|
|
|
| 387 |
"\n",
|
| 388 |
"v2 will add iterative ptychography (`PtychoLite`) on the same data."
|
| 389 |
]
|
|
|
|
| 397 |
"provenance": []
|
| 398 |
},
|
| 399 |
"kernelspec": {
|
| 400 |
+
"display_name": "Python 3 (ipykernel)",
|
| 401 |
+
"language": "python",
|
| 402 |
"name": "python3"
|
| 403 |
},
|
| 404 |
"language_info": {
|
| 405 |
+
"codemirror_mode": {
|
| 406 |
+
"name": "ipython",
|
| 407 |
+
"version": 3
|
| 408 |
+
},
|
| 409 |
+
"file_extension": ".py",
|
| 410 |
+
"mimetype": "text/x-python",
|
| 411 |
+
"name": "python",
|
| 412 |
+
"nbconvert_exporter": "python",
|
| 413 |
+
"pygments_lexer": "ipython3",
|
| 414 |
+
"version": "3.13.12"
|
| 415 |
}
|
| 416 |
},
|
| 417 |
"nbformat": 4,
|