def giveConvModel(self, SubModelImageIn): nch, npol, N0x_in, N0y_in = SubModelImageIn.shape Nin = np.max([N0y_in, N0x_in]) * 2 if Nin % 2 == 0: Nin += 1 SubModelImage = np.zeros((nch, 1, Nin, Nin), dtype=SubModelImageIn.dtype) Aedge, Bedge = GiveEdgesDissymetric( (N0x_in / 2, N0y_in / 2), (N0x_in, N0y_in), (Nin / 2, Nin / 2), (Nin, Nin)) x0d, x1d, y0d, y1d = Aedge x0f, x1f, y0f, y1f = Bedge SubModelImage[..., x0f:x1f, y0f:y1f] = SubModelImageIn[..., x0d:x1d, y0d:y1d] PSF = self.PSF nPSF = PSF.shape[-1] AedgeP, BedgeP = GiveEdgesDissymetric( (Nin / 2, Nin / 2), (Nin, Nin), (nPSF / 2, nPSF / 2), (nPSF, nPSF)) x0dP, x1dP, y0dP, y1dP = AedgeP x0fP, x1fP, y0fP, y1fP = BedgeP SubPSF = PSF[..., x0fP:x1fP, y0fP:y1fP] # import pylab # pylab.clf() # pylab.subplot(1,2,1) # pylab.imshow(PSF[0,0],interpolation="nearest") # pylab.subplot(1,2,2) # pylab.imshow(SubPSF[0,0],interpolation="nearest") # #pylab.colorbar() # pylab.draw() # pylab.show(False) # stop ConvModel = np.zeros_like(SubModelImage) for ich in range(nch): for ipol in range(npol): ConvModel[ich, ipol] = scipy.signal.fftconvolve(SubModelImage[ich, ipol], SubPSF[ich, ipol], mode='same') # import pylab # pylab.clf() # ax=pylab.subplot(1,3,1) # pylab.imshow(SubModelImage[ich,ipol],interpolation="nearest") # pylab.subplot(1,3,2) # pylab.imshow(SubPSF[ich,ipol],interpolation="nearest") # pylab.subplot(1,3,3,sharex=ax,sharey=ax) # pylab.imshow(ConvModel[ich,ipol],interpolation="nearest") # #pylab.colorbar() # pylab.draw() # pylab.show(False) # stop return ConvModel[..., x0f:x1f, y0f:y1f]
def giveEdgesIslands(self, ListIslands): print >> log, " extracting Island edges" ListEdgesIslands = [] _, _, nx, _ = self._MaskArray.shape #Ed=np.zeros_like(self._MaskArray) for Island in ListIslands: x, y = np.array(Island).T EdgesIsland = [] for iPix in range(x.size): xc, yc = x[iPix], y[iPix] Aedge, Bedge = GiveEdgesDissymetric((xc, yc), (nx, nx), (1, 1), (3, 3)) x0d, x1d, y0d, y1d = Aedge m = self._MaskArray[0, 0][x0d:x1d, y0d:y1d] if 1 in m: EdgesIsland.append((xc, yc)) #Ed[0,0,xc,yc]=1 ListEdgesIslands.append(EdgesIsland) # import pylab # ax=pylab.subplot(1,2,1) # pylab.imshow(self._MaskArray[0,0],interpolation="nearest") # pylab.subplot(1,2,2,sharex=ax,sharey=ax) # pylab.imshow(Ed[0,0],interpolation="nearest") # pylab.draw() # pylab.show(False) return ListEdgesIslands
def GiveModelImage(self, FreqIn=None, out=None, DoAbs=False): """ Renders a model image at the specified frequency(ies) Args: FreqIn: scalar or vector of frequencies out: if not None, image to be rendered into. Must have correct shape. Returns: Model image """ if DoAbs: f_apply = np.abs else: f_apply = lambda x: x RefFreq = self.DicoSMStacked["RefFreq"] if FreqIn is None: FreqIn = np.array([RefFreq]) #if type(FreqIn)==float: # FreqIn=np.array([FreqIn]).flatten() #if type(FreqIn)==np.ndarray: FreqIn = np.array([FreqIn.ravel()]).flatten() _, npol, nx, ny = self.ModelShape N0x = nx N0y = ny nchan = FreqIn.size if out is not None: if out.shape != (nchan, npol, nx, ny) or out.dtype != np.float32: raise RuntimeError( "supplied image has incorrect type (%s) or shape (%s)" % (out.dtype, out.shape)) ModelImage = out else: ModelImage = np.zeros((nchan, npol, nx, ny), dtype=np.float32) if "Comp" not in self.DicoSMStacked.keys(): return ModelImage DicoComp = self.DicoSMStacked["Comp"] DicoSM = {} for key in DicoComp.keys(): for pol in range(npol): Sol = DicoComp[key][ "SolsArray"][:, pol] #/self.DicoSMStacked[key]["SumWeights"] x, y = key #print>>log, "%s : %s"%(str(key),str(Sol)) for iFunc in range(Sol.size): ThisComp = self.ListScales[iFunc] ThisAlpha = ThisComp["Alpha"] for ch in range(nchan): Flux = Sol[iFunc] * (FreqIn[ch] / RefFreq)**(ThisAlpha) Flux = f_apply(Flux) if ThisComp["ModelType"] == "Delta": ModelImage[ch, pol, x, y] += Flux elif ThisComp["ModelType"] == "Gaussian": Gauss = ThisComp["Model"] Sup, _ = Gauss.shape x0, x1 = x - Sup / 2, x + Sup / 2 + 1 y0, y1 = y - Sup / 2, y + Sup / 2 + 1 Aedge, Bedge = GiveEdgesDissymetric( (x, y), (N0x, N0y), (Sup / 2, Sup / 2), (Sup, Sup)) x0d, x1d, y0d, y1d = Aedge x0p, x1p, y0p, y1p = Bedge ModelImage[ch, pol, x0d:x1d, y0d:y1d] += Gauss[x0p:x1p, y0p:y1p] * Flux # vmin,vmax=np.min(self._MeanDirtyOrig[0,0]),np.max(self._MeanDirtyOrig[0,0]) # vmin,vmax=-1,1 # #vmin,vmax=np.min(ModelImage),np.max(ModelImage) # pylab.clf() # ax=pylab.subplot(1,3,1) # pylab.imshow(self._MeanDirtyOrig[0,0],interpolation="nearest",vmin=vmin,vmax=vmax) # pylab.subplot(1,3,2,sharex=ax,sharey=ax) # pylab.imshow(self._MeanDirty[0,0],interpolation="nearest",vmin=vmin,vmax=vmax) # pylab.colorbar() # pylab.subplot(1,3,3,sharex=ax,sharey=ax) # pylab.imshow( ModelImage[0,0],interpolation="nearest",vmin=vmin,vmax=vmax) # pylab.colorbar() # pylab.draw() # pylab.show(False) # print np.max(ModelImage[0,0]) # # stop return ModelImage
def SubStep(self,(dx,dy),LocalSM): _,npol,_,_ = self._MeanDirty.shape x0,x1,y0,y1=self.DirtyExtent xc,yc=dx,dy #NpixFacet=self.SubPSF.shape[-1] #PSF=self.CubePSFScales[iScale] N0=self._MeanDirty.shape[-1] N1=LocalSM.shape[-1] # PSF=PSF[N1/2-1:N1/2+2,N1/2-1:N1/2+2] # N1=PSF.shape[-1] #Aedge,Bedge=self.GiveEdges((xc,yc),N0,(N1/2,N1/2),N1) N0x,N0y=self._MeanDirty.shape[-2::] Aedge,Bedge=GiveEdgesDissymetric((xc,yc),(N0x,N0y),(N1/2,N1/2),(N1,N1)) #_,n,n=self.PSF.shape # PSF=self.PSF.reshape((n,n)) # print "Fpol00",Fpol factor = -1. # Fpol[0,0,0]*self.Gain # print "Fpol01",Fpol nch, npol, nx, ny = LocalSM.shape # print Fpol[0,0,0] # print Aedge # print Bedge #print>>log, " Removing %f Jy at (%i %i) (peak of %f Jy)"%(Fpol[0,0,0]*self.Gain,dx,dy,Fpol[0,0,0]) # PSF=self.PSF[0]
def SubStep(self, dx, dy, LocalSM): _, npol, _, _ = self._MeanDirty.shape x0, x1, y0, y1 = self.DirtyExtent xc, yc = dx, dy #NpixFacet=self.SubPSF.shape[-1] #PSF=self.CubePSFScales[iScale] N0 = self._MeanDirty.shape[-1] N1 = LocalSM.shape[-1] # PSF=PSF[N1/2-1:N1/2+2,N1/2-1:N1/2+2] # N1=PSF.shape[-1] #Aedge,Bedge=self.GiveEdges(xc,yc,N0,N1/2,N1/2,N1) N0x, N0y = self._MeanDirty.shape[-2::] Aedge, Bedge = GiveEdgesDissymetric(xc, yc, N0x, N0y, N1 // 2, N1 // 2, N1, N1) #_,n,n=self.PSF.shape # PSF=self.PSF.reshape((n,n)) # print "Fpol00",Fpol factor = -1. # Fpol[0,0,0]*self.Gain # print "Fpol01",Fpol nch, npol, nx, ny = LocalSM.shape # print Fpol[0,0,0] # print Aedge # print Bedge #print>>log, " Removing %f Jy at (%i %i) (peak of %f Jy)"%(Fpol[0,0,0]*self.Gain,dx,dy,Fpol[0,0,0]) # PSF=self.PSF[0] x0d, x1d, y0d, y1d = Aedge x0p, x1p, y0p, y1p = Bedge # nxPSF=self.CubePSFScales.shape[-1] # x0,x1=nxPSF/2-self.SupWeightWidth,nxPSF/2+self.SupWeightWidth+1 # y0,y1=nxPSF/2-self.SupWeightWidth,nxPSF/2+self.SupWeightWidth+1 # x0p=x0+x0p # x1p=x0+x1p # y0p=y0+y0p # y1p=y0+y1p # Bedge=x0p,x1p,y0p,y1p # # import pylab # pylab.clf() # ax=pylab.subplot(1,3,1) # vmin,vmax=self._CubeDirty.min(),self._CubeDirty.max() # pylab.imshow(self._MeanDirty[0,0,x0d:x1d,y0d:y1d],interpolation="nearest",vmin=vmin,vmax=vmax) # pylab.colorbar() # pylab.subplot(1,3,2,sharex=ax,sharey=ax) # pylab.imshow(np.mean(LocalSM,axis=0)[0,x0p:x1p,y0p:y1p],interpolation="nearest",vmin=vmin,vmax=vmax) # pylab.colorbar() # pylab.draw() # #print "Fpol02",Fpol # # NpParallel.A_add_B_prod_factor((self.Dirty),LocalSM,Aedge,Bedge,factor=float(factor),NCPU=self.NCPU) # <<<<<<< HEAD # self._CubeDirty[:,:,x0d:x1d,y0d:y1d] -= LocalSM[:,:,x0p:x1p,y0p:y1p] # ======= # self._CubeDirty[:,:,x0d:x1d,y0d:y1d] -= LocalSM[:,:,x0p:x1p,y0p:y1p] cube, sm = self._CubeDirty[:, :, x0d:x1d, y0d:y1d], LocalSM[:, :, x0p:x1p, y0p:y1p] # if self.DoPlot: # AA0=cube[0,0,:,:].copy() # vmin,vmax=np.min(AA0),np.max(AA0) # AA1=sm[0,0,:,:].copy() # import pylab # pylab.clf() # pylab.subplot(1,3,1) # pylab.imshow(AA0,interpolation="nearest") # pylab.colorbar() # pylab.subplot(1,3,2) # pylab.imshow(AA1,interpolation="nearest") # pylab.colorbar() # pylab.subplot(1,3,3) # pylab.imshow((AA0-AA1),interpolation="nearest") # pylab.colorbar() # pylab.draw() # pylab.show(False) # pylab.pause(0.1) numexpr.evaluate('cube-sm', out=cube, casting="unsafe") #a-=b if self._MeanDirty is not self._CubeDirty: ### old code, got MeanDirty out of alignment with CubeDirty somehow ## W=np.float32(self.DicoDirty["WeightChansImages"]) ## self._MeanDirty[0,:,x0d:x1d,y0d:y1d]-=np.sum(LocalSM[:,:,x0p:x1p,y0p:y1p]*W.reshape((W.size,1,1,1)),axis=0) meanimage = self._MeanDirty[:, :, x0d:x1d, y0d:y1d] # cube.mean(axis=0, out=meanimage) should be a bit faster, but we experienced problems with some numpy versions, # see https://github.com/cyriltasse/DDFacet/issues/325 # So use array copy instead (which makes an intermediate array) if cube.shape[0] > 1: meanimage[...] = (cube * self._band_weights).sum(axis=0) # cube.mean(axis=0, out=meanimage) else: meanimage[0, ...] = cube[0, ...] # ## this is slower: # self._MeanDirty[0,:,x0d:x1d,y0d:y1d] = self._CubeDirty[:,:,x0d:x1d,y0d:y1d].mean(axis=0) # np.save("_MeanDirty",self._MeanDirty) # np.save("_CubeDirty",self._CubeDirty) # stop if self._peakMode is "sigma": a, b = self._MeanDirty[:, :, x0d:x1d, y0d:y1d], self._NoiseMap[:, :, x0d:x1d, y0d:y1d] numexpr.evaluate("a/b", out=self._PeakSearchImage[:, :, x0d:x1d, y0d:y1d]) elif self._peakMode is "weighted": a, b = self._MeanDirty[:, :, x0d:x1d, y0d:y1d], self._peakWeightImage[:, :, x0d:x1d, y0d:y1d] numexpr.evaluate("a*b", out=self._PeakSearchImage[:, :, x0d:x1d, y0d:y1d])
def ConvolveFFT(self, A, OutMode="Data", AddNoise=None): shin = A.shape T = ClassTimeIt.ClassTimeIt("ConvolveFFT") T.disable() Asq = self.PM.ModelToSquareArray(A, TypeInOut=("Parms", OutMode)) T.timeit("0") NFreqBand, npol, N, _ = Asq.shape zN = 2 * N + 1 zN, _ = EstimateNpix(float(zN)) zAsq = np.zeros((NFreqBand, npol, zN, zN), dtype=Asq.dtype) zAsq[:, :, zN / 2 - N / 2:zN / 2 + N / 2 + 1, zN / 2 - N / 2:zN / 2 + N / 2 + 1] = Asq[:, :, :, :] T.timeit("1") if AddNoise is not None: zAsq += np.random.randn(*zAsq.shape) * AddNoise N0x = zAsq.shape[-1] xc0 = N0x / 2 N1 = self.PSF.shape[-1] Aedge, Bedge = GiveEdgesDissymetric((xc0, xc0), (N0x, N0x), (N1 / 2, N1 / 2), (N1, N1)) x0d, x1d, y0d, y1d = Aedge x0s, x1s, y0s, y1s = Bedge SubPSF = self.PSF[:, :, x0s:x1s, y0s:y1s] #xc=self.PSF.shape[-1]/2 #SubPSF=self.PSF[:,:,xc-N:xc+N+1,xc-N:xc+N+1] Conv = np.zeros_like(zAsq) T.timeit("2") for ich in range(NFreqBand): for ipol in range(npol): Conv[ich, ipol] = scipy.signal.fftconvolve(zAsq[ich, ipol], SubPSF[ich, ipol], mode='same') #Conv[ich,ipol]=ModFFTW.ConvolveFFTW2D((zAsq[ich,ipol]), (SubPSF[ich,ipol])) # import pylab # pylab.subplot(1,3,1) # pylab.imshow(Conv[ich,ipol][zN/2-N/2:zN/2+N/2+1,zN/2-N/2:zN/2+N/2+1],interpolation="nearest") # pylab.subplot(1,3,2) # pylab.imshow(Conv1[ich,ipol][zN/2-N/2:zN/2+N/2+1,zN/2-N/2:zN/2+N/2+1],interpolation="nearest") # pylab.subplot(1,3,3) # pylab.imshow((Conv-Conv1)[ich,ipol][zN/2-N/2:zN/2+N/2+1,zN/2-N/2:zN/2+N/2+1],interpolation="nearest") # pylab.colorbar() # pylab.draw() # pylab.show() T.timeit("3 [%s]" % str(zAsq.shape)) A = self.PM.SquareArrayToModel(Conv[:, :, zN / 2 - N / 2:zN / 2 + N / 2 + 1, zN / 2 - N / 2:zN / 2 + N / 2 + 1], TypeInOut=(OutMode, OutMode)) T.timeit("4") if OutMode == "Data": NPixOut = self.NPixListData else: NPixOut = self.NPixListParms # import pylab # pylab.clf() # pylab.subplot(1,3,1) # pylab.imshow(zAsq[0,0],interpolation="nearest") # pylab.subplot(1,3,2) # pylab.imshow(SubPSF[0,0],interpolation="nearest") # pylab.subplot(1,3,3) # pylab.imshow(Conv[0,0],interpolation="nearest") # pylab.draw() # pylab.show(False) # stop return A.reshape((NFreqBand, npol, NPixOut))