File: elx_get_tp_list.py

package info (click to toggle)
elastix 5.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 56,288 kB
  • sloc: cpp: 61,192; lisp: 4,121; python: 777; sh: 227; xml: 182; makefile: 35
file content (88 lines) | stat: -rw-r--r-- 3,251 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import sys
import os
import re
import os.path
import glob
import shutil
from optparse import OptionParser

#-------------------------------------------------------------------------------
# the main function
# cd bin_VS2010
# ctest -C Release
# cd Testing
# python ../../Testing/elx_get_tp_list.py -l elastix_run* -o ../../Testing/Baselines
# cd ..
# cmake .
# ctest -C Release -R COMPARE_TP
# cd ..
# git add Testing/Baselines/TransformParameters_*
# git commit -m "ENH: updating baselines after recent change X"
# git push
def main():
    # usage, parse parameters
    usage = "usage: %prog [options] arg";
    parser = OptionParser( usage );

    # option to debug and verbose
    parser.add_option( "-v", "--verbose",
      action="store_true", dest="verbose" );

    # options to control files
    parser.add_option( "-l", "--list", type="string", dest="directoryList", help="list of elastix output directories" );
    parser.add_option( "-o", "--output", type="string", dest="outputDirectory", help="output base directory" );

    (options, args) = parser.parse_args();

    # Check if option -l is given
    if options.directoryList == None :
      parser.error( "The option directory list (-l) should be given" );

    # Check if option -d is given
    if options.outputDirectory == None :
      parser.error( "The option output directory (-o) should be given" );

    if not os.path.exists( options.outputDirectory ) :
      print( "The output directory " + options.outputDirectory + " does not exist. Create it before running" );

    # Convert string -l to list
    dirList = options.directoryList.split( ' ' );
    # Add everything not processed
    dirList.extend( args );

    for directory in dirList :
      # Skip directories with "Threads" in the name
      if "Threads" in directory : continue;

      # Find the largest TransformParameters.?.txt
      inputFileNames = glob.glob( os.path.join( directory, "TransformParameters.?.txt" ) );
      inputFileNames.sort( reverse = True );
      inputFileName = inputFileNames[0];

      # Create a name for the output file name
      directory = re.sub( '\/$', '', directory );
      dir_part = list( os.path.split( directory ) ).pop();
      dir_part = dir_part.replace( "elastix_run_", "", 1 );
      outputFileName = "TransformParameters_" + dir_part + ".txt.in";
      outputFileName = os.path.join( options.outputDirectory, outputFileName );
      print( outputFileName );

      # Copy the results as the new baselines, while replacing
      # the initial transform to point to the correct path.
      f1 = open( inputFileName, 'rU' );
      f2 = open( outputFileName, 'w' );
      for oldline in f1 :
        newline = oldline;
        if "InitialTransformParametersFileName" in oldline :
          oldFileName = os.path.basename( oldline.split()[1][1:-2] );
          newFileName = os.path.join( "@ELASTIX_DATA_DIR@", oldFileName );
          if not "NoInitialTransform" in oldFileName :
            newline = "(InitialTransformParametersFileName \"" + newFileName + "\")\n";
        f2.write( newline );
      f1.close(); f2.close();

    return 0

#-------------------------------------------------------------------------------
if __name__ == '__main__':
    sys.exit(main())