Skip to content

Commit

Permalink
Refs #4333. Export FileFinder and Sample classes to Python.
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Dec 20, 2011
1 parent ae614cc commit 683b65a
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Code/Mantid/Framework/PythonInterface/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ set ( TEST_PY_FILES
test/python/AlgorithmManagerTest.py
test/python/AnalysisDataServiceTest.py
test/python/ConfigServiceTest.py
test/python/ExperimentInfoTest.py
test/python/FilePropertyTest.py
test/python/FileFinderTest.py
test/python/FrameworkManagerTest.py
test/python/IComponentTest.py
test/python/IEventWorkspaceTest.py
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ set ( EXPORT_FILES
src/Axis.cpp
src/IPeak.cpp
src/BoxController.cpp
src/FileFinder.cpp
src/Sample.cpp
)

# Files containing additional helper code that are not related to exporting class/functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ void export_ExperimentInfo()

class_<ExperimentInfo, boost::noncopyable>("ExperimentInfo", no_init)
.def("getInstrument", &ExperimentInfo::getInstrument, "Returns the instrument for this run")
.def("sample", &ExperimentInfo::sample, return_value_policy<copy_const_reference>(),
.def("sample", &ExperimentInfo::sample, return_value_policy<reference_existing_object>(),
"Return the Sample object. This cannot be modified, use mutableSample to modify.")
.def("mutableSample", &ExperimentInfo::mutableSample, return_value_policy<reference_existing_object>(),
"Return a modifiable Sample object.")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "MantidAPI/FileFinder.h"
#include <boost/python/class.hpp>
#include <boost/python/reference_existing_object.hpp>

using Mantid::API::FileFinder;
using Mantid::API::FileFinderImpl;
using namespace boost::python;

void export_FileFinder()
{
class_<FileFinderImpl, boost::noncopyable>("FileFinder", no_init)
.def("Instance", &FileFinder::Instance, return_value_policy<reference_existing_object>(),
"Returns a reference to the FileFinder singleton instance")
.staticmethod("Instance")
.def("getFullPath", &FileFinderImpl::getFullPath,
"Return a full path to the given file if it can be found within datasearch.directories paths. "
"An empty string is returned otherwise.")
.def("findRuns", &FileFinderImpl::findRuns, "Find a list of files file given a hint. "
"The hint can be a comma separated list of run numbers and can also include ranges of runs, e.g. 123-135 or equivalently 123-35"
"If no instrument prefix is given then the current default is used.")
;
}

33 changes: 33 additions & 0 deletions Code/Mantid/Framework/PythonInterface/mantid/api/src/Sample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "MantidAPI/Sample.h"
#include "MantidGeometry/Crystal/OrientedLattice.h"
#include <boost/python/class.hpp>
#include <boost/python/copy_const_reference.hpp>
#include <boost/python/register_ptr_to_python.hpp>

using Mantid::API::Sample;
using Mantid::Geometry::OrientedLattice;
using namespace boost::python;

void export_Sample()
{
register_ptr_to_python<Sample*>();
register_ptr_to_python<boost::shared_ptr<Sample> >();


class_< Sample, boost::noncopyable >("Sample", no_init)
.def("getName", &Sample::getName, return_value_policy<copy_const_reference>(), "Returns the string name of the sample")
.def("getOrientedLattice", (const OrientedLattice & (Sample::*)() const)&Sample::getOrientedLattice,
return_value_policy<copy_const_reference>(), "Get the oriented lattice for this sample")
.def("hasOrientedLattice", &Sample::hasOrientedLattice, "Returns True if this sample has an oriented lattice, false otherwise")
.def("size", &Sample::size, "Return the number of samples contained within this sample")
// Required for ISIS SANS reduction until the full sample geometry is defined on loading
.def("getGeometryFlag", &Sample::getGeometryFlag, "Return the geometry flag.")
.def("getThickness", &Sample::getThickness, "Return the thickness in mm")
.def("getHeight", &Sample::getHeight, "Return the height in mm")
.def("getWidth", &Sample::getWidth, "Return the width in mm")
// -------------------------Operators -------------------------------------
.def("__len__", &Sample::size)
.def("__getitem__", &Sample::operator[], return_internal_reference<>())
;
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <boost/python/register_ptr_to_python.hpp>

using Mantid::Geometry::Instrument;
using Mantid::Geometry::Instrument_sptr;
using Mantid::Geometry::Instrument_const_sptr;
using Mantid::Geometry::CompAssembly;
using Mantid::Geometry::IObjComponent;
using Mantid::Geometry::IDetector;
Expand All @@ -12,7 +14,8 @@ using namespace boost::python;

void export_Instrument()
{
register_ptr_to_python<boost::shared_ptr<Instrument> >();
register_ptr_to_python<Instrument_sptr>();
register_ptr_to_python<Instrument_const_sptr>();

class_<Instrument, bases<CompAssembly>, boost::noncopyable>("Instrument", no_init)
.def("getSample", (boost::shared_ptr<IObjComponent> (Instrument::*)())&Instrument::getSample,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@
###############################################################################
from testhelpers import run_algorithm
from mantid.geometry import Instrument
from mantid.api import Sample

class ExperimentInfoTest(unittest.TestCase):

_expt_ws = None

def setUp(self):
if self.__class__._expt_ws is None:
alg = run_algorithm('Load', Filename='LOQ48127.raw', SpectrumMax=1, child=True)
self.__class__._expt_ws = alg.getProperty("OutputWorkspace").value

def test_information_access(self):
alg = run_algorithm('Load', Filename='LOQ48127.raw', SpectrumMax=2, child=True)
expt = alg.getProperty("OutputWorkspace")
inst = expt.getInstrument()
self.assertTrue(isinstance(Instrument))
self.assertEquals(expt.getRunNumber(), 48127)
inst = self._expt_ws.getInstrument()
self.assertTrue(isinstance(inst, Instrument))
self.assertEquals(self._expt_ws.getRunNumber(), 48127)

def test_sample_access_returns_sample_object(self):
sample = self._expt_ws.sample()
self.assertTrue(isinstance(sample, Sample))
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import unittest
from mantid.api import FileFinder
import os

class FileFinderTest(unittest.TestCase):

def test_full_path_returns_an_absolute_path_and_the_files_exists(self):
path = FileFinder.Instance().getFullPath("CNCS_7860_event.nxs")
self.assertTrue(len(path) > 0)
# We can't be sure what the full path is in general but it should certainly exist!
self.assertTrue(os.path.exists(path))

def test_find_runs_returns_absolute_paths_of_given_runs(self):
runs = FileFinder.Instance().findRuns("CNCS7860")
self.assertTrue(len(runs) == 1)
# We can't be sure what the full path is in general but it should certainly exist!
self.assertTrue(os.path.exists(runs[0]))

0 comments on commit 683b65a

Please sign in to comment.