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

GeneratePythonScript v1

../_images/ImageNotFound.png

Enable screenshots using DOCS_SCREENSHOTS in CMake

Summary

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

See Also

RecordPythonScript, GenerateIPythonNotebook

This algorithm is also known as: ExportHistory

Properties

Name

Direction

Type

Default

Description

InputWorkspace

Input

Workspace

Mandatory

An input workspace.

Filename

Input

string

The name of the file into which the workspace history will be generated. Allowed extensions: [‘.py’]

ScriptText

Output

string

Saves the history of the workspace to a variable.

UnrollAll

Input

boolean

False

Unroll all algorithms to show just their child algorithms.

StartTimestamp

Input

string

The filter start time in the format YYYY-MM-DD HH:mm:ss

EndTimestamp

Input

string

The filter end time in the format YYYY-MM-DD HH:mm:ss

AppendTimestamp

Input

boolean

False

Appends the time the command was run as a comment afterwards

SpecifyAlgorithmVersions

Input

string

Specify Old

When to specify which algorithm version was used by Mantid. Allowed values: [‘Specify Old’, ‘Specify All’, ‘Specify None’]

IgnoreTheseAlgs

Input

str list

A list of algorithms to filter out of the built script

IgnoreTheseAlgProperties

Input

list of str lists

A list of algorithm properties to filter out of the built script

AppendExecCount

Input

boolean

False

Whether execCount should be appended to the end of the built string

Description

Retrieves the algorithm history of the workspace and saves it to a Python script file or Python variable.

A time range can be specified which will restrict the algorithms in the scrit to those which were executed between the given times, if no end time was specified then algorithms from the start time up to the current time will be included in the generated script.

Start and end times are given in ISO8601 format: YYYY-MM-DD HH:mm:ss, for example 3:25 PM on July the 4th 2014 would be 2014-07-04 15:25:00.

Usage

Example - generate a python script for a workspace:

#create a workspace and run some operations on it
ws = CreateSampleWorkspace()
ws = CropWorkspace(ws, XMin=7828.162291, XMax=11980.906921)
ws = Power(ws, Exponent=1.5)
ws = RenameWorkspace(ws, OutputWorkspace="MyTestWorkspace")

script_text = GeneratePythonScript(ws)
print(script_text.strip())

Output:

# Python script generated by Mantid
# Version ...
# SHA-1 ...

from mantid.simpleapi import *

CreateSampleWorkspace(OutputWorkspace='ws')
CropWorkspace(InputWorkspace='ws', OutputWorkspace='ws', XMin=7828.1622909999996, XMax=11980.906921)
Power(InputWorkspace='ws', OutputWorkspace='ws', Exponent=1.5)
RenameWorkspace(InputWorkspace='ws', OutputWorkspace='MyTestWorkspace')

Example - generate a python script giving a range of start times:

import time

# Do some operations on the workspace with a pause between them
ws = CreateSampleWorkspace()
ws = CropWorkspace(ws, XMin=7828.162291, XMax=11980.906921)
time.sleep(2)
ws = Power(ws, Exponent=1.5)
ws = RenameWorkspace(ws, OutputWorkspace="MyTestWorkspace")

# Get the execution time of the last algorithm and subtract 1 second
history = mtd['MyTestWorkspace'].getHistory()
last = history.getAlgorithmHistory(history.size() - 1)
from_time = last.executionDate() - int(1e9)

# Generate a script with a given start time
script_text = GeneratePythonScript(ws, StartTimestamp=str(from_time))
print(script_text.strip())

Output:

# Python script generated by Mantid
# Version ...
# SHA-1 ...

from mantid.simpleapi import *

Power(InputWorkspace='ws', OutputWorkspace='ws', Exponent=1.5)
RenameWorkspace(InputWorkspace='ws', OutputWorkspace='MyTestWorkspace')

Example - generate a python script and save it to file:

import os

#create a workspace and run some operations on it
ws = CreateSampleWorkspace()
ws = CropWorkspace(ws, XMin=7828.162291, XMax=11980.906921)
ws = Power(ws, Exponent=1.5)
ws = RenameWorkspace(ws, OutputWorkspace="MyTestWorkspace")

path = os.path.join(os.path.expanduser("~"), 'myscript.py')
GeneratePythonScript(ws, Filename=path)

with open (path, 'r') as script:
  print(script.read().strip())

Output:

# Python script generated by Mantid
# Version ...
# SHA-1 ...

from mantid.simpleapi import *

CreateSampleWorkspace(OutputWorkspace='ws')
CropWorkspace(InputWorkspace='ws', OutputWorkspace='ws', XMin=7828.1622909999996, XMax=11980.906921)
Power(InputWorkspace='ws', OutputWorkspace='ws', Exponent=1.5)
RenameWorkspace(InputWorkspace='ws', OutputWorkspace='MyTestWorkspace')

Categories: AlgorithmIndex | Utility\Python

Source

C++ header: GeneratePythonScript.h

C++ source: GeneratePythonScript.cpp