Masking Pipeline


In [1]:
from chxanalys.chx_libs import np, roi, time, datetime, os,  getpass, db, get_images,LogNorm, plt,ManualMask

from chxanalys.chx_generic_functions import (get_detector, get_fields, get_sid_filenames,load_data, 
                                             RemoveHot, show_img, get_avg_img, reverse_updown)
 
%matplotlib notebook


/opt/conda_envs/analysis/lib/python3.5/site-packages/filestore/retrieve.py:15: UserWarning: Do not import filestore.retrieve, import filestore.api instead
  warnings.warn("Do not import filestore.retrieve, "
/opt/conda_envs/analysis/lib/python3.5/site-packages/IPython/html.py:14: ShimWarning: The `IPython.html` package has been deprecated. You should import from `notebook` instead. `IPython.html.widgets` has moved to `ipywidgets`.
  "`IPython.html.widgets` has moved to `ipywidgets`.", ShimWarning)

Path for Saving Results


In [2]:
path = '/XF11ID/analysis/2016_3/masks/'
print ("The analysis results will be saved in : %s"%path)


The analysis results will be saved in : /XF11ID/analysis/2016_3/masks/

Get the image series and metadata from the uid


In [3]:
uid ='f8545c' #count : 1 ['f8545c'] (scan num: 5374) (Measurement: )
uid ='b83ce8'

In [4]:
detector = get_detector( db[uid ] )
print ('Detector is:  %s'%detector  )
sud = get_sid_filenames(db[uid])
print ('scan_id, full-uid, data path are:  %s--%s--%s'%(sud[0], sud[1], sud[2][0] ))


Detector is:  eiger4m_single_image
scan_id, full-uid, data path are:  6276--b83ce866-1783-46ae-9291-a38572093917--/XF11ID/data/2016/11/12/a76b8702-fae9-4b5b-a632_1491

In [5]:
#imgs = load_data( uid, detector, reverse= True  )
imgs = load_data( uid, detector, reverse= False  )
md = imgs.md


hdf5 path = /XF11ID/data/2016/11/12/a76b8702-fae9-4b5b-a632_1491_master.h5

In [6]:
imgs


Out[6]:
<Frames>
Length: 1 frames
Frame Shape: 2167 x 2070
Pixel Datatype: uint32

In [7]:
Nimg=len(imgs)

In [8]:
md


Out[8]:
{'beam_center_x': 1476.0,
 'beam_center_y': 418.0,
 'count_time': 0.99998999,
 'detector_distance': 4.8899999,
 'frame_time': 1.0,
 'incident_wavelength': 1.2848103,
 'pixel_mask': array([[0, 0, 0, ..., 0, 0, 4],
        [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]], dtype=uint32),
 'x_pixel_size': 7.5000004e-05,
 'y_pixel_size': 7.5000004e-05}

In [9]:
#%matplotlib inline

In [ ]:


In [10]:
pixel_mask =  1- np.int_( np.array( md['pixel_mask'], dtype= bool)  )

In [11]:
#%run /XF11ID/analysis/Analysis_Pipelines/Develop/chxanalys/chxanalys/Create_Report.py
#%run /XF11ID/analysis/Analysis_Pipelines/Develop/chxanalys/chxanalys/chx_generic_functions.py

In [ ]:

show image and the pixel mask


In [12]:
show_img(  imgs[0] , vmin=.0001, vmax=1000, logs=True, image_name ='uid=%s'%uid )



In [ ]:


In [13]:
show_img(pixel_mask, vmin=0, vmax=1, image_name ='pixel_mask--uid=%s'%uid )


Remove hotspots in the image


In [14]:
imgs.shape


Out[14]:
(2167, 2070)

In [15]:
avg_img =  get_avg_img( imgs, sampling = 1000, plot_ = True, uid =uid)



In [16]:
#show_img(  avg_img*md['pixel_mask'],   vmin= .01, vmax= 100,
#         logs= True, aspect=1.,image_name ='img*pixel_mask--uid=%s'%uid )

In [17]:
#show_img(  avg_img*md['pixel_mask'],   vmin= 1, vmax= 1000,  aspect=1.,
#         
#         logs= True, image_name ='img*pixel_mask--uid=%s'%uid )

In [18]:
#show_img(  avg_img*md['pixel_mask'],   vmin= 1, vmax= 1000, xlim=[1000, 2070], ylim=[1000, 1500], aspect=1.,
#         
#         logs= True, image_name ='img*pixel_mask--uid=%s'%uid )

In [19]:
avg_img.shape


Out[19]:
(2167, 2070)

In [20]:
2167-784


Out[20]:
1383

In [21]:
mask_rh  = RemoveHot( avg_img, 5E4, plot_=True)


Manually create the mask file


In [22]:
fig, ax = plt.subplots()
m = ManualMask(ax, avg_img* md['pixel_mask']*mask_rh,
               cmap='viridis',origin='lower',
               vmin=.01, vmax=1.50) 
plt.show()



In [ ]:


In [23]:
new_mask = m.mask
manu_mask  = new_mask.copy()
fig, ax = plt.subplots()
im=ax.imshow(new_mask,origin='lower' ,vmin=0, vmax=1,cmap='viridis')
fig.colorbar(im)
plt.show()



In [28]:
manu_mask  = new_mask.copy()

Create a polygon mask


In [29]:
from skimage.draw import line_aa, line, polygon, circle

To create multi-rectangle masks, for each sub-mask,

  • Make x= the coordinate-x of the four cornners (pay attention to the reserve image-xy to python-xy )
  • Make y= the coordinate-y of the four cornners

In [30]:
def create_cross_mask(  image, center, wy_left=4, wy_right=4, wx_up=4, wx_down=4,
                     center_circle = True, center_radius=10
                     ):
    '''
    Give image and the beam center to create a cross-shaped mask
    wy_left: the width of left h-line
    wy_right: the width of rigth h-line
    wx_up: the width of up v-line
    wx_down: the width of down v-line
    center_circle: if True, create a circle with center and center_radius
    
    Return:
    the cross mask
    '''
    imy, imx = image.shape   
    cx,cy = center
    bst_mask = np.zeros_like( image , dtype = bool)   
    ###
    #for right part    
    wy = wy_right
    x = np.array( [ cx, imx, imx, cx  ])  
    y = np.array( [ cy-wy, cy-wy, cy + wy, cy + wy])
    rr, cc = polygon( y,x)
    bst_mask[rr,cc] =1
    
    ###
    #for left part    
    wy = wy_left
    x = np.array( [0,  cx, cx,0  ])  
    y = np.array( [ cy-wy, cy-wy, cy + wy, cy + wy])
    rr, cc = polygon( y,x)
    bst_mask[rr,cc] =1    
    
    ###
    #for up part    
    wx = wx_up
    x = np.array( [ cx-wx, cx + wx, cx+wx, cx-wx  ])  
    y = np.array( [ cy, cy, imy, imy])
    rr, cc = polygon( y,x)
    bst_mask[rr,cc] =1    
    
    ###
    #for low part    
    wx = wx_down
    x = np.array( [ cx-wx, cx + wx, cx+wx, cx-wx  ])  
    y = np.array( [ 0,0, cy, cy])
    rr, cc = polygon( y,x)
    bst_mask[rr,cc] =1   
    
    rr, cc = circle( cy, cx, center_radius)
    bst_mask[rr,cc] =1   
    
    
    full_mask= ~bst_mask
    
    return full_mask

In [31]:
md


Out[31]:
{'beam_center_x': 1476.0,
 'beam_center_y': 418.0,
 'count_time': 0.99998999,
 'detector_distance': 4.8899999,
 'frame_time': 1.0,
 'incident_wavelength': 1.2848103,
 'pixel_mask': array([[0, 0, 0, ..., 0, 0, 4],
        [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]], dtype=uint32),
 'x_pixel_size': 7.5000004e-05,
 'y_pixel_size': 7.5000004e-05}

In [32]:
show_img(  imgs[0] , vmin=.0001, vmax=1000, logs=True, image_name ='uid=%s'%uid )



In [33]:
avg_img.shape


Out[33]:
(2167, 2070)

In [34]:
2167-786


Out[34]:
1381

In [35]:
md['beam_center_x'],2167-md['beam_center_y']


Out[35]:
(1476.0, 1749.0)

In [36]:
full_mask1 = create_cross_mask(  avg_img, center=[ 1476, 1749],
                            wy_left=0, wy_right= 0, 
                              wx_up= 0, wx_down= 0,center_radius= 30 )

In [59]:
full_mask2 = create_cross_mask(  avg_img, center=[ 1476, 1630],
                            wy_left=0, wy_right= 0, 
                              wx_up= 20, wx_down= 40,center_radius= 0 )

In [79]:
slit_scatter_mask = create_cross_mask(  avg_img, center=[ 1476, 1728],
                            wy_left=10, wy_right= 10, 
                              wx_up= 0, wx_down= 0,center_radius= 0 )

In [80]:
full_mask = full_mask1 * full_mask2 * slit_scatter_mask

In [81]:
show_img( full_mask  )



In [ ]:


In [82]:
if False:#center of beam stop
    bst_mask = np.zeros_like( avg_img , dtype = bool)

    beam_center = [1381, 1471]

    ##h
    cx= 1750
    cy = 1380
    wx= 2070- cx
    wy = 20

    x = np.array( [ cx-wx, cx+wx, cx+wx, cx - wx,  ])  
    y = np.array( [ cy-wy, cy-wy, cy + wy, cy + wy])
    rr, cc = polygon( y,x)
    bst_mask[rr,cc] =1


    ##h
    cx= 1750
    cy = 1380
    wx= 2070- cx
    wy = 20

    x = np.array( [ cx-wx, cx+wx, cx+wx, cx - wx,  ])  
    y = np.array( [ cy-wy, cy-wy, cy + wy, cy + wy])
    rr, cc = polygon( y,x)
    bst_mask[rr,cc] =1





    #c=1381
    #w=5
    #bst_mask2 = np.zeros_like( avg_img , dtype = bool)
    #x = np.array( [ 0,    1340,   1340,     0])
    #y = np.array( [ c-w, c-w,  c+w,    c+w])
    #rr, cc = polygon( y,x)
    #bst_mask2[rr,cc] =1



    full_mask= ~bst_mask #* ~bst_mask2 *~bst_mask3 *~bst_mask4

In [83]:
#show_img(full_mask)

In [84]:
mask = np.array ( full_mask * pixel_mask*mask_rh  , dtype = bool )

In [85]:
fig, ax = plt.subplots()
#new_mask = 
im=ax.imshow(   (~mask) * avg_img,origin='lower' , 
      norm= LogNorm( vmin=0.1, vmax= 1e2 ), cmap='viridis')
#im = ax.imshow(avg_img,  cmap='viridis',origin='lower', norm= LogNorm( vmin=0.001, vmax=100 ) )
plt.show()



In [86]:
fig, ax = plt.subplots()
#new_mask = 
#im=ax.imshow(   (Mask) * avg_img,origin='lower' , 
#      norm= LogNorm( vmin=0.001, vmax=30 ), cmap='viridis')
#im = ax.imshow((mask)*avg_img,  cmap='viridis',origin='lower', norm= LogNorm( vmin=0.001, vmax=100 ) )
im = ax.imshow((mask)*avg_img,  cmap='viridis',origin='lower', norm= LogNorm( vmin=1, vmax=1000 ) )
plt.show()


Combine the hand-drawn/polygon mask and the pixel mask and hot pixel mask


In [87]:
#mask = np.array ( ~new_mask* ~plgon_mask * md['pixel_mask']*mask_rh, dtype = bool )

fig, ax = plt.subplots()
im=ax.imshow(mask, origin='lower' ,vmin=0, vmax=1,cmap='viridis')
fig.colorbar(im)
plt.show()


Save the combined mask to use in further data analysis


In [88]:
np.save(  path +   uid +"_GT_mask", mask)

In [89]:
path +   uid +"_GT_mask"


Out[89]:
'/XF11ID/analysis/2016_3/masks/b83ce8_GT_mask'

In [87]:
#np.save(  path +   'Nov10_4M' +"_mask", mask)

In [14]:
#np.save(  path +   'Nov12_4M' +"pixel_mask", pixel_mask)

In [147]:
#path +   'Octo28_4M' +"_mask"

In [40]:
#np.save(  path +   'Octo21_1M_pixel' +"_mask", md['pixel_mask'])

In [88]:
uid


Out[88]:
'f8545c'

In [ ]: