#!/usr/bin/python import sys import ConfigParser import time import datetime import string import csv import os import os.path if len(sys.argv)!=2: print "Wrong Number of Arguments" print "Usage: startEval syntheticdata_configfile" sys.exit(1) # some global constants displacementFieldFilenameExtension = ".mha" analyzeHeaderFilenameExtension = ".hdr" analyzeImageFilenameExtension = ".img" stdOutLogfileExtension = ".log" # a dictionary which stores filename extensions for synthetic transforms syntheticFilenameDictionary = { 'ApplySimulatedBreathingTransformation' : '_simbr_old_', 'ApplySimulatedBreathingTransformationWithIntensityVariation' : '_simbr_', 'ApplySyntheticEBSTransform' : '_grid_', 'ApplySyntheticGaussianRBFTransform' : '_synth_gauss_rbf_', 'ApplySyntheticTPSTransform' : '_synth_tps_', 'ApplyTPSTransform' : '_airway_', 'ApplySharpBreathingTransformation' : '_simbr_sharp_', 'ApplyUniformPeriodicTransform' : '_uniform_periodic_', 'ApplyDisplacementFieldTransform' : '_df_'} configParser = ConfigParser.ConfigParser() configParser.read( sys.argv[1] ) print "Dataset Processing Configuration File: " + sys.argv[1] print "We are in synthetic transformation mode, moving image will be created automatically!" print "Start Processing .." writeDisplacements = configParser.get("Parameters","WriteDisplacements") generateDiagrams = configParser.get("Parameters","generateDiagrams") zipOutputFiles = configParser.get("Parameters","ZipOutputFiles") resampleDefaultValue = configParser.get("Images","resampleDefaultValue") evaluationTag = configParser.get("Identification","evaluationTag") evaluationOnly = configParser.get("Identification", "evaluationOnly") existingEvaluationDate = configParser.get("Identification", "existingEvaluationDate") #print evaluationOnly #print existingEvaluationDate originalImageInputPath = configParser.get("Images","originalImageInputPath") if not os.path.isdir(originalImageInputPath): print "Error: original image input directory'" + originalImageInputPath + "'doesn't exist!" sys.exit(1) originalImageFilename = configParser.get("Images","originalImage") originalImageAbsoluteFilename = originalImageInputPath + originalImageFilename # test if the original image file exists if not os.path.isfile(originalImageAbsoluteFilename): print "Error: original image '" + originalImageAbsoluteFilename + "'doesn't exist!" sys.exit(1) print "Original Image: " + originalImageAbsoluteFilename resultsBasePath = configParser.get( "Filenames", "outputResultsBasePath" ) syntheticDataOutputPath = resultsBasePath + "synthetic/" # test if the syntheticDataOutputPath exists if not os.path.isdir(syntheticDataOutputPath): print syntheticDataOutputPath + " doesn't exist! -> Let's try to create it!" try: os.makedirs(syntheticDataOutputPath) except IOError: print "it was impossible to create directory: " + syntheticDataOutputPath sys.exit(1) if evaluationOnly != "true": # append a subdirectory that identifies the date of the evaluation run: resultsBasePath = resultsBasePath + datetime.date.today().isoformat() + "/" + evaluationTag + "/" else: # append a subdirectory that identifies the date of the already existingevaluation run: resultsBasePath = resultsBasePath + existingEvaluationDate + "/" + evaluationTag + "/" # test if the resultsBasePath exists if not os.path.isdir(resultsBasePath): print resultsBasePath + " doesn't exist! -> Let's try to create it!" try: os.makedirs(resultsBasePath) except IOError: print "it was impossible to create directory: " + resultsBasePath sys.exit(1) resultsOutputImagePath = resultsBasePath + "output_images/" if not os.path.isdir(resultsOutputImagePath): print resultsOutputImagePath + " doesn't exist! -> Let's try to create it!" try: os.mkdir(resultsOutputImagePath) except IOError: print "it was impossible to create directory: " + resultsOutputImagePath sys.exit(1) print "output images will be written to: " + resultsOutputImagePath resultTimeConfigFilename = resultsBasePath + configParser.get( "Filenames" , "ComputationTime" ) resultDeformationFieldsFilename = resultsBasePath + configParser.get( "Filenames" , "DisplacementField" ) resultIntensityFilename = resultsBasePath + configParser.get( "Filenames" , "IntensityResults" ) configFilename = resultsBasePath + configParser.get( "Filenames", "ConfigFileName" ) configFilenameExtension = configParser.get( "Filenames", "ConfigFileNameExtension" ) transformationToolCommandPath = configParser.get( "Execution", "TransformationPath" ) commandLineInterface = configParser.get( "Execution", "CommandLineInterface" ) commandLineInterfacePath = configParser.get( "Execution", "CommandLineInterfacePath" ) evaluationToolCommandPath = configParser.get( "Execution", "EvaluationToolPath" ) evaluationToolCommandImages = configParser.get( "Execution", "EvaluationToolImages" ) evaluationToolCommandDisplacements = configParser.get( "Execution", "EvaluationToolDisplacements" ) commandLineInterfaceCMD = commandLineInterfacePath + commandLineInterface evaluationToolImagesCMD = evaluationToolCommandPath + evaluationToolCommandImages evaluationToolDisplacementsCMD = evaluationToolCommandPath + evaluationToolCommandDisplacements # test if the command line registration and the evaluation tools exist and are executable if not os.path.isfile(commandLineInterfaceCMD): print "command line interface tool: '" + commandLineInterfaceCMD + "' doesn't exist!" sys.exit(1) if not os.path.isfile(evaluationToolImagesCMD): print "evaluation tool: '" + evaluationToolImagesCMD + "' doesn't exist!" sys.exit(1) if not os.path.isfile(evaluationToolDisplacementsCMD): print "evaluation tool: '" + evaluationToolDisplacementsCMD + "' doesn't exist!" sys.exit(1) if not os.access(commandLineInterfaceCMD, os.X_OK): print "command line interface tool: '" + commandLineInterfaceCMD + "' is not executable!" sys.exit(1) if not os.access(evaluationToolImagesCMD, os.X_OK): print "evaluation tool: '" + evaluationToolImagesCMD + "' is not executable!" sys.exit(1) if not os.access(evaluationToolDisplacementsCMD, os.X_OK): print "evaluation tool: '" + evaluationToolDisplacementsCMD + "' is not executable!" sys.exit(1) # now let's create all the synthetic images filesToZip = [] syntheticImageAbsoluteFilenameList = [] syntheticDisplacementAbsoluteFilenameList = [] for SynthTransformList in configParser.options( "SyntheticTransformationParameters" ): # print SynthTransformList currentline = configParser.get( "SyntheticTransformationParameters" , SynthTransformList ).split(",") transformationToolCommand = currentline[0] argument_string1 = currentline[1] argument_string2 = currentline[1] for arg in currentline[2:]: argument_string1 = argument_string1 + "_" + arg argument_string2 = argument_string2 + " " + arg transformationCMD = transformationToolCommandPath + transformationToolCommand # first, test if the tool exists and is executable if not os.path.isfile(transformationCMD): print "synthetic transformation tool: '" + transformationCMD + "' doesn't exist!" sys.exit(1) if not os.access(transformationCMD, os.X_OK): print "synthetic transformation tool: '" + transformationCMD + "' is not executable!" sys.exit(1) # now create the command for the synthetic transformation syntheticImageAbsoluteFilename = syntheticDataOutputPath + originalImageFilename[:-4] + syntheticFilenameDictionary[transformationToolCommand] + argument_string1 + analyzeHeaderFilenameExtension syntheticDisplacementAbsoluteFilename = syntheticImageAbsoluteFilename[:-4] + displacementFieldFilenameExtension transformationCMDn = transformationCMD + " " + originalImageAbsoluteFilename + " " + syntheticImageAbsoluteFilename + " " + argument_string2 syntheticImageAbsoluteFilenameList = syntheticImageAbsoluteFilenameList + [syntheticImageAbsoluteFilename] syntheticDisplacementAbsoluteFilenameList = syntheticDisplacementAbsoluteFilenameList + [syntheticDisplacementAbsoluteFilename] # only perform the synthetic transformation if the file doesn't yet exist if not os.path.isfile(syntheticImageAbsoluteFilename): # add the new created files to the list of files to zip filesToZip = filesToZip + [string.replace(syntheticImageAbsoluteFilename,analyzeHeaderFilenameExtension,analyzeImageFilenameExtension)] filesToZip = filesToZip + [syntheticDisplacementAbsoluteFilename] print print "--------------------------------------------------" print "Execute Synthetic Transformation Tool" print "--------------------------------------------------" print print transformationCMDn (pin, pout, perr) = os.popen3( transformationCMDn ) print pout.read() print perr.read() print # test if the synthetic images all have been created for synthImgFilename in syntheticImageAbsoluteFilenameList: if not os.path.isfile(synthImgFilename): print "Error: synthetically warped image '" + synthImgFilename + "'doesn't exist!" sys.exit(1) # test if the synthetic displacement fields all have been created for synthDispFilename in syntheticDisplacementAbsoluteFilenameList: if not os.path.isfile(synthDispFilename): print "Error: synthetic displacement field '" + synthDispFilename + "'doesn't exist!" sys.exit(1) counter = 0; configFilenameList = [] for syntheticImageAbsoluteFilename in syntheticImageAbsoluteFilenameList: for ProcessingList in configParser.options( "ProcessingList" ): print "current synthetic image: " + syntheticImageAbsoluteFilename print "current algorithm: " + ProcessingList print index = string.rfind(syntheticImageAbsoluteFilename, '/') + 1 syntheticImageFilename = syntheticImageAbsoluteFilename[index:] currentline = configParser.get( "ProcessingList" , ProcessingList ).split(",") currentlineLength = len( currentline ) upperCaseMode = string.upper( currentline[0] ) mode = currentline[0] syntheticTransformIdentifier = syntheticImageFilename[:-4] warpedMovingImageFilename = originalImageFilename[:-4] + "_" + string.lower( ProcessingList ) + "_warp_to_" + syntheticTransformIdentifier + analyzeHeaderFilenameExtension warpingDeformationFilename = warpedMovingImageFilename[:-4] + displacementFieldFilenameExtension fname = configFilename + str(counter) + "_" + ProcessingList + configFilenameExtension configFilenameList = [fname] + configFilenameList counter = counter + 1 currentfile = open( fname, 'w') currentfile.write("#This is a Config File\n") currentfile.write("#Name of Execution: " + ProcessingList + "\n") currentfile.write("EXECUTION_NAME " + ProcessingList + "\n") currentfile.write("FIXED_INPUT_FILENAME " + syntheticImageAbsoluteFilename + "\n") currentfile.write("MOVING_INPUT_FILENAME " + originalImageAbsoluteFilename + "\n") currentfile.write("WRITE_DISPLACEMENT_FIELD " + writeDisplacements + "\n") currentfile.write("OUTPUT_IMAGE_FILENAME " + resultsOutputImagePath + warpedMovingImageFilename + "\n") currentfile.write("OUTPUT_DISPLACEMENT_FILENAME " + resultsOutputImagePath + warpingDeformationFilename + "\n") currentfile.write("RESAMPLE_DEFAULT_VALUE " + resampleDefaultValue + "\n" ) currentfile.write("REGISTRATION_MODE " + mode + "\n" ) # parse the standard parameters file parametersFile = open ( 'parameters.txt' ) readingParametersFinished = "false" while readingParametersFinished == "false": parametersLine = parametersFile.readline() if not parametersLine: break if not parametersLine == "\n": if not parametersLine.startswith('#'): # skip irrelevant information if parametersLine.startswith('['): # get headers of parameters if parametersLine.find( "[" + upperCaseMode + "]" ) != -1: # find correct header # consume parameter values until the next group or EOF is reached count = 1 while 1: parametersLine = parametersFile.readline() if parametersLine.startswith('[') or parametersLine == '': readingParametersFinished = "true" break if not parametersLine == "\n": splittedParameters = parametersLine.split(':') if len(splittedParameters) != 2: print "Syntax error in parameter file: " + parametersFile sys.exit(1) standardValue = splittedParameters[1] parameterName = splittedParameters[0] if currentlineLength > count: currentfile.write( parameterName + " " + currentline[count] + "\n" ) else: currentfile.write( parameterName + " " + standardValue ) print "Take a standard Parameter Value for " + parameterName count = count + 1 parametersFile.close() currentfile.close() print "Generation of", counter, "Configfiles finished" if evaluationOnly != "true": timeResultList = [ ['ExecutionName','ExecutionTime'] , ['Original','0'] ] print print "--------------------------------------------------" print "Starting Computation" print "--------------------------------------------------" print # perform registration algorithm here for currentConfigFilename in configFilenameList: t0 = time.time() # get current execution name currentConfigFile = open ( currentConfigFilename ) lines = currentConfigFile.readlines() currentExecutionName = lines[2].split()[1] currentConfigFile.close() resultStdOutputFilename = string.replace(currentConfigFilename, configFilenameExtension, stdOutLogfileExtension) print print "--------------------------------------------------" print " Execute Command Line Registration Tool" print "--------------------------------------------------" print print " " + commandLineInterfaceCMD + " " + currentConfigFilename (pin, pout, perr) = os.popen3( commandLineInterfaceCMD + " " + currentConfigFilename ) info1 = pout.read() info2 = perr.read() print computationTime = time.time() - t0 # write the results to disk print currentExecutionName + " :" , computationTime ,"seconds Runtime" timeResultList = timeResultList + [[currentExecutionName, str( computationTime )]] currentOutputFile = open ( resultStdOutputFilename,'w' ) currentOutputFile.writelines( info1 ) currentOutputFile.writelines( info2 ) currentOutputFile.close() # write time results to disk if os.path.isfile(resultTimeConfigFilename): writeFile = open( resultTimeConfigFilename ,'a'); writer = csv.writer( writeFile, dialect='excel', delimiter=';' ) writer.writerows(timeResultList) writeFile.close() else: writeFile = open( resultTimeConfigFilename ,'w'); writer = csv.writer( writeFile, dialect='excel', delimiter=';' ) writer.writerows(timeResultList) writeFile.close() # create the files that build up the results if os.path.isfile(resultIntensityFilename): intensityCreationFile = open( resultIntensityFilename, "a" ) intensityCreationFile.close() else: intensityCreationFile = open( resultIntensityFilename, "w" ) intensityCreationFile.write( "Filename1 ;Filename2 ;RMS_int ;STD_int ;min int;max int;normalized mutual information ;median deviation int ;edge overlap\n" ) intensityCreationFile.close() if os.path.isfile(resultDeformationFieldsFilename): displacementsCreationFile = open( resultDeformationFieldsFilename, "a" ) displacementsCreationFile.close() else: displacementsCreationFile = open( resultDeformationFieldsFilename, "w" ) displacementsCreationFile.write( "Filename1 ;Filename2 ;RMS_disp ;max_disp ;median_dev_disp; jacobian_zero_xings\n" ) displacementsCreationFile.close() # peform evaluation for every config file for currentConfigFilename2 in configFilenameList: currentFile = open( currentConfigFilename2 ) lines = currentFile.readlines() # get the output filenames from the config file idx1 = lines[6].rfind(' ') + 1 idx2 = lines[7].rfind(' ') + 1 outputImageFilename = lines[6][idx1:-1] outputDefFilename = lines[7][idx2:-1] filesToZip = filesToZip + [string.replace(outputImageFilename,analyzeHeaderFilenameExtension,analyzeImageFilenameExtension)] filesToZip = filesToZip + [outputDefFilename] # also get the fixed (synthetic) input filename from the config file idx3 = lines[3].rfind(' ') + 1 syntheticImageFilename = lines[3][idx3:-1] syntheticDisplacementFilename = syntheticImageFilename[:-4] + displacementFieldFilenameExtension if writeDisplacements == "false": print "evaluation without displacements" # write a dummy displacements.csv file displacementsFile = open( resultDeformationFieldsFilename, "a+" ) displacementsFile.write( "n.a.;n.a.;0;0;0;0\n" ) displacementsFile.write( "n.a.;n.a.;0;0;0;0\n" ) displacementsFile.close() # this creates the intensity.csv file evaluationToolCMDn = evaluationToolImagesCMD + " " + syntheticImageFilename + " " + originalImageAbsoluteFilename + " " + outputImageFilename + " " + resultIntensityFilename else: print "full evaluation with displacements" evaluationToolCMDn = evaluationToolDisplacementsCMD + " " + syntheticImageFilename + " " + originalImageAbsoluteFilename + " " + outputImageFilename + " " + syntheticDisplacementFilename + " " + outputDefFilename + " " + resampleDefaultValue + " " + resultIntensityFilename + " " + resultDeformationFieldsFilename print print "--------------------------------------------------" print " Execute Evaluation Tool" print "--------------------------------------------------" print print " " + evaluationToolCMDn (pin, pout, perr) = os.popen3( evaluationToolCMDn ) print pout.read() print perr.read() print # if chosen, zip the created intermediate images if zipOutputFiles == "true": for fileToZip in filesToZip: if os.path.isfile(fileToZip): zipCommand = "gzip" + " -f " + fileToZip + " && " + "rm -f " + fileToZip print zipCommand (pin,pout,perr) = os.popen3( zipCommand ) print pout.read() print perr.read()