This page provides example Python scripts for:
- Processing plug-ins:
Python Script - Image to Image [Scripting]
1) Take an image (with axes) and return 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
2) Take an image and return an image of random noise of the same dimensions, overwriting axes, data and titles (click here to download)
# Example script for use with the 'Python Script - Image to Image [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. # # 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
Python Script - Image to XY [Scripting]
1) Take an image (with axes) and return XY data (with axes) converting 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
2) Take a 2D image and return 1D XY data of random noise of the same length as the image y-axis, overwriting axes, data and titles (click here to download)
# Example script for use with the 'Python Script - Image 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. # # 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
Python Script - XY to XY [Scripting]
Quick links:
1) Take XY data, and return XY data converting data to noise
2) Take XY data and return XY data of random noise of the same length, overwriting axes, data and titles (click here to download)
# 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. # # 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
3) Take small angle scattering I vs q data and transform this to produce a Guinier plot (click here to download)
# 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
4) Take small angle scattering I vs q data and transform this to produce a Kratky plot (click here to download)
# 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 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
5) Take small angle scattering I vs q data and transform this to produce a Porod plot (click here to download)
# 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 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