Skip to content

Commit

Permalink
refs #6025. Fix SmoothNeighbours.
Browse files Browse the repository at this point in the history
  • Loading branch information
OwenArnold committed Oct 28, 2012
1 parent ea4635e commit ab8711a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ class DLLExport SmoothNeighbours : public API::Algorithm
virtual int version() const { return (1);}
/// Algorithm's category for identification overriding a virtual method
virtual const std::string category() const { return "Transforms\\Smoothing";}

/// Determine wheter to treat the instrument as contianinng rectangular detectors or not.
bool executionUsedRectangularDetectorInstrument() const
{
return this->isRectangularDetectorInstrument();
}
private:
/// Sets documentation strings for this algorithm
virtual void initDocs();
Expand All @@ -106,7 +110,7 @@ class DLLExport SmoothNeighbours : public API::Algorithm

void execWorkspace2D(Mantid::API::MatrixWorkspace_sptr ws);
void execEvent(Mantid::DataObjects::EventWorkspace_sptr ws);

bool isRectangularDetectorInstrument() const;
void findNeighboursRectangular();
void findNeighboursUbiqutious();
Mantid::Geometry::Instrument_const_sptr fetchInstrument() const;
Expand Down
48 changes: 45 additions & 3 deletions Code/Mantid/Framework/Algorithms/src/SmoothNeighbours.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,50 @@ double SmoothNeighbours::translateToMeters(const std::string radiusUnits, const
return translatedRadius;
}

/**
Determine whether we can assume that the instrument is made up of rectangular detectors.
Looks at the first detector and makes the decision for the whole instrument based on whether this is rectangular or not.
It would be best if this sort of functionality could be put onto the instrument.
@return True if it has rectangular detectors.
*/
bool SmoothNeighbours::isRectangularDetectorInstrument() const
{
bool isRectangularDetectorInstrument;
if(!inWS)
{
throw std::runtime_error("InputWorkspace has not been provided.");
}

size_t nHistograms = inWS->getNumberHistograms();
for(size_t i = 0; i < nHistograms; ++i)
{
auto detector = inWS->getDetector(i);
if(detector->isMonitor())
{
continue;
}
else
{
if(detector->type() == "RectangularDetectorPixel")
{
g_log.information("Assuming Rectangular Detectors for SmoothNeighbours on this data.");
isRectangularDetectorInstrument = true;
}
else
{
g_log.information("Assuming Non-Rectangular Detectors for SmoothNeighbours on this data.");
isRectangularDetectorInstrument = false;
}
}
}

// If the radius is non-zero, then we don't want to treat this as a rectangular detector instrument.
return isRectangularDetectorInstrument && (Radius <= 0);
}


//--------------------------------------------------------------------------------------------
/** Executes the algorithm
Expand Down Expand Up @@ -628,8 +672,7 @@ void SmoothNeighbours::exec()
m_prog = new Progress(this, 0.0, 0.2, inWS->getNumberHistograms());

// Collect the neighbours with either method.

if (Radius <= 0.0)
if (isRectangularDetectorInstrument())
findNeighboursRectangular();
else
findNeighboursUbiqutious();
Expand All @@ -647,7 +690,6 @@ void SmoothNeighbours::exec()
throw std::runtime_error("This algorithm requires a Workspace2D or EventWorkspace as its input.");
}


//--------------------------------------------------------------------------------------------
/** Execute the algorithm for a Workspace2D/don't preserve events input */
void SmoothNeighbours::execWorkspace2D(Mantid::API::MatrixWorkspace_sptr ws)
Expand Down
53 changes: 52 additions & 1 deletion Code/Mantid/Framework/Algorithms/test/SmoothNeighboursTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class SmoothNeighboursTest : public CxxTest::TestSuite
double Radius = 0.0,
bool ConvertTo2D = false, int numberOfNeighbours=8)
{
// Pixels will be spaced 0.008 apart
// Pixels will be spaced 0.008 apart.
EventWorkspace_sptr in_ws = WorkspaceCreationHelper::createEventWorkspaceWithFullInstrument(1, 20, false);

if (type == WEIGHTED)
Expand Down Expand Up @@ -300,6 +300,57 @@ class SmoothNeighboursTest : public CxxTest::TestSuite
true /*Convert2D*/);
}

void doTestTreatAsRectangular(MatrixWorkspace_sptr inWS, const bool expectedToTreatAsRectangular, const double radius)
{
SmoothNeighbours alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() );
TS_ASSERT( alg.isInitialized() );
alg.setProperty("InputWorkspace", inWS);
alg.setProperty("OutputWorkspace", "testMW");
alg.setProperty("PreserveEvents", false);
alg.setProperty("WeightedSum", "Flat");
alg.setProperty("NumberOfNeighbours", 8);
alg.setProperty("IgnoreMaskedDetectors", true);
alg.setProperty("Radius", radius);
alg.setProperty("RadiusUnits", "NumberOfPixels");
TS_ASSERT_THROWS_NOTHING( alg.execute() );

TS_ASSERT_EQUALS(expectedToTreatAsRectangular, alg.executionUsedRectangularDetectorInstrument());
}

void test_is_rectangular_detector_instrument()
{
// Creates a Rectangular detector instrument
EventWorkspace_sptr inWS = WorkspaceCreationHelper::createEventWorkspaceWithFullInstrument(1, 20, false);

const bool treatAsRectangular = true;
const double radius = 0;

doTestTreatAsRectangular(inWS, treatAsRectangular, radius);
}

void test_is_rectangular_detector_instrument_but_treat_as_non_rectangular_as_positive_radius_used()
{
// Creates a Rectangular detector instrument
EventWorkspace_sptr inWS = WorkspaceCreationHelper::createEventWorkspaceWithFullInstrument(1, 20, false);


const double radius = 1; // Positive radius value.
const bool treatAsRectangular = false; // since the radius value will be > 0

doTestTreatAsRectangular(inWS, treatAsRectangular, radius);
}

void test_isNotRectangularDetectorInstrument()
{
// Creates a Non-Rectangular detector instrument.
MatrixWorkspace_sptr inWS = WorkspaceCreationHelper::create2DWorkspaceWithFullInstrument(100, 10);

const bool treatAsRectangular = false; // This is a non-rectangular instrument.
const double radius = 0; // This would have crashed Mantid previously.

doTestTreatAsRectangular(inWS, treatAsRectangular, radius);
}

};

Expand Down

0 comments on commit ab8711a

Please sign in to comment.