Esempio n. 1
0
  def Fit() :
      if W.verbose>0 :verbose =1 


      if (W.type["fit"]=="Gaussian2D"):  
         tmp = {"spread_y":W.suposed_param["spread_x"] , "theta":0.1 }
         W.suposed_param.update(   tmp  ) 

      elif(W.type["fit"]=='Gaussian'): pass 

      elif (W.type["fit"]=='Moffat'): # 1.5 = /np.sqrt(1/2**(-1/b)-1)
        W.suposed_param.update( {'spread_x':1.5*my_fwhm,'exponent':2}  )  

      elif (W.type["fit"]=="Moffat2D"):    # 0.83 = sqrt(ln(2))
         tmp = {"spread_y":W.suposed_param["spread_x"] , "theta":0.1 ,"exponent":2}
         W.suposed_param.update(   tmp  ) 

      elif (W.type["fit"]=='Bessel1'): pass 

      elif (W.type["fit"]=="Bessel12D"):    # 0.83 = sqrt(ln(2))
         tmp = {"spread_y":W.suposed_param["spread_x"] , "theta":0.1 }
         W.suposed_param.update(   tmp  ) 

      elif ("Gaussian_hole" in W.type["fit"]): # we consider 2D or not, same_center or not 
         W.suposed_param.update( { 'center_x_hole':x0,'center_y_hole':y0,'spread_x_hole':0.83*(my_fwhm)/2,'spread_y_hole':0.83*my_fwhm/2,'intensity_hole':0,'theta':0.1,'theta_hole':0.1}  ) 
         if not ("2D" in W.type["fit"]): 
               W.suposed_param["2D"]=0 
               doNotFit.append("theta"); doNotFit.append("theta_hole")
               doNotFit.append("spread_y"); doNotFit.append("spread_y_hole")
         else : W.suposed_param["2D"]=1 
         if ("same_center" in W.type["fit"]): 
               W.suposed_param["same_center"]=1
               doNotFit.append("center_x_hole"); doNotFit.append("center_y_hole")
         doNotFit.append("2D"); doNotFit.append("same_center")


      # ACTUAL FIT  
      if W.type["fit"] != "None" :
         res = FF.leastsqFit(vars(BF)[W.type["fit"]],(x,y),W.suposed_param,IX,err=eIX,doNotFit=doNotFit,bounds=James,verbose=verbose)  
      else : 
          restmp = {'center_x':x0,'center_y':y0,'intensity':my_max,"r99x":5*my_fwhm,"r99y":5*my_fwhm }
          res= (restmp,0,0, IX,0 )
      return res 
