few more pyFAI related functions includind a neat way to quickly find the center and some utility (pyFAI_dict)
This commit is contained in:
		
							parent
							
								
									518fc6175e
								
							
						
					
					
						commit
						0a7b628ac1
					
				
							
								
								
									
										71
									
								
								mcutils.py
								
								
								
								
							
							
						
						
									
										71
									
								
								mcutils.py
								
								
								
								
							| 
						 | 
					@ -1220,6 +1220,24 @@ def insertInSortedArray(a,v):
 | 
				
			||||||
  a[idx]=v
 | 
					  a[idx]=v
 | 
				
			||||||
  return a
 | 
					  return a
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					##### X-ray images #############
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def pyFAIread(fname):
 | 
				
			||||||
 | 
					  import fabio
 | 
				
			||||||
 | 
					  f = fabio.open(fname)
 | 
				
			||||||
 | 
					  data = f.data
 | 
				
			||||||
 | 
					  del f
 | 
				
			||||||
 | 
					  return data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def pyFAI_dict(ai):
 | 
				
			||||||
 | 
					  """ ai is a pyFAI azimuthal intagrator"""
 | 
				
			||||||
 | 
					  methods = dir(ai)
 | 
				
			||||||
 | 
					  methods = [m for m in methods if m.find("get_") == 0]
 | 
				
			||||||
 | 
					  names   = [m[4:] for m in methods]
 | 
				
			||||||
 | 
					  values  = [getattr(ai,m)() for m in methods]
 | 
				
			||||||
 | 
					  ret = dict( zip(names,values) )
 | 
				
			||||||
 | 
					  return ret
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def pyFAI1d(ai, imgs, mask = None, npt_radial = 600, method = 'csr',safe=True,dark=10., polCorr = 1):
 | 
					def pyFAI1d(ai, imgs, mask = None, npt_radial = 600, method = 'csr',safe=True,dark=10., polCorr = 1):
 | 
				
			||||||
    """ ai is a pyFAI azimuthal intagrator 
 | 
					    """ ai is a pyFAI azimuthal intagrator 
 | 
				
			||||||
              it can be defined with pyFAI.load(ponifile)
 | 
					              it can be defined with pyFAI.load(ponifile)
 | 
				
			||||||
| 
						 | 
					@ -1252,6 +1270,59 @@ def pyFAI2d(ai, imgs, mask = None, npt_radial = 600, npt_azim=360,method = 'csr'
 | 
				
			||||||
      out[_i] = i2d
 | 
					      out[_i] = i2d
 | 
				
			||||||
    return q,azTheta,np.squeeze(out)
 | 
					    return q,azTheta,np.squeeze(out)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def _calc_R(x,y, xc, yc):
 | 
				
			||||||
 | 
					  """ calculate the distance of each 2D points from the center (xc, yc) """
 | 
				
			||||||
 | 
					  return np.sqrt((x-xc)**2 + (y-yc)**2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def _chi2(c, x, y):
 | 
				
			||||||
 | 
					  """ calculate the algebraic distance between the data points and the mean
 | 
				
			||||||
 | 
					      circle centered at c=(xc, yc) """
 | 
				
			||||||
 | 
					  Ri = _calc_R(x, y, *c)
 | 
				
			||||||
 | 
					  return Ri - Ri.mean()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def leastsq_circle(x,y):
 | 
				
			||||||
 | 
					  from scipy import optimize
 | 
				
			||||||
 | 
					  # coordinates of the barycenter
 | 
				
			||||||
 | 
					  center_estimate = np.nanmean(x), np.nanmean(y)
 | 
				
			||||||
 | 
					  center, ier = optimize.leastsq(_chi2, center_estimate, args=(x,y))
 | 
				
			||||||
 | 
					  xc, yc = center
 | 
				
			||||||
 | 
					  Ri       = _calc_R(x, y, *center)
 | 
				
			||||||
 | 
					  R        = Ri.mean()
 | 
				
			||||||
 | 
					  residu   = np.sum((Ri - R)**2)
 | 
				
			||||||
 | 
					  return xc, yc, R
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def pyFAI_find_center(img,psize=100e-6,dist=0.1,wavelength=0.8e-10,**kwargs):
 | 
				
			||||||
 | 
					  import pyFAI
 | 
				
			||||||
 | 
					  plt.ion()
 | 
				
			||||||
 | 
					  kw = dict( pixel1 = psize, pixel2 = psize, dist = dist,wavelength=wavelength )
 | 
				
			||||||
 | 
					  kw.update(kwargs)
 | 
				
			||||||
 | 
					  ai =  pyFAI.azimuthalIntegrator.AzimuthalIntegrator(**kw)
 | 
				
			||||||
 | 
					  fig_img,ax_img = plt.subplots(1,1)
 | 
				
			||||||
 | 
					  fig_pyfai,ax_pyfai = plt.subplots(1,1)
 | 
				
			||||||
 | 
					  fig_pyfai = plt.figure(2)
 | 
				
			||||||
 | 
					  ax_img.imshow(img)
 | 
				
			||||||
 | 
					  plt.sca(ax_img); # set figure to use for mouse interaction
 | 
				
			||||||
 | 
					  ans = ""
 | 
				
			||||||
 | 
					  print("Enter 'end' when done")
 | 
				
			||||||
 | 
					  while ans != "end":
 | 
				
			||||||
 | 
					    if ans == "":
 | 
				
			||||||
 | 
					      print("Click on beam center:")
 | 
				
			||||||
 | 
					      plt.sca(ax_img); # set figure to use for mouse interaction
 | 
				
			||||||
 | 
					      xc,yc = plt.ginput()[0]
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
 | 
					      xc,yc = map(float,ans.split(","))
 | 
				
			||||||
 | 
					    print("Selected center:",xc,yc)
 | 
				
			||||||
 | 
					    ai.set_poni1(xc*psize)
 | 
				
			||||||
 | 
					    ai.set_poni2(yc*psize)
 | 
				
			||||||
 | 
					    q,az,i = pyFAI2d(ai,img)
 | 
				
			||||||
 | 
					    ax_pyfai.pcolormesh(q,az,i)
 | 
				
			||||||
 | 
					    ax_pyfai.set_title(str( (xc,yc) ))
 | 
				
			||||||
 | 
					    plt.pause(0.01)
 | 
				
			||||||
 | 
					    plt.draw()
 | 
				
			||||||
 | 
					    ans=input("Enter to continue with clinking or enter xc,yc values")
 | 
				
			||||||
 | 
					  print("Final values: (in pixels) %.3f %.3f"%(xc,yc))
 | 
				
			||||||
 | 
					  return ai
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					
 | 
				
			||||||
### Objects ###
 | 
					### Objects ###
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue