Band Excitation data procesing¶
Suhas Somnath, Chris R. Smith, Stephen Jesse¶
The Center for Nanophase Materials Science and The Institute for Functional Imaging for Materials Oak Ridge National Laboratory 9/2/2020
Reference:¶
This Jupyter notebook uses pycroscopy to analyze Band Excitation data. We request you to reference the Arxiv paper titled “USID and Pycroscopy - Open frameworks for storing and analyzing spectroscopic and imaging data” in your publications.
Jupyter Notebooks:¶
This is a Jupyter Notebook - it contains text and executable code cells
. To learn more about how to use it, please see this video. Please see the image below for some basic tips on using this notebook.
If you have any questions or need help running this notebook, please get in touch with your host if you are a users at the Center for Nanophase Materials Science (CNMS) or our google group.
Image courtesy of Jean Bilheux from the neutron imaging GitHub repository.
Configure the notebook¶
[1]:
# Make sure needed packages are installed and up-to-date
import sys
!conda install --yes --prefix {sys.prefix} numpy scipy matplotlib scikit-learn Ipython ipywidgets h5py
!{sys.executable} -m pip install -U --no-deps BGlib # this will automatically install sidpy and pyUSID as well
DirectoryNotACondaEnvironmentError: The target directory exists, but it is not a conda environment.
Use 'conda create' to convert the directory to a conda environment.
target directory: /opt/hostedtoolcache/Python/3.9.17/x64
Requirement already satisfied: BGlib in /opt/hostedtoolcache/Python/3.9.17/x64/lib/python3.9/site-packages (0.0.4)
[2]:
# Ensure python 3 compatibility
from __future__ import division, print_function, absolute_import
# Import necessary libraries:
# General utilities:
import os
# Computation:
import numpy as np
import h5py
# Visualization:
import matplotlib.pyplot as plt
from IPython.display import display, HTML
# The engineering components supporting BGlib:
import sidpy
import pyUSID as usid
# Finally, BGlib itself
from BGlib import be as belib
# Make Notebook take up most of page width
display(HTML(data="""
<style>
div#notebook-container { width: 95%; }
div#menubar-container { width: 65%; }
div#maintoolbar-container { width: 99%; }
</style>
"""))
[3]:
# set up notebook to show plots within the notebook
%matplotlib notebook
Set some basic preferences¶
This notebook performs some functional fitting whose duration can be substantially decreased by using more memory and CPU cores. We have provided default values below but you may choose to change them if necessary. Setting max_cores
to None
will allow usage of all but one CPU core for the computations.
By default, results of the functional fitting will be written back to the same HDF5 file. However, if you prefer to write results into different HDF5 files, please set the results_to_new_file
parameter to True
instead. Users of the DataFed Scientific Data Management System may want to set this parameter to True
.
[4]:
max_mem = 1024*8 # Maximum memory to use, in Mbs. Default = 1024
max_cores = None # Number of logical cores to use in fitting. None uses all but 2 available cores.
results_to_new_file = False
Make the data USID compatible¶
Converting the raw data into a USID formatted hierarchical data format (HDF or .h5) file gives you access to the fast fitting algorithms and powerful analysis functions within the broader pycroscopy ecosystem
H5 files:¶
are like smart containers that can store matrices with data, folders to organize these datasets, images, metadata like experimental parameters, links or shortcuts to datasets, etc.
are readily compatible with high-performance computing facilities
scale very efficiently from few kilobytes to several terabytes
can be read and modified using any language including Python, Matlab, C/C++, Java, Fortran, Igor Pro, etc.
You can load either of the following:¶
Any .mat or .txt parameter file from the original experiment
A .h5 file generated from the raw data using BGlib - skips translation
You can select desired file type by choosing the second option in the pull down menu on the bottom right of the file window
[5]:
input_file_path = '/Users/syz/Desktop/BEPS_NDF/Example_1/20170225_MoTe2_S3_F4_vdc_sweep_contact_0001/20170225_MoTe2_S3_F4_vdc_sweep_contact_parms_0001.txt'
[6]:
(data_dir, filename) = os.path.split(input_file_path)
if input_file_path.endswith('.h5'):
# No translation here
h5_path = input_file_path
force = True # Set this to true to force patching of the datafile.
tl = belib.translators.LabViewH5Patcher()
tl.translate(h5_path, force_patch=force)
else:
# Set the data to be translated
data_path = input_file_path
(junk, base_name) = os.path.split(data_dir)
# Check if the data is in the new or old format. Initialize the correct translator for the format.
if base_name == 'newdataformat':
(junk, base_name) = os.path.split(junk)
translator = belib.translators.BEPSndfTranslator(max_mem_mb=max_mem)
else:
translator = belib.translators.BEodfTranslator(max_mem_mb=max_mem)
if base_name.endswith('_d'):
base_name = base_name[:-2]
# Translate the data
print(translator)
h5_path = translator.translate(data_path, show_plots=True, save_plots=False)
/opt/hostedtoolcache/Python/3.9.17/x64/lib/python3.9/site-packages/sidpy/sid/translator.py:42: FutureWarning: Consider using sidpy.Reader instead of sidpy.Translator if possible and contribute your reader to ScopeReaders
warn('Consider using sidpy.Reader instead of sidpy.Translator if '
<BGlib.be.translators.be_odf.BEodfTranslator object at 0x7faa7d189dc0>
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
Cell In[6], line 25
23 # Translate the data
24 print(translator)
---> 25 h5_path = translator.translate(data_path, show_plots=True, save_plots=False)
File /opt/hostedtoolcache/Python/3.9.17/x64/lib/python3.9/site-packages/BGlib/be/translators/be_odf.py:151, in BEodfTranslator.translate(self, file_path, show_plots, save_plots, do_histogram, verbose)
149 file_path = path.abspath(file_path)
150 (folder_path, basename) = path.split(file_path)
--> 151 (basename, path_dict) = self._parse_file_path(file_path)
153 h5_path = path.join(folder_path, basename + '.h5')
154 tot_bins_multiplier = 1
File /opt/hostedtoolcache/Python/3.9.17/x64/lib/python3.9/site-packages/BGlib/be/translators/be_odf.py:848, in BEodfTranslator._parse_file_path(data_filepath)
840 """
841 A single pair of real and imaginary files are / were generated for:
842 BE-Line and BEPS (compiled version only generated out-of-field or 'read')
843 Two pairs of real and imaginary files were generated for later BEPS datasets
844 These have 'read' and 'write' prefixes to denote out or in field respectively
845 """
846 path_dict = dict()
--> 848 for file_name in listdir(folder_path):
849 abs_path = path.join(folder_path, file_name)
850 if file_name.endswith('.txt') and file_name.find('parm') > 0:
FileNotFoundError: [Errno 2] No such file or directory: '/Users/syz/Desktop/BEPS_NDF/Example_1/20170225_MoTe2_S3_F4_vdc_sweep_contact_0001'
[7]:
folder_path, h5_raw_file_name = os.path.split(h5_path)
h5_file = h5py.File(h5_path, 'r+')
print('Working on:\n' + h5_path)
h5_main = usid.hdf_utils.find_dataset(h5_file, 'Raw_Data')[0]
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[7], line 1
----> 1 folder_path, h5_raw_file_name = os.path.split(h5_path)
2 h5_file = h5py.File(h5_path, 'r+')
3 print('Working on:\n' + h5_path)
NameError: name 'h5_path' is not defined
The file contents are stored in a tree structure, just like files on a conventional computer. The data is stored as a 2D matrix (position, spectroscopic value) regardless of the dimensionality of the data. Thus, the positions will be arranged as row0-col0, row0-col1…. row0-colN, row1-col0…. and the data for each position is stored as it was chronologically collected
The main dataset is always accompanied by four ancillary datasets that explain the position and spectroscopic value of any given element in the dataset.
[8]:
print('Datasets and datagroups within the file:\n------------------------------------')
sidpy.hdf.hdf_utils.print_tree(h5_file)
print('\nThe main dataset:\n------------------------------------')
print(h5_main)
print('\nMetadata or attributes in the measurement datagroup\n------------------------------------')
for key, val in sidpy.hdf.hdf_utils.get_attributes(h5_main.parent.parent).items():
print('{} : {}'.format(key, val))
Datasets and datagroups within the file:
------------------------------------
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[8], line 2
1 print('Datasets and datagroups within the file:\n------------------------------------')
----> 2 sidpy.hdf.hdf_utils.print_tree(h5_file)
4 print('\nThe main dataset:\n------------------------------------')
5 print(h5_main)
NameError: name 'h5_file' is not defined
Get some basic parameters from the H5 file¶
This information will be vital for futher analysis and visualization of the data
[9]:
h5_pos_inds = h5_main.h5_pos_inds
pos_dims = h5_main.pos_dim_sizes
pos_labels = h5_main.pos_dim_labels
print(pos_labels, pos_dims)
h5_meas_grp = h5_main.parent.parent
parm_dict = sidpy.hdf.hdf_utils.get_attributes(h5_meas_grp)
expt_type = sidpy.hdf.hdf_utils.get_attr(h5_file, 'data_type')
is_ckpfm = expt_type == 'cKPFMData'
if is_ckpfm:
num_write_steps = parm_dict['VS_num_DC_write_steps']
num_read_steps = parm_dict['VS_num_read_steps']
num_fields = 2
if expt_type != 'BELineData':
vs_mode = sidpy.hdf.hdf_utils.get_attr(h5_meas_grp, 'VS_mode')
try:
field_mode = sidpy.hdf.hdf_utils.get_attr(h5_meas_grp, 'VS_measure_in_field_loops')
except KeyError:
print('field mode could not be found. Setting to default value')
field_mode = 'out-of-field'
try:
vs_cycle_frac = sidpy.hdf.hdf_utils.get_attr(h5_meas_grp, 'VS_cycle_fraction')
except KeyError:
print('VS cycle fraction could not be found. Setting to default value')
vs_cycle_frac = 'full'
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[9], line 1
----> 1 h5_pos_inds = h5_main.h5_pos_inds
2 pos_dims = h5_main.pos_dim_sizes
3 pos_labels = h5_main.pos_dim_labels
NameError: name 'h5_main' is not defined
Visualize the raw data¶
Use the sliders below to visualize spatial maps (2D only for now), and spectrograms. For simplicity, all the spectroscopic dimensions such as frequency, excitation bias, cycle, field, etc. have been collapsed to a single slider.
[10]:
fig = belib.viz.be_viz_utils.jupyter_visualize_be_spectrograms(h5_main)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[10], line 1
----> 1 fig = belib.viz.be_viz_utils.jupyter_visualize_be_spectrograms(h5_main)
NameError: name 'h5_main' is not defined
Fit the Band Excitation (BE) spectra¶
Fit each of the acquired spectra to a simple harmonic oscillator (SHO) model to extract the following information regarding the response: * Oscillation amplitude * Phase * Resonance frequency * Quality factor
By default, the cell below will take any previous result instead of re-computing the SHO fit
[11]:
sho_fit_points = 5 # The number of data points at each step to use when fitting
sho_override = False # Force recompute if True
h5_sho_targ_grp = None
if results_to_new_file:
h5_sho_file_path = os.path.join(folder_path,
h5_raw_file_name.replace('.h5', '_sho_fit.h5'))
print('\n\nSHO Fits will be written to:\n' + h5_sho_file_path + '\n\n')
f_open_mode = 'w'
if os.path.exists(h5_sho_file_path):
f_open_mode = 'r+'
h5_sho_file = h5py.File(h5_sho_file_path, mode=f_open_mode)
h5_sho_targ_grp = h5_sho_file
sho_fitter = belib.analysis.BESHOfitter(h5_main, cores=max_cores, verbose=False, h5_target_group=h5_sho_targ_grp)
sho_fitter.set_up_guess(guess_func=belib.analysis.be_sho_fitter.SHOGuessFunc.complex_gaussian,
num_points=sho_fit_points)
h5_sho_guess = sho_fitter.do_guess(override=sho_override)
sho_fitter.set_up_fit()
h5_sho_fit = sho_fitter.do_fit(override=sho_override)
h5_sho_grp = h5_sho_fit.parent
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[11], line 15
12 h5_sho_file = h5py.File(h5_sho_file_path, mode=f_open_mode)
13 h5_sho_targ_grp = h5_sho_file
---> 15 sho_fitter = belib.analysis.BESHOfitter(h5_main, cores=max_cores, verbose=False, h5_target_group=h5_sho_targ_grp)
16 sho_fitter.set_up_guess(guess_func=belib.analysis.be_sho_fitter.SHOGuessFunc.complex_gaussian,
17 num_points=sho_fit_points)
18 h5_sho_guess = sho_fitter.do_guess(override=sho_override)
NameError: name 'h5_main' is not defined
Visualize the SHO results¶
Here, we visualize the parameters for the SHO fits. BE-line (3D) data is visualized via simple spatial maps of the SHO parameters while more complex BEPS datasets (4+ dimensions) can be visualized using a simple interactive visualizer below.
You can choose to visualize the guesses for SHO function or the final fit values from the first line of the cell below.
Use the sliders below to inspect the BE response at any given location.
[12]:
h5_sho_spec_inds = h5_sho_fit.h5_spec_inds
sho_spec_labels = h5_sho_fit.spec_dim_labels
if is_ckpfm:
# It turns out that the read voltage index starts from 1 instead of 0
# Also the VDC indices are NOT repeating. They are just rising monotonically
write_volt_index = np.argwhere(sho_spec_labels == 'write_bias')[0][0]
read_volt_index = np.argwhere(sho_spec_labels == 'read_bias')[0][0]
h5_sho_spec_inds[read_volt_index, :] -= 1
h5_sho_spec_inds[write_volt_index, :] = np.tile(np.repeat(np.arange(num_write_steps), num_fields), num_read_steps)
(Nd_mat, success, nd_labels) = usid.hdf_utils.reshape_to_n_dims(h5_sho_fit, get_labels=True)
print('Reshape Success: ' + str(success))
print(nd_labels)
print(Nd_mat.shape)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[12], line 1
----> 1 h5_sho_spec_inds = h5_sho_fit.h5_spec_inds
2 sho_spec_labels = h5_sho_fit.spec_dim_labels
4 if is_ckpfm:
5 # It turns out that the read voltage index starts from 1 instead of 0
6 # Also the VDC indices are NOT repeating. They are just rising monotonically
NameError: name 'h5_sho_fit' is not defined
[13]:
use_sho_guess = False
use_static_viz_func = True
if use_sho_guess:
sho_dset = h5_sho_guess
else:
sho_dset = h5_sho_fit
if expt_type == 'BELineData' or len(pos_dims) != 2:
use_static_viz_func = True
step_chan = None
else:
if vs_mode not in ['AC modulation mode with time reversal',
'DC modulation mode']:
use_static_viz_func = True
else:
if vs_mode == 'DC modulation mode':
step_chan = 'DC_Offset'
else:
step_chan = 'AC_Amplitude'
if not use_static_viz_func:
try:
# use interactive visualization
belib.viz.be_viz_utils.jupyter_visualize_beps_sho(sho_dset, step_chan)
except:
raise
print('There was a problem with the interactive visualizer')
use_static_viz_func = True
else:
chan_grp = h5_main.parent
meas_grp = chan_grp.parent
# show plots of SHO results vs. applied bias
figs = belib.viz.be_viz_utils.visualize_sho_results(sho_dset, show_plots=True, save_plots=False,
expt_type=expt_type, meas_type=vs_mode,
field_mode=field_mode)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[13], line 7
5 sho_dset = h5_sho_guess
6 else:
----> 7 sho_dset = h5_sho_fit
9 if expt_type == 'BELineData' or len(pos_dims) != 2:
10 use_static_viz_func = True
NameError: name 'h5_sho_fit' is not defined
Fit loops to a function¶
This is applicable only to DC voltage spectroscopy datasets from BEPS. The PFM hysteresis loops in this dataset will be projected to maximize the loop area and then fitted to a function.
Note: This computation generally takes a while for reasonably sized datasets.
[14]:
# Do the Loop Fitting on the SHO Fit dataset
loop_success = False
h5_loop_group = None
if results_to_new_file:
h5_loop_file_path = os.path.join(folder_path,
h5_raw_file_name.replace('.h5', '_loop_fit.h5'))
print('\n\nLoop Fits will be written to:\n' + h5_loop_file_path + '\n\n')
f_open_mode = 'w'
if os.path.exists(h5_loop_file_path):
f_open_mode = 'r+'
h5_loop_file = h5py.File(h5_loop_file_path, mode=f_open_mode)
h5_loop_group = h5_loop_file
loop_fitter = belib.analysis.BELoopFitter(h5_sho_fit, expt_type, vs_mode, vs_cycle_frac,
cores=max_cores, h5_target_group=h5_loop_group,
verbose=False)
loop_fitter.set_up_guess()
h5_loop_guess = loop_fitter.do_guess(override=False)
# Calling explicitely here since Fitter won't do it automatically
h5_guess_loop_parms = loop_fitter.extract_loop_parameters(h5_loop_guess)
loop_fitter.set_up_fit()
h5_loop_fit = loop_fitter.do_fit(override=False)
h5_loop_group = h5_loop_fit.parent
loop_success = True
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[14], line 15
12 h5_loop_file = h5py.File(h5_loop_file_path, mode=f_open_mode)
13 h5_loop_group = h5_loop_file
---> 15 loop_fitter = belib.analysis.BELoopFitter(h5_sho_fit, expt_type, vs_mode, vs_cycle_frac,
16 cores=max_cores, h5_target_group=h5_loop_group,
17 verbose=False)
18 loop_fitter.set_up_guess()
19 h5_loop_guess = loop_fitter.do_guess(override=False)
NameError: name 'h5_sho_fit' is not defined
Prepare datasets for visualization¶
[15]:
# Prepare some variables for plotting loops fits and guesses
# Plot the Loop Guess and Fit Results
if loop_success:
h5_projected_loops = usid.USIDataset(h5_loop_guess.parent['Projected_Loops'])
h5_proj_spec_inds = h5_projected_loops.h5_spec_inds
h5_proj_spec_vals = h5_projected_loops.h5_spec_vals
# reshape the vdc_vec into DC_step by Loop
sort_order = usid.hdf_utils.get_sort_order(h5_proj_spec_inds)
dims = usid.hdf_utils.get_dimensionality(h5_proj_spec_inds[()],
sort_order[::-1])
vdc_vec = np.reshape(h5_proj_spec_vals[h5_proj_spec_vals.attrs['DC_Offset']], dims).T
#Also reshape the projected loops to Positions-DC_Step-Loop
# Also reshape the projected loops to Positions-DC_Step-Loop
proj_nd = h5_projected_loops.get_n_dim_form()
proj_3d = np.reshape(proj_nd, [h5_projected_loops.shape[0],
proj_nd.shape[2], -1])
Visualize Loop fits¶
[16]:
use_static_plots = True
if loop_success:
if not use_static_plots:
try:
fig = belib.viz.be_viz_utils.jupyter_visualize_beps_loops(h5_projected_loops, h5_loop_guess, h5_loop_fit)
except:
print('There was a problem with the interactive visualizer')
use_static_plots = True
if use_static_plots:
for iloop in range(h5_loop_guess.shape[1]):
fig, ax = belib.viz.be_viz_utils.plot_loop_guess_fit(vdc_vec[:, iloop], proj_3d[:, :, iloop],
h5_loop_guess[:, iloop], h5_loop_fit[:, iloop],
title='Loop {} - All Positions'.format(iloop))
Loop Parameters¶
We will now load the loop parameters caluculated from the fit and plot them.
[17]:
h5_loop_parameters = h5_loop_group['Fit_Loop_Parameters']
fig = belib.viz.be_viz_utils.jupyter_visualize_parameter_maps(h5_loop_parameters)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[17], line 1
----> 1 h5_loop_parameters = h5_loop_group['Fit_Loop_Parameters']
2 fig = belib.viz.be_viz_utils.jupyter_visualize_parameter_maps(h5_loop_parameters)
TypeError: 'NoneType' object is not subscriptable
[18]:
map_parm = 'Work of Switching'
plot_cycle = 0
plot_position = (int(pos_dims[0]/2), int(pos_dims[1]/2))
plot_bias_step = 0
fig = belib.viz.be_viz_utils.plot_loop_sho_raw_comparison(h5_loop_parameters, h5_sho_grp, h5_main,
selected_loop_parm=map_parm,
selected_loop_cycle=plot_cycle,
selected_loop_pos=plot_position,
selected_step=plot_bias_step)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[18], line 3
1 map_parm = 'Work of Switching'
2 plot_cycle = 0
----> 3 plot_position = (int(pos_dims[0]/2), int(pos_dims[1]/2))
4 plot_bias_step = 0
6 fig = belib.viz.be_viz_utils.plot_loop_sho_raw_comparison(h5_loop_parameters, h5_sho_grp, h5_main,
7 selected_loop_parm=map_parm,
8 selected_loop_cycle=plot_cycle,
9 selected_loop_pos=plot_position,
10 selected_step=plot_bias_step)
NameError: name 'pos_dims' is not defined
Save and close¶
Save the .h5 file that we are working on by closing it.
Also, consider exporting this notebook as a notebook or an html file. To do this, go to File >> Download as >> HTML
Finally consider saving this notebook if necessary
[19]:
h5_file.close()
if results_to_new_file:
h5_sho_fit.file.close()
h5_loop_fit.file.close()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[19], line 1
----> 1 h5_file.close()
2 if results_to_new_file:
3 h5_sho_fit.file.close()
NameError: name 'h5_file' is not defined