bobleesj commited on
Commit
a47ca22
·
verified ·
1 Parent(s): 8b14705

docs(notebook): add show4dstem_colab.ipynb for one-click Colab workshop

Browse files
Files changed (1) hide show
  1. notebooks/show4dstem_colab.ipynb +145 -0
notebooks/show4dstem_colab.ipynb ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "b5f2091f",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Workshop: Show4DSTEM from Hugging Face via `Dataset4dstem.from_tensor` (no local install needed)\n",
9
+ "\n",
10
+ "This notebook runs in **Google Colab** (or any Jupyter with internet + Chrome). You need\n",
11
+ "only `quantem.widget` (TestPyPI rc) and `quantem` (dev fork; the `from_tensor`\n",
12
+ "constructor lives on the `dataset-support-torch` branch). No CUDA, no h5, no\n",
13
+ "`quantem.live`.\n",
14
+ "\n",
15
+ "[Open in Colab](https://colab.research.google.com/?url=https%3A%2F%2Fhuggingface.co%2Fdatasets%2Fbobleesj%2Fquantem-data%2Fresolve%2Fmain%2Fnotebooks%2Fshow4dstem_colab.ipynb)\n",
16
+ "\n",
17
+ "Flow: download a pre-binned NumPy 4D-STEM dataset from Hugging Face -> wrap it as a\n",
18
+ "torch tensor -> `Dataset4dstem.from_tensor` carries the sampling -> `Show4DSTEM` renders\n",
19
+ "in your browser via WebGPU. Drag the scan cursor; the diffraction pattern updates live."
20
+ ]
21
+ },
22
+ {
23
+ "cell_type": "code",
24
+ "execution_count": null,
25
+ "id": "d9055753",
26
+ "metadata": {},
27
+ "outputs": [],
28
+ "source": [
29
+ "# quantem.widget rc from TestPyPI; quantem from the dev fork branch that\n",
30
+ "# carries Dataset4dstem.from_tensor (the torch-native constructor).\n",
31
+ "!pip install -q --pre --extra-index-url https://test.pypi.org/simple/ quantem.widget huggingface_hub\n",
32
+ "!pip install -q git+https://github.com/bobleesj/quantem.git@dataset-support-torch"
33
+ ]
34
+ },
35
+ {
36
+ "cell_type": "code",
37
+ "execution_count": null,
38
+ "id": "95dc87e5",
39
+ "metadata": {},
40
+ "outputs": [],
41
+ "source": [
42
+ "import quantem, quantem.widget\n",
43
+ "print(\"quantem \", quantem.__version__)\n",
44
+ "print(\"quantem.widget \", quantem.widget.__version__)"
45
+ ]
46
+ },
47
+ {
48
+ "cell_type": "code",
49
+ "execution_count": null,
50
+ "id": "590f83f1",
51
+ "metadata": {},
52
+ "outputs": [],
53
+ "source": [
54
+ "# Download the pre-binned NumPy 4D-STEM dataset from Hugging Face. Public,\n",
55
+ "# cached locally after the first run. ~302 MB; takes 5-30 s on Colab.\n",
56
+ "import os, json\n",
57
+ "import numpy as np\n",
58
+ "from huggingface_hub import snapshot_download\n",
59
+ "\n",
60
+ "ASSET = \"gold_512_npy_bin8\" # or \"gold_512_npy_bin4\" for the larger 1.2 GB version\n",
61
+ "folder = snapshot_download(\n",
62
+ " \"bobleesj/quantem-data\",\n",
63
+ " repo_type=\"dataset\",\n",
64
+ " allow_patterns=[f\"4dstem/{ASSET}/*\"],\n",
65
+ ")\n",
66
+ "asset_dir = os.path.join(folder, \"4dstem\", ASSET)\n",
67
+ "data = np.load(os.path.join(asset_dir, \"data.npy\")) # (512, 512, 24, 24) uint16\n",
68
+ "meta = json.load(open(os.path.join(asset_dir, \"meta.json\"))) # sampling + units + optics\n",
69
+ "\n",
70
+ "print(\"shape:\", data.shape, \"dtype:\", data.dtype)\n",
71
+ "print(\"sampling:\", meta[\"sampling\"], meta[\"units\"])\n",
72
+ "print(\"optics: voltage=%s kV, probe=%s mrad, CL=%s mm\" % (\n",
73
+ " meta.get(\"voltage_kV\"), meta.get(\"probe_semiangle_mrad\"), meta.get(\"camera_length_mm\"),\n",
74
+ "))"
75
+ ]
76
+ },
77
+ {
78
+ "cell_type": "code",
79
+ "execution_count": null,
80
+ "id": "1e14a283",
81
+ "metadata": {},
82
+ "outputs": [],
83
+ "source": [
84
+ "# numpy -> torch tensor -> Dataset4dstem.from_tensor. The dataset carries\n",
85
+ "# `sampling` + `units` so the widget paints the scale bar and axis labels.\n",
86
+ "import torch\n",
87
+ "from quantem.core.datastructures import Dataset4dstem\n",
88
+ "\n",
89
+ "dset = Dataset4dstem.from_tensor(\n",
90
+ " torch.from_numpy(data),\n",
91
+ " sampling=meta[\"sampling\"],\n",
92
+ " units=meta[\"units\"],\n",
93
+ " name=meta[\"name\"],\n",
94
+ ")\n",
95
+ "print(\"dataset:\", type(dset).__name__, \"shape:\", dset.shape, \"sampling:\", dset.sampling)"
96
+ ]
97
+ },
98
+ {
99
+ "cell_type": "code",
100
+ "execution_count": null,
101
+ "id": "d99029a3",
102
+ "metadata": {},
103
+ "outputs": [],
104
+ "source": [
105
+ "# Render in your browser via WebGPU. Drag the cursor in the real-space image on the\n",
106
+ "# left; the CBED on the right updates in real time.\n",
107
+ "from quantem.widget import Show4DSTEM\n",
108
+ "\n",
109
+ "Show4DSTEM(dset)"
110
+ ]
111
+ },
112
+ {
113
+ "cell_type": "markdown",
114
+ "id": "c1d194fd",
115
+ "metadata": {},
116
+ "source": [
117
+ "## What just happened\n",
118
+ "\n",
119
+ "1. Two `pip install` lines: `quantem.widget` (rc from TestPyPI) + `quantem` (dev fork\n",
120
+ " branch with the torch-native `Dataset4dstem.from_tensor`).\n",
121
+ "2. `snapshot_download` pulled a pre-binned NumPy file (~302 MB) from `bobleesj/quantem-data`.\n",
122
+ "3. `np.load` parsed it directly - no custom decompression, no h5 parser.\n",
123
+ "4. `Dataset4dstem.from_tensor` wrapped the torch tensor with sampling + units.\n",
124
+ "5. `Show4DSTEM` rendered it in your browser via WebGPU.\n",
125
+ "\n",
126
+ "No CUDA on Colab. No quantem.live. No private repos.\n",
127
+ "\n",
128
+ "## Try next\n",
129
+ "\n",
130
+ "- Swap `ASSET` to `\"gold_512_npy_bin4\"` for the sharper (larger, 1.2 GB) version.\n",
131
+ "- Try `gold_haadf_npy` with `Show2D` (a 2D HAADF image; same pattern, different widget).\n",
132
+ "- Bring your own 4D-STEM data: save as `(scan_row, scan_col, k_row, k_col)` NumPy +\n",
133
+ " a `meta.json` carrying sampling + units, and pass through the same pipeline."
134
+ ]
135
+ }
136
+ ],
137
+ "metadata": {
138
+ "kernelspec": {
139
+ "display_name": "Python 3",
140
+ "name": "python3"
141
+ }
142
+ },
143
+ "nbformat": 4,
144
+ "nbformat_minor": 5
145
+ }