Example #1
0
def test():
    nx=17
    DataTrue=np.zeros((1,1,nx,nx),np.float32)
    DataTrue[0,0,nx/2,nx/2]=1.
    DataPSF=DataTrue.copy()
    PSF=ModFFTW.ConvolveGaussian(DataPSF,CellSizeRad=1,GaussPars=[(1.,1.,0.)])

    DataTrue.fill(0)
    DataTrue[0,0,nx/2,nx/2]=1.
    DataTrue[0,0,nx/2+5,nx/2+4]=1.
    #DataTrue[0,0,nx/2,nx/2]=1.
    DataTrue=ModFFTW.ConvolveGaussian(DataTrue,CellSizeRad=1,GaussPars=[(1,.5,0.)])
    #DataTrue=np.random.randn(*DataTrue.shape)

    DataTrue[0,0,nx/2-5,nx/2+4]+=2.
    DataTrue[0,0,nx/2-2,nx/2+2]+=2.
    # DataTrue+=np.random.randn(*DataTrue.shape)*0.05

    Dirty=ModFFTW.ConvolveGaussian(DataTrue,CellSizeRad=1,GaussPars=[(1.,1.,0.)])
    #DataConv=ModFFTW.ConvolveGaussian(DataTrue,CellSizeRad=1,GaussPars=[(1.,1.,0.)])
    #DataConv=DataTrue
    #IndDataTrue=ArrayToInd(DataConv)
    FreqsInfo=None

    _,_,indx,indy=np.where(DataTrue>1e-1)
    ListPixParms=[ij for ij in zip(indx,indy)]

    _,_,indx,indy=np.where(Dirty>1e-1)
    ListPixData=[ij for ij in zip(indx,indy)]

    CEv=ClassEvolveGA(Dirty,PSF,FreqsInfo,ListPixData=ListPixData,ListPixParms=ListPixParms)
    CEv.ArrayMethodsMachine.DataTrue=DataTrue


    return CEv
Example #2
0
def testMF():
    nx=17

    nf=2
    nu=np.linspace(100,200,nf)*1e6
    FreqsInfo=None
    FreqsInfo={"freqs":[nu[0:1],nu[1:2]],"WeightChansImages":np.array([0.5, 0.5])}

    # nf=1
    # nu=np.linspace(100,200,nf)*1e6
    # FreqsInfo=None

    PSF=np.zeros((nf,1,nx,nx),np.float32)
    PSF[:,0,nx/2,nx/2]=1.
    PSF=ModFFTW.ConvolveGaussian(PSF,CellSizeRad=1,GaussPars=[(1.,1.,0.)]*nf)

    DataModel=np.zeros((1,1,nx,nx),np.float32)
    Alpha=np.zeros((1,1,nx,nx),np.float32)

    DataModel[0,0,nx/2,nx/2]=1.
    #Alpha[0,0,:,:]=-.8

    #DataModel[0,0,nx/2+5,nx/2+4]=1.
    DataModel=ModFFTW.ConvolveGaussian(DataModel,CellSizeRad=1,GaussPars=[(1,.5,0.)])
    #Alpha[0,0,nx/2,nx/2]=-.8

    DataModel[0,0,nx/2,nx/2]=+1.
    Alpha[0,0,nx/2,nx/2]=.8
    DataModel[0,0,nx/2-5,nx/2+4]+=2.
    #DataModel[0,0,nx/2-2,nx/2+2]+=2.
    Alpha[0,0,nx/2-5,nx/2+4]=-.8

    FreqRef=np.mean(nu)
    nur=nu.reshape((2,1,1,1))
    DataModelMF=DataModel*(nur/FreqRef)**Alpha

    Dirty=ModFFTW.ConvolveGaussian(DataModelMF,CellSizeRad=1,GaussPars=[(1.,1.,0.)]*nf)

    #DataConv=ModFFTW.ConvolveGaussian(DataTrue,CellSizeRad=1,GaussPars=[(1.,1.,0.)])
    #DataConv=DataTrue
    #IndDataTrue=ArrayToInd(DataConv)

    indx,indy=np.where(Dirty[0,0]>1e-1)
    ListPixParms=[ij for ij in zip(indx,indy)]
    indx,indy=np.where(Dirty[0,0]>1e-2)
    ListPixParms=[ij for ij in zip(indx,indy)]
    ListPixData=ListPixParms

    GD={"GAClean":{"GASolvePars":["S","Alpha"]}}


    CEv=ClassEvolveGA(Dirty,PSF,FreqsInfo,ListPixParms=ListPixParms,ListPixData=ListPixData,GD=GD)
    CEv.ArrayMethodsMachine.DataTrue=DataModelMF
    CEv.ArrayMethodsMachine.PM.DicoIParm["S"]["DataModel"]=DataModel


    return CEv
