Hi, I am trying to convert some Units of a workspace axis and always get an error that the workspace does not have units, even if I set the units by hand.
I made some minimal example, below.
Am I doing something wrong in setting the units?
from mantid.simpleapi import *
ws = CreateSampleWorkspace()
axis = ws.getAxis(0)
unit = axis.getUnit()
out = ConvertUnits(ws, Target='TOF') ## works
axis.setUnit("Label").setLabel(unit.caption(), unit.symbol())
out = ConvertUnits(ws, Target='TOF') # crashes
the last line gives:
ValueError: Invalid value for property InputWorkspace (MatrixWorkspace) from string “ws”: The workspace must have units
Mantid version is either newest stable or newest nightly.
The Label property type is for when you just want to specify text labels alongside your data. We use it when we want to label the data after fitting with “Fit” and “residuals” and things like that.
ConvertUnits uses the type of the Unit class, rather than the Symbol to determine how to convert the units, and has no idea what to do with the text Label type.
Apparently I still do not understand it, this is not working:
from mantid.simpleapi import *
ws = CreateSampleWorkspace()
axis = ws.getAxis(0)
axis.setUnit(“Degrees”)
out = ConvertUnits(ws, Target=‘Degrees’)
The last two lines on that last example clash. Your script Creates the Sample Workspace, Sets a Label on an Axis as “Degrees”, which is probably not scientifically correct, and then tries to ConvertUnits from Degrees to Degrees.
It would be useful to know what exactly you are trying to do as these simple examples limit what we can help you with. If you’d prefer to email we can continue discussing this via: daniel.murphy@stfc.ac.uk
On your final line, ConvertUnits fails because “Degrees” isn’t one of the options in the list of Units: Units so it is not invloved in the Unit Conversions we offer.
Hope to hear from you soon to see how we can help!
The name of the units to convert to (must be one of those registered in the Unit Factory). Allowed values: [‘Degrees’, ‘DeltaE’, ‘DeltaE_inFrequency’, ‘DeltaE_inWavenumber’, ‘dSpacing’, ‘dSpacingPerpendicular’, ‘Empty’, ‘Energy’, ‘Energy_inWavenumber’, ‘Label’, ‘Momentum’, ‘MomentumTransfer’, ‘QSquared’, ‘SpinEchoLength’, ‘SpinEchoTime’, ‘Temperature’, ‘Time’, ‘TOF’, ‘Wavelength’]
What I am really trying to do is the following:
I have a MatrixWorkspace which was created with ConvertMDHistoToMatrixWorkspace and has Scattering Angle as X-axis, now I would like to have the X-axis changed to q or d-spacing.
Seems ConvertAxisByFormula is better suited. https://docs.mantidproject.org/nightly/algorithms/ConvertAxisByFormula-v1.html