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

Python in Mantid: Exercise 2

The aim of this exercise is to create a general script that can accept user interaction to alter the parameters for future uses

Perform a set of algorithms by pointing and clicking within Mantid Use the history window to generate a script based upon the executed algorithms Use the dialog functions to make the script more general for future runs.

A - Processing ISIS Data

Running an analysis Manually

Here you are going to perform a number of steps in the Mantid user interface to correct a LOQ dataset using the incident beam monitor. For more information in a specific algorithm, click on its name.

  1. Load v1 - Use the LOQ48097.raw file, set the OutputWorkspace to Small_Angle and set LoadMonitors to Separate.
  2. ConvertUnits v1 - Convert the units for the Monitor workspace to wavelength with EMode=Elastic.
  3. ConvertUnits v1 - Do the same for the small_angle data with EMode=Elastic.
  4. Rebin v1 - Rebin the monitor from 2.2 to 10 in log steps of 0.035. To achieve this, the rebin params string will be “2.2,-0.035,10.0”.
  5. Rebin v1 - Do the same for the small_angle data.
  6. ExtractSingleSpectrum v1 - Use WorkspaceIndex=1 on the Small_Angle_monitors workspace to pull out the 2nd monitor spectrum, putting the answer back into the input workspace.
  7. Divide v1 - Run divide with LHSWorkspace as the small_angle data workspace & RHSWorkspace as the monitor, into an OutputWorkspace called Corrected_data.

Generating a script from a workspace

Once you have performed an analysis you can easily save it out as a script

  1. Right-Click on the final result workspace, then select Show History
  2. Click on the Script to Clipboard button
  3. Paste the code into the Script Editor
  4. Delete all the entries in the Workspace Toolbox
  5. Run the script to check it works!

One step further

  1. Extract the binning params from the first Rebin, print them using the logger, AND use them as the input for the second Rebin
  2. Add comments to your script to explain what it does!

B - Plotting ILL Data

Load and Plot

  1. Load v1 the file 164198.nxs
  2. Double-click on the main data workspace 164198
  3. Produce a normal plot of the spectrum numbers: 50,100,200,300

Edit in Options Menu

  1. Click on the Gear icon to open the Options menu
  2. In the Axes tab… Change the title to “My Beautiful Plot”
  3. Set the x-limits to 460-600, the y-scale to log and the y-limits to 1-2000
  4. In the Curves tab… Select spectrum 50 and give this curve “a funky label”
  5. … for the re-labelled curve, un-hide the error bars and set Capsize = 1.0
  6. … Select spectrum 300 and change the line color to black
  7. Click Apply

Generating a script from a plot

  1. Click the generate a script button GenerateAScript.png and copy&paste the script into the script editor
  2. Close your beautiful plot and run the script to re-generate it!

C - Processing and Plotting SNS Data

Running an analysis Manually

  1. Load v1 - Load the file EQSANS_6071_event.nxs.
  2. ConvertUnits v1 - Convert the units for both the monitor and detector workspaces to wavelength
  3. Rebin v1 - Rebin both workspaces in wavelength from 2.5 to 5.5 in linear steps of 0.1
  4. SumSpectra v1 - Sum up all the detectors to give the beam profile
  5. Divide v1 - Normalize the rebinned and summed spectra in wavelength by the monitors

Generating a script from a workspace

Once you have performed an analysis you can easily save it out as a script.

  1. Right-Click on the final result workspace normalized, then select Show History
  2. Click on the Script to File button and save the file somewhere useful!
  3. Open the script by selecting File > Open Script or use the keyboard shortcut Ctrl+O (or Cmd+O)
  4. Delete all workspaces and Run the script to check it works!

Plot and Edit

  1. Double-click on the normalized spectrum to plot it
  2. Right-click on run_monitors_lambda_rebinned and select Plot > Overplot Spectrum.
  3. Also, overplot with run_lambda_summed
  4. In the Options menu (Gear icon)
  5. Set the x-upper-limit to 4.5
  6. Set the y-scale to log
  7. Click Apply

Generating a script from a plot

  1. Click the generate a script button GenerateAScript.png and select Script to Clipboard
  2. Paste the code on the end of the script for processing the data
  3. Delete all workspaces and plots and run the script to reprocess and plot the data

Solutions