mirror of
https://github.com/cdtools-developers/cdtools.git
synced 2026-11-24 14:27:53 +01:00
Add ability to load/use Bragg geometry ptycho data, also add a 1D convolve to try with the terrible data from CSX
This commit is contained in:
1 parent
6471a17c34
commit
a09cd4b39e
6 files changed
+138
-17
No files matched your search
+18
-5
@@ -135,12 +135,13 @@ def get_sample_info(cxi_file):
|
||||
metadata[attr] = str(s1[attr][()].decode())
|
||||
except AttributeError as e:
|
||||
metadata[attr] = str(np.array(s1[attr][:])[0].decode())
|
||||
|
||||
|
||||
float_attrs = ['concentration',
|
||||
'mass',
|
||||
'temperature',
|
||||
'thickness',
|
||||
'unit_cell_volume']
|
||||
|
||||
for attr in float_attrs:
|
||||
if attr in s1:
|
||||
metadata[attr] = np.float32(s1[attr][()])
|
||||
@@ -148,11 +149,14 @@ def get_sample_info(cxi_file):
|
||||
if 'unit_cell' in s1:
|
||||
metadata['unit_cell'] = np.array(s1['unit_cell']).astype(np.float32)
|
||||
|
||||
# TODO: Add my nonstandard "surface normal" attribute here
|
||||
|
||||
# TODO: I should add the sample geometry as a valid metadata that can
|
||||
# be copied over
|
||||
|
||||
if 'geometry_1/orientation' in s1:
|
||||
orient = np.array(s1['geometry_1/orientation']).astype(np.float32)
|
||||
xvec = orient[:3] / np.linalg.norm(orient[:3])
|
||||
yvec = orient[3:] / np.linalg.norm(orient[3:])
|
||||
metadata['orientation'] = np.array([xvec,yvec,
|
||||
np.cross(xvec,yvec)])
|
||||
|
||||
# Check if the metadata is empty
|
||||
if metadata == {}:
|
||||
metadata = None
|
||||
@@ -444,7 +448,16 @@ def add_sample_info(cxi_file, metadata):
|
||||
cxi_file['entry_1'].create_group('sample_1')
|
||||
s1 = cxi_file['entry_1/sample_1']
|
||||
|
||||
if 'orientation' in metadata:
|
||||
if 'geometry_1' not in s1:
|
||||
s1.create_group('geometry_1')
|
||||
# Only store the part of this matrix as defined in the CXI file spec
|
||||
s1['geometry_1'].create_dataset('orientation',
|
||||
data=metadata['orientation'].ravel()[:6])
|
||||
|
||||
for key, value in metadata.items():
|
||||
if key == 'orientation':
|
||||
continue # this is a special case
|
||||
if isinstance(value,(str,bytes)):
|
||||
s1[key] = np.string_(value)
|
||||
elif isinstance(value, datetime.datetime):
|
||||
|
||||
@@ -4,7 +4,8 @@ import torch as t
|
||||
from CDTools.tools import cmath
|
||||
|
||||
__all__ = ['centroid', 'centroid_sq', 'sinc_subpixel_shift',
|
||||
'find_subpixel_shift', 'find_pixel_shift', 'find_shift']
|
||||
'find_subpixel_shift', 'find_pixel_shift', 'find_shift',
|
||||
'convolve_1d']
|
||||
|
||||
|
||||
def centroid(im, dims=2):
|
||||
@@ -206,3 +207,55 @@ def find_shift(im1, im2, resolution=10):
|
||||
resolution=resolution)
|
||||
|
||||
return subpixel_shift
|
||||
|
||||
|
||||
def convolve_1d(image, kernel, dim=0, fftshift_kernel=True):
|
||||
"""Convolves an image with a 1d kernel along a specified dimension
|
||||
|
||||
The convolution is a circular convolution calculated using a Fourier
|
||||
transform. The calculation is done so the input remains differentiable
|
||||
with respect to the output.
|
||||
|
||||
If the image has a final dimension of 2, it is assumed to be complex.
|
||||
Otherwise, the image is assumed to be real. The image and kernel
|
||||
must either both be real or both be complex.
|
||||
|
||||
Args:
|
||||
image (torch.Tensor) : The image to convolve
|
||||
kernel (torch.Tensor) : The 1d kernel to convolve with
|
||||
dim (int) : Default 0, the dimension to convolve along
|
||||
fftshift_kernel (bool) : Default True, whether to fftshift the kernel first.
|
||||
|
||||
Returns:
|
||||
(torch.Tensor) : The convolved image
|
||||
"""
|
||||
|
||||
complex_things = 2
|
||||
if image.shape[-1] != 2:
|
||||
image = t.stack((image,t.zeros_like(image)),dim=-1)
|
||||
complex_things -= 1
|
||||
|
||||
if kernel.shape[-1] != 2:
|
||||
kernel = t.stack((kernel,t.zeros_like(kernel)),dim=-1)
|
||||
complex_things -= 1
|
||||
|
||||
# Take a correlation
|
||||
if fftshift_kernel:
|
||||
kernel = cmath.ifftshift(kernel)
|
||||
|
||||
|
||||
# We have to transpose the relevant dimension to -2 before using the fft,
|
||||
# which expects to operate on the final non-complex dimension
|
||||
trans_im = t.transpose(image, dim, -2)
|
||||
|
||||
fft_im = t.fft(trans_im, 1)
|
||||
fft_kernel = t.fft(kernel, 1)
|
||||
trans_conv = t.ifft(cmath.cmult(fft_im,fft_kernel), 1)
|
||||
|
||||
conv_im = t.transpose(trans_conv, dim, -2)
|
||||
|
||||
# If nothing was input as complex, the result should be returned as real
|
||||
if complex_things == 0:
|
||||
return conv_im[...,0]
|
||||
else:
|
||||
return conv_im
|
||||
@@ -240,7 +240,10 @@ def SHARP_style_probe(dataset, shape, det_slice, propagation_distance=None):
|
||||
# to use the mask or not?
|
||||
intensities = np.zeros(shape)
|
||||
for params, im in dataset:
|
||||
intensities[det_slice] += dataset.mask.cpu().numpy() * im.cpu().numpy()
|
||||
if hasattr(dataset,'mask') and dataset.mask is not None:
|
||||
intensities[det_slice] += dataset.mask.cpu().numpy() * im.cpu().numpy()
|
||||
else:
|
||||
intensities[det_slice] += im.cpu().numpy()
|
||||
intensities /= len(dataset)
|
||||
|
||||
# Subtract off a known background if it's stored
|
||||
|
||||
Reference in new issue
Block a user