Using peaksworkspace

Hi, I’m trying to set up a script in Workbench that reads in single-crystal peaks and does some calculations. I had two questions (I’m a python novice so apologies in advance for that).

What I’ve done:

  1. I successfully generated a single crystal peaksworkspace (by clicking in show instrument view).
  2. I successfully read this into the workbench editor using sxllst = mtd[‘SingleCrystalPeakTable’] .
  3. Mantid tells me this is a PeaksWorkspace object, so I read: Peaks Workspace)

Now my Q’s

  1. I can’t get the number of peaks in the workspace i.e. npk = sxllst.getNumberOfPeaks() returns "AttributeError: 'peaksWorkspace object has no attribute ‘getNumberOfPeaks’

  2. I am able to extract attributes of a single peak ‘n’ by using p = sxllst.getPeak(n) and can then pull out wavelength, d-spacing etc. by calling these attributes of p. However, I want to get the information for all of the peaks and store this in a single array. (e.g. a single array containing the d-spacings of all peaks). I tried various guesses sxllst.getPeak(n1:n2); sxllst.getPeaks(n1,n2) sxllst.getPeaks([n1 n2]) etc. but none of these worked!

Thanks!

Hi,

In answer to your first question, It seems that sxllst.getNumberPeaks() works as you intend and the documentation example highlighting .getNumberOfPeaks is incorrect (sorry!)

As for your second request to fill an array with certain properties, I have put together a small script which uses a for loop to access values from all of the peaks:

import numpy as np
from mantid.simpleapi import *

sxllst = mtd['SingleCrystalPeakTable']
npk = sxllst.getNumberPeaks()
print npk

num_properties = 5 # p, wl, tof, d, shape
array = np.zeros((npk,num_properties))
print array[0,0]

for i in range(npk):
    p = sxllst.getPeak(i) # locate the ith peak
    #get the individual properties of the ith peak
    array[i,0] = i
    array[i,1] = p.getWavelength()
    array[i,2] = p.getTOF()
    array[i,3] = p.getDSpacing()
    array[i,4] = p.getIntensity()

print "| Peak Number | Wavelength | Time of Flight | D-spacing | Intensity |"
print array

I hope this helps. Can I ask, is having these parameters stored in an array what you want or do you essentially want them in another tableworkspace?

Hi, thanks for the quick response! Also, thanks for the loop. The reason to extract all the peaks in the list is that I’m trying to adapt some matlab code I have which automatically indexes the peaks. The way that code is structured, it
has several subroutines that it’s necessary to pass the peaks data between as matrices.

Cheers, Malcolm

Okay, well if you’ve any more questions then please get in touch!