XPCS Pipeline

"This notebook corresponds to version {{ version }} of the pipeline tool: https://github.com/NSLS-II/pipelines"

NSLS2 data retrieval imports


In [1]:
from databroker import DataBroker as db, get_images, get_table, get_events
from filestore.api import register_handler, deregister_handler
from filestore.retrieve import _h_registry, _HANDLER_CACHE

In [ ]:
hdr = db[{{ uid }}]

In [2]:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import time
from ipywidgets import interact

In [6]:
cd /home/yuzhang/chx-pipelines/Develops/


/home/yuzhang/chx-pipelines/Develops

In [4]:
cd /XF11ID/analysis/Analysis_Pipelines/Develop/


/XF11ID/analysis/Analysis_Pipelines/Develop

In [7]:
%run develop.py
%run two_time.py

In [8]:
%matplotlib notebook
#%matplotlib inline

Users put

  • uid for Bluesky Scan
  • filename for acquiring data directly by EigerSofteare

In [9]:
BlueScan = True
DirectAcq = False
detector = 'eiger_4M_cam_img_image_lightfield'  #for 4M

Users put uid here


In [10]:
if BlueScan:
    uid = '54614d43'
    #uid = '95782687'
    uid = '95782687'
    uid= 'ff9f20c0'
    uid='71720966'
    uid='1663d34a'
    uid = 'f505e052-3baa-47d4-bdc4-61c2eb1bcc7a'  #sid= 551, 1%PEG, 
    uid='ee6975a1-9161'   #1% wt PEG
    
else:
    uid = '/XF11ID/data/2015/11/23/d01ab510-3cf3-4719-bee3_795_master.h5'

Get data


In [13]:
if BlueScan:
    hdr = db[uid]
    ev, = get_events(  hdr, [detector] )
    imgs = ev['data'][detector]
else:    
    imgs =  Images(uid)
print (imgs)
Nimg=len(imgs)


hdf5 path = /XF11ID/data/2015/11/23/5f7b93e2-71ef-41b0-9c2c_138_master.h5
<Frames>
Length: 10000 frames
Frame Shape: 2167 x 2070
Pixel Datatype: uint16

Get data path


In [14]:
if BlueScan:
    from datetime import datetime
    dt = datetime.fromtimestamp(hdr['start'].time)
    path ='/XF11ID/analysis' + '/%s/%s/%s/' % (dt.year, dt.month, dt.day)
else:
    path ='/XF11ID/analysis/2015/11/23/' 
path


Out[14]:
'/XF11ID/analysis/2015/11/23/'

Note: experiment information

  • The physical size of the pixels
  • Wavelegth of the X-rays - (units in Angstroms)
  • Detector to sample distance
  • Exposure time - (units in seconds)
  • acqusition period - (units in seconds)
  • dead time - (units in seconds)
  • time per frame = (exposure time + dead_time or acqusition period) - (units in seconds)

In [15]:
imgs.md


Out[15]:
{'beam_center_x': 852.0,
 'beam_center_y': 1830.0,
 'count_time': 0.0049899998,
 'detector_distance': 4.8400002,
 'frame_time': 0.0049999999,
 'framerate': 200.00000447034847,
 'incident_wavelength': 1.3776,
 'pixel_mask': array([[1, 1, 1, ..., 1, 1, 0],
        [1, 1, 1, ..., 1, 1, 1],
        [1, 1, 1, ..., 1, 1, 1],
        ..., 
        [1, 1, 1, ..., 1, 1, 1],
        [1, 1, 1, ..., 1, 1, 1],
        [1, 1, 1, ..., 1, 1, 1]], dtype=uint32),
 'x_pixel_size': 7.5000004e-05,
 'y_pixel_size': 7.5000004e-05}

In [16]:
# The physical size of the pixels
dpix = imgs.md['x_pixel_size'] * 1000.  
lambda_ = imgs.md['incident_wavelength']    # wavelegth of the X-rays in Angstroms
Ldet = 4812.        # detector to sample distance (mm)

exposuretime= imgs.md['count_time']
acquisition_period = imgs.md['frame_time']

# deadtime= 0   # 60e-6 
# timeperframe = exposuretime + deadtime
timeperframe = acquisition_period  

timeperframe, exposuretime


Out[16]:
(0.0049999999, 0.0049899998)

load a mask

load the mask saved in the mask_pipeline


In [17]:
mask = np.load( path +  str(uid)+ "_mask.npy")

Reverse the mask in y-direction due to the coordination difference between python and Eiger software


In [18]:
maskr = mask[::-1,:]

Plot the mask


In [19]:
fig, ax = plt.subplots()
im=ax.imshow(maskr, origin='lower' ,vmin=0,vmax=1,cmap='viridis')
fig.colorbar(im)
plt.show()


Interactive way to browse through images.

