3.3.1 Easier to Use originpro Python Package Examples


Originlab is introducing a new Python package called "originpro" which is much easier to use then OriginExt or PyOrigin. The originpro package works on a higher level so user can make use of Origin workbooks, matrix and graphs with very little code.

This package will be preinstalled with the upcoming Origin2021, scheduled for Oct 2020 release. You can contact Originlab to request a beta version here

Python originpro Examples

Logistic Regression

In this example, we will show you how to send data in Origin's worksheet to Python's pandas DataFrame and send DataFrame back to Origin's worksheet

#This example requires to install statsmodels module

import pandas as pd
import statsmodels.api as sm

import originpro as op

#Send active worksheet data to Python's DataFrame
#Columns C and D in the worksheet are categorical columns 
wks = op.find_sheet( 'w' )
df = wks.to_df()

#Perfrom Logistic Regression in Python
cat_columns = df.select_dtypes(['category']).columns
df[cat_columns] = df[cat_columns].apply(lambda x: x.cat.codes)
df['intercept'] = 1.0

logit = sm.Logit(df['Career_Change'], df[['Age','Salary','Gender','intercept']])

result = logit.fit()

#Merge two DataFrames by index: result.params (Convert Series to DataFrame first) and result.conf_int()
res_df = pd.merge( result.params.to_frame(), result.conf_int(), right_index=True, left_index=True )
res_df.columns = ['Fitted Parameter', '95% CI Lower', '95% CI Upper']

#Send result to a new worksheet in Origin
wksR = op.new_sheet( 'w', 'Logistic Regression Result' )

#Send DataFrame (res_df) to worksheet (wksR)
#res_df includes three columns, and its index is row names, which should also be set to worksheet.

#Send DataFrame values to worksheet, and first column for DataFrame index.
wksR.from_df( res_df, 0, True )

Friedman's Super Smoother

In this example, we will show you how to send data in Origin's worksheet to Python's numpy, smooth data and send smoothed result back to Origin's worksheet.

#This example requires to install supersmoother and pandas packages
from supersmoother import SuperSmoother

import originpro as op
import numpy as np
import pandas as pd

#Get data from the active worksheet
wks = op.find_sheet('w')
cx = np.array( wks.to_list( 0 ) )
cy = np.array( wks.to_list( 1 ) )

#Perfrom Friedman's Super Smoother
#alpha: smoothing level, (0 < alpha < 10)
smoother = SuperSmoother( alpha = 2 )

#Fit the smoother
smoother.fit( cx, cy );

#Predict the smoothed function for inpit x
yfit = smoother.predict( cx )

#Output the smoothed result to worksheet in Origin.
df = pd.DataFrame( {'Smoothed': yfit} )
wks.from_df( df, 2 )

Extract Colors from an Image

This example lets you extract all of the colors from an image such as a graph image from a publication, and send the RGB values back to Origin's worksheet.

#This example requires to install numpy and extcolors packages
import tkinter as tk
from tkinter import filedialog
import extcolors
import numpy as np
import originpro as org

#select image from Open File dialog
root = tk.Tk()
file_path = filedialog.askopenfilename(title = "Select Image", filetypes = [("png files", "*.png"), ("jpg files", "*.jpg"), ("bmp files", "*.bmp"), ("jpeg files", "*.jpeg")])                                     

#extract colors
colors, pixel_count = extcolors.extract(file_path)

except AttributeError:
print("Please select an image first!")

colors = np.array(colors)
rgb,pixel = map(list, zip(*colors))
r = []
g = []
b = []
for row in rgb:
#output colors
wks = org.find_sheet('w')
wks.from_list(0, r)
wks.from_list(1, g)
wks.from_list(2, b)

Lomb-Scargle Periodogram

This example performs Lomb-Scargle Periodogram analysis for a non-uniformly sampled signal, and the power spectral density result is presented in the graph.

#This example requires to install scipy, pandas packages.

import originpro as op

import numpy as np
from scipy.signal import lombscargle

import pandas as pd

#Import XY data from columns in worksheet 
wks = op.find_sheet('w')
t = np.array( wks.to_list( 0 ) )
y = np.array( wks.to_list( 1 ) )

#Calculate angular frequency as input for lombscargle function
n = t.size
ofac = 4  # Oversampling factor
hifac = 1
T = t[-1] - t[0]
Ts = T / (n - 1)
nout = np.round(0.5 * ofac * hifac * n) 
f = np.arange(1, nout+1) / (n * Ts * ofac)
f_ang = f * 2 * np.pi

#Perform Lomb-Scargle periodogram analysis
pxx = lombscargle(t, y, f_ang, precenter=True)
pxx = pxx / f[-1] #PSD type

#f unit: mHz, PSD unit: dB/Hz
f = 1000*f
pxx = 10*np.log10( pxx ) 
df = pd.DataFrame( {'Frequency':f, 'Power/Frequency':pxx} )
#Send Python's DataFrame to the worksheet in Origin
wks.from_df( df, 2 )