@@ -17,8 +17,7 @@ def rename(inname, outDir, extension_addition, extension_change=''):
1717 """
1818 Takes inname path and replaces dir with outdir and adds extension before file type
1919 """
20- spt = inname .rsplit (os .sep , 1 )
21- initPath = spt [0 ]
20+ initPath = os .path .dirname (inname )
2221 outname = inname .replace (initPath , outDir )
2322 current_extension = "." + inname .split ("." )[- 1 ]
2423 if extension_addition != '' :
@@ -68,8 +67,9 @@ def getOrigin(inname):
6867 text = origin_file .read ()
6968 origin = text .split ("\n " )
7069 origin_file .close ()
71- cmd = ["rm" ,infoPrefix + "_origin.txt" ,infoPrefix + "_spacing.txt" , infoPrefix + "_size.txt" ]
72- subprocess .check_call (cmd )
70+ os .remove (infoPrefix + "_origin.txt" )
71+ os .remove (infoPrefix + "_spacing.txt" )
72+ os .remove (infoPrefix + "_size.txt" )
7373 return origin
7474
7575def center (outDir , inDataList ):
@@ -130,10 +130,10 @@ def applyCOMAlignment(outDir, inDataListSeg, raw=[]):
130130 os .makedirs (outDir )
131131 if raw :
132132 inDataListImg = raw
133- rawoutDir = outDir + '/ images'
133+ rawoutDir = os . path . join ( outDir , ' images')
134134 if not os .path .exists (rawoutDir ):
135135 os .makedirs (rawoutDir )
136- binaryoutDir = outDir + '/ segmentations'
136+ binaryoutDir = os . path . join ( outDir , ' segmentations')
137137 if not os .path .exists (binaryoutDir ):
138138 os .makedirs (binaryoutDir )
139139 outDataListSeg = []
@@ -223,20 +223,19 @@ def applyRigidAlignment(parentDir, inDataListSeg, inDataListImg, refFile, antial
223223 then it also applys the same transformation on the corresponding list of
224224 raw files (MRI/CT ...)
225225 """
226- outDir = parentDir + '/ aligned'
227- transoutDir = outDir + '/ transformations'
226+ outDir = os . path . join ( parentDir , ' aligned')
227+ transoutDir = os . path . join ( outDir , ' transformations')
228228
229229 if not os .path .exists (outDir ):
230230 os .makedirs (outDir )
231231 if not os .path .exists (transoutDir ):
232232 os .makedirs (transoutDir )
233233
234234 # identify the reference scan
235- refDir = outDir + '/ reference'
235+ refDir = os . path . join ( outDir , ' reference')
236236 if not os .path .exists (refDir ):
237237 os .makedirs (refDir )
238- spt = refFile .rsplit (os .sep , 1 )
239- initPath = spt [0 ]
238+ initPath = os .path .dirname (refFile )
240239 newRefFile = refFile .replace (initPath , refDir )
241240
242241 ref_dtnrrdfilename = newRefFile .replace ('.nrrd' , '.DT.nrrd' )
@@ -266,8 +265,8 @@ def applyRigidAlignment(parentDir, inDataListSeg, inDataListImg, refFile, antial
266265
267266
268267 if processRaw :
269- rawoutDir = outDir + '/ images/'
270- binaryoutDir = outDir + '/ segmentations'
268+ rawoutDir = os . path . join ( outDir , ' images' )
269+ binaryoutDir = os . path . join ( outDir + 'segmentations' )
271270
272271 if not os .path .exists (rawoutDir ):
273272 os .makedirs (rawoutDir )
@@ -279,19 +278,17 @@ def applyRigidAlignment(parentDir, inDataListSeg, inDataListImg, refFile, antial
279278 outSegDataList = []
280279 for i in range (len (inDataListSeg )):
281280 seginname = inDataListSeg [i ]
282- spt = seginname .rsplit (os .sep , 1 )
283- initPath = spt [0 ]
284- filename = spt [1 ]
281+ initPath = os .path .dirname (seginname )
282+ filename = os .path .basename (seginname )
285283 segoutname = seginname .replace (initPath , binaryoutDir )
286284 segoutname = segoutname .replace ('.nrrd' , '.aligned.nrrd' )
287285 transoutname = seginname .replace (initPath , transoutDir )
288286 transformation = transoutname .replace ('.nrrd' , '.transformationMatrix.txt' )
289287 outSegDataList .append (segoutname )
290288
291289 rawinname = inDataListImg [i ]
292- sptR = rawinname .rsplit (os .sep , 1 )
293- initPath = sptR [0 ] + os .sep
294- filename = sptR [1 ]
290+ initPath = os .path .dirname (rawinname )
291+ filename = os .path .basename (rawinname )
295292 rawoutname = rawinname .replace (initPath , rawoutDir )
296293 rawoutname = rawoutname .replace ('.nrrd' , '.aligned.nrrd' )
297294 outRawDataList .append (rawoutname )
@@ -336,9 +333,7 @@ def applyRigidAlignment(parentDir, inDataListSeg, inDataListImg, refFile, antial
336333 outDataList = []
337334 for i in range (len (inDataListSeg )):
338335 inname = inDataListSeg [i ]
339- spt = inname .rsplit (os .sep , 1 )
340- initPath = spt [0 ]
341- #filename = spt[1]
336+ initPath = os .path .dirname (inname )
342337 outname = inname .replace (initPath , outDir )
343338 outname = outname .replace ('.nrrd' , '.aligned.nrrd' )
344339 transoutname = inname .replace (initPath , transoutDir )
@@ -383,18 +378,18 @@ def applyCropping(parentDir, inDataListSeg, inDataListImg, paddingSize=10, proce
383378 This function takes in a filelist and crops them according to the largest
384379 bounding box which it discovers
385380 """
386- outDir = parentDir + '/ cropped'
381+ outDir = os . path . join ( parentDir , ' cropped')
387382 if not os .path .exists (outDir ):
388383 os .makedirs (outDir )
389- cropinfoDir = outDir + '/ crop_info/'
384+ cropinfoDir = os . path . join ( outDir , ' crop_info' )
390385 if not os .path .exists (cropinfoDir ):
391386 os .makedirs (cropinfoDir )
392387 # first create a txtfile with all the scan names in it.
393- txtfile = cropinfoDir + "_dataList.txt"
388+ txtfile = os . path . join ( cropinfoDir , "_dataList.txt" )
394389 with open (txtfile , 'w' ) as filehandle :
395390 for listitem in inDataListSeg :
396391 filehandle .write ('%s\n ' % listitem )
397- outPrefix = cropinfoDir + "largest_bounding_box"
392+ outPrefix = os . path . join ( cropinfoDir , "largest_bounding_box" )
398393 execCommand = ["FindLargestBoundingBox" , "--paddingSize" , str (
399394 paddingSize ), "--inFilename" , txtfile , "--outPrefix" , outPrefix ]
400395 subprocess .check_call (execCommand )
@@ -406,8 +401,8 @@ def applyCropping(parentDir, inDataListSeg, inDataListImg, paddingSize=10, proce
406401 smI1 = np .loadtxt (outPrefix + "_smallestIndex1.txt" )
407402 smI2 = np .loadtxt (outPrefix + "_smallestIndex2.txt" )
408403 if processRaw :
409- rawoutDir = outDir + '/ images/'
410- binaryoutDir = outDir + '/ segmentations/'
404+ rawoutDir = os . path . join ( outDir , ' images' )
405+ binaryoutDir = os . path . join ( outDir , ' segmentations' )
411406 if not os .path .exists (rawoutDir ):
412407 os .makedirs (rawoutDir )
413408 if not os .path .exists (binaryoutDir ):
@@ -417,15 +412,11 @@ def applyCropping(parentDir, inDataListSeg, inDataListImg, paddingSize=10, proce
417412 for i in range (len (inDataListSeg )):
418413 innameSeg = inDataListSeg [i ]
419414 innameImg = inDataListImg [i ]
420- sptSeg = innameSeg .rsplit (os .sep , 1 )
421- initPath = sptSeg [0 ] + os .sep
422- #filename = sptSeg[1]
415+ initPath = os .path .dirname (innameSeg )
423416 outnameSeg = innameSeg .replace (initPath , binaryoutDir )
424417 outnameSeg = outnameSeg .replace ('.nrrd' , '.cropped.nrrd' )
425418 outDataListSeg .append (outnameSeg )
426- sptImg = innameImg .rsplit (os .sep , 1 )
427- initPath = sptImg [0 ] + os .sep
428- #filename = sptImg[1]
419+ initPath = os .path .dirname (innameImg )
429420 outnameImg = innameImg .replace (initPath , rawoutDir )
430421 outnameImg = outnameImg .replace ('.nrrd' , '.cropped.nrrd' )
431422 outDataListImg .append (outnameImg )
@@ -447,9 +438,7 @@ def applyCropping(parentDir, inDataListSeg, inDataListImg, paddingSize=10, proce
447438 outDataList = []
448439 for i in range (len (inDataListSeg )):
449440 inname = inDataListSeg [i ]
450- spt = inname .rsplit (os .sep , 1 )
451- initPath = spt [0 ]
452- #filename = spt[1]
441+ initPath = os .path .dirname (inname )
453442 outname = inname .replace (initPath , outDir )
454443 outname = outname .replace ('.nrrd' , '.cropped.nrrd' )
455444 outDataList .append (outname )
@@ -476,11 +465,11 @@ def create_meshfromDT_xml(xmlfilename, tpdtnrrdfilename, vtkfilename):
476465 file .close ()
477466
478467def applyDistanceTransforms (parentDir , inDataList ,antialiasIterations = 20 , smoothingIterations = 1 , isoValue = 0 , percentage = 50 ):
479- outDir = parentDir + '/ groom_and_meshes'
468+ outDir = os . path . join ( parentDir , ' groom_and_meshes')
480469 if not os .path .exists (outDir ):
481470 os .makedirs (outDir )
482471
483- finalDTDir = parentDir + '/ distance_transforms'
472+ finalDTDir = os . path . join ( parentDir , ' distance_transforms')
484473 if not os .path .exists (finalDTDir ):
485474 os .makedirs (finalDTDir )
486475
@@ -524,21 +513,21 @@ def applyDistanceTransforms(parentDir, inDataList,antialiasIterations=20, smooth
524513def anatomyPairsToSingles (outDir , seg_list , img_list , reference_side ):
525514 if not os .path .exists (outDir ):
526515 os .makedirs (outDir )
527- outSegDir = outDir + "/ segmentations"
516+ outSegDir = os . path . join ( outDir , " segmentations")
528517 if not os .path .exists (outSegDir ):
529518 os .mkdir (outSegDir )
530- outImgDir = outDir + "/ images"
519+ outImgDir = os . path . join ( outDir , " images")
531520 if not os .path .exists (outImgDir ):
532521 os .mkdir (outImgDir )
533522 imageList = []
534523 meshList = []
535524 for img in img_list :
536- img_name = img . rsplit ( os .sep , 1 )[ 1 ]
525+ img_name = os .path . basename ( img )
537526 prefix = img_name .split ("_" )[0 ]
538- if reference_side == 'right' :
527+ if reference_side == 'right' :
539528 ref = 'R'
540529 flip = 'L'
541- elif reference_side == 'left' :
530+ elif reference_side == 'left' :
542531 ref = 'L'
543532 flip = 'R'
544533 else :
@@ -555,10 +544,10 @@ def anatomyPairsToSingles(outDir, seg_list, img_list, reference_side):
555544 flip_seg = seg
556545 # if we have ref seg, copy image and seg over with appropriate name
557546 if ref_seg != 'None' :
558- seg_out = ref_seg .replace (ref_seg . rsplit ( os .sep , 1 )[ 0 ] , outSegDir )
547+ seg_out = ref_seg .replace (os .path . dirname ( ref_seg ) , outSegDir )
559548 meshList .append (seg_out )
560549 shutil .copy (ref_seg , seg_out )
561- img_out = img .replace (img . rsplit ( os .sep , 1 )[ 0 ] , outImgDir )
550+ img_out = img .replace (os .path . dirname ( img ) , outImgDir )
562551 img_out = img_out .replace (prefix , ref_prefix )
563552 imageList .append (img_out )
564553 shutil .copy (img , img_out )
@@ -567,7 +556,7 @@ def anatomyPairsToSingles(outDir, seg_list, img_list, reference_side):
567556 print ("\n ############## Reflecting ###############" )
568557 img_out = rename (img , outImgDir , 'reflect' ).replace (prefix , flip_prefix )
569558 imageList .append (img_out )
570- centerFilename = outDir + "/" + prefix + "_origin.txt"
559+ centerFilename = os . path . join ( outDir , prefix + "_origin.txt" )
571560 execCommand = ["ReflectVolumes" , "--inFilename" , img , "--outFilename" , img_out , "--centerFilename" , centerFilename , "--inputDirection" , "0" ]
572561 subprocess .check_call (execCommand )
573562 print ("\n ############## Reflecting ###############" )
@@ -584,7 +573,7 @@ def MeshesToVolumes(outDir, meshList, imgList):
584573 if not os .path .exists (outDir ):
585574 os .mkdir (outDir )
586575 for mesh in meshList :
587- mesh_name = mesh . rsplit ( os .sep , 1 )[ 1 ]
576+ mesh_name = os .path . basename ( mesh )
588577 extension = mesh_name .split ("." )[- 1 ]
589578 prefix = mesh_name .split ("_" )[0 ] + "_" + mesh_name .split ("_" )[1 ]
590579 # change to ply if needed
@@ -603,7 +592,7 @@ def MeshesToVolumes(outDir, meshList, imgList):
603592 if prefix in image_file :
604593 image = image_file
605594 # write origin, size, and spacing info to text file
606- infoPrefix = outDir + '/' + prefix
595+ infoPrefix = os . path . join ( outDir , prefix )
607596 execCommand = ["WriteImageInfoToText" ,"--inFilename" ,image , "--outPrefix" , infoPrefix ]
608597 subprocess .check_call (execCommand )
609598 # get origin, size, and spacing data
@@ -688,7 +677,7 @@ def SelectCuttingPlane(input_file):
688677 if file_format == "nrrd" :
689678 print ("\n Creating mesh from: " + input_file )
690679 print ("\n Saving as: " + input_vtk )
691- xml_filename = input_file . rsplit (os .sep , 1 )[ 0 ] + "/ cutting_plane_nrrd2vtk.xml"
680+ xml_filename = os . path . join (os .path . dirname ( input_file ), " cutting_plane_nrrd2vtk.xml")
692681 create_meshfromDT_xml (xml_filename , input_file , input_vtk )
693682 execCommand = ["MeshFromDistanceTransforms" , xml_filename ]
694683 subprocess .check_call (execCommand )
0 commit comments