Could Someone Help me with Script for Data Analysis in Mantid?

Hello there, :wave:

I am new to using Mantid and am currently trying to analyze some data for my research project. I have a specific task that I am hoping to accomplish using a script; but I am having trouble figuring out the right commands and workflow.

My dataset consists of neutron scattering data; and I need to perform some basic data reduction steps. Specifically; I would like to normalize the data; apply a background correction; and then visualize the results using a plot. I have read through the documentation and have found some examples; but I am struggling to piece everything together into a coherent script.

I have been using LoadNexus to import my .nxs files; but I am unsure if I need to set any specific parameters for my dataset.

Also, I have gone through this post; https://forum.mantidproject.org/t/outputcompositemembers-in-fit-with-histogram-evaluation-type-integration-error-mlops/ which definitely helped me out a lot.

I would appreciate any tips on applying background corrections effectively. Are there specific functions or methods in Mantid that are particularly useful for this? :thinking:

Thank you in advance for your help and assistance. :innocent:

Hi Robert,

Apologies for the delay in getting back to you, hopefully I’ll be more useful from now onwards. :innocent:

If you haven’t already, I would start by looking at this page on running algorithms in Python of our Python in Mantid course.

(I’ll post the rest of my answer below because there is a limit of two links for a new post)

The Mantid basic course might also contain some useful information, in particular this page, which shows how you can produce a script automatically to recreate your workflows.

From what you have mentioned, it looks like the script you are looking for might look like this:

# import mantid algorithms, numpy and matplotlib
from mantid.simpleapi import *
import matplotlib.pyplot as plt
import numpy as np
from mantid.plots._compatability import plotSpectrum #import needed outside Workbench

'''Processing'''

# Load the spectral range of the interest of the data
ws=Load(Filename="GEM40979.raw", SpectrumMin=451, SpectrumMax=650)

# Create a workspace with Flat Background
wsFlat = CreateSampleWorkspace("Histogram","Flat background")
wsFlat = wsFlat * 10

# Remove Background
ws_no_bg = RemoveBackground(InputWorkspace=ws, BkgWorkspace=wsFlat)

# Convert to dSpacing
ws_norm=NormaliseSpectra(InputWorkspace=ws_no_bg)

'''Plotting'''

# Plot three spectra
plotSpectrum(ws_norm, [0,1,2])

# Get Figure and Axes for formatting
fig, axes = plt.gcf(), plt.gca()

# Alter the Y label
axes.set_ylabel('Neutron Counts ($\AA$)$^{-1}$')

# Optionally rename the curve titles
axes.legend(["bank2 sp-431", "bank2 sp-432", "bank2 sp-433", "bank2 sp-436"])

# Give the plot a title
plt.title("Example", fontsize=20)

Please keep in mind that I just put this together as an example, with disregard for physical correctness.

Using LoadNexus is fine, any parameters you set should not change the values of the data.

As for background corrections, from what I could find it looks like RemoveBackground could be quite useful?

If you need further assistance, could let me know which instrument nexus files are you loading into Mantid? I could get some members of my team from that particular science area to comment on this thread :relieved: