DeltaPDF3D algorithm

Hello,

I am trying to use the DeltaPDF3D algorithm in Diffraction/Utilities which requires an MDHistoWorkspace as input, dimensions must be [H,0,0], [0,K,0] and [0,0,L].
I fail to get the required input using createMDHistoWorkspace since the algorithm requires comma separated dimension names.
So
Names="[H,0,0], [0,K,0], [0,0,L]"
obviously fails while
Names=“H,K,L”
works.
I tried playing with single and double quotes without success.
What I am doing wrong?
How to get it right?

Thanks
Best
Ruggero

Another user has run into this problem too, but great to have this on the forum! I think the way Names chooses how many inputs there are is annoying. This short example seems to work:

# import mantid algorithms, numpy and matplotlib
from mantid.simpleapi import *
import matplotlib.pyplot as plt
import numpy as np
 
S  =range(0,1000);
ERR=range(0,1000);
 
DeltaPDF3D_MDH=CreateMDHistoWorkspace(Dimensionality=3,Extents='-3,3,-10,10,-1,1',SignalInput=S,ErrorInput=ERR,\
                          NumberOfBins='10,10,10',Names=('[H,0,0]','[0,K,0]','[0,0,L]'),\
                          Units='rlu,rlu,rlu')
                         
DeltaPDF3D(InputWorkspace='DeltaPDF3D_MDH',OutputWorkspace='fft',
           Method='None', WindowFunction='None')

Tell me if this works for you or not, and feel free to get in touch with any other questions!

Thanks!
Indeed didn’t think about the tuple.
Maybe it is enough to update the documentation.
It seems to work, I do not get that error anymore.

But I’ve got another error since DeltaPDF3D requires astropy which is apparently missing.
How to install it into Mantid?
Or, alternatively I have astropy installed in my system, how make Mantid this aware of that?

Last question, I guess it is possible to import Mantid tree n the system Python via sys.path.append(…)
Which would be the path to append in a MacOS?

Thanks
Ruggero

Great, I will update the documentation to reflect this. I’m not too familiar with this algorithm so I’ll do some digging to see why astropy is missing! One thing you can try is installing astropy into the mantid python. I’ve posted the instructions for doing this separately as this is a rather common request: Install Python packages with pip

Tell me if you have any other questions!

Hello,

one more question on the usage of the DeltaPDF3D algorithm.
I would like to save the OutputWorkspace and IntermediateWorkspace using SaveMD, without using the GUI functionality, so in a python script.

With reference to the online documentation, suppose the call is as follows:
ws = DeltaPDF3D(InputWorkspace=‘DeltaPDF3D_MDH’,OutputWorkspace=‘fft4’, IntermediateWorkspace=‘int4’, Method=‘Punch and fill’, Size=0.3, CropSphere=True, SphereMax=3, Convolution=True, WindowFunction=‘None’)

Then the
SaveMD(ws, Filename=‘somename.h5’)

does not work and gives:
ValueError: When converting parameter “InputWorkspace”: Unable to extract shared_ptr from Python object…

However if I save using the GUI, it works as expected.

Thanks

Hi,
Your script is very close. SaveMD wants the name of the workspaces which appear in the WorkspacesToolbox. So you could use:

DeltaPDF3D(InputWorkspace=‘DeltaPDF3D_MDH’,OutputWorkspace=‘fft4’, IntermediateWorkspace=‘int4’, Method=‘Punch and fill’, Size=0.3, CropSphere=True, SphereMax=3, Convolution=True, WindowFunction=‘None’)

SaveMD('fft4', Filename = 'fft4.h5')
SaveMD('int4', Filename = 'int4.h5')

Tell me if that works for you!

PS. I’ve removed the “ws = ” from the DeltaPDF3D line as this is often used to create the OutputWorkspace name, but instead you have it defined as a parameter (OutputWorkspace=‘fft4’) which is nice and we don’t need both.

Hi,

thanks
I still get error:

SaveMD-[Notice] SaveMD started
SaveMD-[Error] Error in execution of algorithm SaveMD:
SaveMD-[Error] NXopen(/home/lqmr/X/BN57-01-16rec_25p5_0p1_sub_spf_kpf.h5, 5) failed
Traceback (most recent call last):
File “/opt/Mantid/lib/mantid/simpleapi.py”, line 1041, in call
algm.execute()
RuntimeError: NXopen(/home/lqmr/X/BN57-01-16rec_25p5_0p1_sub_spf_kpf.h5, 5) failed

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “mantid_deltaPDF.py”, line 32, in
SaveMD(‘BN57_pf’, Filename=fout1)
File “/opt/Mantid/lib/mantid/simpleapi.py”, line 1048, in call
raise RuntimeError(msg) from e
RuntimeError: SaveMD-v2: NXopen(/home/lqmr/X/BN57-01-16rec_25p5_0p1_sub_spf_kpf.h5, 5) failed

fout1 is just a string.
It is not clear to me what the runtime error is.

Thanks

It almost looks like the underscore is being parsed incorrectly. Can you try an output string without an underscore, such as "BN57pf". Do you still get the same error?

Can you try directly providing the path and try without the underscore, so:

SaveMD("BN57pf", "/home/lqmr/Desktop/BN57pf.h5")

Alternatively, can you use the Algorithm Dialog, by typing SaveMD in the Algorithm Toolbox and clicking Execute, to see if this method works!?

I tried without underscore and with the full path, and both fail with the same error.

Using the Algorithm dialog it works.

I just wanted to make a script to run in the system python via

from mantid.simpleapi import *

Try the script in my latest post below :arrow_down:

I see the same NXopen error as you on MacOS, that’s why I wasn’t seeing it on Ubuntu!

Okay, this works for me on MacOS. It seems you need to either set the “Default Save Directory” in File > ManageUserDirectories or define the full path to save. Can you try this script (but obviously use the correct path for your machine):

# import mantid algorithms, numpy and matplotlib
from mantid.simpleapi import *
import matplotlib.pyplot as plt
import numpy as np
 
DeltaPDF3D_MDE = CreateMDWorkspace(Dimensions='3', Extents='-3.1,3.1,-3.1,3.1,-0.1,0.1', Names='[H,0,0],[0,K,0],[0,0,L]',
                                  Units='rlu,rlu,rlu', SplitInto='4',Frames='HKL,HKL,HKL')
# Add some Bragg peaks
for h in range(-3,4):
  for k in range(-3,4):
     FakeMDEventData(DeltaPDF3D_MDE, PeakParams='100,'+str(h)+','+str(k)+',0,0.01', RandomSeed='1337')
# Add addiontal peaks on [0.5,0.5,0.5] type positions
# This would correspond to negative substitutional correlations
for h in [-2.5,-1.5,-0.5,0.5,1.5,2.5]:
  for k in range(-3,4):
     FakeMDEventData(DeltaPDF3D_MDE, PeakParams='20,'+str(h)+','+str(k)+',0,0.1', RandomSeed='13337')

BinMD(InputWorkspace='DeltaPDF3D_MDE', AlignedDim0='[H,0,0],-3.05,3.05,61', AlignedDim1='[0,K,0],-3.05,3.05,61',
     AlignedDim2='[0,0,L],-0.1,0.1,1', OutputWorkspace='DeltaPDF3D_MDH')
           
DeltaPDF3D(InputWorkspace='DeltaPDF3D_MDH',OutputWorkspace='fft_4',IntermediateWorkspace='int_4',
           Method='Punch and fill',Size=0.3,CropSphere=True,SphereMax=3,Convolution=True, WindowFunction='None')

SaveMD(Filename='/Users/danielmurphy/Desktop/fft_4.h5', InputWorkspace='fft_4')
SaveMD(Filename='/Users/danielmurphy/Desktop/int_4.h5', InputWorkspace='int_4')

Thanks.

I was actually on Ubuntu 18.04 on Windows using the Windows Linux Subsystem.
But the error was in the fact that I (user) didn’t have write permission on the folder I was trying to write in to. I did not understand why…
I am really sorry for the mistake.

On MacOs, I am not able to import the mantid path.

In [1]: import sys

In [2]: sys.path.append(r’/Applications/MantidWorkbench.app/Contents/MacOS/’)

In [3]: from mantid.simpleapi import *

SystemError Traceback (most recent call last)
in
----> 1 from mantid.simpleapi import *

/Applications/MantidWorkbench.app/Contents/MacOS/mantid/init.py in
103 # registry will be missing entries if all are not loaded.
104 ###############################################################################
→ 105 from mantid import kernel as _kernel
106 from mantid import api as _api
107 from mantid import geometry as _geometry

/Applications/MantidWorkbench.app/Contents/MacOS/mantid/kernel/init.py in
34
35 # insert all the classes from _kernel in the mantid.kernel namespace
—> 36 import_mantid_cext(’._kernel’, ‘mantid.kernel’, globals())
37
38 from mantid.kernel._aliases import *

/Applications/MantidWorkbench.app/Contents/MacOS/mantid/utils/_utils.py in import_mantid_cext(modulename, package, caller_globals)
35 # import from PACKAGE.MODULE, this is used for mantid packages, where the .pyd files
36 # are placed at e.g. from mantid.kernel import _kernel
—> 37 lib = import_module(modulename, package)
38 except ImportError as e1:
39 # import relative to current working directory, this simulates doing import _kernel

/opt/local/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/init.py in import_module(name, package)
125 break
126 level += 1
→ 127 return _bootstrap._gcd_import(name[level:], package, level)
128
129

SystemError: initialization of _kernel raised unreported exception

Should I import a different Mantid path?
I am using python 3.8, installed via MacPorts, should I use/install another python3.8?

Thanks

If possible you should use the python bundled with MantidWorkbench. Launch with:

/Applications/MantidWorkbench.app/Contents/MacOS/mantidpython --classic

Other commands to launch mantidpython from terminal are here: MantidPython and Notebook