Пример #1
0
    def _run_interface(self, runtime):
        #tmpDir = tempfile.mkdtemp()
        tmpDir = os.getcwd() + os.sep + 'tmp_nLinReg'
        os.mkdir(tmpDir)
        source = self.inputs.in_source_file
        target = self.inputs.in_target_file
        s_base = basename(os.path.splitext(source)[0])
        t_base = basename(os.path.splitext(target)[0])
        if not isdefined(self.inputs.out_file_xfm):
            self.inputs.out_file_xfm = fname_presuffix(os.getcwd()+os.sep+s_base+"_TO_"+t_base, suffix=self._suffix+'xfm', use_ext=False)
        if not isdefined(self.inputs.out_file_img):
            self.inputs.out_file_img = fname_presuffix(os.getcwd()+os.sep+s_base+"_TO_"+t_base, suffix=self._suffix)

        prev_xfm = None
        if self.inputs.init_file_xfm:
            prev_xfm = self.inputs.init_file_xfm

        if self.inputs.normalize:
            inorm_target = tmpDir+"/"+t_base+"_inorm.mnc"
            inorm_source = tmpDir+"/"+s_base+"_inorm.mnc"

            run_resample = minc.Resample();
            run_resample.inputs.in_file=target
            run_resample.inputs.out_file=inorm_target
            run_resample.inputs.like=source

            if self.inputs.verbose:
                print run_resample.cmdline
            if self.inputs.run:
                run_resample.run()

            run_inormalize = InormalizeCommand();
            run_inormalize.inputs.in_file=source
            run_inormalize.inputs.out_file=inorm_source
            run_inormalize.inputs.model_file=inorm_target
            if self.inputs.verbose:
                print run_inormalize.cmdline
            if self.inputs.run:
                run_inormalize.run()
        else:
            inorm_target = target
            inorm_source = source 


        class tracc_args:
            def __init__(self, nonlinear, weight, stiffness, similarity, sub_lattice):
                # self.debug=debug
                self.nonlinear=nonlinear
                self.weight=weight
                self.stiffness=stiffness
                self.similarity=similarity
                self.sub_lattice=sub_lattice

        class conf:
            def __init__(self, step, blur_fwhm, iterations, lattice_diam):
                self.step=step
                self.blur_fwhm=blur_fwhm
                self.iterations=iterations
                self.lattice_diam=lattice_diam

        conf1 = conf(32,16,20,96)
        conf2 = conf(16,8,20,48)
        conf3 = conf(12,6,20,36)
        conf4 = conf(8,4,20,24)
        conf5 = conf(6,3,20,18)
        conf6 = conf(4,2,10,12)
        conf_list = [ conf1, conf2, conf3, conf4, conf5, conf6 ]
        nonlin_tracc_args = tracc_args('corrcoeff',1.0,1,0.3,6)

        i=1
        for confi in conf_list:
            tmp_source=tmpDir+"/"+s_base+"_fwhm.mnc"
            tmp_source_blur_base=tmpDir+"/"+s_base+"_fwhm"+str(confi.blur_fwhm)
            tmp_source_blur=tmpDir+"/"+s_base+"_fwhm"+str(confi.blur_fwhm)+"_blur.mnc"
            tmp_target=tmpDir+"/"+t_base+"_fwhm.mnc"
            tmp_target_blur_base=tmpDir+"/"+t_base+"_fwhm"+str(confi.blur_fwhm)
            tmp_target_blur=tmpDir+"/"+t_base+"_fwhm"+str(confi.blur_fwhm)+"_blur.mnc"
            tmp_xfm = tmpDir+"/"+t_base+"_conf"+str(i)+".xfm";
            tmp_rspl_vol = tmpDir+"/"+s_base+"_conf"+str(i)+".mnc";

            print '-------+------- iteration'+str(i)+' -------+-------'
            print '       | steps : \t\t'+ str(confi.step)
            print '       | blur_fwhm : \t\t'+ str(confi.blur_fwhm)
            print '       | nonlinear : \t\t'+ str(nonlin_tracc_args.nonlinear)
            print '       | weight : \t\t'+ str(nonlin_tracc_args.weight)
            print '       | stiffness : \t\t'+ str(nonlin_tracc_args.stiffness)
            print '       | similarity : \t\t'+ str(nonlin_tracc_args.similarity)
            print '       | sub_lattice : \t\t'+ str(nonlin_tracc_args.sub_lattice)
            print '       | source : \t\t'+ tmp_source_blur
            print '       | target : \t\t'+ tmp_target_blur
            print '       | xfm : \t\t\t'+ tmp_xfm
            print '       | out : \t\t\t'+ tmp_rspl_vol
            print '\n'

            if self.inputs.in_source_mask and self.inputs.in_target_mask:
                if os.path.isfile(self.inputs.in_source_mask) and not os.path.exists(tmpDir+"/"+s_base+"_masked.mnc"):
                    source = tmpDir+"/"+s_base+"_masked.mnc"
                    run_calc = minc.Calc();
                    run_calc.inputs.input_files = [inorm_source, self.inputs.in_source_mask]
                    run_calc.inputs.output_file = source
                    run_calc.inputs.expression='A[1] > 0.5 ? A[0] : A[1]'
                    if self.inputs.verbose:
                        print run_calc.cmdline
                    if self.inputs.run:
                        run_calc.run()

                if os.path.isfile(self.inputs.in_target_mask) and not os.path.exists(tmpDir+"/"+t_base+"_masked.mnc"):
                    target = tmpDir+"/"+t_base+"_masked.mnc"
                    run_calc.inputs.in_file = [inorm_target, self.inputs.in_target_mask]
                    run_calc.inputs.out_file = target
                    run_calc.inputs.expression='A[1] > 0.5 ? A[0] : A[1]'
                    if self.inputs.verbose:
                        print run_calc.cmdline
                    if self.inputs.run:
                        run_calc.run()
            else:
                source = inorm_source
                target = inorm_target


            run_smooth = minc.Blur();
            run_smooth.inputs.input_file=target
            run_smooth.inputs.fwhm=confi.blur_fwhm
            run_smooth.inputs.output_file_base=tmp_target_blur_base
            if self.inputs.verbose:
                print run_smooth.cmdline
            if self.inputs.run:
                run_smooth.run()

            run_smooth = minc.Blur();
            run_smooth.inputs.input_file=source
            run_smooth.inputs.fwhm=confi.blur_fwhm
            run_smooth.inputs.output_file_base=tmp_source_blur_base
            if self.inputs.verbose:
                print run_smooth.cmdline
            if self.inputs.run:
                run_smooth.run()

            run_tracc = TraccCommand();
            run_tracc.inputs.in_source_file=tmp_source_blur
            run_tracc.inputs.in_target_file=tmp_target_blur
            run_tracc.inputs.steps=str(confi.step)+' '+str(confi.step)+' '+str(confi.step)
            run_tracc.inputs.iterations=confi.iterations
            run_tracc.inputs.nonlinear=nonlin_tracc_args.nonlinear
            run_tracc.inputs.weight=nonlin_tracc_args.weight
            run_tracc.inputs.stiffness=nonlin_tracc_args.stiffness
            run_tracc.inputs.similarity=nonlin_tracc_args.similarity
            run_tracc.inputs.sub_lattice=nonlin_tracc_args.sub_lattice
            run_tracc.inputs.lattice=str(confi.lattice_diam)+' '+str(confi.lattice_diam)+' '+str(confi.lattice_diam)
            if i == len(conf_list):
                run_tracc.inputs.out_file_xfm=self.inputs.out_file_xfm
            else :
                run_tracc.inputs.out_file_xfm=tmp_xfm
            print "\nOutput of minctracc:" +  run_tracc.inputs.out_file_xfm + "\n"
            if i == 1:
                run_tracc.inputs.identity=True
            if prev_xfm:
                run_tracc.inputs.transformation=prev_xfm
            if self.inputs.in_source_mask:
                run_tracc.inputs.in_source_mask=self.inputs.in_source_mask
            if self.inputs.in_target_mask:
                run_tracc.inputs.in_target_mask=self.inputs.in_target_mask

            if self.inputs.verbose:
                print run_tracc.cmdline
            run_tracc.run()

            
            if i == len(conf_list):
                prev_xfm = self.inputs.out_file_xfm
            else :
                prev_xfm = tmp_xfm
            
            run_resample = minc.Resample();
            run_resample.inputs.input_file=source
            run_resample.inputs.output_file=tmp_rspl_vol
            run_resample.inputs.like=target
            run_resample.inputs.transformation=run_tracc.inputs.out_file_xfm
            if self.inputs.verbose:
                print run_resample.cmdline
            if self.inputs.run:
                run_resample.run()

            i += 1


        if isdefined(self.inputs.init_file_xfm):
            run_concat = minc.XfmConcat();
            run_concat.inputs.input_files=[ self.inputs.init_xfm, prev_xfm  ]
            run_concat.inputs.output_file=self.inputs.out_file_xfm
            if self.inputs.verbose:
                print run_concat.cmdline
            if self.inputs.run:
                run_concat.run()

        # else:
        #     if self.inputs.verbose:
        #         cmd=' '.join(['cp', prev_xfm, self.inputs.out_file_xfm])
        #         print(cmd)
        #     if self.inputs.run:
        #         shutil.copy(prev_xfm, self.inputs.out_file_xfm)

        print '\n-+- creating '+self.inputs.out_file_img+' using '+self.inputs.out_file_xfm+' -+-\n'
        run_resample = minc.Resample();
        run_resample.inputs.input_file=self.inputs.in_source_file
        run_resample.inputs.output_file=self.inputs.out_file_img
        run_resample.inputs.like=self.inputs.in_target_file
        run_resample.inputs.transformation=self.inputs.out_file_xfm
        if self.inputs.verbose:
            print run_resample.cmdline
        if self.inputs.run:
            run_resample.run()

        #shutil.rmtree(tmpDir)
        return runtime
Пример #2
0
    def _run_interface(self, runtime):
        #tmpDir = tempfile.mkdtemp()
        tmpDir = os.getcwd() + os.sep + 'tmp_PETtoT1LinRegRunning'  #tempfile.mkdtemp()
        os.mkdir(tmpDir)
        source = self.inputs.in_source_file
        target = self.inputs.in_target_file
        s_base = basename(os.path.splitext(source)[0])
        t_base = basename(os.path.splitext(target)[0])


        if not isdefined(self.inputs.out_file_xfm):
            self.inputs.out_file_xfm = os.getcwd()+os.sep+s_base+self._suffix+'.xfm'
        if not isdefined(self.inputs.out_file_xfm_invert):
            self.inputs.out_file_xfm_invert = os.getcwd()+os.sep+t_base+self._suffix+'.xfm'
        if not isdefined(self.inputs.out_file_img):
            self.inputs.out_file_img = os.getcwd()+os.sep+s_base+self._suffix+ '.mnc'

        #print("\n\n\n")
        #print( self.inputs.out_file_img )
        #print("\n\n\n")
        #exit(0)

        prev_xfm = None
        if self.inputs.init_file_xfm:
            prev_xfm = self.inputs.init_file_xfm

        source = self.inputs.in_source_file
        target = self.inputs.in_target_file
        s_base = basename(os.path.splitext(source)[0])
        t_base = basename(os.path.splitext(target)[0])

        if self.inputs.in_source_mask and self.inputs.in_target_mask:
            if os.path.isfile(self.inputs.in_source_mask):
                source = tmpDir+"/"+s_base+"_masked.mnc"
                #run_calc = CalcCommand();
                run_calc = minc.Calc();
                #MIC run_calc.inputs.in_file = [self.inputs.in_source_file, self.inputs.in_source_mask]
                run_calc.inputs.input_files = [self.inputs.in_source_file, self.inputs.in_source_mask]
                #MIC run_calc.inputs.out_file = source
                run_calc.inputs.output_file = source

                print 'Source Mask:', source
                # run_calc.inputs.expression='if(A[1]>0.5){out=A[0];}else{out=A[1];}'
                run_calc.inputs.expression='A[1] > 0.5 ? A[0] : A[1]'
                if self.inputs.verbose:
                    print run_calc.cmdline
                if self.inputs.run:
                    run_calc.run()


            if os.path.isfile(self.inputs.in_target_mask):
                target = tmpDir+"/"+t_base+"_masked.mnc"
                run_calc.inputs.input_files = [self.inputs.in_target_file, self.inputs.in_target_mask]
                #run_calc.inputs.out_file = target
                run_calc.inputs.output_file = target

                print 'Target Mask:', target
                run_calc.inputs.expression='A[1] > 0.5 ? A[0] : A[1]'
                if self.inputs.verbose:
                    print run_calc.cmdline
                if self.inputs.run:
                    run_calc.run()

        class conf:
            def __init__(self, type_, est, blur_fwhm_target, blur_fwhm_source, steps, tolerance, simplex, lsq, blur_gradient):
                self.type_=type_
                self.est=est
                self.blur_fwhm_target=blur_fwhm_target
                self.blur_fwhm_source=blur_fwhm_source
                self.steps=steps
                self.tolerance=tolerance
                self.simplex=simplex
                self.lsq=lsq
		self.blur_gradient=blur_gradient
        
        if isdefined( self.inputs.lsq ) :
            lsq0=self.inputs.lsq    
            lsq1=self.inputs.lsq    
            lsq2=self.inputs.lsq    
            lsq3=self.inputs.lsq    
            lsq4=self.inputs.lsq    
        else :
            lsq0="lsq6"
            lsq1="lsq6"    
            lsq2="lsq7"    
            lsq3="lsq9"    
            lsq4="lsq12"    

        #conf1 = conf("blur", "-est_translations", 10, 6, "8 8 8", 0.01, 8, lsq1)
        #conf2 = conf("blur", "", 6, 6, "4 4 4", 0.004, 6, lsq2)
        #conf3 = conf("blur", "", 4, 4, "2 2 2", 0.002, 4, lsq3)
        
        conf0 = conf("blur", "-est_translations", 16, 16, "8 8 8", 0.01, 32, lsq0, False)
        conf1 = conf("blur", "", 8, 8, "4 4 4", 0.004, 16, lsq1, False)
        conf2 = conf("blur", "", 4, 4, "4 4 4", 0.004, 8, lsq2, False)
        conf3 = conf("blur", "", 4, 4, "4 4 4", 0.004, 4, lsq3, True)
        conf4 = conf("blur", "", 2, 2, "2 2 2", 0.004, 2, lsq4, True)

        #conf_list = [ conf0 ] #, conf1, conf2, conf3, conf4 ]
        conf_list = [  conf0, conf1, conf2, conf3, conf4 ]

        i=1
        for confi in conf_list:
            tmp_source=tmpDir+"/"+s_base+"_fwhm"+str(confi.blur_fwhm_source)
            tmp_source_blur_base=tmpDir+"/"+s_base+"_fwhm"+str(confi.blur_fwhm_source)
            tmp_source_blur=tmpDir+"/"+s_base+"_fwhm"+str(confi.blur_fwhm_source)+"_"+confi.type_+".mnc"
            tmp_target=tmpDir+"/"+t_base+"_fwhm"+str(confi.blur_fwhm_target)
            tmp_target_blur_base=tmpDir+"/"+t_base+"_fwhm"+str(confi.blur_fwhm_target)
            tmp_target_blur=tmpDir+"/"+t_base+"_fwhm"+str(confi.blur_fwhm_target)+"_"+confi.type_+".mnc"
            tmp_xfm = tmpDir+"/"+t_base+"_conf"+str(i)+".xfm";
            tmp_rspl_vol = tmpDir+"/"+s_base+"_conf"+str(i)+".mnc";

            print '-------+------- iteration'+str(i)+' -------+-------'
            print '       | steps : \t\t'+ confi.steps
            print '       | lsq : \t\t'+ confi.lsq
            print '       | blur_fwhm_mri : \t'+ str(confi.blur_fwhm_target)
            print '       | blur_fwhm_pet : \t'+ str(confi.blur_fwhm_source)
            print '       | simplex : \t\t'+ str(confi.simplex)
            print '       | source : \t\t'+ tmp_source_blur
            print '       | target : \t\t'+ tmp_target_blur
            print '       | xfm : \t\t\t'+ tmp_xfm
            print '       | out : \t\t\t'+ tmp_rspl_vol
            print '\n'

            run_smooth = minc.Blur();
            run_smooth.inputs.input_file=target
            run_smooth.inputs.fwhm=confi.blur_fwhm_target
            run_smooth.inputs.output_file_base=tmp_target_blur_base
            if confi.blur_gradient :
                run_smooth.inputs.gradient=True
            
            print run_smooth.cmdline
            run_smooth.run()

            run_smooth = minc.Blur();
            run_smooth.inputs.input_file=source
            run_smooth.inputs.fwhm=confi.blur_fwhm_source
            run_smooth.inputs.output_file_base=tmp_source_blur_base
            if confi.blur_gradient :
                run_smooth.inputs.gradient=True
            run_smooth.inputs.no_apodize=True
            
            print run_smooth.cmdline
            run_smooth.run()

            run_tracc = TraccCommand();
            run_tracc.inputs.in_source_file=tmp_source_blur
            run_tracc.inputs.in_target_file=tmp_target_blur
            run_tracc.inputs.out_file_xfm=tmp_xfm
            run_tracc.inputs.objective_func=self.inputs.metric 
            run_tracc.inputs.steps=confi.steps
            run_tracc.inputs.simplex=confi.simplex
            run_tracc.inputs.tolerance=confi.tolerance
            run_tracc.inputs.est=confi.est
            run_tracc.inputs.lsq=confi.lsq 
            if prev_xfm:
                run_tracc.inputs.transformation=prev_xfm
            if self.inputs.in_source_mask:
                run_tracc.inputs.in_source_mask=self.inputs.in_source_mask
            if self.inputs.in_target_mask:
                run_tracc.inputs.in_target_mask=self.inputs.in_target_mask

            print run_tracc.cmdline
            if self.inputs.run:
                run_tracc.run()

            run_resample = minc.Resample();
            run_resample.inputs.input_file=source
            run_resample.inputs.output_file=tmp_rspl_vol
            run_resample.inputs.like=target
            run_resample.inputs.transformation=tmp_xfm
            if self.inputs.verbose:
                print run_resample.cmdline
            if self.inputs.run:
                run_resample.run()

            prev_xfm = tmp_xfm
            i += 1

            print '\n'

     
        #No need for this because the final xfm file includes the initial one
        if isdefined(self.inputs.error):
            ###Create rotation xfm files based on transform error
            transformNode = pe.Node(interface=rsl.param2xfmInterfaceCommand(), name='transformNode')
            transformNode.inputs.error = self.inputs.error 
            
            ###
            run_concat = minc.ConcatCommand();
            run_concat.inputs.in_file=transformNode.inputs.output_file
            run_concat.inputs.in_file_2=tmp_xfm
            run_concat.inputs.out_file=self.inputs.out_file_xfm
            print run_concat.cmdline
            run_concat.run()
            
            tmp_xfm = self.inputs.out_file_xfm
            
            misregister_pet = minc.Resample();
            misregister_pet.inputs.input_file=self.inputs.out_file_img
            #misregister_pet.inputs.output_file=tmpDir+os.sep+"temp_pet_4d_misaligned.mnc" 
            misregister_pet.inputs.use_input_sampling=True
            misregister_pet.inputs.transformation=self.inputs.out_file_xfm
            shutil.copy(self.inputs.output_file, self.inputs.out_file_img)
        else :
            cmd=' '.join(['cp', tmp_xfm, self.inputs.out_file_xfm])
            print(cmd)
            shutil.copy(tmp_xfm, self.inputs.out_file_xfm)

        #Invert transformation
        run_xfmpetinvert = minc.XfmInvert();
        run_xfmpetinvert.inputs.input_file = self.inputs.out_file_xfm
        run_xfmpetinvert.inputs.output_file = self.inputs.out_file_xfm_invert
        if self.inputs.verbose:
            print run_xfmpetinvert.cmdline
        if self.inputs.run:
            run_xfmpetinvert.run()



        #if self.inputs.out_file_img:
        print '\n-+- Resample 3d PET image -+-\n'
        run_resample = minc.Resample();
        run_resample.inputs.input_file=self.inputs.in_source_file
        run_resample.inputs.output_file=self.inputs.out_file_img
        run_resample.inputs.like=self.inputs.in_target_file
        run_resample.inputs.transformation=self.inputs.out_file_xfm

        print '\n\n', self.inputs.out_file_xfm
        print self.inputs.out_file_xfm_invert
        print self.inputs.out_file_img, '\n\n'

        if self.inputs.verbose:
            print run_resample.cmdline
        if self.inputs.run:
            run_resample.run()

        #shutil.rmtree(tmpDir)
        return runtime