Python Scripting within Processing - Example Scripts
A brief YouTube overview of how to use Python scripting from within DAWN’s processing perspective can be found below
Â
Â
Building on this tutorial, this page outlines example Python scripts for:
Â
- 1 Image to Image
- 2 Image to XY
- 3 XY to XY
- 3.1 XY data input, returning XY data, converting the data to noise
- 3.2 XY data input, returning XY data of random noise of the same length, overwriting axes, data and titles
- 3.3 XY small angle scattering data input, returning XY small angle scattering data transformed into a Guinier plot
- 3.4 XY small angle scattering data input, returning XY small angle scattering data transformed into a Kratky plot
- 3.5 XY small angle scattering data input, returning XY small angle scattering data transformed into a Porod plot
Image to Image
Image with axes input, returning an image with axes
Â
import numpy as np
def run(xaxis, yaxis, data, **kwargs):
#generate random noise the same shape as the data
noise = np.random.rand(*data.shape)
#add it to the output dictionary
script_outputs = {"data":noise}
#also return the x-axis
script_outputs["xaxis"] = xaxis
#and the y-axis
script_outputs["yaxis"] = yaxis
return script_outputs
Image input, returning an image of random noise of the same dimensions, overwriting axes, data and titles
Â
# Example script for use with the 'Python Script - Image to Image [Scripting]' processing plug-in.
#
# Last updated 2016-11-02
#
# Copyright (c) 2016 Diamond Light Source Ltd.
#
#
# The DAWN Python PyDev actor will execute this script, invoking a 'run' method in this file.
# This actor will pass the data and any associated variables from the dataset, which we will
# catch as a dictionary.
#
# The dictionary keys passed for the XY to XY plugin are always:
#
# 'current_slice' = The current slice of the data, typically [0, Frame Number, X Length]
# 'data' = The data, as a 2D array
# 'data_dimensions' = The dimensionality of the data
# 'data_title' = The title of the data
# 'dataset_name' = By default, the NeXus path to the dataset, or a given title of the dataset
# 'file_path' = The path to the file
# 'total' = Number of frames passed
# 'xaxis' = The x axis values, i.e. the x axis scale
# 'xaxis_title' = The title of the x-axis values/scale
# 'yaxis' = The y axis values, i.e. the y axis scale
# 'yaxis_title' = The title of the y-axis values/scale
#
# with potentially one or more of the following, depending on the dataset passed:
#
# 'auxiliary' = Any auxiliary data associated with the dataset
# 'error' = Any error values for the data, as a 2D array
# 'mask' = A mask, indicating if there are any values not to evaluate, as a 2D boolean array
#
# The PyDev actor will expect, at least, all of the first set of keys to be returned.
# However, it will accept any of the second set of keys being inserted or removed to/from
# the returned dictionary.
#
# An error will be thrown back to the processing perspective GUI if the script does not execute.
#
# Below is a simple example where random data, of the same size to the input is returned
# complete with all the potential additional keys added as well.
# Always handy to have numpy
import numpy
# The method the the PyDev actor will call, we'll catch any and all arguements as a dictionary
def run(**kwargs):
# Extract out the data
data = kwargs['data']
# and find it's length
x, y = data.shape
# Generate some random data the same length as the input
# N.B. The output data length can be different to the input but must be 2D
randomData = numpy.random.randn(x, y)
# Generate some error values for the plot
randomError = numpy.abs(randomData) * 10
# Generate some data values as well
randomData = numpy.abs(randomData) * 100
# Input the error values into the input dictionary
kwargs['data'] = randomData
# Input the data values into the input dictionary
kwargs['error'] = randomError
# Delineate some axes so that the axis titles plot
# (No data in the xaxis/yaxis variables means no titles!)
kwargs['xaxis'] = numpy.arange(0, x)
kwargs['yaxis'] = numpy.arange(0, y)
# Give new titles for everything
kwargs['data_title'] = "Random Data"
kwargs['xaxis_title'] = "New X-Axis Title"
kwargs['yaxis_title'] = "New Y-Axis Title"
# Return the dictionary to DAWN
return kwargs
Image to XY
Â
Image with axes input, returning XY data with axes, converting the data to noise
Â
import numpy as np
def run(xaxis, yaxis, data, **kwargs):
#generate random noise the same shape as the data
noise = np.random.rand(data.shape[1])
#add it to the output dictionary
script_outputs = {"data":noise}
#also return the x-axis
script_outputs["xaxis"] = xaxis
return script_outputs
Image input, returning XY data of random noise of the same length as the image y-axis, overwriting axes, data and titles
Â
XY to XY
Â
XY data input, returning XY data, converting the data to noise
Â
XY data input, returning XY data of random noise of the same length, overwriting axes, data and titles
Â
XY small angle scattering data input, returning XY small angle scattering data transformed into a Guinier plot
Â
XY small angle scattering data input, returning XY small angle scattering data transformed into a Kratky plot
XY small angle scattering data input, returning XY small angle scattering data transformed into a Porod plot
Â