Skip to content

Commit

Permalink
Merge branch 'master' into 11756_add_link_private_to_cmakelists
Browse files Browse the repository at this point in the history
  • Loading branch information
mantid-roman committed May 20, 2015
2 parents 381b60b + 118baa1 commit 38caeb4
Show file tree
Hide file tree
Showing 42 changed files with 1,298 additions and 227 deletions.
2 changes: 1 addition & 1 deletion Code/Mantid/Build/CMake/VersionNumber.cmake
@@ -1,7 +1,7 @@
# Set the version number here for MantidVersion and the package filenames

set ( VERSION_MAJOR 3 )
set ( VERSION_MINOR 3 )
set ( VERSION_MINOR 4 )

# UNCOMMENT the next 'set' line to 'force' the patch version number to
# a value (instead of using the count coming out of 'git describe')
Expand Down
6 changes: 6 additions & 0 deletions Code/Mantid/Framework/API/src/WorkspaceOpOverloads.cpp
Expand Up @@ -553,6 +553,12 @@ void WorkspaceHelpers::makeDistribution(MatrixWorkspace_sptr workspace,
if (workspace->isDistribution() == forwards)
return;

// If we're not able to get a writable reference to Y, then this is an event
// workspace, which we can't operate on.
if (workspace->id() == "EventWorkspace")
throw std::runtime_error("Event workspaces cannot be directly converted "
"into distributions.");

const size_t numberOfSpectra = workspace->getNumberHistograms();

std::vector<double> widths(workspace->readX(0).size());
Expand Down
Expand Up @@ -64,6 +64,8 @@ class DLLExport ConvertToDistribution : public API::Algorithm {
void init();
/// Execution code
void exec();
/// Validate inputs
std::map<std::string, std::string> validateInputs();
};

} // namespace Algorithms
Expand Down
11 changes: 11 additions & 0 deletions Code/Mantid/Framework/Algorithms/src/ConvertToDistribution.cpp
Expand Up @@ -27,5 +27,16 @@ void ConvertToDistribution::exec() {
WorkspaceHelpers::makeDistribution(workspace);
}

std::map<std::string, std::string> ConvertToDistribution::validateInputs() {
std::map<std::string, std::string> errors;

MatrixWorkspace_sptr workspace = getProperty("Workspace");
if (workspace->id() == "EventWorkspace")
errors["Workspace"] = "Event workspaces cannot be directly converted to "
"distributions.";

return errors;
}

} // namespace Algorithms
} // namespace Mantid
Expand Up @@ -567,6 +567,11 @@ FitPeakOffsetResult GetDetOffsetsMultiPeaks::calculatePeakOffset(
fr.numpeakstofit = 0;
fr.numpeaksindrange = 0;

fr.highestpeakpos = 0.0;
fr.highestpeakdev = 0.0;
fr.resolution = 0.0;
fr.dev_resolution = 0.0;

// Checks for empty and dead detectors
if ((isEvent) && (eventW->getEventList(wi).empty())) {
// empty detector will be masked
Expand Down
Expand Up @@ -107,6 +107,17 @@ class DLLExport PlotPeakByLogValue : public API::Algorithm {

/// Create a list of input workspace names
std::vector<InputData> makeNames() const;

/// Create a minimizer string based on template string provided
std::string getMinimizerString(const std::string &wsName,
const std::string &specIndex);

/// Base name of output workspace
std::string m_baseName;

/// Record of workspaces output by the minimizer
std::map<std::string, std::vector<std::string>> m_minimizerWorkspaces;

};

} // namespace CurveFitting
Expand Down
78 changes: 66 additions & 12 deletions Code/Mantid/Framework/CurveFitting/src/PlotPeakByLogValue.cpp
Expand Up @@ -9,8 +9,10 @@
#include <algorithm>
#include <Poco/StringTokenizer.h>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string/replace.hpp>

#include "MantidCurveFitting/PlotPeakByLogValue.h"
#include "MantidAPI/IFuncMinimizer.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/FuncMinimizerFactory.h"
#include "MantidAPI/CostFunctionFactory.h"
Expand All @@ -27,6 +29,10 @@
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/MandatoryValidator.h"

namespace {
Mantid::Kernel::Logger g_log("PlotPeakByLogValue");
}

namespace Mantid {
namespace CurveFitting {

Expand Down Expand Up @@ -93,15 +99,11 @@ void PlotPeakByLogValue::init() {
"functions that"
"have attribute WorkspaceIndex.");

std::vector<std::string> minimizerOptions =
FuncMinimizerFactory::Instance().getKeys();
declareProperty("Minimizer", "Levenberg-Marquardt",
boost::make_shared<StringListValidator>(minimizerOptions),
"Minimizer to use for fitting. Minimizers available are "
"'Levenberg-Marquardt', 'Simplex', \n"
"'Levenberg-Marquardt', 'Simplex', 'FABADA',\n"
"'Conjugate gradient (Fletcher-Reeves imp.)', 'Conjugate "
"gradient (Polak-Ribiere imp.)' and 'BFGS'",
Direction::InOut);
"gradient (Polak-Ribiere imp.)' and 'BFGS'");

std::vector<std::string> costFuncOptions =
CostFunctionFactory::Instance().getKeys();
Expand All @@ -111,6 +113,10 @@ void PlotPeakByLogValue::init() {
"are 'Least squares' and 'Ignore positive peaks'",
Direction::InOut);

declareProperty("MaxIterations", 500,
"Stop after this number of iterations if a good fit is not "
"found");

declareProperty("CreateOutput", false, "Set to true to create output "
"workspaces with the results of the "
"fit(default is false).");
Expand Down Expand Up @@ -141,7 +147,7 @@ void PlotPeakByLogValue::exec() {
bool createFitOutput = getProperty("CreateOutput");
bool outputCompositeMembers = getProperty("OutputCompositeMembers");
bool outputConvolvedMembers = getProperty("ConvolveMembers");
std::string baseName = getPropertyValue("OutputWorkspace");
m_baseName = getPropertyValue("OutputWorkspace");

bool isDataName = false; // if true first output column is of type string and
// is the data source name
Expand Down Expand Up @@ -244,7 +250,7 @@ void PlotPeakByLogValue::exec() {
<< " with " << std::endl;
g_log.debug() << ifun->asString() << std::endl;

std::string spectrum_index = boost::lexical_cast<std::string>(j);
const std::string spectrum_index = boost::lexical_cast<std::string>(j);
std::string wsBaseName = "";

if (createFitOutput)
Expand All @@ -259,8 +265,11 @@ void PlotPeakByLogValue::exec() {
fit->setProperty("WorkspaceIndex", j);
fit->setPropertyValue("StartX", getPropertyValue("StartX"));
fit->setPropertyValue("EndX", getPropertyValue("EndX"));
fit->setPropertyValue("Minimizer", getPropertyValue("Minimizer"));
fit->setPropertyValue(
"Minimizer", getMinimizerString(wsNames[i].name, spectrum_index));
fit->setPropertyValue("CostFunction", getPropertyValue("CostFunction"));
fit->setPropertyValue("MaxIterations",
getPropertyValue("MaxIterations"));
fit->setProperty("CalcErrors", true);
fit->setProperty("CreateOutput", createFitOutput);
fit->setProperty("OutputCompositeMembers", outputCompositeMembers);
Expand Down Expand Up @@ -324,19 +333,30 @@ void PlotPeakByLogValue::exec() {
groupAlg->initialize();
groupAlg->setProperty("InputWorkspaces", covariance_workspaces);
groupAlg->setProperty("OutputWorkspace",
baseName + "_NormalisedCovarianceMatrices");
m_baseName + "_NormalisedCovarianceMatrices");
groupAlg->execute();

groupAlg = AlgorithmManager::Instance().createUnmanaged("GroupWorkspaces");
groupAlg->initialize();
groupAlg->setProperty("InputWorkspaces", parameter_workspaces);
groupAlg->setProperty("OutputWorkspace", baseName + "_Parameters");
groupAlg->setProperty("OutputWorkspace", m_baseName + "_Parameters");
groupAlg->execute();

groupAlg = AlgorithmManager::Instance().createUnmanaged("GroupWorkspaces");
groupAlg->initialize();
groupAlg->setProperty("InputWorkspaces", fit_workspaces);
groupAlg->setProperty("OutputWorkspace", baseName + "_Workspaces");
groupAlg->setProperty("OutputWorkspace", m_baseName + "_Workspaces");
groupAlg->execute();
}

for (auto it = m_minimizerWorkspaces.begin();
it != m_minimizerWorkspaces.end(); ++it) {
const std::string paramName = (*it).first;
API::IAlgorithm_sptr groupAlg =
AlgorithmManager::Instance().createUnmanaged("GroupWorkspaces");
groupAlg->initialize();
groupAlg->setProperty("InputWorkspaces", (*it).second);
groupAlg->setProperty("OutputWorkspace", m_baseName + "_" + paramName);
groupAlg->execute();
}
}
Expand Down Expand Up @@ -553,5 +573,39 @@ PlotPeakByLogValue::makeNames() const {
return nameList;
}

/**
* Formats the minimizer string for a given spectrum from a given workspace.
*
* @param wsName Name of workspace being fitted
* @param specIndex Index of spectrum being fitted
* @return Formatted minimizer string
*/
std::string
PlotPeakByLogValue::getMinimizerString(const std::string &wsName,
const std::string &specIndex) {
std::string format = getPropertyValue("Minimizer");
std::string wsBaseName = wsName + "_" + specIndex;
boost::replace_all(format, "$wsname", wsName);
boost::replace_all(format, "$wsindex", specIndex);
boost::replace_all(format, "$basename", wsBaseName);
boost::replace_all(format, "$outputname", m_baseName);

auto minimizer = FuncMinimizerFactory::Instance().createMinimizer(format);
auto minimizerProps = minimizer->getProperties();
for (auto it = minimizerProps.begin(); it != minimizerProps.end(); ++it) {
Mantid::API::WorkspaceProperty<> *wsProp =
dynamic_cast<Mantid::API::WorkspaceProperty<> *>(*it);
if (wsProp) {
std::string wsPropValue = (*it)->value();
if (wsPropValue != "") {
std::string wsPropName = (*it)->name();
m_minimizerWorkspaces[wsPropName].push_back(wsPropValue);
}
}
}

return format;
}

} // namespace CurveFitting
} // namespace Mantid
67 changes: 63 additions & 4 deletions Code/Mantid/Framework/CurveFitting/test/PlotPeakByLogValueTest.h
Expand Up @@ -57,6 +57,21 @@ namespace
};

