\(\renewcommand\AA{\unicode{x212B}}\)

RecordPythonScript v1

../_images/ImageNotFound.png

Enable screenshots using DOCS_SCREENSHOTS in CMake

Warning

RecordPythonScript is deprecated. Use GeneratePythonScript version 1 instead.

Summary

An Algorithm to generate a Python script file to reproduce the history of a workspace.

See Also

GenerateIPythonNotebook, GeneratePythonScript

Properties

Name

Direction

Type

Default

Description

Filename

Input

string

Mandatory

The file into which the Python script will be generated. Allowed extensions: [‘.py’]

This algorithm records the python commans of any algoithms that execute while it is running and saves them out to a file.

This is similar to extracting the history of a workspace, but outputs evey algorithm that occurs during it’s execution rather than just for a specific workspace.

The file is written out once this algotithm is cancelled, either using the running algorithm details button or via python.

Usage

Example:

from threading import Thread
import os, time

#find a suitable directory to save the file
fileDir = config["defaultsave.directory"]
if not os.path.isdir(fileDir):
    #use the users home directory if default save is not set
    fileDir = os.path.expanduser('~')
outputFile = os.path.join(fileDir,"MyRecording.py")

def startRecording():
    try:
        RecordPythonScript(outputFile)
    except RuntimeError:
        pass
thread = Thread(target = startRecording)
thread.start()

# a short pause to allow the thread to start
time.sleep(0.1)

ws = CreateSampleWorkspace("Event","Multiple Peaks")
wsOut=CreateFlatEventWorkspace(ws,RangeStart=15000,RangeEnd=18000)
wsOut=RebinToWorkspace(wsOut,ws,PreserveEvents=True)

# This will cancel the rocording algorithm
# you can do the same in the GUI
# by clicking on the details button on the bottom right
AlgorithmManager.cancelAll()
thread.join()

#Load and print the resulting file
print("The result file has the following python recorded")
with open(outputFile, "r") as file:
    print(file.read().rstrip())

#cleanup
os.remove(outputFile)

Output:

The result file has the following python recorded
CreateSampleWorkspace(OutputWorkspace='ws',WorkspaceType='Event',Function='Multiple Peaks',UserDefinedFunction='',XUnit='TOF',XMin='0',XMax='20000',BinWidth='200',NumEvents='1000',Random='0',NumScanPoints='1',InstrumentName='basic_rect',NumBanks='2',NumMonitors='0',BankPixelWidth='10',PixelDiameter='0.0080000000000000002',PixelHeight='0.00020000000000000001',PixelSpacing='0.0080000000000000002',BankDistanceFromSample='5',SourceDistanceFromSample='10')
CreateFlatEventWorkspace(InputWorkspace='ws',RangeStart='15000',RangeEnd='18000',OutputWorkspace='wsOut')
RebinToWorkspace(WorkspaceToRebin='wsOut',WorkspaceToMatch='ws',OutputWorkspace='wsOut',PreserveEvents='1')

Categories: AlgorithmIndex | Utility\Python | Deprecated

Source

C++ header: RecordPythonScript.h

C++ source: RecordPythonScript.cpp