Chapter 1: Introduction
Prerequisites¶
part of
MSE672: Introduction to Transmission Electron Microscopy
Spring 2026
by Gerd Duscher
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.
cd
Packages¶
The idea behind any python program is to make use of the highly efficient libraries that already exist.
I use anaconda3 (not miniconda) which is available for all major operating systems.
We use a few modules that come with every python installation, like:
math
sys
os
We use mostly the common packages for scientific computing in python (all included in anaconda3) The most important ones are:
Numpy - the numerical library
Scipy the scientific library
Matplotlib the interactive plotting library
These packages are expected to be installed on your computer to run the notebooks of this book. These packages are already installed in Google colab.
For specialist applications we do not reinvent the wheel and use those on an as needed basis.
Example is the library to register a stack of images:
SimpleITK integration of AI in image analysis and for high performing computer algorithms
pyNSID the atomistic simulation program is used for crystallographic data
ase together with a symmetry package
For dialogs, we use the capabilities of Jupyter Widgets provided by:
All routines that are introduced in the notebooks are also available (for analysis) in the provided package
If you install pyTEMlib with the code cell below all packages you need for this book will be installed.
Basic Installation of Python Environment¶
I recommend installing the free anaconda3 from this link.
If you have an old version of anaconda, please reinstall the new version. The lecture is based on fairly new packages of scipy and numpy, dask, and h5py, which will create problems in old anaconda versions.
Once you have installed anaconda type
pip install pyTEMlib
Alternatively you can just run the code cell below. Be aware that the installation process may take a while.
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.2026.1.0':
print('installing pyTEMlib')
!{sys.executable} -m pip install --upgrade pyTEMlib
# ------------------------------
print('done')done
Data Format¶
All data in this course are stored in the file format of
which is based on
In the memory the data are stored in dictionaries in the
sidpy data format
which provides a lot of functionalities for plotting and is the basis for any other package of the pycroscopy ecosystem.
Notebook preamble¶
As a minimum Any notebook in this course has to have the following libraries loaded :
# import matplotlib and numpy
# Define iteractivity with this **magic** command
# use "inline" instead of "widget" for non-interactive plots
%matplotlib widget
import matplotlib.pylab as plt
import numpy as npNumpy¶
The calculations depend on Numpy and an installation of that package that is compiled to include BLAS and LAPACK libraries will be much faster than the standalone version. For example the numpy installed on ubuntu with > sudo apt-get install python3-numpy or at windows you can install the numpy package from Gohlke’s webpage which compiled against the MKL library of Intel. If you used anaconda3, everything is already optimized.
The command below lets you see what you have
## What is numpy compiled against
np.__config__.show()
print('numpy version: ',np.version.version)
import scipy as sp
print('scipy version: ',sp.__version__)Build Dependencies:
blas:
detection method: pkgconfig
found: true
include directory: C:/Users/gduscher/AppData/Local/anaconda3/Library/include
lib directory: C:/Users/gduscher/AppData/Local/anaconda3/Library/lib
name: mkl-sdl
openblas configuration: unknown
pc file directory: C:\miniconda3\conda-bld\numpy_and_numpy_base_1763980694785\_h_env\Library\lib\pkgconfig
version: '2025'
lapack:
detection method: pkgconfig
found: true
include directory: C:/Users/gduscher/AppData/Local/anaconda3/Library/include
lib directory: C:/Users/gduscher/AppData/Local/anaconda3/Library/lib
name: mkl-sdl
openblas configuration: unknown
pc file directory: C:\miniconda3\conda-bld\numpy_and_numpy_base_1763980694785\_h_env\Library\lib\pkgconfig
version: '2025'
Compilers:
c:
commands: cl.exe
linker: link
name: msvc
version: 19.29.30159
c++:
commands: cl.exe
linker: link
name: msvc
version: 19.29.30159
cython:
commands: cython
linker: cython
name: cython
version: 3.1.4
Machine Information:
build:
cpu: x86_64
endian: little
family: x86_64
system: windows
host:
cpu: x86_64
endian: little
family: x86_64
system: windows
Python Information:
path: C:\miniconda3\conda-bld\numpy_and_numpy_base_1763980694785\_h_env\python.exe
version: '3.13'
SIMD Extensions:
baseline:
- SSE
- SSE2
- SSE3
found:
- SSSE3
- SSE41
- POPCNT
- SSE42
- AVX
- F16C
numpy version: 2.3.5
scipy version: 1.16.3
TEM Library¶
You will have to run the code cell below at least once to install the library with the programs needed for the analysis of data.
The code cell below will install pyTEMlib directly from pypi
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.2026.1.0':
print('installing pyTEMlib')
!{sys.executable} -m pip install --upgrade pyTEMlib
print('done')done
Now we load pyTEMlib to make it available for this notebook.
import pyTEMlib
print(f'pyTEM version: {pyTEMlib.__version__}')pyTEM version: 0.2026.1.1
Test¶
Let’s test if the installation was successful and plot a unit cell. You can rotate the plot around, zoom and select. Try it!
# ---- Input ------------
crystal_name = 'graphite'
# -----------------------
# make a structure dictionary with crystal_tools
atoms = pyTEMlib.crystal_tools.structure_by_name(crystal_name)
print(atoms)
import ase.visualize
ase.visualize.view(atoms*(4,4,1), viewer='x3d')
Atoms(symbols='C4', pbc=False, cell=[[2.46772414, 0.0, 0.0], [-1.2338620699999996, 2.1371117947721068, 0.0], [0.0, 0.0, 6.711]])
help(pyTEMlib.crystal_tools.structure_by_name)Help on function structure_by_name in module pyTEMlib.crystal_tools:
structure_by_name(crystal_name: str) -> ase.atoms.Atoms | None
Provides crystal structure in ase.Atoms format.
Additional information is stored in the info attribute as a dictionary
Parameter
---------
crystal_name: str
Please note that the chemical expressions are not case-sensitive.
Returns
-------
atoms: ase.Atoms
structure
Example
-------
>> # for a list of pre-defined crystal structures
>> import pyTEMlib.crystal_tools
>> print(pyTEMlib.crystal_tools.crystal_data_base.keys())
>>
>> atoms = pyTEMlib.crystal_tools.structure_by_name('Silicon')
>> print(atoms)
>> print(atoms.info)
Get used to changing parameters and type silicon instead of Graphite in the code cell above.
Navigation¶
Chapter 1: Introduction
List of Content: Front
Appendix¶
I am using some extensions to jupyter notebooks which can be installed with the following cells
I like mostly the table of content extension that shows where in the notebook I am and lets me jump to different parts easily.
# Install a pip package in the current Jupyter kernel
import sys
!{sys.executable} -m jupyter nbextension listKnown nbextensions:
config dir: C:\Users\gdusc\anaconda3\etc\jupyter\nbconfig
notebook section
jupyter-jsmol/extension enabled
jupyterlab-plotly/extension enabled
jupyter-js-widgets/extension enabled
- Validating: ok
- Validating: ok
- Validating: ok
Use one of the following (uncomment pip and comment out the conda command line, if you are not in an anaconda environment)
import sys
#!{sys.executable} -m pip install jupyter_contrib_nbextensions
!conda install --yes --prefix {sys.prefix} jupyter_contrib_nbextensionsCollecting package metadata (current_repodata.json): ...working... done
Solving environment: ...working... failed with initial frozen solve. Retrying with flexible solve.
Collecting package metadata (repodata.json): ...working... done
Solving environment: ...working... failed with initial frozen solve. Retrying with flexible solve.
PackagesNotFoundError: The following packages are not available from current channels:
- jupyter_contrib_nbextensions
Current channels:
- https://repo.anaconda.com/pkgs/main/win-64
- https://repo.anaconda.com/pkgs/main/noarch
- https://repo.anaconda.com/pkgs/r/win-64
- https://repo.anaconda.com/pkgs/r/noarch
- https://repo.anaconda.com/pkgs/msys2/win-64
- https://repo.anaconda.com/pkgs/msys2/noarch
To search for alternate channels that may provide the conda package you're
looking for, navigate to
https://anaconda.org
and use the search bar at the top of the page.
!{sys.executable} -m jupyter nbextension enable toc2Enabling notebook extension toc2...
- Validating: problems found:
- require? X toc2
%%javascript
$('<div id="toc"></div>').css({position: 'fixed', top: '120px', left: 0}).appendTo(document.body);
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js');