VBoussot commited on
Commit
6afc1ec
·
verified ·
1 Parent(s): d2b63cb

Add FireANTs presets (Affine/SyN/IMPACT); optional-mask defaults; elastix skips non-restrictive masks

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitignore +2 -0
  2. CBCT_CT_HeadNeck/app.json +5 -3
  3. CBCT_CT_HeadNeck/elastix_engine.py +12 -1
  4. CBCT_CT_HeadNeck/model.pt +0 -0
  5. CBCT_CT_MRSeg/app.json +5 -3
  6. CBCT_CT_MRSeg/elastix_engine.py +12 -1
  7. CBCT_CT_MRSeg/model.pt +0 -0
  8. CBCT_CT_TS/app.json +5 -3
  9. CBCT_CT_TS/elastix_engine.py +12 -1
  10. CBCT_CT_TS/model.pt +0 -0
  11. ConvexAdam_Coarse/app.json +4 -2
  12. ConvexAdam_Composite/app.json +4 -2
  13. ConvexAdam_Fine/app.json +36 -6
  14. FireANTs_Affine/Evaluation_with_fid.yml +22 -0
  15. FireANTs_Affine/Evaluation_with_images.yml +35 -0
  16. FireANTs_Affine/Evaluation_with_seg.yml +29 -0
  17. FireANTs_Affine/Model.py +534 -0
  18. FireANTs_Affine/NOTICE +40 -0
  19. FireANTs_Affine/Prediction.yml +132 -0
  20. FireANTs_Affine/Uncertainty.yml +24 -0
  21. FireANTs_Affine/app.json +96 -0
  22. FireANTs_Affine/model.pt +3 -0
  23. FireANTs_Affine/requirements.txt +1 -0
  24. FireANTs_IMPACT/Evaluation_with_fid.yml +22 -0
  25. FireANTs_IMPACT/Evaluation_with_images.yml +35 -0
  26. FireANTs_IMPACT/Evaluation_with_seg.yml +29 -0
  27. FireANTs_IMPACT/Model.py +534 -0
  28. FireANTs_IMPACT/NOTICE +40 -0
  29. FireANTs_IMPACT/Prediction.yml +139 -0
  30. FireANTs_IMPACT/Uncertainty.yml +24 -0
  31. FireANTs_IMPACT/app.json +96 -0
  32. FireANTs_IMPACT/model.pt +3 -0
  33. FireANTs_IMPACT/requirements.txt +1 -0
  34. FireANTs_SyN/Evaluation_with_fid.yml +22 -0
  35. FireANTs_SyN/Evaluation_with_images.yml +35 -0
  36. FireANTs_SyN/Evaluation_with_seg.yml +29 -0
  37. FireANTs_SyN/Model.py +534 -0
  38. FireANTs_SyN/NOTICE +40 -0
  39. FireANTs_SyN/Prediction.yml +132 -0
  40. FireANTs_SyN/Uncertainty.yml +24 -0
  41. FireANTs_SyN/app.json +96 -0
  42. FireANTs_SyN/model.pt +3 -0
  43. FireANTs_SyN/requirements.txt +1 -0
  44. Generic_Rigid/app.json +4 -2
  45. Generic_Rigid/elastix_engine.py +12 -1
  46. Generic_Rigid/model.pt +0 -0
  47. Generic_Rigid_BSpline/app.json +4 -2
  48. Generic_Rigid_BSpline/elastix_engine.py +12 -1
  49. Generic_Rigid_BSpline/model.pt +0 -0
  50. MR_CT_HeadNeck/app.json +4 -2
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ __pycache__/
2
+ *.pyc
CBCT_CT_HeadNeck/app.json CHANGED
@@ -1,7 +1,7 @@
1
  {
2
  "display_name": "CBCT/CT Head&Neck",
3
  "short_description": "Optimized preset for CBCT/CT registration on head & neck",
4
- "description": "A five-level recursive B-spline deformable registration optimized for CBCT/CT head-and-neck alignment, driven by the IMPACT metric using semantic features extracted from pretrained TotalSegmentator TorchScript models. The optimization follows a multi-resolution ASGD scheme with up to 300, 300, 200, 200, and 150 iterations and 2000 stochastic spatial samples per level. Features are extracted at progressively finer voxel scales (6 mm, 3 mm, 3 mm, 2×2×3 mm, 2×2×3 mm) using L1 distances on selected internal layers of the network. A composite objective (IMPACT + mutual information + bending energy penalty, with increased MI weight) ensures robust cross-modality alignment in complex head-and-neck anatomy while enforcing smooth, physically plausible deformations.",
5
  "task": "registration",
6
  "tta": 0,
7
  "mc_dropout": 0,
@@ -22,12 +22,14 @@
22
  "FixedMask": {
23
  "display_name": "Fixed mask (optional)",
24
  "volume_type": "SEGMENTATION",
25
- "required": false
 
26
  },
27
  "MovingMask": {
28
  "display_name": "Moving mask (optional)",
29
  "volume_type": "SEGMENTATION",
30
- "required": false
 
31
  }
32
  },
33
  "outputs": {
 
1
  {
2
  "display_name": "CBCT/CT Head&Neck",
3
  "short_description": "Optimized preset for CBCT/CT registration on head & neck",
4
+ "description": "A five-level recursive B-spline deformable registration optimized for CBCT/CT head-and-neck alignment, driven by the IMPACT metric using semantic features extracted from pretrained TotalSegmentator TorchScript models. The optimization follows a multi-resolution ASGD scheme with up to 300, 300, 200, 200, and 150 iterations and 2000 stochastic spatial samples per level. Features are extracted at progressively finer voxel scales (6 mm, 3 mm, 3 mm, 2\u00d72\u00d73 mm, 2\u00d72\u00d73 mm) using L1 distances on selected internal layers of the network. A composite objective (IMPACT + mutual information + bending energy penalty, with increased MI weight) ensures robust cross-modality alignment in complex head-and-neck anatomy while enforcing smooth, physically plausible deformations.",
5
  "task": "registration",
6
  "tta": 0,
7
  "mc_dropout": 0,
 
22
  "FixedMask": {
23
  "display_name": "Fixed mask (optional)",
24
  "volume_type": "SEGMENTATION",
25
+ "required": false,
26
+ "default": "ones"
27
  },
28
  "MovingMask": {
29
  "display_name": "Moving mask (optional)",
30
  "volume_type": "SEGMENTATION",
31
+ "required": false,
32
+ "default": "ones"
33
  }
34
  },
35
  "outputs": {
CBCT_CT_HeadNeck/elastix_engine.py CHANGED
@@ -44,6 +44,17 @@ from Model import _sorted_specs, generate_impact_parameter_map, load_models_regi
44
  ELASTIX_CACHE = Path.home() / ".cache" / "konfai" / "elastix-impact"
45
 
46
 
 
 
 
 
 
 
 
 
 
 
 
47
  class ElastixEngine:
48
  """Run the elastix-IMPACT binary on a fixed/moving pair; return (moved, dvf) on the fixed grid.
49
 
@@ -235,7 +246,7 @@ class ElastixEngine:
235
 
236
  args = [str(self._elastix_bin), "-f", str(fixed_path), "-m", str(moving_path)]
237
  for flag, mask, name in (("-fMask", fixed_mask, "FixedMask.mha"), ("-mMask", moving_mask, "MovingMask.mha")):
238
- if mask is not None:
239
  mask_path = work / name
240
  sitk.WriteImage(sitk.Cast(mask, sitk.sitkUInt8), str(mask_path))
241
  args += [flag, str(mask_path)]
 
44
  ELASTIX_CACHE = Path.home() / ".cache" / "konfai" / "elastix-impact"
45
 
46
 
47
+ def _is_partial_mask(mask: "sitk.Image | None") -> bool:
48
+ """True only for a mask that actually restricts the metric region — some voxels in, some out. An
49
+ absent optional mask arrives as a whole-image (all-ones) default from KonfAI, and an all-zero mask
50
+ is degenerate; both are treated as no mask, so elastix runs without ``-fMask`` / ``-mMask`` (i.e.
51
+ the whole image) instead of paying for a mask that restricts nothing."""
52
+ if mask is None:
53
+ return False
54
+ arr = sitk.GetArrayViewFromImage(mask)
55
+ return bool((arr > 0).any()) and bool((arr == 0).any())
56
+
57
+
58
  class ElastixEngine:
59
  """Run the elastix-IMPACT binary on a fixed/moving pair; return (moved, dvf) on the fixed grid.
60
 
 
246
 
247
  args = [str(self._elastix_bin), "-f", str(fixed_path), "-m", str(moving_path)]
248
  for flag, mask, name in (("-fMask", fixed_mask, "FixedMask.mha"), ("-mMask", moving_mask, "MovingMask.mha")):
249
+ if _is_partial_mask(mask):
250
  mask_path = work / name
251
  sitk.WriteImage(sitk.Cast(mask, sitk.sitkUInt8), str(mask_path))
252
  args += [flag, str(mask_path)]
CBCT_CT_HeadNeck/model.pt CHANGED
Binary files a/CBCT_CT_HeadNeck/model.pt and b/CBCT_CT_HeadNeck/model.pt differ
 
CBCT_CT_MRSeg/app.json CHANGED
@@ -1,7 +1,7 @@
1
  {
2
  "display_name": "CBCT/CT preset with MRSegmentator",
3
  "short_description": "Generic CBCT/CT deformable registration using MRSegmentator features",
4
- "description": "A four-level recursive B-spline deformable registration optimized for generic CBCT/CT alignment, driven by the IMPACT metric using semantic features extracted from the pretrained MRSegmentator model. The scheme follows a multi-resolution strategy with up to 300, 300, 250, and 200 ASGD iterations and 2000 stochastic spatial samples per level. Features are extracted at progressively finer voxel scales (3 mm, 3 mm, 2×2×3 mm, 2×2×3 mm), with a level-dependent combination of Dice-based segmentation overlap and L1 feature distances on selected internal layers of MRSegmentator. Early levels rely on pure Dice supervision, while finer stages progressively integrate feature-level alignment with increasing L1 contribution (0.3/0.7, 0.5/0.5) and a final purely feature-based stage. The optimization minimizes a composite objective (IMPACT + mutual information + bending energy penalty), enabling robust cross-modality alignment between CBCT and CT while enforcing smooth, physically plausible deformations.",
5
  "task": "registration",
6
  "tta": 0,
7
  "mc_dropout": 0,
@@ -22,12 +22,14 @@
22
  "FixedMask": {
23
  "display_name": "Fixed mask (optional)",
24
  "volume_type": "SEGMENTATION",
25
- "required": false
 
26
  },
27
  "MovingMask": {
28
  "display_name": "Moving mask (optional)",
29
  "volume_type": "SEGMENTATION",
30
- "required": false
 
31
  }
32
  },
33
  "outputs": {
 
1
  {
2
  "display_name": "CBCT/CT preset with MRSegmentator",
3
  "short_description": "Generic CBCT/CT deformable registration using MRSegmentator features",
4
+ "description": "A four-level recursive B-spline deformable registration optimized for generic CBCT/CT alignment, driven by the IMPACT metric using semantic features extracted from the pretrained MRSegmentator model. The scheme follows a multi-resolution strategy with up to 300, 300, 250, and 200 ASGD iterations and 2000 stochastic spatial samples per level. Features are extracted at progressively finer voxel scales (3 mm, 3 mm, 2\u00d72\u00d73 mm, 2\u00d72\u00d73 mm), with a level-dependent combination of Dice-based segmentation overlap and L1 feature distances on selected internal layers of MRSegmentator. Early levels rely on pure Dice supervision, while finer stages progressively integrate feature-level alignment with increasing L1 contribution (0.3/0.7, 0.5/0.5) and a final purely feature-based stage. The optimization minimizes a composite objective (IMPACT + mutual information + bending energy penalty), enabling robust cross-modality alignment between CBCT and CT while enforcing smooth, physically plausible deformations.",
5
  "task": "registration",
6
  "tta": 0,
7
  "mc_dropout": 0,
 
22
  "FixedMask": {
23
  "display_name": "Fixed mask (optional)",
24
  "volume_type": "SEGMENTATION",
25
+ "required": false,
26
+ "default": "ones"
27
  },
28
  "MovingMask": {
29
  "display_name": "Moving mask (optional)",
30
  "volume_type": "SEGMENTATION",
31
+ "required": false,
32
+ "default": "ones"
33
  }
34
  },
35
  "outputs": {
CBCT_CT_MRSeg/elastix_engine.py CHANGED
@@ -44,6 +44,17 @@ from Model import _sorted_specs, generate_impact_parameter_map, load_models_regi
44
  ELASTIX_CACHE = Path.home() / ".cache" / "konfai" / "elastix-impact"
45
 
46
 
 
 
 
 
 
 
 
 
 
 
 
47
  class ElastixEngine:
48
  """Run the elastix-IMPACT binary on a fixed/moving pair; return (moved, dvf) on the fixed grid.
49
 
@@ -235,7 +246,7 @@ class ElastixEngine:
235
 
236
  args = [str(self._elastix_bin), "-f", str(fixed_path), "-m", str(moving_path)]
237
  for flag, mask, name in (("-fMask", fixed_mask, "FixedMask.mha"), ("-mMask", moving_mask, "MovingMask.mha")):
238
- if mask is not None:
239
  mask_path = work / name
240
  sitk.WriteImage(sitk.Cast(mask, sitk.sitkUInt8), str(mask_path))
241
  args += [flag, str(mask_path)]
 
44
  ELASTIX_CACHE = Path.home() / ".cache" / "konfai" / "elastix-impact"
45
 
46
 
47
+ def _is_partial_mask(mask: "sitk.Image | None") -> bool:
48
+ """True only for a mask that actually restricts the metric region — some voxels in, some out. An
49
+ absent optional mask arrives as a whole-image (all-ones) default from KonfAI, and an all-zero mask
50
+ is degenerate; both are treated as no mask, so elastix runs without ``-fMask`` / ``-mMask`` (i.e.
51
+ the whole image) instead of paying for a mask that restricts nothing."""
52
+ if mask is None:
53
+ return False
54
+ arr = sitk.GetArrayViewFromImage(mask)
55
+ return bool((arr > 0).any()) and bool((arr == 0).any())
56
+
57
+
58
  class ElastixEngine:
59
  """Run the elastix-IMPACT binary on a fixed/moving pair; return (moved, dvf) on the fixed grid.
60
 
 
246
 
247
  args = [str(self._elastix_bin), "-f", str(fixed_path), "-m", str(moving_path)]
248
  for flag, mask, name in (("-fMask", fixed_mask, "FixedMask.mha"), ("-mMask", moving_mask, "MovingMask.mha")):
249
+ if _is_partial_mask(mask):
250
  mask_path = work / name
251
  sitk.WriteImage(sitk.Cast(mask, sitk.sitkUInt8), str(mask_path))
252
  args += [flag, str(mask_path)]
CBCT_CT_MRSeg/model.pt CHANGED
Binary files a/CBCT_CT_MRSeg/model.pt and b/CBCT_CT_MRSeg/model.pt differ
 
CBCT_CT_TS/app.json CHANGED
@@ -1,7 +1,7 @@
1
  {
2
  "display_name": "CBCT/CT preset with TotalSegmentator",
3
  "short_description": "Generic CBCT/CT deformable registration using TotalSegmentator features",
4
- "description": "A four-level recursive B-spline deformable registration optimized for generic CBCT/CT alignment, driven by the IMPACT metric using semantic features extracted from pretrained TotalSegmentator TorchScript models. The optimization follows a multi-resolution ASGD scheme with up to 300, 300, 250, and 200 iterations using 2000 random spatial samples per level. Features are extracted at progressively finer voxel scales (3 mm, 3 mm, 2×2×3 mm, 2×2×3 mm), starting with Dice-based overlap on segmentation outputs and progressively integrating feature-level alignment via L1 distances on selected internal layers (0.3/0.7 then 0.5/0.5 L1/Dice), ending with a final purely feature-based stage. A composite objective (IMPACT + mutual information + bending energy penalty) ensures robust cross-modality alignment while enforcing smooth, physically plausible deformations.",
5
  "task": "registration",
6
  "tta": 0,
7
  "mc_dropout": 0,
@@ -22,12 +22,14 @@
22
  "FixedMask": {
23
  "display_name": "Fixed mask (optional)",
24
  "volume_type": "SEGMENTATION",
25
- "required": false
 
26
  },
27
  "MovingMask": {
28
  "display_name": "Moving mask (optional)",
29
  "volume_type": "SEGMENTATION",
30
- "required": false
 
31
  }
32
  },
33
  "outputs": {
 
1
  {
2
  "display_name": "CBCT/CT preset with TotalSegmentator",
3
  "short_description": "Generic CBCT/CT deformable registration using TotalSegmentator features",
4
+ "description": "A four-level recursive B-spline deformable registration optimized for generic CBCT/CT alignment, driven by the IMPACT metric using semantic features extracted from pretrained TotalSegmentator TorchScript models. The optimization follows a multi-resolution ASGD scheme with up to 300, 300, 250, and 200 iterations using 2000 random spatial samples per level. Features are extracted at progressively finer voxel scales (3 mm, 3 mm, 2\u00d72\u00d73 mm, 2\u00d72\u00d73 mm), starting with Dice-based overlap on segmentation outputs and progressively integrating feature-level alignment via L1 distances on selected internal layers (0.3/0.7 then 0.5/0.5 L1/Dice), ending with a final purely feature-based stage. A composite objective (IMPACT + mutual information + bending energy penalty) ensures robust cross-modality alignment while enforcing smooth, physically plausible deformations.",
5
  "task": "registration",
6
  "tta": 0,
7
  "mc_dropout": 0,
 
22
  "FixedMask": {
23
  "display_name": "Fixed mask (optional)",
24
  "volume_type": "SEGMENTATION",
25
+ "required": false,
26
+ "default": "ones"
27
  },
28
  "MovingMask": {
29
  "display_name": "Moving mask (optional)",
30
  "volume_type": "SEGMENTATION",
31
+ "required": false,
32
+ "default": "ones"
33
  }
34
  },
35
  "outputs": {
CBCT_CT_TS/elastix_engine.py CHANGED
@@ -44,6 +44,17 @@ from Model import _sorted_specs, generate_impact_parameter_map, load_models_regi
44
  ELASTIX_CACHE = Path.home() / ".cache" / "konfai" / "elastix-impact"
45
 
46
 
 
 
 
 
 
 
 
 
 
 
 
47
  class ElastixEngine:
48
  """Run the elastix-IMPACT binary on a fixed/moving pair; return (moved, dvf) on the fixed grid.
49
 
@@ -235,7 +246,7 @@ class ElastixEngine:
235
 
236
  args = [str(self._elastix_bin), "-f", str(fixed_path), "-m", str(moving_path)]
237
  for flag, mask, name in (("-fMask", fixed_mask, "FixedMask.mha"), ("-mMask", moving_mask, "MovingMask.mha")):
238
- if mask is not None:
239
  mask_path = work / name
240
  sitk.WriteImage(sitk.Cast(mask, sitk.sitkUInt8), str(mask_path))
241
  args += [flag, str(mask_path)]
 
44
  ELASTIX_CACHE = Path.home() / ".cache" / "konfai" / "elastix-impact"
45
 
46
 
47
+ def _is_partial_mask(mask: "sitk.Image | None") -> bool:
48
+ """True only for a mask that actually restricts the metric region — some voxels in, some out. An
49
+ absent optional mask arrives as a whole-image (all-ones) default from KonfAI, and an all-zero mask
50
+ is degenerate; both are treated as no mask, so elastix runs without ``-fMask`` / ``-mMask`` (i.e.
51
+ the whole image) instead of paying for a mask that restricts nothing."""
52
+ if mask is None:
53
+ return False
54
+ arr = sitk.GetArrayViewFromImage(mask)
55
+ return bool((arr > 0).any()) and bool((arr == 0).any())
56
+
57
+
58
  class ElastixEngine:
59
  """Run the elastix-IMPACT binary on a fixed/moving pair; return (moved, dvf) on the fixed grid.
60
 
 
246
 
247
  args = [str(self._elastix_bin), "-f", str(fixed_path), "-m", str(moving_path)]
248
  for flag, mask, name in (("-fMask", fixed_mask, "FixedMask.mha"), ("-mMask", moving_mask, "MovingMask.mha")):
249
+ if _is_partial_mask(mask):
250
  mask_path = work / name
251
  sitk.WriteImage(sitk.Cast(mask, sitk.sitkUInt8), str(mask_path))
252
  args += [flag, str(mask_path)]
CBCT_CT_TS/model.pt CHANGED
Binary files a/CBCT_CT_TS/model.pt and b/CBCT_CT_TS/model.pt differ
 
ConvexAdam_Coarse/app.json CHANGED
@@ -22,12 +22,14 @@
22
  "FixedMask": {
23
  "display_name": "Fixed mask (optional)",
24
  "volume_type": "SEGMENTATION",
25
- "required": false
 
26
  },
27
  "MovingMask": {
28
  "display_name": "Moving mask (optional)",
29
  "volume_type": "SEGMENTATION",
30
- "required": false
 
31
  }
32
  },
33
  "outputs": {
 
22
  "FixedMask": {
23
  "display_name": "Fixed mask (optional)",
24
  "volume_type": "SEGMENTATION",
25
+ "required": false,
26
+ "default": "ones"
27
  },
28
  "MovingMask": {
29
  "display_name": "Moving mask (optional)",
30
  "volume_type": "SEGMENTATION",
31
+ "required": false,
32
+ "default": "ones"
33
  }
34
  },
35
  "outputs": {
ConvexAdam_Composite/app.json CHANGED
@@ -22,12 +22,14 @@
22
  "FixedMask": {
23
  "display_name": "Fixed mask (optional)",
24
  "volume_type": "SEGMENTATION",
25
- "required": false
 
26
  },
27
  "MovingMask": {
28
  "display_name": "Moving mask (optional)",
29
  "volume_type": "SEGMENTATION",
30
- "required": false
 
31
  }
32
  },
33
  "outputs": {
 
22
  "FixedMask": {
23
  "display_name": "Fixed mask (optional)",
24
  "volume_type": "SEGMENTATION",
25
+ "required": false,
26
+ "default": "ones"
27
  },
28
  "MovingMask": {
29
  "display_name": "Moving mask (optional)",
30
  "volume_type": "SEGMENTATION",
31
+ "required": false,
32
+ "default": "ones"
33
  }
34
  },
35
  "outputs": {
ConvexAdam_Fine/app.json CHANGED
@@ -6,10 +6,38 @@
6
  "tta": 0,
7
  "mc_dropout": 0,
8
  "vram_plan": {
9
- "8": {"patch_size": [128, 128, 128], "batch_size": 1},
10
- "16": {"patch_size": [192, 192, 192], "batch_size": 1},
11
- "24": {"patch_size": [256, 256, 256], "batch_size": 1},
12
- "40": {"patch_size": [320, 320, 320], "batch_size": 1}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  },
14
  "models": [
15
  "model.pt"
@@ -28,12 +56,14 @@
28
  "FixedMask": {
29
  "display_name": "Fixed mask (optional)",
30
  "volume_type": "SEGMENTATION",
31
- "required": false
 
32
  },
33
  "MovingMask": {
34
  "display_name": "Moving mask (optional)",
35
  "volume_type": "SEGMENTATION",
36
- "required": false
 
37
  }
38
  },
39
  "outputs": {
 
6
  "tta": 0,
7
  "mc_dropout": 0,
8
  "vram_plan": {
9
+ "8": {
10
+ "patch_size": [
11
+ 128,
12
+ 128,
13
+ 128
14
+ ],
15
+ "batch_size": 1
16
+ },
17
+ "16": {
18
+ "patch_size": [
19
+ 192,
20
+ 192,
21
+ 192
22
+ ],
23
+ "batch_size": 1
24
+ },
25
+ "24": {
26
+ "patch_size": [
27
+ 256,
28
+ 256,
29
+ 256
30
+ ],
31
+ "batch_size": 1
32
+ },
33
+ "40": {
34
+ "patch_size": [
35
+ 320,
36
+ 320,
37
+ 320
38
+ ],
39
+ "batch_size": 1
40
+ }
41
  },
42
  "models": [
43
  "model.pt"
 
56
  "FixedMask": {
57
  "display_name": "Fixed mask (optional)",
58
  "volume_type": "SEGMENTATION",
59
+ "required": false,
60
+ "default": "ones"
61
  },
62
  "MovingMask": {
63
  "display_name": "Moving mask (optional)",
64
  "volume_type": "SEGMENTATION",
65
+ "required": false,
66
+ "default": "ones"
67
  }
68
  },
69
  "outputs": {
FireANTs_Affine/Evaluation_with_fid.yml ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Evaluator:
2
+ metrics:
3
+ FixedFid:
4
+ targets_criterions:
5
+ MovingFid:
6
+ criterions_loader:
7
+ TRE: {}
8
+ Dataset:
9
+ groups_src:
10
+ Volume_0:
11
+ groups_dest:
12
+ FixedFid:
13
+ transforms: None
14
+ Reference_0:
15
+ groups_dest:
16
+ MovingFid:
17
+ transforms: None
18
+ subset: None
19
+ dataset_filenames:
20
+ - ./Dataset:mha
21
+ validation: None
22
+ train_name: ImpactReg
FireANTs_Affine/Evaluation_with_images.yml ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Evaluator:
2
+ metrics:
3
+ FixedImage:
4
+ targets_criterions:
5
+ MovingImage;Mask:
6
+ criterions_loader:
7
+ MAESaveMap:
8
+ reduction: mean
9
+ dataset: ./Evaluations/ImpactReg/Output:mha
10
+ group: MAE_map
11
+ Dataset:
12
+ groups_src:
13
+ Volume_0:
14
+ groups_dest:
15
+ FixedImage:
16
+ transforms:
17
+ TensorCast:
18
+ dtype: float32
19
+ Reference_0:
20
+ groups_dest:
21
+ MovingImage:
22
+ transforms:
23
+ TensorCast:
24
+ dtype: float32
25
+ Mask_0:
26
+ groups_dest:
27
+ Mask:
28
+ transforms:
29
+ TensorCast:
30
+ dtype: uint8
31
+ subset: None
32
+ dataset_filenames:
33
+ - ./Dataset:mha
34
+ validation: None
35
+ train_name: ImpactReg
FireANTs_Affine/Evaluation_with_seg.yml ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Evaluator:
2
+ metrics:
3
+ FixedSeg:
4
+ targets_criterions:
5
+ MovingSeg:
6
+ criterions_loader:
7
+ DiceSaveMap:
8
+ labels: None
9
+ dataset: ./Evaluations/ImpactReg/Output:mha
10
+ group: Seg_MAE_map
11
+ Dataset:
12
+ groups_src:
13
+ Volume_0:
14
+ groups_dest:
15
+ FixedSeg:
16
+ transforms:
17
+ TensorCast:
18
+ dtype: uint8
19
+ Reference_0:
20
+ groups_dest:
21
+ MovingSeg:
22
+ transforms:
23
+ TensorCast:
24
+ dtype: uint8
25
+ subset: None
26
+ dataset_filenames:
27
+ - ./Dataset:mha
28
+ validation: None
29
+ train_name: ImpactReg
FireANTs_Affine/Model.py ADDED
@@ -0,0 +1,534 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2025 Valentin Boussot
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+ # SPDX-License-Identifier: Apache-2.0
16
+ #
17
+ # This wrapper does NOT copy any FireANTs source: it only calls the public FireANTs API of the
18
+ # separately-installed ``fireants`` wheel (PyPI). FireANTs is distributed under the FireANTs License
19
+ # v1.0 and must be cited — see the NOTICE file in this directory for the license, copyright and
20
+ # bibliography that ship with this app.
21
+
22
+ """FireANTs registration as a self-contained KonfAI model (shared by the FireANTs presets).
23
+
24
+ Same idiomatic ``add_module`` graph and the same output contract as the ConvexAdam preset
25
+ (``MovedImage`` + ``DisplacementField`` on the FIXED grid, split by two ``ChannelSelect``), so the
26
+ orchestrator / app.json / ensemble / uncertainty are unchanged. The engine chains FireANTs' own
27
+ composable stages (GPU, Riemannian Adam), each seeding the next like ANTs' ``-t`` stages:
28
+
29
+ Rigid (MI, centre-of-mass init) -> Affine (MI, seeded by the rigid) -> deformable
30
+
31
+ The deformable stage is selected by ``deformable_method`` — the ONE knob that specialises this shared
32
+ Model.py into the different presets (exactly as ConvexAdam's shared Model.py is specialised by
33
+ ``stages``):
34
+
35
+ "syn" symmetric diffeomorphic SyN (CC) — invertible, higher quality, averages cleanly for ensembling
36
+ "greedy" greedy diffeomorphic (CC) — one-directional, faster / lower VRAM
37
+ "none" linear only — Rigid+Affine, no deformable (the FireANTs_Affine preset)
38
+
39
+ Masks: the optional Fixed/Moving masks restrict the metric to a region. FireANTs implements this by
40
+ carrying the mask as the last image channel and prefixing the metric with ``masked_``; a mask is only
41
+ honoured when it actually restricts (some voxels in, some out), so the common mask-free path is
42
+ unchanged (an absent optional mask arrives as a whole-image default and is treated as no mask).
43
+
44
+ The deformable stages produce the single TOTAL displacement field on the fixed grid (the linear
45
+ pre-align is baked in via ``init_affine``, ANTs convention); ``none`` uses the affine matrix directly.
46
+ ``MovedImage`` and the emitted ``DisplacementField`` are rebuilt from that transform with SimpleITK —
47
+ the same output path as the ConvexAdam engine — so all presets/engines are interchangeable in an
48
+ ensemble. FireANTs' output-transform writer only serialises to a file, so the deformable field is
49
+ round-tripped through a temporary NIfTI (no FireANTs internals are reimplemented here).
50
+
51
+ NOTE: do NOT add ``from __future__ import annotations`` — KonfAI's config engine relies on
52
+ runtime-evaluated annotations (``get_origin``); PEP 563 stringized annotations break binding.
53
+ """
54
+
55
+ import contextlib
56
+ import json
57
+ import os
58
+ import tempfile
59
+ from dataclasses import dataclass
60
+ from pathlib import Path
61
+ from typing import Annotated, Literal
62
+
63
+ import numpy as np
64
+ import SimpleITK as sitk
65
+ import torch
66
+ from konfai.metric.measure import IMPACTReg
67
+ from konfai.network import network
68
+ from konfai.utils.config import Choices, Range
69
+ from konfai.utils.dataset import Attribute, data_to_image, image_to_data
70
+
71
+ DIM = 3
72
+
73
+ # Feature-model registry (models.json): the available IMPACT feature models, fetched from HF (NOT bundled).
74
+ # Only consulted by the "impact" deformable metric; ``KONFAI_IMPACT_MODELS_REGISTRY`` (a local path) wins
75
+ # for dev/offline. Mirrors the ConvexAdam preset so the same 30-model catalogue and picker are shared.
76
+ _IMPACT_MODELS_REGISTRY = "VBoussot/impact-torchscript-models:models.json"
77
+
78
+ _DISTANCES: dict[str, type[torch.nn.Module]] = {"L1": torch.nn.L1Loss, "L2": torch.nn.MSELoss}
79
+
80
+
81
+ def registry_choices() -> list[str]:
82
+ """The per-model ``ref`` picker's values — model refs (``repo:path``) from the feature-model registry."""
83
+ repo = _IMPACT_MODELS_REGISTRY.split(":", 1)[0]
84
+ return [f"{repo}:{key}" for key in load_models_registry()]
85
+
86
+
87
+ def load_models_registry(ref: str = _IMPACT_MODELS_REGISTRY) -> dict:
88
+ """Load ``models.json`` (available feature models). ``KONFAI_IMPACT_MODELS_REGISTRY`` (local path) wins
89
+ for dev/offline; otherwise ``ref`` is a ``repo:file`` Hugging Face reference (fetched, not bundled)."""
90
+ from huggingface_hub import hf_hub_download
91
+
92
+ local = os.environ.get("KONFAI_IMPACT_MODELS_REGISTRY", "")
93
+ if local:
94
+ path = Path(local)
95
+ elif ":" in ref:
96
+ repo, filename = ref.split(":", 1)
97
+ path = Path(hf_hub_download(repo_id=repo, filename=filename, repo_type="model")) # nosec B615
98
+ else:
99
+ raise ValueError(
100
+ f"models_registry '{ref}' must be a 'repo:file' Hugging Face reference — or set "
101
+ "KONFAI_IMPACT_MODELS_REGISTRY to a local file for offline use."
102
+ )
103
+ return json.loads(path.read_text(encoding="utf-8"))
104
+
105
+
106
+ def _sorted_specs(mapping: dict) -> list:
107
+ """A dict keyed by string indices ('0','1',...) -> its values in numeric order."""
108
+ return [mapping[k] for k in sorted(mapping, key=lambda key: int(key))]
109
+
110
+
111
+ @dataclass
112
+ class ModelSpec:
113
+ """One IMPACT feature model in the deformable metric (several are fused). ``ref`` picks the model; the
114
+ rest are its per-model knobs — the same as the ConvexAdam / elastix ``ModelSpec`` except ``voxel_size``
115
+ (an itk-impact resampling knob) has no meaning for FireANTs' geometry-free torch ``custom_loss`` and is
116
+ intentionally absent."""
117
+
118
+ ref: Annotated[str, Choices(registry_choices)]
119
+ layers_mask: str = "01" # per-layer bitmask, one char per model layer ('1' = use, '0' = skip), like elastix
120
+ layers_weight: float = 1.0 # this model's weight in the multi-model fusion
121
+ pca: Annotated[int, Range(0, 100)] = 0 # keep the top-K principal components of the features (0 = no PCA)
122
+ distance: Literal["L1", "L2"] = "L1"
123
+
124
+
125
+ @contextlib.contextmanager
126
+ def _no_texpr_fuser():
127
+ """Disable the TensorExpr JIT fuser while IMPACT's TorchScript feature model runs under autograd.
128
+
129
+ The IMPACT feature models are TorchScript; run under FireANTs' gradient optimisation the TensorExpr
130
+ fuser trips on shape ops (``aten::size`` INTERNAL ASSERT). Scoped and restored so no other torch/JIT
131
+ user is affected; the modern profiling executor stays on (this is NOT the legacy executor).
132
+ """
133
+ torch._C._jit_set_texpr_fuser_enabled(False)
134
+ try:
135
+ yield
136
+ finally:
137
+ torch._C._jit_set_texpr_fuser_enabled(True)
138
+
139
+
140
+ class _ImpactCore(IMPACTReg):
141
+ """One IMPACT feature model, exposed as a FireANTs ``forward(moved, fixed)``.
142
+
143
+ Reuses ``IMPACTReg._compute`` / ``preprocessing`` verbatim — the stats-normalised feature extraction
144
+ (the model wants per-image ``[min, mean, max, std]``) and the per-layer weighted distance — so the
145
+ metric is exactly KonfAI's, not a re-derivation. Only KonfAI's config-binding ``__init__`` and its
146
+ ``Attribute``-based geometry are replaced: FireANTs passes raw tensors at the current pyramid scale, so
147
+ the intensity statistics are computed from those tensors directly. ``pca`` (absent from KonfAI's torch
148
+ ``IMPACTReg``) is added here as a per-layer feature-space reduction matching itk-impact.
149
+ """
150
+
151
+ def __init__(self, ref: str, in_channels: int, weights: list[float], distance: str, pca: int) -> None:
152
+ from huggingface_hub import hf_hub_download
153
+
154
+ torch.nn.Module.__init__(self) # bypass IMPACTReg.__init__ (KONFAI_CONFIG_PATH / apply_config binding)
155
+ self.name = "Reg"
156
+ self.in_channels = int(in_channels)
157
+ self.weights = [float(w) for w in weights]
158
+ self.nb_layer = len(self.weights)
159
+ self.loss = _DISTANCES[distance]()
160
+ self.pca = int(pca) # PCA lives in KonfAI's IMPACTReg._compute (same behaviour as itk-impact)
161
+ self.dim = DIM
162
+ self.shape = None # score the whole (downsampled) tensor — no ModelPatch tiling
163
+ if ":" in ref: # a "repo:path" HF reference; otherwise a local model file
164
+ repo, filename = ref.split(":", 1)
165
+ self.model_path = hf_hub_download(repo, filename, repo_type="model") # nosec B615
166
+ else:
167
+ self.model_path = ref
168
+ self.model = None # lazy-loaded on the first forward, like IMPACTReg
169
+
170
+ @staticmethod
171
+ def _stats(tensor: torch.Tensor) -> dict:
172
+ detached = tensor.detach()
173
+ return {
174
+ "ImageMin": float(detached.min()),
175
+ "ImageMean": float(detached.mean()),
176
+ "ImageMax": float(detached.max()),
177
+ "ImageStd": float(detached.std()),
178
+ }
179
+
180
+ def forward(self, moved: torch.Tensor, fixed: torch.Tensor) -> torch.Tensor: # type: ignore[override]
181
+ if self.model is None:
182
+ self.model = torch.jit.load(self.model_path) # nosec B614
183
+ self.model.to(moved.device).eval()
184
+ with _no_texpr_fuser():
185
+ loss, true_nb = self._compute(moved, [self._stats(moved)], fixed, [self._stats(fixed)], None)
186
+ return loss / max(true_nb, 1)
187
+
188
+
189
+ class ImpactFeatureLoss(torch.nn.Module):
190
+ """FireANTs ``custom_loss`` = the KonfAI IMPACT metric fused over several feature models.
191
+
192
+ ``forward(moved, fixed)`` sums each model's ``layers_weight * IMPACT(model)``. A model's per-layer
193
+ weights come from its ``layers_mask`` bitmask; its input channel count is read from the registry
194
+ (``models.json`` ``numberofchannels``) so it never has to be configured by hand.
195
+ """
196
+
197
+ def __init__(self, specs: list["ModelSpec"]) -> None:
198
+ super().__init__()
199
+ registry = load_models_registry()
200
+ self._cores = torch.nn.ModuleList()
201
+ self._model_weights: list[float] = []
202
+ for spec in specs:
203
+ in_channels = int(registry.get(spec.ref.split(":", 1)[-1], {}).get("numberofchannels", 1))
204
+ weights = [1.0 if char == "1" else 0.0 for char in spec.layers_mask]
205
+ self._cores.append(_ImpactCore(spec.ref, in_channels, weights, spec.distance, spec.pca))
206
+ self._model_weights.append(float(spec.layers_weight))
207
+
208
+ def forward(self, moved: torch.Tensor, fixed: torch.Tensor) -> torch.Tensor:
209
+ total: torch.Tensor | None = None
210
+ for weight, core in zip(self._model_weights, self._cores, strict=True):
211
+ term = weight * core(moved, fixed)
212
+ total = term if total is None else total + term
213
+ return total
214
+
215
+
216
+ class FireANTsEngine:
217
+ """Register a fixed/moving pair with FireANTs (Rigid -> Affine -> [SyN | Greedy | none]); return
218
+ (moved, dvf) on the fixed grid.
219
+
220
+ ``fireants`` is imported lazily inside :meth:`register` so this module can be imported for config
221
+ /signature introspection (SlicerImpactReg reads the tuning knobs off the ``RegistrationNet``
222
+ annotations) on a machine without a GPU or without FireANTs installed.
223
+ """
224
+
225
+ def __init__(
226
+ self,
227
+ scales: list[int],
228
+ affine_iterations: list[int],
229
+ deformable_iterations: list[int],
230
+ cc_kernel: int,
231
+ affine_metric: str,
232
+ affine_lr: float,
233
+ deformable_method: str,
234
+ deformable_metric: str,
235
+ deformable_lr: float,
236
+ integrator_n: int,
237
+ smooth_warp_sigma: float,
238
+ smooth_grad_sigma: float,
239
+ seed: int,
240
+ impact_specs: list["ModelSpec"],
241
+ ) -> None:
242
+ self._scales = [int(s) for s in scales]
243
+ self._affine_iterations = [int(i) for i in affine_iterations]
244
+ self._deformable_iterations = [int(i) for i in deformable_iterations]
245
+ self._cc_kernel = int(cc_kernel)
246
+ self._affine_metric = affine_metric
247
+ self._affine_lr = float(affine_lr)
248
+ self._deformable_method = deformable_method
249
+ self._deformable_metric = deformable_metric
250
+ self._deformable_lr = float(deformable_lr)
251
+ self._integrator_n = int(integrator_n)
252
+ self._smooth_warp_sigma = float(smooth_warp_sigma)
253
+ self._smooth_grad_sigma = float(smooth_grad_sigma)
254
+ self._seed = int(seed)
255
+ # IMPACT deformable metric (only used when deformable_metric == "impact"): KonfAI IMPACT feature
256
+ # models drive the SyN/greedy stage instead of the analytic CC/MI/MSE.
257
+ self._impact_specs = impact_specs
258
+
259
+ @staticmethod
260
+ def _is_partial_mask(mask: "sitk.Image | None") -> bool:
261
+ """True only for a mask that actually restricts the region — some voxels in, some out. An absent
262
+ optional mask arrives as a whole-image (all-ones) default and an all-zero mask is degenerate; both
263
+ are treated as no mask so the plain (non-masked) metric path is used."""
264
+ if mask is None:
265
+ return False
266
+ arr = sitk.GetArrayViewFromImage(mask)
267
+ return bool((arr > 0).any()) and bool((arr == 0).any())
268
+
269
+ @staticmethod
270
+ def _affine_to_sitk(affine_matrix: "torch.Tensor") -> sitk.AffineTransform:
271
+ """FireANTs' physical (LPS) linear matrix -> SimpleITK AffineTransform (fixed -> moving points),
272
+ the same convention FireANTs writes into an ANTs ``0GenericAffine.mat``."""
273
+ matrix = affine_matrix.float().cpu().numpy()[0]
274
+ affine = sitk.AffineTransform(DIM)
275
+ affine.SetMatrix(matrix[:DIM, :DIM].flatten().astype(np.float64))
276
+ affine.SetTranslation(matrix[:DIM, DIM].astype(np.float64))
277
+ return affine
278
+
279
+ def _total_field_transform(self, reg) -> sitk.Transform:
280
+ """Optimise a deformable stage and return its TOTAL displacement (affine baked in) as a
281
+ SimpleITK ``DisplacementFieldTransform`` on the fixed grid.
282
+
283
+ FireANTs serialises the total field (ANTs convention, fixed grid) only to a file, so it is
284
+ round-tripped through a temporary NIfTI — its public API, no internals reimplemented."""
285
+ reg.optimize()
286
+ with tempfile.TemporaryDirectory() as tmp:
287
+ warp_path = os.path.join(tmp, "total_warp.nii.gz")
288
+ reg.save_as_ants_transforms(warp_path)
289
+ total_field = sitk.ReadImage(warp_path, sitk.sitkVectorFloat64)
290
+ return sitk.DisplacementFieldTransform(total_field) # consumes total_field
291
+
292
+ def register(
293
+ self,
294
+ fixed: sitk.Image,
295
+ moving: sitk.Image,
296
+ device_index: int,
297
+ fixed_mask: sitk.Image | None = None,
298
+ moving_mask: sitk.Image | None = None,
299
+ ) -> tuple[np.ndarray, np.ndarray]:
300
+ """Register ``moving`` onto ``fixed``; return (moved, dvf) as channel-first arrays on the fixed grid."""
301
+ from fireants.io import BatchedImages, Image
302
+ from fireants.io.imagemask import apply_mask_to_image, generate_image_mask_allones
303
+ from fireants.registration.affine import AffineRegistration
304
+ from fireants.registration.rigid import RigidRegistration
305
+
306
+ torch.manual_seed(self._seed)
307
+ device = f"cuda:{device_index}" if device_index >= 0 else "cpu"
308
+ # FireANTs' Image ctor accepts a SimpleITK image directly, so the fixed/moving cross into
309
+ # FireANTs in-memory (no file load) with their geometry preserved.
310
+ fixed_img = Image(fixed, device=device)
311
+ moving_img = Image(moving, device=device)
312
+
313
+ # Masked metric only when a mask genuinely restricts the region. FireANTs' masked mode wants the
314
+ # mask as the last channel of BOTH images (all-ones where one side has none) and a ``masked_``
315
+ # metric prefix; the plain path is untouched when no real mask is present.
316
+ use_fixed_mask = self._is_partial_mask(fixed_mask)
317
+ use_moving_mask = self._is_partial_mask(moving_mask)
318
+ masked = use_fixed_mask or use_moving_mask
319
+ if masked:
320
+ fmask = Image(fixed_mask, device=device) if use_fixed_mask else generate_image_mask_allones(fixed_img)
321
+ mmask = Image(moving_mask, device=device) if use_moving_mask else generate_image_mask_allones(moving_img)
322
+ fixed_img = apply_mask_to_image(fixed_img, fmask)
323
+ moving_img = apply_mask_to_image(moving_img, mmask)
324
+
325
+ bf = BatchedImages([fixed_img])
326
+ bm = BatchedImages([moving_img])
327
+ affine_loss = f"masked_{self._affine_metric}" if masked else self._affine_metric
328
+ deformable_loss = f"masked_{self._deformable_metric}" if masked else self._deformable_metric
329
+
330
+ # Linear: Rigid(MI, COM init) -> Affine(MI, seeded by the rigid), mirroring ANTs. The affine
331
+ # seeds the deformable stage (or is the whole transform when deformable_method == "none").
332
+ rigid = RigidRegistration(
333
+ scales=self._scales,
334
+ iterations=self._affine_iterations,
335
+ fixed_images=bf,
336
+ moving_images=bm,
337
+ loss_type=affine_loss,
338
+ optimizer="Adam",
339
+ optimizer_lr=self._affine_lr,
340
+ cc_kernel_size=self._cc_kernel,
341
+ init_translation="cof",
342
+ )
343
+ rigid.optimize()
344
+ rigid_matrix = rigid.get_rigid_matrix().detach()
345
+
346
+ affine = AffineRegistration(
347
+ scales=self._scales,
348
+ iterations=self._affine_iterations,
349
+ fixed_images=bf,
350
+ moving_images=bm,
351
+ loss_type=affine_loss,
352
+ optimizer="Adam",
353
+ optimizer_lr=self._affine_lr,
354
+ cc_kernel_size=self._cc_kernel,
355
+ init_rigid=rigid_matrix,
356
+ )
357
+ affine.optimize()
358
+ affine_matrix = affine.get_affine_matrix().detach()
359
+
360
+ # Deformable stage (or none). SyN and Greedy share the same constructor surface; both warm-start
361
+ # from the affine so their TOTAL transform already bakes in the linear pre-align.
362
+ if self._deformable_method == "none":
363
+ transform: sitk.Transform = self._affine_to_sitk(affine_matrix)
364
+ else:
365
+ if self._deformable_method == "syn":
366
+ from fireants.registration.syn import SyNRegistration as Deformable
367
+ elif self._deformable_method == "greedy":
368
+ from fireants.registration.greedy import GreedyRegistration as Deformable
369
+ else:
370
+ raise ValueError(
371
+ f"Unknown deformable_method '{self._deformable_method}' (expected 'syn', 'greedy' or 'none')."
372
+ )
373
+ # "impact" swaps the analytic metric for a KonfAI IMPACT feature loss on the deformable stage
374
+ # (the linear pre-align keeps its own affine_metric); masks do not restrict the IMPACT metric.
375
+ if self._deformable_metric == "impact":
376
+ loss_type: str = "custom"
377
+ custom_loss: torch.nn.Module | None = ImpactFeatureLoss(self._impact_specs)
378
+ else:
379
+ loss_type, custom_loss = deformable_loss, None
380
+ reg = Deformable(
381
+ scales=self._scales,
382
+ iterations=self._deformable_iterations,
383
+ fixed_images=bf,
384
+ moving_images=bm,
385
+ loss_type=loss_type,
386
+ custom_loss=custom_loss,
387
+ cc_kernel_size=self._cc_kernel,
388
+ deformation_type="compositive",
389
+ integrator_n=self._integrator_n,
390
+ smooth_warp_sigma=self._smooth_warp_sigma,
391
+ smooth_grad_sigma=self._smooth_grad_sigma,
392
+ optimizer="Adam",
393
+ optimizer_lr=self._deformable_lr,
394
+ init_affine=affine_matrix,
395
+ )
396
+ transform = self._total_field_transform(reg)
397
+
398
+ if torch.cuda.is_available():
399
+ torch.cuda.synchronize()
400
+
401
+ # Rebuild moved + DVF from the single transform on the fixed grid — the ConvexAdam output path,
402
+ # so every FireANTs preset emits identical-shaped results.
403
+ moved = sitk.Resample(moving, fixed, transform, sitk.sitkLinear, 0.0, moving.GetPixelID())
404
+ dvf = sitk.TransformToDisplacementField(
405
+ transform,
406
+ sitk.sitkVectorFloat64,
407
+ fixed.GetSize(),
408
+ fixed.GetOrigin(),
409
+ fixed.GetSpacing(),
410
+ fixed.GetDirection(),
411
+ )
412
+ moved_np, _ = image_to_data(moved)
413
+ dvf_np, _ = image_to_data(dvf)
414
+ return moved_np, dvf_np
415
+
416
+
417
+ class FireANTsRegistration(torch.nn.Module):
418
+ """Graph module: (fixed, moving) tensors + their geometry -> moved image + DVF on the fixed grid.
419
+
420
+ ``accepts_attributes = True`` opts this module into receiving the per-branch ``Attribute`` list
421
+ alongside the tensors (same convention as the ConvexAdam / elastix engines); registration needs the
422
+ physical geometry, and the mask branches restrict the metric.
423
+ """
424
+
425
+ accepts_attributes = True
426
+
427
+ def __init__(self, engine: FireANTsEngine) -> None:
428
+ super().__init__()
429
+ self._engine = engine
430
+
431
+ def forward(
432
+ self,
433
+ fixed: torch.Tensor,
434
+ moving: torch.Tensor,
435
+ fixed_mask: torch.Tensor,
436
+ moving_mask: torch.Tensor,
437
+ attributes: list[list[Attribute]],
438
+ ) -> torch.Tensor:
439
+ # attributes = [fixed, moving, fixed_mask, moving_mask] branch attrs; each a list[Attribute] over
440
+ # the batch. Returns, per sample, the moved image (1 channel) channel-stacked with the
441
+ # displacement field (DIM channels); downstream ChannelSelect modules split them. A whole-image
442
+ # mask (the default when none is supplied) restricts nothing.
443
+ fixed_attrs, moving_attrs, fmask_attrs, mmask_attrs = attributes
444
+ device_index = fixed.device.index if fixed.device.type == "cuda" else -1
445
+ combined = []
446
+ # FireANTs runs a gradient-based instance optimisation (Riemannian Adam over the warp); the
447
+ # predictor calls forward under torch.inference_mode(), which forbids autograd. The image tensors
448
+ # have already crossed to numpy/SimpleITK here, so re-enable grad for the optimisation.
449
+ with torch.inference_mode(False), torch.enable_grad():
450
+ for b in range(fixed.shape[0]):
451
+ fixed_img = data_to_image(fixed[b].detach().cpu().numpy(), fixed_attrs[b])
452
+ moving_img = data_to_image(moving[b].detach().cpu().numpy(), moving_attrs[b])
453
+ fixed_mask_img = data_to_image(fixed_mask[b].detach().cpu().numpy(), fmask_attrs[b])
454
+ moving_mask_img = data_to_image(moving_mask[b].detach().cpu().numpy(), mmask_attrs[b])
455
+ moved_np, dvf_np = self._engine.register(
456
+ fixed_img, moving_img, device_index, fixed_mask_img, moving_mask_img
457
+ )
458
+ combined.append(torch.from_numpy(np.concatenate([moved_np, dvf_np], axis=0)))
459
+ return torch.stack(combined, dim=0).to(fixed.device)
460
+
461
+
462
+ class ChannelSelect(torch.nn.Module):
463
+ """Select a channel slice ``[start:stop]`` (splits the registration output into moved / DVF)."""
464
+
465
+ def __init__(self, start: int, stop: int) -> None:
466
+ super().__init__()
467
+ self._start = start
468
+ self._stop = stop
469
+
470
+ def forward(self, tensor: torch.Tensor) -> torch.Tensor:
471
+ return tensor[:, self._start : self._stop]
472
+
473
+
474
+ class RegistrationNet(network.Network):
475
+ """Pairwise FireANTs registration as an ``add_module`` graph (fixed = branch 0, moving = branch 1,
476
+ fixed mask = 2, moving mask = 3; masks restrict the metric, whole-image = no restriction).
477
+
478
+ Outputs on the fixed grid: ``MovedImage`` (moving resampled onto fixed) and ``DisplacementField``
479
+ (the DIM-component displacement field, in mm). Geometry is attached by the predictor via
480
+ ``same_as_group: Volume_0:Fixed``. The knobs below are read straight from these annotations by the
481
+ UI: ``Annotated[.., Range]`` gives numeric spin bounds; ``Literal`` a dropdown. ``deformable_method``
482
+ is the knob that specialises this shared model into each FireANTs preset.
483
+ """
484
+
485
+ def __init__(
486
+ self,
487
+ optimizer: network.OptimizerLoader = network.OptimizerLoader(),
488
+ schedulers: dict[str, network.LRSchedulersLoader] = {
489
+ "default:ReduceLROnPlateau": network.LRSchedulersLoader(0)
490
+ },
491
+ outputs_criterions: dict[str, network.TargetCriterionsLoader] = {"default": network.TargetCriterionsLoader()},
492
+ scales: list[int] = [4, 2, 1],
493
+ affine_iterations: list[int] = [200, 100, 50],
494
+ deformable_iterations: list[int] = [200, 100, 50],
495
+ cc_kernel: Annotated[int, Range(1, 21)] = 5,
496
+ affine_metric: Literal["mi", "cc", "mse"] = "mi",
497
+ affine_lr: Annotated[float, Range(0.0, 10.0)] = 0.003,
498
+ deformable_method: Literal["none", "syn", "greedy"] = "syn",
499
+ deformable_metric: Literal["cc", "mi", "mse", "impact"] = "cc",
500
+ deformable_lr: Annotated[float, Range(0.0, 10.0)] = 0.25,
501
+ integrator_n: Annotated[int, Range(1, 100)] = 10,
502
+ smooth_warp_sigma: Annotated[float, Range(0.0, 100.0)] = 0.5,
503
+ smooth_grad_sigma: Annotated[float, Range(0.0, 100.0)] = 1.0,
504
+ seed: int = 42,
505
+ models: dict[str, ModelSpec] = {},
506
+ ) -> None:
507
+ super().__init__(
508
+ in_channels=1,
509
+ optimizer=optimizer,
510
+ schedulers=schedulers,
511
+ outputs_criterions=outputs_criterions,
512
+ dim=3,
513
+ )
514
+ engine = FireANTsEngine(
515
+ scales,
516
+ affine_iterations,
517
+ deformable_iterations,
518
+ cc_kernel,
519
+ affine_metric,
520
+ affine_lr,
521
+ deformable_method,
522
+ deformable_metric,
523
+ deformable_lr,
524
+ integrator_n,
525
+ smooth_warp_sigma,
526
+ smooth_grad_sigma,
527
+ seed,
528
+ _sorted_specs(models),
529
+ )
530
+ self.add_module(
531
+ "Registration", FireANTsRegistration(engine), in_branch=[0, 1, 2, 3], out_branch=["registration"]
532
+ )
533
+ self.add_module("MovedImage", ChannelSelect(0, 1), in_branch=["registration"], out_branch=["moved"])
534
+ self.add_module("DisplacementField", ChannelSelect(1, 4), in_branch=["registration"], out_branch=["dvf"])
FireANTs_Affine/NOTICE ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FireANTs_SyN — third-party attribution
2
+ ======================================
3
+
4
+ This KonfAI app drives FireANTs, an external registration library. FireANTs is NOT
5
+ redistributed here as source: the app depends on the official `fireants` wheel
6
+ (https://pypi.org/project/fireants/), which is installed at resolve time (see
7
+ requirements.txt) and called through its public Python API only. No FireANTs source
8
+ code (functions, classes, or modules) is copied into this app.
9
+
10
+ FireANTs is distributed under the **FireANTs License, Version 1.0 (July 2025)**, a
11
+ custom license modified from Apache 2.0. Its redistribution clause requires that, when
12
+ FireANTs is incorporated as a dependency in other projects, all license terms —
13
+ including attribution and the bibliography below — be maintained.
14
+
15
+ Project : FireANTs
16
+ Source : https://github.com/rohitrango/FireANTs
17
+ License : https://github.com/rohitrango/FireANTs/blob/main/LICENSE
18
+ Copyright (c) 2026 Rohit Jena. All rights reserved.
19
+
20
+ Bibliography (as required by the FireANTs License — cite if you use this app)
21
+ ----------------------------------------------------------------------------
22
+
23
+ @article{jena2024fireants,
24
+ title={FireANTs: Adaptive Riemannian Optimization for Multi-Scale Diffeomorphic Registration},
25
+ author={Jena, Rohit and Chaudhari, Pratik and Gee, James C},
26
+ journal={Nature Communications},
27
+ year={2024}
28
+ }
29
+
30
+ @inproceedings{jena2025scalable,
31
+ title={A Scalable Distributed Framework for Multimodal {GigaVoxel} Image Registration},
32
+ author={Jena, Rohit and Zope, Vedant and Chaudhari, Pratik and Gee, James C},
33
+ booktitle={The Fourteenth International Conference on Learning Representations},
34
+ year={2026},
35
+ url={https://openreview.net/forum?id=8dLexnao2h}
36
+ }
37
+
38
+ The wrapper code in this directory (Model.py, the KonfAI app configuration) is original
39
+ work © 2025 Valentin Boussot, licensed under Apache-2.0, and is a separate work that
40
+ merely links to the FireANTs interfaces.
FireANTs_Affine/Prediction.yml ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Predictor:
2
+ Model:
3
+ classpath: Model:RegistrationNet
4
+ RegistrationNet:
5
+ scales:
6
+ - 4
7
+ - 2
8
+ - 1
9
+ affine_iterations:
10
+ - 200
11
+ - 100
12
+ - 50
13
+ deformable_iterations:
14
+ - 200
15
+ - 100
16
+ - 50
17
+ cc_kernel: 5
18
+ affine_metric: mi
19
+ affine_lr: 0.003
20
+ deformable_method: none
21
+ deformable_metric: cc
22
+ deformable_lr: 0.25
23
+ integrator_n: 10
24
+ smooth_warp_sigma: 0.5
25
+ smooth_grad_sigma: 1.0
26
+ seed: 42
27
+ outputs_criterions: None
28
+ Dataset:
29
+ groups_src:
30
+ Volume_0:
31
+ groups_dest:
32
+ Fixed:
33
+ transforms:
34
+ TensorCast:
35
+ dtype: float32
36
+ inverse: false
37
+ patch_transforms: None
38
+ is_input: true
39
+ Volume_1:
40
+ groups_dest:
41
+ Moving:
42
+ transforms:
43
+ TensorCast:
44
+ dtype: float32
45
+ inverse: false
46
+ patch_transforms: None
47
+ is_input: true
48
+ Volume_2:
49
+ groups_dest:
50
+ FixedMask:
51
+ transforms:
52
+ TensorCast:
53
+ dtype: float32
54
+ inverse: false
55
+ patch_transforms: None
56
+ is_input: true
57
+ Volume_3:
58
+ groups_dest:
59
+ MovingMask:
60
+ transforms:
61
+ TensorCast:
62
+ dtype: float32
63
+ inverse: false
64
+ patch_transforms: None
65
+ is_input: true
66
+ augmentations:
67
+ DataAugmentation_0:
68
+ data_augmentations:
69
+ Flip:
70
+ f_prob:
71
+ - 0
72
+ - 0.5
73
+ - 0.5
74
+ vector_field: true
75
+ prob: 1
76
+ nb: 2
77
+ Patch:
78
+ patch_size: None
79
+ overlap: None
80
+ mask: None
81
+ pad_value: None
82
+ extend_slice: 0
83
+ subset: None
84
+ filter: None
85
+ dataset_filenames:
86
+ - ./Dataset/:mha
87
+ use_cache: false
88
+ batch_size: 1
89
+ num_workers: None
90
+ pin_memory: false
91
+ prefetch_factor: None
92
+ persistent_workers: None
93
+ outputs_dataset:
94
+ MovedImage:
95
+ OutputDataset:
96
+ name_class: OutSameAsGroupDataset
97
+ before_reduction_transforms: None
98
+ after_reduction_transforms: None
99
+ final_transforms:
100
+ TensorCast:
101
+ dtype: float32
102
+ inverse: false
103
+ dataset_filename: Moved:mha
104
+ group: Moved
105
+ same_as_group: Volume_0:Fixed
106
+ patch_combine: None
107
+ inverse_transform: false
108
+ reduction: Mean
109
+ Mean: {}
110
+ DisplacementField:
111
+ OutputDataset:
112
+ name_class: OutSameAsGroupDataset
113
+ before_reduction_transforms: None
114
+ after_reduction_transforms: None
115
+ final_transforms:
116
+ TensorCast:
117
+ dtype: float32
118
+ inverse: false
119
+ dataset_filename: DVF:mha
120
+ group: DVF
121
+ same_as_group: Volume_0:Fixed
122
+ patch_combine: None
123
+ inverse_transform: false
124
+ reduction: Mean
125
+ Mean: {}
126
+ train_name: ImpactReg-FireANTs-Affine
127
+ manual_seed: 42
128
+ gpu_checkpoints: None
129
+ images_log: None
130
+ combine: Mean
131
+ autocast: false
132
+ data_log: None
FireANTs_Affine/Uncertainty.yml ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Evaluator:
2
+ metrics:
3
+ Uncertainty:
4
+ targets_criterions:
5
+ None:
6
+ criterions_loader:
7
+ Mean:
8
+ name: Uncertainty
9
+ Dataset:
10
+ groups_src:
11
+ Volume_0:
12
+ groups_dest:
13
+ Uncertainty:
14
+ transforms:
15
+ Norm: {}
16
+ StandardDeviation: {}
17
+ Save:
18
+ dataset: ./Uncertainties/ImpactReg/Output:mha
19
+ group: None
20
+ subset: None
21
+ dataset_filenames:
22
+ - ./Dataset:mha
23
+ validation: None
24
+ train_name: ImpactReg
FireANTs_Affine/app.json ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "display_name": "FireANTs (Affine)",
3
+ "short_description": "Rigid + Affine linear alignment on GPU (FireANTs), no deformable stage.",
4
+ "description": "GPU linear registration with FireANTs: a Rigid (MI, centre-of-mass init) then Affine (MI) alignment optimised with Riemannian Adam, with no deformable stage. Produces the moved image and the affine displacement field on the fixed grid \u2014 use it on its own, or as a linear pre-align feeding a deformable preset. FireANTs is an external dependency under the FireANTs License v1.0 (see NOTICE).",
5
+ "task": "registration",
6
+ "tta": 0,
7
+ "mc_dropout": 0,
8
+ "models": [
9
+ "model.pt"
10
+ ],
11
+ "inputs": {
12
+ "Fixed": {
13
+ "display_name": "Fixed image",
14
+ "volume_type": "VOLUME",
15
+ "required": true
16
+ },
17
+ "Moving": {
18
+ "display_name": "Moving image",
19
+ "volume_type": "VOLUME",
20
+ "required": true
21
+ },
22
+ "FixedMask": {
23
+ "display_name": "Fixed mask (optional)",
24
+ "volume_type": "SEGMENTATION",
25
+ "required": false,
26
+ "default": "ones"
27
+ },
28
+ "MovingMask": {
29
+ "display_name": "Moving mask (optional)",
30
+ "volume_type": "SEGMENTATION",
31
+ "required": false,
32
+ "default": "ones"
33
+ }
34
+ },
35
+ "outputs": {
36
+ "MovedImage": {
37
+ "display_name": "Moved image",
38
+ "volume_type": "VOLUME",
39
+ "required": true
40
+ },
41
+ "DisplacementField": {
42
+ "display_name": "Displacement field",
43
+ "volume_type": "VOLUME",
44
+ "required": false
45
+ }
46
+ },
47
+ "inputs_evaluations": {
48
+ "Image": {
49
+ "Evaluation_with_images.yml": {
50
+ "FixedImage": {
51
+ "display_name": "Fixed image",
52
+ "volume_type": "VOLUME",
53
+ "required": true
54
+ },
55
+ "MovingImage": {
56
+ "display_name": "Moving image",
57
+ "volume_type": "VOLUME",
58
+ "required": true
59
+ },
60
+ "Mask": {
61
+ "display_name": "Evaluation mask",
62
+ "volume_type": "SEGMENTATION",
63
+ "required": false
64
+ }
65
+ }
66
+ },
67
+ "Segmentation": {
68
+ "Evaluation_with_seg.yml": {
69
+ "FixedSeg": {
70
+ "display_name": "Fixed segmentation",
71
+ "volume_type": "SEGMENTATION",
72
+ "required": true
73
+ },
74
+ "MovingSeg": {
75
+ "display_name": "Moving segmentation",
76
+ "volume_type": "SEGMENTATION",
77
+ "required": true
78
+ }
79
+ }
80
+ },
81
+ "Landmarks": {
82
+ "Evaluation_with_fid.yml": {
83
+ "FixedFid": {
84
+ "display_name": "Fixed landmarks",
85
+ "volume_type": "FIDUCIALS",
86
+ "required": true
87
+ },
88
+ "MovingFid": {
89
+ "display_name": "Moving landmarks",
90
+ "volume_type": "FIDUCIALS",
91
+ "required": true
92
+ }
93
+ }
94
+ }
95
+ }
96
+ }
FireANTs_Affine/model.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:de99fbc36331ce674639acc774f52b4a2d0027f2f312d9d28669e831a0c4fd7e
3
+ size 1249
FireANTs_Affine/requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ fireants
FireANTs_IMPACT/Evaluation_with_fid.yml ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Evaluator:
2
+ metrics:
3
+ FixedFid:
4
+ targets_criterions:
5
+ MovingFid:
6
+ criterions_loader:
7
+ TRE: {}
8
+ Dataset:
9
+ groups_src:
10
+ Volume_0:
11
+ groups_dest:
12
+ FixedFid:
13
+ transforms: None
14
+ Reference_0:
15
+ groups_dest:
16
+ MovingFid:
17
+ transforms: None
18
+ subset: None
19
+ dataset_filenames:
20
+ - ./Dataset:mha
21
+ validation: None
22
+ train_name: ImpactReg
FireANTs_IMPACT/Evaluation_with_images.yml ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Evaluator:
2
+ metrics:
3
+ FixedImage:
4
+ targets_criterions:
5
+ MovingImage;Mask:
6
+ criterions_loader:
7
+ MAESaveMap:
8
+ reduction: mean
9
+ dataset: ./Evaluations/ImpactReg/Output:mha
10
+ group: MAE_map
11
+ Dataset:
12
+ groups_src:
13
+ Volume_0:
14
+ groups_dest:
15
+ FixedImage:
16
+ transforms:
17
+ TensorCast:
18
+ dtype: float32
19
+ Reference_0:
20
+ groups_dest:
21
+ MovingImage:
22
+ transforms:
23
+ TensorCast:
24
+ dtype: float32
25
+ Mask_0:
26
+ groups_dest:
27
+ Mask:
28
+ transforms:
29
+ TensorCast:
30
+ dtype: uint8
31
+ subset: None
32
+ dataset_filenames:
33
+ - ./Dataset:mha
34
+ validation: None
35
+ train_name: ImpactReg
FireANTs_IMPACT/Evaluation_with_seg.yml ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Evaluator:
2
+ metrics:
3
+ FixedSeg:
4
+ targets_criterions:
5
+ MovingSeg:
6
+ criterions_loader:
7
+ DiceSaveMap:
8
+ labels: None
9
+ dataset: ./Evaluations/ImpactReg/Output:mha
10
+ group: Seg_MAE_map
11
+ Dataset:
12
+ groups_src:
13
+ Volume_0:
14
+ groups_dest:
15
+ FixedSeg:
16
+ transforms:
17
+ TensorCast:
18
+ dtype: uint8
19
+ Reference_0:
20
+ groups_dest:
21
+ MovingSeg:
22
+ transforms:
23
+ TensorCast:
24
+ dtype: uint8
25
+ subset: None
26
+ dataset_filenames:
27
+ - ./Dataset:mha
28
+ validation: None
29
+ train_name: ImpactReg
FireANTs_IMPACT/Model.py ADDED
@@ -0,0 +1,534 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2025 Valentin Boussot
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+ # SPDX-License-Identifier: Apache-2.0
16
+ #
17
+ # This wrapper does NOT copy any FireANTs source: it only calls the public FireANTs API of the
18
+ # separately-installed ``fireants`` wheel (PyPI). FireANTs is distributed under the FireANTs License
19
+ # v1.0 and must be cited — see the NOTICE file in this directory for the license, copyright and
20
+ # bibliography that ship with this app.
21
+
22
+ """FireANTs registration as a self-contained KonfAI model (shared by the FireANTs presets).
23
+
24
+ Same idiomatic ``add_module`` graph and the same output contract as the ConvexAdam preset
25
+ (``MovedImage`` + ``DisplacementField`` on the FIXED grid, split by two ``ChannelSelect``), so the
26
+ orchestrator / app.json / ensemble / uncertainty are unchanged. The engine chains FireANTs' own
27
+ composable stages (GPU, Riemannian Adam), each seeding the next like ANTs' ``-t`` stages:
28
+
29
+ Rigid (MI, centre-of-mass init) -> Affine (MI, seeded by the rigid) -> deformable
30
+
31
+ The deformable stage is selected by ``deformable_method`` — the ONE knob that specialises this shared
32
+ Model.py into the different presets (exactly as ConvexAdam's shared Model.py is specialised by
33
+ ``stages``):
34
+
35
+ "syn" symmetric diffeomorphic SyN (CC) — invertible, higher quality, averages cleanly for ensembling
36
+ "greedy" greedy diffeomorphic (CC) — one-directional, faster / lower VRAM
37
+ "none" linear only — Rigid+Affine, no deformable (the FireANTs_Affine preset)
38
+
39
+ Masks: the optional Fixed/Moving masks restrict the metric to a region. FireANTs implements this by
40
+ carrying the mask as the last image channel and prefixing the metric with ``masked_``; a mask is only
41
+ honoured when it actually restricts (some voxels in, some out), so the common mask-free path is
42
+ unchanged (an absent optional mask arrives as a whole-image default and is treated as no mask).
43
+
44
+ The deformable stages produce the single TOTAL displacement field on the fixed grid (the linear
45
+ pre-align is baked in via ``init_affine``, ANTs convention); ``none`` uses the affine matrix directly.
46
+ ``MovedImage`` and the emitted ``DisplacementField`` are rebuilt from that transform with SimpleITK —
47
+ the same output path as the ConvexAdam engine — so all presets/engines are interchangeable in an
48
+ ensemble. FireANTs' output-transform writer only serialises to a file, so the deformable field is
49
+ round-tripped through a temporary NIfTI (no FireANTs internals are reimplemented here).
50
+
51
+ NOTE: do NOT add ``from __future__ import annotations`` — KonfAI's config engine relies on
52
+ runtime-evaluated annotations (``get_origin``); PEP 563 stringized annotations break binding.
53
+ """
54
+
55
+ import contextlib
56
+ import json
57
+ import os
58
+ import tempfile
59
+ from dataclasses import dataclass
60
+ from pathlib import Path
61
+ from typing import Annotated, Literal
62
+
63
+ import numpy as np
64
+ import SimpleITK as sitk
65
+ import torch
66
+ from konfai.metric.measure import IMPACTReg
67
+ from konfai.network import network
68
+ from konfai.utils.config import Choices, Range
69
+ from konfai.utils.dataset import Attribute, data_to_image, image_to_data
70
+
71
+ DIM = 3
72
+
73
+ # Feature-model registry (models.json): the available IMPACT feature models, fetched from HF (NOT bundled).
74
+ # Only consulted by the "impact" deformable metric; ``KONFAI_IMPACT_MODELS_REGISTRY`` (a local path) wins
75
+ # for dev/offline. Mirrors the ConvexAdam preset so the same 30-model catalogue and picker are shared.
76
+ _IMPACT_MODELS_REGISTRY = "VBoussot/impact-torchscript-models:models.json"
77
+
78
+ _DISTANCES: dict[str, type[torch.nn.Module]] = {"L1": torch.nn.L1Loss, "L2": torch.nn.MSELoss}
79
+
80
+
81
+ def registry_choices() -> list[str]:
82
+ """The per-model ``ref`` picker's values — model refs (``repo:path``) from the feature-model registry."""
83
+ repo = _IMPACT_MODELS_REGISTRY.split(":", 1)[0]
84
+ return [f"{repo}:{key}" for key in load_models_registry()]
85
+
86
+
87
+ def load_models_registry(ref: str = _IMPACT_MODELS_REGISTRY) -> dict:
88
+ """Load ``models.json`` (available feature models). ``KONFAI_IMPACT_MODELS_REGISTRY`` (local path) wins
89
+ for dev/offline; otherwise ``ref`` is a ``repo:file`` Hugging Face reference (fetched, not bundled)."""
90
+ from huggingface_hub import hf_hub_download
91
+
92
+ local = os.environ.get("KONFAI_IMPACT_MODELS_REGISTRY", "")
93
+ if local:
94
+ path = Path(local)
95
+ elif ":" in ref:
96
+ repo, filename = ref.split(":", 1)
97
+ path = Path(hf_hub_download(repo_id=repo, filename=filename, repo_type="model")) # nosec B615
98
+ else:
99
+ raise ValueError(
100
+ f"models_registry '{ref}' must be a 'repo:file' Hugging Face reference — or set "
101
+ "KONFAI_IMPACT_MODELS_REGISTRY to a local file for offline use."
102
+ )
103
+ return json.loads(path.read_text(encoding="utf-8"))
104
+
105
+
106
+ def _sorted_specs(mapping: dict) -> list:
107
+ """A dict keyed by string indices ('0','1',...) -> its values in numeric order."""
108
+ return [mapping[k] for k in sorted(mapping, key=lambda key: int(key))]
109
+
110
+
111
+ @dataclass
112
+ class ModelSpec:
113
+ """One IMPACT feature model in the deformable metric (several are fused). ``ref`` picks the model; the
114
+ rest are its per-model knobs — the same as the ConvexAdam / elastix ``ModelSpec`` except ``voxel_size``
115
+ (an itk-impact resampling knob) has no meaning for FireANTs' geometry-free torch ``custom_loss`` and is
116
+ intentionally absent."""
117
+
118
+ ref: Annotated[str, Choices(registry_choices)]
119
+ layers_mask: str = "01" # per-layer bitmask, one char per model layer ('1' = use, '0' = skip), like elastix
120
+ layers_weight: float = 1.0 # this model's weight in the multi-model fusion
121
+ pca: Annotated[int, Range(0, 100)] = 0 # keep the top-K principal components of the features (0 = no PCA)
122
+ distance: Literal["L1", "L2"] = "L1"
123
+
124
+
125
+ @contextlib.contextmanager
126
+ def _no_texpr_fuser():
127
+ """Disable the TensorExpr JIT fuser while IMPACT's TorchScript feature model runs under autograd.
128
+
129
+ The IMPACT feature models are TorchScript; run under FireANTs' gradient optimisation the TensorExpr
130
+ fuser trips on shape ops (``aten::size`` INTERNAL ASSERT). Scoped and restored so no other torch/JIT
131
+ user is affected; the modern profiling executor stays on (this is NOT the legacy executor).
132
+ """
133
+ torch._C._jit_set_texpr_fuser_enabled(False)
134
+ try:
135
+ yield
136
+ finally:
137
+ torch._C._jit_set_texpr_fuser_enabled(True)
138
+
139
+
140
+ class _ImpactCore(IMPACTReg):
141
+ """One IMPACT feature model, exposed as a FireANTs ``forward(moved, fixed)``.
142
+
143
+ Reuses ``IMPACTReg._compute`` / ``preprocessing`` verbatim — the stats-normalised feature extraction
144
+ (the model wants per-image ``[min, mean, max, std]``) and the per-layer weighted distance — so the
145
+ metric is exactly KonfAI's, not a re-derivation. Only KonfAI's config-binding ``__init__`` and its
146
+ ``Attribute``-based geometry are replaced: FireANTs passes raw tensors at the current pyramid scale, so
147
+ the intensity statistics are computed from those tensors directly. ``pca`` (absent from KonfAI's torch
148
+ ``IMPACTReg``) is added here as a per-layer feature-space reduction matching itk-impact.
149
+ """
150
+
151
+ def __init__(self, ref: str, in_channels: int, weights: list[float], distance: str, pca: int) -> None:
152
+ from huggingface_hub import hf_hub_download
153
+
154
+ torch.nn.Module.__init__(self) # bypass IMPACTReg.__init__ (KONFAI_CONFIG_PATH / apply_config binding)
155
+ self.name = "Reg"
156
+ self.in_channels = int(in_channels)
157
+ self.weights = [float(w) for w in weights]
158
+ self.nb_layer = len(self.weights)
159
+ self.loss = _DISTANCES[distance]()
160
+ self.pca = int(pca) # PCA lives in KonfAI's IMPACTReg._compute (same behaviour as itk-impact)
161
+ self.dim = DIM
162
+ self.shape = None # score the whole (downsampled) tensor — no ModelPatch tiling
163
+ if ":" in ref: # a "repo:path" HF reference; otherwise a local model file
164
+ repo, filename = ref.split(":", 1)
165
+ self.model_path = hf_hub_download(repo, filename, repo_type="model") # nosec B615
166
+ else:
167
+ self.model_path = ref
168
+ self.model = None # lazy-loaded on the first forward, like IMPACTReg
169
+
170
+ @staticmethod
171
+ def _stats(tensor: torch.Tensor) -> dict:
172
+ detached = tensor.detach()
173
+ return {
174
+ "ImageMin": float(detached.min()),
175
+ "ImageMean": float(detached.mean()),
176
+ "ImageMax": float(detached.max()),
177
+ "ImageStd": float(detached.std()),
178
+ }
179
+
180
+ def forward(self, moved: torch.Tensor, fixed: torch.Tensor) -> torch.Tensor: # type: ignore[override]
181
+ if self.model is None:
182
+ self.model = torch.jit.load(self.model_path) # nosec B614
183
+ self.model.to(moved.device).eval()
184
+ with _no_texpr_fuser():
185
+ loss, true_nb = self._compute(moved, [self._stats(moved)], fixed, [self._stats(fixed)], None)
186
+ return loss / max(true_nb, 1)
187
+
188
+
189
+ class ImpactFeatureLoss(torch.nn.Module):
190
+ """FireANTs ``custom_loss`` = the KonfAI IMPACT metric fused over several feature models.
191
+
192
+ ``forward(moved, fixed)`` sums each model's ``layers_weight * IMPACT(model)``. A model's per-layer
193
+ weights come from its ``layers_mask`` bitmask; its input channel count is read from the registry
194
+ (``models.json`` ``numberofchannels``) so it never has to be configured by hand.
195
+ """
196
+
197
+ def __init__(self, specs: list["ModelSpec"]) -> None:
198
+ super().__init__()
199
+ registry = load_models_registry()
200
+ self._cores = torch.nn.ModuleList()
201
+ self._model_weights: list[float] = []
202
+ for spec in specs:
203
+ in_channels = int(registry.get(spec.ref.split(":", 1)[-1], {}).get("numberofchannels", 1))
204
+ weights = [1.0 if char == "1" else 0.0 for char in spec.layers_mask]
205
+ self._cores.append(_ImpactCore(spec.ref, in_channels, weights, spec.distance, spec.pca))
206
+ self._model_weights.append(float(spec.layers_weight))
207
+
208
+ def forward(self, moved: torch.Tensor, fixed: torch.Tensor) -> torch.Tensor:
209
+ total: torch.Tensor | None = None
210
+ for weight, core in zip(self._model_weights, self._cores, strict=True):
211
+ term = weight * core(moved, fixed)
212
+ total = term if total is None else total + term
213
+ return total
214
+
215
+
216
+ class FireANTsEngine:
217
+ """Register a fixed/moving pair with FireANTs (Rigid -> Affine -> [SyN | Greedy | none]); return
218
+ (moved, dvf) on the fixed grid.
219
+
220
+ ``fireants`` is imported lazily inside :meth:`register` so this module can be imported for config
221
+ /signature introspection (SlicerImpactReg reads the tuning knobs off the ``RegistrationNet``
222
+ annotations) on a machine without a GPU or without FireANTs installed.
223
+ """
224
+
225
+ def __init__(
226
+ self,
227
+ scales: list[int],
228
+ affine_iterations: list[int],
229
+ deformable_iterations: list[int],
230
+ cc_kernel: int,
231
+ affine_metric: str,
232
+ affine_lr: float,
233
+ deformable_method: str,
234
+ deformable_metric: str,
235
+ deformable_lr: float,
236
+ integrator_n: int,
237
+ smooth_warp_sigma: float,
238
+ smooth_grad_sigma: float,
239
+ seed: int,
240
+ impact_specs: list["ModelSpec"],
241
+ ) -> None:
242
+ self._scales = [int(s) for s in scales]
243
+ self._affine_iterations = [int(i) for i in affine_iterations]
244
+ self._deformable_iterations = [int(i) for i in deformable_iterations]
245
+ self._cc_kernel = int(cc_kernel)
246
+ self._affine_metric = affine_metric
247
+ self._affine_lr = float(affine_lr)
248
+ self._deformable_method = deformable_method
249
+ self._deformable_metric = deformable_metric
250
+ self._deformable_lr = float(deformable_lr)
251
+ self._integrator_n = int(integrator_n)
252
+ self._smooth_warp_sigma = float(smooth_warp_sigma)
253
+ self._smooth_grad_sigma = float(smooth_grad_sigma)
254
+ self._seed = int(seed)
255
+ # IMPACT deformable metric (only used when deformable_metric == "impact"): KonfAI IMPACT feature
256
+ # models drive the SyN/greedy stage instead of the analytic CC/MI/MSE.
257
+ self._impact_specs = impact_specs
258
+
259
+ @staticmethod
260
+ def _is_partial_mask(mask: "sitk.Image | None") -> bool:
261
+ """True only for a mask that actually restricts the region — some voxels in, some out. An absent
262
+ optional mask arrives as a whole-image (all-ones) default and an all-zero mask is degenerate; both
263
+ are treated as no mask so the plain (non-masked) metric path is used."""
264
+ if mask is None:
265
+ return False
266
+ arr = sitk.GetArrayViewFromImage(mask)
267
+ return bool((arr > 0).any()) and bool((arr == 0).any())
268
+
269
+ @staticmethod
270
+ def _affine_to_sitk(affine_matrix: "torch.Tensor") -> sitk.AffineTransform:
271
+ """FireANTs' physical (LPS) linear matrix -> SimpleITK AffineTransform (fixed -> moving points),
272
+ the same convention FireANTs writes into an ANTs ``0GenericAffine.mat``."""
273
+ matrix = affine_matrix.float().cpu().numpy()[0]
274
+ affine = sitk.AffineTransform(DIM)
275
+ affine.SetMatrix(matrix[:DIM, :DIM].flatten().astype(np.float64))
276
+ affine.SetTranslation(matrix[:DIM, DIM].astype(np.float64))
277
+ return affine
278
+
279
+ def _total_field_transform(self, reg) -> sitk.Transform:
280
+ """Optimise a deformable stage and return its TOTAL displacement (affine baked in) as a
281
+ SimpleITK ``DisplacementFieldTransform`` on the fixed grid.
282
+
283
+ FireANTs serialises the total field (ANTs convention, fixed grid) only to a file, so it is
284
+ round-tripped through a temporary NIfTI — its public API, no internals reimplemented."""
285
+ reg.optimize()
286
+ with tempfile.TemporaryDirectory() as tmp:
287
+ warp_path = os.path.join(tmp, "total_warp.nii.gz")
288
+ reg.save_as_ants_transforms(warp_path)
289
+ total_field = sitk.ReadImage(warp_path, sitk.sitkVectorFloat64)
290
+ return sitk.DisplacementFieldTransform(total_field) # consumes total_field
291
+
292
+ def register(
293
+ self,
294
+ fixed: sitk.Image,
295
+ moving: sitk.Image,
296
+ device_index: int,
297
+ fixed_mask: sitk.Image | None = None,
298
+ moving_mask: sitk.Image | None = None,
299
+ ) -> tuple[np.ndarray, np.ndarray]:
300
+ """Register ``moving`` onto ``fixed``; return (moved, dvf) as channel-first arrays on the fixed grid."""
301
+ from fireants.io import BatchedImages, Image
302
+ from fireants.io.imagemask import apply_mask_to_image, generate_image_mask_allones
303
+ from fireants.registration.affine import AffineRegistration
304
+ from fireants.registration.rigid import RigidRegistration
305
+
306
+ torch.manual_seed(self._seed)
307
+ device = f"cuda:{device_index}" if device_index >= 0 else "cpu"
308
+ # FireANTs' Image ctor accepts a SimpleITK image directly, so the fixed/moving cross into
309
+ # FireANTs in-memory (no file load) with their geometry preserved.
310
+ fixed_img = Image(fixed, device=device)
311
+ moving_img = Image(moving, device=device)
312
+
313
+ # Masked metric only when a mask genuinely restricts the region. FireANTs' masked mode wants the
314
+ # mask as the last channel of BOTH images (all-ones where one side has none) and a ``masked_``
315
+ # metric prefix; the plain path is untouched when no real mask is present.
316
+ use_fixed_mask = self._is_partial_mask(fixed_mask)
317
+ use_moving_mask = self._is_partial_mask(moving_mask)
318
+ masked = use_fixed_mask or use_moving_mask
319
+ if masked:
320
+ fmask = Image(fixed_mask, device=device) if use_fixed_mask else generate_image_mask_allones(fixed_img)
321
+ mmask = Image(moving_mask, device=device) if use_moving_mask else generate_image_mask_allones(moving_img)
322
+ fixed_img = apply_mask_to_image(fixed_img, fmask)
323
+ moving_img = apply_mask_to_image(moving_img, mmask)
324
+
325
+ bf = BatchedImages([fixed_img])
326
+ bm = BatchedImages([moving_img])
327
+ affine_loss = f"masked_{self._affine_metric}" if masked else self._affine_metric
328
+ deformable_loss = f"masked_{self._deformable_metric}" if masked else self._deformable_metric
329
+
330
+ # Linear: Rigid(MI, COM init) -> Affine(MI, seeded by the rigid), mirroring ANTs. The affine
331
+ # seeds the deformable stage (or is the whole transform when deformable_method == "none").
332
+ rigid = RigidRegistration(
333
+ scales=self._scales,
334
+ iterations=self._affine_iterations,
335
+ fixed_images=bf,
336
+ moving_images=bm,
337
+ loss_type=affine_loss,
338
+ optimizer="Adam",
339
+ optimizer_lr=self._affine_lr,
340
+ cc_kernel_size=self._cc_kernel,
341
+ init_translation="cof",
342
+ )
343
+ rigid.optimize()
344
+ rigid_matrix = rigid.get_rigid_matrix().detach()
345
+
346
+ affine = AffineRegistration(
347
+ scales=self._scales,
348
+ iterations=self._affine_iterations,
349
+ fixed_images=bf,
350
+ moving_images=bm,
351
+ loss_type=affine_loss,
352
+ optimizer="Adam",
353
+ optimizer_lr=self._affine_lr,
354
+ cc_kernel_size=self._cc_kernel,
355
+ init_rigid=rigid_matrix,
356
+ )
357
+ affine.optimize()
358
+ affine_matrix = affine.get_affine_matrix().detach()
359
+
360
+ # Deformable stage (or none). SyN and Greedy share the same constructor surface; both warm-start
361
+ # from the affine so their TOTAL transform already bakes in the linear pre-align.
362
+ if self._deformable_method == "none":
363
+ transform: sitk.Transform = self._affine_to_sitk(affine_matrix)
364
+ else:
365
+ if self._deformable_method == "syn":
366
+ from fireants.registration.syn import SyNRegistration as Deformable
367
+ elif self._deformable_method == "greedy":
368
+ from fireants.registration.greedy import GreedyRegistration as Deformable
369
+ else:
370
+ raise ValueError(
371
+ f"Unknown deformable_method '{self._deformable_method}' (expected 'syn', 'greedy' or 'none')."
372
+ )
373
+ # "impact" swaps the analytic metric for a KonfAI IMPACT feature loss on the deformable stage
374
+ # (the linear pre-align keeps its own affine_metric); masks do not restrict the IMPACT metric.
375
+ if self._deformable_metric == "impact":
376
+ loss_type: str = "custom"
377
+ custom_loss: torch.nn.Module | None = ImpactFeatureLoss(self._impact_specs)
378
+ else:
379
+ loss_type, custom_loss = deformable_loss, None
380
+ reg = Deformable(
381
+ scales=self._scales,
382
+ iterations=self._deformable_iterations,
383
+ fixed_images=bf,
384
+ moving_images=bm,
385
+ loss_type=loss_type,
386
+ custom_loss=custom_loss,
387
+ cc_kernel_size=self._cc_kernel,
388
+ deformation_type="compositive",
389
+ integrator_n=self._integrator_n,
390
+ smooth_warp_sigma=self._smooth_warp_sigma,
391
+ smooth_grad_sigma=self._smooth_grad_sigma,
392
+ optimizer="Adam",
393
+ optimizer_lr=self._deformable_lr,
394
+ init_affine=affine_matrix,
395
+ )
396
+ transform = self._total_field_transform(reg)
397
+
398
+ if torch.cuda.is_available():
399
+ torch.cuda.synchronize()
400
+
401
+ # Rebuild moved + DVF from the single transform on the fixed grid — the ConvexAdam output path,
402
+ # so every FireANTs preset emits identical-shaped results.
403
+ moved = sitk.Resample(moving, fixed, transform, sitk.sitkLinear, 0.0, moving.GetPixelID())
404
+ dvf = sitk.TransformToDisplacementField(
405
+ transform,
406
+ sitk.sitkVectorFloat64,
407
+ fixed.GetSize(),
408
+ fixed.GetOrigin(),
409
+ fixed.GetSpacing(),
410
+ fixed.GetDirection(),
411
+ )
412
+ moved_np, _ = image_to_data(moved)
413
+ dvf_np, _ = image_to_data(dvf)
414
+ return moved_np, dvf_np
415
+
416
+
417
+ class FireANTsRegistration(torch.nn.Module):
418
+ """Graph module: (fixed, moving) tensors + their geometry -> moved image + DVF on the fixed grid.
419
+
420
+ ``accepts_attributes = True`` opts this module into receiving the per-branch ``Attribute`` list
421
+ alongside the tensors (same convention as the ConvexAdam / elastix engines); registration needs the
422
+ physical geometry, and the mask branches restrict the metric.
423
+ """
424
+
425
+ accepts_attributes = True
426
+
427
+ def __init__(self, engine: FireANTsEngine) -> None:
428
+ super().__init__()
429
+ self._engine = engine
430
+
431
+ def forward(
432
+ self,
433
+ fixed: torch.Tensor,
434
+ moving: torch.Tensor,
435
+ fixed_mask: torch.Tensor,
436
+ moving_mask: torch.Tensor,
437
+ attributes: list[list[Attribute]],
438
+ ) -> torch.Tensor:
439
+ # attributes = [fixed, moving, fixed_mask, moving_mask] branch attrs; each a list[Attribute] over
440
+ # the batch. Returns, per sample, the moved image (1 channel) channel-stacked with the
441
+ # displacement field (DIM channels); downstream ChannelSelect modules split them. A whole-image
442
+ # mask (the default when none is supplied) restricts nothing.
443
+ fixed_attrs, moving_attrs, fmask_attrs, mmask_attrs = attributes
444
+ device_index = fixed.device.index if fixed.device.type == "cuda" else -1
445
+ combined = []
446
+ # FireANTs runs a gradient-based instance optimisation (Riemannian Adam over the warp); the
447
+ # predictor calls forward under torch.inference_mode(), which forbids autograd. The image tensors
448
+ # have already crossed to numpy/SimpleITK here, so re-enable grad for the optimisation.
449
+ with torch.inference_mode(False), torch.enable_grad():
450
+ for b in range(fixed.shape[0]):
451
+ fixed_img = data_to_image(fixed[b].detach().cpu().numpy(), fixed_attrs[b])
452
+ moving_img = data_to_image(moving[b].detach().cpu().numpy(), moving_attrs[b])
453
+ fixed_mask_img = data_to_image(fixed_mask[b].detach().cpu().numpy(), fmask_attrs[b])
454
+ moving_mask_img = data_to_image(moving_mask[b].detach().cpu().numpy(), mmask_attrs[b])
455
+ moved_np, dvf_np = self._engine.register(
456
+ fixed_img, moving_img, device_index, fixed_mask_img, moving_mask_img
457
+ )
458
+ combined.append(torch.from_numpy(np.concatenate([moved_np, dvf_np], axis=0)))
459
+ return torch.stack(combined, dim=0).to(fixed.device)
460
+
461
+
462
+ class ChannelSelect(torch.nn.Module):
463
+ """Select a channel slice ``[start:stop]`` (splits the registration output into moved / DVF)."""
464
+
465
+ def __init__(self, start: int, stop: int) -> None:
466
+ super().__init__()
467
+ self._start = start
468
+ self._stop = stop
469
+
470
+ def forward(self, tensor: torch.Tensor) -> torch.Tensor:
471
+ return tensor[:, self._start : self._stop]
472
+
473
+
474
+ class RegistrationNet(network.Network):
475
+ """Pairwise FireANTs registration as an ``add_module`` graph (fixed = branch 0, moving = branch 1,
476
+ fixed mask = 2, moving mask = 3; masks restrict the metric, whole-image = no restriction).
477
+
478
+ Outputs on the fixed grid: ``MovedImage`` (moving resampled onto fixed) and ``DisplacementField``
479
+ (the DIM-component displacement field, in mm). Geometry is attached by the predictor via
480
+ ``same_as_group: Volume_0:Fixed``. The knobs below are read straight from these annotations by the
481
+ UI: ``Annotated[.., Range]`` gives numeric spin bounds; ``Literal`` a dropdown. ``deformable_method``
482
+ is the knob that specialises this shared model into each FireANTs preset.
483
+ """
484
+
485
+ def __init__(
486
+ self,
487
+ optimizer: network.OptimizerLoader = network.OptimizerLoader(),
488
+ schedulers: dict[str, network.LRSchedulersLoader] = {
489
+ "default:ReduceLROnPlateau": network.LRSchedulersLoader(0)
490
+ },
491
+ outputs_criterions: dict[str, network.TargetCriterionsLoader] = {"default": network.TargetCriterionsLoader()},
492
+ scales: list[int] = [4, 2, 1],
493
+ affine_iterations: list[int] = [200, 100, 50],
494
+ deformable_iterations: list[int] = [200, 100, 50],
495
+ cc_kernel: Annotated[int, Range(1, 21)] = 5,
496
+ affine_metric: Literal["mi", "cc", "mse"] = "mi",
497
+ affine_lr: Annotated[float, Range(0.0, 10.0)] = 0.003,
498
+ deformable_method: Literal["none", "syn", "greedy"] = "syn",
499
+ deformable_metric: Literal["cc", "mi", "mse", "impact"] = "cc",
500
+ deformable_lr: Annotated[float, Range(0.0, 10.0)] = 0.25,
501
+ integrator_n: Annotated[int, Range(1, 100)] = 10,
502
+ smooth_warp_sigma: Annotated[float, Range(0.0, 100.0)] = 0.5,
503
+ smooth_grad_sigma: Annotated[float, Range(0.0, 100.0)] = 1.0,
504
+ seed: int = 42,
505
+ models: dict[str, ModelSpec] = {},
506
+ ) -> None:
507
+ super().__init__(
508
+ in_channels=1,
509
+ optimizer=optimizer,
510
+ schedulers=schedulers,
511
+ outputs_criterions=outputs_criterions,
512
+ dim=3,
513
+ )
514
+ engine = FireANTsEngine(
515
+ scales,
516
+ affine_iterations,
517
+ deformable_iterations,
518
+ cc_kernel,
519
+ affine_metric,
520
+ affine_lr,
521
+ deformable_method,
522
+ deformable_metric,
523
+ deformable_lr,
524
+ integrator_n,
525
+ smooth_warp_sigma,
526
+ smooth_grad_sigma,
527
+ seed,
528
+ _sorted_specs(models),
529
+ )
530
+ self.add_module(
531
+ "Registration", FireANTsRegistration(engine), in_branch=[0, 1, 2, 3], out_branch=["registration"]
532
+ )
533
+ self.add_module("MovedImage", ChannelSelect(0, 1), in_branch=["registration"], out_branch=["moved"])
534
+ self.add_module("DisplacementField", ChannelSelect(1, 4), in_branch=["registration"], out_branch=["dvf"])
FireANTs_IMPACT/NOTICE ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FireANTs_SyN — third-party attribution
2
+ ======================================
3
+
4
+ This KonfAI app drives FireANTs, an external registration library. FireANTs is NOT
5
+ redistributed here as source: the app depends on the official `fireants` wheel
6
+ (https://pypi.org/project/fireants/), which is installed at resolve time (see
7
+ requirements.txt) and called through its public Python API only. No FireANTs source
8
+ code (functions, classes, or modules) is copied into this app.
9
+
10
+ FireANTs is distributed under the **FireANTs License, Version 1.0 (July 2025)**, a
11
+ custom license modified from Apache 2.0. Its redistribution clause requires that, when
12
+ FireANTs is incorporated as a dependency in other projects, all license terms —
13
+ including attribution and the bibliography below — be maintained.
14
+
15
+ Project : FireANTs
16
+ Source : https://github.com/rohitrango/FireANTs
17
+ License : https://github.com/rohitrango/FireANTs/blob/main/LICENSE
18
+ Copyright (c) 2026 Rohit Jena. All rights reserved.
19
+
20
+ Bibliography (as required by the FireANTs License — cite if you use this app)
21
+ ----------------------------------------------------------------------------
22
+
23
+ @article{jena2024fireants,
24
+ title={FireANTs: Adaptive Riemannian Optimization for Multi-Scale Diffeomorphic Registration},
25
+ author={Jena, Rohit and Chaudhari, Pratik and Gee, James C},
26
+ journal={Nature Communications},
27
+ year={2024}
28
+ }
29
+
30
+ @inproceedings{jena2025scalable,
31
+ title={A Scalable Distributed Framework for Multimodal {GigaVoxel} Image Registration},
32
+ author={Jena, Rohit and Zope, Vedant and Chaudhari, Pratik and Gee, James C},
33
+ booktitle={The Fourteenth International Conference on Learning Representations},
34
+ year={2026},
35
+ url={https://openreview.net/forum?id=8dLexnao2h}
36
+ }
37
+
38
+ The wrapper code in this directory (Model.py, the KonfAI app configuration) is original
39
+ work © 2025 Valentin Boussot, licensed under Apache-2.0, and is a separate work that
40
+ merely links to the FireANTs interfaces.
FireANTs_IMPACT/Prediction.yml ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Predictor:
2
+ Model:
3
+ classpath: Model:RegistrationNet
4
+ RegistrationNet:
5
+ scales:
6
+ - 4
7
+ - 2
8
+ - 1
9
+ affine_iterations:
10
+ - 200
11
+ - 100
12
+ - 50
13
+ deformable_iterations:
14
+ - 200
15
+ - 100
16
+ - 50
17
+ cc_kernel: 5
18
+ affine_metric: mi
19
+ affine_lr: 0.003
20
+ deformable_method: syn
21
+ deformable_metric: impact
22
+ deformable_lr: 0.25
23
+ integrator_n: 10
24
+ smooth_warp_sigma: 0.5
25
+ smooth_grad_sigma: 1.0
26
+ seed: 42
27
+ models:
28
+ '0':
29
+ ref: VBoussot/impact-torchscript-models:TS/M291.pt
30
+ layers_mask: '01'
31
+ layers_weight: 1.0
32
+ pca: 0
33
+ distance: L1
34
+ outputs_criterions: None
35
+ Dataset:
36
+ groups_src:
37
+ Volume_0:
38
+ groups_dest:
39
+ Fixed:
40
+ transforms:
41
+ TensorCast:
42
+ dtype: float32
43
+ inverse: false
44
+ patch_transforms: None
45
+ is_input: true
46
+ Volume_1:
47
+ groups_dest:
48
+ Moving:
49
+ transforms:
50
+ TensorCast:
51
+ dtype: float32
52
+ inverse: false
53
+ patch_transforms: None
54
+ is_input: true
55
+ Volume_2:
56
+ groups_dest:
57
+ FixedMask:
58
+ transforms:
59
+ TensorCast:
60
+ dtype: float32
61
+ inverse: false
62
+ patch_transforms: None
63
+ is_input: true
64
+ Volume_3:
65
+ groups_dest:
66
+ MovingMask:
67
+ transforms:
68
+ TensorCast:
69
+ dtype: float32
70
+ inverse: false
71
+ patch_transforms: None
72
+ is_input: true
73
+ augmentations:
74
+ DataAugmentation_0:
75
+ data_augmentations:
76
+ Flip:
77
+ f_prob:
78
+ - 0
79
+ - 0.5
80
+ - 0.5
81
+ vector_field: true
82
+ prob: 1
83
+ nb: 2
84
+ Patch:
85
+ patch_size: None
86
+ overlap: None
87
+ mask: None
88
+ pad_value: None
89
+ extend_slice: 0
90
+ subset: None
91
+ filter: None
92
+ dataset_filenames:
93
+ - ./Dataset/:mha
94
+ use_cache: false
95
+ batch_size: 1
96
+ num_workers: None
97
+ pin_memory: false
98
+ prefetch_factor: None
99
+ persistent_workers: None
100
+ outputs_dataset:
101
+ MovedImage:
102
+ OutputDataset:
103
+ name_class: OutSameAsGroupDataset
104
+ before_reduction_transforms: None
105
+ after_reduction_transforms: None
106
+ final_transforms:
107
+ TensorCast:
108
+ dtype: float32
109
+ inverse: false
110
+ dataset_filename: Moved:mha
111
+ group: Moved
112
+ same_as_group: Volume_0:Fixed
113
+ patch_combine: None
114
+ inverse_transform: false
115
+ reduction: Mean
116
+ Mean: {}
117
+ DisplacementField:
118
+ OutputDataset:
119
+ name_class: OutSameAsGroupDataset
120
+ before_reduction_transforms: None
121
+ after_reduction_transforms: None
122
+ final_transforms:
123
+ TensorCast:
124
+ dtype: float32
125
+ inverse: false
126
+ dataset_filename: DVF:mha
127
+ group: DVF
128
+ same_as_group: Volume_0:Fixed
129
+ patch_combine: None
130
+ inverse_transform: false
131
+ reduction: Mean
132
+ Mean: {}
133
+ train_name: ImpactReg-FireANTs-IMPACT
134
+ manual_seed: 42
135
+ gpu_checkpoints: None
136
+ images_log: None
137
+ combine: Mean
138
+ autocast: false
139
+ data_log: None
FireANTs_IMPACT/Uncertainty.yml ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Evaluator:
2
+ metrics:
3
+ Uncertainty:
4
+ targets_criterions:
5
+ None:
6
+ criterions_loader:
7
+ Mean:
8
+ name: Uncertainty
9
+ Dataset:
10
+ groups_src:
11
+ Volume_0:
12
+ groups_dest:
13
+ Uncertainty:
14
+ transforms:
15
+ Norm: {}
16
+ StandardDeviation: {}
17
+ Save:
18
+ dataset: ./Uncertainties/ImpactReg/Output:mha
19
+ group: None
20
+ subset: None
21
+ dataset_filenames:
22
+ - ./Dataset:mha
23
+ validation: None
24
+ train_name: ImpactReg
FireANTs_IMPACT/app.json ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "display_name": "FireANTs (IMPACT)",
3
+ "short_description": "Rigid + Affine + SyN driven by the IMPACT deep-feature metric on GPU (FireANTs).",
4
+ "description": "GPU deformable registration with FireANTs where the SyN stage is driven by the KonfAI IMPACT metric — deep features from one or more pretrained TorchScript models — instead of an intensity metric. A Rigid (MI) + Affine (MI) linear pre-align seeds a symmetric diffeomorphic SyN optimised with Riemannian Adam against a weighted multi-model feature distance. Produces the moved image and the total displacement field on the fixed grid. FireANTs is an external dependency under the FireANTs License v1.0 (see NOTICE); the feature models are fetched from VBoussot/impact-torchscript-models.",
5
+ "task": "registration",
6
+ "tta": 0,
7
+ "mc_dropout": 0,
8
+ "models": [
9
+ "model.pt"
10
+ ],
11
+ "inputs": {
12
+ "Fixed": {
13
+ "display_name": "Fixed image",
14
+ "volume_type": "VOLUME",
15
+ "required": true
16
+ },
17
+ "Moving": {
18
+ "display_name": "Moving image",
19
+ "volume_type": "VOLUME",
20
+ "required": true
21
+ },
22
+ "FixedMask": {
23
+ "display_name": "Fixed mask (optional)",
24
+ "volume_type": "SEGMENTATION",
25
+ "required": false,
26
+ "default": "ones"
27
+ },
28
+ "MovingMask": {
29
+ "display_name": "Moving mask (optional)",
30
+ "volume_type": "SEGMENTATION",
31
+ "required": false,
32
+ "default": "ones"
33
+ }
34
+ },
35
+ "outputs": {
36
+ "MovedImage": {
37
+ "display_name": "Moved image",
38
+ "volume_type": "VOLUME",
39
+ "required": true
40
+ },
41
+ "DisplacementField": {
42
+ "display_name": "Displacement field",
43
+ "volume_type": "VOLUME",
44
+ "required": false
45
+ }
46
+ },
47
+ "inputs_evaluations": {
48
+ "Image": {
49
+ "Evaluation_with_images.yml": {
50
+ "FixedImage": {
51
+ "display_name": "Fixed image",
52
+ "volume_type": "VOLUME",
53
+ "required": true
54
+ },
55
+ "MovingImage": {
56
+ "display_name": "Moving image",
57
+ "volume_type": "VOLUME",
58
+ "required": true
59
+ },
60
+ "Mask": {
61
+ "display_name": "Evaluation mask",
62
+ "volume_type": "SEGMENTATION",
63
+ "required": false
64
+ }
65
+ }
66
+ },
67
+ "Segmentation": {
68
+ "Evaluation_with_seg.yml": {
69
+ "FixedSeg": {
70
+ "display_name": "Fixed segmentation",
71
+ "volume_type": "SEGMENTATION",
72
+ "required": true
73
+ },
74
+ "MovingSeg": {
75
+ "display_name": "Moving segmentation",
76
+ "volume_type": "SEGMENTATION",
77
+ "required": true
78
+ }
79
+ }
80
+ },
81
+ "Landmarks": {
82
+ "Evaluation_with_fid.yml": {
83
+ "FixedFid": {
84
+ "display_name": "Fixed landmarks",
85
+ "volume_type": "FIDUCIALS",
86
+ "required": true
87
+ },
88
+ "MovingFid": {
89
+ "display_name": "Moving landmarks",
90
+ "volume_type": "FIDUCIALS",
91
+ "required": true
92
+ }
93
+ }
94
+ }
95
+ }
96
+ }
FireANTs_IMPACT/model.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:de99fbc36331ce674639acc774f52b4a2d0027f2f312d9d28669e831a0c4fd7e
3
+ size 1249
FireANTs_IMPACT/requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ fireants
FireANTs_SyN/Evaluation_with_fid.yml ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Evaluator:
2
+ metrics:
3
+ FixedFid:
4
+ targets_criterions:
5
+ MovingFid:
6
+ criterions_loader:
7
+ TRE: {}
8
+ Dataset:
9
+ groups_src:
10
+ Volume_0:
11
+ groups_dest:
12
+ FixedFid:
13
+ transforms: None
14
+ Reference_0:
15
+ groups_dest:
16
+ MovingFid:
17
+ transforms: None
18
+ subset: None
19
+ dataset_filenames:
20
+ - ./Dataset:mha
21
+ validation: None
22
+ train_name: ImpactReg
FireANTs_SyN/Evaluation_with_images.yml ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Evaluator:
2
+ metrics:
3
+ FixedImage:
4
+ targets_criterions:
5
+ MovingImage;Mask:
6
+ criterions_loader:
7
+ MAESaveMap:
8
+ reduction: mean
9
+ dataset: ./Evaluations/ImpactReg/Output:mha
10
+ group: MAE_map
11
+ Dataset:
12
+ groups_src:
13
+ Volume_0:
14
+ groups_dest:
15
+ FixedImage:
16
+ transforms:
17
+ TensorCast:
18
+ dtype: float32
19
+ Reference_0:
20
+ groups_dest:
21
+ MovingImage:
22
+ transforms:
23
+ TensorCast:
24
+ dtype: float32
25
+ Mask_0:
26
+ groups_dest:
27
+ Mask:
28
+ transforms:
29
+ TensorCast:
30
+ dtype: uint8
31
+ subset: None
32
+ dataset_filenames:
33
+ - ./Dataset:mha
34
+ validation: None
35
+ train_name: ImpactReg
FireANTs_SyN/Evaluation_with_seg.yml ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Evaluator:
2
+ metrics:
3
+ FixedSeg:
4
+ targets_criterions:
5
+ MovingSeg:
6
+ criterions_loader:
7
+ DiceSaveMap:
8
+ labels: None
9
+ dataset: ./Evaluations/ImpactReg/Output:mha
10
+ group: Seg_MAE_map
11
+ Dataset:
12
+ groups_src:
13
+ Volume_0:
14
+ groups_dest:
15
+ FixedSeg:
16
+ transforms:
17
+ TensorCast:
18
+ dtype: uint8
19
+ Reference_0:
20
+ groups_dest:
21
+ MovingSeg:
22
+ transforms:
23
+ TensorCast:
24
+ dtype: uint8
25
+ subset: None
26
+ dataset_filenames:
27
+ - ./Dataset:mha
28
+ validation: None
29
+ train_name: ImpactReg
FireANTs_SyN/Model.py ADDED
@@ -0,0 +1,534 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2025 Valentin Boussot
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ #
15
+ # SPDX-License-Identifier: Apache-2.0
16
+ #
17
+ # This wrapper does NOT copy any FireANTs source: it only calls the public FireANTs API of the
18
+ # separately-installed ``fireants`` wheel (PyPI). FireANTs is distributed under the FireANTs License
19
+ # v1.0 and must be cited — see the NOTICE file in this directory for the license, copyright and
20
+ # bibliography that ship with this app.
21
+
22
+ """FireANTs registration as a self-contained KonfAI model (shared by the FireANTs presets).
23
+
24
+ Same idiomatic ``add_module`` graph and the same output contract as the ConvexAdam preset
25
+ (``MovedImage`` + ``DisplacementField`` on the FIXED grid, split by two ``ChannelSelect``), so the
26
+ orchestrator / app.json / ensemble / uncertainty are unchanged. The engine chains FireANTs' own
27
+ composable stages (GPU, Riemannian Adam), each seeding the next like ANTs' ``-t`` stages:
28
+
29
+ Rigid (MI, centre-of-mass init) -> Affine (MI, seeded by the rigid) -> deformable
30
+
31
+ The deformable stage is selected by ``deformable_method`` — the ONE knob that specialises this shared
32
+ Model.py into the different presets (exactly as ConvexAdam's shared Model.py is specialised by
33
+ ``stages``):
34
+
35
+ "syn" symmetric diffeomorphic SyN (CC) — invertible, higher quality, averages cleanly for ensembling
36
+ "greedy" greedy diffeomorphic (CC) — one-directional, faster / lower VRAM
37
+ "none" linear only — Rigid+Affine, no deformable (the FireANTs_Affine preset)
38
+
39
+ Masks: the optional Fixed/Moving masks restrict the metric to a region. FireANTs implements this by
40
+ carrying the mask as the last image channel and prefixing the metric with ``masked_``; a mask is only
41
+ honoured when it actually restricts (some voxels in, some out), so the common mask-free path is
42
+ unchanged (an absent optional mask arrives as a whole-image default and is treated as no mask).
43
+
44
+ The deformable stages produce the single TOTAL displacement field on the fixed grid (the linear
45
+ pre-align is baked in via ``init_affine``, ANTs convention); ``none`` uses the affine matrix directly.
46
+ ``MovedImage`` and the emitted ``DisplacementField`` are rebuilt from that transform with SimpleITK —
47
+ the same output path as the ConvexAdam engine — so all presets/engines are interchangeable in an
48
+ ensemble. FireANTs' output-transform writer only serialises to a file, so the deformable field is
49
+ round-tripped through a temporary NIfTI (no FireANTs internals are reimplemented here).
50
+
51
+ NOTE: do NOT add ``from __future__ import annotations`` — KonfAI's config engine relies on
52
+ runtime-evaluated annotations (``get_origin``); PEP 563 stringized annotations break binding.
53
+ """
54
+
55
+ import contextlib
56
+ import json
57
+ import os
58
+ import tempfile
59
+ from dataclasses import dataclass
60
+ from pathlib import Path
61
+ from typing import Annotated, Literal
62
+
63
+ import numpy as np
64
+ import SimpleITK as sitk
65
+ import torch
66
+ from konfai.metric.measure import IMPACTReg
67
+ from konfai.network import network
68
+ from konfai.utils.config import Choices, Range
69
+ from konfai.utils.dataset import Attribute, data_to_image, image_to_data
70
+
71
+ DIM = 3
72
+
73
+ # Feature-model registry (models.json): the available IMPACT feature models, fetched from HF (NOT bundled).
74
+ # Only consulted by the "impact" deformable metric; ``KONFAI_IMPACT_MODELS_REGISTRY`` (a local path) wins
75
+ # for dev/offline. Mirrors the ConvexAdam preset so the same 30-model catalogue and picker are shared.
76
+ _IMPACT_MODELS_REGISTRY = "VBoussot/impact-torchscript-models:models.json"
77
+
78
+ _DISTANCES: dict[str, type[torch.nn.Module]] = {"L1": torch.nn.L1Loss, "L2": torch.nn.MSELoss}
79
+
80
+
81
+ def registry_choices() -> list[str]:
82
+ """The per-model ``ref`` picker's values — model refs (``repo:path``) from the feature-model registry."""
83
+ repo = _IMPACT_MODELS_REGISTRY.split(":", 1)[0]
84
+ return [f"{repo}:{key}" for key in load_models_registry()]
85
+
86
+
87
+ def load_models_registry(ref: str = _IMPACT_MODELS_REGISTRY) -> dict:
88
+ """Load ``models.json`` (available feature models). ``KONFAI_IMPACT_MODELS_REGISTRY`` (local path) wins
89
+ for dev/offline; otherwise ``ref`` is a ``repo:file`` Hugging Face reference (fetched, not bundled)."""
90
+ from huggingface_hub import hf_hub_download
91
+
92
+ local = os.environ.get("KONFAI_IMPACT_MODELS_REGISTRY", "")
93
+ if local:
94
+ path = Path(local)
95
+ elif ":" in ref:
96
+ repo, filename = ref.split(":", 1)
97
+ path = Path(hf_hub_download(repo_id=repo, filename=filename, repo_type="model")) # nosec B615
98
+ else:
99
+ raise ValueError(
100
+ f"models_registry '{ref}' must be a 'repo:file' Hugging Face reference — or set "
101
+ "KONFAI_IMPACT_MODELS_REGISTRY to a local file for offline use."
102
+ )
103
+ return json.loads(path.read_text(encoding="utf-8"))
104
+
105
+
106
+ def _sorted_specs(mapping: dict) -> list:
107
+ """A dict keyed by string indices ('0','1',...) -> its values in numeric order."""
108
+ return [mapping[k] for k in sorted(mapping, key=lambda key: int(key))]
109
+
110
+
111
+ @dataclass
112
+ class ModelSpec:
113
+ """One IMPACT feature model in the deformable metric (several are fused). ``ref`` picks the model; the
114
+ rest are its per-model knobs — the same as the ConvexAdam / elastix ``ModelSpec`` except ``voxel_size``
115
+ (an itk-impact resampling knob) has no meaning for FireANTs' geometry-free torch ``custom_loss`` and is
116
+ intentionally absent."""
117
+
118
+ ref: Annotated[str, Choices(registry_choices)]
119
+ layers_mask: str = "01" # per-layer bitmask, one char per model layer ('1' = use, '0' = skip), like elastix
120
+ layers_weight: float = 1.0 # this model's weight in the multi-model fusion
121
+ pca: Annotated[int, Range(0, 100)] = 0 # keep the top-K principal components of the features (0 = no PCA)
122
+ distance: Literal["L1", "L2"] = "L1"
123
+
124
+
125
+ @contextlib.contextmanager
126
+ def _no_texpr_fuser():
127
+ """Disable the TensorExpr JIT fuser while IMPACT's TorchScript feature model runs under autograd.
128
+
129
+ The IMPACT feature models are TorchScript; run under FireANTs' gradient optimisation the TensorExpr
130
+ fuser trips on shape ops (``aten::size`` INTERNAL ASSERT). Scoped and restored so no other torch/JIT
131
+ user is affected; the modern profiling executor stays on (this is NOT the legacy executor).
132
+ """
133
+ torch._C._jit_set_texpr_fuser_enabled(False)
134
+ try:
135
+ yield
136
+ finally:
137
+ torch._C._jit_set_texpr_fuser_enabled(True)
138
+
139
+
140
+ class _ImpactCore(IMPACTReg):
141
+ """One IMPACT feature model, exposed as a FireANTs ``forward(moved, fixed)``.
142
+
143
+ Reuses ``IMPACTReg._compute`` / ``preprocessing`` verbatim — the stats-normalised feature extraction
144
+ (the model wants per-image ``[min, mean, max, std]``) and the per-layer weighted distance — so the
145
+ metric is exactly KonfAI's, not a re-derivation. Only KonfAI's config-binding ``__init__`` and its
146
+ ``Attribute``-based geometry are replaced: FireANTs passes raw tensors at the current pyramid scale, so
147
+ the intensity statistics are computed from those tensors directly. ``pca`` (absent from KonfAI's torch
148
+ ``IMPACTReg``) is added here as a per-layer feature-space reduction matching itk-impact.
149
+ """
150
+
151
+ def __init__(self, ref: str, in_channels: int, weights: list[float], distance: str, pca: int) -> None:
152
+ from huggingface_hub import hf_hub_download
153
+
154
+ torch.nn.Module.__init__(self) # bypass IMPACTReg.__init__ (KONFAI_CONFIG_PATH / apply_config binding)
155
+ self.name = "Reg"
156
+ self.in_channels = int(in_channels)
157
+ self.weights = [float(w) for w in weights]
158
+ self.nb_layer = len(self.weights)
159
+ self.loss = _DISTANCES[distance]()
160
+ self.pca = int(pca) # PCA lives in KonfAI's IMPACTReg._compute (same behaviour as itk-impact)
161
+ self.dim = DIM
162
+ self.shape = None # score the whole (downsampled) tensor — no ModelPatch tiling
163
+ if ":" in ref: # a "repo:path" HF reference; otherwise a local model file
164
+ repo, filename = ref.split(":", 1)
165
+ self.model_path = hf_hub_download(repo, filename, repo_type="model") # nosec B615
166
+ else:
167
+ self.model_path = ref
168
+ self.model = None # lazy-loaded on the first forward, like IMPACTReg
169
+
170
+ @staticmethod
171
+ def _stats(tensor: torch.Tensor) -> dict:
172
+ detached = tensor.detach()
173
+ return {
174
+ "ImageMin": float(detached.min()),
175
+ "ImageMean": float(detached.mean()),
176
+ "ImageMax": float(detached.max()),
177
+ "ImageStd": float(detached.std()),
178
+ }
179
+
180
+ def forward(self, moved: torch.Tensor, fixed: torch.Tensor) -> torch.Tensor: # type: ignore[override]
181
+ if self.model is None:
182
+ self.model = torch.jit.load(self.model_path) # nosec B614
183
+ self.model.to(moved.device).eval()
184
+ with _no_texpr_fuser():
185
+ loss, true_nb = self._compute(moved, [self._stats(moved)], fixed, [self._stats(fixed)], None)
186
+ return loss / max(true_nb, 1)
187
+
188
+
189
+ class ImpactFeatureLoss(torch.nn.Module):
190
+ """FireANTs ``custom_loss`` = the KonfAI IMPACT metric fused over several feature models.
191
+
192
+ ``forward(moved, fixed)`` sums each model's ``layers_weight * IMPACT(model)``. A model's per-layer
193
+ weights come from its ``layers_mask`` bitmask; its input channel count is read from the registry
194
+ (``models.json`` ``numberofchannels``) so it never has to be configured by hand.
195
+ """
196
+
197
+ def __init__(self, specs: list["ModelSpec"]) -> None:
198
+ super().__init__()
199
+ registry = load_models_registry()
200
+ self._cores = torch.nn.ModuleList()
201
+ self._model_weights: list[float] = []
202
+ for spec in specs:
203
+ in_channels = int(registry.get(spec.ref.split(":", 1)[-1], {}).get("numberofchannels", 1))
204
+ weights = [1.0 if char == "1" else 0.0 for char in spec.layers_mask]
205
+ self._cores.append(_ImpactCore(spec.ref, in_channels, weights, spec.distance, spec.pca))
206
+ self._model_weights.append(float(spec.layers_weight))
207
+
208
+ def forward(self, moved: torch.Tensor, fixed: torch.Tensor) -> torch.Tensor:
209
+ total: torch.Tensor | None = None
210
+ for weight, core in zip(self._model_weights, self._cores, strict=True):
211
+ term = weight * core(moved, fixed)
212
+ total = term if total is None else total + term
213
+ return total
214
+
215
+
216
+ class FireANTsEngine:
217
+ """Register a fixed/moving pair with FireANTs (Rigid -> Affine -> [SyN | Greedy | none]); return
218
+ (moved, dvf) on the fixed grid.
219
+
220
+ ``fireants`` is imported lazily inside :meth:`register` so this module can be imported for config
221
+ /signature introspection (SlicerImpactReg reads the tuning knobs off the ``RegistrationNet``
222
+ annotations) on a machine without a GPU or without FireANTs installed.
223
+ """
224
+
225
+ def __init__(
226
+ self,
227
+ scales: list[int],
228
+ affine_iterations: list[int],
229
+ deformable_iterations: list[int],
230
+ cc_kernel: int,
231
+ affine_metric: str,
232
+ affine_lr: float,
233
+ deformable_method: str,
234
+ deformable_metric: str,
235
+ deformable_lr: float,
236
+ integrator_n: int,
237
+ smooth_warp_sigma: float,
238
+ smooth_grad_sigma: float,
239
+ seed: int,
240
+ impact_specs: list["ModelSpec"],
241
+ ) -> None:
242
+ self._scales = [int(s) for s in scales]
243
+ self._affine_iterations = [int(i) for i in affine_iterations]
244
+ self._deformable_iterations = [int(i) for i in deformable_iterations]
245
+ self._cc_kernel = int(cc_kernel)
246
+ self._affine_metric = affine_metric
247
+ self._affine_lr = float(affine_lr)
248
+ self._deformable_method = deformable_method
249
+ self._deformable_metric = deformable_metric
250
+ self._deformable_lr = float(deformable_lr)
251
+ self._integrator_n = int(integrator_n)
252
+ self._smooth_warp_sigma = float(smooth_warp_sigma)
253
+ self._smooth_grad_sigma = float(smooth_grad_sigma)
254
+ self._seed = int(seed)
255
+ # IMPACT deformable metric (only used when deformable_metric == "impact"): KonfAI IMPACT feature
256
+ # models drive the SyN/greedy stage instead of the analytic CC/MI/MSE.
257
+ self._impact_specs = impact_specs
258
+
259
+ @staticmethod
260
+ def _is_partial_mask(mask: "sitk.Image | None") -> bool:
261
+ """True only for a mask that actually restricts the region — some voxels in, some out. An absent
262
+ optional mask arrives as a whole-image (all-ones) default and an all-zero mask is degenerate; both
263
+ are treated as no mask so the plain (non-masked) metric path is used."""
264
+ if mask is None:
265
+ return False
266
+ arr = sitk.GetArrayViewFromImage(mask)
267
+ return bool((arr > 0).any()) and bool((arr == 0).any())
268
+
269
+ @staticmethod
270
+ def _affine_to_sitk(affine_matrix: "torch.Tensor") -> sitk.AffineTransform:
271
+ """FireANTs' physical (LPS) linear matrix -> SimpleITK AffineTransform (fixed -> moving points),
272
+ the same convention FireANTs writes into an ANTs ``0GenericAffine.mat``."""
273
+ matrix = affine_matrix.float().cpu().numpy()[0]
274
+ affine = sitk.AffineTransform(DIM)
275
+ affine.SetMatrix(matrix[:DIM, :DIM].flatten().astype(np.float64))
276
+ affine.SetTranslation(matrix[:DIM, DIM].astype(np.float64))
277
+ return affine
278
+
279
+ def _total_field_transform(self, reg) -> sitk.Transform:
280
+ """Optimise a deformable stage and return its TOTAL displacement (affine baked in) as a
281
+ SimpleITK ``DisplacementFieldTransform`` on the fixed grid.
282
+
283
+ FireANTs serialises the total field (ANTs convention, fixed grid) only to a file, so it is
284
+ round-tripped through a temporary NIfTI — its public API, no internals reimplemented."""
285
+ reg.optimize()
286
+ with tempfile.TemporaryDirectory() as tmp:
287
+ warp_path = os.path.join(tmp, "total_warp.nii.gz")
288
+ reg.save_as_ants_transforms(warp_path)
289
+ total_field = sitk.ReadImage(warp_path, sitk.sitkVectorFloat64)
290
+ return sitk.DisplacementFieldTransform(total_field) # consumes total_field
291
+
292
+ def register(
293
+ self,
294
+ fixed: sitk.Image,
295
+ moving: sitk.Image,
296
+ device_index: int,
297
+ fixed_mask: sitk.Image | None = None,
298
+ moving_mask: sitk.Image | None = None,
299
+ ) -> tuple[np.ndarray, np.ndarray]:
300
+ """Register ``moving`` onto ``fixed``; return (moved, dvf) as channel-first arrays on the fixed grid."""
301
+ from fireants.io import BatchedImages, Image
302
+ from fireants.io.imagemask import apply_mask_to_image, generate_image_mask_allones
303
+ from fireants.registration.affine import AffineRegistration
304
+ from fireants.registration.rigid import RigidRegistration
305
+
306
+ torch.manual_seed(self._seed)
307
+ device = f"cuda:{device_index}" if device_index >= 0 else "cpu"
308
+ # FireANTs' Image ctor accepts a SimpleITK image directly, so the fixed/moving cross into
309
+ # FireANTs in-memory (no file load) with their geometry preserved.
310
+ fixed_img = Image(fixed, device=device)
311
+ moving_img = Image(moving, device=device)
312
+
313
+ # Masked metric only when a mask genuinely restricts the region. FireANTs' masked mode wants the
314
+ # mask as the last channel of BOTH images (all-ones where one side has none) and a ``masked_``
315
+ # metric prefix; the plain path is untouched when no real mask is present.
316
+ use_fixed_mask = self._is_partial_mask(fixed_mask)
317
+ use_moving_mask = self._is_partial_mask(moving_mask)
318
+ masked = use_fixed_mask or use_moving_mask
319
+ if masked:
320
+ fmask = Image(fixed_mask, device=device) if use_fixed_mask else generate_image_mask_allones(fixed_img)
321
+ mmask = Image(moving_mask, device=device) if use_moving_mask else generate_image_mask_allones(moving_img)
322
+ fixed_img = apply_mask_to_image(fixed_img, fmask)
323
+ moving_img = apply_mask_to_image(moving_img, mmask)
324
+
325
+ bf = BatchedImages([fixed_img])
326
+ bm = BatchedImages([moving_img])
327
+ affine_loss = f"masked_{self._affine_metric}" if masked else self._affine_metric
328
+ deformable_loss = f"masked_{self._deformable_metric}" if masked else self._deformable_metric
329
+
330
+ # Linear: Rigid(MI, COM init) -> Affine(MI, seeded by the rigid), mirroring ANTs. The affine
331
+ # seeds the deformable stage (or is the whole transform when deformable_method == "none").
332
+ rigid = RigidRegistration(
333
+ scales=self._scales,
334
+ iterations=self._affine_iterations,
335
+ fixed_images=bf,
336
+ moving_images=bm,
337
+ loss_type=affine_loss,
338
+ optimizer="Adam",
339
+ optimizer_lr=self._affine_lr,
340
+ cc_kernel_size=self._cc_kernel,
341
+ init_translation="cof",
342
+ )
343
+ rigid.optimize()
344
+ rigid_matrix = rigid.get_rigid_matrix().detach()
345
+
346
+ affine = AffineRegistration(
347
+ scales=self._scales,
348
+ iterations=self._affine_iterations,
349
+ fixed_images=bf,
350
+ moving_images=bm,
351
+ loss_type=affine_loss,
352
+ optimizer="Adam",
353
+ optimizer_lr=self._affine_lr,
354
+ cc_kernel_size=self._cc_kernel,
355
+ init_rigid=rigid_matrix,
356
+ )
357
+ affine.optimize()
358
+ affine_matrix = affine.get_affine_matrix().detach()
359
+
360
+ # Deformable stage (or none). SyN and Greedy share the same constructor surface; both warm-start
361
+ # from the affine so their TOTAL transform already bakes in the linear pre-align.
362
+ if self._deformable_method == "none":
363
+ transform: sitk.Transform = self._affine_to_sitk(affine_matrix)
364
+ else:
365
+ if self._deformable_method == "syn":
366
+ from fireants.registration.syn import SyNRegistration as Deformable
367
+ elif self._deformable_method == "greedy":
368
+ from fireants.registration.greedy import GreedyRegistration as Deformable
369
+ else:
370
+ raise ValueError(
371
+ f"Unknown deformable_method '{self._deformable_method}' (expected 'syn', 'greedy' or 'none')."
372
+ )
373
+ # "impact" swaps the analytic metric for a KonfAI IMPACT feature loss on the deformable stage
374
+ # (the linear pre-align keeps its own affine_metric); masks do not restrict the IMPACT metric.
375
+ if self._deformable_metric == "impact":
376
+ loss_type: str = "custom"
377
+ custom_loss: torch.nn.Module | None = ImpactFeatureLoss(self._impact_specs)
378
+ else:
379
+ loss_type, custom_loss = deformable_loss, None
380
+ reg = Deformable(
381
+ scales=self._scales,
382
+ iterations=self._deformable_iterations,
383
+ fixed_images=bf,
384
+ moving_images=bm,
385
+ loss_type=loss_type,
386
+ custom_loss=custom_loss,
387
+ cc_kernel_size=self._cc_kernel,
388
+ deformation_type="compositive",
389
+ integrator_n=self._integrator_n,
390
+ smooth_warp_sigma=self._smooth_warp_sigma,
391
+ smooth_grad_sigma=self._smooth_grad_sigma,
392
+ optimizer="Adam",
393
+ optimizer_lr=self._deformable_lr,
394
+ init_affine=affine_matrix,
395
+ )
396
+ transform = self._total_field_transform(reg)
397
+
398
+ if torch.cuda.is_available():
399
+ torch.cuda.synchronize()
400
+
401
+ # Rebuild moved + DVF from the single transform on the fixed grid — the ConvexAdam output path,
402
+ # so every FireANTs preset emits identical-shaped results.
403
+ moved = sitk.Resample(moving, fixed, transform, sitk.sitkLinear, 0.0, moving.GetPixelID())
404
+ dvf = sitk.TransformToDisplacementField(
405
+ transform,
406
+ sitk.sitkVectorFloat64,
407
+ fixed.GetSize(),
408
+ fixed.GetOrigin(),
409
+ fixed.GetSpacing(),
410
+ fixed.GetDirection(),
411
+ )
412
+ moved_np, _ = image_to_data(moved)
413
+ dvf_np, _ = image_to_data(dvf)
414
+ return moved_np, dvf_np
415
+
416
+
417
+ class FireANTsRegistration(torch.nn.Module):
418
+ """Graph module: (fixed, moving) tensors + their geometry -> moved image + DVF on the fixed grid.
419
+
420
+ ``accepts_attributes = True`` opts this module into receiving the per-branch ``Attribute`` list
421
+ alongside the tensors (same convention as the ConvexAdam / elastix engines); registration needs the
422
+ physical geometry, and the mask branches restrict the metric.
423
+ """
424
+
425
+ accepts_attributes = True
426
+
427
+ def __init__(self, engine: FireANTsEngine) -> None:
428
+ super().__init__()
429
+ self._engine = engine
430
+
431
+ def forward(
432
+ self,
433
+ fixed: torch.Tensor,
434
+ moving: torch.Tensor,
435
+ fixed_mask: torch.Tensor,
436
+ moving_mask: torch.Tensor,
437
+ attributes: list[list[Attribute]],
438
+ ) -> torch.Tensor:
439
+ # attributes = [fixed, moving, fixed_mask, moving_mask] branch attrs; each a list[Attribute] over
440
+ # the batch. Returns, per sample, the moved image (1 channel) channel-stacked with the
441
+ # displacement field (DIM channels); downstream ChannelSelect modules split them. A whole-image
442
+ # mask (the default when none is supplied) restricts nothing.
443
+ fixed_attrs, moving_attrs, fmask_attrs, mmask_attrs = attributes
444
+ device_index = fixed.device.index if fixed.device.type == "cuda" else -1
445
+ combined = []
446
+ # FireANTs runs a gradient-based instance optimisation (Riemannian Adam over the warp); the
447
+ # predictor calls forward under torch.inference_mode(), which forbids autograd. The image tensors
448
+ # have already crossed to numpy/SimpleITK here, so re-enable grad for the optimisation.
449
+ with torch.inference_mode(False), torch.enable_grad():
450
+ for b in range(fixed.shape[0]):
451
+ fixed_img = data_to_image(fixed[b].detach().cpu().numpy(), fixed_attrs[b])
452
+ moving_img = data_to_image(moving[b].detach().cpu().numpy(), moving_attrs[b])
453
+ fixed_mask_img = data_to_image(fixed_mask[b].detach().cpu().numpy(), fmask_attrs[b])
454
+ moving_mask_img = data_to_image(moving_mask[b].detach().cpu().numpy(), mmask_attrs[b])
455
+ moved_np, dvf_np = self._engine.register(
456
+ fixed_img, moving_img, device_index, fixed_mask_img, moving_mask_img
457
+ )
458
+ combined.append(torch.from_numpy(np.concatenate([moved_np, dvf_np], axis=0)))
459
+ return torch.stack(combined, dim=0).to(fixed.device)
460
+
461
+
462
+ class ChannelSelect(torch.nn.Module):
463
+ """Select a channel slice ``[start:stop]`` (splits the registration output into moved / DVF)."""
464
+
465
+ def __init__(self, start: int, stop: int) -> None:
466
+ super().__init__()
467
+ self._start = start
468
+ self._stop = stop
469
+
470
+ def forward(self, tensor: torch.Tensor) -> torch.Tensor:
471
+ return tensor[:, self._start : self._stop]
472
+
473
+
474
+ class RegistrationNet(network.Network):
475
+ """Pairwise FireANTs registration as an ``add_module`` graph (fixed = branch 0, moving = branch 1,
476
+ fixed mask = 2, moving mask = 3; masks restrict the metric, whole-image = no restriction).
477
+
478
+ Outputs on the fixed grid: ``MovedImage`` (moving resampled onto fixed) and ``DisplacementField``
479
+ (the DIM-component displacement field, in mm). Geometry is attached by the predictor via
480
+ ``same_as_group: Volume_0:Fixed``. The knobs below are read straight from these annotations by the
481
+ UI: ``Annotated[.., Range]`` gives numeric spin bounds; ``Literal`` a dropdown. ``deformable_method``
482
+ is the knob that specialises this shared model into each FireANTs preset.
483
+ """
484
+
485
+ def __init__(
486
+ self,
487
+ optimizer: network.OptimizerLoader = network.OptimizerLoader(),
488
+ schedulers: dict[str, network.LRSchedulersLoader] = {
489
+ "default:ReduceLROnPlateau": network.LRSchedulersLoader(0)
490
+ },
491
+ outputs_criterions: dict[str, network.TargetCriterionsLoader] = {"default": network.TargetCriterionsLoader()},
492
+ scales: list[int] = [4, 2, 1],
493
+ affine_iterations: list[int] = [200, 100, 50],
494
+ deformable_iterations: list[int] = [200, 100, 50],
495
+ cc_kernel: Annotated[int, Range(1, 21)] = 5,
496
+ affine_metric: Literal["mi", "cc", "mse"] = "mi",
497
+ affine_lr: Annotated[float, Range(0.0, 10.0)] = 0.003,
498
+ deformable_method: Literal["none", "syn", "greedy"] = "syn",
499
+ deformable_metric: Literal["cc", "mi", "mse", "impact"] = "cc",
500
+ deformable_lr: Annotated[float, Range(0.0, 10.0)] = 0.25,
501
+ integrator_n: Annotated[int, Range(1, 100)] = 10,
502
+ smooth_warp_sigma: Annotated[float, Range(0.0, 100.0)] = 0.5,
503
+ smooth_grad_sigma: Annotated[float, Range(0.0, 100.0)] = 1.0,
504
+ seed: int = 42,
505
+ models: dict[str, ModelSpec] = {},
506
+ ) -> None:
507
+ super().__init__(
508
+ in_channels=1,
509
+ optimizer=optimizer,
510
+ schedulers=schedulers,
511
+ outputs_criterions=outputs_criterions,
512
+ dim=3,
513
+ )
514
+ engine = FireANTsEngine(
515
+ scales,
516
+ affine_iterations,
517
+ deformable_iterations,
518
+ cc_kernel,
519
+ affine_metric,
520
+ affine_lr,
521
+ deformable_method,
522
+ deformable_metric,
523
+ deformable_lr,
524
+ integrator_n,
525
+ smooth_warp_sigma,
526
+ smooth_grad_sigma,
527
+ seed,
528
+ _sorted_specs(models),
529
+ )
530
+ self.add_module(
531
+ "Registration", FireANTsRegistration(engine), in_branch=[0, 1, 2, 3], out_branch=["registration"]
532
+ )
533
+ self.add_module("MovedImage", ChannelSelect(0, 1), in_branch=["registration"], out_branch=["moved"])
534
+ self.add_module("DisplacementField", ChannelSelect(1, 4), in_branch=["registration"], out_branch=["dvf"])
FireANTs_SyN/NOTICE ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FireANTs_SyN — third-party attribution
2
+ ======================================
3
+
4
+ This KonfAI app drives FireANTs, an external registration library. FireANTs is NOT
5
+ redistributed here as source: the app depends on the official `fireants` wheel
6
+ (https://pypi.org/project/fireants/), which is installed at resolve time (see
7
+ requirements.txt) and called through its public Python API only. No FireANTs source
8
+ code (functions, classes, or modules) is copied into this app.
9
+
10
+ FireANTs is distributed under the **FireANTs License, Version 1.0 (July 2025)**, a
11
+ custom license modified from Apache 2.0. Its redistribution clause requires that, when
12
+ FireANTs is incorporated as a dependency in other projects, all license terms —
13
+ including attribution and the bibliography below — be maintained.
14
+
15
+ Project : FireANTs
16
+ Source : https://github.com/rohitrango/FireANTs
17
+ License : https://github.com/rohitrango/FireANTs/blob/main/LICENSE
18
+ Copyright (c) 2026 Rohit Jena. All rights reserved.
19
+
20
+ Bibliography (as required by the FireANTs License — cite if you use this app)
21
+ ----------------------------------------------------------------------------
22
+
23
+ @article{jena2024fireants,
24
+ title={FireANTs: Adaptive Riemannian Optimization for Multi-Scale Diffeomorphic Registration},
25
+ author={Jena, Rohit and Chaudhari, Pratik and Gee, James C},
26
+ journal={Nature Communications},
27
+ year={2024}
28
+ }
29
+
30
+ @inproceedings{jena2025scalable,
31
+ title={A Scalable Distributed Framework for Multimodal {GigaVoxel} Image Registration},
32
+ author={Jena, Rohit and Zope, Vedant and Chaudhari, Pratik and Gee, James C},
33
+ booktitle={The Fourteenth International Conference on Learning Representations},
34
+ year={2026},
35
+ url={https://openreview.net/forum?id=8dLexnao2h}
36
+ }
37
+
38
+ The wrapper code in this directory (Model.py, the KonfAI app configuration) is original
39
+ work © 2025 Valentin Boussot, licensed under Apache-2.0, and is a separate work that
40
+ merely links to the FireANTs interfaces.
FireANTs_SyN/Prediction.yml ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Predictor:
2
+ Model:
3
+ classpath: Model:RegistrationNet
4
+ RegistrationNet:
5
+ scales:
6
+ - 4
7
+ - 2
8
+ - 1
9
+ affine_iterations:
10
+ - 200
11
+ - 100
12
+ - 50
13
+ deformable_iterations:
14
+ - 200
15
+ - 100
16
+ - 50
17
+ cc_kernel: 5
18
+ affine_metric: mi
19
+ affine_lr: 0.003
20
+ deformable_method: syn
21
+ deformable_metric: cc
22
+ deformable_lr: 0.25
23
+ integrator_n: 10
24
+ smooth_warp_sigma: 0.5
25
+ smooth_grad_sigma: 1.0
26
+ seed: 42
27
+ outputs_criterions: None
28
+ Dataset:
29
+ groups_src:
30
+ Volume_0:
31
+ groups_dest:
32
+ Fixed:
33
+ transforms:
34
+ TensorCast:
35
+ dtype: float32
36
+ inverse: false
37
+ patch_transforms: None
38
+ is_input: true
39
+ Volume_1:
40
+ groups_dest:
41
+ Moving:
42
+ transforms:
43
+ TensorCast:
44
+ dtype: float32
45
+ inverse: false
46
+ patch_transforms: None
47
+ is_input: true
48
+ Volume_2:
49
+ groups_dest:
50
+ FixedMask:
51
+ transforms:
52
+ TensorCast:
53
+ dtype: float32
54
+ inverse: false
55
+ patch_transforms: None
56
+ is_input: true
57
+ Volume_3:
58
+ groups_dest:
59
+ MovingMask:
60
+ transforms:
61
+ TensorCast:
62
+ dtype: float32
63
+ inverse: false
64
+ patch_transforms: None
65
+ is_input: true
66
+ augmentations:
67
+ DataAugmentation_0:
68
+ data_augmentations:
69
+ Flip:
70
+ f_prob:
71
+ - 0
72
+ - 0.5
73
+ - 0.5
74
+ vector_field: true
75
+ prob: 1
76
+ nb: 2
77
+ Patch:
78
+ patch_size: None
79
+ overlap: None
80
+ mask: None
81
+ pad_value: None
82
+ extend_slice: 0
83
+ subset: None
84
+ filter: None
85
+ dataset_filenames:
86
+ - ./Dataset/:mha
87
+ use_cache: false
88
+ batch_size: 1
89
+ num_workers: None
90
+ pin_memory: false
91
+ prefetch_factor: None
92
+ persistent_workers: None
93
+ outputs_dataset:
94
+ MovedImage:
95
+ OutputDataset:
96
+ name_class: OutSameAsGroupDataset
97
+ before_reduction_transforms: None
98
+ after_reduction_transforms: None
99
+ final_transforms:
100
+ TensorCast:
101
+ dtype: float32
102
+ inverse: false
103
+ dataset_filename: Moved:mha
104
+ group: Moved
105
+ same_as_group: Volume_0:Fixed
106
+ patch_combine: None
107
+ inverse_transform: false
108
+ reduction: Mean
109
+ Mean: {}
110
+ DisplacementField:
111
+ OutputDataset:
112
+ name_class: OutSameAsGroupDataset
113
+ before_reduction_transforms: None
114
+ after_reduction_transforms: None
115
+ final_transforms:
116
+ TensorCast:
117
+ dtype: float32
118
+ inverse: false
119
+ dataset_filename: DVF:mha
120
+ group: DVF
121
+ same_as_group: Volume_0:Fixed
122
+ patch_combine: None
123
+ inverse_transform: false
124
+ reduction: Mean
125
+ Mean: {}
126
+ train_name: ImpactReg-FireANTs-SyN
127
+ manual_seed: 42
128
+ gpu_checkpoints: None
129
+ images_log: None
130
+ combine: Mean
131
+ autocast: false
132
+ data_log: None
FireANTs_SyN/Uncertainty.yml ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Evaluator:
2
+ metrics:
3
+ Uncertainty:
4
+ targets_criterions:
5
+ None:
6
+ criterions_loader:
7
+ Mean:
8
+ name: Uncertainty
9
+ Dataset:
10
+ groups_src:
11
+ Volume_0:
12
+ groups_dest:
13
+ Uncertainty:
14
+ transforms:
15
+ Norm: {}
16
+ StandardDeviation: {}
17
+ Save:
18
+ dataset: ./Uncertainties/ImpactReg/Output:mha
19
+ group: None
20
+ subset: None
21
+ dataset_filenames:
22
+ - ./Dataset:mha
23
+ validation: None
24
+ train_name: ImpactReg
FireANTs_SyN/app.json ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "display_name": "FireANTs (SyN)",
3
+ "short_description": "Rigid + Affine + SyN diffeomorphic registration on GPU (FireANTs).",
4
+ "description": "GPU deformable registration with FireANTs: a Rigid (MI) + Affine (MI) linear pre-align followed by a symmetric diffeomorphic SyN stage (CC), all optimised with Riemannian Adam. Produces the moved image and the total displacement field on the fixed grid. FireANTs is an external dependency under the FireANTs License v1.0 (see NOTICE).",
5
+ "task": "registration",
6
+ "tta": 0,
7
+ "mc_dropout": 0,
8
+ "models": [
9
+ "model.pt"
10
+ ],
11
+ "inputs": {
12
+ "Fixed": {
13
+ "display_name": "Fixed image",
14
+ "volume_type": "VOLUME",
15
+ "required": true
16
+ },
17
+ "Moving": {
18
+ "display_name": "Moving image",
19
+ "volume_type": "VOLUME",
20
+ "required": true
21
+ },
22
+ "FixedMask": {
23
+ "display_name": "Fixed mask (optional)",
24
+ "volume_type": "SEGMENTATION",
25
+ "required": false,
26
+ "default": "ones"
27
+ },
28
+ "MovingMask": {
29
+ "display_name": "Moving mask (optional)",
30
+ "volume_type": "SEGMENTATION",
31
+ "required": false,
32
+ "default": "ones"
33
+ }
34
+ },
35
+ "outputs": {
36
+ "MovedImage": {
37
+ "display_name": "Moved image",
38
+ "volume_type": "VOLUME",
39
+ "required": true
40
+ },
41
+ "DisplacementField": {
42
+ "display_name": "Displacement field",
43
+ "volume_type": "VOLUME",
44
+ "required": false
45
+ }
46
+ },
47
+ "inputs_evaluations": {
48
+ "Image": {
49
+ "Evaluation_with_images.yml": {
50
+ "FixedImage": {
51
+ "display_name": "Fixed image",
52
+ "volume_type": "VOLUME",
53
+ "required": true
54
+ },
55
+ "MovingImage": {
56
+ "display_name": "Moving image",
57
+ "volume_type": "VOLUME",
58
+ "required": true
59
+ },
60
+ "Mask": {
61
+ "display_name": "Evaluation mask",
62
+ "volume_type": "SEGMENTATION",
63
+ "required": false
64
+ }
65
+ }
66
+ },
67
+ "Segmentation": {
68
+ "Evaluation_with_seg.yml": {
69
+ "FixedSeg": {
70
+ "display_name": "Fixed segmentation",
71
+ "volume_type": "SEGMENTATION",
72
+ "required": true
73
+ },
74
+ "MovingSeg": {
75
+ "display_name": "Moving segmentation",
76
+ "volume_type": "SEGMENTATION",
77
+ "required": true
78
+ }
79
+ }
80
+ },
81
+ "Landmarks": {
82
+ "Evaluation_with_fid.yml": {
83
+ "FixedFid": {
84
+ "display_name": "Fixed landmarks",
85
+ "volume_type": "FIDUCIALS",
86
+ "required": true
87
+ },
88
+ "MovingFid": {
89
+ "display_name": "Moving landmarks",
90
+ "volume_type": "FIDUCIALS",
91
+ "required": true
92
+ }
93
+ }
94
+ }
95
+ }
96
+ }
FireANTs_SyN/model.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:de99fbc36331ce674639acc774f52b4a2d0027f2f312d9d28669e831a0c4fd7e
3
+ size 1249
FireANTs_SyN/requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ fireants
Generic_Rigid/app.json CHANGED
@@ -22,12 +22,14 @@
22
  "FixedMask": {
23
  "display_name": "Fixed mask (optional)",
24
  "volume_type": "SEGMENTATION",
25
- "required": false
 
26
  },
27
  "MovingMask": {
28
  "display_name": "Moving mask (optional)",
29
  "volume_type": "SEGMENTATION",
30
- "required": false
 
31
  }
32
  },
33
  "outputs": {
 
22
  "FixedMask": {
23
  "display_name": "Fixed mask (optional)",
24
  "volume_type": "SEGMENTATION",
25
+ "required": false,
26
+ "default": "ones"
27
  },
28
  "MovingMask": {
29
  "display_name": "Moving mask (optional)",
30
  "volume_type": "SEGMENTATION",
31
+ "required": false,
32
+ "default": "ones"
33
  }
34
  },
35
  "outputs": {
Generic_Rigid/elastix_engine.py CHANGED
@@ -44,6 +44,17 @@ from Model import _sorted_specs, generate_impact_parameter_map, load_models_regi
44
  ELASTIX_CACHE = Path.home() / ".cache" / "konfai" / "elastix-impact"
45
 
46
 
 
 
 
 
 
 
 
 
 
 
 
47
  class ElastixEngine:
48
  """Run the elastix-IMPACT binary on a fixed/moving pair; return (moved, dvf) on the fixed grid.
49
 
@@ -235,7 +246,7 @@ class ElastixEngine:
235
 
236
  args = [str(self._elastix_bin), "-f", str(fixed_path), "-m", str(moving_path)]
237
  for flag, mask, name in (("-fMask", fixed_mask, "FixedMask.mha"), ("-mMask", moving_mask, "MovingMask.mha")):
238
- if mask is not None:
239
  mask_path = work / name
240
  sitk.WriteImage(sitk.Cast(mask, sitk.sitkUInt8), str(mask_path))
241
  args += [flag, str(mask_path)]
 
44
  ELASTIX_CACHE = Path.home() / ".cache" / "konfai" / "elastix-impact"
45
 
46
 
47
+ def _is_partial_mask(mask: "sitk.Image | None") -> bool:
48
+ """True only for a mask that actually restricts the metric region — some voxels in, some out. An
49
+ absent optional mask arrives as a whole-image (all-ones) default from KonfAI, and an all-zero mask
50
+ is degenerate; both are treated as no mask, so elastix runs without ``-fMask`` / ``-mMask`` (i.e.
51
+ the whole image) instead of paying for a mask that restricts nothing."""
52
+ if mask is None:
53
+ return False
54
+ arr = sitk.GetArrayViewFromImage(mask)
55
+ return bool((arr > 0).any()) and bool((arr == 0).any())
56
+
57
+
58
  class ElastixEngine:
59
  """Run the elastix-IMPACT binary on a fixed/moving pair; return (moved, dvf) on the fixed grid.
60
 
 
246
 
247
  args = [str(self._elastix_bin), "-f", str(fixed_path), "-m", str(moving_path)]
248
  for flag, mask, name in (("-fMask", fixed_mask, "FixedMask.mha"), ("-mMask", moving_mask, "MovingMask.mha")):
249
+ if _is_partial_mask(mask):
250
  mask_path = work / name
251
  sitk.WriteImage(sitk.Cast(mask, sitk.sitkUInt8), str(mask_path))
252
  args += [flag, str(mask_path)]
Generic_Rigid/model.pt CHANGED
Binary files a/Generic_Rigid/model.pt and b/Generic_Rigid/model.pt differ
 
Generic_Rigid_BSpline/app.json CHANGED
@@ -22,12 +22,14 @@
22
  "FixedMask": {
23
  "display_name": "Fixed mask (optional)",
24
  "volume_type": "SEGMENTATION",
25
- "required": false
 
26
  },
27
  "MovingMask": {
28
  "display_name": "Moving mask (optional)",
29
  "volume_type": "SEGMENTATION",
30
- "required": false
 
31
  }
32
  },
33
  "outputs": {
 
22
  "FixedMask": {
23
  "display_name": "Fixed mask (optional)",
24
  "volume_type": "SEGMENTATION",
25
+ "required": false,
26
+ "default": "ones"
27
  },
28
  "MovingMask": {
29
  "display_name": "Moving mask (optional)",
30
  "volume_type": "SEGMENTATION",
31
+ "required": false,
32
+ "default": "ones"
33
  }
34
  },
35
  "outputs": {
Generic_Rigid_BSpline/elastix_engine.py CHANGED
@@ -44,6 +44,17 @@ from Model import _sorted_specs, generate_impact_parameter_map, load_models_regi
44
  ELASTIX_CACHE = Path.home() / ".cache" / "konfai" / "elastix-impact"
45
 
46
 
 
 
 
 
 
 
 
 
 
 
 
47
  class ElastixEngine:
48
  """Run the elastix-IMPACT binary on a fixed/moving pair; return (moved, dvf) on the fixed grid.
49
 
@@ -235,7 +246,7 @@ class ElastixEngine:
235
 
236
  args = [str(self._elastix_bin), "-f", str(fixed_path), "-m", str(moving_path)]
237
  for flag, mask, name in (("-fMask", fixed_mask, "FixedMask.mha"), ("-mMask", moving_mask, "MovingMask.mha")):
238
- if mask is not None:
239
  mask_path = work / name
240
  sitk.WriteImage(sitk.Cast(mask, sitk.sitkUInt8), str(mask_path))
241
  args += [flag, str(mask_path)]
 
44
  ELASTIX_CACHE = Path.home() / ".cache" / "konfai" / "elastix-impact"
45
 
46
 
47
+ def _is_partial_mask(mask: "sitk.Image | None") -> bool:
48
+ """True only for a mask that actually restricts the metric region — some voxels in, some out. An
49
+ absent optional mask arrives as a whole-image (all-ones) default from KonfAI, and an all-zero mask
50
+ is degenerate; both are treated as no mask, so elastix runs without ``-fMask`` / ``-mMask`` (i.e.
51
+ the whole image) instead of paying for a mask that restricts nothing."""
52
+ if mask is None:
53
+ return False
54
+ arr = sitk.GetArrayViewFromImage(mask)
55
+ return bool((arr > 0).any()) and bool((arr == 0).any())
56
+
57
+
58
  class ElastixEngine:
59
  """Run the elastix-IMPACT binary on a fixed/moving pair; return (moved, dvf) on the fixed grid.
60
 
 
246
 
247
  args = [str(self._elastix_bin), "-f", str(fixed_path), "-m", str(moving_path)]
248
  for flag, mask, name in (("-fMask", fixed_mask, "FixedMask.mha"), ("-mMask", moving_mask, "MovingMask.mha")):
249
+ if _is_partial_mask(mask):
250
  mask_path = work / name
251
  sitk.WriteImage(sitk.Cast(mask, sitk.sitkUInt8), str(mask_path))
252
  args += [flag, str(mask_path)]
Generic_Rigid_BSpline/model.pt CHANGED
Binary files a/Generic_Rigid_BSpline/model.pt and b/Generic_Rigid_BSpline/model.pt differ
 
MR_CT_HeadNeck/app.json CHANGED
@@ -22,12 +22,14 @@
22
  "FixedMask": {
23
  "display_name": "Fixed mask (optional)",
24
  "volume_type": "SEGMENTATION",
25
- "required": false
 
26
  },
27
  "MovingMask": {
28
  "display_name": "Moving mask (optional)",
29
  "volume_type": "SEGMENTATION",
30
- "required": false
 
31
  }
32
  },
33
  "outputs": {
 
22
  "FixedMask": {
23
  "display_name": "Fixed mask (optional)",
24
  "volume_type": "SEGMENTATION",
25
+ "required": false,
26
+ "default": "ones"
27
  },
28
  "MovingMask": {
29
  "display_name": "Moving mask (optional)",
30
  "volume_type": "SEGMENTATION",
31
+ "required": false,
32
+ "default": "ones"
33
  }
34
  },
35
  "outputs": {