Skip to content

feat: template snapshots and image header facts for tfmgr migrate - #20

Merged
oesteban merged 3 commits into
masterfrom
feat/migrate-snapshot
Dec 4, 2026
Merged

oesteban merged 3 commits into
masterfrom
feat/migrate-snapshot

Conversation

@oesteban

@oesteban oesteban commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

Summary

Third of seven PRs for WP4 of Archive 2.0. Stacked on #19 (base feat/generate).

tfmanager/migrate/
├── __init__.py
├── snapshot.py             # Snapshot of a template without image data, from the v2 zip or a commit
├── headers.py              # facts of image headers: which files, read with nibabel, TSV keyed by md5
└── data/headers-2.0.0.tsv  # the facts of the Archive (1,147 files), package data

Snapshot. Each visible file has its size and md5 (from the annex key when annexed), the content of files in git, and the template's .renames.tsv rows. The fingerprint is what tfmgr migrate apply will compare against before it touches a checkout:

fingerprint = "tfm1:" + sha256(
    for each visible path, in byte order:  "{path}\t{size}\t{md5}\n"
    "--\n"
    for each renames row, sorted:          "{old_path}\t{new_path}\n")

Hidden files (.github/, .datalad/) do not count, so bot commits leave it unchanged.

Header facts.

tfmgr migrate headers ARCHIVE --needs      # list needed files whose data are absent
datalad get $(...)                         # fetch them
tfmgr migrate headers ARCHIVE -o TSV       # path, md5, format, shape, zooms, origin, dtype, intent, arrays

The files needed:

  • volumes without an index res (no res, or res-iso4mm);
  • every volume of a template without res blocks;
  • probseg without label (4D or not);
  • GIFTI files whose suffix does not allow their extension.

Each file is read through its link in the template, because nibabel tells formats apart by the file name.

The facts of the Archive (for your approval)

headers-2.0.0.tsv: 1,147 files, 242 NIfTI and 905 GIFTI. Read from checkouts whose fingerprints equal the v2 skeleton's (python-client 45755e5) for all 30 templates; 25 sampled md5s re-checked against the bytes.

4D probabilistic segmentations: 45 (the rows of their probseg.tsv, T02).

  • MNI152NLin2009cAsym (15): MIAL67ThalamicNuclei 14 volumes; aseg 44 (res 1 to 4); DiFuMo 64 to 1024 (res 2 and 3).
  • MNI152NLin6Asym (11): HOCPA 48, HOCPAL 96, HOSPA 21 (res 1 and 2); DiFuMo 64 to 1024 (res 2).
  • SUIT (1): Diedrichsen2009, 34 volumes.
  • dhcpVol (18): for each of 9 cohorts, structures 87 and tissues 9.

Grids, for res names and blocks (R11, R12, new blocks).

  • No ambiguity:
    • MNI305: 1 mm for T1w and the brain mask, 2 mm for the head mask.
    • MNIColin27: 1 mm.
    • NMT31Sym: 0.25 and 0.5 mm, matching res-1 and res-2 in its names.
    • VALiDATe29: 0.3 mm.
    • dhcpVol: 0.5 mm, the same for all cohorts.
    • RESILIENT: 0.19 mm with one origin per cohort, so one block per cohort.
    • SUIT (7) and UNCInfant (39) volumes without res: they match block 1.
    • MNI152NLin2009cSym res-iso4mm: it matches block iso4mm, so it becomes res-4.
  • Fischer344: the hemi-L and hemi-R brain masks have x and z swapped, in the voxel data and in the affine. Transposed back, they cover the brain mask (Dice 0.997); as stored, they are misplaced in world space. This is a data fix for the template's PR. The other 9 volumes share one 0.06 mm grid.
  • MNI152NLin2009cAsym DiFuMo: the res-02 and res-03 files are on DiFuMo's own padded grid (104×123×104 at 2 and 3 mm, origin of the 1 mm block), not on blocks 02 and 03 (97×115×97 and 65×77×65). The data are right at their zooms. Their names and blocks are for PR 4 to decide.
  • MouseIn: the description's blocks 1 and 2 are swapped. All 10 res-1 files are on the 0.15 mm grid and both res-2 files on the 0.025 mm grid, while block 1 says 0.025 mm and block 2 says 0.15 mm. No file is misnamed; the blocks are rebuilt from the headers (T06). (Corrected after merge: this line first said the three res-1 tissue maps were res-2 files.)
  • Origins with flipped signs: MouseIn's block 2 and RESILIENT's cohort origin values give x and y with the opposite sign of the headers, which use RAS like every other template. That looks like ITK's LPS. New blocks are built from the headers (T06).

GIFTI.

  • NMT31Sym: all 831 .label.gii files are meshes (pointset plus triangles), so R13's _boundary.surf.gii fits. 4 of them (the CHARM lobes at scale-1) have no desc-kN, so R13 must cover them too.
  • NMT31Sym 5D atlas-CHARM_dseg and atlas-SARM_dseg (res 1 and 2; x, y, z, 1, 6): each of their 6 volumes equals the matching desc-levelN file, so they duplicate them. Whether to drop or rename them is for PR 4.
  • dhcpAsym: label.gii (18) are label tables (int32, LABEL intent), so they become dseg.label.gii. roi.shape.gii (18) and myelinmap.func.gii (18) are float data, so they are .shape.gii.
  • fsLR desc-nomedialwall_dparc.label.gii (6) are float32 masks (NONE or NORMAL intent), not label tables, so R17's dseg.label.gii would not fit them (PR 4). onavg desc-cortex_dparc.label.gii (6) are label tables (uint8), so R17 fits.
  • fsLR (6) and fsaverage (2) desc-vaavg_midthickness.shape.gii hold one float per vertex (vertex areas), not a mesh (PR 4).
  • Vertex counts match den in all 74 files that have one (10k, 32k, 41k, 59k, 164k).

Evidence

  • On the Archive, load_v2_zip (python-client 45755e5, md5 cfb9f247…) and load_checkout of every template give the same fingerprint for all 30 templates.

  • test_bundled_headers: the shipped table covers exactly the 1,147 files needed() lists in the pinned v2 zip, all with matching md5s.

  • test_migrate_snapshot.py:

    • load_checkout: annex links, unlocked pointers, non-annex links left out, rows;
    • a v2 zip built from the same files gives the same fingerprint;
    • commits to hidden files keep the fingerprint, and a change of content or of rows changes it;
    • needed() with and without res blocks;
    • the TSV round trip, and the ok, missing and stale states;
    • read_header against images.grid on NIfTI-1, NIfTI-2 (4D) and GIFTI;
    • the CLI: --needs, exit 1 without data, exit 2 naming the file when nibabel cannot read it, the table.

    While writing these tests, I found that reading the annex object (whose key carries its own extension) could hand nibabel the wrong format; the command now reads through the link.

  • An unlocked annexed file whose content is absent (a git-annex pointer) has no local path, like a locked one: --needs lists it, and the content checks of tfmgr validate no longer open it as an image (test_local_path, test_cli_headers_unlocked; both fail without 874afa8). needed() also selects .nii volumes, as the content checks do.

  • On the Archive after 874afa8: tfmgr migrate headers ~/tmp/templateflow --needs gives 1147 files needed, 0 without data.

  • CI only runs for pull requests into master, so this stacked PR has no checks. The CI job, run locally on a clean worktree of 874afa8:

    • Python 3.13.7: ruff check . clean; pytest -o addopts="" --doctest-modules tfmanager: 219 passed.
    • Python 3.15.0rc2: ruff check . clean; 219 passed.
    • sdist and wheel build; twine check passed; the wheel ships tfmanager/migrate/data/*.tsv.

Merge Danger

Door: two-way

Blast Radius: CLI

A new command group, tfmgr migrate, with one read-only command, and 194 KB of package data. Nothing else changes, apart from GitTree gaining mode() and link_target(), and DirectoryTree.local_path returning None for git-annex pointers whose content is absent (content validation then skips them, as it skips locked annexed files without content).

tfmanager.migrate.snapshot reads a template, without image data, into a
Snapshot: the size and md5 of each visible file (from the annex key of
annexed files), the content of files in git, and the rows of .renames.tsv.
It reads from the v2 skeleton zip of the client (manifest and root
renames.tsv) or from any commit of a checkout (GitTree). Both give the same
fingerprint (tfm1: sha256 of path, size and md5, plus the rows), and hidden
files do not count. On the Archive, the two agree for all 30 templates.

`tfmgr migrate headers ARCHIVE [--needs] [-o TSV]` lists the files whose
headers the migration needs (volumes without an index res, every volume of
templates without res blocks, probseg without label, GIFTI files whose
suffix does not allow their extension) and reads them with nibabel: shape,
zooms and origin as images.grid gives them, dtype, intent and the number of
GIFTI arrays. Files are read through their link in the template, because
nibabel tells formats apart by the file name. The table is keyed by path and
md5, so a changed file has a stale row.

GitTree gains mode() and link_target(). The tests get a pinned() download
helper and the v2 skeleton fixture (python-client 45755e5, md5 checked).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@oesteban
oesteban force-pushed the feat/migrate-snapshot branch from 305ffce to 805b519 Compare December 4, 2026 11:35
The facts of the 1,147 files that the migration of the Archive needs, read
with `tfmgr migrate headers` from a checkout matching the v2 skeleton of
python-client 45755e5 (the zip and the checkouts have the same fingerprint
for all 30 templates): 242 NIfTI images and 905 GIFTI files, keyed by path
and md5. Shipped as package data, so plans and CI need no image data.

test_bundled_headers checks that the table covers exactly the files
needed() lists in the pinned v2 zip, with matching md5s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@oesteban
oesteban marked this pull request as ready for review December 4, 2026 12:15
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-04T12:18:30.965249Z 4a5f8fb Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Base automatically changed from feat/generate to master December 4, 2026 12:17

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4a5f8fbbff

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tfmanager/migrate/headers.py Resolved
Comment thread tfmanager/cli.py Resolved
Comment thread tfmanager/migrate/headers.py Resolved
DirectoryTree.local_path returned an unlocked annexed file whose content is
absent (a git-annex pointer) as if it were the data, so `tfmgr migrate
headers --needs` left it out and reading it failed, and the content checks
of `tfmgr validate` would open the pointer as an image. Such a file now has
no local path, like a locked annexed file without content.

needed() also selects `.nii` volumes, as the content checks do (no suffix of
the rule set allows `.nii`, and the Archive has none).

Reported by the Codex review of #20.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@oesteban
oesteban merged commit bc6a2f2 into master Dec 4, 2026
6 checks passed
@oesteban
oesteban deleted the feat/migrate-snapshot branch December 4, 2026 12:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant