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:


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

# Example script for use with the 'Python Script - Image to XY [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.
#
# For scripts made for this plug-in the returned data should be reduced from a 2D to a 1D array,
# furthermore removing the yaxis title, whilst not essential, will not result in an error
#
# 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 1D
    randomData = numpy.random.randn(x)

    # 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)

    # Give new titles for everything
    kwargs['data_title'] = "Random Data"
    kwargs['xaxis_title'] = "New X-Axis Title"

    # Return the dictionary to DAWN
    return kwargs

XY to XY

XY data input, returning XY data, converting the data to noise

import numpy as np

def run(xaxis, 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
	return script_outputs

XY data input, returning XY data of random noise of the same length, overwriting axes, data and titles

# Example script for use with the 'Python Script - XY to XY [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 1D 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 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 1D array
# 'mask' = A mask, indicating if there are any values not to evaluate, as a 1D boolean array
# 'yaxis' = The y axis values, i.e. the y axis scale
#
# 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, = 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 1D
    randomData = numpy.random.randn(x)

    # 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)

    # Give new titles for everything
    kwargs['data_title'] = "Random Data"
    kwargs['xaxis_title'] = "New X-Axis Title"
    
    # Return the dictionary to DAWN
    return kwargs

XY small angle scattering data input, returning XY small angle scattering data transformed into a Guinier plot

# Example script for use with the 'Python Script - XY to XY [Scripting]' processing plug-in.
# 
# Last updated 2016-11-02 - For DAWN 2.3
#
# 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.
#
# Below is a simple example where reduced SAXS data is taken in and converted to display a Guinier plot

# Always handy to have numpy
import numpy as np


# 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 and xaxis from the dictionary
    data = kwargs['data']
    xaxis = kwargs['xaxis']

    # Do some 'error' handling
    if 'error' in kwargs:
        del kwargs['error']

    # Do the required mathematics on the data
    guinier = np.log(data)
    guinier_x = np.power(xaxis,2)

    # Set the plot titles and data values
    kwargs['data_title'] = 'ln(I)'
    kwargs['data'] = guinier
    kwargs['xaxis_title'] = 'q^2'
    kwargs['xaxis'] = guinier_x

    return kwargs

XY small angle scattering data input, returning XY small angle scattering data transformed into a Kratky plot

# Example script for use with the 'Python Script - XY to XY [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.
#
# Below is a simple example where reduced SAXS data is taken in and converted to display a Kratky plot

# Always handy to have numpy
import numpy as np


# 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 and xaxis from the dictionary
 data = kwargs['data']
 xaxis = kwargs['xaxis']

 # Do some 'error' handling
 if 'error' in kwargs:
 del kwargs['error']

 # Do the required mathematics on the data
 kratky = np.power(xaxis,2)*data

 # Set the plot titles and data values
 kwargs['data_title'] = 'I*q^2'
 kwargs['data'] = kratky
 kwargs['xaxis_title'] = 'q'

 return kwargs

XY small angle scattering data input, returning XY small angle scattering data transformed into a Porod plot

# Example script for use with the 'Python Script - XY to XY [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.
#
# Below is a simple example where reduced SAXS data is taken in and converted to display a Porod plot

# Always handy to have numpy
import numpy as np


# 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 and xaxis from the dictionary
    data = kwargs['data']
    xaxis = kwargs['xaxis']

    # Do some 'error' handling
    if 'error' in kwargs:
        del kwargs['error']

    # Do the required mathematics on the data
    porod = np.power(xaxis, 4) * data
    porod_x = np.power(xaxis, 4)

    # Set the plot titles and data values
    kwargs['data_title'] = 'I*q^4'
    kwargs['data'] = porod
    kwargs['xaxis_title'] = 'q^4'
    kwargs['xaxis'] = porod_x

    # Return the data
    return kwargs