Note : Provide the number of images that you want to browse


In [20]:
def view_image(i):    
    fig, ax = plt.subplots()
    ax.imshow(imgs[i]*mask, interpolation='nearest', cmap='viridis',
                  origin='lower', norm= LogNorm(vmin=0.001, vmax=1e1) )
    ax.set_title("Browse the Image Stack")
    plt.show()

In [21]:
#interact(view_image, i=(0, Nimg-1))

Movie


In [22]:
def view_image(sleeps=1, ims=0, ime = 1):    
    fig, ax = plt.subplots()  
    for i in range( ims, ime  ):
        im=ax.imshow(imgs[i]*mask,  interpolation='nearest', cmap='viridis',
                  origin='lower', norm= LogNorm( vmin=0.01, vmax=10 ) )
        ax.set_title("images_%s"%i)
        
        time.sleep( sleeps )
        plt.draw()
    #fig.colorbar(im)
        
#view_image(.2, 0, 2)

hey, let's see if any images are bad!

load the image intensity (kymograph) saved in the mask_pipeline


In [23]:
kymo_sum = np.load( path +  str(uid)+"_kymo_sum.npy" )

In [24]:
bad_frames = np.where( kymo_sum > 1e5)[0]
bad_frames


Out[24]:
array([7629])

In [25]:
fig, axes = plt.subplots(  )
axes.plot( kymo_sum, '-go'  ) 
ax.set_ylabel('Intensity')
ax.set_xlabel('Frame')
ax.set_title('Kymograph_sum') 
plt.show()


Get the Averaged Image Data

load the average intensity saved in the mask_pipeline


In [26]:
avg_img = np.load( path + str(uid)+"_avg_img.npy" )
avg_imgm =  avg_img * mask

Reverse the image in y-direction due to the coordination difference between python and Eiger software


In [27]:
avg_imgr  = avg_img[::-1,:] 
avg_imgmr  = avg_imgm[::-1,:]

Plot the averged image with the mask


In [28]:
fig, ax = plt.subplots()
im = ax.imshow(avg_imgmr, cmap='viridis',origin='lower',
               norm= LogNorm(vmin=0.001, vmax=1e1))
ax.set_title("Masked Averaged Image_Reversed")
fig.colorbar(im)
plt.show()


Import all the required packages for Data Analysis

Get the approximate center and see the statistic to make sure


In [29]:
imgs.md['beam_center_x'], imgs.md['beam_center_y']


Out[29]:
(852.0, 1830.0)

In [30]:
#center = (imgs.md['beam_center_x'], imgs.md['beam_center_y'])
center = [ 2167 - 336, 849]  #for not reversed
center = [ 336, 849]  #for reversed
center = [ 2167- 1830, 846]
 
center


Out[30]:
[337, 846]

check the center with a ring

to be done-->> a better beam center finding algorithm


In [31]:
fig, ax = plt.subplots()
im = ax.imshow(avg_imgr, cmap='viridis',origin='lower', norm= LogNorm(vmin=0.001, vmax=1e1))

radius = 54
circle=plt.Circle( [center[1], center[0]], radius, color='b', alpha=1.0, lw=2, edgecolor='r',fill=False)
plt.gcf().gca().add_artist(circle)


ax.set_title("Masked Averaged Image_Reversed")
fig.colorbar(im)


rwidth = 100 
x1,x2 = [center[1] - rwidth, center[1] + rwidth]
y1,y2 = [center[0] - rwidth, center[0] + rwidth]
ax.set_xlim( [x1,x2])
ax.set_ylim( [y1,y2])

plt.show()


/home/yuzhang/.conda/envs/user_analysis/lib/python3.4/site-packages/matplotlib/patches.py:107: UserWarning: Setting the 'color' property will overridethe edgecolor or facecolor properties. 
  warnings.warn("Setting the 'color' property will override"

Circular Average : compute the radial integartion from the center of the speckle pattern


In [32]:
bin_centers, ring_averages= circular_average(avg_imgr,  center, pixel_size=(dpix, dpix), mask= maskr)

#  convert to q (reciprocal space)
two_theta = utils.radius_to_twotheta(Ldet, bin_centers)
q_val = utils.twotheta_to_q(two_theta, lambda_)

In [33]:
fig,axes = plt.subplots(figsize=(8, 6))
axes.semilogy(q_val, ring_averages, '-o')
axes.set_title('Circular Average')
axes.set_ylabel('Ring Avearge')
axes.set_xlabel('Q ('r'$\AA^{-1}$ )')
axes.set_xlim(0.001, 0.02)
axes.set_ylim(0.001, 10.0)
plt.show()



In [34]:
fig,axes = plt.subplots(figsize=(8, 6))
axes.semilogy(bin_centers/dpix, ring_averages, '-o')
axes.set_title('Circular Average')
axes.set_ylabel('Ring Avearge')
axes.set_xlabel('Bin Centers, (pixel)')
axes.set_xlim(30,  250)
axes.set_ylim(0.001, 10.0)
plt.show()


Create label array (Q rings)


In [35]:
inner_radius = 58 # radius of the first ring
width = 2       # width of each ring
spacing =  (166 - 58)/9 - 2    # spacing between rings
num_rings = 6   # number of rings

#  find the edges of the required rings
edges = roi.ring_edges(inner_radius, width, spacing, num_rings)
edges


Out[35]:
array([[  58.,   60.],
       [  70.,   72.],
       [  82.,   84.],
       [  94.,   96.],
       [ 106.,  108.],
       [ 118.,  120.]])

In [36]:
two_theta = utils.radius_to_twotheta(Ldet, edges*dpix)
q_ring_val = utils.twotheta_to_q(two_theta, lambda_)

q_ring_center = np.average(q_ring_val, axis=1)
q_ring_center


Out[36]:
array([ 0.00419415,  0.0050472 ,  0.00590025,  0.00675329,  0.00760634,
        0.00845939])

In [37]:
rings = roi.rings(edges, center, avg_imgmr.shape)

ring_mask = rings*maskr

Extract the labeled array


In [38]:
ring_mask


Out[38]:
array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ..., 
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]])

In [39]:
qind, pixelist = roi.extract_label_indices(   ring_mask  )
noqs = len( np.unique(qind) )
nopr = np.bincount(qind, minlength=(noqs+1))[1:]

In [40]:
qind


Out[40]:
array([6, 6, 6, ..., 6, 6, 6])

In [41]:
nopr


Out[41]:
array([ 128,  312,  459,  583,  848, 1068])

Number of pixels in each q ring

check center


In [42]:
pixel = roi.roi_pixel_values(avg_imgmr, ring_mask, [2] )
fig,ax=plt.subplots()
ax.plot( pixel[0][0] ,'bo', ls='-' )


Out[42]:
[<matplotlib.lines.Line2D at 0x7fedba99dbe0>]

In [43]:
fig, axes = plt.subplots( figsize=(8, 6))
#axes.semilogy(q_val, ring_averages, '-o')
axes.plot(q_val, ring_averages, '-o')
axes.set_title('Circular Average with the Q ring values')
axes.set_ylabel('Ring Avearge')
axes.set_xlabel('Bin Centers 'r'$\AA^{-1}$')
axes.set_xlim(0.00, 0.02)
axes.set_ylim(0, 6)
for i in range(num_rings):
    axes.axvline(q_ring_center[i])
plt.show()



In [44]:
#plt.close('all')

In [47]:
# plot the figure
fig, axes = plt.subplots(figsize=(8,8))
axes.set_title("Labeled Array on Averaged Data")
im,im_label = show_label_array_on_image(axes, avg_imgmr, ring_mask, imshow_cmap='viridis',
                        cmap='Paired',
                         vmin=0.01, vmax=5,  origin="lower")
#rwidth = 200 
#x1,x2 = [center[1] - rwidth, center[1] + rwidth]
#y1,y2 = [center[0] - rwidth, center[0] + rwidth]
#axes.set_xlim( [x1,x2])
#axes.set_ylim( [y1,y2])

#fig.colorbar(im)
rwidth = 200 

x1,x2 = [center[1] - rwidth, center[1] + rwidth]
y1,y2 = [center[0] - rwidth, center[0] + rwidth]
axes.set_xlim( [x1,x2])
axes.set_ylim( [y1,y2])
fig.colorbar(im_label)
plt.show()


Kymograph(waterfall plot) of the max-intensity ring


In [46]:
imgs_ =imgs
imgsr = Reverse_Coordinate(imgs_, mask)

users put the number of ring with max intensity here


In [48]:
max_inten_ring =2

In [49]:
#kymo = roi.kymograph(imgsr, ring_mask, num = max_inten_ring)

In [50]:
t0 = time.time()
data_pixel =   Get_Pixel_Array( imgsr, pixelist).get_data()
run_time(t0)


Total time: 4.19 min

In [51]:
pixelist_qi =  np.where( qind == max_inten_ring)[0]         
data_pixel_qi = data_pixel[:,pixelist_qi]

In [52]:
fig, ax = plt.subplots(figsize=(8,6))
ax.set_ylabel('Pixel')
ax.set_xlabel('Frame')
ax.set_title('Kymograph')

im = ax.imshow(data_pixel_qi.T, cmap='viridis', vmax=5.0)
fig.colorbar( im   )
ax.set_aspect(30.)
plt.show()


Time~Mean intensities for each ring


In [53]:
mean_inten = get_mean_intensity( data_pixel, qind)

In [54]:
times = np.arange(  mean_inten[1].shape[0]   )  #*timeperframe  # get the time for each frame

fig, ax = plt.subplots(figsize=(8, 6))
ax.set_title("Mean intensity of each ring")
for i in range(num_rings):
    ax.plot(times, mean_inten[i+1], '--o', label="Ring "+str(i+1))
    ax.set_xlabel("Frame")
    ax.set_ylabel("Mean Intensity")
ax.legend(loc='best') 
plt.show()


Mean intensities for each ring


In [55]:
K_mean = np.array( [mean_inten[i].mean() for i in  list(mean_inten.keys() )] )

In [56]:
K_mean


Out[56]:
array([ 3.36094063,  2.10911154,  0.65901917,  0.49677616,  0.3595105 ,
        0.18890328])

XSVS analysis

Get max count number


In [57]:
max_cts = data_pixel.max()
max_cts


Out[57]:
26.0

In [62]:
%run speckle.py

Users provide frame info (start-frame and end-frame) for analysis


In [58]:
good_start = 0
good_end =  10# if using Nimg, then calculate all the images

good_end =    Nimg  #hen calculate all the images
imgs_ =imgs[good_start: good_end]
imgsr = Reverse_Coordinate(imgs_, mask)

In [93]:
len(imgs_)


Out[93]:
10000

In [104]:
time_steps = utils.geometric_series(2,   len(imgs_)  )
time_steps


Out[104]:
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192]

In [103]:
#time_steps = utils.geometric_series(2,   600  )
#time_steps

In [107]:
num_times = len(time_steps)

In [94]:
spe_cts_all, std_dev = xsvs( (imgsr,), np.int_(ring_mask), timebin_num=2,
        number_of_img= len(imgs_), max_cts=int(max_cts+2), bad_images=None, threshold = 5000 )


##########Total time: 4.84 min

This will provide the normalized bin edges for each integration time


In [ ]:
#Knorm_bin_edges, Knorm_bin_centers = speckle.normalize_bin_edges( len(time_steps), num_rings, K_mean, int(max_cts+2))

In [95]:
bin_edges, bin_centers, Knorm_bin_edges, Knorm_bin_centers = get_bin_edges(
      len(time_steps), num_rings, K_mean, int(max_cts+2)  )

Plot the results for each q ring


In [96]:
sx = int(round(np.sqrt(num_rings)) )
if num_rings%sx == 0: 
    sy = int(num_rings/sx)
else:
    sy=int(num_rings/sx+1)
fig = plt.figure(figsize=(10,6))
plt.title('uid= %s'%uid,fontsize=20, y =1.02)  
plt.axes(frameon=False)
plt.xticks([])
plt.yticks([])
for i in range(num_rings):
    for j in range(len(time_steps)):
        axes = fig.add_subplot(sx, sy, i+1 )
        axes.set_xlabel("K/<K>")
        axes.set_ylabel("P(K)")
        art, = axes.plot(Knorm_bin_edges[j, i][:-1], spe_cts_all[j, i], '-o',
                     label=str(time_steps[j])+" ms")
        axes.set_xlim(0, 2.5)
        axes.set_title("Q "+ '%.4f  '%(q_ring_center[i])+ r'$\AA^{-1}$')
        axes.legend(loc='best', fontsize = 6)
plt.show()
fig.tight_layout()


Plot the results with error bars


In [66]:
if False:
    
    sx = int(round(np.sqrt(num_rings)) )
    if num_rings%sx == 0: 
        sy = int(num_rings/sx)
    else:
        sy=int(num_rings/sx+1)
    fig = plt.figure(figsize=(10, 6))
    plt.title('uid= %s'%uid,fontsize=20, y =1.02)  
    plt.axes(frameon=False)
    plt.xticks([])
    plt.yticks([])
    for i in range(num_rings):
        for j in range(len(time_steps)):
            axes = fig.add_subplot(sx, sy, i+1 )
            axes.set_xlabel("K/<K>")
            axes.set_ylabel("P(K)")
            art = axes.errorbar(Knorm_bin_edges[j, i][:-1], spe_cts_all[j, i],
                         std_dev[j, i], fmt='-o',label=str(time_steps[j])+" ms")

            axes.set_xlim(0, 2.5)
            axes.set_title("Q "+ '%.3f  '%(q_ring_center[i])+ r'$\AA^{-1}$')
            axes.legend(loc='best', fontsize = 6)
    plt.show()
    fig.tight_layout()

Fit XSVS results

Negative Binomaial Distribution

$P(K) =(\frac{M}{<K>})^M \frac{K ^(M-1)}{\Gamma(M)}\exp(-M\frac{K}{<K>})$

Gamma Distribution

$P(K)=\frac{\Gamma(K+M)}{\Gamma(K+1)\Gamma(M)}(\frac{M}{M+<K>})^M(\frac{<K>}{M+<K>})^K$

Poission Distribution

$P(K) = \frac{<K>^K}{K!}\exp(-<K>)$

Fitting with Negative Binomial and Gamma and Poisson Distribution


In [67]:
from lmfit import  Model
from scipy.interpolate import UnivariateSpline

g_mod = Model(gamma_dist, indepdent_vars=['K'])
#g_mod = Model( gamma_dist )
n_mod = Model(nbinom_dist)
p_mod = Model(poisson_dist)
dc_mod = Model(diff_mot_con_factor)

fit with Negative Binomial Function


In [110]:
M_val = {}
K_val = {}
sx = int(round(np.sqrt(num_rings)))
if num_rings%sx == 0: 
    sy = int(num_rings/sx)
else:
    sy = int(num_rings/sx+1)
fig = plt.figure(figsize=(10, 6))
plt.title('uid= %s'%uid+" Fitting with Negative Binomial Function", fontsize=20, y=1.02)  
plt.axes(frameon=False)
plt.xticks([])
plt.yticks([])
for i in range(num_rings):
    M_val[i]=[]
    K_val[i]=[]
    for j in range(   num_times  ):
        # find the best values for K and M from fitting
        result_n = n_mod.fit(spe_cts_all[j, i],
                             bin_values=bin_edges[j, i][:-1],
                             K=5, M=12)
        M_val[i].append(result_n.best_values['M'])
        K_val[i].append(result_n.best_values['K'])
        axes = fig.add_subplot(sx, sy, i+1 )
        axes.set_xlabel("K/<K>")
        axes.set_ylabel("P(K)")
        
        #  Using the best K and M values interpolate and get more values for fitting curve
        fitx_ = np.linspace(0, max(Knorm_bin_edges[j, i][:-1]), 100*2**j   )   
        
        fitx = np.linspace(0, max(bin_edges[j, i][:-1]), 100*2**j   )         
        fity = nbinom_dist( fitx, K_val[i][j], M_val[i][j] ) # M and K are fitted best values

        
        if j == 0:
            art, = axes.plot( fitx_,fity, '-b',  label="nbinom")
        else:
            art, = axes.plot( fitx_,fity, '-b')
            
        art, = axes.plot(Knorm_bin_edges[j, i][:-1], spe_cts_all[j, i], 'o',
                     label=str(time_steps[j])+" ms")
        
        axes.set_xlim(0, 3.5)
        # Annotate the best K and M values on the plot
        axes.annotate(r'K='+'%.3f'%( K_val[i][0]) +','+r'M='+'%.3f'%(M_val[i][0]),
                      xy=(1, 0.25),
                      xycoords='axes fraction', fontsize=16,
                      horizontalalignment='right', verticalalignment='bottom')
        axes.set_title("Q "+ '%.4f  '%(q_ring_center[i])+ r'$\AA^{-1}$')
        axes.legend(loc='best', fontsize = 6)
plt.show()
fig.tight_layout()



In [98]:
[K_val[i][0] for i in range(num_rings)]


Out[98]:
[3.3165319464701533,
 2.0941134953236222,
 0.65487867458291715,
 0.49482226403256657,
 0.35853928147592601,
 0.18873607742325987]

In [99]:
K_mean


Out[99]:
array([ 3.36094063,  2.10911154,  0.65901917,  0.49677616,  0.3595105 ,
        0.18890328])

Get the contast factor of detected signals is 1/M

$ \beta = 1/M $


In [111]:
contrast_factor = np.zeros((num_rings, num_times))

for i in range(num_rings):
    for j in range(num_times):
        contrast_factor[i, j] =  1/M_val[i][j]
#contrast_factor

In [113]:
contrast_factor[0, :]


Out[113]:
array([ 0.20145751,  0.19958295,  0.1986697 ,  0.19844629,  0.19822649,
        0.1964244 ,  0.19127847,  0.17949491,  0.16201437,  0.13767692,
        0.12513249,  0.11121047,  0.08732275,  0.02834312])

In [133]:
times = np.array( time_steps ) * exposuretime

In [144]:
fig, ax = plt.subplots(figsize=(6, 6))
for i in range(num_rings):
    ax.semilogx(times, contrast_factor[i, :], "-o",
                label="Q ="+ '%.4f  '%(q_ring_center[i])+ r'$\AA^{-1}$')
    ax.set_ylabel("Contrast Factor " r'($\beta $)')
ax.set_title("Contrast Factor for Each Q Rings")
ax.set_xlabel("Time (s)")
ax.legend(loc='best')
#ax.set_xticks([j for j in range(num_times)])
#ax.set_xticklabels(['%i ' % time_steps[j] for j in range(num_times)])
plt.show()



In [ ]:


In [165]:
relax_rate = []

