def MovePix(self, indiv, iPix, Flux, FluxWeighted=True, InReg=None): dx, dy = np.mgrid[-1:1:3 * 1j, -1:1:3 * 1j] Dx = np.int32(np.concatenate((dx.flatten()[0:4], dx.flatten()[5::]))) Dy = np.int32(np.concatenate((dy.flatten()[0:4], dy.flatten()[5::]))) # ArrayModel=self.PM.GiveModelArray(indiv) # ArrayModel_S=self.PM.ArrayToSubArray(indiv,Type="S") ArrayModel_S = indiv # ArrayModel_S.reshape((1,ArrayModel_S.size))*np.ones((2,1)) A = self.PM.ModelToSquareArray(ArrayModel_S, TypeInOut=("Parms", "Parms"), DomainOut="Parms") nf, npol, nx, nx = A.shape #A=np.mean(A,axis=0).reshape(1,npol,nx,nx) mapi, mapj = self.PM.SquareGrids["Parms"]["MappingIndexToXYPix"] i0, j0 = mapi[iPix], mapj[iPix] FluxWeighted = False if FluxWeighted: iAround = i0 + Dx jAround = j0 + Dy cx = ((iAround >= 0) & (iAround < nx)) cy = ((jAround >= 0) & (jAround < nx)) indIN = (cx & cy) iAround = iAround[indIN] jAround = jAround[indIN] sAround = A[0, 0, iAround, jAround].copy() #sInt=np.sum(sAround) #sAround[sAround==0]=sInt*0.05 X = np.arange(iAround.size) DM = ClassDistMachine() DM.setRefSample(X, W=sAround, Ns=10) ind = int(round(DM.GiveSample(1)[0])) ind = np.max([0, ind]) ind = np.min([ind, iAround.size - 1]) ind = indIN[ind] # else: # if InReg is None: # reg=random.random() # else: # reg=InReg # ind=int(reg*8) i1 = i0 + Dx[InReg] j1 = j0 + Dy[InReg] f0 = Flux #alpha*A[0,0,i0,j0] _, _, nx, ny = A.shape condx = ((i1 > 0) & (i1 < nx)) condy = ((j1 > 0) & (j1 < ny)) if condx & condy: A[0, 0, i1, j1] += f0 A[0, 0, i0, j0] -= f0 AParm = self.PM.SquareArrayToModel(A, TypeInOut=("Parms", "Parms")) ArrayModel_S.flat[:] = AParm.flat[:] return indiv
def GiveCompacity(self,S): DM=ClassDistMachine() #S.fill(1) #S[0]=100 DM.setRefSample(np.arange(S.size),W=np.sort(S),Ns=100,xmm=[0,S.size-1])#,W=sAround,Ns=10) #DM.setRefSample(S)#,W=sAround,Ns=10) xs,ys=DM.xyCumulD dx=xs[1]-xs[0] I=2.*(S.size-np.sum(ys)*dx)/S.size-1. return I
def test(): import DDFacet.ToolsDir.Gaussian _, _, PSF = DDFacet.ToolsDir.Gaussian.Gaussian(10, 311, 1.) #PSF.fill(1.) #import scipy.signal #PP=scipy.signal.fftconvolve(PSF,PSF, mode='same') #print Fact import pylab pylab.clf() pylab.imshow(PSF, interpolation="nearest") pylab.colorbar() pylab.draw() pylab.show(False) pylab.pause(0.1) Dirty = np.zeros_like(PSF) nx, _ = Dirty.shape Dirty[nx / 2, nx / 2 + 10] += 2. Dirty[nx / 2 + 10, nx / 2 + 10] += 2. Dirty = np.random.randn(*(Dirty.shape)) PSF = PSF.reshape((1, 1, nx, nx)) * np.ones((2, 1, 1, 1)) Dirty = Dirty.reshape((1, 1, nx, nx)) * np.ones((2, 1, 1, 1)) Dirty[1, :, :, :] = Dirty[0, :, :, :] * 2 x, y = np.mgrid[0:nx, 0:nx] dx = 10 nc = nx / 2 x = x[nc - dx:nc + dx, nc - dx:nc + dx].flatten() y = y[nc - dx:nc + dx, nc - dx:nc + dx].flatten() ListPixParms = [(x[i], y[i]) for i in range(x.size)] x, y = np.mgrid[0:nx, 0:nx] dx = 10 x = x[nc - dx:nc + dx, nc - dx:nc + dx].flatten() y = y[nc - dx:nc + dx, nc - dx:nc + dx].flatten() ListPixData = [(x[i], y[i]) for i in range(x.size)] CC = ClassConvMachine(PSF, ListPixParms, ListPixData, "Matrix") NFreqBands, _, _, _ = Dirty.shape NPixListParms = len(ListPixParms) NPixListData = len(ListPixData) Array = np.zeros((NFreqBands, 1, NPixListParms), np.float32) x0, y0 = np.array(ListPixParms).T for iBand in range(NFreqBands): Array[iBand, 0, :] = Dirty[iBand, 0, x0, y0] Array = Array.reshape((NFreqBands, NPixListParms)) import pylab Lchi0 = [] Lchi1 = [] NTries = 5000 ArrKeep0 = np.zeros((NTries, NPixListParms), Array.dtype) ArrKeep1 = np.zeros((NTries, NPixListParms), Array.dtype) for i in range(NTries): Array = np.random.randn(*Array.shape) #T=ClassTimeIt.ClassTimeIt() chi0 = np.sum(Array**2) Lchi0.append(chi0) ConvArray0 = CC.Convolve(Array) chi1 = np.sum(ConvArray0**2) #T.timeit("0") #ConvArray1=CC.Convolve(Array,ConvMode="Vector").ravel() #T.timeit("1") #r=chi1/chi0 #print "%f -> %f [%r]"%(chi0,chi1,r) NChan, _, NN = ConvArray0.shape NN = int(np.sqrt(NN)) ArrKeep0[i] = Array[0].ravel() ArrKeep1[i] = ConvArray0[0].ravel() # pylab.clf() # pylab.imshow(ConvArray0.reshape((2,NN,NN))[0],interpolation="nearest") # pylab.draw() # pylab.show(False) # pylab.pause(0.1) Lchi1.append(chi1) #print np.var(Array),np.var(ConvArray0)/Fact Fact = CC.NormData[0] print np.median(np.std(ArrKeep0, axis=0)**2) print np.median(np.std(ArrKeep1, axis=0)**2 / Fact) return from scipy.stats import chi2 from DDFacet.ToolsDir.GeneDist import ClassDistMachine DM = ClassDistMachine() rv = chi2(Array.size) x = np.linspace(0, 2 * rv.moment(1), 1000) P = rv.cdf(x) pylab.clf() pylab.subplot(2, 1, 1) #yd,xe=pylab.histogram(Lchi0,bins=100,normed=True) #xd=(xe[1::]+xe[0:-1])/2. #yd/=np.sum(yd) xd, yd = DM.giveCumulDist(np.array(Lchi0), Ns=100) #dx=xd[1]-xd[0] #yd/=dx pylab.plot(xd, yd) pylab.plot(x, P) pylab.xlim(0, 1600) pylab.subplot(2, 1, 2) xd, yd = DM.giveCumulDist(np.array(Lchi1), Ns=20) # yd,xe=pylab.histogram(Lchi1,bins=100,normed=True) # xd=(xe[1::]+xe[0:-1])/2. # dx=xd[1]-xd[0] # yd/=np.sum(yd) # yd/=dx print np.mean(Lchi1) / Fact print np.mean(Lchi0) # #pylab.xlim(0,800) # #pylab.hist(Lchi1,bins=100) import scipy.interpolate cdf = scipy.interpolate.interp1d(xd, yd, "cubic") x = np.linspace(xd.min(), xd.max(), 1000) #pylab.plot(x,cdf(x),ls="",marker=".") #pylab.plot(xd,yd,ls="",marker="s") y = cdf(x) x, y = xd, yd y = y[1::] - y[0:-1] x = (x[1::] + x[0:-1]) / 2. pylab.plot(x, y, ls="", marker=".") #pylab.xlim(0,1600) pylab.draw() pylab.show(False) # import pylab # pylab.clf() # #pylab.plot(ConvArray0.ravel()) # pylab.imshow(PSF[0,0]) # #pylab.plot(ConvArray1) # #pylab.plot(ConvArray1-ConvArray0) # pylab.draw() # pylab.show(False) stop
def __init__(self, ConvMachine): self.ConvMachine = ConvMachine self.DM = ClassDistMachine()
class ClassPDFMachine(): def __init__(self, ConvMachine): self.ConvMachine = ConvMachine self.DM = ClassDistMachine() def setPDF(self, indivIn, std, Chi2_0=None, NReal=100): indiv = indivIn.copy() indiv.fill(0) LTries = [] for iReal in range(NReal): #print iReal LTries.append( self.ConvMachine.ConvolveFFT(indiv, OutMode="Data", AddNoise=1.).ravel()) ATries = np.array(LTries) ThisStd = np.mean(np.std(ATries, axis=0)) ATries *= std / ThisStd AChi2 = np.sum(ATries**2, axis=1) if Chi2_0 is not None: AChi2 *= Chi2_0 / np.mean(AChi2) self.MeanChi2 = np.mean(AChi2) xd, yd = self.DM.giveDist(AChi2, Ns=20) dx = xd[1] - xd[0] yd /= dx pdf = scipy.interpolate.interp1d(xd, yd, "cubic") x = np.linspace(xd.min(), xd.max(), 1000) y = pdf(x) self.MaxPDF = np.max(y) #x,y=xd, yd self.InterpPDF = pdf self.InterpX0 = xd.min() self.InterpX1 = xd.max() # import pylab # pylab.clf() # pylab.plot(x,y,ls="",marker=".",color="blue") # pylab.plot(xd, yd,ls="",marker=".",color="green") # pylab.scatter([self.MeanChi2], [0],marker=".",color="black") # pylab.draw() # pylab.show() def pdfScalar(self, Chi2): if not (self.InterpX0 < Chi2 < self.InterpX1): return 1e-20 p = self.InterpPDF(Chi2) if p <= self.MaxPDF / 4.: return 1e-20 return p def logpdfScalar(self, Chi2): return np.log(self.pdfScalar(Chi2)) def pdf(self, Chi2): if type(Chi2) == list or type(Chi2) == np.ndarray: ans = [] for chi2 in Chi2: ans.append(self.pdfScalar(chi2)) if type(Chi2) == np.ndarray: ans = np.array(ans) return ans else: return self.pdfScalar(Chi2) def logpdf(self, Chi2): if type(Chi2) == list or type(Chi2) == np.ndarray: ans = [] for chi2 in Chi2: ans.append(self.logpdfScalar(chi2)) if type(Chi2) == np.ndarray: ans = np.array(ans) return ans else: return self.logpdfScalar(Chi2)