-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Developed Codes for SAXS-GiSAXS_XPCS #12
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
import numpy as np | ||
|
||
|
||
def radial_grid(center, shape, pixel_size=None): | ||
"""Convert a cartesian grid (x,y) to the radius relative to some center | ||
|
||
Parameters | ||
---------- | ||
center : tuple | ||
point in image where r=0; may be a float giving subpixel precision. | ||
Order is (rr, cc). | ||
shape : tuple | ||
Image shape which is used to determine the maximum extent of output | ||
pixel coordinates. | ||
Order is (rr, cc). | ||
pixel_size : sequence, optional | ||
The physical size of the pixels. | ||
len(pixel_size) should be the same as len(shape) | ||
defaults to (1,1) | ||
|
||
Returns | ||
------- | ||
r : array | ||
The distance of each pixel from `center` | ||
Shape of the return value is equal to the `shape` input parameter | ||
""" | ||
|
||
if pixel_size is None: | ||
pixel_size = (1, 1) | ||
|
||
X, Y = np.meshgrid(pixel_size[1] * (np.arange(shape[1]) - center[1]), | ||
pixel_size[0] * (np.arange(shape[0]) - center[0])) | ||
return np.sqrt(X*X + Y*Y) | ||
|
||
|
||
def bin_1D(x, y, nx=None, min_x=None, max_x=None): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same function iskbeam/utils bin_!D? @yugangzhang Why do you need the exact same function from skbeam in chxtools? can't you import from there? |
||
""" | ||
Bin the values in y based on their x-coordinates | ||
|
||
Parameters | ||
---------- | ||
x : array | ||
position | ||
y : array | ||
intensity | ||
nx : integer, optional | ||
number of bins to use defaults to default bin value | ||
min_x : float, optional | ||
Left edge of first bin defaults to minimum value of x | ||
max_x : float, optional | ||
Right edge of last bin defaults to maximum value of x | ||
|
||
Returns | ||
------- | ||
edges : array | ||
edges of bins, length nx + 1 | ||
|
||
val : array | ||
sum of values in each bin, length nx | ||
|
||
count : array | ||
The number of counts in each bin, length nx | ||
""" | ||
|
||
# handle default values | ||
if min_x is None: | ||
min_x = np.min(x) | ||
if max_x is None: | ||
max_x = np.max(x) | ||
if nx is None: | ||
nx = int(max_x - min_x) | ||
|
||
# use a weighted histogram to get the bin sum | ||
bins = np.linspace(start=min_x, stop=max_x, num=nx+1, endpoint=True) | ||
val, _ = np.histogram(a=x, bins=bins, weights=y) | ||
# use an un-weighted histogram to get the counts | ||
count, _ = np.histogram(a=x, bins=bins) | ||
# return the three arrays | ||
return bins, val, count | ||
|
||
def bin_edges_to_centers(input_edges): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @yugangzhang isn't this the same function in skbeam/utils bin_edges_to_centers function? |
||
""" | ||
Helper function for turning a array of bin edges into | ||
an array of bin centers | ||
|
||
Parameters | ||
---------- | ||
input_edges : array-like | ||
N + 1 values which are the left edges of N bins | ||
and the right edge of the last bin | ||
|
||
Returns | ||
------- | ||
centers : ndarray | ||
A length N array giving the centers of the bins | ||
""" | ||
input_edges = np.asarray(input_edges) | ||
return (input_edges[:-1] + input_edges[1:]) * 0.5 | ||
|
||
|
||
def circular_average(image, calibrated_center, threshold=0, nx=1500, | ||
pixel_size=(1, 1), min_x=None, max_x=None, mask=None): | ||
"""Circular average of the the image data | ||
The circular average is also known as the radial integration | ||
Parameters | ||
---------- | ||
image : array | ||
Image to compute the average as a function of radius | ||
calibrated_center : tuple | ||
The center of the image in pixel units | ||
argument order should be (row, col) | ||
threshold : int, optional | ||
Ignore counts above `threshold` | ||
default is zero | ||
nx : int, optional | ||
number of bins in x | ||
defaults is 100 bins | ||
pixel_size : tuple, optional | ||
The size of a pixel (in a real unit, like mm). | ||
argument order should be (pixel_height, pixel_width) | ||
default is (1, 1) | ||
min_x : float, optional number of pixels | ||
Left edge of first bin defaults to minimum value of x | ||
max_x : float, optional number of pixels | ||
Right edge of last bin defaults to maximum value of x | ||
Returns | ||
------- | ||
bin_centers : array | ||
The center of each bin in R. shape is (nx, ) | ||
ring_averages : array | ||
Radial average of the image. shape is (nx, ). | ||
""" | ||
radial_val = radial_grid(calibrated_center, image.shape, pixel_size) | ||
|
||
|
||
if mask is not None: | ||
#maks = np.ones_like( image ) | ||
mask = np.array( mask, dtype = bool) | ||
binr = radial_val[mask] | ||
image_mask = np.array( image )[mask] | ||
|
||
else: | ||
binr = np.ravel( radial_val ) | ||
image_mask = np.ravel(image) | ||
|
||
bin_edges, sums, counts = bin_1D( binr, | ||
image_mask, | ||
nx, | ||
min_x=min_x, | ||
max_x=max_x) | ||
|
||
#print (counts) | ||
th_mask = counts > threshold | ||
ring_averages = sums[th_mask] / counts[th_mask] | ||
|
||
bin_centers = bin_edges_to_centers(bin_edges)[th_mask] | ||
|
||
return bin_centers, ring_averages |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
from chx_libs import * | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make these relative imports ( |
||
from chx_generic_functions import show_img | ||
|
||
|
||
##### | ||
#load data by databroker | ||
|
||
|
||
def get_sid_filenames(header): | ||
"""get a bluesky scan_id, unique_id, filename by giveing uid and detector | ||
|
||
Parameters | ||
---------- | ||
header: a header of a bluesky scan, e.g. db[-1] | ||
|
||
Returns | ||
------- | ||
scan_id: integer | ||
unique_id: string, a full string of a uid | ||
filename: sring | ||
|
||
Usuage: | ||
sid,uid, filenames = get_sid_filenames(db[uid]) | ||
|
||
""" | ||
|
||
keys = [k for k, v in header.descriptors[0]['data_keys'].items() if 'external' in v] | ||
events = get_events( header, keys, handler_overrides={key: RawHandler for key in keys}) | ||
key, = keys | ||
filenames = [ str( ev['data'][key][0]) + '_'+ str(ev['data'][key][2]['seq_id']) for ev in events] | ||
sid = header['start']['scan_id'] | ||
uid= header['start']['uid'] | ||
|
||
return sid,uid, filenames | ||
|
||
|
||
def load_data( uid , detector = 'eiger4m_single_image' ): | ||
"""load bluesky scan data by giveing uid and detector | ||
|
||
Parameters | ||
---------- | ||
uid: unique ID of a bluesky scan | ||
detector: the used area detector | ||
|
||
Returns | ||
------- | ||
image data: a pims frames series | ||
if not success read the uid, will return image data as 0 | ||
|
||
Usuage: | ||
imgs = load_data( uid, detector ) | ||
md = imgs.md | ||
""" | ||
hdr = db[uid] | ||
flag =1 | ||
while flag<4 and flag !=0: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be removed now that the MongoDBs are being managed correctly. |
||
try: | ||
ev, = get_events(hdr, [detector]) | ||
flag =0 | ||
except: | ||
flag += 1 | ||
print ('Trying again ...!') | ||
|
||
if flag: | ||
print ("Can't Load Data!") | ||
uid = '00000' #in case of failling load data | ||
imgs = 0 | ||
else: | ||
imgs = ev['data'][detector] | ||
#print (imgs) | ||
return imgs | ||
|
||
def get_frames_from_dscan( hdr, detector = 'eiger4m_single_image' ): | ||
ev = get_events(hdr, [detector]) | ||
length = int( hdr['start']['plan_args']['num'] ) | ||
shape = hdr['descriptors'][0]['data_keys'][detector]['shape'][:2] | ||
imgs = np.zeros( [ length, shape[1],shape[0]] ) | ||
for i in range( int( hdr['start']['plan_args']['num'] ) ): | ||
imgs[i] = next(ev)['data']['eiger4m_single_image'][0] | ||
|
||
return np.array( imgs ) | ||
|
||
def load_metadata(hdr, name): | ||
seq_of_img_stacks = get_images(hdr, name) | ||
return seq_of_img_stacks[0].md | ||
def load_images(hdr, name): | ||
seq_of_img_stacks = get_images(hdr, name) # 1 x 21 x 2167 x 2070 | ||
return np.squeeze(np.asarray(seq_of_img_stacks)) | ||
|
||
|
||
def RemoveHot( img,threshold= 1E7, plot_=True ): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Names of functions should be lowercase with underscores, not "CamelCase." |
||
mask = np.ones_like( np.array( img ) ) | ||
badp = np.where( np.array(img) >= threshold ) | ||
if len(badp[0])!=0: | ||
mask[badp] = 0 | ||
if plot_: | ||
show_img( mask ) | ||
return mask | ||
|
||
|
||
############ | ||
###plot data | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
def get_avg_img( data_series, sampling = 100, plot_ = False , *argv,**kwargs): | ||
'''Get average imagef from a data_series by every sampling number to save time''' | ||
avg_img = np.average(data_series[:: sampling], axis=0) | ||
if plot_: | ||
fig, ax = plt.subplots() | ||
uid = 'uid' | ||
if 'uid' in kwargs.keys(): | ||
uid = kwargs['uid'] | ||
|
||
im = ax.imshow(avg_img , cmap='viridis',origin='lower', | ||
norm= LogNorm(vmin=0.001, vmax=1e2)) | ||
#ax.set_title("Masked Averaged Image") | ||
ax.set_title('Uid= %s--Masked Averaged Image'%uid) | ||
fig.colorbar(im) | ||
plt.show() | ||
return avg_img | ||
|
||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same function in skbeam/utils radial_grid?