Skip to content

Commit

Permalink
refs #5083 Change construction logic
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenArnold committed Apr 11, 2012
1 parent c6a9ae0 commit 281aa37
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 38 deletions.
8 changes: 4 additions & 4 deletions Code/Mantid/Framework/API/src/MatrixWorkspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ namespace Mantid
{
throw Kernel::Exception::NotFoundError("MatrixWorkspace::getNeighbours - Cannot find spectrum number for detector", comp->getID());
}
std::map<specid_t, V3D> neighbours = m_nearestNeighbours->neighbours(spectra[0], radius);
std::map<specid_t, V3D> neighbours = m_nearestNeighbours->neighboursInRadius(spectra[0], radius);
return neighbours;
}

Expand All @@ -393,7 +393,7 @@ namespace Mantid
{
m_nearestNeighbours.reset(m_nearestNeighboursFactory->create(this->getInstrument(), *m_spectraMap, ignoreMaskedDetectors));
}
std::map<specid_t, V3D> neighbours = m_nearestNeighbours->neighbours(spec, radius);
std::map<specid_t, V3D> neighbours = m_nearestNeighbours->neighboursInRadius(spec, radius);
return neighbours;
}

Expand All @@ -409,9 +409,9 @@ namespace Mantid
{
if ( !m_nearestNeighbours )
{
m_nearestNeighbours.reset(m_nearestNeighboursFactory->create(this->getInstrument(), *m_spectraMap, ignoreMaskedDetectors));
m_nearestNeighbours.reset(m_nearestNeighboursFactory->create(nNeighbours, this->getInstrument(), *m_spectraMap, ignoreMaskedDetectors));
}
std::map<specid_t, V3D> neighbours = m_nearestNeighbours->neighbours(spec, false, nNeighbours);
std::map<specid_t, V3D> neighbours = m_nearestNeighbours->neighbours(spec);
return neighbours;
}

Expand Down
19 changes: 9 additions & 10 deletions Code/Mantid/Framework/API/test/MatrixWorkspaceTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,28 +434,27 @@ class MatrixWorkspaceTest : public CxxTest::TestSuite
//Create a nearest neighbours product, which can be returned.
SpectrumDistanceMap map;
MockNearestNeighbours* product = new MockNearestNeighbours;
EXPECT_CALL(*product, neighbours(_,_,_)).WillRepeatedly(Return(map));
EXPECT_CALL(*product, neighbours(_)).WillRepeatedly(Return(map));
EXPECT_CALL(*product, die()).Times(1); //Created once and destroyed once!

//Create a factory, for generating the nearest neighbour products
MockNearestNeighboursFactory* factory = new MockNearestNeighboursFactory;
EXPECT_CALL(*factory, create(_,_,_)).Times(1).WillOnce(Return(product));
EXPECT_CALL(*factory, create(_,_,_,_)).Times(1).WillOnce(Return(product));

WorkspaceTester wkspace(factory);
wkspace.initialize(1,4,3);
wkspace.getNeighboursExact(0, 1); //First call should construct nearest neighbours before calling ::neighbours
wkspace.getNeighboursExact(0, 1); //Second call should not construct nearest neighbours before calling ::neighbours

}

