Adaptive Fourier Filtering#
part of
a pycroscopy ecosystem package
Notebook by Gerd Duscher, 2023
Microscopy Facilities
Institute of Advanced Materials & Manufacturing
The University of Tennessee, Knoxville
Model based analysis and quantification of data acquired with transmission electron microscopes
Content#
An introduction into Fourier Filtering of images.
Install pyTEMlib#
If you have not done so in the Introduction Notebook, please test and install pyTEMlib and other important packages with the code cell below.
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
if test_package('pyTEMlib') < '0.2025.3.0':
print('installing pyTEMlib')
!{sys.executable} -m pip install --upgrade pyTEMlib -q
print('done')
done
Loading of necessary libraries#
Please note, that we only need to load the pyTEMlib library, which is based on sidpy Datasets.
%matplotlib widget
import numpy as np
import matplotlib.pylab as plt
import sys
if 'google.colab' in sys.modules:
from google.colab import output
output.enable_custom_widget_manager()
from google.colab import drive
import sys
sys.path.insert(0, '../../')
sys.path.insert(0, '../../../sidpy')
import sidpy
%load_ext autoreload
%autoreload 2
import pyTEMlib
import pyTEMlib.file_tools # File input/ output library
import pyTEMlib.image_tools
if 'google.colab' in sys.modules:
drive.mount("/content/drive")
print('pyTEMlib version: ', pyTEMlib.__version__)
note_book_version = '2025.05.19'
note_book_name='pyTEMib/notebooks/Imaging/Adaptive_Fourier_Filter'
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
Qt dialogs are not available
SimpleITK not installed; Registration Functions for Image Stacks not available
install with: conda install -c simpleitk simpleitk
pyTEMlib version: 0.2025.03.0
Open File#
The data are held in the memory in the sidpy format with rather extensive capabilities.
All results can be stored in that NSID format file.
First we select the file and the dataset (the dropdown menu at the bottom), in case there are several.
fileWidget = pyTEMlib.file_tools.FileWidget()
Now, we open and plot the selected dataset. Select another one and rerun only cell below (Home button gets you back to original).
Please note that you can zoom and drag the intensity values in the color-bar.
datasets = fileWidget.datasets
dataset = fileWidget.selected_dataset
if dataset.data_type.name != 'IMAGE':
print('We really would need an image here')
view = dataset.plot()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[4], line 2
1 datasets = fileWidget.datasets
----> 2 dataset = fileWidget.selected_dataset
4 if dataset.data_type.name != 'IMAGE':
5 print('We really would need an image here')
AttributeError: 'FileWidget' object has no attribute 'selected_dataset'
#power_spectrum.add_provenance('jup', 'kkle', 1)
print(dataset.provenance)
im = dataset.sum(axis=0)
im.provenance
{'sidpy': {'from_array_': '_0.12.8_2025-03-20-08:38:20.367207'}, 'open_file_2025-03-20-08:38:21.093136': {'package': 'pyTEMlib', 'version': '0.2025.03.0', 'linked_data': 'SciFiReader.EMDReader'}, 'sum_2025-03-20-08:38:28.246056': {'package': 'sidpy', 'version': '0.12.8'}}
{'sidpy': {'from_array_': '_0.12.8_2025-03-20-08:38:20.367207'},
'open_file_2025-03-20-08:38:21.093136': {'package': 'pyTEMlib',
'version': '0.2025.03.0',
'linked_data': 'SciFiReader.EMDReader'},
'sum_2025-03-20-08:38:28.246056': {'package': 'sidpy', 'version': '0.12.8'},
'sum_2025-03-20-10:48:06.952517': {'package': 'sidpy', 'version': '0.12.8'}}
Power Spectrum of Image#
if dataset.data_type.name == 'IMAGE_STACK':
stack_dim = dataset.get_dimensions_by_type('TEMPORAL')
image = dataset.sum(axis=stack_dim)
image.data_type = 'Image'
elif dataset.data_type.name == 'IMAGE':
image = dataset
else:
print('We really would need an image here')
power_spectrum = pyTEMlib.image_tools.power_spectrum(image, smoothing=1)
power_spectrum.view_metadata()
print('source: ', power_spectrum.source)
view = power_spectrum.plot()
fft :
smoothing : 1
minimum_intensity : 13.52620834490971
maximum_intensity : 21.520821921762174
source: sum_aggregate_DCFI%28HAADF%29
Spot Detection in Fourier Transform#
Change the spot_threshold value slightly up or down (down is more spots)
# ------Input----------
spot_threshold=0.15
# ---------------------
spots, _ = pyTEMlib.image_tools.diffractogram_spots(power_spectrum, spot_threshold=spot_threshold)
spots = spots[np.linalg.norm(spots[:,:2],axis=1)<7,:]
spots = spots[np.linalg.norm(spots[:,:2],axis=1)>0.5,:]
power_spectrum.plot()
plt.gca().scatter(spots[:,0],spots[:,1], color='red', alpha=0.4);
Found 19 reflections
dataset.provenance
{'sidpy': {'from_array_': '_0.12.7_2025-03-19-18:27:18.331601'}}
Adaptive Fourier Filter#
filtered_dataset = pyTEMlib.image_tools.adaptive_fourier_filter(image, spots,
low_pass=2.0, reflection_radius=.3)
view = filtered_dataset.plot(cmap='gray')
Let’s see what we did - In Fourier space, of course.
filtered_power_spectrum = pyTEMlib.image_tools.power_spectrum(filtered_dataset, smoothing=0)
power_spectrum.view_metadata()
print('source: ', power_spectrum.source)
view = filtered_power_spectrum.plot()
fft :
smoothing : 1
minimum_intensity : 13.52620834490971
maximum_intensity : 21.520821921762174
source: sum_aggregate_DCFI%28HAADF%29
Please note that the spots are ordered from center to outside.
The third parameter of a spot is its angle.
print(spots[:5])
[[-6.70853549 -0.36592012 -1.62528778]
[ 6.70853549 0.36592012 1.51630487]
[ 5.42781508 -3.96413461 2.20159394]
[-5.42781508 3.96413461 -0.93999871]
[-3.110321 -5.97669526 -2.66175213]]
Log the result#
filtered_dataset.title = 'Fourier_Filtered_Image'
datasets.update({'Log_000': filtered_dataset})
Save Datasets to file#
group = pyTEMlib.file_tools.save_dataset(datasets, filename=dataset.metadata['filename'])
Cannot overwrite file. Using: p1-3-hr3-1.hf5
C:\Users\gduscher\AppData\Local\anaconda3\lib\site-packages\pyNSID\io\hdf_io.py:111: UserWarning: main_data_name should not contain the "-" character. Reformatted name from:p1-3-hr3 to p1_3_hr3
warn('main_data_name should not contain the "-" character. Reformatted'
C:\Users\gduscher\AppData\Local\anaconda3\lib\site-packages\pyNSID\io\hdf_utils.py:376: FutureWarning: validate_h5_dimension may be removed in a future version
warn('validate_h5_dimension may be removed in a future version',
C:\Users\gduscher\AppData\Local\anaconda3\lib\site-packages\pyNSID\io\hdf_utils.py:376: FutureWarning: validate_h5_dimension may be removed in a future version
warn('validate_h5_dimension may be removed in a future version',
A convenient function to select a dataset (for further processing, visualization or whatever)
chooser = pyTEMlib.file_tools.ChooseDataset(datasets)
chooser.dataset.metadata
{'analysis': 'adaptive fourier filtered',
'spots': array([[-2.62027028, 2.21908057, -0.86811058],
[ 2.62027028, -2.21908057, 2.27348207],
[-3.4226497 , 0.43880124, -1.44328677],
[ 3.4226497 , -0.43880124, 1.69830589],
[-3.22205484, -1.25371784, -1.94187537],
[ 3.22205484, 1.25371784, 1.19971728],
[ 1.31640373, -3.23459202, 2.75508635],
[-1.31640373, 3.23459202, -0.38650631],
[-2.10624597, -2.80832796, -2.49809154],
[ 2.10624597, 2.80832796, 0.64350111],
[-3.51040995, 0.33850382, -1.47466498],
[ 3.51040995, -0.33850382, 1.66692768],
[-0.60178456, -3.48533559, -2.97061654],
[ 0.60178456, 3.48533559, 0.17097612],
[-2.21908057, -2.93369974, -2.49400159],
[ 2.21908057, 2.93369974, 0.64759107],
[-4.73905343, 3.67339327, -0.91140362],
[ 4.73905343, -3.67339327, 2.23018903],
[-5.52889567, -2.36952671, -1.97568811],
[ 5.52889567, 2.36952671, 1.16590454],
[-0.78984224, -6.0303828 , -3.01135688],
[ 0.78984224, 6.0303828 , 0.13023578]]),
'low_pass': 2.0,
'reflection_radius': 0.3}
A tree-like plot of the file
print(dataset.h5_dataset.file.filename)
pyTEMlib.file_tools.h5_tree(dataset.h5_dataset.file)
C:\Users\gduscher\Documents\Github\pyTEMlib\example_data\p1-3-hr3-1.hf5
/
├ Measurement_000
---------------
├ Channel_000
-----------
├ p1_3_hr3
--------
├ metadata
--------
├ experiment
----------
├ original_metadata
-----------------
├ DM
--
├ DocumentObjectList
------------------
├ 0
-
├ AnnotationGroupList
-------------------
├ 0
-
├ Font
----
├ ObjectTags
----------
├ ImageDisplayInfo
----------------
├ DimensionLabels
---------------
├ MainSliceId
-----------
├ ObjectTags
----------
├ DocumentTags
------------
├ Image Behavior
--------------
├ UnscaledTransform
-----------------
├ ZoomAndMoveTransform
--------------------
├ ImageData
---------
├ Calibrations
------------
├ Brightness
----------
├ Dimension
---------
├ 0
-
├ 1
-
├ Dimensions
----------
├ ImageSourceList
---------------
├ 0
-
├ Id
--
├ ImageTags
---------
├ Acquisition
-----------
├ Device
------
├ CCD
---
├ Configuration
-------------
├ Transpose
---------
├ Frame
-----
├ Area
----
├ Transform
---------
├ Transform List
--------------
├ 0
-
├ Transpose
---------
├ CCD
---
├ Intensity
---------
├ Range
-----
├ Transform
---------
├ Transform List
--------------
├ 0
-
├ 1
-
├ Reference Images
----------------
├ Dark
----
├ Sequence
--------
├ Parameters
----------
├ Base Detector
-------------
├ Detector
--------
├ Environment
-----------
├ High Level
----------
├ Shutter
-------
├ Transform
---------
├ Objects
-------
├ 0
-
├ 1
-
├ 2
-
├ 3
-
├ DataBar
-------
├ Custom elements
---------------
├ Microscope Info
---------------
├ Items
-----
├ 0
-
├ 1
-
├ 2
-
├ MinVersionList
--------------
├ 0
-
├ Page Behavior
-------------
├ PageTransform
-------------
├ SentinelList
------------
├ Thumbnails
----------
├ 0
-
├ UniqueID
--------
├ p1_3_hr3
├ x
├ y
├ Log_000
-------
├ Fourier_Filtered_Image
----------------------
├ Fourier_Filtered_Image
├ metadata
--------
├ x
├ y
Close File#
let’s close the file but keep the filename
filename = dataset.h5_dataset.file.filename
dataset.h5_dataset.file.close()
Simulate new notebook#
We can now simulate a new notebook and open the file again.
new_dataset= pyTEMlib.file_tools.open_file(filename)
choose_image = pyTEMlib.file_tools.ChooseDataset(new_dataset)
view = choose_image.dataset.plot()
We want to make an image operation of the images in the file.
choose_image = pyTEMlib.file_tools.ChooseDataset(new_dataset)
print(' subtract')
choose_image2 = pyTEMlib.file_tools.ChooseDataset(new_dataset)
subtract
new_image = np.array(choose_image.dataset) - np.array(choose_image2.dataset)
new_image = choose_image.dataset.like_data(new_image)
vew = new_image.plot()
choose_image.dataset.metadata
{'filename': 'C:\\Users\\gduscher\\Documents\\Github\\pyTEMlib\\example_data\\p1-3-hr3.dm3',
'experiment': {'acceleration_voltage': 199990.28125,
'exposure_time': 1.0,
'microscope': 'Libra 200 MC'}}