Strange behavior of ConvertUnits

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.

These are the Unit types that ConvertUnits knows how to handle.

Hi Nick, thanks for the explanation,
I did not recognize that “Label” is the unitID, now this makes sense.

Apparently I still do not understand it, this is not working:
from mantid.simpleapi import *
ws = CreateSampleWorkspace()
axis = ws.getAxis(0)
out = ConvertUnits(ws, Target=‘Degrees’)

Hi Thomas,

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:

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!

Ah, so ConvertUnits can only handle “TOF convertible” units.

“Available non-TOF Convertible units” means not convertible at all. Because I thought they are convertible as long as you do not convert to TOF.

This is really confusing, since the Documentation of ConvertUnits, says you can convert to “Degrees”:

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.

Yes that’s the case. I’m a little confused as to why that is on the ConvertUnits documentation. I’ll get that changed to avoid confusion.

ConvertAxisByFormula seems to be appropirate, assuming you have a fixed wavelength value?