-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #4333. IPeaksWorkspace, TableWorkspace and history to Python.
- Loading branch information
1 parent
9775b28
commit 4ac1adb
Showing
12 changed files
with
231 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
Code/Mantid/Framework/PythonInterface/mantid/api/src/IPeak.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "MantidAPI/IPeak.h" | ||
#include <boost/python/class.hpp> | ||
#include <boost/python/register_ptr_to_python.hpp> | ||
|
||
using Mantid::API::IPeak; | ||
using namespace boost::python; | ||
|
||
void export_IPeak() | ||
{ | ||
register_ptr_to_python<IPeak*>(); | ||
|
||
class_<IPeak, boost::noncopyable>("IPeak", no_init) | ||
.def("getDetectorID", &IPeak::getDetectorID, "Get the ID of the detector at the center of the peak") | ||
.def("setDetectorID", &IPeak::setDetectorID, "Set the detector ID and look up and cache values related to it.") | ||
.def("getRunNumber", &IPeak::getRunNumber, "Return the run number this peak was measured at") | ||
.def("setRunNumber", &IPeak::setRunNumber, "Set the run number that measured this peak") | ||
.def("getH", &IPeak::getH, "Get the H index of the peak") | ||
.def("getK", &IPeak::getK, "Get the K index of the peak") | ||
.def("getL", &IPeak::getL, "Get the L index of the peak") | ||
.def("getHKL", &IPeak::getHKL, "Get HKL as a V3D object") | ||
.def("setHKL", (void (IPeak::*)(double,double,double)) &IPeak::setHKL, "Set the HKL values of this peak") | ||
.def("setH", &IPeak::setH, "Get the H index of the peak") | ||
.def("setK", &IPeak::setK, "Get the K index of the peak") | ||
.def("setL", &IPeak::setL, "Get the L index of the peak") | ||
.def("getQLabFrame", &IPeak::getQLabFrame, "Return the Q change (of the lattice, k_i - k_f) for this peak.\n" | ||
"The Q is in the Lab frame: the goniometer rotation was NOT taken out.\n" | ||
"Note: There is no 2*pi factor used, so |Q| = 1/wavelength.") | ||
.def("getQSampleFrame", &IPeak::getQSampleFrame, "Return the Q change (of the lattice, k_i - k_f) for this peak." | ||
"The Q is in the Sample frame: the goniometer rotation WAS taken out. ") | ||
.def("setQLabFrame", &IPeak::setQLabFrame, "Set the peak using the peak's position in reciprocal space, in the lab frame.") | ||
.def("setQSampleFrame", &IPeak::setQSampleFrame, "Set the peak using the peak's position in reciprocal space, in the sample frame.") | ||
.def("setWavelength", &IPeak::setWavelength, "Set the incident wavelength of the neutron. Calculates the energy from this assuming elastic scattering.") | ||
.def("getWavelength", &IPeak::getWavelength, "Return the incident wavelength") | ||
.def("getScattering", &IPeak::getScattering, "Calculate the scattering angle of the peak") | ||
.def("getDSpacing", &IPeak::getDSpacing, "Calculate the d-spacing of the peak, in 1/Angstroms") | ||
.def("getTOF", &IPeak::getTOF, "Calculate the time of flight (in microseconds) of the neutrons for this peak") | ||
.def("getInitialEnergy", &IPeak::getInitialEnergy, "Get the initial (incident) neutron energy") | ||
.def("getFinalEnergy", &IPeak::getFinalEnergy, "Get the final neutron energy") | ||
.def("setInitialEnergy", &IPeak::setInitialEnergy, "Set the initial (incident) neutron energy") | ||
.def("setFinalEnergy", &IPeak::setFinalEnergy, "Set the final neutron energy") | ||
.def("getIntensity", &IPeak::getIntensity, "Return the integrated peak intensity") | ||
.def("getSigmaIntensity", &IPeak::getSigmaIntensity, "Return the error on the integrated peak intensity") | ||
.def("setIntensity", &IPeak::setIntensity, "Set the integrated peak intensity") | ||
.def("setSigmaIntensity", &IPeak::setSigmaIntensity, "Set the error on the integrated peak intensity") | ||
.def("getBinCount", &IPeak::getBinCount, "Return the # of counts in the bin at its peak") | ||
.def("setBinCount", &IPeak::setBinCount, "Set the # of counts in the bin at its peak") | ||
.def("getRow", &IPeak::getRow, "For RectangularDetectors only, returns the row (y) of the pixel of the detector.") | ||
.def("getCol", &IPeak::getCol, "For RectangularDetectors only, returns the column (x) of the pixel of the detector.") | ||
.def("getDetPos", &IPeak::getDetPos, "Return the detector position vector") | ||
.def("getL1", &IPeak::getL1, "Return the L1 flight path length (source to sample), in meters. ") | ||
.def("getL2", &IPeak::getL2, "Return the L2 flight path length (sample to detector), in meters.") | ||
; | ||
} | ||
|
32 changes: 32 additions & 0 deletions
32
Code/Mantid/Framework/PythonInterface/mantid/api/src/IPeaksWorkspace.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include "MantidAPI/IPeaksWorkspace.h" | ||
#include "MantidAPI/IPeak.h" | ||
#include "MantidPythonInterface/kernel/SingleValueTypeHandler.h" | ||
#include <boost/python/class.hpp> | ||
#include <boost/python/register_ptr_to_python.hpp> | ||
#include <boost/python/return_internal_reference.hpp> | ||
|
||
using Mantid::API::IPeaksWorkspace; | ||
using Mantid::API::IPeaksWorkspace_sptr; | ||
using Mantid::API::IPeak; | ||
using Mantid::API::ExperimentInfo; | ||
using Mantid::API::ITableWorkspace; | ||
|
||
using namespace boost::python; | ||
|
||
void export_IPeaksWorkspace() | ||
{ | ||
register_ptr_to_python<IPeaksWorkspace_sptr>(); | ||
|
||
// IPeaksWorkspace class | ||
class_< IPeaksWorkspace, bases<ITableWorkspace, ExperimentInfo>, boost::noncopyable >("IPeaksWorkspace", no_init) | ||
.def("getNumberPeaks", &IPeaksWorkspace::getNumberPeaks, "Returns the number of peaks within the workspace") | ||
.def("addPeak", &IPeaksWorkspace::addPeak, "Add a peak to the workspace") | ||
.def("removePeak", &IPeaksWorkspace::removePeak, "Remove a peak from the workspace") | ||
.def("getPeak", &IPeaksWorkspace::getPeakPtr, return_internal_reference<>(), "Returns a peak at the given index" ) | ||
.def("createPeak", &IPeaksWorkspace::createPeak, return_internal_reference<>(), "Create a Peak and return it") | ||
; | ||
|
||
DECLARE_SINGLEVALUETYPEHANDLER(IPeaksWorkspace, Mantid::Kernel::DataItem_sptr); | ||
|
||
} | ||
|
20 changes: 20 additions & 0 deletions
20
Code/Mantid/Framework/PythonInterface/mantid/api/src/ITableWorkspace.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#include "MantidAPI/ITableWorkspace.h" | ||
#include "MantidPythonInterface/kernel/SingleValueTypeHandler.h" | ||
#include <boost/python/class.hpp> | ||
#include <boost/python/register_ptr_to_python.hpp> | ||
|
||
using Mantid::API::ITableWorkspace; | ||
using Mantid::API::ITableWorkspace_sptr; | ||
using Mantid::API::Workspace; | ||
using namespace boost::python; | ||
|
||
void export_ITableWorkspace() | ||
{ | ||
register_ptr_to_python<ITableWorkspace_sptr>(); | ||
|
||
class_<ITableWorkspace,bases<Workspace>, boost::noncopyable>("ITableWorkspace", no_init) | ||
; | ||
|
||
DECLARE_SINGLEVALUETYPEHANDLER(ITableWorkspace, Mantid::Kernel::DataItem_sptr); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
Code/Mantid/Framework/PythonInterface/mantid/api/src/WorkspaceHistory.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include "MantidAPI/WorkspaceHistory.h" | ||
#include "MantidAPI/IAlgorithm.h" | ||
#include <boost/python/class.hpp> | ||
#include <boost/python/self.hpp> | ||
#include <boost/python/operators.hpp> | ||
#include <boost/python/register_ptr_to_python.hpp> | ||
|
||
using Mantid::API::WorkspaceHistory; | ||
using Mantid::API::IAlgorithm; | ||
using namespace boost::python; | ||
|
||
void export_WorkspaceHistory() | ||
{ | ||
register_ptr_to_python<WorkspaceHistory*>(); | ||
|
||
class_<WorkspaceHistory, boost::noncopyable>("WorkspaceHistory", no_init) | ||
.def("lastAlgorithm", &WorkspaceHistory::lastAlgorithm, "Returns the last algorithm run on this workspace so that its properties can be accessed") | ||
.def("getAlgorithm", &WorkspaceHistory::getAlgorithm, "Returns the algorithm at the given index in the history") | ||
// ----------------- Operators -------------------------------------- | ||
.def("__getitem__", &WorkspaceHistory::getAlgorithm) | ||
.def(self_ns::str(self)) | ||
; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
Code/Mantid/Framework/PythonInterface/test/python/IPeaksWorkspaceTest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import unittest | ||
from testhelpers import run_algorithm | ||
from mantid import mtd, IPeaksWorkspace, V3D | ||
|
||
class IPeaksWorkspaceTest(unittest.TestCase): | ||
""" | ||
Test the python interface to PeaksWorkspace's | ||
""" | ||
|
||
def setUp(self): | ||
run_algorithm('LoadEventNexus',Filename='CNCS_7860_event.nxs', OutputWorkspace='cncs', | ||
FilterByTimeStart=60.0,FilterByTimeStop=60.5,LoadMonitors=False) | ||
run_algorithm('CreatePeaksWorkspace',InstrumentWorkspace='cncs', OutputWorkspace='peaks') | ||
|
||
def test_interface(self): | ||
""" Rudimentary test to get peak and get/set some values """ | ||
pws = mtd['peaks'] | ||
self.assertTrue(isinstance(pws, IPeaksWorkspace)) | ||
self.assertEqual(pws.getNumberPeaks(), 1) | ||
p = pws.getPeak(0) | ||
|
||
# Try a few IPeak get/setters. Not everything. | ||
p.setH(234) | ||
self.assertEqual(p.getH(), 234) | ||
p.setHKL(5,6,7) | ||
self.assertEqual(p.getH(), 5) | ||
self.assertEqual(p.getK(), 6) | ||
self.assertEqual(p.getL(), 7) | ||
|
||
hkl = p.getHKL() | ||
self.assertEquals(hkl, V3D(5,6,7)) | ||
|
||
p.setIntensity(456) | ||
p.setSigmaIntensity(789) | ||
self.assertEqual(p.getIntensity(), 456) | ||
self.assertEqual(p.getSigmaIntensity(), 789) | ||
|
||
# Finally try to remove a peak | ||
pws.removePeak(0) | ||
self.assertEqual(pws.getNumberPeaks(), 0) | ||
|
||
# Create a new peak at some Q in the lab frame | ||
qlab = V3D(1,2,3) | ||
p = pws.createPeak(qlab, 1.54) | ||
self.assertAlmostEquals( p.getQLabFrame().X(), 1.0, 3) | ||
|
||
# Now try to add the peak back | ||
pws.addPeak(p) | ||
self.assertEqual(pws.getNumberPeaks(), 1) | ||
|
||
# Check that it is what we added to it | ||
p = pws.getPeak(0) | ||
self.assertAlmostEquals( p.getQLabFrame().X(), 1.0, 3) | ||
|
||
mtd.remove('cncs') | ||
mtd.remove('peaks') | ||
|
||
if __name__ == '__main__': | ||
unittest.main() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters