rhkpy package
Submodules
rhkpy.rhkpy_loader module
- rhkpy.rhkpy_loader.load_rhksm4(filename)[source]
Load the data from the .sm4 file using the old loader from spym
- class rhkpy.rhkpy_loader.rhkdata(filename, repetitions=0, alternate=True, loadraw=False, **kwargs)[source]
Bases:
objectA container for the xarray based structure of the RHK data. Loads the RHK “sm4” file from the path at:
filename.- Parameters:
filename (str) – path and filename of the “sm4” file to be loaded
repetitions (int, optional) – The number of repeated aquisitions of spectra in tip position, defaults to 0
alternate (bool, optional) – True if the bias is swept forward and backward, False if not, defaults to True
loadraw (bool, optional) – Set to True if you want the raw topography data, defaults to False
Some variables of the
rhkdataclass:- Variables:
All the variables can be listed by calling:
rhkdata.print_info.Note
If you want to skip the “flatten” filter of the topography images, use: loadraw = True.
- Example:
import rhkpy # Load dI/dV spectra, measured along a line filename = 'linespectra.sm4' linespec = rhkpy.rhkdata(filename) # display the contents of the spectroscopy `xarray` instance linespec.spectra <xarray.Dataset> Dimensions: (bias: 501, dist: 64, repetitions: 1, biasscandir: 2) Coordinates: * bias (bias) float64 0.5 0.498 0.496 0.494 ... -0.496 -0.498 -0.5 * dist (dist) float64 0.0 0.5279 1.056 1.584 ... 32.2 32.73 33.26 * repetitions (repetitions) int32 0 * biasscandir (biasscandir) <U5 'left' 'right' Data variables: lia (bias, dist, repetitions, biasscandir) float64 3.585 ... 5.185 current (bias, dist, repetitions, biasscandir) float64 99.49 ... -132.7 x (dist) float64 -37.48 -36.97 -36.45 ... -5.902 -5.384 -4.866 y (dist) float64 -173.5 -173.6 -173.7 ... -179.7 -179.9 -180.0 Attributes: (12/15) filename: line_9K_ABC6_2020_11_01_12_12_27_213.sm4 bias: 0.49999988 bias units: V setpoint: 99.99999439624929 setpoint units: pA measurement date: 11/01/20 ... ... LI amplitude unit: mV LI frequency: 1300.0 LI frequency unit: Hz LI phase: -102.9999998 datatype: line spectype: iv # select the dI/dV signal (lia) and average the # forward and backward bias sweeps and repetitions linespec_avg = linespec.spectra.lia.mean(dim = ['biasscandir', 'repetitions']) # plot the dI/dV values along the remaining coordinates: bias, dist linespec_avg.plot()
- coord_to_absolute()[source]
Returns a new
rhkdatainstance, with the coordinates updated to reflect the abolute tip position. This includes X, Y offset and rotation.- Returns:
rhkdatainstance, with the same data and metadata, but therhkdata.image,xarrayvariable coordinates shifted to absolute tip positions.- Return type:
rhkdatainstance- Example:
import rhkpy m = rhkpy.rhkdata('didv map.sm4') # Take the `rhkdata` instance (image or map): `m`, # and convert the image coordinates to absolute values m_abs = m.coord_to_absolute() # coordinates of the instance `m` # We can see it runs from 0 to 100 nm print(m.image.x.min().data, m.image.x.max().data) 0.0 100.0 # check the same corrdinate for the new `m_abs` print(m_abs.image.x.min().data, m_abs.image.x.max().data) -877.0008892433623 -741.0633876547834 # we can see it's now shows the exact tip position # the image is also rotated, as the "scan angle" attribute shows m_abs.image.attrs['scan angle'] 30.0 # plot the rotated and offset image m_abs.image.topography.sel(scandir = 'forward').plot()
- mrep()[source]
Returns a new instance of
rhkdata, with the data averaged (usingxarray.Variable.mean) along the ‘repetitions’ coordinate. Meant to be shorthand for:rhkdata_instance.spectra.mean(dim = 'repetitions').
- msw()[source]
Returns a new instance of
rhkdata, with the data averaged (usingxarray.Variable.mean) along the ‘biasscandir’ or ‘zscandir’ coordinate. Meant to be shorthand for:rhkdata_instance.spectra.mean(dim = 'biasscandir'), orrhkdata_instance.spectra.mean(dim = 'zscandir').
- polyflatten(**kwargs)[source]
Uses
polyflatten()to flatten the selected datafield in therhkdatainstance. All keywords accepted bypolyflatten()can be passed.- Returns:
rhdatainstance, with the selectedfield_typeflattened. Defaultfield_type= ‘topography’.- Return type:
rhdatainstance
- qplot(width=None, **kwargs)[source]
Quick plot of the
rhkdatainstance- Parameters:
width (float, optional) – set size of plot, defaults to None
The colorscales used for density plots can be specified by the keywords below. For possible colorscale options see the HoloViews colormaps.
- Parameters:
cmap_topo (str, optional) – topography colorscale, defaults to ‘fire’
cmap_spec (str, optional) – colorscale for plotting dI/dV data, defaults to ‘viridis’
- Returns:
panelplot- Return type:
panel
rhkpy.rhkpy_process module
- rhkpy.rhkpy_process.bgsubtract(x_data, y_data, polyorder=1, toplot=False, fitmask=None, hmin=0.5, hmax=10000, wmin=1.5, wmax=20, prom=2, exclusion_factor=3, peak_pos=None)[source]
Takes the
x_dataandy_dataand automatically finds peaks, usingscipy.find_peaks. These peaks are then used to define the areas of the background signal (y_data). In the areas with the peaks removed, the background is fitted by a polynomial of order given by the optional argument:polyorder. The fit is performed byscipy.optimize.curve_fit. The function returns they_datavalues with the background removed, the background polinomial values themselves and the coefficients of the background fit results, as used bynumpy.polyval.In cases, where the automatic peak find is not functioning as expected, one can pass the values in
x_data, at which peaks appear. In this case, thewminoption determines the width of all peaks.If a
fitmaskis supplied for fitting, the fitmask is not calculated and only a polynomial fit is performed. This can decrease the runtime.- Parameters:
x_data (
numpyarray) – variable of the data (typically bias voltage)y_data (
numpyarray) – data values (typically dI/dV)polyorder (int, optional) – order of polynomial used to fit the background, defaults to 3
toplot (bool, optional) – if True a plot of: the fit, the background used and positions of the peaks is shown, defaults to False
fitmask (
numpyarray) – Fitmask to be used for polynomial fitting.hmin (float, optional) – minimum height of the peaks passed to
scipy.signal.find_peaks, defaults to 50hmax (float, optional) – maximum height of the peaks passed to
scipy.signal.find_peaks, defaults to 10000wmin (float, optional) – minimum width of the peaks, passed to
scipy.signal.find_peaks, defaults to 4wmax (float, optional) – maximum width of the peaks passed to
scipy.signal.find_peaks, defaults to 60prom (float, optional) – prominence of the peaks, passed to
scipy.signal.find_peaks, defaults to 10exclusion_factor (float, optional) – this parameter multiplies the width of the peaks found by
scipy.signal.find_peaks, or specified bywminif the peak positions are passed by hand, usingpeak_pos, defaults to 6peak_pos (list of floats, optional) – list of the peak positions in
x_datavalues used for exclusion, defaults to None
- Returns:
y_data_nobg,bg_values,coeff,params_used_at_run,mask,covar- Return type:
tuple: (
numpyarray,numpyarray,numpyarray, dictionary,numpyarray,numpyarray)
y_data_nobg: data values, with the background subtracted,bg_values: the polynomial values of the fit, at thex_datapositions,coeff: coefficients of the polynomial fit, as used by:numpy.polyval,params_used_at_run: parameters used at runtimemask: the calculated fitmaskcovar: covariance of the fit parameters
Note
Using the option:
peak_pos, awmin*exclusion_factor/2region (measured in datapoints) on both sides of the peaks is excluded from the background fit. If automatic peak finding is used, the exclusion area is calculated in a similar way, but the width of the individual peaks are used, as determined byscipy.signal.find_peaks.
- rhkpy.rhkpy_process.coord_to_absolute(xrobj)[source]
Takes as input the
rhkdata.imagevariable of anrhkdatainstance. Returns a newxarrayinstance, with the coordinates updated to reflect the abolute tip position. This includes X, Y offset and rotation.- Parameters:
xrobj (
xarrayDataset) –xarrayimage variable of anrhkdataobject- Returns:
xarrayrhkdata.imageinstance, with the same data and metadata as the input and the coordinates shifted to absolute tip positions.- Return type:
xarrayDataset- Example:
import rhkpy m = rhkpy.rhkdata('didv map.sm4') # Take the `rhkdata` instance (image or map): `m`, # and convert the image coordinates to absolute values m_abs_image = rhkpy.coord_to_absolute(m.image) # coordinates of the instance `m` # We can see it runs from 0 to 100 nm print(m.image.x.min().data, m.image.x.max().data) 0.0 100.0 # check the same corrdinate for the new `m_abs` print(m_abs_image.x.min().data, m_abs_image.x.max().data) -877.0008892433623 -741.0633876547834 # we can see it's now shows the exact tip position # the image is also rotated, as the "scan angle" attribute shows m_abs_image.attrs['scan angle'] 30.0 # plot the rotated and offset image m_abs_image.topography.sel(scandir = 'forward').plot()
- rhkpy.rhkpy_process.gaussian(x, x0=0, ampl=2, width=0.05, offset=0)[source]
Gaussian function. Width and amplitude parameters have the same meaning as for
lorentz().- Parameters:
x (float,
numpyarray, etc.) – values for the x coordinatex0 (float) – shift along the x corrdinate
ampl (float) – amplitude of the peak
width (float) – FWHM of the peak
offset (float) – offset along the function value
- Returns:
values of a Gaussian function
- Return type:
float,
numpyarray, etc.
- rhkpy.rhkpy_process.gaussian2(x, x01=-5, ampl1=1, width1=0.05, x02=5, ampl2=1, width2=0.05, offset=0)[source]
Double Gaussian function
- Parameters:
x (float,
numpyarray, etc.) – values for the x coordinatex01 (float, optional) – position of the peak, defaults to -5
ampl1 (float, optional) – amplitude of the peak, defaults to 1
width1 (float, optional) – width of the peak, defaults to 10
x02 (float, optional) – position of the peak, defaults to 5
ampl2 (float, optional) – amplitude of the peak, defaults to 1
width2 (float, optional) – width of the peak, defaults to 10
offset (float, optional) – offset, defaults to 0
- Returns:
values of a double Gaussian function
- Return type:
float,
numpyarray, etc.
- rhkpy.rhkpy_process.genthumbs(folderpath='./', **kwargs)[source]
Generate thumbnails for the sm4 files present in the current folder (usually the folder where the jupyter notebook is present). It
folderpathis specified it generates the thumbnails in the path given. All other files are ignored. Subfolders are ignored. The method usesqplot()to make the png images.- Parameters:
folderpath (str, optional) – path to the folder containing the sm4 files, defaults to ‘./’
- Example:
import rhkpy # generate thumbnails of the sm4 files in the current working directory rhkpy.genthumbs() # generate thumbnails for the folder "stm measurements/maps" rhkpy.genthumbs(folderpath = './stm measurements/maps/')
Note
Possible options for
folderpathare:relative path: “./” means the current directory. “../” is one directory above the current one.
absolute path: Can start with: “c:/users/averagejoe/data”
If you use backslashes to separate folder names, remember to append “r” to the beginning of the path to escape backslashes. For example:
folderpath = r"c:\users\averagejoe\data". Paths can be copied directly from Windows explorer, if you append an “r”.
- rhkpy.rhkpy_process.lorentz(x, x0=0, ampl=2, width=0.05, offset=0)[source]
Single Lorentz function
- Parameters:
x (float,
numpyarray, etc.) – values for the x coordinatex0 (float) – x corrdinate
ampl (float) – amplitude of the peak
width (float) – FWHM of the peak
offset (float) – offset along the function value
- Returns:
values of a single Lorentz function
- Return type:
float,
numpyarray, etc.
Note
The area of the peak can be given by:
area = np.pi * amplitude * width / 2
- rhkpy.rhkpy_process.mapsection(specmap, start_point, end_point)[source]
Makes a section across a dI/dV spectroscopy map:
specmap. Starting and end points:start_pointtoend_point. It usesxarray.Dataset.interpto interpolate between data values.- Parameters:
specmap (
xarrayDataSet) – the spectraxarrayvariable of anrhkdatainstance. Found under:rhkpy.rhkpy_loader.rhkdata.spectra.start_point (tuple: (float, float)) – starting point for the line section. In the format: (x, y), found in the
specpos_x,specpos_ycoordinates ofspecmap.end_point (tuple: (float, float)) – end point for the line section. In the format: (x, y), found in the
specpos_x,specpos_ycoordinates ofspecmap.
- Returns:
xarrayDataSet of the line section- Return type:
xarrayDataSet
Takes any number of
rhkdataarguments: ‘map’, ‘line’, ‘spec’ and plots all of them on a single plot. Plotting of the spectroscopy positions can be skipped by setting the optional keyword:plot_specto False. Plotting is done in the order of passing of the arguments. First argument will be plotted first.The color map used for plotting topography images can be specified by the
cmapoptional keyword argument. Default value is ‘bone’.Colors for use in the labels can be specified by the optional keyword:
palette_name. If this is used, the number of colors also needs to be specified, by:num_colors. For possible palette options look at the bokeh palettes.- Returns:
holoviewsplot- Return type:
holoviews- Example:
import rhkpy # Load some data didvmap = rhkpy.rhkdata('didvmap path/map.sm4') topography = rhkpy.rhkdata('topo path/topo1.sm4') single_spec = rhkpy.rhkdata('single spec path/single spec.sm4') # plot the topography and spectroscopy positions rhkpy.navigation(topography, didvmap, single_spec) # skip plotting the spectroscopy positions rhkpy.navigation(topography, didvmap, single_spec, plot_spec = False) # In the above examples, the image from topography # is plotted before the image data of didivmap! # You can change the plotting order by changing # the order of the `rhkdata` instances in the arguments. rhkpy.navigation(didvmap, topography, single_spec) # now didvmap is plotted first
Note
Arguments are plotted in the order they are passed to
navigation().The spectroscopy positions in dI/dV maps can be simply visualized by just passing the map
rhkdatainstance.
- rhkpy.rhkpy_process.peakfit(xrobj, func=<function gaussian>, fitresult=None, stval=None, bounds=None, toplot=False, pos_x=None, pos_y=None, **kwargs)[source]
Fitting a function to peaks in the data contained in the
xrobjDataArray. Currently, Datasets with multiple DataArrays is not supported. Peak fitting is always assumed to be along the bias coordinate.- Parameters:
xrobj (
xarray) –xarrayDataArray, of a single spectrum or a map.func (function, optional) – function to be used for fitting, defaults to gaussian
fitresult (
xarrayDataset, optional) – anxarrayDataset of a previous fit calculation, with matching dimensions. If this is passed topeakfit(), the fit calculation in skipped and the passed Dataset is used.stval (dictionary of
funcparameters, optional) – starting values for the fit parameters offunc. You are free to specify only some of the values, the rest will be filled by defaults. Defaults are given in the starting values for keyword arguments infunc.bounds (dictionary of
funcparameters, with tuples containing lower and upper bounds, optional) – bounds for the fit parameters, used byxarray.curvefit. Simlar dictionary, likestval, but the values area a list, with lower and upper components. Defaults to Nonetoplot (boolean, optional) – plot the fit result, defaults to
Falsepos_x (int or float, optional) – pos_x parameter of an
xarraymap to be used in conjunction withtoplot = Truepos_y (int or float, optional) – pos_y parameter of an
xarraymap to be used in conjunction withtoplot = True
- Returns:
fitted parameters of
funcand covariances in a Dataset- Return type:
xarrayDataset- Example:
import rhkpy # example coming soon
Note
Use
toplot= True to tweak the starting values. Iftoplot= True, in case of a map, if nopos_xandpos_yare specified, the middle of the map is used for plotting.Passing a
boundsdictionary topeakfit()seems to increase the fitting time significantly. This might be an issue withxarray.DataArray.curvefit.By passing a previous fit result, using the optional parameter
fitresult, we can just plot the fit result at multiple regions of the map.
See also
It is good practice, to crop the data to the vicinity of the peak you want to fit to.
- rhkpy.rhkpy_process.polyflatten(xrobj, field_type='topography', **kwargs)[source]
Fits a polynomial to the fast scan lines of topography data and subtracts it from the lines.
The keyword argument
polyorderworks the same way as inbgsubtract(). Keywords used bybgsubtract()can be passed.Still needs testing.
- Parameters:
xrobj (
xarrayDataset,rhkdata.image) –xarrayimage variable of anrhkdataobjectfield_type (str, optional) – select the DataArray: ‘topography’, ‘current’ or ‘lia’, defaults to ‘topography’
- Returns:
New
rhkdata.imageDataset ofrhkdata, with the DataArray specifiec byfield_typeflattened.- Return type:
xarrayDataset
- rhkpy.rhkpy_process.polynomial_fit(order, x_data, y_data)[source]
Polinomial fit to x_data, y_data
- Parameters:
order (int) – order of the polinomial to be fit
x_data (
numpyarray) – x coordinate of the datay_data (
numpyarray) – y coordinate of the data
- Returns:
coefficients of the polinomial
coeff, as used bynumpy.polyval, covariance matrixcovar, as returned byscipy.optimize.curve_fit- Return type:
tuple: (
numpyarray,numpyarray)