Skip to main content

Posts

CDF of Gaussian distribution

The cumulative distribution function (CDF) of Gaussian distribution with standard deviation (sig) and mean (cen) in python Ref:  https://www.mathworks.com/help/matlab/ref/erf.html https://introcs.cs.princeton.edu/java/11gaussian/ =============================================== python code: ================================================ from scipy import special import matplotlib.pyplot as plt import numpy as np def erfValue(x, cen, sig):     #           1                 x - cen     # erf(x) = --- (1 + erf(-----------------))     #           2             sqrt(2) * sig     return 0.5*(1+special.erf((x-cen)/(np.sqrt(2)*sig))) def erfcValue(x, cen, sig):     return 1-erfValue(x, cen, sig) cen = 9.54 sig=2 xx = np.arange(0,20,step=.01) erf = erfValue(xx, cen, sig) plt.figure() plt.plot(xx,erf,'r'...
Recent posts

conda env list

 all the env would be listed in users/current_user/.conda/environments.txt the content is : C:\ProgramData\miniconda3 C:\ProgramData\miniconda3\envs\GMS_VENV_PYTHON C:\ProgramData\Miniconda3\envs\GMS_VENV_PYTHON C:\ProgramData\Miniconda3\envs\em C:\ProgramData\Miniconda3\envs\tf

UI template II

https://github.com/Renfong/dm-script-tool/blob/master/UI_Template_2.s // Interactive UI template // // 2021/05/06 // Renfong interface call_functions{ number GetBoxNumber(object self); }; class UI_Functions : object { number true, false number UIObjectID object MainUI void SetUIObjectID(object self, number id) { UIObjectID = id MainUI = GetScriptObjectFromID(UIObjectID) }; UI_Functions(object self) { true = 1; false = 0 result("Obect \"UI_Functions\" ["+self.ScriptObjectGetID()+"] constructed. \n") }; ~UI_Functions(object self) { true = 1; false = 0 result("Obect\"UI_Functions\" ["+self.ScriptObjectGetID()+"] deconstructed. \n") }; void btn1response(object self) { number num = MainUI.GetBoxNumber() OKdialog("Number = "+num) }; }; class MainUI : UIFrame { TagGroup btn1, numbox object UI_Functions number true, false, ver MainUI(object self){ true = 1...

How to do PCA for EELS SI in python

PCA is the popular statistic tool to denoise the EELS spectrum image. Here is the simple example to apply PCA by numpy package ================================== """ ref: https://stackoverflow.com/questions/13224362/principal-component-analysis-pca-in-python/49629816#49629816 @author: renfong """ import numpy as np import matplotlib.pyplot as plt import hyperspy.api as hs from numpy import argsort from numpy.linalg import eigh #%% load data data = hs.load('t1.dm3').data #%% define pca def pca(data, pc_count = None):     """     Principal component analysis using eigenvalues     note: this mean-centers and auto-scales the data (in-place)     """     C = np.dot(data.T, data)       # covariance matrix     E, V = eigh(C)     key = argsort(E)[::-1][:pc_count]     E, V = E[key], V[:, key]     U = data @ V  # equvalent to np.dot(data, V)     return U, E, V #%% reconstruc...

Simple Pushbuttons UI Template for dm-script

 // Pushbutton UI template // // 2021/04/21 // Renfong class UI_Handler : object { number true, false number UIObjectID void SetUIObjectID(object self, number id) { UIObjectID = id }; UI_Handler(object self) { true = 1; false = 0 result("Obect \"UI_Handler\" ["+self.ScriptObjectGetID()+"] constructed. \n") }; ~UI_Handler(object self) { true = 1; false = 0 result("Obect\"UI_Handler\" ["+self.ScriptObjectGetID()+"] deconstructed. \n") }; void btn1response(object self) { OKdialog("This is a template") }; }; class MainUI : UIFrame { TagGroup btn1 object UI_Handler number true, false, ver MainUI(object self){ true = 1; false = 0; ver=0.1; UI_Handler = alloc(UI_Handler) result("Obect \"MainUI\" ["+self.ScriptObjectGetID()+"] constructed. \n") }; ~MainUI(object self){ result("Obect \"MainUI\" ["+self.Scr...

numpy.polyfit example

 #%% load package import numpy as np import matplotlib.pyplot as plt #%% example 1: simple second order fitting # assume y=2x**2 xx = np.arange(-3,3,step=0.2) yy = 2*xx**2  yy +=  (np.random.random(len(xx))-0.5)  # add noise plt.figure(1, dpi=150) plt.plot(xx, yy, 'ro') # curve fitting lin_fit = np.polyfit(xx, yy, 2) print(lin_fit) x2 = np.arange(-3,3,step=.01) y2 = np.polyval(lin_fit, x2) plt.figure(1) plt.plot(x2,y2, 'k') plt.legend(['raw data','fitting curve']) plt.legend(['raw data','fitting curve']) plt.plot(x2[np.argmin(y2)], np.min(y2), 'b*', markersize=10) plt.text(x2[np.argmin(y2)], np.min(y2)-1,           'min: (%.2f, %.2f)'%(x2[np.argmin(y2)], np.min(y2))) plt.axis([-3,3,-2,20]) plt.show() #================================================= #%% example 2 # assume y-y0 = a*(x-x0)**2 # --> y = ax**2 - 2a*x0*x + a*x0**2 + y0 # let a=-1, x0=1.2, y0=0.7, i.e. the maximum value is located at (1.2, 0.7) # --> y = ...

Making a GUI in dm-script

 // 2 button dialog example  // To invert the most front image. // Modified from http://www.dmscripting.com/files/Example_Button_Enabling_Dialog.s // Renfong // 2021/03/24 // Global variables taggroup firstbutton, secondbutton number true=1 number false=0 image src // the class createbuttondialog is of the type user interface frame (UIFrame), and responds to interactions // with the dialog - in this case pressing buttons class CreateButtonDialog : uiframe { void button1response(object self) { //the response when the button is pressed self.SetElementIsEnabled("first", false); // these commands set the button as enabled or not enabled self.SetElementIsEnabled("second", true); // "second" in this command is the identifier of the button 'secondbutton' // put action1 here src.GetFrontImage() result(src.GetName()+" is picked.\n") }; void button2response(object self) { //the response when the second button is press...