"""
This example script imports an Excel file and
creates a predictionset from that dataset.
"""
import umetrics
import os
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='Import Predictionset', message=message)
tk_main.destroy()
def run():
show_message('This script imports data to an existing project and sets it as the current predictionset.')
# 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 path for the data file and where the project is located
scriptpath = os.path.dirname(os.path.realpath(__file__))
datapath = scriptpath + r'\Data\Foods_predset.xls'
projectpath = scriptpath + r'\Data\Importpredset.usp'
# UseIndexes tells that the index of the sheets should be used instead of the sheet name
sheetOptions = ''
# Open the project
project = umetrics.simca.ProjectHandler.open_project(projectpath)
# Set the project as the active project in SIMCA to be able to create plots
umetrics.SimcaApp.set_active_project(project)
datafile = umetrics.impdata.read_file(datapath, openoptions = sheetOptions)
# Specify primary and secondary ID
# Set first row to primary variable ID
datafile.importspec.set_row_type(0, umetrics.impdata.ImportSpecification.rowtype.primaryvarid)
# Set first colum to primary observation ID
datafile.importspec.set_col_type(0, umetrics.impdata.ImportSpecification.columntype.primaryobsid)
# Set second colum to secondary observation ID
datafile.importspec.set_col_type(1, umetrics.impdata.ImportSpecification.columntype.secondaryobsid)
# Create the dataset
ds_num1 = project.create_dataset(datafile, "DS_As_Predset")
# Create a prediction set using the dataset for the active model
active_model = project.get_active_model()
if (active_model == 0) :
raise LookupError('No active model')
# Create a new predictionset from the dataset
predset = project.create_predictionset(active_model)
predset.as_dataset(ds_num1)
predset.save("MyPredset")
# Set predictionset as current predictionset to use
project.set_predictionset("MyPredset")
# Create some prediction plots
if (project.is_model_fitted(active_model) == False) :
project.fit_model(active_model)
builder = umetrics.SimcaApp.plot_list_builder()
builder.dmod_column_plot(umetrics.SimcaApp.PlotListBuilder.DModType.DModXPS)
builder.predicted_score_scatter_plot()
if __name__ == "__main__":
run()