sx = int( round (np.sqrt(num_rings)) )
if num_rings%sx==0:
    sy = int(num_rings/sx)
else:
    sy = int(num_rings/sx+1)
    
#fig = plt.figure(figsize=(14, 10))
fig = plt.figure()
plt.title('uid= %s'%uid + "Contrast Factor for Each Q Rings", fontsize=14, y =1.08)  
plt.axis('off')
for sn in range(num_rings):
    ax = fig.add_subplot(sx, sy, sn+1 )
    
    y= contrast_factor[sn, :]  
    result_dc = dc_mod.fit(y, times=time_steps,
                           relaxation_rate=1.0, contrast_factor=0.78, cf_baseline=0)
    relax_rate.append(result_dc.best_values['relaxation_rate'])
 
    #ax.plot(contrast_factor[i, :], "o", label="Q ="+ '%.4f  '%(q_ring_center[i])+ r'$\AA^{-1}$')
    ax.semilogx(times, y, "o", 
                label="Q ="+ '%.4f  '%(q_ring_center[i])+ r'$\AA^{-1}$')
    ax.semilogx(times, result_dc.best_fit, '-r')
 
    ax.set_title(" Q= " + '%.5f  '%(q_ring_center[sn]) + r'$\AA^{-1}$')  
    ax.set_ylim([min(y)*.95, max(y[1:]) *1.05])
    txts = r'$\gamma$' + r'$ = %.5f$'%(relax_rate[sn]) +  r'$ s^{-1}$'
    ax.text(x =0.015, y=.55, s=txts, fontsize=14, transform=ax.transAxes)              
 
fig.tight_layout()



In [157]:
relax_rate = []

fig, ax = plt.subplots(figsize=(8, 8))
ax.set_title('uid= %s'%uid + "Contrast Factor for Each Q Rings", fontsize=20, y =1.05) 

for i in range(num_rings):  
    result_dc = dc_mod.fit(contrast_factor[i, :], times=time_steps,
                           relaxation_rate=1.0, contrast_factor=0.78, cf_baseline=0)
    relax_rate.append(result_dc.best_values['relaxation_rate'])
 
    #ax.plot(contrast_factor[i, :], "o", label="Q ="+ '%.4f  '%(q_ring_center[i])+ r'$\AA^{-1}$')
    ax.semilogx(times, contrast_factor[i, :], "o", 
                label="Q ="+ '%.4f  '%(q_ring_center[i])+ r'$\AA^{-1}$')
    ax.semilogx(times, result_dc.best_fit, '-r')

ax.set_ylabel("Contrast Factor " r'($\beta $)') 
ax.set_xlabel("Time (s)")
ax.legend(loc='best')
#ax.set_xticks([j for j in range(num_times)])
#ax.set_xticklabels(['%i ' % time_steps[j] for j in range(num_times)])
plt.show()



In [ ]:


In [ ]:


In [115]:
relax_rate


Out[115]:
[0.00085026623980752347,
 0.0019530121376543641,
 0.00037336340197273541,
 0.00073928013514846602,
 0.0018243361405930977,
 0.0013120986822549681]

In [116]:
fig, ax = plt.subplots(figsize=(8, 8))
ax.plot(q_ring_center**2, relax_rate, 'ro', ls='--')
ax.set_ylabel('Relaxation rate 'r'$\gamma$'"($s^{-1}$)")
ax.set_xlabel("$q^2$"r'($\AA^{-2}$)')
plt.show()



In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:
%run speckle.py

One time Correlation

Note : Enter the number of levels and number of buffers for Muliti tau one time correlation number of buffers has to be even. More details in https://github.com/scikit-xray/scikit-xray/blob/master/skxray/core/correlation.py


In [ ]:
nopr

In [ ]:
edges

In [ ]:
ring_mask = np.array( ring_mask, dtype=int)

In [ ]:
good_start = 0
good_end = Nimg   #all the images

#good_start = 7600
#good_end = 7650    

imgs_ =imgs[good_start: good_end-1]
imgsr = Reverse_Coordinate(imgs_, mask)

In [ ]:
g2_t1, lag_steps_t1 = autocor_one_time( num_buf,  ring_mask, imgsr, num_lev=None,
                                       bad_images=None, threshold= 65500 )

In [ ]:
g2, lag_steps = g2_t1, lag_steps_t1
lags = lag_steps*timeperframe

In [ ]:
tg2 = np.hstack( [ lags.reshape( len(lags),1), g2] )
#np.save( path + 'g2_%s-%s--%s'%(uid,good_start, good_end), tg2)
np.savetxt( path + 'g2_%s-%s--%s.txt'%(uid,good_start, good_end), tg2)

Plot the one time correlation functions


In [ ]:
sx = int(round(np.sqrt(num_rings)) )
if num_rings%sx == 0: 
    sy = int(num_rings/sx)
