Skip to content

Commit

Permalink
Refs #11551. HKL from table extraction is now separate from PawleyFit
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Wedel committed Apr 15, 2015
1 parent d360b28 commit 9f138a5
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 44 deletions.
Expand Up @@ -9,7 +9,27 @@
namespace Mantid {
namespace CurveFitting {

/** PawleyFit
/** @class V3DFromHKLColumnExtractor
Small helper class to extract HKLs as V3D from table columns. The table
column can either store V3D directly, or a string with various separators:
, ; [ ] (space)
*/
struct DLLExport V3DFromHKLColumnExtractor {
Kernel::V3D operator()(const API::Column_const_sptr &hklColumn,
size_t i) const;

protected:
Kernel::V3D getHKLFromV3DColumn(const API::Column_const_sptr &hklColumn,
size_t i) const;
Kernel::V3D getHKLFromStringColumn(const API::Column_const_sptr &hklColumn,
size_t i) const;

Kernel::V3D getHKLFromString(const std::string &hklString) const;
};

/** @class PawleyFit
This algorithm uses the Pawley-method to refine lattice parameters using a
powder diffractogram and a list of unique Miller indices. From the initial
Expand Down Expand Up @@ -59,10 +79,6 @@ class DLLExport PawleyFit : public API::Algorithm {
const Kernel::Unit_sptr &unit, double startX,
double endX) const;

Kernel::V3D getHKLFromColumn(size_t i,
const API::Column_const_sptr &hklColumn) const;
Kernel::V3D getHkl(const std::string &hklString) const;

API::ITableWorkspace_sptr
getLatticeFromFunction(const PawleyFunction_sptr &pawleyFn) const;
API::ITableWorkspace_sptr
Expand Down
78 changes: 47 additions & 31 deletions Code/Mantid/Framework/CurveFitting/src/PawleyFit.cpp
Expand Up @@ -72,14 +72,16 @@ void PawleyFit::addHKLsToFunction(PawleyFunction_sptr &pawleyFn,
pawleyFn->clearPeaks();

try {
V3DFromHKLColumnExtractor extractor;

Column_const_sptr hklColumn = tableWs->getColumn("HKL");
Column_const_sptr dColumn = tableWs->getColumn("d");
Column_const_sptr intensityColumn = tableWs->getColumn("Intensity");
Column_const_sptr fwhmColumn = tableWs->getColumn("FWHM (rel.)");

for (size_t i = 0; i < tableWs->rowCount(); ++i) {
try {
V3D hkl = getHKLFromColumn(i, hklColumn);
V3D hkl = extractor(hklColumn, i);

double d = (*dColumn)[i];
double center = getTransformedCenter(d, unit);
Expand All @@ -102,36 +104,6 @@ void PawleyFit::addHKLsToFunction(PawleyFunction_sptr &pawleyFn,
}
}

/// Tries to extract Miller indices as V3D from column.
V3D PawleyFit::getHKLFromColumn(size_t i,
const Column_const_sptr &hklColumn) const {
if (hklColumn->type() == "V3D") {
return hklColumn->cell<V3D>(i);
}

return getHkl(hklColumn->cell<std::string>(i));
}

/// Try to extract a V3D from the given string with different separators.
V3D PawleyFit::getHkl(const std::string &hklString) const {
auto delimiters = boost::is_any_of(" ,[];");

std::string workingCopy = boost::trim_copy_if(hklString, delimiters);
std::vector<std::string> indicesStr;
boost::split(indicesStr, workingCopy, delimiters);

if (indicesStr.size() != 3) {
throw std::invalid_argument("Input string cannot be parsed as HKL.");
}

V3D hkl;
hkl.setX(boost::lexical_cast<double>(indicesStr[0]));
hkl.setY(boost::lexical_cast<double>(indicesStr[1]));
hkl.setZ(boost::lexical_cast<double>(indicesStr[2]));

return hkl;
}

/// Creates a table containing the cell parameters stored in the supplied
/// function.
ITableWorkspace_sptr
Expand Down Expand Up @@ -401,5 +373,49 @@ void PawleyFit::exec() {
setProperty("ReducedChiSquare", chiSquare);
}

/// Tries to extract Miller indices as V3D from column.
V3D V3DFromHKLColumnExtractor::operator()(const Column_const_sptr &hklColumn,
size_t i) const {
if (hklColumn->type() == "V3D") {
return getHKLFromV3DColumn(hklColumn, i);
}

return getHKLFromStringColumn(hklColumn, i);
}

/// Returns the i-th cell of a V3D column.
V3D V3DFromHKLColumnExtractor::getHKLFromV3DColumn(
const Column_const_sptr &hklColumn, size_t i) const {
return hklColumn->cell<V3D>(i);
}

/// Pass the cell value as string to getHKLFromString.
V3D V3DFromHKLColumnExtractor::getHKLFromStringColumn(
const Column_const_sptr &hklColumn, size_t i) const {

return getHKLFromString(hklColumn->cell<std::string>(i));
}

/// Try to extract a V3D from the given string with different separators.
V3D V3DFromHKLColumnExtractor::getHKLFromString(const std::string &hklString)
const {
auto delimiters = boost::is_any_of(" ,[];");

std::string workingCopy = boost::trim_copy_if(hklString, delimiters);
std::vector<std::string> indicesStr;
boost::split(indicesStr, workingCopy, delimiters);

if (indicesStr.size() != 3) {
throw std::invalid_argument("Input string cannot be parsed as HKL.");
}

V3D hkl;
hkl.setX(boost::lexical_cast<double>(indicesStr[0]));
hkl.setY(boost::lexical_cast<double>(indicesStr[1]));
hkl.setZ(boost::lexical_cast<double>(indicesStr[2]));

return hkl;
}

} // namespace CurveFitting
} // namespace Mantid
21 changes: 13 additions & 8 deletions Code/Mantid/Framework/CurveFitting/test/PawleyFitTest.h
Expand Up @@ -12,6 +12,7 @@
#include "MantidTestHelpers/WorkspaceCreationHelper.h"

using Mantid::CurveFitting::PawleyFit;
using Mantid::CurveFitting::V3DFromHKLColumnExtractor;
using namespace Mantid::API;
using namespace Mantid::Kernel;

Expand All @@ -23,17 +24,17 @@ class PawleyFitTest : public CxxTest::TestSuite {
static void destroySuite(PawleyFitTest *suite) { delete suite; }

void testGetHKL() {
TestablePawleyFit pfit;
TestableV3DFromHKLColumnExtractor extractor;

V3D referenceHKL(1, 2, 3);

TS_ASSERT_EQUALS(pfit.getHkl("1 2 3"), referenceHKL);
TS_ASSERT_EQUALS(pfit.getHkl(" 1 2 3 "), referenceHKL);
TS_ASSERT_EQUALS(pfit.getHkl("1 2 3"), referenceHKL);
TS_ASSERT_EQUALS(pfit.getHkl("1,2,3"), referenceHKL);
TS_ASSERT_EQUALS(pfit.getHkl("1;2;3"), referenceHKL);
TS_ASSERT_EQUALS(pfit.getHkl("[1,2,3]"), referenceHKL);
TS_ASSERT_EQUALS(pfit.getHkl("[1;2 3]"), referenceHKL);
TS_ASSERT_EQUALS(extractor.getHKLFromString("1 2 3"), referenceHKL);
TS_ASSERT_EQUALS(extractor.getHKLFromString(" 1 2 3 "), referenceHKL);
TS_ASSERT_EQUALS(extractor.getHKLFromString("1 2 3"), referenceHKL);
TS_ASSERT_EQUALS(extractor.getHKLFromString("1,2,3"), referenceHKL);
TS_ASSERT_EQUALS(extractor.getHKLFromString("1;2;3"), referenceHKL);
TS_ASSERT_EQUALS(extractor.getHKLFromString("[1,2,3]"), referenceHKL);
TS_ASSERT_EQUALS(extractor.getHKLFromString("[1;2 3]"), referenceHKL);
}

void testFitHexagonalCellQ() {
Expand Down Expand Up @@ -152,6 +153,10 @@ class PawleyFitTest : public CxxTest::TestSuite {
~TestablePawleyFit() {}
};

class TestableV3DFromHKLColumnExtractor : public V3DFromHKLColumnExtractor {
friend class PawleyFitTest;
};

ITableWorkspace_sptr getHCPTable() {
ITableWorkspace_sptr tableWs = WorkspaceFactory::Instance().createTable();
tableWs->addColumn("V3D", "HKL");
Expand Down

0 comments on commit 9f138a5

Please sign in to comment.