DECLARE_FUNCTION(PLOTPEAKBYLOGVALUETEST_Fun)

class PropertyNameIs
{
public:
PropertyNameIs(std::string name) : m_name(name) {};

bool operator()(Mantid::Kernel::PropertyHistory_sptr p)
{
return p->name() == m_name;
}

private:
std::string m_name;
};

}

class PlotPeak_Expression
Expand Down Expand Up @@ -205,9 +220,9 @@ class PlotPeakByLogValueTest : public CxxTest::TestSuite

deleteData();
WorkspaceCreationHelper::removeWS("PlotPeakResult");

}


void testWorkspaceList_plotting_against_ws_names()
{
createData();
Expand Down Expand Up @@ -363,7 +378,7 @@ class PlotPeakByLogValueTest : public CxxTest::TestSuite
void test_createOutputOptionMultipleWorkspaces()
{
createData();

PlotPeakByLogValue alg;
alg.initialize();
alg.setPropertyValue("Input","PlotPeakGroup_0;PlotPeakGroup_1;PlotPeakGroup_2");
Expand Down Expand Up @@ -432,11 +447,55 @@ class PlotPeakByLogValueTest : public CxxTest::TestSuite
auto fit = AnalysisDataService::Instance().retrieveWS<const MatrixWorkspace>(wsNames[i]);
TS_ASSERT( fit );
TS_ASSERT( fit->getNumberHistograms() == 5);
}
}

AnalysisDataService::Instance().clear();
}


void testMinimizer()
{
createData();

PlotPeakByLogValue alg;
alg.initialize();
alg.setPropertyValue("Input","PlotPeakGroup_0");
alg.setPropertyValue("OutputWorkspace","PlotPeakResult");
alg.setProperty("CreateOutput", true);
alg.setPropertyValue("WorkspaceIndex","1");
alg.setPropertyValue("Function","name=LinearBackground,A0=1,A1=0.3;name=Gaussian,PeakCentre=5,Height=2,Sigma=0.1");
alg.setPropertyValue("MaxIterations", "50");
// This is a stupid use case but will at least demonstrate the functionality
alg.setPropertyValue("Minimizer", "Levenberg-Marquardt,AbsError=0.01,RelError=$wsindex");

alg.execute();
TS_ASSERT(alg.isExecuted());

auto fits = AnalysisDataService::Instance().retrieveWS<const WorkspaceGroup>("PlotPeakResult_Workspaces");
TS_ASSERT(fits);

if (fits->size() > 0)
{
// Get the Fit algorithm history
auto fit = fits->getItem(0);
const auto & wsHistory = fit->getHistory();
const auto & child = wsHistory.getAlgorithmHistory(0);
TS_ASSERT_EQUALS(child->name(), "Fit");
const auto & properties = child->getProperties();

// Check max iterations property
PropertyNameIs maxIterationsCheck("MaxIterations");
auto prop = std::find_if(properties.begin(), properties.end(), maxIterationsCheck);
TS_ASSERT_EQUALS((*prop)->value(), "50");

// Check minimizer property
PropertyNameIs minimizerCheck("Minimizer");
prop = std::find_if(properties.begin(), properties.end(), minimizerCheck);
TS_ASSERT_EQUALS((*prop)->value(), "Levenberg-Marquardt,AbsError=0.01,RelError=1");
}
}


private:

WorkspaceGroup_sptr m_wsg;
Expand Down Expand Up @@ -487,7 +546,7 @@ class PlotPeakByLogValueTest : public CxxTest::TestSuite

}
xdata.access()[i] = xValue;
}
}
testWS->setX(0, xdata);
testWS->setX(1, xdata);
return testWS;
Expand Down
1 change: 1 addition & 0 deletions Code/Mantid/Framework/Kernel/src/DateValidator.cpp
Expand Up @@ -26,6 +26,7 @@ struct tm getTimeValue(const std::string &sDate, std::string &error) {
timeinfo.tm_mon = 0;
timeinfo.tm_year = 0;
timeinfo.tm_wday = 0;
timeinfo.tm_yday = 0;
timeinfo.tm_isdst = -1;

std::basic_string<char>::size_type index, off = 0;
Expand Down
Expand Up @@ -2,8 +2,9 @@
#include "MantidKernel/V3D.h"
#include <boost/python/class.hpp>
#include <boost/python/operators.hpp>
#include <boost/python/return_value_policy.hpp>
#include <boost/python/copy_const_reference.hpp>
#include <boost/python/register_ptr_to_python.hpp>
#include <boost/python/return_value_policy.hpp>

using Mantid::Kernel::Quat;
using Mantid::Kernel::V3D;
Expand All @@ -22,6 +23,8 @@ using boost::python::return_value_policy;
void export_Quat()
// clang-format on
{
boost::python::register_ptr_to_python<boost::shared_ptr<Quat> >();

//Quat class
class_< Quat >("Quat", "Quaternions are the 3D generalization of complex numbers. "
"Quaternions are used for roations in 3D spaces and often implemented for "
Expand Down
@@ -1,6 +1,8 @@
#include "MantidPythonInterface/kernel/StlExportDefinitions.h"

#include "MantidKernel/DateAndTime.h"
#include "MantidKernel/Quat.h"
#include "MantidKernel/V3D.h"

using Mantid::PythonInterface::std_vector_exporter;
using Mantid::PythonInterface::std_set_exporter;
Expand All @@ -18,6 +20,8 @@ void exportStlContainers()
std_vector_exporter<bool>::wrap("std_vector_bool");
std_vector_exporter<std::string>::wrap("std_vector_str");
std_vector_exporter<Mantid::Kernel::DateAndTime>::wrap("std_vector_dateandtime");
std_vector_exporter<Mantid::Kernel::Quat>::wrap("std_vector_quat");
std_vector_exporter<Mantid::Kernel::V3D>::wrap("std_vector_v3d");
//std::set
std_set_exporter<int>::wrap("std_set_int");
std_set_exporter<std::string>::wrap("std_set_str");
Expand Down

0 comments on commit 38caeb4

Please sign in to comment.