Skip to content

Commit

Permalink
Fix GeneratePythonScript for Load algorithm Refs #6812
Browse files Browse the repository at this point in the history
It does not currently handle properties that don't exist when the algorithm is
initialized very well.
  • Loading branch information
martyngigg committed Mar 29, 2013
1 parent 585393b commit 73d6288
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 37 deletions.
41 changes: 31 additions & 10 deletions Code/Mantid/Framework/Algorithms/src/GeneratePythonScript.cpp
Expand Up @@ -21,8 +21,8 @@ Example format:
*WIKI*/
#include "MantidAlgorithms/GeneratePythonScript.h"
#include "MantidKernel/System.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/MultipleFileProperty.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/AlgorithmHistory.h"

Expand Down Expand Up @@ -200,15 +200,36 @@ std::string GeneratePythonScript::genParamString(
params += ",";
}

// Try and cast the property to a MultiFileProperty or FileProperty type. If successful, we have to guard against
// the the occurence of directory separators (forward or back slashes) in the file path that the
// property contains. We do this by appending "r" onto the parameter in the output, to make it
// a Python "raw" string.
Property *p = ialg_Sptr->getPointerToProperty(name);
auto* fp = dynamic_cast<FileProperty*>(p);
auto* mp = dynamic_cast<MultipleFileProperty*>(p);
if(fp || mp) params += name + "=r'" + value + "'";
else params += name + "='" + value + "'";
// Needs to deal with properties that don't exist
// yet in a much better way
if(algHistName == "Load")
{
if(name == "Filename")
{
params += name + "=r'" + value + "'";
}
else
{
params += name + "='" + value + "'";
}
}
else
{
// Try and cast the property to a FileProperty type. If successful, we have to guard against
// the the occurence of directory separators (forward or back slashes) in the file path that the
// property contains. We do this by appending "r" onto the parameter in the output, to make it
// a Python "raw" string.
Property *p = ialg_Sptr->getPointerToProperty(name);
FileProperty* fp = dynamic_cast<FileProperty*> ((p));
if(NULL != fp)
{
params += name + "=r'" + value + "'";
}
else
{
params += name + "='" + value + "'";
}
}
}
}

Expand Down
45 changes: 18 additions & 27 deletions Code/Mantid/Framework/Algorithms/test/GeneratePythonScriptTest.h
Expand Up @@ -36,6 +36,7 @@ class GeneratePythonScriptTest : public CxxTest::TestSuite
// 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[] = {
Expand All @@ -56,7 +57,7 @@ class GeneratePythonScriptTest : public CxxTest::TestSuite
"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')", // Not tested.
"Load(Filename=r'C:/Mantid/Test/AutoTestData/IRS26173_ipg.nxs',OutputWorkspace='IRS26173_ipg',SpectrumMax='1')", // Not tested.
""
};

Expand All @@ -76,51 +77,41 @@ class GeneratePythonScriptTest : public CxxTest::TestSuite
TS_ASSERT_THROWS_NOTHING( alg.execute(); );
TS_ASSERT( alg.isExecuted() );

// Read in the file, and parse each line into a vector of strings.
std::string filename = alg.getProperty("Filename");
std::ifstream file(filename.c_str(), std::ifstream::in);
std::vector<std::string> lines;

while (file.good())
{
char testArray[256];
file.getline(testArray, 256);
std::string line = testArray;
lines.push_back(line);
}

std::vector<std::string>::iterator lineIter = lines.begin();

int lineCount = 0;

// 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
for( ; lineIter != lines.end(); ++lineIter)
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))
{
const std::string & algLine = *lineIter;
std::string::size_type filenamePos = algLine.find("Filename=");
std::string::size_type mapfilePos = algLine.find("MapFile=");
std::string::size_type filenamePos = scriptLine.find("Filename=");
std::string::size_type mapfilePos = scriptLine.find("MapFile=");
if(filenamePos != std::string::npos)
{
TS_ASSERT_EQUALS(algLine[filenamePos+9], 'r');
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(algLine[mapfilePos+8], 'r');
TS_ASSERT_EQUALS(scriptLine[mapfilePos+8], 'r');
}
else
{
TS_ASSERT_EQUALS(algLine,result[lineCount]);
TS_ASSERT_EQUALS(scriptLine,result[lineCount]);
}
lineCount++;
}

// Remove workspace from the data service.
// AnalysisDataService::Instance().remove(outWSName);
file.close();
if (Poco::File(filename).exists()) Poco::File(filename).remove();
//if (Poco::File(filename).exists()) Poco::File(filename).remove();
}
};

Expand Down

0 comments on commit 73d6288

Please sign in to comment.