Masking Pipeline

CHX Olog NoteBook

CHX Olog (https://logbook.nsls2.bnl.gov/11-ID/)


In [2]:
from chxanalys.chx_libs import (np, roi, time, datetime, os,  getpass, db, get_images,LogNorm, plt,ManualMask)
from chxanalys.chx_libs import cmap_albula, cmap_vge, random
from chxanalys.chx_generic_functions import (get_detector, get_meta_data,create_user_folder,
                                             get_fields,  get_sid_filenames,load_data,
                                             RemoveHot, show_img,get_avg_img, 
                                             reverse_updown,create_cross_mask,mask_badpixels )
from skimage.draw import line_aa, line, polygon, circle
 
%matplotlib notebook

Path for Saving Results


In [3]:
CYCLE= '2017_2'  #change clycle here

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


The analysis results will be saved in : /XF11ID/analysis/2017_2/masks/

Get the image series and metadata from the uid


In [8]:
uid = 'af84e5'# (scan num: 3218) (Measurement: scattering for mask )


#uid = 'ea5492' # (scan num: 3216) (Measurement: direct beam position )

uid = '76cab3' # (scan num: 3265) (Measurement: Single image )
uid = '3d08d0' # (scan num: 4010) (Measurement: for GISAXS mask )

In [9]:
md = get_meta_data( uid )
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:  4010--3d08d0c2-551b-4b57-8be7-4685c53a5939--/XF11ID/data/2017/07/15/5330577f-2032-4662-b728_12008

In [10]:
print(md['beam_center_y'], md['beam_center_x'])


1701000.0 1373.0

In [11]:
imgs = load_data( uid, detector, reverse= False  )
#imgs = load_data( uid, detector, reverse= True  )
md.update( imgs.md );Nimg = len(imgs);
#if 'number of images'  not in list(md.keys()):
md['number of images']  = Nimg
pixel_mask =  1- np.int_( np.array( imgs.md['pixel_mask'], dtype= bool)  )
print( 'The data are: %s' %imgs )


The data are: <Frames>
Length: 1 frames
Frame Shape: 2167 x 2070
Pixel Datatype: uint32

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

In [13]:
img_choice_N = 1  #can change this number to select more frames for average
img_samp_index = random.sample( range(len(imgs)), img_choice_N) 
avg_img =  get_avg_img( imgs, img_samp_index, plot_ = False, uid = uid)

show image and the pixel mask

  • show image

In [14]:
show_img(  avg_img*pixel_mask , vmin=.001, vmax=1e4, logs=True, 
         image_name ='uid=%s'%uid, aspect=1, cmap= cmap_albula  )


  • pixel mask

Update Pixel Mask due to bad pixel of Eiger Detector


In [15]:
pixel_mask = mask_badpixels( pixel_mask, md['detector'])

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


Remove hotspots in the image


In [17]:
#avg_img =  get_avg_img( imgs, sampling = 1000, plot_ = False, uid =uid)

In [19]:
mask_rh  = RemoveHot( avg_img, 2**20-1, plot_=True)



In [20]:
show_img(avg_img*pixel_mask,vmin=0.1,vmax=1e3, logs=True, 
         image_name= 'uid= %s with pixel mask'%uid , aspect=1, cmap= cmap_albula  )



In [ ]:

Create a polygon mask

  • check beam center

In [21]:
md['beam_center_x'], md['beam_center_y']


Out[21]:
(1373.0, 1701000.0)

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


In [25]:
path


Out[25]:
'/XF11ID/analysis/2017_2/masks/'

In [105]:
#creat the right part mask
partial_mask = create_cross_mask(  avg_img,center=[1377.0,1193.0],
                            wy_left= 0, wy_right= 0, 
                              wx_up= 20, wx_down= 0,center_radius= 0 )

In [106]:
show_img(  partial_mask   )



In [123]:
#np.save( '/XF11ID/analysis/2017_1/masks/Ver_Beamstop', partial_mask )
#Ver_Beamstop = np.load( '/XF11ID/analysis/2017_1/masks/Ver_Beamstop.npy' )
#Ver_Beamstop = move_beamstop( Vertical_Beamstop, xshift=0, yshift=0 )

In [107]:
#creat the left/right/up/down part mask
partial_mask *=  create_cross_mask(  avg_img, center=[ 1205,1838],
                            wy_left= 0, wy_right= 65, 
                              wx_up= 0, wx_down=0,center_radius= 0 )
#partial_mask2[1285:1350,1430:1440,] = False

In [108]:
#creat the left/right/up/down part mask
partial_mask *=  create_cross_mask(  avg_img, center=[ 1370,1925],
                            wy_left= 0, wy_right= 50, 
                              wx_up= 0, wx_down=0,center_radius= 0 )
#partial_mask2[1285:1350,1430:1440,] = False

In [109]:
#np.save( '/XF11ID/analysis/2017_1/masks/Hor_Beamstop', partial_mask )
#Hor_Beamstop = np.load( '/XF11ID/analysis/2017_1/masks/Hor_Beamstop.npy' )
#Hor_Beamstop = move_beamstop( Hor_Beamstop, xshift=0, yshift=0 )

In [110]:
show_img(  partial_mask   )



In [111]:
#creat the left/right/up/down part mask
#partial_mask *=  create_cross_mask(  avg_img, center=[ 1615,2000],
#                            wy_left= 0, wy_right= 100, 
#                              wx_up= 0, wx_down=0,center_radius= 0 )
#partial_mask2[1285:1350,1430:1440,] = False

In [112]:
#creat the left/right/up/down part mask
partial_mask *=  create_cross_mask(  avg_img, center=[ 1030,395],
                            wy_left= 145, wy_right= 0, 
                              wx_up= 0, wx_down= 0,center_radius= 0 )

In [113]:
#np.save( '/XF11ID/analysis/2017_1/masks/Bad_4M', partial_mask )
#Bad_4M = np.load( '/XF11ID/analysis/2017_1/masks/Bad_4M.npy' )

In [114]:
#create a circle mask for windows
if False: #make it True to make window mask
    window_shadow  = ~create_cross_mask(  avg_img, center=[ 911,997],
                           wy_left= 0, wy_right= 0, 
                             wx_up= 0, wx_down= 0,center_circle=True, center_radius= 680)
else:
    window_shadow = 1

In [126]:
full_mask = partial_mask *window_shadow
#full_mask = Ver_Beamstop * Hor_Beamstop *Bad_4M *window_shadow

In [ ]:


In [127]:
show_img( full_mask, aspect = 1 )



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

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



In [120]:
fig, ax = plt.subplots()
im = ax.imshow((mask)*avg_img,  cmap= cmap_albula,origin='lower',norm= LogNorm( vmin=.1, vmax=1e5 ),
               interpolation='none')
plt.show()


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


In [156]:
#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 [157]:
np.save(  path +   uid +"_mask", mask)

In [158]:
path +   uid +"_mask"


Out[158]:
'/XF11ID/analysis/2017_2/masks/3d08d0_mask'

save with a meaningful filename, make False after excute to avoid overwrite


In [128]:
if False:
    meaningful_name = 'Jul15_GISAXS' 
    np.save(  path +   meaningful_name, mask)
    print( path +   meaningful_name )

In [ ]:


In [164]:
path +   meaningful_name


Out[164]:
'/XF11ID/analysis/2017_2/masks/Jul15_GISAXS'

In [165]:
uid


Out[165]:
'3d08d0'