Esempio n. 2
0
def TightBinaryPsf(grid,search=False): # slowlyer 
    """search = True means we search the maximum """

    ########
    #" FIRST GUESS 
    max0 = G.star1 
    max1 = G.star2
  
    star_distance = np.sqrt(   (max0[0]-max1[0])**2 + (max0[1]-max1[1])**2  ) # distance between two pooints 
    my_center = [(max0[0]+max1[0])/2 , (max0[1]+max1[1])/2 ]
    dist0 = min( IF.FWHM(grid,max0) , star_distance /2  )
    dist1 = min( IF.FWHM(grid,max1) , star_distance /2  ) 

    ###########
    # make limits od fit  
    bd_x0 = (max0[0] - star_distance/2, max0[0] + star_distance/2    ) 
    bd_y0 = (max0[1] - star_distance/2, max0[1] + star_distance/2    ) 

    bd_x1 = (max1[0] - star_distance/2, max1[0] + star_distance/2    ) 
    bd_y1 = (max1[1] - star_distance/2, max1[1] + star_distance/2    ) 
    
    ########
    # DEFINE fitting space 
    fit_range = star_distance + 5 * max(dist0,dist1) # range of the fit 
        # the error
    rx1,rx2=  int(my_center[0] - fit_range/2) ,  int(my_center[0] + fit_range/2)
    ry1,ry2 = int(my_center[1] - fit_range/2) ,  int(my_center[1] + fit_range/2)
    if W.verbose>3:
          print "----->IF.BinaryPSF :"
          print "The fit is done between points ", (rx1,ry1)," and ", (rx2,ry2) 
          print  "with fit", W.type["fit"] 
    X,Y=np.arange(int(rx1),int(rx2)+1),np.arange(int(ry1),int(ry2)+1)
    y,x = np.meshgrid(Y,X) 
    IX=grid[rx1:rx2+1,ry1:ry2+1]
    IX,mIX=IF.FindBadPixel(IX)#,r=r)
    eIX = (IX-mIX).std() 
    eIX *= np.ones(IX.shape)

    ###################
    ## Supposed params and bounds #
    ###################
    W.suposed_param={'x0':max0[0],'x1':max1[0],'y0':max0[1],'y1':max1[1],
         'spread_x0':0.83*(dist0),'spread_x1':0.83*dist1,                  
         'spread_y0':0.83*(dist0),'spread_y1':0.83*dist1,                  
         'intensity0':grid[max0[0]][max0[1]],'intensity1':grid[max1[0]][max1[1]],
         'background':0, "theta":1  }

    cut1 = W.Im0 [G.star1[0]-2:G.star1[0]+2, G.star1[1]-2:G.star1[1]+2] 
    min1 = np.median(cut1)  
    max1   = np.max(cut1)  
    max1 = 2*max1 - min1

    cut2 = W.Im0 [G.star2[0]-2:G.star2[0]+2, G.star2[1]-2:G.star2[1]+2] 
    min2 = np.median(cut2)  
    max2   = np.max(cut2) 
    max2 = 2*max2 - min2
    James={
          'x0':(G.star1[0]-2,G.star1[0]+2),
          'x1':(G.star2[0]-2,G.star2[0]+2),
          'y0':(G.star1[1]-2,G.star1[1]+2),
          'y1':(G.star2[1]-2,G.star2[1]+2),
         'spread_x0':(-0.1,None) ,
	 'spread_x1':(-0.1,None),                  
         'spread_y0':(-0.1,None),  
	 'spread_y1':(-0.1,None) ,                  
         'intensity0':(min1,max1) ,
	 'intensity1':(min2,max2) ,
         'background':(None,None) , 
	 "theta":(-0.1,3.24) } # becasue James Bound hahahah, These are the fitting limits of the varaibles . WARNING    we put the intensity positive becasue in a binary fit situation you know.... who knows 




    ###########
    ## DO NOT FIT, dic_for_fit 
    if "Bessel" in W.type["fit"] : 
       if W.verbose > 0  : print "WARNING : no bessel 2pt fit type now,fit type is set to gaussian"
       W.type["fit"]="Gaussian"

    if "Moffat" in W.type["fit"] : 
       W.suposed_param['b0'],W.suposed_param['b1']=1.8,1.8
       James['b0']=(1,3) ; James['b1']=(1,3)  
         			    
    doNotFit = []
    dic_for_fit = {"same_psf": W.same_psf,"aniso":0}
    if "2D" in W.type["fit"] : dic_for_fit["aniso"]=1

    if W.same_psf:  
       doNotFit.append("spread_x1") 
       doNotFit.append("spread_y1") 
       if not "2D" in W.type["fit"] :
         doNotFit.append("spread_y0") 
         doNotFit.append("theta") 
       if "Moffat" in W.type["fit"] : 
         doNotFit.append("b1") 
    else : # not same psf  
       if not "2D" in W.type["fit"] : 
         doNotFit.append("spread_y0") 
         doNotFit.append("spread_y1") 
         doNotFit.append("theta") 




    ##########
    # PRINT 
    if W.verbose>3 : print "Binary FiT, supposed parameters : ", W.suposed_param  
    if W.verbose>3 : print  "fit type is : " ,W.type["fit"]
    if W.verbose>3 : print "anisoplanetism="+ str(bool(dic_for_fit["aniso"])),"same_psf="+str(bool(dic_for_fit["same_psf"]))



    #####################
    ## FIT FIT  ahora si que si 
    res = FF.leastsqFit(
           lambda x,y: vars(BF)[W.type["fit"].replace("2D","")+"2pt"](x,y,dic=dic_for_fit),
              (x,y),W.suposed_param,IX,
              err=eIX,doNotFit=doNotFit,
              bounds=James,
              verbose = W.verbose>1,
              ) 
       
    ##############
    # Restore not fitted variables 
    def Restore(list,to_change,reference): 
        list[0][to_change] = list[0][reference]
        list[1][to_change] = list[1][reference]
        return list
       
    if W.same_psf:  
       res = Restore(res,"spread_x1","spread_x0")
       res = Restore(res,"spread_y1","spread_y0")
       if not "2D" in W.type["fit"] :
         res = Restore(res,"spread_y0","spread_x0")
       if "Moffat" in W.type["fit"] : 
         res = Restore(res,"b1","b0")
    else : # not same psf  
       if not "2D" in W.type["fit"] : 
         res = Restore(res,"spread_y0","spread_x0")
         res = Restore(res,"spread_y1","spread_x1")
                            		


    # BACKKUP DIC   
    tmp = {} ; tmp.update(res[0])
    res[0]["fit_dic"]= tmp			  


         	 
    # FWHM , Photo < from fit 
    dic_copy0 = res[0].copy() 
    dic_copy1 = res[0].copy() 
    for key in res[0].keys() : 
       if "0" in key : 
          dic_copy0[  key.replace("0","")]  = dic_copy0[ key ]
       elif "1" in key : 
          dic_copy1[  key.replace("1","")]  = dic_copy1[ key ]
       # nevermind the center x0 and x1
    dic_copy0["exponent"] = dic_copy0["b0"]
    dic_copy1["exponent"] = dic_copy1["b1"]

    tmp =  IF.FwhmFromFit( dic_copy0 , W.type["fit"]  ) 
    res[0].update( {"fwhm_x0":tmp["fwhm_x"] ,  
                    "fwhm_y0":tmp["fwhm_y"] , 
                    "photometry_fit0":tmp["photometry_fit"] , 
		   } ) 
    tmp =  IF.FwhmFromFit( dic_copy1 , W.type["fit"] ) 
    res[0].update( {"fwhm_x1":tmp["fwhm_x"] ,  
                    "fwhm_y1":tmp["fwhm_y"] , 
                    "photometry_fit1":tmp["photometry_fit"] 
		   } ) 
 

    if False :  
      if "Gaussian" in W.type["fit"] :
        if "2D" in W.type["fit"] :
         photometry0= np.pi*res[0]['intensity0']*res[0]['spread_x0']*res[0]['spread_y0']
         photometry1= np.pi*res[0]['intensity1']*res[0]['spread_x1']*res[0]['spread_y1']
        else : 
         photometry0= np.pi*res[0]['intensity0']*res[0]['spread_x0']**2
         photometry1= np.pi*res[0]['intensity1']*res[0]['spread_x1']**2

      if "Moffat" in W.type["fit"] :
        if "2D" in W.type["fit"] : 
         photometry0= np.pi*res[0]['intensity0']*res[0]['spread_x0']*res[0]['spread_y0']/(res[0]['b0']-1)
         photometry1= np.pi*res[0]['intensity1']*res[0]['spread_x1']*res[0]['spread_y1']/(res[0]['b1']-1)
        else : 
         photometry0= np.pi*res[0]['intensity0']*res[0]['spread_x0']**2/(res[0]['b0']-1)
         photometry1= np.pi*res[0]['intensity1']*res[0]['spread_x1']**2/(res[0]['b1']-1)

      if "hole" in W.type["fit"] : photometry-= np.pi*res[0]['intensity_hole']*res[0]['spread_x_hole']*res[0]['spread_y_hole']

      res[0]['center_x'],res[0]['center_y']=my_center[0],my_center[1] # to draw
      res[0]["photometry0"],res[0]["photometry1"]=photometry0,photometry1


    res[0]["my_photometry0"],res[0]["my_photometry1"]= res[0]["photometry_fit0"],res[0]["photometry_fit1"]

    return res