def GiveSpectralIndexMap(self, CellSizeRad=1., GaussPars=[(1, 1, 0)], DoConv=True, MaxSpi=100, MaxDR=1e+6,
                             threshold=None):
        dFreq = 1e6
        # f0=self.DicoSMStacked["AllFreqs"].min()
        # f1=self.DicoSMStacked["AllFreqs"].max()
        RefFreq = self.DicoSMStacked["RefFreq"]
        f0 = RefFreq / 1.5
        f1 = RefFreq * 1.5

        M0 = self.GiveModelImage(f0)
        M1 = self.GiveModelImage(f1)
        if DoConv:
            # M0=ModFFTW.ConvolveGaussian(M0,CellSizeRad=CellSizeRad,GaussPars=GaussPars)
            # M1=ModFFTW.ConvolveGaussian(M1,CellSizeRad=CellSizeRad,GaussPars=GaussPars)
            # M0,_=ModFFTW.ConvolveGaussianWrapper(M0,Sig=GaussPars[0][0]/CellSizeRad)
            # M1,_=ModFFTW.ConvolveGaussianWrapper(M1,Sig=GaussPars[0][0]/CellSizeRad)
            M0, _ = ModFFTW.ConvolveGaussianScipy(M0, Sig=GaussPars[0][0] / CellSizeRad)
            M1, _ = ModFFTW.ConvolveGaussianScipy(M1, Sig=GaussPars[0][0] / CellSizeRad)

        # print M0.shape,M1.shape
        # compute threshold for alpha computation by rounding DR threshold to .1 digits (i.e. 1.65e-6 rounds to 1.7e-6)
        if threshold is not None:
            minmod = threshold
        elif not np.all(M0 == 0):
            minmod = float("%.1e" % (np.max(np.abs(M0)) / MaxDR))
        else:
            minmod = 1e-6

        # mask out pixels above threshold
        mask = (M1 < minmod) | (M0 < minmod)
        print("computing alpha map for model pixels above %.1e Jy (based on max DR setting of %g)" % (
              minmod, MaxDR), file=log)
        M0[mask] = minmod
        M1[mask] = minmod
        # with np.errstate(invalid='ignore'):
        #    alpha = (np.log(M0)-np.log(M1))/(np.log(f0/f1))
        # print
        # print np.min(M0),np.min(M1),minmod
        # print
        alpha = (np.log(M0) - np.log(M1)) / (np.log(f0 / f1))
        alpha[mask] = 0

        # mask out |alpha|>MaxSpi. These are not physically meaningful anyway
        mask = alpha > MaxSpi
        alpha[mask] = MaxSpi
        masked = mask.any()
        mask = alpha < -MaxSpi
        alpha[mask] = -MaxSpi
        if masked or mask.any():
            print(ModColor.Str("WARNING: some alpha pixels outside +/-%g. Masking them." % MaxSpi, col="red"), file=log)
        return alpha
Exemplo n.º 2
0
    def GiveSpectralIndexMap(self,
                             CellSizeRad=1.,
                             GaussPars=[(1, 1, 0)],
                             DoConv=True,
                             MaxSpi=100,
                             MaxDR=1e+6,
                             threshold=None):

        dFreq = 1e6
        RefFreq = self.DicoSMStacked["RefFreq"]
        f0 = RefFreq / 1.5  #self.DicoSMStacked["AllFreqs"].min()
        f1 = RefFreq * 1.5  #self.DicoSMStacked["AllFreqs"].max()
        M0 = self.GiveModelImage(f0)
        M1 = self.GiveModelImage(f1)
        if DoConv:
            #M0=ModFFTW.ConvolveGaussian(M0,CellSizeRad=CellSizeRad,GaussPars=GaussPars)
            #M1=ModFFTW.ConvolveGaussian(M1,CellSizeRad=CellSizeRad,GaussPars=GaussPars)
            #M0,_=ModFFTW.ConvolveGaussianWrapper(M0,Sig=GaussPars[0][0]/CellSizeRad)
            #M1,_=ModFFTW.ConvolveGaussianWrapper(M1,Sig=GaussPars[0][0]/CellSizeRad)
            M0, _ = ModFFTW.ConvolveGaussianScipy(M0,
                                                  Sig=GaussPars[0][0] /
                                                  CellSizeRad)
            M1, _ = ModFFTW.ConvolveGaussianScipy(M1,
                                                  Sig=GaussPars[0][0] /
                                                  CellSizeRad)

        # compute threshold for alpha computation by rounding DR threshold to .1 digits (i.e. 1.65e-6 rounds to 1.7e-6)
        if threshold is not None:
            minmod = threshold
        elif not np.all(M0 == 0):
            minmod = float("%.1e" % (np.max(np.abs(M0)) / MaxDR))
        else:
            minmod = 1e-6

        # mask out pixels above threshold
        mask = (M1 < minmod) | (M0 < minmod)
        print >> log, "computing alpha map for model pixels above %.1e Jy (based on max DR setting of %g)" % (
            minmod, MaxDR)
        M0[mask] = minmod
        M1[mask] = minmod
        alpha = (np.log(M0) - np.log(M1)) / (np.log(f0 / f1))
        alpha[mask] = 0

        # Np=1000
        # indx,indy=np.int64(np.random.rand(Np)*M0.shape[0]),np.int64(np.random.rand(Np)*M0.shape[1])
        # med=np.median(np.abs(M0[:,:,indx,indy]))
        # Mask=((M1>100*med)&(M0>100*med))
        # alpha=np.zeros_like(M0)
        # alpha[Mask]=(np.log(M0[Mask])-np.log(M1[Mask]))/(np.log(f0/f1))
        return alpha
Exemplo n.º 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)