Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,25 @@ 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``).
* The content of the migration to TemplateFlow 2.0: ``tfmanager.migrate.content`` updates
the files of a template after its renames. References to renamed files follow them in
JSON files (T05); template_description.json takes ``res`` blocks that fit its files'
headers, ``RRID:`` prefixes, ``Funding`` as a list and cohort ages as ``[min, max]``
(T06); files written by hand (T10), the generated files (T08) and ``.gitattributes``
(T09) are written; and, only when the result passes the validator in strict mode, the
versions change and CHANGES gets a 2.0.0 entry (T11). ``migrate.engine.plan_template``
joins names and content: a renamed file whose bytes change gets an ``n/a`` row, so its
old name keeps its old bytes. The config's ``edits`` correct fields of JSON files. A test
pins the ``res`` blocks of the migrated Archive (``tests/data/res-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
Expand Down
46 changes: 43 additions & 3 deletions tfmanager/migrate/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ def matches(self, entities: dict, suffix: str | None, extension: str) -> bool:
return True


@dataclass(frozen=True)
class Edit:
"""Fields to set or remove in a JSON file of one template, after the renames (``edits``)."""

id: str
template: str
path: str
"""The file's new name, relative to the template."""
why: str
set: dict = field(default_factory=dict)
remove: tuple[str, ...] = ()

def apply(self, data: dict) -> dict:
out = {k: v for k, v in data.items() if k not in self.remove}
out.update(self.set)
return out


@dataclass(frozen=True)
class Config:
version: str
Expand All @@ -56,6 +74,8 @@ class Config:
renames: tuple[Rename, ...] = ()
res_assign: dict = field(default_factory=dict)
"""``tpl-<label>/<path>`` -> ``{'res': key, 'why': ...}``."""
gitattributes: str = ''
edits: tuple[Edit, ...] = ()
digest: str = ''

def atlas_label(self, template: str, label: str) -> str | None:
Expand All @@ -67,6 +87,9 @@ def atlas_label(self, template: str, label: str) -> str | None:
def renames_for(self, template: str) -> list[Rename]:
return [r for r in self.renames if r.template == template]

def edits_for(self, template: str) -> list[Edit]:
return [e for e in self.edits if e.template == template]

def assigned(self, template: str) -> dict[str, str]:
prefix = f'{template}/'
return {
Expand Down Expand Up @@ -98,9 +121,14 @@ def _check(data: dict) -> list[str]:
for item in data.get('atlas_labels', []):
if (item['template'], item['to']) in froms:
problems.append(f'atlas_labels: {item["to"]} is renamed again (a chain).')
ids = [r['id'] for r in data.get('renames', [])]
for dup in sorted({i for i in ids if ids.count(i) > 1}):
problems.append(f'renames: the id {dup} is used twice.')
for part in ('renames', 'edits'):
ids = [r['id'] for r in data.get(part, [])]
for dup in sorted({i for i in ids if ids.count(i) > 1}):
problems.append(f'{part}: the id {dup} is used twice.')
for item in data.get('edits', []):
both = set(item.get('set', {})) & set(item.get('remove', []))
if both:
problems.append(f'edits {item["id"]}: sets and removes {", ".join(sorted(both))}.')
for item in data.get('renames', []):
for part in ('match', 'set'):
for key, value in item.get(part, {}).items():
Expand Down Expand Up @@ -148,6 +176,18 @@ def load_config(path: str | Path | None = None, version: str = '2.0.0') -> Confi
for r in data.get('renames', [])
),
res_assign=dict(data.get('res_assign', {})),
gitattributes=data.get('gitattributes', ''),
edits=tuple(
Edit(
id=e['id'],
template=e['template'],
path=e['path'],
why=e['why'],
set=e.get('set', {}),
remove=tuple(e.get('remove', ())),
)
for e in data.get('edits', [])
),
digest=hashlib.sha256(text.encode()).hexdigest(),
)

Expand Down
42 changes: 42 additions & 0 deletions tfmanager/migrate/config/2.0.0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@ policies:
# A new res key is the voxel size when it is an integer, isotropic and free; otherwise the
# smallest free index.
res_key: zoom
# A renamed text file whose bytes change gets an n/a row: its old name keeps serving its
# last old bytes (released clients, the legacy skeleton), and the new name is a new file.
content_change: freeze
# BIDSVersion and TemplateFlowVersion change only when the result passes the validator in
# strict mode (no errors at the metadata level); otherwise both stay as they are.
flip: when-clean

# T09: the .gitattributes of every template. Binary files and GIFTI (XML) go to the annex;
# the text files of the rule set stay in git, whatever their size.
gitattributes: |
* annex.backend=MD5E
**/.git* annex.largefiles=nothing
* annex.largefiles=((mimeencoding=binary)and(largerthan=0))
*.gii annex.largefiles=anything
*.json annex.largefiles=nothing
*.tsv annex.largefiles=nothing
*.md annex.largefiles=nothing
*.txt annex.largefiles=nothing
LICENSE annex.largefiles=nothing
CHANGES annex.largefiles=nothing

# R01: atlas labels that are versions or typos become names (author and year)
atlas_labels:
Expand Down Expand Up @@ -132,6 +152,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:
Expand All @@ -142,3 +171,16 @@ res_assign:
tpl-Fischer344/tpl-Fischer344_hemi-R_desc-brain_mask.nii.gz:
res: '1'
why: As its hemi-L twin.

# Edits of JSON files, after the renames (paths are new names, relative to the template):
# fields to set or remove, with the reason
edits:
- id: Diedrichsen2009-source
template: tpl-MNI152NLin6Asym
path: tpl-MNI152NLin6Asym_atlas-Diedrichsen2009_res-1_dseg.json
set: {Source: Diedrichsen_2009/atl-Anatom_space-MNI_dseg.nii}
why: >-
Source was a path on its author's computer (TF320). SourceRepository and SourceCommit
name the repository and its commit; at that commit, the file
Diedrichsen_2009/atl-Anatom_space-MNI_dseg.nii has the sidecar's SourceSHA256
(434471d8..., checked 2026-12-04).
26 changes: 24 additions & 2 deletions tfmanager/migrate/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
"version": {"type": "string", "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$"},
"policies": {
"type": "object",
"required": ["res_key"],
"required": ["res_key", "content_change", "flip"],
"additionalProperties": false,
"properties": {"res_key": {"enum": ["zoom"]}}
"properties": {
"res_key": {"enum": ["zoom"]},
"content_change": {"enum": ["freeze"]},
"flip": {"enum": ["when-clean"]}
}
},
"gitattributes": {"type": "string", "minLength": 1, "pattern": "\\n$"},
"atlas_labels": {
"type": "array",
"items": {
Expand Down Expand Up @@ -74,6 +79,23 @@
"why": {"type": "string", "minLength": 10}
}
}
},
"edits": {
"type": "array",
"items": {
"type": "object",
"required": ["id", "template", "path", "why"],
"additionalProperties": false,
"properties": {
"id": {"type": "string", "pattern": "^[A-Za-z0-9-]+$"},
"template": {"$ref": "#/$defs/template"},
"path": {"type": "string", "pattern": "^[^/].*\\.json$"},
"set": {"type": "object", "minProperties": 1},
"remove": {"type": "array", "minItems": 1, "items": {"type": "string"}},
"why": {"type": "string", "minLength": 10}
},
"anyOf": [{"required": ["set"]}, {"required": ["remove"]}]
}
}
},
"$defs": {
Expand Down
Loading