Example #3
0
 def restoreFittedBeam(rot):
     imgSize = 256
     cellSize = np.deg2rad(4./3600.)
     params = (10, 5, rot) #maj, min, theta
     #create input with code borrowed from Tigger:
     xx,yy = np.meshgrid(np.arange(0,imgSize),np.arange(0,imgSize))
     inp = gauss2d([1,imgSize/2,imgSize/2,params[1],params[0],params[2]],circle=0,rotate=1,vheight=0)(xx,yy)
     inp = inp.reshape(1,1,imgSize,imgSize)
     #fit
     fittedParams = tuple((fitter.FitCleanBeam(inp[0, 0, :, :]) *
                           np.array([cellSize, cellSize, 1])).tolist())
     #restore fitted clean beam with an FFT convolution:
     delta = np.zeros([1, 1, imgSize, imgSize])
     delta[0, 0, imgSize // 2, imgSize // 2] = 1
     rest, _ = fftconvolve.ConvolveGaussianScipy(delta,
                                              Sig=5,
                                              GaussPar=fittedParams)
     
     rest = fftconvolve.ConvolveGaussian(shareddict={"in":delta,
                                                     "out":delta},
                                                     field_in="in",
                                                     field_out="out",
                                          ch=0,
                                          CellSizeRad=cellSize,
                                          GaussPars_ch=fittedParams,
                                          Normalise=False)
                                          
     assert np.allclose(inp, rest, rtol=1e-2, atol=1e-2)
    def MakeMultiScaleCube(self):
        if self.CubePSFScales is not None: return
        print("Making MultiScale PSFs...", file=log)
        LScales = self.GD["HMP"]["Scales"]
        if 0 in LScales: LScales.remove(0)
        LRatios = self.GD["HMP"]["Ratios"]
        NTheta = self.GD["HMP"]["NTheta"]

        _, _, nx, ny = self.SubPSF.shape
        NScales = len(LScales)
        NRatios = len(LRatios)
        CubePSFScales = np.zeros(
            (NScales + 1 + NRatios * NTheta * (NScales), nx, ny))

        Scales = np.array(LScales)
        Ratios = np.array(LRatios)

        self.ListScales = []
        CubePSFScales[0, :, :] = self.SubPSF[0, 0, :, :]
        self.ListScales.append({"ModelType": "Delta"})
        iSlice = 1

        Support = 61

        for i in range(NScales):
            Minor = Scales[i] / (2. * np.sqrt(2. * np.log(2.)))
            Major = Minor
            PSFGaussPars = (Major, Minor, 0.)
            CubePSFScales[iSlice, :, :] = ModFFTW.ConvolveGaussian(
                self.SubPSF, CellSizeRad=1., GaussPars=[PSFGaussPars])[0, 0]
            Gauss = ModFFTW.GiveGauss(Support,
                                      CellSizeRad=1.,
                                      GaussPars=PSFGaussPars)
            self.ListScales.append({
                "ModelType": "Gaussian",
                "Model": Gauss,
                "ModelParams": PSFGaussPars,
                "Scale": i
            })

            iSlice += 1

        Theta = np.arange(0., np.pi - 1e-3, np.pi / NTheta)

        for iScale in range(NScales):
            for ratio in Ratios:
                for th in Theta:
                    Minor = Scales[iScale] / (2. * np.sqrt(2. * np.log(2.)))
                    Major = Minor * ratio
                    PSFGaussPars = (Major, Minor, th)
                    CubePSFScales[iSlice, :, :] = ModFFTW.ConvolveGaussian(
                        self.SubPSF, CellSizeRad=1.,
                        GaussPars=[PSFGaussPars])[0, 0]
                    Max = np.max(CubePSFScales[iSlice, :, :])
                    CubePSFScales[iSlice, :, :] /= Max
                    # pylab.clf()
                    # pylab.subplot(1,2,1)
                    # pylab.imshow(CubePSFScales[0,:,:],interpolation="nearest")
                    # pylab.subplot(1,2,2)
                    # pylab.imshow(CubePSFScales[iSlice,:,:],interpolation="nearest")
                    # pylab.title("Scale = %s"%str(PSFGaussPars))
                    # pylab.draw()
                    # pylab.show(False)
                    # pylab.pause(0.1)
                    iSlice += 1
                    Gauss = ModFFTW.GiveGauss(
                        Support, CellSizeRad=1., GaussPars=PSFGaussPars) / Max
                    self.ListScales.append({
                        "ModelType": "Gaussian",
                        "Model": Gauss,
                        "ModelParams": PSFGaussPars,
                        "Scale": iScale
                    })

        # Max=np.max(np.max(CubePSFScales,axis=1),axis=1)
        # Max=Max.reshape((Max.size,1,1))
        # CubePSFScales=CubePSFScales/Max

        self.CubePSFScales = np.float32(CubePSFScales)
        self.WeightWidth = 6
        CellSizeRad = 1.
        PSFGaussPars = (self.WeightWidth, self.WeightWidth, 0.)
        self.WeightFunction = ModFFTW.GiveGauss(self.SubPSF.shape[-1],
                                                CellSizeRad=1.,
                                                GaussPars=PSFGaussPars)
        #self.WeightFunction.fill(1)
        self.SupWeightWidth = 3. * self.WeightWidth
        print("   ... Done", file=log)
Example #5
0
    def giveBrutalRestored(self, DicoResidual):
        print >> log, "  Running Brutal deconvolution..."
        ListSilentModules = [
            "ClassImageDeconvMachineMSMF", "ClassPSFServer",
            "ClassMultiScaleMachine", "GiveModelMachine",
            "ClassModelMachineMSMF", "ClassImageDeconvMachineHogbom",
            "ClassModelMachineHogbom"
        ]
        # MyLogger.setSilent(ListSilentModules)
        self.DicoDirty = DicoResidual
        self.Orig_MeanDirty = self.DicoDirty["MeanImage"].copy()
        self.Orig_Dirty = self.DicoDirty["ImageCube"].copy()

        if self.NoiseMapReShape is not None:
            print >> log, "Deconvolving on SNR map"
            self.DeconvMachine.RMSFactor = 0

        self.DeconvMachine.Init(
            PSFVar=self.DicoVariablePSF,
            PSFAve=self.DicoVariablePSF["EstimatesAvgPSF"][-1],
            GridFreqs=self.GridFreqs,
            DegridFreqs=self.DegridFreqs,
            RefFreq=self.RefFreq)

        if self.NoiseMapReShape is not None:
            self.DeconvMachine.setNoiseMap(self.NoiseMapReShape,
                                           PNRStop=self.GD["Mask"]["SigTh"])

        # # #########################
        # # debug
        # MaskImage=image("image_dirin_SSD_test.dirty.fits.mask.fits").getdata()
        # nch,npol,_,_=MaskImage.shape
        # MaskArray=np.zeros(MaskImage.shape,np.bool8)
        # for ch in range(nch):
        #     for pol in range(npol):
        #         MaskArray[ch,pol,:,:]=np.bool8(MaskImage[ch,pol].T[::-1].copy())[:,:]
        # self.DeconvMachine.setMask(np.bool8(1-MaskArray))
        # # #########################

        self.DeconvMachine.Update(self.DicoDirty, DoSetMask=False)
        self.DeconvMachine.updateRMS()

        # ModConstructor = ClassModModelMachine(self.GD)
        # ModelMachine = ModConstructor.GiveMM(Mode=self.GD["Deconv"]["Mode"])
        # #print "ModelMachine"
        # #time.sleep(30)
        # self.ModelMachine=ModelMachine
        # #self.ModelMachine.DicoSMStacked=self.DicoBasicModelMachine
        # self.ModelMachine.setRefFreq(self.RefFreq,Force=True)
        # self.MinorCycleConfig["ModelMachine"] = ModelMachine
        # #self.ModelMachine.setModelShape(self.SubDirty.shape)
        # #self.ModelMachine.setListComponants(self.DeconvMachine.ModelMachine.ListScales)
        # #self.DeconvMachine.Update(self.DicoSubDirty,DoSetMask=False)
        # #self.DeconvMachine.updateMask(np.logical_not(self.SubMask))
        # self.DeconvMachine.updateModelMachine(ModelMachine)
        self.DeconvMachine.resetCounter()
        self.DeconvMachine.Deconvolve(UpdateRMS=False)

        print >> log, "  Getting model image..."
        Model = self.ModelMachine.GiveModelImage(DoAbs=True)
        if "Comp" in self.ExternalModelMachine.DicoSMStacked.keys():
            Model += np.abs(self.ExternalModelMachine.GiveModelImage())
        ModelImage = Model[0, 0]

        print >> log, "  Convolving image with beam %s..." % str(
            self.DicoVariablePSF["EstimatesAvgPSF"][1])
        #from DDFacet.ToolsDir import Gaussian

        # Sig_rad=np.max(self.DicoVariablePSF["EstimatesAvgPSF"][1][0:2])
        # Sig_pix=Sig_rad/self.DicoDirty["ImageInfo"]["CellSizeRad"]
        # Sig_pix=np.max([1,Sig_pix])#*2
        # n_pix=int(Sig_pix*4)
        # if n_pix%2==0: n_pix+=1

        # _,_,G=Gaussian.GaussianSymetric(Sig_pix,n_pix)

        # from DDFacet.ToolsDir.GiveEdges import GiveEdgesDissymetric

        # N1=G.shape[0]
        # N0x,N0y=ModelImage.shape
        # indx,indy=np.where(ModelImage!=0)
        # ModelConv=np.zeros_like(ModelImage)
        # for iComp in range(indx.size):
        #     xc,yc=indx[iComp],indy[iComp]
        #     Aedge,Bedge=GiveEdgesDissymetric((xc,yc),(N0x,N0y),(N1/2,N1/2),(N1,N1))
        #     x0d,x1d,y0d,y1d=Aedge
        #     x0p,x1p,y0p,y1p=Bedge
        #     ModelConv[x0d:x1d,y0d:y1d]+=G[x0p:x1p,y0p:y1p]*ModelImage[xc,yc]

        # # ModelConv=scipy.signal.convolve2d(ModelImage,G,mode="same")

        ModelConv = ModFFTW.ConvolveGaussian(
            {0: Model},
            0,
            0,
            0,
            CellSizeRad=self.DicoDirty["ImageInfo"]["CellSizeRad"],
            GaussPars_ch=self.DicoVariablePSF["EstimatesAvgPSF"][1])

        #GaussPar=[i*5 for i in self.DicoVariablePSF["EstimatesAvgPSF"][1]]
        #ModelConv+=ModFFTW.ConvolveGaussian(Model, CellSizeRad=self.DicoDirty["ImageInfo"]["CellSizeRad"],
        #                                    GaussPars=[GaussPar])

        self.ModelConv = ModelConv.reshape(self.DicoDirty["MeanImage"].shape)

        self.Restored = self.ModelConv + self.DicoDirty["MeanImage"]

        self.DicoDirty["MeanImage"][...] = self.Orig_MeanDirty[...]
        self.DicoDirty["ImageCube"][...] = self.Orig_Dirty[...]

        self.DeconvMachine.Reset()
        #MyLogger.setLoud(ListSilentModules)
        return self.Restored