void test_get_neighbours_radius()
{
//Create a nearest neighbours product, which can be returned.
SpectrumDistanceMap map;
MockNearestNeighbours* product = new MockNearestNeighbours;
EXPECT_CALL(*product, neighbours(_,_)).WillRepeatedly(Return(map));
EXPECT_CALL(*product, neighboursInRadius(_,_)).WillRepeatedly(Return(map));
EXPECT_CALL(*product, die()).Times(1); //Created once and destroyed once!

//Create a factory, for generating the nearest neighbour products
MockNearestNeighboursFactory* factory = new MockNearestNeighboursFactory;
EXPECT_CALL(*factory, create(_,_,_)).Times(1).WillOnce(Return(product));
Expand All @@ -471,7 +470,7 @@ class MatrixWorkspaceTest : public CxxTest::TestSuite
//Create a nearest neighbours product, which can be returned.
SpectrumDistanceMap map;
MockNearestNeighbours* product = new MockNearestNeighbours;
EXPECT_CALL(*product, neighbours(_,_)).WillRepeatedly(Return(map));
EXPECT_CALL(*product, neighboursInRadius(_,_)).WillRepeatedly(Return(map));
EXPECT_CALL(*product, die()).Times(1); //Should be explicitly called upon reset.

//Create a factory, for generating the nearest neighbour products
Expand All @@ -492,15 +491,15 @@ class MatrixWorkspaceTest : public CxxTest::TestSuite
SpectrumDistanceMap mapA, mapB, mapC;

MockNearestNeighbours* productA = new MockNearestNeighbours;
EXPECT_CALL(*productA, neighbours(_,_)).WillRepeatedly(Return(mapA));
EXPECT_CALL(*productA, neighboursInRadius(_,_)).WillRepeatedly(Return(mapA));
EXPECT_CALL(*productA, die()).Times(1);

MockNearestNeighbours* productB = new MockNearestNeighbours;
EXPECT_CALL(*productB, neighbours(_,_)).WillRepeatedly(Return(mapB));
EXPECT_CALL(*productB, neighboursInRadius(_,_)).WillRepeatedly(Return(mapB));
EXPECT_CALL(*productB, die()).Times(1);

MockNearestNeighbours* productC = new MockNearestNeighbours;
EXPECT_CALL(*productC, neighbours(_,_)).WillRepeatedly(Return(mapC));
EXPECT_CALL(*productC, neighboursInRadius(_,_)).WillRepeatedly(Return(mapC));
EXPECT_CALL(*productC, die()).Times(1);

//Create a factory, for generating the nearest neighbour products
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ namespace Mantid
virtual ~INearestNeighbours() {};

// Neighbouring spectra by radius
virtual std::map<specid_t, Mantid::Kernel::V3D> neighbours(specid_t spectrum, double radius=0.0) const = 0;
virtual std::map<specid_t, Mantid::Kernel::V3D> neighboursInRadius(specid_t spectrum, double radius=0.0) const = 0;

// Neighbouring spectra by exact number of neighbours
virtual std::map<specid_t, Mantid::Kernel::V3D> neighbours(specid_t spectrum, bool force, int numberofneighbours=8) const = 0;
virtual std::map<specid_t, Mantid::Kernel::V3D> neighbours(specid_t spectrum) const = 0;
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ namespace Mantid
/// Factory method
virtual INearestNeighbours* create(boost::shared_ptr<const Instrument> instrument,
const ISpectraDetectorMap & spectraMap, bool ignoreMasked=false) = 0;
/// Factory method
virtual INearestNeighbours* create(int numberOfNeighbours, boost::shared_ptr<const Instrument> instrument,
const ISpectraDetectorMap & spectraMapbool, bool ignoreMasked=false) = 0;
/// Destructor
virtual ~INearestNeighboursFactory(){};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,19 @@ namespace Mantid
/// Constructor with an instrument and a spectra map
NearestNeighbours(boost::shared_ptr<const Instrument> instrument,
const ISpectraDetectorMap & spectraMap, bool ignoreMasked=true);

/// Constructor with an instrument and a spectra map and number of neighbours
NearestNeighbours(int nNeighbours, boost::shared_ptr<const Instrument> instrument,
const ISpectraDetectorMap & spectraMap, bool ignoreMasked=true);

/// Default (empty) destructor
virtual ~NearestNeighbours() {};

// Neighbouring spectra by radius
std::map<specid_t, Mantid::Kernel::V3D> neighbours(specid_t spectrum, double radius=0.0) const;
std::map<specid_t, Mantid::Kernel::V3D> neighboursInRadius(specid_t spectrum, double radius=0.0) const;

// Neighbouring spectra by
std::map<specid_t, Mantid::Kernel::V3D> neighbours(specid_t spectrum, bool force, int numberofneighbours=8) const;
std::map<specid_t, Mantid::Kernel::V3D> neighbours(specid_t spectrum) const;

protected:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ namespace Geometry
/// Factory Method
NearestNeighbours* create(boost::shared_ptr<const Instrument> instrument,
const ISpectraDetectorMap & spectraMap, bool ignoreMasked=false);
/// Factory Method
NearestNeighbours* create(int numberOfNeighbours, boost::shared_ptr<const Instrument> instrument,
const ISpectraDetectorMap & spectraMap, bool ignoreMasked=false);
/// Destructor
virtual ~NearestNeighboursFactory();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ namespace Mantid
using Mantid::detid_t;
using Kernel::V3D;

/**

/**
* Constructor
* @param instrument :: A shared pointer to Instrument object
* @param spectraMap :: A reference to the spectra-detector mapping
Expand All @@ -29,6 +30,21 @@ namespace Mantid
this->build(m_noNeighbours);
}


/**
* Constructor
* @param nNeighbours :: Number of neighbours to use
* @param instrument :: A shared pointer to Instrument object
* @param spectraMap :: A reference to the spectra-detector mapping
* @param ignoreMaskedDetectors :: flag indicating that masked detectors should be ignored.
*/
NearestNeighbours::NearestNeighbours(int nNeighbours, boost::shared_ptr<const Instrument> instrument,
const ISpectraDetectorMap & spectraMap, bool ignoreMaskedDetectors) :
m_instrument(instrument), m_spectraMap(spectraMap), m_noNeighbours(nNeighbours), m_cutoff(-DBL_MAX), m_scale(), m_radius(0), m_bIgnoreMaskedDetectors(ignoreMaskedDetectors)
{
this->build(m_noNeighbours);
}

/**
* Returns a map of the spectrum numbers to the distances for the nearest neighbours.
* @param spectrum :: Spectrum ID of the central pixel
Expand All @@ -37,12 +53,8 @@ namespace Mantid
* @return map of Detector ID's to distance
* @throw NotFoundError if component is not recognised as a detector
*/
std::map<specid_t, V3D> NearestNeighbours::neighbours(const specid_t spectrum, bool force, const int noNeighbours) const
std::map<specid_t, V3D> NearestNeighbours::neighbours(const specid_t spectrum) const
{
if(force || m_noNeighbours != int(noNeighbours))
{
const_cast<NearestNeighbours*>(this)->build(noNeighbours);
}
return defaultNeighbours(spectrum);
}

Expand All @@ -53,7 +65,7 @@ namespace Mantid
* @return map of Detector ID's to distance
* @throw NotFoundError if component is not recognised as a detector
*/
std::map<specid_t, V3D> NearestNeighbours::neighbours(const specid_t spectrum, const double radius) const
std::map<specid_t, V3D> NearestNeighbours::neighboursInRadius(const specid_t spectrum, const double radius) const
{
// If the radius is stupid then don't let it continue as well be stuck forever
if( radius < 0.0 || radius > 10.0 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,18 @@ namespace Geometry
return new NearestNeighbours(instrument, spectraMap, ignoreMasked);
}


/*
Factory Method
@param numberOfNeighbours : Number of neighbours.
@param instrument : Instrument containing detectors
@param spectraMap : Spectra to detector map.
@param ignoreMasked : True to ignore masked detectors
*/
NearestNeighbours* NearestNeighboursFactory::create(int numberOfNeighbours, boost::shared_ptr<const Instrument> instrument,
const ISpectraDetectorMap & spectraMap, bool ignoreMasked)
{
return new NearestNeighbours(numberOfNeighbours, instrument, spectraMap, ignoreMasked);
}

} // namespace Mantid
} // namespace Geometry
20 changes: 10 additions & 10 deletions Code/Mantid/Framework/Geometry/test/NearestNeighboursTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ class NearestNeighboursTest : public CxxTest::TestSuite
Instrument_sptr m_instrument(new Instrument(instrument, pmap));

// Create the NearestNeighbours object directly.
NearestNeighbours nn(m_instrument, *spectramap);
NearestNeighbours nn(actualNeighboursNumber, m_instrument, *spectramap);

// Check distances calculated in NearestNeighbours compare with those using getDistance on component
std::map<specid_t, V3D> distances = nn.neighbours(14, true, actualNeighboursNumber);
std::map<specid_t, V3D> distances = nn.neighbours(14);

// We should have 8 neighbours when not specifying a range.
TS_ASSERT_EQUALS(expectedNeighboursNumber, distances.size());
Expand Down Expand Up @@ -112,11 +112,11 @@ class NearestNeighboursTest : public CxxTest::TestSuite

// Check that the 'radius' option works as expected
// Lower radius
distances = nn.neighbours(14, 0.008);
distances = nn.neighboursInRadius(14, 0.008);
TS_ASSERT_EQUALS(distances.size(), 4);

// Higher than currently computed
distances = nn.neighbours(14, 6.0);
distances = nn.neighboursInRadius(14, 6.0);
TS_ASSERT_EQUALS(distances.size(), 17);
}

Expand Down Expand Up @@ -157,11 +157,11 @@ class NearestNeighboursTest : public CxxTest::TestSuite

// Too close!
specid_t spec = 256 + 2*16+3; // This gives the spectrum number for this detector
nb = nn.neighbours(spec, 0.003);
nb = nn.neighboursInRadius(spec, 0.003);
TS_ASSERT_EQUALS( nb.size(), 0 );

// The ones above below and next to it
nb = nn.neighbours(spec, 0.016);
nb = nn.neighboursInRadius(spec, 0.016);
TS_ASSERT_EQUALS( nb.size(), 4 );

}
Expand Down Expand Up @@ -223,7 +223,7 @@ class NearestNeighboursTestPerformance : public CxxTest::TestSuite
NearestNeighbours nn(m_instrument, *spectramap);
for(size_t i = 0; i < 2000; i++)
{
nn.neighbours(1, 5.0);
nn.neighboursInRadius(1, 5.0);
}
}

Expand All @@ -240,7 +240,7 @@ class NearestNeighboursTestPerformance : public CxxTest::TestSuite
NearestNeighbours nn(m_instrument, *spectramap);
for(size_t i = 0; i < 2000; i++)
{
nn.neighbours(1, 0.0);
nn.neighboursInRadius(1, 0.0);
}
}

Expand All @@ -254,10 +254,10 @@ class NearestNeighboursTestPerformance : public CxxTest::TestSuite
Instrument_sptr m_instrument(new Instrument(instrument, pmap));

// Create the NearestNeighbours object directly.
NearestNeighbours nn(m_instrument, *spectramap);
for(size_t i = 0; i < 2000; i++)
{
nn.neighbours(1, true, 8);
NearestNeighbours nn(8, m_instrument, *spectramap);
nn.neighbours(1);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class MockNearestNeighboursFactory : public Mantid::Geometry::INearestNeighbours
{
public:
MOCK_METHOD3(create, Mantid::Geometry::INearestNeighbours*(boost::shared_ptr<const Mantid::Geometry::Instrument>,const Mantid::Geometry::ISpectraDetectorMap&, bool));
MOCK_METHOD4(create, Mantid::Geometry::INearestNeighbours*(int,boost::shared_ptr<const Mantid::Geometry::Instrument>,const Mantid::Geometry::ISpectraDetectorMap&, bool));
virtual ~MockNearestNeighboursFactory()
{
}
Expand All @@ -40,8 +41,8 @@ class MockNearestNeighboursFactory : public Mantid::Geometry::INearestNeighbours
typedef std::map<Mantid::specid_t, Mantid::Kernel::V3D> SpectrumDistanceMap;
class MockNearestNeighbours : public Mantid::Geometry::INearestNeighbours {
public:
MOCK_CONST_METHOD2(neighbours, SpectrumDistanceMap(specid_t spectrum, double radius));
MOCK_CONST_METHOD3(neighbours, SpectrumDistanceMap(specid_t spectrum, bool force, int numberofneighbours));
MOCK_CONST_METHOD2(neighboursInRadius, SpectrumDistanceMap(specid_t spectrum, double radius));
MOCK_CONST_METHOD1(neighbours, SpectrumDistanceMap(specid_t spectrum));
MOCK_METHOD0(die, void());
virtual ~MockNearestNeighbours()
{
Expand Down

0 comments on commit 281aa37

Please sign in to comment.