Chapter 3: Introduction


5.3. Homework 3#

Reading Microscopy Data

Download

OpenInColab

part of

MSE672: Introduction to Transmission Electron Microscopy

by Gerd Duscher, Spring 2025
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.3.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.3.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.2025.1.0':
    print('installing pyTEMlib')
    !{sys.executable} -m pip install  --upgrade pyTEMlib -q
# ------------------------------
print('done')

5.3.1.2. Load the plotting and pyTEMlib packages#

Google Colab: Make sure to restart the Session (Crtl-M)

%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__='2025_03_09'

5.3.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 2025 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.3.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)

5.3.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')
print(f"indicated pixel size: {main_dataset.x[1]-main_dataset.x[0]:.2f} nm")

5.3.5. Second Image#

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

5.3.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.3.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')
print(f"indicated pixel size: {second_dataset.x[1]-second_dataset.x[0]:.2f} nm")

5.3.8. Question#

  • What are the measured 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