bobleesj commited on
Commit
8eb365e
·
1 Parent(s): ea467e0

docs: draft widget-tutorials meta.json process and dummy 8x8 test (#3)

Browse files

- docs: draft widget-tutorials meta.json process and dummy 8x8 test (46a78773368957d3d3ff08d1d7f9b5cb0db83910)
- docs: teach Hugging Face Write token setup for Community PRs (7b9bb147046b39661de5d5f191ee2eb859846ce8)
- refactor: run checker as python -m check_meta (183c2248c7f5d1df90d6eaa34d738d6d513507c1)
- docs: add coding-agent Community PR steps (383faf3845c136258fba42f09531a55af0bb27f8)
- remove dummy 8x8 process-test folder (966dd006cb35ea83bd4973695f41310bf6e3b6c1)

Files changed (4) hide show
  1. README.md +185 -37
  2. check_meta/__init__.py +97 -0
  3. check_meta/__main__.py +31 -0
  4. templates/meta.json +19 -0
README.md CHANGED
@@ -10,59 +10,207 @@ pretty_name: quantem-data
10
 
11
  # quantem-data
12
 
13
- Reference electron-microscopy datasets (gold 4D-STEM and HAADF) for browsing
14
- and learning with [quantem.widget](https://github.com/bobleesj/quantem.widget).
15
- This repo just hosts the data all instructions live in one place:
 
16
 
17
- **How to download and use this data:** follow the
18
- [Download data tutorial](https://bobleesj.github.io/quantem.widget/tutorials/download_data.html)
19
- in the quantem.widget docs. One call per dataset:
 
 
 
 
 
 
20
 
21
  ```python
22
- from quantem.widget import Show4DSTEM
23
  from quantem.widget.datasets import show4dstem_gold
24
 
25
- Show4DSTEM(show4dstem_gold())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  ```
27
 
28
- Layout two trees:
 
 
 
29
 
30
- - **`widget-tutorials/<widget>/<dataset>/<size>/`** — baseline tutorial
31
- bundles for the core widgets (`show1d/`, `show4dstem/`, `showfolder/`;
32
- data shared by Show2D and Show3D lives under `shared/`). These back the
33
- one-call loaders in `quantem.widget.datasets` and the Colab workshops.
34
- Baseline tutorials only — real work uses your own data.
35
- - **`4dstem/`, `haadf/`** — full-size originals (Arina h5, Velox EMD) for
36
- power users.
37
 
38
- Every NumPy bundle ships a `meta.json` (shape, dtype, sampling, units,
39
- optics) next to `data.npy`.
 
 
 
 
 
40
 
41
- Workshop notebooks (Google Colab, no local install):
42
- [Show4DSTEM demo](https://colab.research.google.com/gist/bobleesj/54864ce5b2a6f0a4fd5ae1e5d5719b45/show4dstem_colab.ipynb)
43
- ·
44
- [Workshop v1](https://colab.research.google.com/gist/bobleesj/a05a90185c6cddbb331342cae6d7e9c1/berk_workshop_v1.ipynb)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  ## Acquisition parameters
47
 
48
- The 20260423 drift session's optics are **confirmed via the session's own
49
- HAADF EMD** (`AccelerationVoltage`, `BeamConvergence`, `CameraLength`).
50
- 4D-STEM `scan_sampling` is an **operator pattern from a sibling SSB session**
51
- — the drift acquisition itself was never per-file calibrated.
 
 
 
 
 
 
 
 
52
 
53
- | dataset | voltage | probe | CL | scan | scan sampling | det pitch | mag |
54
- |---|---|---|---|---|---|---|---|
55
- | `haadf/gold_haadf_npy` | 300 kV ✓ | 30 mrad ✓ | 91 mm ✓ | 4096² image | 0.0186 nm/px | n/a | FOV 76.2 nm |
56
- | `4dstem/gold_512_npy_bin8` | 300 kV ✓ | 30 mrad ✓ | 91 mm ✓ | 512² | 0.5 Å (op) | 3.68 mrad/px | unknown |
57
- | `4dstem/gold_512_npy_bin4` | 300 kV ✓ | 30 mrad ✓ | 91 mm ✓ | 512² | 0.5 Å (op) | 1.84 mrad/px | unknown |
58
- | `4dstem/gold_512` | 300 kV ✓ | 30 mrad ✓ | 91 mm ✓ | 512² | 0.5 Å (op) | 0.46 mrad/px | unknown |
59
- | `4dstem/gold_30mrad1.3mx04`…`09` | 300 kV | 30 mrad | 91 mm | smaller | (session yaml) | 0.46 mrad/px | 1.3 Mx |
60
- | `haadf/gold_haadf.emd` | 300 kV ✓ | 30 mrad ✓ | 91 mm ✓ | 4096² image | 0.0186 nm/px | n/a | FOV 76.2 nm |
61
 
62
  ✓ = confirmed via EMD/yaml. `(op)` = operator pattern, not file-certified.
63
 
64
  ## License
65
 
66
- MIT free to use for anything. Citing
67
- [quantem.widget](https://github.com/bobleesj/quantem.widget) in publications
68
- is appreciated.
 
 
 
 
 
 
 
 
10
 
11
  # quantem-data
12
 
13
+ **This page is in draft mode. We are still exploring the upload process
14
+ and the `meta.json` template.** Keys, the checker, and these instructions
15
+ will change as we try real pull requests. Use the current required keys
16
+ for new PRs, and expect the template to grow.
17
 
18
+ **This repo is currently used primarily by
19
+ [quantem.widget**](https://github.com/electronmicroscopy/quantem.widget)
20
+ (tutorial notebooks and `quantem.widget.datasets`). Other QuantEM packages
21
+ may use it later.
22
+
23
+ Public electron-microscopy data. MIT license. Downloads need no token.
24
+ This page is the upload and download protocol.
25
+
26
+ ## Download
27
 
28
  ```python
 
29
  from quantem.widget.datasets import show4dstem_gold
30
 
31
+ ds = show4dstem_gold(size="small")
32
+ ```
33
+
34
+ Notebook: [Example Data](https://github.com/electronmicroscopy/quantem.widget/blob/main/docs/tutorials/download_data.ipynb).
35
+
36
+ ## Add data (Hugging Face pull request)
37
+
38
+ Anyone with a Hugging Face account can add data. We follow a pull request
39
+ process. Do not use `quantem.widget.io.upload`.
40
+
41
+ Downloads need no token. Opening a pull request does. Create an account,
42
+ then a token at
43
+ [huggingface.co/settings/tokens](https://huggingface.co/settings/tokens).
44
+ Use a **Write** token (classic). A **Read** token can download; it cannot
45
+ open a PR. A fine-grained token also works if `--create-pr` is allowed on
46
+ a public dataset. You do not need collaborator access to this repo.
47
+
48
+ ```bash
49
+ pip install huggingface_hub # skip if `hf` is already on PATH
50
+ hf auth login
51
  ```
52
 
53
+ Paste the token. The prompt hides it. When asked `Add token as git
54
+ credential? [y/N]`, answer `n`. `hf upload` reads
55
+ `~/.cache/huggingface/token`. You do not need a git credential helper.
56
+ Ignore any hint to run `git config --global credential.helper store`.
57
 
58
+ ```bash
59
+ hf auth whoami
60
+ ```
 
 
 
 
61
 
62
+ That should print your username. If you already logged in with a Read
63
+ token, run `hf auth login --force` and paste a Write token. If
64
+ `--create-pr` returns 403 after a valid login, the token is Read or the
65
+ fine-grained scopes omit Community PRs; make a Write token and login
66
+ again. On SSH (no browser on that machine), paste a token. Do not use the
67
+ browser login unless the browser and `hf` run on the same machine. Do not
68
+ commit the token.
69
 
70
+ A PR is the data file(s) plus one `meta.json`. Example already on the hub
71
+ (2 files):
72
+
73
+ ```text
74
+ widget-tutorials/show4dstem/gold-512-bin4/full/data.npy
75
+ widget-tutorials/show4dstem/gold-512-bin4/full/meta.json
76
+ ```
77
+
78
+ One EMD: `gold_haadf.emd` + `meta.json` (2 files). A session folder: the
79
+ `.emd` files + `meta.json`.
80
+
81
+ Copy this `meta.json`. Required: `name`, `source`, `date`, `sample`,
82
+ `shape`, `dtype`, `permission`. Optional: `sampling`, `units`, optics.
83
+ Omit a key if unknown. Do not invent a number.
84
+
85
+ `shape` is `[row, col]` for an image, `[frame, row, col]` for a stack, or
86
+ `[scan_row, scan_col, det_row, det_col]` for 4D-STEM. `dtype` is the NumPy
87
+ name of the stored array (or of the EMD image). Examples already on this
88
+ repo:
89
+
90
+
91
+ | Data | `shape` | `dtype` |
92
+ | ------------------------- | -------------------- | -------- |
93
+ | 2D HAADF (`gold-haadf`) | `[4096, 4096]` | `uint16` |
94
+ | 4D-STEM (`gold-512-bin4`) | `[512, 512, 48, 48]` | `uint16` |
95
+ | One EMD HAADF | `[4096, 4096]` | `uint16` |
96
+
97
+
98
+ Processed float data uses `"float32"`. Read both values from the file
99
+ (`arr.shape`, `arr.dtype`, or the EMD image) and copy them into
100
+ `meta.json`.
101
+
102
+ ```json
103
+ {
104
+ "name": "gold_512_npy_bin4",
105
+ "source": "Arina 4D-STEM, Nano Titan, session 20260423, gold_004_master.h5",
106
+ "date": "2026-04-23",
107
+ "sample": "gold nanoparticles",
108
+ "shape": [512, 512, 48, 48],
109
+ "dtype": "uint16",
110
+ "permission": {
111
+ "received": true,
112
+ "from": ["Sangjoon Lee"],
113
+ "collectors": ["Sangjoon Lee"],
114
+ "date": "2026-04-23"
115
+ },
116
+ "sampling": [0.5, 0.5, 1.84, 1.84],
117
+ "units": ["A", "A", "mrad", "mrad"],
118
+ "voltage_kV": 300,
119
+ "semiangle_mrad": 30,
120
+ "camera_length_mm": 91
121
+ }
122
+ ```
123
+
124
+ `source` is microscope, facility, and original file. `date` is acquisition
125
+ day (`YYYY-MM-DD`). `permission.received` must be `true`. `from` is who granted the share (one name or several). `collectors` is who
126
+ used the instrument (one name or several). `permission.date` is when that
127
+ share was granted.
128
+
129
+ ```bash
130
+ python -m check_meta ./gold-512-bin4
131
+ hf upload bobleesj/quantem-data ./gold-512-bin4 \
132
+ widget-tutorials/show4dstem/gold-512-bin4/full \
133
+ --repo-type dataset --create-pr
134
+ ```
135
+
136
+ Run `python -m check_meta` from a checkout of this dataset so the
137
+ `check_meta/` package is importable. `./gold-512-bin4` is your local
138
+ folder (`data.npy` + `meta.json`). The last `hf upload` path is the
139
+ folder on this repo. The hub path is
140
+ `widget-tutorials/<widget>/<name>/<size>/`. For a new ShowBragg set that
141
+ would be `widget-tutorials/showbragg/<name>/full`. Size is `small`,
142
+ `medium`, `large`, or `full`. Use `widget-tutorials/shared/` only when
143
+ more than one widget will read the same files.
144
+
145
+ `python -m check_meta` must print `ok` or do not open the PR. Resume a stopped
146
+ upload with `--revision refs/pr/N` (do not pass `--create-pr` again).
147
+ Merge is on the
148
+ [Community tab](https://huggingface.co/datasets/bobleesj/quantem-data/discussions).
149
+
150
+ ### Coding agents
151
+
152
+ A coding agent can open the Community pull request. The human creates the
153
+ Write token and attests permission. The agent does not invent
154
+ `permission.*`, `sampling`, or optics. The agent does not run
155
+ `hf auth login` (that prompt is interactive). The agent does not write the
156
+ token into a repository file.
157
+
158
+ ```bash
159
+ # human already ran `hf auth login` or exported HF_TOKEN (Write)
160
+ hf auth whoami
161
+ hf download bobleesj/quantem-data --repo-type dataset \
162
+ --include "check_meta/**" --include "templates/meta.json" \
163
+ --local-dir ./quantem-data-tools
164
+ export PYTHONPATH="$PWD/quantem-data-tools${PYTHONPATH:+:$PYTHONPATH}"
165
+ python -m check_meta ./gold-512-bin4
166
+ hf upload bobleesj/quantem-data ./gold-512-bin4 \
167
+ widget-tutorials/show4dstem/gold-512-bin4/full \
168
+ --repo-type dataset --create-pr \
169
+ --commit-message "add show4dstem gold-512-bin4 full tutorial data"
170
+ ```
171
+
172
+ `hf auth whoami` must print a username. If it fails or `--create-pr`
173
+ returns 403, stop and ask the human for a Write token (`export HF_TOKEN=...`).
174
+ If `python -m check_meta` is not `ok`, stop. After the upload, give the
175
+ human the Community URL. Do not merge.
176
+
177
+ Copyable widget prompt:
178
+ [Add tutorial data](https://github.com/electronmicroscopy/quantem.widget/blob/main/docs/agent-prompts.md#add-tutorial-data-hugging-face-pull-request).
179
+
180
+ If a notebook should call `quantem.widget.datasets`, open a GitHub PR after
181
+ this one merges:
182
+ [Contribute tutorial data](https://github.com/electronmicroscopy/quantem.widget/blob/main/docs/tutorials/contribute_data.md).
183
+
184
+ Colab: [Show4DSTEM](https://colab.research.google.com/gist/bobleesj/54864ce5b2a6f0a4fd5ae1e5d5719b45/show4dstem_colab.ipynb)
185
+ · [Workshop v1](https://colab.research.google.com/gist/bobleesj/a05a90185c6cddbb331342cae6d7e9c1/berk_workshop_v1.ipynb)
186
 
187
  ## Acquisition parameters
188
 
189
+ The 20260423 drift session optics are confirmed via the session HAADF EMD.
190
+ 4D-STEM `scan_sampling` is an operator pattern from a sibling SSB session.
191
+
192
+
193
+ | dataset | voltage | probe | CL | scan | scan sampling | det pitch | mag |
194
+ | -------------------------------- | -------- | --------- | ------- | ----------- | -------------- | ------------ | ----------- |
195
+ | `haadf/gold_haadf_npy` | 300 kV ✓ | 30 mrad ✓ | 91 mm ✓ | 4096² image | 0.0186 nm/px | n/a | FOV 76.2 nm |
196
+ | `4dstem/gold_512_npy_bin8` | 300 kV ✓ | 30 mrad ✓ | 91 mm ✓ | 512² | 0.5 Å (op) | 3.68 mrad/px | unknown |
197
+ | `4dstem/gold_512_npy_bin4` | 300 kV ✓ | 30 mrad ✓ | 91 mm ✓ | 512² | 0.5 Å (op) | 1.84 mrad/px | unknown |
198
+ | `4dstem/gold_512` | 300 kV ✓ | 30 mrad ✓ | 91 mm ✓ | 512² | 0.5 Å (op) | 0.46 mrad/px | unknown |
199
+ | `4dstem/gold_30mrad1.3mx04`…`09` | 300 kV | 30 mrad | 91 mm | smaller | (session yaml) | 0.46 mrad/px | 1.3 Mx |
200
+ | `haadf/gold_haadf.emd` | 300 kV ✓ | 30 mrad ✓ | 91 mm ✓ | 4096² image | 0.0186 nm/px | n/a | FOV 76.2 nm |
201
 
 
 
 
 
 
 
 
 
202
 
203
  ✓ = confirmed via EMD/yaml. `(op)` = operator pattern, not file-certified.
204
 
205
  ## License
206
 
207
+ Files on this repo are published under **MIT**.
208
+
209
+ **You must have explicit written permission from the person who collected
210
+ the data before you open a pull request**, unless you collected the data
211
+ yourself. Put that receipt in `meta.json` (`permission.received`,
212
+ `permission.from`, `permission.collectors`, `permission.date`). If you
213
+ collected it yourself, set `from` and `collectors` to your name.
214
+
215
+ Do not open a pull request without that permission. `python -m check_meta`
216
+ must print `ok`.
check_meta/__init__.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Refuse a folder whose meta.json is missing required keys.
2
+
3
+ Required: name, source, date, sample, shape, dtype, permission
4
+ (permission.received, permission.from, permission.collectors,
5
+ permission.date). from and collectors are one or more names.
6
+ Optional: sampling, units, and optics keys.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import json
12
+ import re
13
+ from pathlib import Path
14
+
15
+ REQUIRED = ("name", "source", "date", "sample", "shape", "dtype", "permission")
16
+ PERMISSION_REQUIRED = ("received", "from", "collectors", "date")
17
+ DATE_RE = re.compile(r"^\d{4}-\d{2}-\d{2}$")
18
+
19
+
20
+ def _name_list(value: object) -> list[str] | None:
21
+ """Accept one name or a list of names. Empty is missing."""
22
+
23
+ if isinstance(value, str) and value.strip():
24
+ return [value.strip()]
25
+ if isinstance(value, list) and value and all(isinstance(item, str) and item.strip() for item in value):
26
+ return [item.strip() for item in value]
27
+ return None
28
+
29
+
30
+ def check_meta(folder: Path) -> list[str]:
31
+ """Return a list of problems. Empty means the sidecar is complete."""
32
+
33
+ problems: list[str] = []
34
+ path = folder / "meta.json"
35
+ if not path.is_file():
36
+ return [f"missing {path}"]
37
+ try:
38
+ meta = json.loads(path.read_text(encoding="utf-8"))
39
+ except json.JSONDecodeError as exc:
40
+ return [f"{path} is not JSON: {exc}"]
41
+ if not isinstance(meta, dict):
42
+ return [f"{path} must be a JSON object"]
43
+ for key in REQUIRED:
44
+ value = meta.get(key)
45
+ if value in (None, "", []):
46
+ problems.append(f"missing {key}")
47
+ if "name" in meta and not isinstance(meta.get("name"), str):
48
+ problems.append("name must be a string")
49
+ if "source" in meta and not isinstance(meta.get("source"), str):
50
+ problems.append("source must be a string")
51
+ if "sample" in meta and not isinstance(meta.get("sample"), str):
52
+ problems.append("sample must be a string")
53
+ permission = meta.get("permission")
54
+ if permission not in (None, "", []):
55
+ if not isinstance(permission, dict):
56
+ problems.append("permission must be an object")
57
+ else:
58
+ for key in PERMISSION_REQUIRED:
59
+ value = permission.get(key)
60
+ if value in (None, "", []):
61
+ problems.append(f"missing permission.{key}")
62
+ if permission.get("received") is not True:
63
+ problems.append("permission.received must be true")
64
+ if permission.get("from") not in (None, "", []) and _name_list(permission.get("from")) is None:
65
+ problems.append("permission.from must be one name or a list of names")
66
+ if permission.get("collectors") not in (None, "", []) and _name_list(permission.get("collectors")) is None:
67
+ problems.append("permission.collectors must be one name or a list of names")
68
+ perm_date = permission.get("date")
69
+ if perm_date not in (None, "") and not (isinstance(perm_date, str) and DATE_RE.match(perm_date)):
70
+ problems.append("permission.date must be YYYY-MM-DD")
71
+ date = meta.get("date")
72
+ if date not in (None, "", []) and not (isinstance(date, str) and DATE_RE.match(date)):
73
+ problems.append("date must be YYYY-MM-DD")
74
+ shape = meta.get("shape")
75
+ if shape not in (None, "", []):
76
+ if not (
77
+ isinstance(shape, list)
78
+ and len(shape) >= 2
79
+ and all(isinstance(n, int) and not isinstance(n, bool) for n in shape)
80
+ ):
81
+ problems.append("shape must be a list of ints, length >= 2")
82
+ dtype = meta.get("dtype")
83
+ if dtype not in (None, "", []) and not isinstance(dtype, str):
84
+ problems.append("dtype must be a string")
85
+ sampling = meta.get("sampling")
86
+ if sampling not in (None, "", []):
87
+ if not (isinstance(sampling, list) and all(isinstance(n, (int, float)) and not isinstance(n, bool) for n in sampling)):
88
+ problems.append("sampling must be a list of numbers")
89
+ if isinstance(shape, list) and isinstance(sampling, list) and len(sampling) != len(shape):
90
+ problems.append("sampling length must match shape length")
91
+ units = meta.get("units")
92
+ if units not in (None, "", []):
93
+ if not (isinstance(units, list) and all(isinstance(u, str) for u in units)):
94
+ problems.append("units must be a list of strings")
95
+ if isinstance(shape, list) and isinstance(units, list) and len(units) != len(shape):
96
+ problems.append("units length must match shape length")
97
+ return problems
check_meta/__main__.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """CLI: python -m check_meta /path/to/folder"""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import sys
7
+ from pathlib import Path
8
+
9
+ from check_meta import REQUIRED, check_meta
10
+
11
+
12
+ def main(argv: list[str] | None = None) -> int:
13
+ parser = argparse.ArgumentParser(prog="python -m check_meta")
14
+ parser.add_argument("folder", type=Path, help="folder that contains meta.json")
15
+ args = parser.parse_args(argv)
16
+ folder: Path = args.folder
17
+ if not folder.is_dir():
18
+ print(f"not a folder: {folder}", file=sys.stderr)
19
+ return 2
20
+ problems = check_meta(folder)
21
+ if problems:
22
+ print(f"{folder / 'meta.json'}:")
23
+ for item in problems:
24
+ print(f" {item}")
25
+ return 1
26
+ print(f"{folder / 'meta.json'}: ok ({', '.join(REQUIRED)})")
27
+ return 0
28
+
29
+
30
+ if __name__ == "__main__":
31
+ raise SystemExit(main())
templates/meta.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "gold_512_npy_bin4",
3
+ "source": "Arina 4D-STEM, Nano Titan, session 20260423, gold_004_master.h5",
4
+ "date": "2026-04-23",
5
+ "sample": "gold nanoparticles",
6
+ "shape": [512, 512, 48, 48],
7
+ "dtype": "uint16",
8
+ "permission": {
9
+ "received": true,
10
+ "from": ["Sangjoon Lee"],
11
+ "collectors": ["Sangjoon Lee"],
12
+ "date": "2026-04-23"
13
+ },
14
+ "sampling": [0.5, 0.5, 1.84, 1.84],
15
+ "units": ["A", "A", "mrad", "mrad"],
16
+ "voltage_kV": 300,
17
+ "semiangle_mrad": 30,
18
+ "camera_length_mm": 91
19
+ }