else:
    sy=int(num_rings/sx+1)
#fig = plt.figure(figsize=(14, 10))
fig = plt.figure()
plt.title('uid= %s'%uid,fontsize=20, y =1.08)  
plt.axis('off')
for sn in range(num_rings):
    ax = fig.add_subplot(sx,sy,sn+1 )
    ax.set_ylabel("g2") 
    ax.set_title(" Q= " + '%.5f  '%(q_ring_center[sn]) + r'$\AA^{-1}$')
    y=g2[:, sn]
    ax.semilogx(lags, y, '-o', markersize=6) 
    #ax.set_ylim([min(y)*.95, max(y[1:])*1.05 ])
    ax.set_ylim([    min(y) , max(y[1:]) ])
    ax.set_xlim([   min(lags)+0*1e-6, max(lags)])
plt.show()
fig.tight_layout()

Fit g2


In [ ]:
from lmfit import  Model
mod = Model(corr.auto_corr_scat_factor)

In [ ]:
rate = []

sx = int( round (np.sqrt(num_rings)) )
if num_rings%sx==0:
    sy = int(num_rings/sx)
else:
    sy = int(num_rings/sx+1)
    
#fig = plt.figure(figsize=(14, 10))
fig = plt.figure()
plt.title('uid= %s'%uid, fontsize=20, y =1.08)  
for sn in range(num_rings):
    ax = fig.add_subplot(sx, sy, sn+1 )
    y=g2[1:, sn]
    result1 = mod.fit(y, lags=lags[1:], beta=.1,
                      relaxation_rate =.5, baseline=1.0)
    rate.append(result1.best_values['relaxation_rate'])
    
    ax.semilogx(lags[1:], y, 'ro')
    ax.semilogx(lags[1:], result1.best_fit, '-b')
    ax.set_title(" Q= " + '%.5f  '%(q_ring_center[sn]) + r'$\AA^{-1}$')  
    ax.set_ylim([min(y)*.95, max(y[1:]) *1.05])
    txts = r'$\gamma$' + r'$ = %.3f$'%(rate[sn]) +  r'$ s^{-1}$'
    ax.text(x =0.015, y=.55, s=txts, fontsize=14, transform=ax.transAxes)              
 
fig.tight_layout()

Plot the relaxation rates vs (q_ring_center)**2


In [ ]:
fig, ax=plt.subplots()
ax.plot(q_ring_center**2, rate, 'ro', ls='--')
ax.set_ylabel('Relaxation rate 'r'$\gamma$'"($s^{-1}$)")
ax.set_xlabel("$q^2$"r'($\AA^{-2}$)')
plt.show()

Fitted the Diffusion Coefficinet D0


In [ ]:
D0 = np.polyfit(q_ring_center**2, rate, 1)
gmfit = np.poly1d(D0)
print ('The fitted diffusion coefficient D0 is:  %.2E   A^2S-1'%D0[0])

In [ ]:
fig,ax = plt.subplots()
ax.plot(q_ring_center**2, rate, 'ro', ls='')
ax.plot(q_ring_center**2,  gmfit(q_ring_center**2),  ls='-')
ax.set_ylabel('Relaxation rate 'r'$\gamma$'"($s^{-1}$)")
ax.set_xlabel("$q^2$"r'($\AA^{-2}$)')
plt.show()

Two_time Correlation


In [ ]:
good_start= 0
good_end = 10000
imgs_ =imgs[good_start: good_end]
imgsr = Reverse_Coordinate(imgs_, mask)

In [ ]:
g12b = auto_two_Array( imgsr, ring_mask, data_pixel = data_pixel )

In [ ]:
g12_num = 0  #0: the firs ring
data = g12b[:,:,g12_num]
fig, ax = plt.subplots()
im=ax.imshow( data, origin='lower' , cmap='viridis', 
             norm= LogNorm( vmin= 1, vmax= 1.6 ), 
        extent=[0, data.shape[0]*timeperframe, 0, data.shape[0]*timeperframe ] )
ax.set_title('0-%s frames--Qth= %s'%(Nimg,g12_num))
ax.set_xlabel( r'$t_1$ $(s)$', fontsize = 18)
ax.set_ylabel( r'$t_2$ $(s)$', fontsize = 18)
fig.colorbar(im)
plt.show()

find the bad frames and mask it in two-time correlation


In [ ]:
#np.where( g12b[:,:,0] == g12b[:,:,0].min() )

In [ ]:
g12b_mask = make_g12_mask(g12b,[7629])
g12bm = masked_g12(g12b,[7629])

get one time correlation


In [ ]:
g2b = get_one_time_from_two_time(g12bm, timeperframe= timeperframe)

In [ ]:
sx = int(round(np.sqrt(num_rings)) )
if num_rings%sx == 0: 
    sy = int(num_rings/sx)
