Chapter 2: Introduction


5.2. Homework 2#

Reading Microscopy Data

Download

OpenInColab

part of

MSE672: Introduction to Transmission Electron Microscopy

by Gerd Duscher, Spring 2024
Microscopy Facilities
Institute of Advanced Materials & Manufacturing
Materials Science & Engineering
The University of Tennessee, Knoxville

Background and methods to analysis and quantification of data acquired with transmission electron microscopes.

5.2.1. Load Packages#

First we need to load the libraries we want to use. Here we use:

  • numpy: numerical library

  • matplotlib: graphic library

  • pyTEMlib: TEM microsocpy library

All of those packages except pyTEMlib are provided by annaconda.

5.2.1.1. Check Installed Packages#

import sys
import importlib.metadata
def test_package(package_name):
    """Test if package exists and returns version or -1"""
    try:
        version = importlib.metadata.version(package_name)
    except importlib.metadata.PackageNotFoundError:
        version = -1
    return version

# pyTEMlib setup ------------------
if test_package('pyTEMlib') < '0.2024.1.0':
    print('installing pyTEMlib')
    !{sys.executable} -m pip install  --upgrade pyTEMlib -q
# ------------------------------
print('done')
done

5.2.1.2. Load the plotting and pyTEMlib packages#

%matplotlib  widget
import matplotlib.pylab as plt
import numpy as np
import sys

if 'google.colab' in sys.modules:
    from google.colab import output
    output.enable_custom_widget_manager()
    from google.colab import drive

import pyTEMlib
import pyTEMlib.file_tools  as ft     # File input/ output library


# For archiving reasons it is a good idea to print the version numbers out at this point
print('pyTEM version: ',pyTEMlib.__version__)
__notebook__='CH1_04-Reading_File'
__notebook_version__='2024_01_09'
You don't have igor2 installed.     If you wish to open igor files, you will need to install it     (pip install igor2) before attempting.
You don't have gwyfile installed.     If you wish to open .gwy files, you will need to      install it (pip install gwyfile) before attempting.
Symmetry functions of spglib enabled
pyTEM version:  0.2023.9.1

5.2.2. Open a file#

This function opens a hfd5 file in the pyNSID style which enables you to keep track of your data analysis.

Please see the Installation notebook for installation.

Please note that the plotting routine of matplotlib was introduced in Matplotlib and Numpy for Micrographs notebook.

In the first lab we used a sample with a carbon grid of a periodicity of 500 nm

If you run this notebook on your own computer you should download your images from the google drive for 2023 Lab Data, if you run it on google colab you can go to the drive directory in the dialog below.

In any case:

You must log into Google with your UTK account to be able to read these data.

Go to the folder of your data and select a file

if 'google.colab' in sys.modules:
    drive.mount("/content/drive")
fileWidget = ft.FileWidget()

Now we plot that file

main_dataset = fileWidget.selected_dataset
v = main_dataset.plot()

5.2.3. Determination of Magnification#

We plot the image in pixels and there will be a line to select the length of a feature.

Any Mouseclick will extend the line from the last click

fig = plt.figure()

plt.imshow(main_dataset.T)
plt.title(main_dataset.title)
ax = plt.gca()
start_x = 0

fixed_line = False
line = plt.plot([0, 100],[0,200], color = 'orange')


def on_click(event):
   
    if event.inaxes:
        (start_x, end_x), (start_y, end_y) = line[0].get_data()
        start_x = end_x
        start_y = end_y
        end_x = event.xdata
        end_y = event.ydata

        line[0].set_data([start_x, end_x],[start_y, end_y])

        plt.draw()
# mouse_reference = plt.connect('motion_notify_event', on_move)
fig.canvas.mpl_connect('button_press_event', on_click)
15

5.2.4. Length and Angle of Line#

line_coordinates = np.array(line[0].get_data()).T
vector = line_coordinates[0]-line_coordinates[1]
print(f' The line is {np.linalg.norm(vector):.2f}pixels long')
print(f' The angle is {np.degrees(np.arctan2(vector[1], vector[0]))%180:.2f} degrees')
 The line is 1638.61pixels long
 The angle is 172.23 degrees

5.2.5. Second Image#

second_fileWidget = ft.FileWidget()
second_dataset = second_fileWidget.selected_dataset
view = second_dataset.plot()

5.2.6. Determination of Magnification of Image 2#

We plot the image in pixels and there will be a line to select the length of a feature.

Any Mouseclick will extend the line from the last click

fig = plt.figure()

plt.imshow(second_dataset.T)
plt.title(second_dataset.title)
ax = plt.gca()
start_x = 0

fixed_line = False
line = plt.plot([0, 100],[0,200], color = 'orange')


def on_click(event):
   
    if event.inaxes:
        (start_x, end_x), (start_y, end_y) = line[0].get_data()
        start_x = end_x
        start_y = end_y
        end_x = event.xdata
        end_y = event.ydata

        line[0].set_data([start_x, end_x],[start_y, end_y])

        plt.draw()
# mouse_reference = plt.connect('motion_notify_event', on_move)
fig.canvas.mpl_connect('button_press_event', on_click)

5.2.7. Length and Angle of Line of Image 2#

line_coordinates = np.array(line[0].get_data()).T
vector = line_coordinates[0]-line_coordinates[1]
print(f' The line is {np.linalg.norm(vector):.2f}pixels long')
print(f' The angle is {np.degrees(np.arctan2(vector[1], vector[0]))%180:.2f} degrees')
 The line is 851.57pixels long
 The angle is 30.07 degrees

5.2.8. Question#

  • What are the pixel sizes in the two images

  • What is the relative change in magnification with respect to pixel and with respect to indicated Magnification

  • What is the relative rotation