Example #1
0
def main(options=None):
    if options == None:
        f = open(SaveName, 'rb')
        options = pickle.load(f)

    if options.OutDicoModel is None:
        raise ValueError("--OutDicoModel should be specified")
    ModConstructor = ClassModModelMachine()
    MM = ModConstructor.GiveInitialisedMMFromFile(options.InDicoModel)
    if options.MaskName:
        MM.CleanMaskedComponants(options.MaskName)

    if options.FilterNegComp:
        MM.RemoveNegComponants()

    MM.ToFile(options.OutDicoModel)
Example #2
0
    def __init__(self,BaseImageName,BeamPix=5,ResidualImName="",DoAlpha=1,
                 MaskName="",CleanNegComp=False,
                 NBands=1,
                 SmoothMode=0,MakeCorrected=1,options=None):
        self.DoAlpha=DoAlpha
        self.BaseImageName=BaseImageName
        self.BeamPix=BeamPix
        self.NBands=NBands
        self.OutName=options.OutName
        self.options=options
        self.SmoothMode=SmoothMode
        self.MakeCorrected=MakeCorrected
        self.header_dict={}
        FileDicoModel="%s.DicoModel"%BaseImageName

        # ClassModelMachine,DicoModel=GiveModelMachine(FileDicoModel)
        # self.ModelMachine=ClassModelMachine(Gain=0.1)
        # self.ModelMachine.FromDico(DicoModel)

        print("Building model machine", file=log)
        ModConstructor = ClassModModelMachine()
        self.ModelMachine=ModConstructor.GiveInitialisedMMFromFile(FileDicoModel)
        if MaskName!="":
            self.ModelMachine.CleanMaskedComponants(MaskName)
        if CleanNegComp:
            self.ModelMachine.CleanNegComponants(box=10,sig=2)


        if ResidualImName=="":
            #if "App" in self.ModeNorm:
            #    FitsFile="%s.app.residual.fits"%BaseImageName
            #else:
            #    FitsFile="%s.int.residual.fits"%BaseImageName
            ResidualImName=FitsFile="%s.app.residual.fits"%BaseImageName
        else:
            ResidualImName=FitsFile=ResidualImName

        if self.MakeCorrected:
            if self.SmoothMode:
                NormImageName="%s.MeanSmoothNorm.fits"%BaseImageName
            else:
                NormImageName="%s.Norm.fits"%BaseImageName
            

        print("Reading residual image", file=log)
        self.FitsFile=FitsFile
        im=image(FitsFile)

        c=im.coordinates()
        self.radec=c.dict()["direction0"]["crval"]
        CellSizeRad,_=c.dict()["direction0"]["cdelt"]
        self.CellSizeRad=np.abs(CellSizeRad)
        self.Cell=(self.CellSizeRad*180/np.pi)*3600
        self.CellArcSec=self.Cell

        self.ResidualData=im.getdata()
        nchan,npol,_,_=self.ResidualData.shape
        testImage=np.zeros_like(self.ResidualData)

        print("Transposing residual...", file=log)
        if ResidualImName!="":
            for ch in range(nchan):
                for pol in range(npol):
                    testImage[ch,pol,:,:]=self.ResidualData[ch,pol,:,:].T[::-1,:]#*1.0003900000000001

            
        if self.MakeCorrected:
            print("Reading beam...", file=log)
            SqrtNormImage=np.zeros_like(self.ResidualData)
            imNorm=image(NormImageName).getdata()
            print("Transposing beam...", file=log)
            for ch in range(nchan):
                for pol in range(npol):
                    SqrtNormImage[ch,pol,:,:]=np.sqrt(imNorm[ch,pol,:,:].T[::-1,:])
        else:
            SqrtNormImage=np.ones_like(self.ResidualData)

        _,_,nx,_=testImage.shape
        Nr=10000
        indx,indy=np.int64(np.random.rand(Nr)*nx),np.int64(np.random.rand(Nr)*nx)
        self.StdResidual=np.std(testImage[0,0,indx,indy])
        self.Residual=testImage
        self.SqrtNormImage=SqrtNormImage
Example #3
0
def main(options=None):
    if options==None:
        f = open(SaveName,'rb')
        options = pickle.load(f)

    SkyModel=options.SkyModel

    if "," in SkyModel:
        SMList=SkyModel.split(",")
        print>>log, "Concatenating SkyModels %s"%(str(SMList))
        ThisCat=np.load(SMList[0])
        ThisCat=ThisCat.view(np.recarray)
        ThisNDir=len(list(set(ThisCat.Cluster.tolist())))
        CurrentMaxCluster=ThisNDir
        CatList=[ThisCat]
        for SM in SMList[1::]:
            ThisCat=np.load(SM)
            ThisCat=ThisCat.view(np.recarray)
            ThisNDir=len(list(set(ThisCat.Cluster.tolist())))
            ThisCat.Cluster+=CurrentMaxCluster
            CurrentMaxCluster+=ThisNDir
            CatList.append(ThisCat)
        cat=np.concatenate(CatList)
        cat=cat.view(np.recarray)
        OutSkyModel=options.OutSkyModel
        print>>log, "Saving in %s"%(OutSkyModel)
        np.save(OutSkyModel,cat)
        SM=ClassSM.ClassSM(OutSkyModel+".npy",
                           ReName=True,
                           DoREG=True,
                           SaveNp=True)
        SM.Rename()
        SM.Save()

        return

    if options.BaseImageName!="":
        #from DDFacet.Imager.ClassModelMachine import ClassModelMachine

        FileDicoModel="%s.DicoModel"%options.BaseImageName

        # ClassModelMachine,DicoModel=GiveModelMachine(FileDicoModel)
        # MM=ClassModelMachine(Gain=0.1)
        # MM.FromDico(DicoModel)

        ModConstructor = ClassModModelMachine()
        MM=ModConstructor.GiveInitialisedMMFromFile(FileDicoModel)

        SqrtNormImage=None
        if options.ApparantFlux:
            FileSqrtNormImage="%s.Norm.fits"%options.BaseImageName
            imSqrtNormImage=image(FileSqrtNormImage)
            SqrtNormImage=imSqrtNormImage.getdata()
            nchan,npol,_,_=SqrtNormImage.shape
            for ch in range(nchan):
                for pol in range(npol):
                    SqrtNormImage[ch,pol,:,:]=(SqrtNormImage[ch,pol,:,:].T[::-1,:])
            

        

        #FitsFile="%s.model.fits"%options.BaseImageName
        FitsFile="%s.dirty.fits"%options.BaseImageName
        #MM.FromFile(DicoModel)
        if options.MaskName!="":
            MM.CleanMaskedComponants(options.MaskName)
        if options.CleanNegComp:
            MM.CleanNegComponants(box=10,sig=2)

        SkyModel=options.BaseImageName+".npy"
        # reproduce code from old ClassModelMachine
        RefFreq=MM.DicoSMStacked["RefFreq"]
        f0=RefFreq/1.5
        f1=RefFreq*1.5
        try:
            MM.setFreqMachine([f0,f1],[MM.RefFreq])
        except:
            pass # this is an old version of DDF which doesn't need this
        MM.ToNPYModel(FitsFile,SkyModel,BeamImage=SqrtNormImage)

        # SkyModel="tmpSourceCat.npy"
        # ModelImage=MM.GiveModelImage()
        # im=image(FitsFile)
        # ModelOrig=im.getdata()
        # indx,indy=np.where(ModelImage[0,0]!=ModelImage[0,0])
        # print ModelImage[0,0,indx,indy],ModelImage[0,0,indx,indy]
        # cell=abs(im.coordinates().dict()["direction0"]["cdelt"][0])*180/np.pi*3600
        # ra,dec=im.coordinates().dict()["direction0"]["crval"]
        # CasaImage=ClassCasaImage.ClassCasaimage("Model",ModelImage.shape,cell,(ra,dec))
        # CasaImage.setdata(ModelImage,CorrT=True)
        # CasaImage.ToFits()
        # CasaImage.close()

    if options.RemoveNegComp==1:
        print "Removing negative component"
        Cat=np.load(SkyModel)
        print Cat.shape
        Cat=Cat.view(np.recarray)
        Cat=Cat[Cat.I>0]
        print Cat.shape
        np.save(SkyModel,Cat)
        

        
    NCluster=int(options.NCluster)
    DoPlot=(int(options.DoPlot)==1)
    DoSelect=(int(options.DoSelect)==1)
    CMethod=int(options.CMethod)

    if DoPlot==0:
        import matplotlib
        matplotlib.use('agg')


    SM=ClassSM.ClassSM(SkyModel,ReName=True,
                       DoREG=True,SaveNp=True,
                       SelSource=DoSelect,ClusterMethod=CMethod)

    if True:
        print>>log, "Removing fake gaussians"
        Cat=SM.SourceCat
        
        indG=np.where(Cat["Gmaj"]>0)[0]
        CatSel=Cat[indG]
        Gmaj=CatSel["Gmaj"]*180/np.pi*3600
        I=CatSel["I"]
        
        ind=np.where((I/Gmaj)>3)[0]
        CatSel[ind]["Gmaj"]=0.
        CatSel[ind]["Gmin"]=0.
        CatSel[ind]["Gangle"]=0.
        CatSel[ind]["Type"]=0.
        #SM.SourceCat[indG][ind]=CatSel
        

        indN=np.arange(SM.SourceCat.shape[0])[indG][ind]
        
        SM.SourceCat["Gmaj"][indN]=0.
        SM.SourceCat["Gmin"][indN]=0.
        SM.SourceCat["Gangle"][indN]=0.
        SM.SourceCat["Type"][indN]=0.
        #print SM.SourceCat[indG][ind]
        #np.save(SkyModel,Cat)
        print>>log, "  done"

    PreCluster=options.ds9PreClusterFile
    SM.Cluster(NCluster=NCluster,DoPlot=DoPlot,PreCluster=PreCluster,FromClusterCat=options.FromClusterCat)
    SM.MakeREG()
    SM.Save()



    if options.DoPrint=="1":
        SM.print_sm2()
Example #4
0
    def giveModel(self, ListPixParms):
        T = ClassTimeIt.ClassTimeIt("giveModel")
        T.disable()
        self.setSubDirty(ListPixParms)
        T.timeit("setsub")
        ModConstructor = ClassModModelMachine(self.GD)
        ModelMachine = ModConstructor.GiveMM(Mode=self.GD["Deconv"]["Mode"])
        #print "ModelMachine"
        #time.sleep(30)
        T.timeit("giveMM")
        self.ModelMachine = ModelMachine
        #self.ModelMachine.DicoSMStacked=self.DicoBasicModelMachine
        self.ModelMachine.setRefFreq(self.RefFreq, Force=True)
        self.ModelMachine.setFreqMachine(self.GridFreqs, self.DegridFreqs)
        self.MinorCycleConfig["ModelMachine"] = ModelMachine
        self.ModelMachine.setModelShape(self.SubDirty.shape)
        #self.ModelMachine.setListComponants(self.DeconvMachine.ModelMachine.ListScales)
        T.timeit("setlistcomp")

        self.DeconvMachine.Update(self.DicoSubDirty, DoSetMask=False)
        #self.DeconvMachine.updateMask(np.logical_not(self.SubMask))
        self.DeconvMachine.updateModelMachine(ModelMachine)
        #self.DeconvMachine.resetCounter()
        T.timeit("update")
        #print "update"
        #time.sleep(30)
        self.DeconvMachine.Deconvolve()
        T.timeit("deconv %s" % str(self.DicoSubDirty["ImageCube"].shape))
        #print "deconv"
        #time.sleep(30)

        ModelImage = self.ModelMachine.GiveModelImage()
        T.timeit("getmodel")

        # import pylab
        # pylab.clf()
        # pylab.subplot(2,2,1)
        # pylab.imshow(self.DicoDirty["MeanImage"][0,0,:,:],interpolation="nearest")
        # pylab.colorbar()
        # pylab.subplot(2,2,2)
        # pylab.imshow(self.DicoSubDirty["MeanImage"][0,0,:,:],interpolation="nearest")
        # pylab.colorbar()
        # pylab.subplot(2,2,3)
        # pylab.imshow(self.SubMask,interpolation="nearest")
        # pylab.colorbar()
        # pylab.subplot(2,2,4)
        # pylab.imshow(ModelImage[0,0],interpolation="nearest")
        # pylab.colorbar()
        # pylab.draw()
        # pylab.show(False)
        # pylab.pause(0.1)

        x, y = self.ArrayPixParms.T

        # PSF,MeanPSF=self.DeconvMachine.PSFServer.GivePSF()
        # ConvModel=ClassConvMachineImages(PSF).giveConvModel(ModelImage*np.ones((self.NFreqBands,1,1,1)))
        # #T.timeit("Conv1")
        # #print "done1"
        # #ConvModel=self.giveConvModel(ModelImage*np.ones((self.NFreqBands,1,1,1)))
        # # print "done2"
        # # T.timeit("Conv2")
        # # import pylab
        # # pylab.clf()
        # # pylab.subplot(1,3,1)
        # # pylab.imshow(ConvModel[0,0],interpolation="nearest")
        # # pylab.subplot(1,3,2)
        # # pylab.imshow(ConvModel1[0,0],interpolation="nearest")
        # # pylab.subplot(1,3,3)
        # # pylab.imshow((ConvModel-ConvModel1)[0,0],interpolation="nearest")
        # # pylab.colorbar()
        # # pylab.draw()
        # # pylab.show(False)
        # # stop

        # ModelOnes=np.zeros_like(ModelImage)
        # ModelOnes[:,:,x,y]=1
        # ConvModelOnes=ClassConvMachineImages(PSF).giveConvModel(ModelOnes*np.ones((self.NFreqBands,1,1,1)))

        # SumConvModel=np.sum(ConvModel[:,:,x,y])
        # SumConvModelOnes=np.sum(ConvModelOnes[:,:,x,y])
        # SumResid=np.sum(self.DeconvMachine._CubeDirty[:,:,x,y])

        # SumConvModel=np.max([SumConvModel,1e-6])

        # factor=(SumResid+SumConvModel)/SumConvModel

        # ###############
        #fMult=1.
        #if 1.<factor<2.:
        #    fMult=factor
        fMult = 1.
        SModel = ModelImage[0, 0, x, y] * fMult
        # ###########"
        # fMult=(np.mean(SumResid))/(np.mean(SumConvModelOnes))
        # SModel=ModelImage[0,0,x,y]+ModelOnes[0,0,x,y]*fMult
        # print fMult
        # print fMult
        # print fMult
        # print fMult
        # print fMult
        # print fMult
        # print fMult
        # ############

        AModel = self.ModelMachine.GiveSpectralIndexMap()[0, 0, x, y]
        T.timeit("spec index")

        return SModel, AModel
Example #5
0
    def __init__(self,
                 GD,
                 DicoVariablePSF,
                 RefFreq,
                 GridFreqs,
                 DegridFreqs,
                 MainCache=None,
                 IdSharedMem="",
                 DoWait=False):
        self.DicoVariablePSF = DicoVariablePSF
        GD = copy.deepcopy(GD)
        self.RefFreq = RefFreq
        self.GridFreqs = GridFreqs
        self.DegridFreqs = DegridFreqs
        self.GD = GD
        self.GD["Parallel"]["NCPU"] = 1
        #self.GD["HMP"]["Alpha"]=[0,0,1]#-1.,1.,5]
        self.GD["HMP"]["Alpha"] = [-1., 1., 5]
        self.GD["Deconv"]["Mode"] = "MORESANE"

        self.GD["Deconv"]["CycleFactor"] = 0
        self.GD["Deconv"]["PeakFactor"] = 0.0
        self.GD["Deconv"]["RMSFactor"] = self.GD["GAClean"]["RMSFactorInitHMP"]

        self.GD["Deconv"]["Gain"] = self.GD["GAClean"]["GainInitHMP"]
        self.GD["Deconv"]["AllowNegative"] = self.GD["GAClean"][
            "AllowNegativeInitHMP"]
        self.GD["Deconv"]["MaxMinorIter"] = self.GD["GAClean"][
            "MaxMinorIterInitHMP"]

        logger.setSilent(SilentModules)

        self.GD["HMP"]["Scales"] = self.GD["GAClean"]["ScalesInitHMP"]

        self.GD["HMP"]["Ratios"] = []
        #self.GD["MultiScale"]["Ratios"]=[]
        self.GD["HMP"]["NTheta"] = 4

        self.GD["HMP"]["SolverMode"] = "NNLS"
        #self.GD["MultiScale"]["SolverMode"]="PI"

        self.NFreqBands = len(DicoVariablePSF["freqs"])
        MinorCycleConfig = dict(self.GD["Deconv"])
        MinorCycleConfig["NCPU"] = self.GD["Parallel"]["NCPU"]
        MinorCycleConfig["NFreqBands"] = self.NFreqBands
        MinorCycleConfig["GD"] = self.GD
        MinorCycleConfig["GridFreqs"] = self.GridFreqs
        MinorCycleConfig["DegridFreqs"] = self.DegridFreqs

        #MinorCycleConfig["RefFreq"] = self.RefFreq

        ModConstructor = ClassModModelMachine(self.GD)
        ModelMachine = ModConstructor.GiveMM(Mode="MORESANE")
        ModelMachine.setRefFreq(self.RefFreq)
        MinorCycleConfig["ModelMachine"] = ModelMachine
        #MinorCycleConfig["CleanMaskImage"]=None
        self.MinorCycleConfig = MinorCycleConfig
        self.DeconvMachine = ClassImageDeconvMachineMoresane.ClassImageDeconvMachine(
            MainCache=MainCache,
            ParallelMode=False,
            RefFreq=self.RefFreq,
            CacheFileName="HMP_Init",
            IdSharedMem=IdSharedMem,
            **self.MinorCycleConfig)
        self.GD["Mask"]["Auto"] = False
        self.GD["Mask"]["External"] = None
        self.MaskMachine = ClassMaskMachine.ClassMaskMachine(self.GD)
        self.DeconvMachine.setMaskMachine(self.MaskMachine)

        self.Margin = 50

        #print "Start 3"
        self.DeconvMachine.Init(PSFVar=self.DicoVariablePSF,
                                PSFAve=self.DicoVariablePSF["PSFSideLobes"],
                                GridFreqs=self.GridFreqs,
                                DegridFreqs=self.DegridFreqs,
                                DoWait=DoWait,
                                RefFreq=self.RefFreq)

        if DoWait:
            print "IINit3"
            time.sleep(10)
            print "Start 4"

        self.DeconvMachine.Update(self.DicoDirty, DoSetMask=False)
        if DoWait:
            print "IINit4"
            time.sleep(10)
Example #6
0
    def __init__(self,
                 GD,
                 ExternalModelMachine=None,
                 DegridFreqs=None,
                 GridFreqs=None,
                 MainCache=None):
        self.GD = copy.deepcopy(GD)

        self.MainCache = MainCache
        self.NoiseMap = None
        self.NoiseMapRestored = None
        self.NoiseMapReShape = None
        self._id_InputMap = None
        self.ExternalModelMachine = ExternalModelMachine
        self.DegridFreqs = DegridFreqs
        self.GridFreqs = GridFreqs
        self.NFreqBands = len(GridFreqs)

        # MyLogger.setSilent(ListSilentModules)
        self.RefFreq = ExternalModelMachine.RefFreq
        # self.GD["Parallel"]["NCPU"]=1
        # self.GD["HMP"]["Alpha"]=[0,0,1]#-1.,1.,5]
        self.GD["HMP"]["Alpha"] = [0, 0, 1]
        # self.GD["Deconv"]["Mode"]="HMP"
        # self.GD["Deconv"]["CycleFactor"]=0
        # self.GD["Deconv"]["PeakFactor"]=0.0
        self.GD["Deconv"]["PSFBox"] = "full"
        self.GD["Deconv"]["MaxMinorIter"] = 10000
        self.GD["Deconv"]["RMSFactor"] = 3.
        # self.GD["HMP"]["Scales"]=[0]
        self.GD["HMP"]["Ratios"] = []
        # self.GD["MultiScale"]["Ratios"]=[]
        self.GD["HMP"]["NTheta"] = 4

        # self.GD["Deconv"]["AllowNegative"]=False
        # self.GD["HMP"]["Scales"]=[0,1,2,4,8,16,32,48,64,96,128]
        # self.GD["HMP"]["SolverMode"]="NNLS"
        # self.GD["HMP"]["Support"]=91
        # self.GD["HMP"]["Taper"]=31
        # self.GD["Deconv"]["Gain"]=.3

        self.GD["HMP"]["SolverMode"] = "PI"
        self.GD["HMP"]["Scales"] = [0]
        self.GD["Deconv"]["Gain"] = .1

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

        self.GD["HMP"]["AllowResidIncrease"] = 0.1
        # self.GD["HMP"]["SolverMode"]="PI"
        MinorCycleConfig = dict(self.GD["Deconv"])
        MinorCycleConfig["NCPU"] = self.GD["Parallel"]["NCPU"]
        MinorCycleConfig["NFreqBands"] = self.NFreqBands
        MinorCycleConfig["RefFreq"] = self.RefFreq
        MinorCycleConfig["GD"] = self.GD
        # MinorCycleConfig["RefFreq"] = self.RefFreq
        # MinorCycleConfig["CleanMaskImage"]=None
        self.MinorCycleConfig = MinorCycleConfig
        if self.GD["Deconv"]["Mode"] in ["HMP", "SSD"]:
            # for SSD we need to set up the HMP ModelMachine.
            self.GD["Deconv"]["Mode"] = "HMP"
            ModConstructor = ClassModModelMachine(self.GD)
            self.ModelMachine = ModConstructor.GiveMM(
                Mode=self.GD["Deconv"]["Mode"])
            self.ModelMachine.setRefFreq(self.RefFreq)
            MinorCycleConfig["ModelMachine"] = self.ModelMachine
            self.MinorCycleConfig = MinorCycleConfig
            from DDFacet.Imager.MSMF import ClassImageDeconvMachineMSMF

            self.DeconvMachine = ClassImageDeconvMachineMSMF.ClassImageDeconvMachine(
                MainCache=self.MainCache,
                ParallelMode=True,
                CacheFileName="HMP_Masking",
                **self.MinorCycleConfig)
        elif self.GD["Deconv"]["Mode"] == "Hogbom":
            from DDFacet.Imager.HOGBOM import ClassImageDeconvMachineHogbom
            self.DeconvMachine = ClassImageDeconvMachineHogbom.ClassImageDeconvMachine(
                MainCache=self.MainCache,
                ParallelMode=True,
                CacheFileName="HMP_Masking",
                **self.MinorCycleConfig)
        else:
            raise NotImplementedError(
                "Mode %s not compatible with automasking" %
                self.GD["Deconv"]["Mode"])
    def __init__(self,
                 GD,
                 NFreqBands,
                 RefFreq,
                 MainCache=None,
                 IdSharedMem=""):
        """Constructs initializer. 
        Note that this should be called pretty much when setting up the imager,
        before APP workers are started, because the object registers APP handlers.
        """
        self.GD = copy.deepcopy(GD)
        self.GD["Parallel"]["NCPU"] = 1
        # self.GD["HMP"]["Alpha"]=[0,0,1]#-1.,1.,5]
        self.GD["HMP"]["Alpha"] = self.GD["GAClean"]["AlphaInitHMP"]
        self.GD["Deconv"]["Mode"] = "HMP"
        self.GD["Deconv"]["CycleFactor"] = 0
        self.GD["Deconv"]["PeakFactor"] = 0.0
        self.GD["Deconv"]["RMSFactor"] = self.GD["GAClean"]["RMSFactorInitHMP"]
        self.GD["Deconv"]["Gain"] = self.GD["GAClean"]["GainInitHMP"]
        self.GD["Deconv"]["AllowNegative"] = self.GD["GAClean"][
            "AllowNegativeInitHMP"]
        self.GD["Deconv"]["MaxMinorIter"] = int(
            self.GD["GAClean"]["MaxMinorIterInitHMP"])

        self.GD["HMP"]["Scales"] = self.GD["GAClean"]["ScalesInitHMP"]

        self.GD["HMP"]["Ratios"] = self.GD["GAClean"]["RatiosInitHMP"]
        # self.GD["MultiScale"]["Ratios"]=[]
        self.GD["HMP"]["NTheta"] = self.GD["GAClean"]["NThetaInitHMP"]

        # print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        # # self.GD["HMP"]["Scales"] = [0,1,2,4,8,16,24,32,48,64]
        # # self.GD["HMP"]["Taper"] = 32
        # # self.GD["HMP"]["Support"] = 32#self.GD["HMP"]["Scales"][-1]
        # self.GD["Deconv"]["RMSFactor"] = 1.
        # self.GD["Deconv"]["AllowNegative"] = True

        self.GD["HMP"]["SolverMode"] = "NNLS"
        # self.GD["MultiScale"]["SolverMode"]="PI"

        self.NFreqBands = NFreqBands
        self.RefFreq = RefFreq
        MinorCycleConfig = dict(self.GD["Deconv"])
        MinorCycleConfig["NCPU"] = self.GD["Parallel"]["NCPU"]
        MinorCycleConfig["NFreqBands"] = self.NFreqBands
        MinorCycleConfig["RefFreq"] = RefFreq

        ModConstructor = ClassModModelMachine(self.GD)
        ModelMachine = ModConstructor.GiveMM(Mode=self.GD["Deconv"]["Mode"])
        ModelMachine.setRefFreq(self.RefFreq)
        MinorCycleConfig["ModelMachine"] = ModelMachine

        self.MinorCycleConfig = MinorCycleConfig
        self.DeconvMachine = ClassImageDeconvMachineMSMF.ClassImageDeconvMachine(
            MainCache=MainCache,
            ParallelMode=True,
            CacheFileName="HMP_Init",
            IdSharedMem=IdSharedMem,
            GD=self.GD,
            **MinorCycleConfig)

        self.GD["Mask"]["Auto"] = False
        self.GD["Mask"]["External"] = None
        self.MaskMachine = ClassMaskMachine.ClassMaskMachine(self.GD)
Example #8
0
    def LoadModel(self):

        # ClassModelMachine,DicoModel=GiveModelMachine(self.FileDicoModel)
        # try:
        #     self.GD["GAClean"]["GASolvePars"]=DicoModel["SolveParam"]
        # except:
        #     self.GD["GAClean"]["GASolvePars"]=["S","Alpha"]
        #     DicoModel["SolveParam"]=self.GD["GAClean"]["GASolvePars"]
        # self.MM=ClassModelMachine(self.GD)
        # self.MM.FromDico(DicoModel)

        # From DicoModel
        ModConstructor = ClassModModelMachine(self.GD)
        self.MM = ModConstructor.GiveInitialisedMMFromFile(self.FileDicoModel)
        #ModelImage0=self.MM.GiveModelImage(np.mean(self.VS.MS.ChanFreq))

        if self.GD["GDkMS"]["ImageSkyModel"]["FilterNegComp"]:
            self.MM.FilterNegComponants(box=15, sig=1)

        if self.GD["GDkMS"]["ImageSkyModel"]["MaskImage"] != None:
            self.MM.CleanMaskedComponants(
                self.GD["GDkMS"]["ImageSkyModel"]["MaskImage"])
        #self.ModelImage=self.MM.GiveModelImage(np.mean(self.VS.MS.ChanFreq))
        model_freqs = self.VS.FreqBandChannelsDegrid[0]
        # original_freqs=self.VS.FreqBandChannels[0]
        # self.MM.setFreqMachine(original_freqs, model_freqs)
        ModelImage = self.MM.GiveModelImage(model_freqs)

        log.print("model image @%s MHz (min,max) = (%f, %f)" %
                  (str(model_freqs / 1e6), ModelImage.min(), ModelImage.max()))

        # # From ModelImage
        # print "im!!!!!!!!!!!!!!!!!!!!!!!"
        # im=image("Model.fits")
        # data=im.getdata()
        # nch,npol,nx,_=data.shape
        # for ch in range(nch):
        #     for pol in range(npol):
        #         data[ch,pol]=data[ch,pol].T[::-1]
        # self.ModelImage=ModelImage=data
        # # ###############################
        self.FacetMachine.ToCasaImage(ModelImage,
                                      ImageName="%s.Model_kMS" %
                                      self.BaseImageName,
                                      Fits=True)
        # #stop

        #del(data)

        self.DicoImager = self.FacetMachine.DicoImager

        NFacets = len(self.FacetMachine.DicoImager)
        self.NFacets = NFacets

        #self.NDirs=NFacets
        #self.Dirs=range(self.NDirs)

        # SolsFile=self.GD["DDESolutions"]["DDSols"]
        # if not(".npz" in SolsFile):
        #     ThisMSName=reformat.reformat(os.path.abspath(self.VS.MSName),LastSlash=False)
        #     SolsFile="%s/killMS.%s.sols.npz"%(self.VS.MSName,SolsFile)
        #     #SolsFile="BOOTES24_SB100-109.2ch8s.ms/killMS.KAFCA.Scalar.50Dir.0.1P.BriggsSq.PreCuster4.sols.npz"

        # DicoSolsFile=np.load(SolsFile)
        # ClusterCat=DicoSolsFile["ClusterCat"]
        # ClusterCat=ClusterCat.view(np.recarray)

        #DicoFacetName="%s.DicoFacet"%self.BaseImageName
        #DicoFacet=DDFacet.Other.MyPickle.Load(DicoFacetName)

        NodeFile = "%s.NodesCat.npy" % self.GD["Output"][
            "Name"]  #BaseImageName
        NodesCat = np.load(NodeFile)
        NodesCat = NodesCat.view(np.recarray)

        self.NDir = NodesCat.shape[0]

        ClusterCat = np.zeros((self.NDir, ),
                              dtype=[('Name', '|S200'), ('ra', np.float),
                                     ('dec', np.float), ('l', np.float),
                                     ('m', np.float), ('SumI', np.float),
                                     ("Cluster", int)])
        ClusterCat = ClusterCat.view(np.recarray)
        ClusterCat.l = NodesCat.l
        ClusterCat.m = NodesCat.m
        ClusterCat.ra = NodesCat.ra
        ClusterCat.dec = NodesCat.dec

        NN = ClusterCat.shape[0]
        Cat=np.zeros((NN,),dtype=[('Name','|S200'),('ra',np.float),('dec',np.float),('Sref',np.float),('I',np.float),('Q',np.float),\
                                  ('U',np.float),('V',np.float),('RefFreq',np.float),('alpha',np.float),('ESref',np.float),\
                                  ('Ealpha',np.float),('kill',np.int),('Cluster',np.int),('Type',np.int),('Gmin',np.float),\
                                  ('Gmaj',np.float),('Gangle',np.float),("Select",np.int),('l',np.float),('m',np.float),
                                  ("Exclude",bool)])
        Cat = Cat.view(np.recarray)
        Cat.RefFreq = 1.
        Cat.ra[:] = ClusterCat.ra
        Cat.dec[:] = ClusterCat.dec
        Cat.I[:] = ClusterCat.SumI[:]
        Cat.Cluster = np.arange(NN)
        Cat.Sref[:] = ClusterCat.SumI[:]

        self.SourceCat = Cat

        self.DicoImager = self.FacetMachine.DicoImager
        self.ClusterCat = ClusterCat
        self.ClusterCat.SumI = 0.

        # ind=np.where(self.ClusterCat.SumI!=0)[0]
        # self.ClusterCat=self.ClusterCat[ind].copy()
        # NFacets=self.ClusterCat.shape[0]
        # log.print( "  There are %i non-zero facets"%NFacets)

        NFacets = len(self.FacetMachine.DicoImager)
        lFacet = np.zeros((NFacets, ), np.float32)
        mFacet = np.zeros_like(lFacet)
        for iFacet in range(NFacets):
            l, m = self.FacetMachine.DicoImager[iFacet]["lmShift"]
            lFacet[iFacet] = l
            mFacet[iFacet] = m

        NDir = ClusterCat.l.size
        d = np.sqrt((ClusterCat.l.reshape((NDir, 1)) -
                     lFacet.reshape((1, NFacets)))**2 +
                    (ClusterCat.m.reshape((NDir, 1)) -
                     mFacet.reshape((1, NFacets)))**2)
        idDir = np.argmin(d, axis=0)

        for iFacet in range(NFacets):
            self.FacetMachine.DicoImager[iFacet]["iDirJones"] = idDir[iFacet]
            # print(iFacet,idDir[iFacet])

        self.SM.ClusterCat = self.ClusterCat
        self.SM.SourceCat = self.SourceCat

        from DDFacet.Other.AsyncProcessPool import APP
        APP.startWorkers()
        #self.VS.CalcWeightsBackground()
        self.FacetMachine.initCFInBackground()
        self.FacetMachine.awaitInitCompletion()

        # for iFacet in range(NFacets):

        #     #self.FacetMachine.SpacialWeigth[iFacet]=NpShared.ToShared("%sSpacialWeight_%3.3i"%(self.IdSharedMem,iFacet),self.FacetMachine.SpacialWeigth[iFacet])
        #     self.FacetMachine.SpacialWeigth[iFacet]=self.FacetMachine._CF[iFacet]["SW"]
        # log.print( "  Splitting model image")
        # self.BuildGridsParallel()
        self.FacetMachine.BuildFacetNormImage()
        self.FacetMachine.setModelImage(ModelImage)
        self.FacetMachine.set_model_grid()
        self.PrepareGridMachinesMapping()
        #self.BuildGridsSerial()
        #self.BuildGridsParallel()

        NFacets = self.ClusterCat.shape[0]
        self.SM.NDir = self.NDirs
        self.SM.Dirs = self.Dirs
        log.print("  There are %i non-zero directions" % self.SM.NDir)
        self.SM.ClusterCat = self.ClusterCat
        self.SM.SourceCat = self.SourceCat

        # self.SM.SourceCat.I[:]=self.ClusterCat.SumI[:]

        self.SM.DicoJonesDirToFacet = self.DicoJonesDirToFacet
        self.SM.GD = self.FacetMachine.GD
        self.SM.DicoImager = self.FacetMachine.DicoImager
        #self.SM.GD["Comp"]["CompDeGridMode"]=0
        CurrentMS = self.VS.ListMS[self.VS.iCurrentMS]
        self.SM.rac = CurrentMS.rac
        self.SM.decc = CurrentMS.decc
        self.SM.AvailableCorrelationProductsIds = self.VS.StokesConverter.AvailableCorrelationProductsIds(
        )
        self.SM.RequiredStokesProductsIds = self.VS.StokesConverter.RequiredStokesProductsIds(
        )
        self.SM.NFreqBands = self.VS.NFreqBands
        self.SM.Path = {"cf_dict_path": self.FacetMachine._CF.path}
        #self.SM.ChanMappingDegrid=self.VS.FreqBandChannelsDegrid[0]
        self.SM.ChanMappingDegrid = self.VS.DicoMSChanMappingDegridding[0]
        self.SM._model_dict = self.FacetMachine._model_dict
        self.SM.MapClusterCatOrigToCut = self.MapClusterCatOrigToCut