From 0c31320c0f6e80372c181b9c9158c17ee3d2cd8b Mon Sep 17 00:00:00 2001 From: Oscar Esteban Date: Fri, 4 Dec 2026 15:10:39 +0100 Subject: [PATCH 1/2] feat(migrate): one-class probsegs take label, and copies merge R21: a probabilistic segmentation of one volume (from the header facts) names its class with label, so desc-brain_probseg becomes label-brain_probseg in MNI152NLin2009cAsym (res-1) and WHS (res-2). A 4D probseg keeps its desc. R22: a file renamed onto a file with the same bytes merges into it. Its old name stays a row of the renames table, pointing at the file it merges into. In MNI152NLin2009cAsym, res-01_desc-brain_probseg was kept in 2020 as a copy of res-01_label-brain_probseg; both old names now point at res-1_label-brain_probseg. The empty res-02_atlas-HOSPA_probseg of MNI152NLin2009cAsym (every voxel 0) is removed by a decision of the config (an n/a row). Co-Authored-By: Claude Opus 5.5 --- CHANGES.rst | 7 ++-- tfmanager/migrate/config/2.0.0.yaml | 9 ++++++ tfmanager/migrate/names.py | 45 +++++++++++++++++++++++--- tfmanager/tests/data/renames-2.0.0.tsv | 5 +-- tfmanager/tests/test_migrate_names.py | 43 +++++++++++++++++++++--- 5 files changed, 95 insertions(+), 14 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 28495d9..82d7d78 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -18,14 +18,15 @@ Unreleased Archive are shipped with tfmanager (``migrate/data/headers-2.0.0.tsv``). Snapshots of templates, from the v2 skeleton zip or from a checkout, have the same fingerprint. * The new names of the migration to TemplateFlow 2.0: ``tfmanager.migrate.names`` renames - the files of a template (rules R01 to R20, then R99) from its snapshot, the header facts + the files of a template (rules R01 to R22, then R99) from its snapshot, the header facts and the decisions of ``migrate/config/2.0.0.yaml`` (atlas labels, renames of single templates, ``res`` keys). Every volume takes the ``res`` key of its grid: blocks that no file matches are rebuilt from their files, and volumes off their block take a new key. Findings M101 to M113 block a plan (uncovered names come with a decision to fill in), and ``migrate.renames`` merges and checks a template's ``.renames.tsv`` as the template - action reads it. A test pins the renames of the Archive - (``tests/data/renames-2.0.0.tsv``). + action reads it. A probabilistic segmentation of one volume names its class with + ``label`` (R21), and a file renamed onto a copy with the same bytes merges into it + (R22). A test pins the renames of the Archive (``tests/data/renames-2.0.0.tsv``). * New warning TF118 redundant-entity: a ``desc``, ``seg`` or ``label`` that tells its file apart from no other file of the template (``res``, ``den``, ``hemi`` and ``cohort`` aside). Masks keep ``desc``, probabilistic segmentations keep ``label``, and diff --git a/tfmanager/migrate/config/2.0.0.yaml b/tfmanager/migrate/config/2.0.0.yaml index 5d2fe8f..6afeb53 100644 --- a/tfmanager/migrate/config/2.0.0.yaml +++ b/tfmanager/migrate/config/2.0.0.yaml @@ -132,6 +132,15 @@ renames: 5D images (x, y, z, 1, 6) whose six volumes equal the six level images, and their tables; the level images have their own tables. + - id: empty-HOSPA-probseg + template: tpl-MNI152NLin2009cAsym + match: {atlas: HOSPA, suffix: probseg} + remove: true + count: 1 + why: >- + Every voxel is 0 (3D uint8, 97x115x97, added 2019-05-12 with no res-01 sibling). The + probabilistic Harvard-Oxford subcortical atlas is in MNI152NLin6Asym (21 volumes). + # Keys of files whose grid is known to be wrong (a data fix follows), or ambiguous (M109) res_assign: tpl-Fischer344/tpl-Fischer344_hemi-L_desc-brain_mask.nii.gz: diff --git a/tfmanager/migrate/names.py b/tfmanager/migrate/names.py index cd929ab..2f12173 100644 --- a/tfmanager/migrate/names.py +++ b/tfmanager/migrate/names.py @@ -1,4 +1,4 @@ -"""The names of a template's files in TemplateFlow 2.0: rules R01 to R20, then R99. +"""The names of a template's files in TemplateFlow 2.0: rules R01 to R21, then R99. Each file goes through the config's decisions about its template (``renames``), then the rules in order; R99 writes the entities in the order of the rule set. Rules only match @@ -7,7 +7,8 @@ The result is checked: the new names must pass the naming checks of ``tfmgr validate`` in strict mode (M101), be unique (M103, M104), not reuse an old name of the renames table (M105) nor a name the legacy skeleton leaves out (M106), and stay in their directory (M107). -A decision of the config must match old names only (M113). +A decision of the config must match old names only (M113). A renamed file that would take +the name of a file with the same bytes (a copy kept for an older name) merges into it (R22). """ from __future__ import annotations @@ -17,7 +18,7 @@ from collections.abc import Callable, Mapping from dataclasses import dataclass, field, replace -from ..naming import FileName, parse, serialize +from ..naming import VOLUME_EXTENSIONS, FileName, parse, serialize from ..ruleset import load_ruleset from ..tree import MappingTree from ..validate import validate_template @@ -240,9 +241,23 @@ def r20(n: Name, ctx: Context) -> Name: return n +@rule('R21', 'A probseg of one volume names its class with label: desc- becomes label-.') +def r21(n: Name, ctx: Context) -> Name: + if n.suffix != 'probseg' or n.extension not in VOLUME_EXTENSIONS: + return n + if n.get('desc') is None or n.get('label') is not None: + return n + entry = ctx.snapshot.entries[ctx.path] + header, state = ctx.headers.get(f'{ctx.snapshot.name}/{ctx.path}', entry.md5) + if header is None: + raise Conflict('M111' if state == 'stale' else 'M110', f'Header facts {state}.') + return n.move('desc', 'label') if header.volumes == 1 else n + + #: Rules outside ``RULES``: R15 renames a file that has no BIDS name, and R99 comes last OTHER_RULES = { 'R15': 'template_sample.tsv becomes tpl-