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
In [2]:
path = '/XF11ID/analysis/2016_3/masks/'
print ("The analysis results will be saved in : %s"%path)
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] ))
In [5]:
#imgs = load_data( uid, detector, reverse= True )
imgs = load_data( uid, detector, reverse= False )
md = imgs.md
In [6]:
imgs
Out[6]:
In [7]:
Nimg=len(imgs)
In [8]:
md
Out[8]:
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 [ ]:
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 )
In [14]:
imgs.shape
Out[14]:
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]:
In [20]:
2167-784
Out[20]:
In [21]:
mask_rh = RemoveHot( avg_img, 5E4, plot_=True)
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()
In [29]:
from skimage.draw import line_aa, line, polygon, circle
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]:
In [32]:
show_img( imgs[0] , vmin=.0001, vmax=1000, logs=True, image_name ='uid=%s'%uid )
In [33]:
avg_img.shape
Out[33]:
In [34]:
2167-786
Out[34]:
In [35]:
md['beam_center_x'],2167-md['beam_center_y']
Out[35]:
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()
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()
In [88]:
np.save( path + uid +"_GT_mask", mask)
In [89]:
path + uid +"_GT_mask"
Out[89]:
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]:
In [ ]: