Skip to content

Commit

Permalink
BUG: Fully remove VRG-related .tra file export (#161)
Browse files Browse the repository at this point in the history
This commit follows up on 5cd70dd ("BUG: Remove obsolete VRG Generation
functionality", 2024-11-10) by removing the unused function `extractSubVolumeForVRG`
and all remaining code associated with `.tra` file calculation and export
during partial volume generation in the pre-processing module.

Specifically, `.tra` file export was previously handled by:
1. `Hierarchical3DRegistrationWidget.onExportButton/onImportButton`, already
   removed in 5cd70dd (referenced above).
2. `AutoscoperMLogic.saveSubVolumesFromSegmentation`, updated in this commit to
   remove `.tra` export support (initially introduced in 7a2dd0f, "ENH: Output
   TRA files on PV gen", 2024-05-14).

This ensures that no VRG feature relics remain in the codebase.
  • Loading branch information
sbelsk authored Feb 27, 2025
1 parent 9a6a713 commit 4920e72
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 121 deletions.
111 changes: 0 additions & 111 deletions AutoscoperM/AutoscoperM.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,6 @@ def onGeneratePartialVolumes(self):
mainOutputDir = self.ui.mainOutputSelector.currentPath
tiffSubDir = self.ui.tiffSubDir.text
tfmSubDir = self.ui.tfmSubDir.text
trackingSubDir = self.ui.trackingSubDir.text
modelSubDir = self.ui.modelSubDir.text
segmentationNode = self.ui.pv_SegNodeComboBox.currentNode()

Expand All @@ -454,15 +453,13 @@ def onGeneratePartialVolumes(self):
mainOutputDir=mainOutputDir,
volumeSubDir=tiffSubDir,
transformSubDir=tfmSubDir,
trackingSubDir=trackingSubDir,
modelSubDir=modelSubDir,
)

self.logic.createPathsIfNotExists(
mainOutputDir,
os.path.join(mainOutputDir, tiffSubDir),
os.path.join(mainOutputDir, tfmSubDir),
os.path.join(mainOutputDir, trackingSubDir),
os.path.join(mainOutputDir, modelSubDir),
)
self.ui.progressBar.setValue(0)
Expand All @@ -473,7 +470,6 @@ def onGeneratePartialVolumes(self):
mainOutputDir,
volumeSubDir=tiffSubDir,
transformSubDir=tfmSubDir,
trackingSubDir=trackingSubDir,
modelSubDir=modelSubDir,
progressCallback=self.updateProgressBar,
)
Expand Down Expand Up @@ -1011,7 +1007,6 @@ def saveSubVolumesFromSegmentation(
outputDir: str,
volumeSubDir: str = "Volumes",
transformSubDir: str = "Transforms",
trackingSubDir: str = "Tracking",
modelSubDir: str = "Models",
progressCallback: Optional[callable] = None,
) -> bool:
Expand Down Expand Up @@ -1040,13 +1035,6 @@ def progressCallback(x):
segmentationNode.GetSegmentation().GetSegmentIDs(segmentIDs)
numSegments = segmentIDs.GetNumberOfValues()

tfmFiles = glob.glob(os.path.join(outputDir, transformSubDir, "*.tfm"))
tfms = [tfm if os.path.basename(tfm).split(".")[0] == "Origin2Dicom" else None for tfm in tfmFiles]
try:
origin2DicomTransformFile = next(item for item in tfms if item is not None)
except StopIteration:
origin2DicomTransformFile = None

for idx in range(numSegments):
segmentID = segmentIDs.GetValue(idx)
segmentName = segmentationNode.GetSegmentation().GetSegment(segmentID).GetName()
Expand Down Expand Up @@ -1086,36 +1074,6 @@ def progressCallback(x):

slicer.mrmlScene.RemoveNode(dicom2autNode)

# Create TRA file
tfm = vtk.vtkMatrix4x4()
tfm.SetElement(0, 3, origin[0])
tfm.SetElement(1, 3, origin[1])
tfm.SetElement(2, 3, origin[2])

if origin2DicomTransformFile is not None:
origin2DicomNode = self.loadTransformFromFile(origin2DicomTransformFile)
origin2DicomNode.Inverse()
tfm = self.applyOrigin2DicomTransform(tfm, origin2DicomNode)
slicer.mrmlScene.RemoveNode(origin2DicomNode)

tfm = self.applyPVol2AutTransform(tfm, pvol2autNode)
slicer.mrmlScene.RemoveNode(pvol2autNode)

tfmR = vtk.vtkMatrix3x3()
vtkAddon.vtkAddonMathUtilities.GetOrientationMatrix(tfm, tfmR)

# Apply RAS to LPS transform
RAS2LPS = vtk.vtkMatrix3x3()
RAS2LPS.SetElement(0, 0, -1)
RAS2LPS.SetElement(1, 1, -1)

vtk.vtkMatrix3x3.Multiply3x3(tfmR, RAS2LPS, tfmR)
vtkAddon.vtkAddonMathUtilities.SetOrientationMatrix(tfmR, tfm)

# Save TRA file
filename = os.path.join(outputDir, trackingSubDir, f"{segmentName}.tra")
IO.writeTRA(filename, [tfm])

# update progress bar
progressCallback((idx + 1) / numSegments * 100)

Expand Down Expand Up @@ -1178,57 +1136,6 @@ def createPathsIfNotExists(*args: tuple) -> None:
if not os.path.exists(arg):
os.makedirs(arg)

@staticmethod
def extractSubVolumeForVRG(
volumeNode: slicer.vtkMRMLVolumeNode,
segmentationNode: slicer.vtkMRMLSegmentationNode,
cameraDebugMode: bool = False,
) -> tuple[vtk.vtkImageData, list[float]]:
"""
Extracts a subvolume from the volumeNode that contains all of the segments in the segmentationNode
:param volumeNode: volume node
:param segmentationNode: segmentation node
:param cameraDebugMode: Whether or not to keep the extracted volume in the scene, defaults to False
:return: tuple containing the extracted volume and the bounds of the volume
"""
mergedSegmentationNode = SubVolumeExtraction.mergeSegments(volumeNode, segmentationNode)
newVolumeNode = SubVolumeExtraction.extractSubVolume(
volumeNode, mergedSegmentationNode, mergedSegmentationNode.GetSegmentation().GetNthSegmentID(0)
)
newVolumeNode.SetName(volumeNode.GetName() + " - Bone Subvolume")

bounds = [0, 0, 0, 0, 0, 0]
newVolumeNode.GetBounds(bounds)

# Copy the metadata from the original volume into the ImageData
newVolumeImageData = vtk.vtkImageData()
newVolumeImageData.DeepCopy(newVolumeNode.GetImageData()) # So we don't modify the original volume
newVolumeImageData.SetSpacing(newVolumeNode.GetSpacing())
origin = list(newVolumeNode.GetOrigin())
origin[0:2] = [x * -1 for x in origin[0:2]]
newVolumeImageData.SetOrigin(origin)

# Ensure we are in the correct orientation (RAS vs LPS)
imageReslice = vtk.vtkImageReslice()
imageReslice.SetInputData(newVolumeImageData)

axes = vtk.vtkMatrix4x4()
axes.Identity()
axes.SetElement(0, 0, -1)
axes.SetElement(1, 1, -1)

imageReslice.SetResliceAxes(axes)
imageReslice.Update()
newVolumeImageData = imageReslice.GetOutput()

if not cameraDebugMode:
slicer.mrmlScene.RemoveNode(newVolumeNode)
slicer.mrmlScene.RemoveNode(mergedSegmentationNode)

return newVolumeImageData, bounds

@staticmethod
def getItemInSequence(sequenceNode: slicer.vtkMRMLSequenceNode, idx: int) -> slicer.vtkMRMLNode:
"""
Expand Down Expand Up @@ -1291,24 +1198,6 @@ def GetVolumeSpacing(node: Union[slicer.vtkMRMLVolumeNode, slicer.vtkMRMLSequenc
return AutoscoperMLogic.getItemInSequence(node, 0)[0].GetSpacing()
return node.GetSpacing()

@staticmethod
def loadTransformFromFile(transformFileName: str) -> slicer.vtkMRMLLinearTransformNode:
return slicer.util.loadNodeFromFile(transformFileName)

@staticmethod
def applyOrigin2DicomTransform(
transform: vtk.vtkMatrix4x4,
origin2DicomTransformNode: slicer.vtkMRMLLinearTransformNode,
) -> vtk.vtkMatrix4x4:
"""Utility function for converting a transform between the dicom centered and
world centered coordinate systems."""
origin2DicomTransformMatrix = vtk.vtkMatrix4x4()
origin2DicomTransformNode.GetMatrixTransformToParent(origin2DicomTransformMatrix)

vtk.vtkMatrix4x4.Multiply4x4(origin2DicomTransformMatrix, transform, transform)

return transform

@staticmethod
def applyPVol2AutTransform(
transform: vtk.vtkMatrix4x4,
Expand Down
10 changes: 0 additions & 10 deletions AutoscoperM/AutoscoperMLib/IO.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import os
from itertools import product

import numpy as np
import slicer
Expand Down Expand Up @@ -202,12 +201,3 @@ def writeTFMFile(filename: str, spacing: list[float], origin: list[float]):
slicer.util.exportNode(transformNode, filename)

slicer.mrmlScene.RemoveNode(transformNode)


def writeTRA(fileName: str, transforms: list[vtk.vtkMatrix4x4]) -> None:
rowWiseStrings = []
for transform in transforms:
rowWiseStrings.append([str(transform.GetElement(i, j)) for i, j in product(range(4), range(4))])
with open(fileName, "w+") as traFile:
for row in rowWiseStrings:
traFile.write(",".join(row) + "\n")
3 changes: 3 additions & 0 deletions AutoscoperM/Resources/UI/AutoscoperM.ui
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,9 @@
<property name="text">
<string>Tracking</string>
</property>
<property name="enabled">
<bool>False</bool>
</property>
</widget>
</item>
<item row="5" column="0">
Expand Down

0 comments on commit 4920e72

Please sign in to comment.