Skip to content

Commit

Permalink
Updated tests to use automatic instr. type determination
Browse files Browse the repository at this point in the history
Refs #7441
  • Loading branch information
arturbekasov committed Jul 24, 2013
1 parent b7e6ac2 commit a0d672d
Showing 1 changed file with 32 additions and 62 deletions.
94 changes: 32 additions & 62 deletions Code/Mantid/Framework/Algorithms/test/SmoothNeighboursTest.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#ifndef SmoothNeighboursTEST_H_
#define SmoothNeighboursTEST_H_

#include "MantidGeometry/Instrument/INearestNeighboursFactory.h"
#include "MantidAlgorithms/SmoothNeighbours.h"
#include "MantidAlgorithms/CheckWorkspacesMatch.h"
#include <cxxtest/TestSuite.h>
#include "MantidKernel/Timer.h"
#include "MantidAlgorithms/SmoothNeighbours.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidDataHandling/LoadInstrument.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidGeometry/Instrument/INearestNeighboursFactory.h"
#include "MantidKernel/System.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
#include "MantidKernel/Timer.h"
#include "MantidTestHelpers/ComponentCreationHelper.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"

#include <cxxtest/TestSuite.h>
#include <iostream>
#include <iomanip>

#include "MantidDataObjects/EventWorkspace.h"
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidDataHandling/LoadInstrument.h"
#include "MantidAPI/FrameworkManager.h"

using namespace Mantid;
using namespace Mantid::Kernel;
using namespace Mantid::API;
Expand Down Expand Up @@ -76,12 +76,10 @@ class SmoothNeighboursTest : public CxxTest::TestSuite

}

void do_test(EventType type, double * expectedY, std::string WeightedSum = "Parabolic", bool PreserveEvents = true,
double Radius = 0.001,
bool ConvertTo2D = false, int numberOfNeighbours=8)
void do_test_non_uniform(EventType type, double * expectedY, std::string WeightedSum = "Parabolic", bool PreserveEvents = true,
double Radius = 0.001, bool ConvertTo2D = false, int numberOfNeighbours=8)
{
// Pixels will be spaced 0.008 apart.
EventWorkspace_sptr in_ws = WorkspaceCreationHelper::createEventWorkspaceWithFullInstrument(1, 20, false);
EventWorkspace_sptr in_ws = WorkspaceCreationHelper::createEventWorkspaceWithNonUniformInstrument(1, false);

if (type == WEIGHTED)
{
Expand All @@ -101,18 +99,16 @@ class SmoothNeighboursTest : public CxxTest::TestSuite
EventList & el = in_ws->getEventList(4);
el += el;

size_t nevents0 = in_ws->getNumberEvents();

// Register the workspace in the data service
AnalysisDataService::Instance().addOrReplace("SmoothNeighboursTest_input", in_ws);

if (ConvertTo2D)
{
FrameworkManager::Instance().exec("ConvertToMatrixWorkspace", 4,
"InputWorkspace", "SmoothNeighboursTest_input",
"OutputWorkspace", "SmoothNeighboursTest_input");
}


// Register the workspace in the data service
SmoothNeighbours alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() );
TS_ASSERT( alg.isInitialized() );
Expand All @@ -129,16 +125,16 @@ class SmoothNeighboursTest : public CxxTest::TestSuite
if (PreserveEvents)
{
EventWorkspace_sptr ws;
TS_ASSERT_THROWS_NOTHING(
ws = AnalysisDataService::Instance().retrieveWS<EventWorkspace>("testEW") );
TS_ASSERT_THROWS_NOTHING(ws = AnalysisDataService::Instance().retrieveWS<EventWorkspace>("testEW"));
TS_ASSERT(ws);
if (!ws) return;
size_t nevents = ws->getNumberEvents();
TS_ASSERT_LESS_THAN( nevents0, nevents);
TS_ASSERT_LESS_THAN(in_ws->getNumberEvents(), ws->getNumberEvents());
}

// Check the values
MatrixWorkspace_sptr ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("testEW");
// Result workspace
MatrixWorkspace_sptr ws;

TS_ASSERT_THROWS_NOTHING(ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("testEW"));
TS_ASSERT(ws);
if (!ws) return;

Expand All @@ -153,7 +149,6 @@ class SmoothNeighboursTest : public CxxTest::TestSuite
TS_ASSERT_DELTA( ws->readY(7)[0], expectedY[7], 1e-4);
TS_ASSERT_DELTA( ws->readY(8)[0], expectedY[8], 1e-4);


AnalysisDataService::Instance().remove("testEW");
}

Expand Down Expand Up @@ -184,16 +179,16 @@ class SmoothNeighboursTest : public CxxTest::TestSuite

size_t nevents0 = in_ws->getNumberEvents();

// Register the workspace in the data service
AnalysisDataService::Instance().addOrReplace("SmoothNeighboursTest_input", in_ws);

if (ConvertTo2D)
{
FrameworkManager::Instance().exec("ConvertToMatrixWorkspace", 4,
"InputWorkspace", "SmoothNeighboursTest_input",
"OutputWorkspace", "SmoothNeighboursTest_input");
}


// Register the workspace in the data service
SmoothNeighbours alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() );
TS_ASSERT( alg.isInitialized() );
Expand All @@ -204,7 +199,6 @@ class SmoothNeighboursTest : public CxxTest::TestSuite
alg.setProperty("WeightedSum", WeightedSum);
alg.setProperty("AdjX", 1);
alg.setProperty("AdjY", 1);
alg.setProperty("ForceEvaluationAsRectangularDetectors", true);
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );

Expand Down Expand Up @@ -327,7 +321,6 @@ class SmoothNeighboursTest : public CxxTest::TestSuite
do_test_rectangular(TOF, expectedY, "Parabolic");
}


void test_event()
{
double expectedY[9] = {2, 2, 2, 2.3636, 2.5454, 2.3636, 2, 2, 2};
Expand All @@ -342,23 +335,20 @@ class SmoothNeighboursTest : public CxxTest::TestSuite

void test_event_Radius_no_WeightedSum()
{
// Note: something seems off in the nearest neighbour calc for this fake instrument. It only finds the neighbours in a column
double expectedY[9] = {2, 2, 2, 2, 3.0, 2, 2, 2, 2};
do_test(TOF, expectedY, "Flat", true /*PreserveEvents*/, 0.009 /* Radius */);
double expectedY[9] = {2.5, 2.3333, 2.5, 2.3333, 2.2222, 2.3333, 2.5, 2.3333, 2.5};
do_test_non_uniform(TOF, expectedY, "Flat", true /*PreserveEvents*/, 0.009 /* Radius */);
}

void test_event_Radius_WeightedSum()
{
// Note: something seems off in the nearest neighbour calc for this fake instrument. It only finds the neighbours in a column
double expectedY[9] = {2, 2, 2, 2, (2. + 4.*9)/10., 2, 2, 2, 2};
do_test(TOF, expectedY, "Linear", true /*PreserveEvents*/, 0.009 /* Radius */);
double expectedY[9] = {2.2038, 2.3218, 2.2038, 2.3218, 2.5501, 2.3218, 2.2038, 2.3218, 2.2038};
do_test_non_uniform(TOF, expectedY, "Linear", true /*PreserveEvents*/, 0.009 /* Radius */);
}

void test_workspace2D()
{
double expectedY[9] = {2, 2, 2, 2.3636, 2.5454, 2.3636, 2, 2, 2};
do_test_rectangular(TOF, expectedY, "Parabolic", false /*PreserveEvents*/
, true /*Convert2D*/);
do_test_rectangular(TOF, expectedY, "Parabolic", false /*PreserveEvents*/, true /*Convert2D*/);
}

void test_workspace2D_no_WeightedSum()
Expand All @@ -369,18 +359,14 @@ class SmoothNeighboursTest : public CxxTest::TestSuite

void test_workspace2D_Radius_no_WeightedSum()
{
// Note: something seems off in the nearest neighbour calc for this fake instrument. It only finds the neighbours in a column
double expectedY[9] = {2, 2, 2, 2, 3.0, 2, 2, 2, 2};
do_test(TOF, expectedY, "Flat", false /*PreserveEvents*/, 0.009 /* Radius */,
true /*Convert2D*/);
double expectedY[9] = {2.5, 2.3333, 2.5, 2.3333, 2.2222, 2.3333, 2.5, 2.3333, 2.5};
do_test_non_uniform(TOF, expectedY, "Flat", false /*PreserveEvents*/, 0.009 /* Radius */, true /*Convert2D*/);
}

void test_workspace2D_Radius_WeightedSum()
{
// Note: something seems off in the nearest neighbour calc for this fake instrument. It only finds the neighbours in a column
double expectedY[9] = {2, 2, 2, 2, (2. + 4.*9)/10., 2, 2, 2, 2};
do_test(TOF, expectedY, "Linear", false /*PreserveEvents*/, 0.009 /* Radius */,
true /*Convert2D*/);
double expectedY[9] = {2.2038, 2.3218, 2.2038, 2.3218, 2.5501, 2.3218, 2.2038, 2.3218, 2.2038};
do_test_non_uniform(TOF, expectedY, "Linear", false /*PreserveEvents*/, 0.009 /* Radius */, true /*Convert2D*/);
}

void test_properties_in_no_group()
Expand Down Expand Up @@ -436,22 +422,6 @@ class SmoothNeighboursTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS("Rectangular Detectors", propSumPixelsY->getGroup());
TS_ASSERT_EQUALS("Rectangular Detectors", propZeroEdgePixels->getGroup());
}

void test_throw_if_mix_rectangular_and_nonrectangular_properties()
{
MatrixWorkspace_sptr inWS = WorkspaceCreationHelper::create2DWorkspaceWithFullInstrument(100, 10);
SmoothNeighbours alg;
alg.setRethrows(true);
TS_ASSERT_THROWS_NOTHING( alg.initialize() );
TS_ASSERT( alg.isInitialized() );
alg.setProperty("InputWorkspace", inWS);
alg.setProperty("OutputWorkspace", "testMW");
alg.setProperty("Radius", 1.0); // This is a 'NonUniform Detector' group property that is now non default.
alg.setProperty("AdjX", 3); // This is a 'Rectangular Dectector' group property that is now non default.
TSM_ASSERT_THROWS("Either setup the algorithm for rectangular detectors or non-rectangular detectors, not both.", alg.execute(), std::invalid_argument );
}


};

#endif /*SmoothNeighboursTEST_H_*/
Expand Down

0 comments on commit a0d672d

Please sign in to comment.