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

RunPythonScript v1

../_images/RunPythonScript-v1_dlg.png

RunPythonScript dialog.

Summary

Executes a snippet of Python code

Properties

Name Direction Type Default Description
InputWorkspace Input Workspace   An input workspace that the python code will modify.The workspace will be in the python variable named ‘input’.
Code Input string   Python code (can be on multiple lines).
Filename Input string   A File containing a python script. Allowed values: [‘py’]
OutputWorkspace Output Workspace   An output workspace to be produced by the python code.The workspace will be in the python variable named ‘output’.

Description

Algorithm that will run a snippet of python code. This is meant to be used by LoadLiveData v1 to perform some processing.

The input & output workspaces can be accessed from the Python code using the variable names input and output respectively.

Within your code input is an actual reference to the input workspace, while output is a string of the output workspace name. You are expected to create the output workspace yourself.

Because of boilerplate code added to the python script being run, stack-traces for errors will be five (5) lines greater than where the error occurred in the supplied python code.

Usage

Example - Summing all the counts in a workspace

ws = CreateSampleWorkspace("Histogram","Multiple Peaks")

script = """
from mantid.simpleapi import Integration, SumSpectra, logger
logger.notice('Logging from within the python script!')
wsOut = Integration(input,OutputWorkspace=output)
wsOut = SumSpectra(wsOut)
"""

RunPythonScript(InputWorkspace=ws,Code=script,OutputWorkspace="wsOut")

wsOut = mtd["wsOut"]
print("The workspace contained a total of {:d} counts".format(int(wsOut.readY(0)[0])))

Output:

The workspace contained a total of 9599 counts

The same script could be saved to a file and specified using the Filename property instead of the Code property.

Categories: AlgorithmIndex | DataHandling\LiveData\Support

Source

C++ header: RunPythonScript.h (last modified: 2020-04-07)

C++ source: RunPythonScript.cpp (last modified: 2020-06-06)