Ejemplo n.º 1
0
def BRAINSCutCreateVector(configurationFilename,
                          probabilityMapDict,
                          normalization,
                          outputXmlFilename,
                          outputVectorFilename):
    print(BRAINSCutCreateVector)
    print(BRAINSCutCreateVector)
    print(BRAINSCutCreateVector)
    print(BRAINSCutCreateVector)
    print(BRAINSCutCreateVector)
    print(BRAINSCutCreateVector)
    import os
    import sys
    for roi in probabilityMapDict.iterkeys():
        if not os.path.exists(probabilityMapDict[roi]):
            print( """ ERROR
                   {fn}  does not exist.
                   """.format( fn=probabilityMapDict[roi]) )
            sys.exit()

    vectorDir = os.path.dirname(os.path.abspath(outputVectorFilename))
    if not os.path.exists(vectorDir):
        os.mkdirs(vectorDir)
    generateProbabilityMap = False
    createVectors = True
    trainModel = False
    applyModel = False
    dummyMethodParameter = {}
    dummyOutputDirDict = {}
    dummyModelFilename = "na"
    probabilityMapGaussianSigma = 1
    from ConfigurationParser import BRAINSCutCMDFromConfigFile
    returnList = BRAINSCutCMDFromConfigFile(configurationFilename,
                                            outputXmlFilename,
                                            probabilityMapDict,
                                            probabilityMapGaussianSigma,
                                            outputVectorFilename,
                                            dummyModelFilename,
                                            generateProbabilityMap,
                                            createVectors,
                                            normalization,
                                            trainModel,
                                            applyModel,
                                            dummyOutputDirDict,
                                            dummyMethodParameter)
    outputVectorFilename = returnList['inputVectorFilename']
    outputVectorHdrFilename = outputVectorFilename + ".hdr"
    outputXmlFilename = os.path.abspath(outputXmlFilename)
    outputNormalization = normalization
    print("""Output of BRAINSCutCreateVector
          outputVectorFilename = {ovf}
          outputVectorHdrFilename = {ovh}
          outputNormalization = {on}
          outputXmlFilename = {oxf}
          """.format( ovf=outputVectorFilename,
                      ovh=outputVectorHdrFilename,
                      on=outputNormalization,
                      oxf=outputXmlFilename))
    return outputVectorFilename, outputVectorHdrFilename, outputNormalization, outputXmlFilename
Ejemplo n.º 2
0
def BRAINSCutTrainModel(
    configurationFilename,
    inputVectorFilename,
    outputModelFilenamePrefix,
    outputXmlFilename,
    methodParameter,
):
    import os.path
    import sys

    generateProbabilityMap = False
    createVectors = False
    trainModel = True
    applyModel = False
    dummyProbMapDict = {}

    p_inputVectorFilename = os.path.abspath(inputVectorFilename)
    p_inputVectorFilename = p_inputVectorFilename[:-3]  # truncate ANN
    print(p_inputVectorFilename)
    print(p_inputVectorFilename)
    print(p_inputVectorFilename)
    print(p_inputVectorFilename)
    print(p_inputVectorFilename)
    print(p_inputVectorFilename)
    print(p_inputVectorFilename)

    p_outputModelFilenamePrefix = os.path.abspath(outputModelFilenamePrefix)
    dummyOutputDirDict = {}
    createvectorsNormalization = "Dummy"
    probabilityMapGaussianSigma = 1
    from ConfigurationParser import BRAINSCutCMDFromConfigFile

    returnList = BRAINSCutCMDFromConfigFile(
        configurationFilename,
        outputXmlFilename,
        dummyProbMapDict,
        probabilityMapGaussianSigma,
        p_inputVectorFilename,
        p_outputModelFilenamePrefix,
        generateProbabilityMap,
        createVectors,
        createvectorsNormalization,
        trainModel,
        applyModel,
        dummyOutputDirDict,
        methodParameter,
    )

    outputModelFileSearchStr = (p_outputModelFilenamePrefix + "*" +
                                str(methodParameter["--numberOfTrees"]) + "*" +
                                "*gz")
    import glob

    trainedModelFile = glob.glob(outputModelFileSearchStr)
    return os.path.abspath(trainedModelFile[0]), methodParameter
Ejemplo n.º 3
0
def BRAINSCutGenerateProbabilityMap(configurationFilename, probabilityMapDict,
                                    gaussianSigma, outputXmlFilename):
    print("""*****************************
          BRAINSCutGenerateProbabilityMap
          """)
    generateProbabilityMap = True
    createVectors = False
    trainModel = False
    applyModel = False
    dummyMethodParameter = {}

    print(("""generate probability map
           {str}
           """.format(str=probabilityMapDict)))

    import os

    for roi in list(probabilityMapDict.keys()):
        print((os.path.abspath(probabilityMapDict[roi])))
        probDir = os.path.dirname(os.path.abspath(probabilityMapDict[roi]))
        if not os.path.exists(probDir):
            os.mkdirs(probDir)
    dummyFilename = "na"
    dummyDict = {}
    createVectorsNormalization = "dummy"
    from ConfigurationParser import BRAINSCutCMDFromConfigFile

    returnList = BRAINSCutCMDFromConfigFile(
        configurationFilename,
        outputXmlFilename,
        probabilityMapDict,
        gaussianSigma,
        dummyFilename,
        dummyFilename,
        generateProbabilityMap,
        createVectors,
        createVectorsNormalization,
        trainModel,
        applyModel,
        dummyDict,
        dummyMethodParameter,
    )
    returnProbMapList = returnList["probabilityMap"]
    import sys

    if list(returnProbMapList.keys()) != list(probabilityMapDict.keys()):
        print("""ERROR
              returnProbMapList has to match probabilityMapDict
              in BRAINSCutGenerateProbabilityMap
              """)
        sys.exit()

    outputXmlFilename = os.path.abspath(outputXmlFilename)
    return returnProbMapList, outputXmlFilename, configurationFilename
Ejemplo n.º 4
0
def BRAINSCutApplyModel(
    configurationFilename,
    probabilityMapDict,
    normalization,
    inputModelFilename,
    methodParameter,
    outputXmlFilename,
):
    import os.path
    import sys

    if isinstance(normalization, list):
        if len(set(normalization)) == 1:
            normalization = normalization[0]
        else:
            print(("""ERROR::
                  Improper normalization input.
                  Currently only one normalization type for
                  all ROIs are supported.
                  Normalization input is not same for all ROIs.
                  {fn}""".format(fn=normalization)))
            sys.exit()
    p_probMapDict = {}
    for roi in list(probabilityMapDict.keys()):
        if not os.path.exists(probabilityMapDict[roi]):
            print(""" ERROR
                   probabilityMapDict[ roi ]  does not exist.
                   """)
            sys.exit()
        p_probMapDict[roi] = os.path.abspath(probabilityMapDict[roi])

    import ConfigurationParser as this

    outputDirDict = this.getOutputDirDict(configurationFilename)
    generateProbabilityMap = False
    createVectors = False
    trainModel = False
    applyModel = True
    dummyInputVectorFilename = ""
    probabilityMapGaussianSigma = 1
    from ConfigurationParser import BRAINSCutCMDFromConfigFile

    returnList = BRAINSCutCMDFromConfigFile(
        configurationFilename,
        outputXmlFilename,
        p_probMapDict,
        probabilityMapGaussianSigma,
        dummyInputVectorFilename,
        inputModelFilename,
        generateProbabilityMap,
        createVectors,
        normalization,
        trainModel,
        applyModel,
        outputDirDict,
        methodParameter,
    )
    import glob

    outputLabelDict = {}
    for session in list(returnList["outputDirDict"].keys()):
        outputDir = returnList["outputDirDict"][session]
        print(outputDir)
        print(outputDir)
        print(outputDir)
        print(outputDir)
        print(outputDir)
        print(outputDir)
        outputSessionDict = {}
        outputSessionDict["label"] = glob.glob(outputDir +
                                               "/*_ANNLabel_seg.nii.gz")
        outputSessionDict["ambigiousLabel"] = glob.glob(
            outputDir + "/*AmbiguousMap.nii.gz")
        outputSessionDict["defPrior"] = glob.glob(outputDir + "/*def.nii.gz")
        outputSessionDict["rawOutput"] = glob.glob(outputDir +
                                                   "/ANNContinuous*.nii.gz")
        outputLabelDict[session] = outputSessionDict

    returnList["outputLabelDict"] = outputLabelDict

    outputLabelDict = returnList["outputLabelDict"]

    return outputLabelDict