<jupyter_start><jupyter_text>**Chapter 2: [Diffraction](Ch2-Diffraction.ipynb)**  Relrod - Sample Geometry and Excitation Errorpart of  **[Analysis of Transmission Electron Microscope Data](_Analysis_of_Transmission_Electron_Microscope_Data.ipynb)**by <NAME>, 2020Microscopy FacilitiesJoint Institute of Advanced MaterialsThe University of Tennessee, KnoxvilleModel based analysis and quantification of data acquired with transmission electron microscopesWe first **load the usual libraries**:<jupyter_code>%pylab --no-import-all notebook 

from mpl_toolkits.mplot3d import Axes3D  # 3D plotting<jupyter_output>Populating the interactive namespace from numpy and matplotlib<jupyter_text>GoalsHere we want to invesitate the influence of sample shape onto the diffraction pattern.Generally, we have to consider the sample to be a thin disk-like shape, which in reciprocal space that is a rod-like shape.It is this rod, together with dynamic scattering, that makes diffraction spots with excitation error possible.In the following, we perform Fourier Transforms on disks with different ratios of height to radius.  Make a disk shaped samplePlease, change ``width`` and ``height`` to see the effect in the Fourier Transform<jupyter_code>radius = 50

height= 8

data = np.zeros([100,100,100])

x, y = np.mgrid[-data.shape[0]/2:data.shape[0]/2,-data.shape[1]/2:data.shape[1]/2]

data[x**2+y**2<radius**2, int(data.shape[2]/2-height/2): int(data.shape[2]/2+1+height/2)  ] = 1
X, Y, Z = np.where(data>0)<jupyter_output><empty_output><jupyter_text>Perform Fourier transform<jupyter_code>r_data = np.fft.fftshift(np.fft.fftn(np.fft.fftshift(data)))

r_data = np.log(1+np.abs(r_data))
r_data[r_data<r_data.max()*.8] = 0
r_data[r_data>0]=1

Xf, Yf, Zf = np.where(r_data>0)<jupyter_output><empty_output><jupyter_text>Plot Data Fast<jupyter_code>fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Plot the surface
ax.scatter(X, Y, Z, color='r', label='real space')
ax.scatter(Xf, Yf, Zf, color='b', label='reciprocal space')

ax.set_xlim(0,data.shape[0])
ax.set_ylim(0,data.shape[1])
ax.set_zlim(0,data.shape[2]);<jupyter_output><empty_output><jupyter_text>Slow Plotting Routine<jupyter_code>fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Plot the surface
ax.voxels(r_data, color='b')
ax.voxels(data, color='r')

ax.set_xlim(0,data.shape[0])
ax.set_ylim(0,data.shape[1])
ax.set_zlim(0,data.shape[2]);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13112745098039216, 'fraction_numerical': 0.027369281045751634, 'mean_word_length': 6.951298701298701, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '516356', 'license': 'MIT', 'n_tokens_mistral': 929, 'n_tokens_neox': 878, 'n_words': 207, 'path': 'Diffraction/CH2-Relrod.ipynb', 'repo_name': 'tommycwong/MSE672-Introduction-to-TEM', 'size': '2454'}