else:
    sy=int(num_rings/sx+1)
#fig = plt.figure(figsize=(14, 10))
fig = plt.figure()
plt.title('uid= %s'%uid,fontsize=20, y =1.08)  
plt.axis('off')
for sn in range(num_rings):
    ax = fig.add_subplot(sx,sy,sn+1 )
    ax.set_ylabel("g2") 
    ax.set_title(" Q= " + '%.5f  '%(q_ring_center[sn]) + r'$\AA^{-1}$')     
    y=g2b[:, sn]
    ax.semilogx( np.arange(len(y))*timeperframe, y, '-o', markersize=6) 
    #ax.semilogx(lags, y, '-o', markersize=6) 
    #ax.set_ylim([min(y)*.95, max(y[1:])*1.05 ])
    ax.set_ylim( [1.0, max(y[1:])*1.05 ] )
    
plt.show()
fig.tight_layout()

plot g2 by multi-tau and g2 from two-time


In [ ]:
sx = int(round(np.sqrt(num_rings)) )
if num_rings%sx == 0: 
    sy = int(num_rings/sx)
else:
    sy=int(num_rings/sx+1)
#fig = plt.figure(figsize=(14, 10))
fig = plt.figure()
plt.title('uid= %s'%uid,fontsize=20, y =1.06)  
plt.axis('off')
for sn in range(num_rings):
    ax = fig.add_subplot(sx,sy,sn+1 )
    ax.set_ylabel("g2") 
    ax.set_title(" Q= " + '%.5f  '%(q_ring_center[sn]) + r'$\AA^{-1}$')     
    y=g2b[:, sn]
    ax.semilogx( np.arange(len(y))*timeperframe, y, '--r', markersize=6) 
    
    y2=g2[:, sn]
    ax.semilogx(lags, y2, 'o', markersize=6) 
    
    #ax.semilogx(lags, y, '-o', markersize=6) 
    #ax.set_ylim([min(y)*.95, max(y[1:])*1.05 ])
    ax.set_ylim( [1.0, max(y2[1:])*1.05 ] )
plt.show()
fig.tight_layout()

Another defination of two-time correlation (different normalization)


In [ ]:
g12b_norm, g12b_not_norm, norms = auto_two_Array_g1_norm( imgsr, ring_mask, data_pixel = data_pixel )

In [ ]:
g2b_norm = get_one_time_from_two_time(g12b_norm,
                             norms=None, nopr=nopr, timeperframe= timeperframe)

In [ ]:
g2b_not_norm = get_one_time_from_two_time(g12b_not_norm,
                             norms=norms, nopr=nopr, timeperframe= timeperframe)

In [ ]:
#num_rings =6


sx = int(round(np.sqrt(num_rings)) )
if num_rings%sx == 0: 
    sy = int(num_rings/sx)
else:
    sy=int(num_rings/sx+1)
#fig = plt.figure(figsize=(14, 10))
fig = plt.figure()
plt.title('uid= %s'%uid,fontsize=20, y =1.06)  
plt.axis('off')
for sn in range(num_rings):
    ax = fig.add_subplot(sx,sy,sn+1 )
    ax.set_ylabel("g2") 
    ax.set_title(" Q= " + '%.5f  '%(q_ring_center[sn]) + r'$\AA^{-1}$')    
    
    y=g2b_norm[:, sn]
    #ax.semilogx( np.arange(len(y))*timeperframe, y, '--r', markersize=3) 
    
    y3=g2b_not_norm[:, sn]
    ax.semilogx( np.arange(len(y))*timeperframe, y3, '-g', lw=4, markersize=3)     
    
    y2=g2[:, sn]
    #ax.semilogx(lags, y2, '-ko', markersize= 3)
    ax.semilogx(lags, y2, 'o', markersize=6) 
    
    #ax.semilogx(lags, y, '-o', markersize=6) 
    #ax.set_ylim([min(y)*.95, max(y[1:])*1.05 ])
    ax.set_ylim( [1.0, max(y2[1:])*1.05 ] )
plt.show()
fig.tight_layout()

get one time correlation @different age


In [ ]:
g12_num =0

g2b = show_g12_age_cuts( g12bm, g12_num=g12_num, slice_num =3, slice_width= 500, 
                slice_start=4000, slice_end= 20000-4000,
    timeperframe= timeperframe,vmin= 1.0, vmax =  1.22) #vmax=g12b[:,:,g12_num].max() )

get taus histgram


In [ ]:
taus = show_g12_tau_cuts(g12b_norm, g12_num=0,  slice_num = 5, slice_width= 1.0, 
    slice_start=3, slice_end= 5000-1,draw_scale_tau =1.0, vmin=1.01,vmax=1.55 )

In [ ]:
his = his_tau(taus, hisbin = 10, plot=True)

In [ ]: