Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get DCFI shifts from a velox STEM file, then apply them to all the raw signals in the dataset.
# Script should be applied to a single Velox emd file, where the file contains a single DCFI dataset
# that has been applied to the detector signal that one wishes to apply to the other detector signals
# A good example is using DCFI on a DF image, and then applying this correction to HAADF, BF and ABF.
import hyperspy.api as hs
import json
import h5py
import numpy as np
def get_nonDCFI_signals(list_of_signals):
'Filters hyperspy signals to only give signals without DCFI'
signals = []
for s in list_of_signals:
try:
s.original_metadata.CustomProperties.ShiftMeasureResultdx
pass
except:
signals.append(s)
return signals
def encoded_json_to_string(encoded):
'Decodes json encoded as uint8 into string, then removes whitespace at end'
words = "".join(map(chr, encoded))
word, _ = words.split('\n')
return word
def get_shifts(filename):
'Loops through a list of signals, grabs first DCFI signal found, gets shift from it'
with h5py.File(filename) as f:
shifts = []
# Loops through available datasets, looking
for key in f['Data/Image'].keys():
encoded = f['Data/Image/{}/Metadata'.format(key)][:].T
first_frame = encoded[0]
word = encoded_json_to_string(first_frame)
if "ShiftMeasureResult" in word:
break
for frame in encoded:
word = encoded_json_to_string(frame)
parsed = json.loads(word)
x = float(parsed['CustomProperties']['ShiftMeasureResult.dx']['value'])
y = float(parsed['CustomProperties']['ShiftMeasureResult.dy']['value'])
shifts.append((y, x)) # Convention is opposite of the hyperspy convention
shifts = np.array(shifts) * -1 # We need how much to shift by, not how much it has been shifted
return shifts
filename = r"Z:\TEM Results\Titan\20190524_IZN6_FEG_Testing\20190524 1424 STEM HAADF-DF4-DF2-BF 11.0 Mx.emd"
shifts = get_shifts(filename)
s = hs.load(filename)
sigs = get_nonDCFI_signals(s)
for sig in sigs:
sig.align2D(shifts=shifts, crop=True, show_progressbar=False)
sumsigs = [s.sum() for s in sigs]
hs.plot.plot_images(sumsigs, cmap='viridis', per_row=2, tight_layout=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.