Esempio n. 1
0
  def _plotStripe(self,points,i,rmin,rmax):
    ax = self.xpx
    ay = self.ypx
    #find closest points to find limits
    m = np.diff(points[:,1])/np.diff(points[:,0])
    c = points[0,1] - points[0,0]*m

    m_ = -1/m
    dist_perp = (ay-m*ax-c)/np.sqrt(m**2+1)
    dist_par  = (ay-m_*ax)/np.sqrt(m_**2+1)
    distparrange = dist_par[np.abs(dist_perp)<=rmax]
    distparrange = [np.min(distparrange),np.max(distparrange)]
    i[np.abs(dist_perp)<rmin] = np.nan


    tperpvec = np.arange(-rmax,rmax,110.*np.sqrt(2)) 
    tparvec  = np.arange(distparrange[0],distparrange[1],110.*np.sqrt(2))
    perpind = np.digitize(dist_perp.ravel(),tperpvec)
    parind  = np.digitize(dist_par.ravel(),tparvec)
    binning = np.ravel_multi_index((perpind,parind),(len(tperpvec)+1,len(tparvec)+1))
    numperbin = np.bincount(binning)
    wireROI = np.bincount(binning, weights=np.asfarray(i.ravel()))
    wireROI = wireROI/numperbin
    P = np.zeros((len(tperpvec)+1) * (len(tparvec)+1))
    P[:len(wireROI)] = wireROI
    P = np.reshape(P,(len(tperpvec)+1,len(tparvec)+1))
    pl.clf()
    tools.imagesc(tparvec[:-1]+np.mean(np.diff(tparvec)),
                  tperpvec[:-1]+np.mean(np.diff(tperpvec)),
                  P[1:-1,1:-1])
    tools.clim_std()
    pl.axis('normal')
    pl.draw()
Esempio n. 2
0
 def _refinePoints(self,points,i,refinerad=5000):
   #refine points
   newpoints = []
   reffig = pl.figure()
   for point in points:
     xind = (point[0]-refinerad <= self.xVec) & (self.xVec <= point[0]+refinerad)
     yind = (point[1]-refinerad <= self.yVec) & (self.yVec <= point[1]+refinerad)
     pl.clf()
     tools.imagesc(self.xVec[xind],self.yVec[yind],
       i[np.min(yind.nonzero()):np.max(yind.nonzero())+1,np.min(xind.nonzero()):np.max(xind.nonzero())+1])
     pl.plot(point[0],point[1],'go')
     tools.clim_std()
     pl.draw()
     newpoints.append(pl.ginput(1)[0])
   points = np.array(newpoints)
   return points
Esempio n. 3
0
 def testCorrfunc(self,order=5,ic=None):
   fig = tools.nfigure('test_correction_func_order_%d'%order)
   plt.clf()
   fig,ax = plt.subplots(1,2,num=fig.number)
   plt.axes(ax[0])
   it = (self.Imat.T/self.I0).T
   tools.imagesc(np.arange(np.shape(self.Imat)[1]),self.I0,(it/np.mean(it,0))-1)
   tools.clim_std(2)
   plt.colorbar()
   plt.draw()
   cf = self.getCorrFunc(order=order,i0_wp=ic,wrapit=False)
   Icorr = cf(self.Imat,self.I0)
   plt.axes(ax[1])
   it = (Icorr.T/self.I0).T
   tools.imagesc((it/np.mean(it,0))-1)
   tools.clim_std(2)
   plt.colorbar()
Esempio n. 4
0
 def createWireMask(self,data=None):
   if data==None:
     import ixppy
     d = ixppy.dataset('/reg/g/xpp/data/example_data/cspad/liquid_scattering/hdf5/xpp43512-r0004.h5',['cspad'])
     data = d.cspad.rdStepData(0,range(10))
   i = np.mean(data,axis=0)
   ib = self.bin(i)
   tools.imagesc(self.xVec,self.yVec,ib)
   tools.clim_std()
   pl.draw()
   print "Roughly select wire end pairs, press middle mouse button when finished."
   wires = []
   tpos = np.array(pl.ginput(2))
   pointno = 0
   while not tpos==[]:
     tpos = np.array(tpos)
     pl.plot(tpos[:,0],tpos[:,1],'go-')
     for pno in range(2):pl.text(tpos[pno,0],tpos[pno,1],str(pointno),color='r')
     wires.append(tpos)
     pointno += 1
     tpos = pl.ginput(2)
   refwires = []
   for pos in wires:
     refwires.append(self._refinePoints(pos,ib))
   wirewidths = []
   print refwires
   for refwire in refwires:
     trad = self._getOneWire(refwire,i,rmax=1000)
     wirewidths.append(trad)
   
   masked = np.zeros(np.shape(i),dtype=bool)
   ax = self.xpx
   ay = self.ypx
   for refwire,wirewidth in zip(refwires,wirewidths):
     points = refwire
     m = np.diff(points[:,1])/np.diff(points[:,0])
     c = points[0,1] - points[0,0]*m
     m_ = -1/m
     dist_perp = (ay-m*ax-c)/np.sqrt(m**2+1)
     dist_par  = (ay-m_*ax)/np.sqrt(m_**2+1)
     masked[np.abs(dist_perp)<wirewidth] = True
   np.save('cspad_last_pixel_mask.npy',masked)
Esempio n. 5
0
def getCorr(order=5,i0=None,Imat=None,i0_wp=1e6,fraclims_dc=[.9,1.1]):
  """ Getting nonlinear correction factors form a calibration dataset consiting of:
  i0     	array of intensities the calibration has been made for
  Imat   	2D array of the corresponding reference patterns, in each row 
  		there is one ravelled array of each intensity bin in i0.
  i0_wp		a working point around which a correction polynomial will be
  		developed for each pixel.
  order		the polynomial order up to which will be deveoped.
  fraclims_dc	relative factor for the i0,Imat data limits which are used to
  		determine the working point location.
  
  Returns

  """

  #i0,Imat = getData()
  msk = tools.filtvec(i0,i0_wp*np.asarray(fraclims_dc))
  p0 = tools.polyFit(i0[msk],Imat[msk,:],2)
  dc = tools.polyVal(p0,i0_wp)
  comps = tools.polyFit(i0-i0_wp,Imat-dc,order,removeOrders=[0])
  compsder = tools.polyDer(comps)
  c = lambda(i): tools.polyVal(comps,i-np.asarray(tools.iterfy(i0_wp)))+dc
  c_prime = lambda(i): tools.polyVal(compsder,i-np.asarray(tools.iterfy(i0_wp)))
  t = lambda(i): (c_prime(i0_wp).T * (i-i0_wp)).T + dc
  
  cprimeic = c_prime(i0_wp)
  dcorr_const = -cprimeic*i0_wp + c(i0_wp) - t(0) 
  def dcorr(i,D):
    return (i*cprimeic.T + dcorr_const.T + ((D-c(i))*cprimeic/c_prime(i)).T).T
    #return (i*cprimeic.T + dcorr_const.T ).T
  return dcorr,comps,t





  tools.nfigure('testplot')
  plt.clf()
  plt.subplot(1,2,1)
  Imean = (Imat.T/i0).T
  tools.imagesc(np.asarray([ti / np.mean(Imean[-10:,:],0) for ti in Imean]))
  tools.clim_std(6)
  cl = plt.gci().get_clim()
  plt.colorbar()
  plt.set_cmap(plt.cm.RdBu_r)
  
  
  plt.subplot(1,2,2)

  cmps = copy.copy(comps)
  cmps[-2,:] = 0
  cc = lambda(i): tools.polyVal(cmps,i-np.asarray(tools.iterfy(i0_wp)))
  Ir = Imat-c(i0)+t(i0)-t(0)
  Ir = dcorr(i0,Imat)
  #Ir = ((Imat-cc(i0)).T/i0).T
  #tools.imagesc(Ir) 
  Ir = (Ir.T/i0).T
  tools.imagesc(np.asarray([ti / np.mean(Ir[-10:,:],0) for ti in Ir]))
  plt.clim(cl)
  plt.colorbar()
  plt.set_cmap(plt.cm.RdBu_r)
  plt.draw()

  tools.nfigure('testplot_components')
  plt.clf()
  ah = None
  for n,comp in enumerate(comps):
    if ah is None:
      ah = plt.subplot(len(comps),1,n+1)
    else:
      plt.subplot(len(comps),1,n+1,sharex=ah)
    plt.plot(comp)
    lims = np.percentile(comp,[1,99])

    plt.ylim(lims)


  return c,c_prime