"""
This example script imports an Excel file and
creates a workset from that dataset.
"""
import umetrics
import os
import tempfile
from tkinter import messagebox, Tk
def show_message(message):
# Show a tkinter messagebox with the message.
tk_main=Tk()
tk_main.withdraw()
messagebox.showinfo(title='Create Project', message=message)
tk_main.destroy()
def run():
show_message('This script will:\n'+
'Read two sheets from an excel file.\n'+
'Create a new project with two datasets from the two sheets.\n'+
'Create and fit a PLS model from the two datasets.')
# Close the current open project
project = umetrics.SimcaApp.get_active_project()
if project.is_open :
if not umetrics.simca.ProjectHandler.close_project(project, True):
return
# The paths for data files and where the project should be saved
scriptpath = os.path.dirname(os.path.realpath(__file__))
datapath = scriptpath + r'\Data\Foods.xls'
projectpath = tempfile.gettempdir() + r'\CreateProject.usp'
if os.path.isfile(projectpath):
os.remove(projectpath)
# Create a setting to read first two sheets from the Excel file
# UseIndexes tells that the index of the sheets should be used instead of the sheet name
optionsSheet1 = ''
optionsSheet2 = ''
# Read the data sheets
sheet1 = umetrics.impdata.read_file(datapath, openoptions = optionsSheet1)
sheet2 = umetrics.impdata.read_file(datapath, openoptions = optionsSheet2)
# Specify primary and secondary ID
# Set first row to primary variable ID
sheet1.importspec.set_row_type(0, umetrics.impdata.ImportSpecification.rowtype.primaryvarid)
sheet2.importspec.set_row_type(0, umetrics.impdata.ImportSpecification.rowtype.primaryvarid)
# Set first colum to primary observation ID
sheet1.importspec.set_col_type(0, umetrics.impdata.ImportSpecification.columntype.primaryobsid)
sheet2.importspec.set_col_type(0, umetrics.impdata.ImportSpecification.columntype.primaryobsid)
# Set second colum to secondary observation ID
sheet1.importspec.set_col_type(1, umetrics.impdata.ImportSpecification.columntype.secondaryobsid)
sheet2.importspec.set_col_type(1, umetrics.impdata.ImportSpecification.columntype.secondaryobsid)
# Create a new project
project = umetrics.simca.ProjectHandler.create_project(projectpath)
# Create the dataset
ds_num1 = project.create_dataset(sheet1, "DS1")
ds_num2 = project.create_dataset(sheet2, "DS2")
# Set the project as the active project in SIMCA to be able to show plots
umetrics.SimcaApp.set_active_project(project)
# Create a workset for the model using the two datasets
workset = project.create_workset([ds_num1, ds_num2])
# Set the last variable to Y
numVars = workset.get_data_collection().size_variables()
workset.set_y(numVars - 1)
# Set the model typ to PLS
workset.set_type(umetrics.simca.modeltype.pls)
# Create the model and fit with three components
model = workset.create_model()
project.fit_model(model[0], numcomp=3)
project.save()
# Create Summary of fit plot and Score scatter plot
builder = umetrics.SimcaApp.plot_list_builder()
builder.score_scatter_plot()
if __name__ == "__main__":
run()