def StackChain(self): P0 = self.ArrayMethodsMachine.PM.GiveIndivZero() P = P0.copy() Model = self.ArrayMethodsMachine.PM.GiveModelArray(P0) N = 0 Chi2Min = 1e10 for iChain in range(self.NChains): ChainParms = self.DicoChains[iChain]["Parms"] ChainChi2 = self.DicoChains[iChain]["Chi2"] #print len(ChainParms),len(ChainChi2) for iPoint in range(len(ChainParms)): P += ChainParms[iPoint] #Model+=self.ArrayMethodsMachine.PM.GiveModelArray(ChainParms[iPoint]) Chi2 = ChainChi2[iPoint] if Chi2 < Chi2Min: Chi2Min = Chi2 Pmin = ChainParms[iPoint] N += 1 Model /= N P /= N sP = P0.copy() for iChain in range(self.NChains): ChainParms = self.DicoChains[iChain]["Parms"] for iPoint in range(len(ChainParms)): sP += (ChainParms[iPoint] - P)**2 N += 1 sP = np.sqrt(sP) / N return Model, P, Pmin, sP
def GiveGridSharp(self, Image, DicoImager, iFacet): nch, npol, _, _ = Image.shape _, _, NpixFacet, _ = self.GridShape x0, x1, y0, y1 = DicoImager[iFacet]["pixExtent"] #ModelIm=np.zeros((nch,npol,NpixFacet,NpixFacet),dtype=np.float32) x0p, x1p = self.PaddingInnerCoord ModelIm = np.zeros(self.GridShape, dtype=self.dtype) #print "xxA:",x0,x1 #print "xxB:",x0p,x1p for ch in range(nch): for pol in range(npol): #ModelIm[ch,pol]=Image[ch,pol,x0:x1,y0:y1].T[::-1,:].real ModelIm[ch, pol, x0p:x1p, x0p:x1p] = Image[ch, pol, x0:x1, y0:y1].T[::-1, :].real ModelIm[ch, pol] /= self.ifzfCF SumFlux = np.sum(ModelIm) #print iFacet,np.max(ModelIm) #return ModelIm, None ModelIm *= (self.OverS * NpixFacet)**2 Grid = self.FFTWMachine.fft(ModelIm) return Grid, SumFlux
def UnPackListArray(Name): Name = str(Name) DimName = Name + '.dimensions' DatName = Name + '.data' Dim = GiveArray(DimName) Dat = GiveArray(DatName) NArray = Dim[0] idx = 1 # read ndims ListNDim = [] for i in range(NArray): ListNDim.append(Dim[idx]) idx += 1 # read shapes ListShapes = [] for i in range(NArray): ndim = ListNDim[i] shape = Dim[idx:idx + ndim] ListShapes.append(shape) idx += ndim idx = 0 # read values ListArray = [] for i in range(NArray): shape = ListShapes[i] size = np.prod(shape) A = Dat[idx:idx + size].reshape(shape) ListArray.append(A) idx += size return ListArray
def EvalSphe(nu): P = np.array([[ 8.203343e-2, -3.644705e-1, 6.278660e-1,-5.335581e-1, 2.312756e-1],\ [ 4.028559e-3, -3.697768e-2, 1.021332e-1,-1.201436e-1, 6.412774e-2]]) Q=np.array([[1.0000000e0, 8.212018e-1, 2.078043e-1],\ [1.0000000e0, 9.599102e-1, 2.918724e-1]]) part = 0 end = 0.0 if ((nu >= 0.0) & (nu < 0.75)): part = 0 end = 0.75 elif ((nu >= 0.75) & (nu <= 1.00)): part = 1 end = 1.00 else: return 0.0 nusq = nu**2 delnusq = nusq - end * end delnusqPow = delnusq top = P[part][0] for k in range(1, 5): top += P[part][k] * delnusqPow delnusqPow *= delnusq bot = Q[part][0] delnusqPow = delnusq for k in range(1, 3): bot += Q[part][k] * delnusqPow delnusqPow *= delnusq result = (1.0 - nusq) * (top / bot) return result
def GiveReorgCF(self, A): Sup = A.shape[0] // self.OverS B = np.zeros((self.OverS, self.OverS, Sup, Sup), dtype=A.dtype) for i in range(self.OverS): for j in range(self.OverS): B[i, j, :, :] = A[i::self.OverS, j::self.OverS] # [::-1,:] B = B.reshape((A.shape[0], A.shape[0])) return B
def testFFTW2D(): n = 1024 A = np.zeros((1, 1, n, n), np.complex128) A[0, 0, n // 2 + 1, n // 2 + 1] = 1 + 0.5 * 1j #A[n//2,n//2]=1#+0.5*1j #A=np.random.randn(6,6)+1j*np.random.randn(6,6) import ModFFTW FM1 = ModFFTW.FFTW(A) FM2 = ModFFTW.FFTWnp(A) FM0 = FFTM2D(A) import ClassTimeIt T = ClassTimeIt.ClassTimeIt() ntest = 1 for i in range(ntest): f0 = FM0.fft(A) if0 = FM0.ifft(A) T.timeit("old") for i in range(ntest): f1 = FM1.fft(A) if1 = FM1.ifft(A) T.timeit("new") for i in range(ntest): f2 = FM2.fft(A) if2 = FM2.ifft(A) T.timeit("newnp") print(np.std(f0 - f1)) print(np.std(if0 - if1)) print(np.std(f0 - f2)) print(np.std(if0 - if2)) stop # f0=FM.fft(A) # f1=FM.ifft(f0) f0 = FM.fft(A) f1 = FM.ifft(f0) pylab.clf() pylab.subplot(2, 2, 1) pylab.imshow(A.T.real, interpolation="nearest") pylab.imshow(A.T.imag, interpolation="nearest") pylab.colorbar() pylab.subplot(2, 2, 2) pylab.imshow(f0.T.real, interpolation="nearest") pylab.imshow(f0.T.imag, interpolation="nearest") pylab.colorbar() pylab.subplot(2, 2, 3) pylab.imshow(f1.T.real, interpolation="nearest") pylab.imshow(f1.T.imag, interpolation="nearest") pylab.colorbar() pylab.draw() pylab.show(False)
def readExternalMaskFromFits(self): CleanMaskImage = self.GD["Mask"]["External"] if not CleanMaskImage: return print(" Reading mask image: %s" % CleanMaskImage, file=log) MaskImage = image(CleanMaskImage).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.ExternalMask = MaskArray
def Rotate(MS,ToRaDec): ra, dec = MS.radec ra1,dec1=ToRaDec # preparing rotation matrices (ORIGINAL PHASE DIRECTION) x = np.sin(ra)*np.cos(dec) y = np.cos(ra)*np.cos(dec) z = np.sin(dec) w = np.array([[x,y,z]]).T x = -np.sin(ra)*np.sin(dec) y = -np.cos(ra)*np.sin(dec) z = np.cos(dec) v = np.array([[x,y,z]]).T x = np.cos(ra) y = -np.sin(ra) z = 0 u = np.array([[x,y,z]]).T T = np.concatenate([u,v,w], axis = -1 ) TT=np.identity(3) x1 = np.sin(ra1)*np.cos(dec1) y1 = np.cos(ra1)*np.cos(dec1) z1 = np.sin(dec1) w1 = np.array([[x1,y1,z1]]).T x1 = -np.sin(ra1)*np.sin(dec1) y1 = -np.cos(ra1)*np.sin(dec1) z1 = np.cos(dec1) v1 = np.array([[x1,y1,z1]]).T x1 = np.cos(ra1) y1 = -np.sin(ra1) z1 = 0 u1 = np.array([[x1,y1,z1]]).T Tshift = np.concatenate([u1,v1,w1], axis=-1) TT = np.dot(Tshift.T,T) uvw_all = MS.uvw uvw=uvw_all Phase=np.dot(np.dot((w-w1).T, T) , uvw.T) uvwNew=np.dot(uvw, TT.T) MS.uvw=uvwNew for chan in range(MS.NSPWChan): wavelength = MS.wavelength_chan.flatten()[chan] f = np.exp(Phase * 2 * np.pi * 1j/wavelength) for pol in range(4): MS.data[:,chan,pol]=MS.data[:,chan,pol] * f.reshape((MS.nrows,))
def SetConvMatrix(self): #print>>log,"SetConvMatrix" PSF=self.PSF NPixPSF=PSF.shape[-1] M=np.zeros((self.NFreqBands,1,self.NPixListData,self.NPixListParms),np.float32) xc=yc=NPixPSF//2 x0,y0=np.array(self.ListPixData).T x1,y1=np.array(self.ListPixParms).T N0=x0.size N1=x1.size dx=(x1.reshape((N1,1))-x0.reshape((1,N0))+xc).T dy=(y1.reshape((N1,1))-y0.reshape((1,N0))+xc).T Cx=((dx>=0)&(dx<NPixPSF)) Cy=((dy>=0)&(dy<NPixPSF)) C=(Cx&Cy) indPSF=np.arange(M.shape[-1]*M.shape[-2]) indPSF_sel=indPSF[C.ravel()] indPixPSF=dx.ravel()[C.ravel()]*NPixPSF+dy.ravel()[C.ravel()] for iBand in range(self.NFreqBands): PSF_Chan=PSF[iBand,0] M[iBand,0].flat[indPSF_sel] = PSF_Chan.flat[indPixPSF.ravel()] self.CM=M self.NormData=np.sum(M**2,axis=2).reshape((self.NFreqBands,self.NPixListParms)) self.DirtyCMMean=np.mean(M,axis=0).reshape((1,1,self.NPixListData,self.NPixListParms)) MParms=np.zeros((self.NFreqBands,1,self.NPixListParms,self.NPixListParms),np.float32) x0,y0=np.array(self.ListPixParms).T x1,y1=np.array(self.ListPixParms).T N0=x0.size N1=x1.size dx=(x1.reshape((N1,1))-x0.reshape((1,N0))+xc).T dy=(y1.reshape((N1,1))-y0.reshape((1,N0))+xc).T Cx=((dx>=0)&(dx<NPixPSF)) Cy=((dy>=0)&(dy<NPixPSF)) C=(Cx&Cy) indPSF=np.arange(MParms.shape[-1]*MParms.shape[-2]) indPSF_sel=indPSF[C.ravel()] indPixPSF=dx.ravel()[C.ravel()]*NPixPSF+dy.ravel()[C.ravel()] for iBand in range(self.NFreqBands): PSF_Chan=PSF[iBand,0] MParms[iBand,0].flat[indPSF_sel] = PSF_Chan.flat[indPixPSF.ravel()] self.CMParms=MParms self.CMParmsMean=np.mean(MParms,axis=0).reshape((1,1,self.NPixListParms,self.NPixListParms))
def setMaskMachine(self, MaskMachine): self.MaskMachine = MaskMachine if self.MaskMachine.ExternalMask is not None: print("Applying external mask", file=log) MaskArray = self.MaskMachine.ExternalMask nch, npol, _, _ = MaskArray.shape self._MaskArray = np.zeros(MaskArray.shape, np.bool8) for ch in range(nch): for pol in range(npol): self._MaskArray[ch, pol, :, :] = np.bool8( 1 - MaskArray[ch, pol].copy())[:, :] self._MaskArray = np.ascontiguousarray(self._MaskArray) self.MaskArray = np.ascontiguousarray(self._MaskArray[0])
def GiveFreqBandsFluxRatio(self, iFacet, Alpha): NFreqBand = self.NFreqBand NAlpha = Alpha.size FreqBandsFluxRatio = np.zeros((NAlpha, NFreqBand), np.float32) for iChannel in range(NFreqBand): for iAlpha in range(NAlpha): ThisAlpha = Alpha[iAlpha] FreqBandsFluxRatio[iAlpha, iChannel] = self.IntExpFunc( Alpha=ThisAlpha, iChannel=iChannel, iFacet=iFacet) return FreqBandsFluxRatio
def ImToGrid(self, ModelIm): nchan, npol, n, _ = ModelIm.shape ModelImCorr = ModelIm * (self.OverS * n)**2 if self.ifzfCF is not None: for ichan in range(nchan): for ipol in range(npol): ModelImCorr[ichan, ipol][:, :] = ModelImCorr[ ichan, ipol][:, :].real / self.ifzfCF ModelUVCorr = self.FFTWMachine.fft(ModelImCorr) return ModelUVCorr
def GiveMDC(ParsetFile="ParsetNew.txt", freqs=None, GD=None, DoReadData=True): MS = [] SM = [] if GD is None: GD = ClassGlobalData(ParsetFile) ListMSCat = GD.DicoConfig["Files"]["FileMSCat"]["Name"] NPointing = len(ListMSCat) ListMS = [] for i in range(NPointing): ThisCat = ListMSCat[i] if ".npy" in ThisCat: ListMS.append(np.load(ThisCat)["dirMSname"][0]) else: ListMS.append(ThisCat) if "FileSourceCat" in GD.DicoConfig["Files"].keys(): ListSM = GD.DicoConfig["Files"]["FileSourceCat"] ThereIsSM = True else: ListSM = [None for i in range(NPointing)] ThereIsSM = False for MSname, SMname in zip(ListMS, ListSM): MS0 = ClassMS.ClassMS(MSname, Col=GD.DicoConfig["Files"]["ColName"], DoReadData=DoReadData) if ThereIsSM: SM0 = ClassSM.ClassSM(SMname) SM0.AppendRefSource((MS0.rac, MS0.decc)) SM.append(SM0) MS0.DelData() MS.append(MS0) MDC = ClassMultiPointingData(GD) for ID in range(NPointing): MDC.setMS(MS[ID], PointingID=ID) if ThereIsSM: MDC.setSM(SM[ID], PointingID=ID) if freqs is None: freqs = MS[ID].ChanFreq.flatten() MDC.setFreqs(freqs, PointingID=ID) MDC.setMappingBL(PointingID=ID) #MME=MeasurementEquation() #HYPERCAL_DIR=os.environ["HYPERCAL_DIR"] #execfile("%s/HyperCal/Scripts/ScriptSetMultiRIME.py"%HYPERCAL_DIR) return MDC, GD #,MME
def ImToGrid(self, ModelIm): npol = self.npol ModelImCorr = ModelIm * (self.WTerm.OverS * self.Padding)**2 nchan, npol, _, _ = ModelImCorr.shape for ichan in range(nchan): for ipol in range(npol): ModelImCorr[ichan, ipol][:, :] = ModelImCorr[ ichan, ipol][:, :].real / self.ifzfCF.real ModelUVCorr = self.FT(ModelImCorr) return ModelUVCorr
def test(): #pBAR= ProgressBar('white', width=50, block='=', empty=' ',Title="Solving ")##, HeaderSize=10) pBAR= ProgressBar(Title=" "+"Init E") nt=10 for NDone in range(nt): f=int(100.*NDone/float(nt-1)) pBAR.render(NDone,nt) timemod.sleep(0.1) pBAR= ProgressBar(Title="Solving hhh ")#, HeaderSize=10) nt=1000 for NDone in range(nt): pBAR.render(NDone,nt) timemod.sleep(0.0001)
def setdata(self, dataIn, CorrT=False): #print>>log, " ----> put data in casa image %s"%self.ImageName data = dataIn.copy() if CorrT: nch, npol, _, _ = dataIn.shape for ch in range(nch): for pol in range(npol): #Need to place stokes data in increasing order because of the linear spacing assumption used in FITS stokes_slice_id = self.Stokes.index( self.sorted_stokes[pol]) data[ch, pol] = dataIn[ch][stokes_slice_id][::-1].T self.imageFlipped = CorrT self.data = data
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 GiveTimeMapping(self, DicoSols, times): """Builds mapping from MS rows to Jones solutions. Args: DicoSols: dictionary of Jones matrices, which includes t0 and t1 entries Returns: Vector of indices, one per each row in DATA, giving the time index of the Jones matrix corresponding to that row. """ print(" Build Time Mapping", file=log) DicoJonesMatrices = DicoSols ind = np.zeros((times.size, ), np.int32) nt, na, nd, _, _, _ = DicoJonesMatrices["Jones"].shape ii = 0 for it in range(nt): t0 = DicoJonesMatrices["t0"][it] t1 = DicoJonesMatrices["t1"][it] ## new code: no assumption of sortedness ind[(times >= t0) & (times < t1)] = it ## old code: assumed times was sorted # indMStime = np.where((times >= t0) & (times < t1))[0] # indMStime = np.ones((indMStime.size,), np.int32)*it # ind[ii:ii+indMStime.size] = indMStime[:] # ii += indMStime.size return ind
def main(): MSListName="MSList6.txt" LMS=["BOOTES24_SB100-109.2ch8s.ms","BOOTES24_SB110-119.2ch8s.ms","BOOTES24_SB120-129.2ch8s.ms","BOOTES24_SB130-139.2ch8s.ms","BOOTES24_SB140-149.2ch8s.ms","BOOTES24_SB150-159.2ch8s.ms"] BaseNameStart="KAFCA.6Freqs.MF.Beam.CompDeg" NSelfCal=4 BaseName_i=BaseNameStart#"%s.%2.2i"%(BaseName,iSelfCal) DDF="DDF.py --MSName=%s --MaxMinorIter=50000 --Gain=0.1 --Npix=16000 --Robust=0 --Weighting=Briggs --Cell=2 --NFacets=11 --ImageName=%s --wmax=50000 --Nw=100 --ColName=CORRECTED_DATA --NCPU=32 --TChunk=10 --OverS=11 --Sup=7 --Scales=[0] --CycleFactor=2. --ScaleAmpGrid=0 --Mode=Clean --DeleteDDFProducts=1 --CompDeGridMode=1 --DDModeGrid=P --BeamMode=LOFAR"%(MSListName,BaseName_i) os.system(DDF) for iSelfCal in range(NSelfCal): for MSName in LMS: KMS="killMS.py --MSName=%s --dt=1 --InCol=CORRECTED_DATA --BaseImageName=%s --SolverType=KAFCA --InitLM=0 --InitLMdt=20 --DoPlot=0 --evPStep=40 --UVMinMax=0.2,300 --NCPU=32 --Resolution=0 --Weighting=Natural --Decorrelation=0 --OverS=17 --BeamMode=LOFAR"%(MSName,BaseName_i) os.system(KMS) BaseName_i+=".S" DDF="DDF.py --MSName=%s --MaxMinorIter=50000 --Gain=0.1 --Npix=16000 --Robust=0 --Weighting=Briggs --Cell=2 --NFacets=11 --ImageName=%s --wmax=50000 --Nw=100 --ColName=CORRECTED_DATA --NCPU=32 --TChunk=10 --OverS=11 --Sup=7 --Scales=[0] --CycleFactor=2. --ScaleAmpGrid=0 --Mode=Clean --DeleteDDFProducts=1 --CompDeGridMode=1 --DDModeGrid=AP --DDSols=KAFCA --BeamMode=LOFAR"%(MSListName,BaseName_i) os.system(DDF)
def PutBackSubsComps(self): # if self.GD["Data"]["RestoreDico"] is None: return SolsFile = self.GD["DDESolutions"]["DDSols"] if not (".npz" in SolsFile): Method = SolsFile ThisMSName = reformat.reformat(os.path.abspath(self.GD["Data"]["MS"]), LastSlash=False) SolsFile = "%s/killMS.%s.sols.npz" % (ThisMSName, Method) DicoSolsFile = np.load(SolsFile) SourceCat = DicoSolsFile["SourceCatSub"] SourceCat = SourceCat.view(np.recarray) # RestoreDico=self.GD["Data"]["RestoreDico"] RestoreDico = DicoSolsFile["ModelName"][()][0:-4] + ".DicoModel" print("Adding previously subtracted components", file=log) ModelMachine0 = ClassModelMachine(self.GD) ModelMachine0.FromFile(RestoreDico) _, _, nx0, ny0 = ModelMachine0.DicoSMStacked["ModelShape"] _, _, nx1, ny1 = self.ModelShape dx = nx1 - nx0 for iSource in range(SourceCat.shape[0]): x0 = SourceCat.X[iSource] y0 = SourceCat.Y[iSource] x1 = x0 + dx y1 = y0 + dx if not ((x1, y1) in self.DicoSMStacked["Comp"].keys()): self.DicoSMStacked["Comp"][(x1, y1)] = ModelMachine0.DicoSMStacked["Comp"][(x0, y0)] else: self.DicoSMStacked["Comp"][(x1, y1)] += ModelMachine0.DicoSMStacked["Comp"][(x0, y0)]
def AppendComponentToDictStacked(self, key, Sols, pol_array_index=0): """ Adds component to model dictionary (with key l,m location tupple). Each component may contain #basis_functions worth of solutions. Note that each basis solution will have multiple Stokes components associated to it. Args: key: the (l,m) centre of the component Fpol: Weight of the solution Sols: Nd array of solutions with length equal to the number of basis functions representing the component. pol_array_index: Index of the polarization (assumed 0 <= pol_array_index < number of Stokes terms in the model) Post conditions: Added component list to dictionary (with keys (l,m) coordinates). This dictionary is stored in self.DicoSMStacked["Comp"] and has keys: "SolsArray": solutions ndArray with shape [#basis_functions,#stokes_terms] "SumWeights": weights ndArray with shape [#stokes_terms] """ nchan, npol, nx, ny = self.ModelShape if not (pol_array_index >= 0 and pol_array_index < npol): raise ValueError("Pol_array_index must specify the index of the slice in the " "model cube the solution should be stored at. Please report this bug.") DicoComp = self.DicoSMStacked.setdefault("Comp", {}) if not (key in DicoComp.keys()): DicoComp[key] = {} for p in range(npol): DicoComp[key]["SolsArray"] = np.zeros((Sols.size, npol), np.float32) DicoComp[key]["SumWeights"] = np.zeros((npol), np.float32) SolNorm = Sols.ravel() * self.GD["Deconv"]["Gain"] DicoComp[key]["SumWeights"][pol_array_index] += 1.0 DicoComp[key]["SolsArray"][:, pol_array_index] += SolNorm
def Plot(self, pop, iGen): V = tools.selBest(pop, 1)[0] S = self.PM.ArrayToSubArray(V, "S") Al = self.PM.ArrayToSubArray(V, "Alpha") # Al.fill(0) # Al[11]=0. # S[0:11]=0 # S[12:]=0 # S[11]=10000. for iChannel in range(self.NFreqBands): self.PlotChannel(pop, iGen, iChannel=iChannel) import pylab pylab.figure(30, figsize=(5, 3)) #pylab.clf() S = self.PM.ArrayToSubArray(V, "S") Al = self.PM.ArrayToSubArray(V, "Alpha") pylab.subplot(1, 2, 1) pylab.plot(S) pylab.subplot(1, 2, 2) pylab.plot(Al) pylab.draw() pylab.show(False) pylab.pause(0.1)
def test_Dot_ListBlockMat_Mat(): nblocks = 50 n = 100 m = 200 B = np.random.randn(nblocks * n, m) ListBlocks = [] BlocksMat = np.zeros((nblocks * n, nblocks * n), float) for iblock in range(nblocks): ThisBlock = np.random.randn(n, n) ListBlocks.append(ThisBlock) istart = iblock * n BlocksMat[istart:istart + n, istart:istart + n] = ThisBlock import ClassTimeIt T = ClassTimeIt.ClassTimeIt() print("Dimensions A[%s], B[%s]" % (BlocksMat.shape, B.shape)) R0 = Dot_ListBlockMat_Mat(ListBlocks, B) T.timeit("ListProd") R1 = np.dot(BlocksMat, B) T.timeit("NpProd") R2 = Dot_ListBlockMat_Mat_Iregular(ListBlocks, B) T.timeit("ListProdIrregular") print(np.allclose(R0, R1)) print(np.allclose(R2, R1))
def GiveBeamFactorsFacet(self, iFacet): if iFacet in self.DicoBeamFactors: return self.DicoBeamFactors[iFacet] SumJonesChan = self.PSFServer.DicoMappingDesc["SumJonesChan"] ChanMappingGrid = self.PSFServer.DicoMappingDesc["ChanMappingGrid"] ChanMappingGridChan = self.PSFServer.DicoMappingDesc[ "ChanMappingGridChan"] ListBeamFactor = [] ListBeamFactorWeightSq = [] for iChannel in range(self.nchan): nfreq = len(self.PSFServer.DicoMappingDesc["freqs"][iChannel]) ThisSumJonesChan = np.zeros(nfreq, np.float64) ThisSumJonesChanWeightSq = np.zeros(nfreq, np.float64) for iMS in SumJonesChan.keys(): ind = np.where(ChanMappingGrid[iMS] == iChannel)[0] channels = ChanMappingGridChan[iMS][ind] ThisSumJonesChan[channels] += SumJonesChan[iMS][iFacet, 0, ind] ThisSumJonesChanWeightSq[channels] += SumJonesChan[iMS][iFacet, 1, ind] ListBeamFactor.append(ThisSumJonesChan) ListBeamFactorWeightSq.append(ThisSumJonesChanWeightSq) self.DicoBeamFactors[ iFacet] = ListBeamFactor, ListBeamFactorWeightSq, self.PSFServer.DicoMappingDesc[ 'MeanJonesBand'][iFacet] return ListBeamFactor, ListBeamFactorWeightSq, self.PSFServer.DicoMappingDesc[ 'MeanJonesBand'][iFacet]
def FitSPIComponents(self, FitCube, nu, nu0): """ Slow version using serial scipy.optimise.curve_fit to fit the spectral indices. Used as a fallback if africanus version not found :param FitCube: (ncomps, nfreqs) data array :param nu: freqs :param nu0: ref freq :return: """ def spi_func(nu, I0, alpha): return I0 * nu**alpha nchan, ncomps = FitCube.shape Iref = np.zeros([ncomps]) varIref = np.zeros([ncomps]) alpha = np.zeros([ncomps]) varalpha = np.zeros([ncomps]) I0 = 1.0 alpha0 = -0.7 for i in range(ncomps): popt, pcov = curve_fit(spi_func, nu / nu0, FitCube[:, i], p0=np.array([I0, alpha0])) Iref[i] = popt[0] varIref[i] = pcov[0, 0] alpha[i] = popt[1] varalpha[i] = pcov[1, 1] return alpha, varalpha, Iref, varIref
def mixToSingle(self, coeffs): """Convert mixed units to a single value. [ coeffs is a sequence of numbers not longer than len(self.factors)+1 -> return the equivalent single value in self's system ] """ #-- 1 -- total = 0.0 #-- 2 -- # [ if len(coeffs) <= len(self.factors)+1 -> # coeffList := a copy of coeffs, right-padded to length # len(self.factors)+1 with zeroes if necessary ] coeffList = self.__pad(coeffs) #-- 3 -- # [ total +:= (coeffList[-1] * # (product of all elements of self.factors)) + # (coeffList[-2] * # (product of all elements of self.factors[:-1])) + # (coeffList[-3] * # (product of all elements of self.factors[:-2])) # ... ] for i in range(-1, -len(self.factors) - 1, -1): total += coeffList[i] total /= self.factors[i] #-- 4 -- total += coeffList[0] #-- 5 -- return total
def AddVisToJonesMapping(self, Jones, VisTimes, VisFreqs): print("Building VisToJones time mapping...", file=log) DicoJonesMatrices = Jones G = DicoJonesMatrices["Jones"] ind = np.zeros((VisTimes.size, ), np.int32) nt, nd, na, _, _, _ = G.shape ii = 0 for it in range(nt): t0 = DicoJonesMatrices["t0"][it] t1 = DicoJonesMatrices["t1"][it] indMStime = np.where((VisTimes >= t0) & (VisTimes < t1))[0] indMStime = np.ones((indMStime.size, ), np.int32) * it # print "=================" # print t0,t1,t1-t0 # print it,indMStime.size,np.max(ind) ind[ii:ii + indMStime.size] = indMStime[:] ii += indMStime.size Jones["Map_VisToJones_Time"] = ind print("Building VisToJones freq mapping...", file=log) FreqDomains = Jones["FreqDomains"] NChanJones = FreqDomains.shape[0] MeanFreqJonesChan = (FreqDomains[:, 0] + FreqDomains[:, 1]) / 2. NVisChan = VisFreqs.size DFreq = np.abs( VisFreqs.reshape((NVisChan, 1)) - MeanFreqJonesChan.reshape((1, NChanJones))) VisToJonesChanMapping = np.argmin(DFreq, axis=1) Jones["Map_VisToJones_Freq"] = VisToJonesChanMapping print(" VisToJonesChanMapping %s" % str(VisToJonesChanMapping), file=log)
def singleToMix(self, value): """Convert to mixed units. [ value is a float -> return value as a sequence of coefficients in self's system ] """ #-- 1 -- # [ whole := whole part of value # frac := fractional part of value ] whole, frac = divmod(value, 1.0) result = [int(whole)] #-- 2 -- # [ result := result with integral parts of value # in self's system appended ] for factorx in range(len(self.factors)): frac *= self.factors[factorx] whole, frac = divmod(frac, 1.0) result.append(int(whole)) #-- 3 -- # [ result := result with frac added to its last element ] result[-1] += frac #-- 4 -- return result
def run(self): while not self.kill_received and self.CondContinue(): #gc.enable() try: iQueue = self.work_queue.get_nowait() #(True,2) except Exception, e: #print "Exception worker: %s"%str(e) break #print "Start %i"%iQueue Queue = NpShared.GiveArray("%sQueue_%3.3i" % (self.IdSharedMem, iQueue)) self.CurrentInvCov = NpShared.GiveArray("%sInvCov_AllFacet" % (self.IdSharedMem)) for iJob in range(Queue.shape[0]): x0, y0, FacetID = Queue[iJob] iFacet = FacetID self.CurrentFacetID = FacetID #self.CurrentCF=NpShared.GiveArray("%sConvMatrix_Facet_%4.4i"%(self.IdSharedMem,iFacet)) self.CurrentCM = NpShared.GiveArray("%sCM_Facet%4.4i" % (self.IdSharedMem, iFacet)) #self.CurrentInvCov=NpShared.GiveArray("%sInvCov_Facet%4.4i"%(self.IdSharedMem,iFacet)) # if self.CurrentInvCov is None: # invCM=ModLinAlg.invSVD(np.float64(self.CurrentCM[0,0]))/self.Var # self.CurrentInvCov=NpShared.ToShared("%sInvCov_Facet%4.4i"%(self.IdSharedMem,iFacet),invCM) iGauss = self.SmearThisComp(x0, y0) Queue[iJob, 2] = iGauss self.result_queue.put({"Success": True, "iQueue": iQueue})
def GetMergedFreqDomains(self, DicoJ0, DicoJ1): print("Compute frequency domains of merged Jones arrays", file=log) FreqDomain0 = DicoJ0["FreqDomains"] FreqDomain1 = DicoJ1["FreqDomains"] f0 = FreqDomain0.flatten().tolist() f1 = FreqDomain1.flatten().tolist() ff = np.array(sorted(list(set(f0 + f1)))) dff = np.abs(ff[1::] - ff[0:-1]) LF = [] MaskSkip = np.zeros((ff.size, ), bool) for iFreq in range(ff.size): if MaskSkip[iFreq]: continue df = np.abs(ff - ff[iFreq]) ind = np.where(df < 1.)[0] MaskSkip[ind] = 1 LF.append(ff[iFreq]) fmin = np.max([FreqDomain0.min(), FreqDomain1.min()]) fmax = np.min([FreqDomain0.max(), FreqDomain1.max()]) ff = np.array(LF) nf = ff.size FreqDomainOut = np.zeros((nf - 1, 2), np.float64) FreqDomainOut[:, 0] = ff[0:-1] FreqDomainOut[:, 1] = ff[1::] fm = np.mean(FreqDomainOut, axis=1) ind = np.where((fm >= fmin) & (fm < fmax))[0] FreqDomainOut = FreqDomainOut[ind] print(" There are %i channels in the merged Jones array" % FreqDomainOut.shape[0], file=log) return FreqDomainOut