Skip to content

Commit

Permalink
Define Run directory for elastix
Browse files Browse the repository at this point in the history
Ooops. Apparently for linux you need to do things like tell it where the code that it should run is. Maybe learn to think independently?
  • Loading branch information
MarkGardnerUSyd committed Aug 14, 2024
1 parent 0eaf283 commit 5d36b62
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
5 changes: 4 additions & 1 deletion CBCTCardiacSegmentation/CreateCBCTSegmentations.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@


def CreateCBCTSegmentations(CBCTDir,OutputDir='./CBCTSegmentations',SegmentationMethod='Synthetic',PlanningCTDir='',ElastixParamDir='',StructFile='',
ElastixRunDir = '',
HeartPadDistance=30,
KeepTempFiles=False):

Expand Down Expand Up @@ -68,7 +69,8 @@ def CreateCBCTSegmentations(CBCTDir,OutputDir='./CBCTSegmentations',Segmentation
Register Images to prepare for Segmentation
"""
if not(SegmentationMethod == 'Direct'):
VolReg = VolumeRegistration(RegOutputDir=TempDir,ParamDir=ElastixParamDir)
VolReg = VolumeRegistration(RegOutputDir=TempDir,ParamDir=ElastixParamDir,
ElastixDir=ElastixRunDir)

#Align PlanningCT Volume to CBCT Volume
print('Initial Alignment of Planning CT File to CBCT File')
Expand Down Expand Up @@ -179,6 +181,7 @@ def CreateCBCTSegmentations(CBCTDir,OutputDir='./CBCTSegmentations',Segmentation
parser.add_argument('--SegmentationMethod',default='Synthetic',type=str,help='The method used to segment the CBCT image {Synthetic|Transform|Direct}. Default is Synthetic')
parser.add_argument('--PlanningCTDir',type=str, default='',help='Location of the Planning CT that is to be segmented. Can be a Dicom Series or Volume file. Default is None')
parser.add_argument('--ElastixParamDir',type=str,default='',help='Location of the elastix parameter files used for the registrations. Default is the parameters is the provided ElastixParameterFiles')
parser.add_argument('--ElastixRunDir',type=str,default='',help='Location of the bin and lib directories from the installed/downaloded version of elastix to be used. Needed for linux implementations')
parser.add_argument('--StructFile',type=str,default='',help='Location of the structures contoured from the planning CT File. Default is None')
parser.add_argument('--KeepTempFiles',action='store_true',help='If true the temporary files and directories created will not be deleted. If False these files will be deleted.')

Expand Down
9 changes: 7 additions & 2 deletions CBCTCardiacSegmentation/Registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class VolumeRegistration:

def __init__(self, RegOutputDir='', ParamDir='', RigidParamFile='', NonRigidParamFile='', InverseParamFile='',
RigidNonRigidParamFile='',TranslationParamFile='',GPUEnabled=False):
RigidNonRigidParamFile='',TranslationParamFile='',ElastixDir='',GPUEnabled=False):

if not ParamDir:
#If no elastix parameter directory defined then get the one
Expand All @@ -26,6 +26,11 @@ def __init__(self, RegOutputDir='', ParamDir='', RigidParamFile='', NonRigidPara

self.RegOutputDir = RegOutputDir

if platform.system() == 'Linux':
assert not(not ElastixDir),'Directory to elastix bin/lib files needs to be added for linux'

self.ElastixDir = ElastixDir

if not ParamDir:
self.RigidParamFile = RigidParamFile
self.NonRigidParamFile = NonRigidParamFile
Expand Down Expand Up @@ -157,7 +162,7 @@ def TransformVolume(self, ParamFile, OutputFile, FixedVolume):
def CallFunctionFromCmdLine(self,CmdStr):
# Run command from terminal
if platform.system() == 'Linux':
run(CmdStr, shell=True, env={"PATH": '/home/mark/elastix/bin', "LD_LIBRARY_PATH": '/home/mark/elastix/lib'})
run(CmdStr, shell=True, env={"PATH": os.path.join(self.ElastixDir,'bin'), "LD_LIBRARY_PATH": os.path.join(self.ElastixDir,'lib')})
else:
run(CmdStr)

Expand Down
11 changes: 8 additions & 3 deletions tests/test_CBCT_Segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def test_BadSegMethodInput():

rmtree(data_path)
rmtree(OutputDir)

def test_NiftiCBCTSegmentationGeneration():

# Download the test data
Expand All @@ -131,6 +131,10 @@ def test_NiftiCBCTSegmentationGeneration():
ElastixParamDir = ''
StructFile = ''

#Define Elastix directories
home_dir = Path(os.path.expanduser('~')) # may have to update for github system
elastix_dir = home_dir / 'ElastixDownload' / 'elastix-5.0.1-linux'

for SegMethod in SegmentationMethods:
print(SegMethod)
OutputDir = './CBCTSegmentations'
Expand All @@ -139,7 +143,8 @@ def test_NiftiCBCTSegmentationGeneration():
SegmentationMethod=SegMethod,
PlanningCTDir=PlanningCTDir,
ElastixParamDir=ElastixParamDir,
StructFile=StructFile)
StructFile=StructFile,
ElastixRunDir=elastix_dir)

CardiacSegList = ['Heart','A_Aorta','A_Cflx','A_Coronary_L','A_Coronary_R','A_LAD','A_Pulmonary','Atrium_L',
'Atrium_R','CN_Atrioventricular','CN_Sinoatrial','V_Venacava_S','Valve_Aortic','Valve_Mitral',
Expand Down Expand Up @@ -210,4 +215,4 @@ def test_NiftiToDicomStruct():
assert Path(OutputStructFile).exists(),'Output Struct {} was not created successfully'.format(OutputStructFile)

rmtree(data_path)
rmtree(OutputDir)
rmtree(OutputDir)

0 comments on commit 5d36b62

Please sign in to comment.