Skip to content

Commit

Permalink
Modify GeneratePythonScriptTest.h to not use file re #7072
Browse files Browse the repository at this point in the history
and in so doing eliminate use of the removed unwrap algorithm.

Signed-off-by: Karl Palmen <karl.palmen@stfc.ac.uk>
  • Loading branch information
KarlPalmen committed Oct 16, 2013
1 parent afcb46a commit 59f8143
Showing 1 changed file with 79 additions and 84 deletions.
163 changes: 79 additions & 84 deletions Code/Mantid/Framework/Algorithms/test/GeneratePythonScriptTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include <iomanip>

#include "MantidAlgorithms/GeneratePythonScript.h"
#include "MantidDataHandling/Load.h"
#include "MantidAlgorithms/CreateWorkspace.h"
#include "MantidAlgorithms/CropWorkspace.h"
#include "MantidAlgorithms/Power.h"
#include "MantidAPI/AlgorithmManager.h"
#include <Poco/File.h>

Expand All @@ -29,89 +31,82 @@ class GeneratePythonScriptTest : public CxxTest::TestSuite

void test_exec()
{
// Load test file into workspace
Mantid::DataHandling::Load loader;
loader.initialize();
// Here we make the assumption that IRS26173_ipg.nxs will not change, and so is
// a valid choice to test the output of the Algorithm against.
loader.setPropertyValue("Filename", "IRS26173_ipg.nxs");
loader.setPropertyValue("OutputWorkspace","LoadedWorkspace");
loader.setPropertyValue("SpectrumMax","1");
loader.setRethrows(true);

std::string result[] = {
"######################################################################",
"#Python Script Generated by GeneratePythonScript Algorithm",
"######################################################################",
"LoadRaw(Filename=r'G:/Spencer/Science/Raw/irs26173.raw',OutputWorkspace='IPG',SpectrumMin='3',SpectrumMax='53')", // Not tested.
"ConvertUnits(InputWorkspace='IPG',OutputWorkspace='Spec',Target='Wavelength')",
"LoadRaw(Filename=r'G:/Spencer/Science/Raw/irs26173.raw',OutputWorkspace='Mon_in',SpectrumMax='1')", // Not tested.
"Unwrap(InputWorkspace='Mon_in',OutputWorkspace='Mon',LRef='37.86')",
"RemoveBins(InputWorkspace='Mon',OutputWorkspace='Mon',XMin='6.14600063416',XMax='6.14800063416',Interpolation='Linear')",
"FFTSmooth(InputWorkspace='Mon',OutputWorkspace='Mon')",
"RebinToWorkspace(WorkspaceToRebin='Spec',WorkspaceToMatch='Mon',OutputWorkspace='Spec')",
"LoadRaw(Filename=r'G:/Spencer/Science/Raw/irs26173.raw',OutputWorkspace='Mon_in',SpectrumMax='1')", // Not tested.
"Unwrap(InputWorkspace='Mon_in',OutputWorkspace='Mon',LRef='37.86')",
"RemoveBins(InputWorkspace='Mon',OutputWorkspace='Mon',XMin='6.14600063416',XMax='6.14800063416',Interpolation='Linear')",
"FFTSmooth(InputWorkspace='Mon',OutputWorkspace='Mon')",
"Divide(LHSWorkspace='Spec',RHSWorkspace='Mon',OutputWorkspace='Spec')",
"ConvertUnits(InputWorkspace='Spec',OutputWorkspace='Spec',Target='DeltaE',EMode='Indirect',EFixed='1.84')",
"GroupDetectors(InputWorkspace='Spec',OutputWorkspace='IPG_3',MapFile=r'G:/Spencer/Science/Mantid/IRIS/PG1op3.map')", // Not tested.
"Load(Filename=r'C:/Mantid/Test/AutoTestData/IRS26173_ipg.nxs',OutputWorkspace='IRS26173_ipg',SpectrumMax='1')", // Not tested.
""
};

TS_ASSERT_THROWS_NOTHING(loader.execute());
TS_ASSERT_EQUALS(loader.isExecuted(), true);

MatrixWorkspace_sptr ws = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("LoadedWorkspace");

TS_ASSERT(NULL != ws);

// Set up and execute the algorithm.
GeneratePythonScript alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() );
TS_ASSERT( alg.isInitialized() );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("InputWorkspace", ws->getName()) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Filename", "GeneratePythonScriptTest.py") );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );


// Compare the contents of the file to the expected result line-by-line.
// If the line contains Filename or MapFile then just check that the string
// is prefixed with r to convert it to a Python raw string and not the actual content
// as the file paths are different
std::string filename = alg.getProperty("Filename");
std::ifstream file(filename.c_str(), std::ifstream::in);
std::string scriptLine;
int lineCount(0);
while(std::getline(file, scriptLine))
{
std::string::size_type filenamePos = scriptLine.find("Filename=");
std::string::size_type mapfilePos = scriptLine.find("MapFile=");
if(filenamePos != std::string::npos)
{
TS_ASSERT_EQUALS(scriptLine[filenamePos+9], 'r');
if(scriptLine.find("Load(") != std::string::npos)
{
// Check Load call has SpectrumMax
TS_ASSERT(scriptLine.find("SpectrumMax='1'") != std::string::npos);
}
}
else if(mapfilePos != std::string::npos)
{
TS_ASSERT_EQUALS(scriptLine[mapfilePos+8], 'r');
}
else
{
TS_ASSERT_EQUALS(scriptLine,result[lineCount]);
}
lineCount++;
}

file.close();
if (Poco::File(filename).exists()) Poco::File(filename).remove();
// Create test workspace
std::string workspaceName = "testGeneratePython";
create_test_workspace( workspaceName );

std::string result[] = {
"######################################################################",
"#Python Script Generated by GeneratePythonScript Algorithm",
"######################################################################",
"CreateWorkspace(OutputWorkspace='testGeneratePython',DataX='1,2,3,5,6',DataY='7,9,16,4,3',DataE='2,3,4,2,1',WorkspaceTitle='Test Workspace')",
"CropWorkspace(InputWorkspace='testGeneratePython',OutputWorkspace='testGeneratePython',XMin='2',XMax='5')",
"Power(InputWorkspace='testGeneratePython',OutputWorkspace='testGeneratePython',Exponent='1.5')",
""
};


// Set up and execute the algorithm.
GeneratePythonScript alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() );
TS_ASSERT( alg.isInitialized() );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("InputWorkspace", workspaceName) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Filename", "GeneratePythonScriptTest.py") );
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );


// Compare the contents of the file to the expected result line-by-line.
std::string filename = alg.getProperty("Filename");
std::ifstream file(filename.c_str(), std::ifstream::in);
std::string scriptLine;
int lineCount(0);
while(std::getline(file, scriptLine))
{
TS_ASSERT_EQUALS(scriptLine,result[lineCount]);
lineCount++;
}

file.close();
if (Poco::File(filename).exists()) Poco::File(filename).remove();
}

void create_test_workspace( const std::string& wsName )
{
Mantid::Algorithms::CreateWorkspace creator;
Mantid::Algorithms::CropWorkspace cropper;
Mantid::Algorithms::Power powerer;

// Set up and execute creation of the workspace
creator.initialize();
creator.setPropertyValue("OutputWorkspace",wsName);
creator.setPropertyValue("DataX","1,2,3,5,6");
creator.setPropertyValue("DataY","7,9,16,4,3");
creator.setPropertyValue("DataE","2,3,4,2,1");
creator.setPropertyValue("WorkspaceTitle","Test Workspace");
creator.setRethrows(true);
TS_ASSERT_THROWS_NOTHING(creator.execute());
TS_ASSERT_EQUALS(creator.isExecuted(), true);

// Set up and execute the cropping of the workspace
cropper.initialize();
cropper.setPropertyValue("InputWorkspace",wsName);
cropper.setPropertyValue("OutputWorkspace",wsName);
cropper.setPropertyValue("XMin","2");
cropper.setPropertyValue("XMax","5");
cropper.setRethrows(true);
TS_ASSERT_THROWS_NOTHING(cropper.execute());
TS_ASSERT_EQUALS(cropper.isExecuted(), true);

// Set up and execute Power algorithm on the workspace
powerer.initialize();
powerer.setPropertyValue("InputWorkspace",wsName);
powerer.setPropertyValue("OutputWorkspace",wsName);
powerer.setPropertyValue("Exponent","1.5");
powerer.setRethrows(true);
TS_ASSERT_THROWS_NOTHING(powerer.execute());
TS_ASSERT_EQUALS(powerer.isExecuted(), true);

}
};

Expand Down

0 comments on commit 59f8143

Please sign in to comment.