Last active
August 27, 2020 12:01
-
-
Save thomasaarholt/786aed63834ab6dc62604ce3e14b7395 to your computer and use it in GitHub Desktop.
Create a TEM-like image (HAADF) from a cif file using ASE and HyperSpy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hyperspy.api as hs | |
import numpy as np | |
import matplotlib.pyplot as plt | |
def cell2sig(cell, pixel_size = 0.05, sigma=0.25): | |
Signal2D = hs.signals.Signal2D | |
Gaussian2D = hs.model.components2D.Gaussian2D | |
XLEN, YLEN = (cell.cell.diagonal()[:2] // pixel_size).astype(int) | |
ax0 = { | |
'name':'y', | |
'size': YLEN, | |
'offset':0, | |
'scale':pixel_size, | |
'units':'Å', | |
} | |
ax1 = { | |
'name':'x', | |
'size': XLEN, | |
'offset':0, | |
'scale':pixel_size, | |
'units':'Å', | |
} | |
axes = [ax0, ax1] | |
s = Signal2D(np.zeros((YLEN, XLEN)), axes=axes) | |
m = s.create_model() | |
# Add equivalent unit cells around the image, so that any gaussians that extend into the image, do so. | |
shifts = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 0), (0, 1), (1, -1), (1, 0), (1, 1)] | |
cell_center = np.array([(ax.high_value + ax.scale + ax.low_value)/2 for ax in s.axes_manager.signal_axes]) | |
diagonal_radius = np.array([ax.high_value for ax in s.axes_manager.signal_axes]) | |
for atom in cell: | |
for offset in shifts: | |
xyposition = atom.position[:2] + cell.cell.diagonal()[:2] * offset | |
if np.abs(np.linalg.norm(xyposition - cell_center)) > np.linalg.norm(cell_center - diagonal_radius) + 1: | |
continue | |
A = atom.number**2 | |
x, y = xyposition | |
g = Gaussian2D(A, sigma, sigma, x, y ) | |
m.append(g) | |
s2 = m.as_signal() | |
return s2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment