Beispiel #1
0
def paratospharm(opts, config, data, outputDir):
    workdir = "%s/Processing/1.MeasurementandSPHARM" % (outputDir)
    pathParaToSPHARM = config["ParaToSPHARMMeshCLPPath"]

    idlist = [id for id, l, g in data]
    parafiles = ["%s/%s.para.vtk" % (workdir, id) for id, l, g in data]
    surffiles = ["%s/%s.surf.vtk" % (workdir, id) for id, l, g in data]
    lowresfiles = ["%s/%s.ip.SPHARM.vtk" % (workdir, id) for id, l, g in data]
    highresfiles = [
        "%s/%s.subj.SPHARM.vtk" % (workdir, id) for id, l, g in data
    ]
    lowresparams = ["%s/%s.ip.Param.vtk" % (workdir, id) for id, l, g in data]
    highresparams = [
        "%s/%s.subj.Param.vtk" % (workdir, id) for id, l, g in data
    ]
    dataset = zip(parafiles, surffiles, lowresfiles, lowresparams,
                  highresfiles, highresparams)

    for idx, (pf, sf, lm, lp, hm, hp) in enumerate(dataset):
        # check if the parafile is newer than lm-output
        if system.is_file_newer(lm, pf, opts.overwrite):
            if idx == 0:
                id = idlist[idx]
                cmd = "{pathParaToSPHARM} {pf} {sf} --subdivLevel 10 --spharmDegree 20 " +\
                        " {workdir}/{id}.ip. --paraOut"
            else:
                id = idlist[idx]
                refid = idlist[0]
                cmd = "{pathParaToSPHARM} {pf} {sf} --subdivLevel 10 --spharmDegree 20  " +\
                        "{workdir}/{id}.ip. --flipTemplateOn " +\
                        "--flipTemplate {workdir}/{refid}.ip.SPHARM.coef --paraOut"
            cmd = cmd.format(**locals())
            system.run_process(cmd, verbose=opts.verbose)

    for idx, (pf, sf, lm, lp, hm, hp) in enumerate(dataset):
        # check if the parafile is newer than hm-output
        if system.is_file_newer(hm, pf, opts.overwrite):
            if idx == 0:
                id = idlist[idx]
                cmd = "{pathParaToSPHARM} {pf} {sf} --subdivLevel 50 --spharmDegree 20 " +\
                        "{workdir}/{id}.subj. --paraOut"
            else:
                id = idlist[idx]
                refid = idlist[0]
                cmd = "{pathParaToSPHARM} {pf} {sf} --subdivLevel 50 --spharmDegree 20 " +\
                        "{workdir}/{id}.subj. --flipTemplateOn " +\
                        "--flipTemplate {workdir}/{refid}.subj.SPHARM.coef --paraOut"
            cmd = cmd.format(**locals())
            system.run_process(cmd, verbose=opts.verbose)
Beispiel #2
0
def paratospharm(opts, config, data, outputDir):
    workdir = "%s/Processing/1.MeasurementandSPHARM" % (outputDir)
    pathParaToSPHARM = config["ParaToSPHARMMeshCLPPath"]

    idlist = [id for id,l,g in data]
    parafiles = ["%s/%s.para.vtk"%(workdir,id) for id,l,g in data]
    surffiles = ["%s/%s.surf.vtk"%(workdir,id) for id,l,g in data]
    lowresfiles = ["%s/%s.ip.SPHARM.vtk"%(workdir,id) for id,l,g in data]
    highresfiles = ["%s/%s.subj.SPHARM.vtk"%(workdir,id) for id,l,g in data]
    lowresparams = ["%s/%s.ip.Param.vtk"%(workdir,id) for id,l,g in data]
    highresparams = ["%s/%s.subj.Param.vtk"%(workdir,id) for id,l,g in data]
    dataset = zip(parafiles, surffiles, lowresfiles, lowresparams, highresfiles, highresparams)

    for idx,(pf,sf,lm,lp,hm,hp) in enumerate(dataset):
        # check if the parafile is newer than lm-output
        if system.is_file_newer(lm, pf, opts.overwrite):
            if idx == 0:
                id = idlist[idx]
                cmd = "{pathParaToSPHARM} {pf} {sf} --subdivLevel 10 --spharmDegree 20 " +\
                        " {workdir}/{id}.ip. --paraOut"
            else:
                id = idlist[idx]
                refid = idlist[0]
                cmd = "{pathParaToSPHARM} {pf} {sf} --subdivLevel 10 --spharmDegree 20  " +\
                        "{workdir}/{id}.ip. --flipTemplateOn " +\
                        "--flipTemplate {workdir}/{refid}.ip.SPHARM.coef --paraOut"
            cmd = cmd.format(**locals())
            system.run_process(cmd,verbose=opts.verbose)


    for idx,(pf,sf,lm,lp,hm,hp) in enumerate(dataset):
        # check if the parafile is newer than hm-output
        if system.is_file_newer(hm, pf, opts.overwrite):
            if idx == 0:
                id = idlist[idx]
                cmd = "{pathParaToSPHARM} {pf} {sf} --subdivLevel 50 --spharmDegree 20 " +\
                        "{workdir}/{id}.subj. --paraOut"
            else:
                id = idlist[idx]
                refid = idlist[0]
                cmd = "{pathParaToSPHARM} {pf} {sf} --subdivLevel 50 --spharmDegree 20 " +\
                        "{workdir}/{id}.subj. --flipTemplateOn " +\
                        "--flipTemplate %s/%s.subj.SPHARM.coef --paraOut"
            cmd = cmd.format(**locals())
            system.run_process(cmd,verbose=opts.verbose)
Beispiel #3
0
def resample_segmentations(opts,config,data,outputdir):
    workdir = "%s/Processing/1.MeasurementandSPHARM" % (outputdir)
    refid = data[0][0]
    reference_image = "{workdir}/{refid}.segpost.nii.gz".format(**locals())

    pathResampleVolume = config["ResampleVolume2Path"]
    for id,l,g in data:
        segpost_image = "{workdir}/{id}.segpost.nii.gz".format(**locals())
        labelMapNCResampled = "{workdir}/{id}.labelMapNCsegmentation.nrrd".format(**locals())
        cmd = "{pathResampleVolume} {segpost_image} {labelMapNCResampled} -R {reference_image} -i nn"
        if system.is_file_newer(labelMapNCResampled, segpost_image, opts.overwrite):
            cmd = cmd.format(**locals())
            system.run_process(cmd,verbose=True)
Beispiel #4
0
def compute_thickness(opts, config, data, outputdir, ids, idl, idh, idn):
    pathThicknessTool = config["measureThicknessFilterPath"]
    for (id, labelmap, group) in data:
        labelmap = "{outputdir}/Processing/1.MeasurementandSPHARM/{id}.boundaryMap.mha".format(**locals())
        # temporary
        #    workdir = "%s/Processing/1.MeasurementandSPHARM/%s_2" % (outputdir, id)
        #    measurementoutput = "%s/Processing/1.MeasurementandSPHARM/%s.thickness.mha" % (outputdir, id)

        workdir = "{outputdir}/Processing/1.MeasurementandSPHARM/{id}".format(**locals())
        measurementoutput = "{outputdir}/Processing/1.MeasurementandSPHARM/" +\
                        "{id}_measurementoutput.nrrd"
        measurementoutput = measurementoutput.format(**locals())

        if (not os.path.exists(workdir)):
            os.makedirs(workdir)

        if system.is_file_newer(measurementoutput, labelmap, opts.overwrite) or \
                system.is_file_newer(measurementoutput, "{workdir}/laplacianCache.nii.gz".format(**locals()), opts.overwrite):
            cmd = "{pathThicknessTool} --mr --sbt --ids {ids} --idl {idl} --idh {idh} --idn {idn} --ttrns 500 " +\
                "--workdir {workdir} --cachedLaplacianOutput laplacianCache.nii.gz {labelmap} {measurementoutput}"
            cmd = cmd.format(**locals())
            system.run_process(cmd,verbose=True)
Beispiel #5
0
def resample_segmentations(opts,config,data,outputdir):
    workdir = "%s/Processing/1.MeasurementandSPHARM" % (outputdir)
    refid = data[0][0]
    reference_image = "{workdir}/{refid}.segpost.nii.gz".format(**locals())

    pathResampleVolume = config["ResampleVolume2Path"]
    for id,l,g in data:
        segpost_image = "{workdir}/{id}.segpost.nii.gz".format(**locals())
        labelMapNCResampled = "{workdir}/{id}.labelMapNCsegmentation.nrrd".format(**locals())
        cmd = "{pathResampleVolume} {segpost_image} {labelMapNCResampled} -R {reference_image} -i nn"
        if system.is_file_newer(labelMapNCResampled, segpost_image, opts.overwrite):
            cmd = cmd.format(**locals())
            system.run_process(cmd,verbose=True)
Beispiel #6
0
def compute_thickness(opts, config, data, outputdir, ids, idl, idh):
    pathThicknessTool = config["measureThicknessFilterPath"]
    for (id, labelmap, group) in data:
        labelmap = "{outputdir}/Processing/1.MeasurementandSPHARM/{id}.boundaryMap.mha".format(**locals())
        # temporary
        #    workdir = "%s/Processing/1.MeasurementandSPHARM/%s_2" % (outputdir, id)
        #    measurementoutput = "%s/Processing/1.MeasurementandSPHARM/%s.thickness.mha" % (outputdir, id)

        workdir = "{outputdir}/Processing/1.MeasurementandSPHARM/{id}".format(**locals())
        measurementoutput = "{outputdir}/Processing/1.MeasurementandSPHARM/" +\
                        "{id}_measurementoutput.nrrd"
        measurementoutput = measurementoutput.format(**locals())

        if (not os.path.exists(workdir)):
            os.makedirs(workdir)

        if system.is_file_newer(measurementoutput, labelmap, opts.overwrite) or \
                system.is_file_newer(measurementoutput, "{workdir}/laplacianCache.nii.gz".format(**locals()), opts.overwrite):
            cmd = "{pathThicknessTool} --mr --sbt --ids {ids} --idl {idl} --idh {idh} --ttrns 500 " +\
                "--workdir {workdir} --cachedLaplacianOutput laplacianCache.nii.gz {labelmap} {measurementoutput}"
            cmd = cmd.format(**locals())
            system.run_process(cmd,verbose=True)
Beispiel #7
0
def regenerate_segmentations(data, config, outputdir):
    pathKmesh = config["kmeshPath"]
    pathKcalc = config["kcalcPath"]
    workdir = "{outputdir}/Processing/1.MeasurementandSPHARM".format(
        **locals())

    # before executing the boundary correction
    # check if none of input is not generated.
    testLabelMapsFail = False in [os.path.exists(x) for (t, x, g) in data]
    if testLabelMapsFail:
        raise RuntimeError("There are missing label maps")

    surfaceInputList = [
        "{workdir}/{id}.subj.SPHARM.vtk".format(**locals())
        for (id, l, g) in data
    ]
    testSurfaceInputsFail = False in [
        os.path.exists(x) for x in surfaceInputList
    ]
    if testSurfaceInputsFail:
        raise RuntimeError(
            "There are missing surface inputs. Check SPHARM results!")

    if opts.nop1 is False:
        for idx, (id, labelMap, group) in enumerate(data):
            labelOutput = "{workdir}/{id}.zerocortex.nrrd".format(**locals())
            surfaceInput = "{workdir}/{id}.subj.SPHARM.vtk".format(**locals())
            #surfaceInput = "{workdir}/{id}.surf.vtk".format(**locals())
            surfaceLabels = "{workdir}/{id}.labels.vtp".format(**locals())

            if system.is_file_newer(labelOutput, labelMap, opts.overwrite):
                cmd = "{pathKcalc} -e 'A==3?0:A' -o {labelOutput} {labelMap}"
                cmd = cmd.format(**locals())
                if system.run_process(cmd, verbose=True) != 0:
                    raise RuntimeError("fail to modify labelmap")

            if system.is_file_newer(surfaceLabels, surfaceInput, opts.overwrite) or \
                    system.is_file_newer(surfaceLabels, labelOutput, opts.overwrite):
                cmd = "{pathKmesh} -sampleImage -zrotate -outputScalarName labels " + \
                        "{labelOutput} {surfaceInput} {surfaceLabels}"
                cmd = cmd.format(**locals())
                if system.run_process(cmd, verbose=True) != 0:
                    raise RuntimError("fail to run kmesh")

    # compute the average of labels scalar values to smooth out
    if opts.nop2 is False:
        cmd = "{pathKmesh} -averageScalars -threshold 1.8 " + \
              "-scalarName labels -outputScalarName meanLabels"
        cmd = cmd.format(**locals())
        for (tag, labelmap, group) in data:
            surfaceMeshWithLabels = "{workdir}/{tag}.labels.vtp".format(
                **locals())
            cmd += " " + surfaceMeshWithLabels
        system.run_process(cmd, verbose=True)

    if opts.nop3 is False:
        for idx, (tag, labelMap, group) in enumerate(data):
            labelOutput = "{workdir}/{tag}.zerocortex.nrrd".format(**locals())
            surfaceLabels = "{workdir}/{tag}.labels.vtp".format(**locals())
            voronoiImage = "{workdir}/{tag}.voronoi.mha".format(**locals())
            surfaceImage = "{workdir}/{tag}.solution.mha".format(**locals())
            boundaryMap = "{workdir}/{tag}.boundaryMap.mha".format(**locals())

            if system.is_file_newer(labelOutput, voronoiImage, opts.overwrite) or \
                    system.is_file_newer(surfaceLabels, voronoiImage, opts.overwrite):
                cmd = "{pathKmesh} -voronoiImage -zrotate -scalarName meanLabels " +\
                    "{labelOutput} {surfaceLabels} -o {voronoiImage}"
                cmd = cmd.format(**locals())
                system.run_process(cmd, verbose=True)

            if system.is_file_newer(labelOutput, voronoiImage, opts.overwrite) or \
                    system.is_file_newer(surfaceLabels, voronoiImage, opts.overwrite):
                cmd = "{pathKmesh} -scanConversion -zrotate " +\
                    "{surfaceLabels} {labelOutput} {surfaceImage}"
                cmd = cmd.format(**locals())
                system.run_process(cmd, verbose=True)

            if system.is_file_newer(voronoiImage, boundaryMap, opts.overwrite) or \
                    system.is_file_newer(surfaceImage, boundaryMap, opts.overwrite):
                cmd = "{pathKcalc} -e 'B>0?3:A' -o {boundaryMap} " +\
                    "{voronoiImage} {surfaceImage}"
                cmd = cmd.format(**locals())
                system.run_process(cmd, verbose=True)
Beispiel #8
0
def sample_thickness(opts, config, csvdata, outputDir, corr_opt):
    meshDir = "{outputDir}/Processing/1.MeasurementandSPHARM".format(
        **locals())
    shapeworksDir = "{outputDir}/Processing/2.shapeworks".format(**locals())
    sampleDir = "{outputDir}/Processing/3.sampling".format(**locals())
    statDir = "{outputDir}/Statistics".format(**locals())
    pathKmesh = config["kmeshPath"]

    if (corr_opt == "initial_dense"):
        extra_args = "--inputPointFlip"
    else:
        extra_args = ""

    # depending on options, choose different meshes
    # loop over ids
    for id in [i for i, l, g in csvdata]:
        distanceVector = "{sampleDir}/{id}.distanceVector.nrrd".format(
            **locals())
        inputMeasurement = "{meshDir}/{id}_measurementoutput.nrrd".format(
            **locals())
        #    inputMeasurement = "%s/%s.thickness.mha" % (meshDir, id)
        if (corr_opt == "initial_dense"):
            samplingTxt = "%s/%s.initialDenseSampling.txt" % (sampleDir, id)
            smoothSamplingTxt = "%s/%s.smoothInitialDenseSampling.txt" % (
                sampleDir, id)
            samplingMeshOutput = "%s/%s.initialDenseSampling.vtk" % (sampleDir,
                                                                     id)
            originalMeshOutput = "%s/%s.initialDenseOriginal.vtk" % (sampleDir,
                                                                     id)
            inputMesh = "%s/%s.subj.SPHARM.vtk" % (meshDir, id)
        elif (corr_opt == "correspondence"):
            samplingTxt = "%s/%s.sampling.txt" % (sampleDir, id)
            smoothSamplingTxt = "%s/%s.smoothSampling.txt" % (sampleDir, id)
            samplingMeshOutput = "%s/%s.sampling.vtk" % (sampleDir, id)
            originalMeshOutput = "%s/%s.original.vtk" % (sampleDir, id)
            inputMesh = "%s/%s.correspondence.vtk" % (shapeworksDir, id)
        elif (corr_opt == "dense_correspondence"):
            samplingTxt = "%s/%s.denseSampling.txt" % (sampleDir, id)
            smoothSamplingTxt = "%s/%s.smoothDenseSampling.txt" % (sampleDir,
                                                                   id)
            samplingMeshOutput = "%s/%s.denseSampling.vtk" % (sampleDir, id)
            originalMeshOutput = "%s/%s.denseOriginal.vtk" % (sampleDir, id)
            inputMesh = "%s/%s.dense_correspondence.vtk" % (shapeworksDir, id)
        elif (corr_opt == "spharm_sampling"):
            samplingTxt = "%s/%s.spharmSampling.txt" % (sampleDir, id)
            smoothSamplingTxt = "%s/%s.smoothSpharmSampling.txt" % (sampleDir,
                                                                    id)
            samplingMeshOutput = "%s/%s.spharmSampling.vtk" % (sampleDir, id)
            originalMeshOutput = "%s/%s.spharmOriginal.vtk" % (sampleDir, id)
            inputMesh = "%s/%s.subj.SPHARM.vtk" % (meshDir, id)

        #samplingCmd = "{MeshPointIntensitySamplingPath} --workDir {workdir} --inputAsPhysicalCoord {extra_args} --distanceVector {distanceVector} -i nn -a {samplingTxt} -m {samplingMeshOutput} --smoothedAttributeOutput {smoothSamplingTxt} --originalMeshOutput {originalMeshOutput} {inputMeasurement} {inputMesh}"
        #exeCmd = samplingCmd.format(**locals())
        # exeCmd = samplingCmd % (outputDir, distanceVector, samplingTxt, samplingMeshOutput, smoothSamplingTxt, originalMeshOutput, inputMeasurement, inputMesh)
        #system.run_process(exeCmd,verbose=True)

        if system.is_file_newer(smoothSamplingTxt, inputMeasurement, opts.overwrite) or \
                system.is_file_newer(smoothSamplingTxt, samplingTxt, opts.overwrite) or \
                system.is_file_newer(smoothSamplingTxt, samplingMeshOutput, opts.overwrite):
            useStreamLine = corr_opt == "spharm_sampling"
            if (not useStreamLine):
                exeCmd = "{pathKmesh} -sampleImage {inputMeasurement} {inputMesh} {samplingMeshOutput} " +\
                            "-outputScalarName Thickness -zrotate"
                exeCmd = exeCmd.format(**locals())
                system.run_process(exeCmd, verbose=True)

                exeCmd = "{pathKmesh} -smoothScalars {samplingMeshOutput} {samplingMeshOutput} " +\
                            "-scalarName Thickness -outputScalarName smoothThickness -iter 3"
                exeCmd = exeCmd.format(**locals())
                system.run_process(exeCmd, verbose=True)

                exeCmd = "{pathKmesh} -exportScalars {samplingMeshOutput} {samplingTxt} -scalarName Thickness"
                exeCmd = exeCmd.format(**locals())
                system.run_process(exeCmd, verbose=True)

                exeCmd = "{pathKmesh} -exportScalars {samplingMeshOutput} {smoothSamplingTxt} " +\
                            "-scalarName smoothThickness "
                exeCmd = exeCmd.format(**locals())
                system.run_process(exeCmd, verbose=True)
            else:
                gradientFile = "{outputDir}/Processing/1.MeasurementandSPHARM/" +\
                        "{id}/laplacianGradient.nrrd".format(**locals())
                gradientVTIFile = "{outputDir}/Processing/1.MeasurementandSPHARM/" +\
                        "{id}/laplacianGradient.vti".format(**locals())
                streamLineFile = "{outputDir}/Processing/1.MeasurementandSPHARM/" +\
                        "{id}/stream_lines.vtp".format(**locals())

                # vti image creation
                exeCmd = "{pathKmesh} -vti {gradientFile} {gradientVTIFile} -attrDim 3"
                exeCmd = exeCmd.format(**locals())
                system.run_process(exeCmd, verbose=True)

                # RK45 integration
                exeCmd = "{pathKmesh} -zrotate -traceDirection backward -traceStream " +\
                    "{gradientVTIFile} {inputMesh} {streamLineFile} {samplingMeshOutput}"
                exeCmd = exeCmd.format(**locals())
                system.run_process(exeCmd, verbose=True)

                # scalar export
                exeCmd = "{pathKmesh} -scalarName Length -exportScalars " +\
                    "{samplingMeshOutput} {smoothSamplingTxt}"
                exeCmd = exeCmd.format(**locals())
                system.run_process(exeCmd, verbose=True)
Beispiel #9
0
def regenerate_segmentations(data, config, outputdir):
    pathKmesh = config["kmeshPath"]
    pathKcalc = config["kcalcPath"]
    workdir = "{outputdir}/Processing/1.MeasurementandSPHARM".format(**locals())

    # before executing the boundary correction
    # check if none of input is not generated.
    testLabelMapsFail = False in [os.path.exists(x) for (t,x,g) in data]
    if testLabelMapsFail:
        raise RuntimeError("There are missing label maps")

    surfaceInputList = ["{workdir}/{id}.subj.SPHARM.vtk".format(**locals()) for (id,l,g) in data]
    testSurfaceInputsFail = False in [os.path.exists(x) for x in surfaceInputList]
    if testSurfaceInputsFail:
        raise RuntimeError("There are missing surface inputs. Check SPHARM results!")

    if opts.nop1 is False:
        for idx, (id,labelMap,group) in enumerate(data):
            labelOutput = "{workdir}/{id}.zerocortex.nrrd".format(**locals())
            surfaceInput = "{workdir}/{id}.subj.SPHARM.vtk".format(**locals())
            #surfaceInput = "{workdir}/{id}.surf.vtk".format(**locals())
            surfaceLabels = "{workdir}/{id}.labels.vtp".format(**locals())

            if system.is_file_newer(labelOutput, labelMap, opts.overwrite):
                cmd = "{pathKcalc} -e 'A==3?0:A' -o {labelOutput} {labelMap}"
                cmd = cmd.format(**locals())
                if system.run_process(cmd,verbose=True) != 0:
                    raise RuntimeError("fail to modify labelmap")

            if system.is_file_newer(surfaceLabels, surfaceInput, opts.overwrite) or \
                    system.is_file_newer(surfaceLabels, labelOutput, opts.overwrite):
                cmd = "{pathKmesh} -sampleImage -zrotate -outputScalarName labels " + \
                        "{labelOutput} {surfaceInput} {surfaceLabels}"
                cmd = cmd.format(**locals())
                if system.run_process(cmd,verbose=True) != 0:
                    raise RuntimError("fail to run kmesh") 

    # compute the average of labels scalar values to smooth out
    if opts.nop2 is False:
        cmd = "{pathKmesh} -averageScalars -threshold 1.8 " + \
              "-scalarName labels -outputScalarName meanLabels"
        cmd = cmd.format(**locals())
        for (tag, labelmap, group) in data:
            surfaceMeshWithLabels = "{workdir}/{tag}.labels.vtp".format(**locals())
            cmd += " " + surfaceMeshWithLabels
        system.run_process(cmd,verbose=True)

    if opts.nop3 is False:
        for idx,(tag,labelMap,group) in enumerate(data):
            labelOutput = "{workdir}/{tag}.zerocortex.nrrd".format(**locals())
            surfaceLabels = "{workdir}/{tag}.labels.vtp".format(**locals())
            voronoiImage = "{workdir}/{tag}.voronoi.mha".format(**locals())
            surfaceImage = "{workdir}/{tag}.solution.mha".format(**locals())
            boundaryMap = "{workdir}/{tag}.boundaryMap.mha".format(**locals())

            if system.is_file_newer(labelOutput, voronoiImage, opts.overwrite) or \
                    system.is_file_newer(surfaceLabels, voronoiImage, opts.overwrite):
                cmd = "{pathKmesh} -voronoiImage -zrotate -scalarName meanLabels " +\
                    "{labelOutput} {surfaceLabels} -o {voronoiImage}"
                cmd = cmd.format(**locals())
                system.run_process(cmd,verbose=True)

            if system.is_file_newer(labelOutput, voronoiImage, opts.overwrite) or \
                    system.is_file_newer(surfaceLabels, voronoiImage, opts.overwrite):
                cmd = "{pathKmesh} -scanConversion -zrotate " +\
                    "{surfaceLabels} {labelOutput} {surfaceImage}"
                cmd = cmd.format(**locals())
                system.run_process(cmd,verbose=True)

            if system.is_file_newer(voronoiImage, boundaryMap, opts.overwrite) or \
                    system.is_file_newer(surfaceImage, boundaryMap, opts.overwrite):
                cmd = "{pathKcalc} -e 'B>0?3:A' -o {boundaryMap} " +\
                    "{voronoiImage} {surfaceImage}"
                cmd = cmd.format(**locals())
                system.run_process(cmd,verbose=True)
Beispiel #10
0
def sample_thickness(opts,config,csvdata,outputDir,corr_opt):
    meshDir = "{outputDir}/Processing/1.MeasurementandSPHARM".format(**locals())
    shapeworksDir = "{outputDir}/Processing/2.shapeworks".format(**locals())
    sampleDir = "{outputDir}/Processing/3.sampling".format(**locals())
    statDir = "{outputDir}/Statistics".format(**locals())
    pathKmesh = config["kmeshPath"]

    if (corr_opt == "initial_dense"):
        extra_args = "--inputPointFlip"
    else:
        extra_args = ""

    # depending on options, choose different meshes
    # loop over ids
    for id in [i for i,l,g in csvdata]:
        distanceVector = "{sampleDir}/{id}.distanceVector.nrrd".format(**locals())
        inputMeasurement = "{meshDir}/{id}_measurementoutput.nrrd".format(**locals())
        #    inputMeasurement = "%s/%s.thickness.mha" % (meshDir, id)
        if (corr_opt == "initial_dense"):
            samplingTxt = "%s/%s.initialDenseSampling.txt" % (sampleDir, id)
            smoothSamplingTxt = "%s/%s.smoothInitialDenseSampling.txt" % (sampleDir, id)
            samplingMeshOutput = "%s/%s.initialDenseSampling.vtk" % (sampleDir, id)
            originalMeshOutput = "%s/%s.initialDenseOriginal.vtk" % (sampleDir, id)
            inputMesh = "%s/%s.subj.SPHARM.vtk" % (meshDir, id)
        elif (corr_opt == "correspondence"):
            samplingTxt = "%s/%s.sampling.txt" % (sampleDir, id)
            smoothSamplingTxt = "%s/%s.smoothSampling.txt" % (sampleDir, id)
            samplingMeshOutput = "%s/%s.sampling.vtk" % (sampleDir, id)
            originalMeshOutput = "%s/%s.original.vtk" % (sampleDir, id)
            inputMesh = "%s/%s.correspondence.vtk" % (shapeworksDir, id)
        elif (corr_opt == "dense_correspondence"):
            samplingTxt = "%s/%s.denseSampling.txt" % (sampleDir, id)
            smoothSamplingTxt = "%s/%s.smoothDenseSampling.txt" % (sampleDir, id)
            samplingMeshOutput = "%s/%s.denseSampling.vtk" % (sampleDir, id)
            originalMeshOutput = "%s/%s.denseOriginal.vtk" % (sampleDir, id)
            inputMesh = "%s/%s.dense_correspondence.vtk" % (shapeworksDir, id)
        elif (corr_opt == "spharm_sampling"):
            samplingTxt = "%s/%s.spharmSampling.txt" % (sampleDir, id)
            smoothSamplingTxt = "%s/%s.smoothSpharmSampling.txt" % (sampleDir, id)
            samplingMeshOutput = "%s/%s.spharmSampling.vtk" % (sampleDir, id)
            originalMeshOutput = "%s/%s.spharmOriginal.vtk" % (sampleDir, id)
            inputMesh = "%s/%s.subj.SPHARM.vtk" % (meshDir, id)


        #samplingCmd = "{MeshPointIntensitySamplingPath} --workDir {workdir} --inputAsPhysicalCoord {extra_args} --distanceVector {distanceVector} -i nn -a {samplingTxt} -m {samplingMeshOutput} --smoothedAttributeOutput {smoothSamplingTxt} --originalMeshOutput {originalMeshOutput} {inputMeasurement} {inputMesh}"
        #exeCmd = samplingCmd.format(**locals())
        # exeCmd = samplingCmd % (outputDir, distanceVector, samplingTxt, samplingMeshOutput, smoothSamplingTxt, originalMeshOutput, inputMeasurement, inputMesh)
        #system.run_process(exeCmd,verbose=True)

        if system.is_file_newer(smoothSamplingTxt, inputMeasurement, opts.overwrite) or \
                system.is_file_newer(smoothSamplingTxt, samplingTxt, opts.overwrite) or \
                system.is_file_newer(smoothSamplingTxt, samplingMeshOutput, opts.overwrite):
            useStreamLine = corr_opt == "spharm_sampling"
            if (not useStreamLine):
                exeCmd = "{pathKmesh} -sampleImage {inputMeasurement} {inputMesh} {samplingMeshOutput} " +\
                            "-outputScalarName Thickness -zrotate"
                exeCmd = exeCmd.format(**locals())
                system.run_process(exeCmd,verbose=True)

                exeCmd = "{pathKmesh} -smoothScalars {samplingMeshOutput} {samplingMeshOutput} " +\
                            "-scalarName Thickness -outputScalarName smoothThickness -iter 3"
                exeCmd = exeCmd.format(**locals())
                system.run_process(exeCmd,verbose=True)

                exeCmd = "{pathKmesh} -exportScalars {samplingMeshOutput} {samplingTxt} -scalarName Thickness"
                exeCmd = exeCmd.format(**locals())
                system.run_process(exeCmd,verbose=True)

                exeCmd = "{pathKmesh} -exportScalars {samplingMeshOutput} {smoothSamplingTxt} " +\
                            "-scalarName smoothThickness "
                exeCmd = exeCmd.format(**locals())
                system.run_process(exeCmd,verbose=True)
            else:
                gradientFile = "{outputDir}/Processing/1.MeasurementandSPHARM/" +\
                        "{id}/laplacianGradient.nrrd".format(**locals())
                gradientVTIFile = "{outputDir}/Processing/1.MeasurementandSPHARM/" +\
                        "{id}/laplacianGradient.vti".format(**locals())
                streamLineFile = "{outputDir}/Processing/1.MeasurementandSPHARM/" +\
                        "{id}/stream_lines.vtp".format(**locals())

                # vti image creation
                exeCmd = "{pathKmesh} -vti {gradientFile} {gradientVTIFile} -attrDim 3"
                exeCmd = exeCmd.format(**locals())
                system.run_process(exeCmd,verbose=True)

                # RK45 integration
                exeCmd = "{pathKmesh} -zrotate -traceDirection backward -traceStream " +\
                    "{gradientVTIFile} {inputMesh} {streamLineFile} {samplingMeshOutput}"
                exeCmd = exeCmd.format(**locals())
                system.run_process(exeCmd,verbose=True)

                # scalar export
                exeCmd = "{pathKmesh} -scalarName Length -exportScalars " +\
                    "{samplingMeshOutput} {smoothSamplingTxt}"
                exeCmd = exeCmd.format(**locals())
                system.run_process(exeCmd,verbose=True)