Commit d70eb89c authored by Anthony Lim's avatar Anthony Lim
Browse files

refs #26878 ALF view model tests completed

parent c1032547
...@@ -34,18 +34,26 @@ ALFCustomInstrumentModel::ALFCustomInstrumentModel() ...@@ -34,18 +34,26 @@ ALFCustomInstrumentModel::ALFCustomInstrumentModel()
} }
/* /*
* Loads data for use in ALFView * Runs load data alg
* Loads data, normalise to current and then converts to d spacing
* @param name:: string name for ALF data * @param name:: string name for ALF data
* @return std::pair<int,std::string>:: the run number and status
*/ */
std::pair<int, std::string> void ALFCustomInstrumentModel::loadAlg(const std::string &name) {
ALFCustomInstrumentModel::loadData(const std::string &name) {
auto alg = AlgorithmManager::Instance().create("Load"); auto alg = AlgorithmManager::Instance().create("Load");
alg->initialize(); alg->initialize();
alg->setProperty("Filename", name); alg->setProperty("Filename", name);
alg->setProperty("OutputWorkspace", m_tmpName); // write to tmp ws alg->setProperty("OutputWorkspace", m_tmpName); // write to tmp ws
alg->execute(); alg->execute();
}
/*
* Loads data for use in ALFView
* Loads data, normalise to current and then converts to d spacing
* @param name:: string name for ALF data
* @return std::pair<int,std::string>:: the run number and status
*/
std::pair<int, std::string>
ALFCustomInstrumentModel::loadData(const std::string &name) {
loadAlg(name);
auto ws = auto ws =
AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(m_tmpName); AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(m_tmpName);
int runNumber = ws->getRunNumber(); int runNumber = ws->getRunNumber();
...@@ -73,17 +81,17 @@ std::map<std::string, bool> ALFCustomInstrumentModel::isDataValid() { ...@@ -73,17 +81,17 @@ std::map<std::string, bool> ALFCustomInstrumentModel::isDataValid() {
auto ws = auto ws =
AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(m_tmpName); AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(m_tmpName);
bool isItALF = false; bool isItALF = false;
bool isItDSpace = false;
if (ws->getInstrument()->getName() == m_instrumentName) { if (ws->getInstrument()->getName() == m_instrumentName) {
isItALF = true; isItALF = true;
} }
auto axis = ws->getAxis(0); auto axis = ws->getAxis(0);
auto unit = axis->unit()->unitID(); auto unit = axis->unit()->unitID();
if (unit == "dSpacing") { bool isItDSpace = true;
isItDSpace = true; if (unit != "dSpacing"){
isItDSpace = false;
} }
return {{"IsValidInstrument", isItALF}, {"IsItDSpace", isItDSpace}}; return {{"IsValidInstrument", isItALF}, {"IsItDSpace", isItDSpace}, {unit, true}};
} }
/* /*
......
...@@ -22,9 +22,11 @@ class DLLExport ALFCustomInstrumentModel ...@@ -22,9 +22,11 @@ class DLLExport ALFCustomInstrumentModel
public: public:
ALFCustomInstrumentModel(); ALFCustomInstrumentModel();
virtual ~ALFCustomInstrumentModel(){}; virtual ~ALFCustomInstrumentModel(){};
// virtual so we can patch them later
virtual void loadAlg(const std::string &name);
virtual void transformData();
std::pair<int, std::string> loadData(const std::string &name) override; std::pair<int, std::string> loadData(const std::string &name) override;
std::map<std::string, bool> isDataValid(); std::map<std::string, bool> isDataValid();
void transformData();
void storeSingleTube(const std::string &name); void storeSingleTube(const std::string &name);
void averageTube(); void averageTube();
bool hasTubeBeenExtracted(const std::string &name); bool hasTubeBeenExtracted(const std::string &name);
......
...@@ -40,13 +40,13 @@ protected: ...@@ -40,13 +40,13 @@ protected:
void loadSideEffects() override; void loadSideEffects() override;
std::pair<instrumentSetUp, instrumentObserverOptions> setupALFInstrument(); std::pair<instrumentSetUp, instrumentObserverOptions> setupALFInstrument();
public:
private:
void setUpInstrumentAnalysisSplitter() override; void setUpInstrumentAnalysisSplitter() override;
void extractSingleTube(); void extractSingleTube();
void averageTube(); void averageTube();
private:
ALFCustomInstrumentView *m_view; ALFCustomInstrumentView *m_view;
ALFCustomInstrumentModel *m_model; ALFCustomInstrumentModel *m_model;
MantidWidgets::PlotFitAnalysisPanePresenter *m_analysisPane; MantidWidgets::PlotFitAnalysisPanePresenter *m_analysisPane;
......
...@@ -8,26 +8,69 @@ ...@@ -8,26 +8,69 @@
#define MANTIDQT_ALFCUSTOMINSTRUMENTMODELTEST_H_ #define MANTIDQT_ALFCUSTOMINSTRUMENTMODELTEST_H_
#include <cxxtest/TestSuite.h> #include <cxxtest/TestSuite.h>
#include <gmock/gmock.h>
#include "ALFCustomInstrumentModel.h" #include "ALFCustomInstrumentModel.h"
#include "MantidAPI/AlgorithmManager.h" #include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/AnalysisDataService.h" #include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/FrameworkManager.h" #include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/MatrixWorkspace_fwd.h" #include "MantidAPI/MatrixWorkspace_fwd.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
#include "MantidGeometry/Instrument.h"
#include "MantidAPI/NumericAxis.h"
#include <string> #include <string>
#include <utility> #include <utility>
using namespace Mantid::API; using namespace Mantid::API;
using namespace MantidQt::CustomInterfaces; using namespace MantidQt::CustomInterfaces;
using namespace testing;
using Mantid::Geometry::Instrument;
namespace{ namespace{
const std::string notALFFile = "ZOOM00006113.nxs"; const std::string notALFFile = "ZOOM00006113.nxs";
} }
class ALFModelTest : public ALFCustomInstrumentModel{
public:
ALFModelTest():m_loadCount(0),m_transformCount(0){};
~ALFModelTest(){};
void loadAlg(const std::string &name) override{(void) name;m_loadCount+=1;};
void transformData() override {m_transformCount+=1;};
int getLoadCount(){return m_loadCount;};
int getTransformCount(){return m_transformCount;};
private:
int m_loadCount;
int m_transformCount;
};
class mockData{
public:
mockData(const std::string &name, const std::string &instName, const int &run, const bool TOF):m_name(name){
std::set<long int> masks;
auto ws = WorkspaceCreationHelper::create2DWorkspaceWithValuesAndXerror(1, 10, false, 0.1, 0.2, 0.01,0.3,masks);
//set instrument
boost::shared_ptr<Instrument> inst = boost::make_shared<Instrument>();
inst->setName(instName);
//set run
ws->mutableRun().addProperty("run_number", run,true);
// set units
ws->setInstrument(inst);
auto axis = ws->getAxis(0);
if(TOF){axis->setUnit("TOF");}else{
axis->setUnit("dSpacing");}
AnalysisDataService::Instance().addOrReplace(m_name, ws);
}
~mockData(){
AnalysisDataService::Instance().remove(m_name);
}
private:
std::string m_name;
};
class ALFCustomInstrumentModelTest : public CxxTest::TestSuite { class ALFCustomInstrumentModelTest : public CxxTest::TestSuite {
public: public:
/// WorkflowAlgorithms do not appear in the FrameworkManager without this line
ALFCustomInstrumentModelTest() { FrameworkManager::Instance(); } ALFCustomInstrumentModelTest() { FrameworkManager::Instance(); }
static ALFCustomInstrumentModelTest *createSuite() { return new ALFCustomInstrumentModelTest(); } static ALFCustomInstrumentModelTest *createSuite() { return new ALFCustomInstrumentModelTest(); }
...@@ -35,59 +78,70 @@ public: ...@@ -35,59 +78,70 @@ public:
static void destroySuite(ALFCustomInstrumentModelTest *suite) { delete suite; } static void destroySuite(ALFCustomInstrumentModelTest *suite) { delete suite; }
void setUp() override { void setUp() override {
//m_workspace = createWorkspace(4, 3); m_model = new ALFModelTest();
//m_ads = std::make_unique<SetUpADSWithWorkspace>("Name", m_workspace);
m_model = new ALFCustomInstrumentModel();
} }
void tearDown() override { void tearDown() override {
AnalysisDataService::Instance().clear(); AnalysisDataService::Instance().clear();
delete m_model; delete m_model;
//m_ads.reset();
//m_workspace.reset();
//m_model.reset();
} }
void test_loadData() { void test_loadData() {
return; auto data = mockData("ALF_tmp","ALF",6113,true);
//auto loadResult = m_model->loadData("ALF"); TS_ASSERT_EQUALS(m_model->getLoadCount(),0);
//TS_ASSERT_EQUALS(loadResult.first, 1);
//TS_ASSERT_EQUALS(loadResult.second, "success"); std::pair<int, std::string> loadResult = m_model->loadData(notALFFile);
TS_ASSERT_EQUALS(m_model->getLoadCount(),1);
TS_ASSERT_EQUALS(m_model->getTransformCount(),1);
TS_ASSERT_EQUALS(loadResult.first, 6113);
TS_ASSERT_EQUALS(loadResult.second, "success");
} }
void test_loadDataNotALF(){ void test_loadDataNotALF(){
auto data = mockData("ALF_tmp","EMU",6113,true);
TS_ASSERT_EQUALS(m_model->getLoadCount(),0);
std::pair<int, std::string> loadResult = m_model->loadData(notALFFile); std::pair<int, std::string> loadResult = m_model->loadData(notALFFile);
TS_ASSERT_EQUALS(m_model->getLoadCount(),1);
TS_ASSERT_EQUALS(m_model->getTransformCount(),0);
TS_ASSERT_EQUALS(loadResult.first, 6113); TS_ASSERT_EQUALS(loadResult.first, 6113);
TS_ASSERT_EQUALS(loadResult.second, "Not the correct instrument, expected ALF"); TS_ASSERT_EQUALS(loadResult.second, "Not the correct instrument, expected ALF");
} }
void test_loadDataDSpace(){ void test_loadDataDSpace(){
//auto loadResult = m_model->loadData("ALF"); auto data = mockData("ALF_tmp","ALF",6113,false);
//TS_ASSERT_EQUALS(loadResult.first, 1); TS_ASSERT_EQUALS(m_model->getLoadCount(),0);
//TS_ASSERT_EQUALS(loadResult.second, "success");
std::pair<int, std::string> loadResult = m_model->loadData(notALFFile);
TS_ASSERT_EQUALS(m_model->getLoadCount(),1);
TS_ASSERT_EQUALS(m_model->getTransformCount(),0);
TS_ASSERT_EQUALS(loadResult.first, 6113);
TS_ASSERT_EQUALS(loadResult.second, "success");
} }
void test_isDataValid(){ void test_isDataValid(){
return; auto data = mockData("ALF_tmp","ALF",6113,true);
} std::map<std::string, bool> isDataValid = m_model->isDataValid();
void test_isDataValidNotALF(){ TS_ASSERT(isDataValid["IsValidInstrument"])
auto alg = AlgorithmManager::Instance().create("Load"); TS_ASSERT(!isDataValid["IsItDSpace"])
alg->initialize();
alg->setProperty("Filename",notALFFile);
alg->setProperty("OutputWorkspace","ALF_tmp");
alg->execute();
std::map<std::string,bool> result = m_model->isDataValid(); }
TS_ASSERT(!result["IsValidInstrument"])
TS_ASSERT(!result["IsItDspace"])
// clean up
AnalysisDataService::Instance().remove("ALF_tmp");
void test_isDataValidNotALF(){
auto data = mockData("ALF_tmp","EMU",6113,true);
std::map<std::string, bool> isDataValid = m_model->isDataValid();
TS_ASSERT(!isDataValid["IsValidInstrument"])
TS_ASSERT(!isDataValid["IsItDSpace"])
} }
void test_isDataValidDSpace(){ void test_isDataValidDSpace(){
return; auto data = mockData("ALF_tmp","ALF",6113,false);
std::map<std::string, bool> isDataValid = m_model->isDataValid();
TS_ASSERT(isDataValid["IsValidInstrument"]);
TS_ASSERT(isDataValid["IsItDSpace"]);
} }
void test_transformData(){ void test_transformData(){
...@@ -95,50 +149,180 @@ public: ...@@ -95,50 +149,180 @@ public:
} }
void test_storeSingleTube(){ void test_storeSingleTube(){
return; auto data = mockData("CURVES","ALF",6113,false);
m_model->storeSingleTube("test");
auto outputWS = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("extractedTubes_test");
TS_ASSERT_DELTA(outputWS->readX(0)[0],-22.9, 0.1);
TS_ASSERT_EQUALS(outputWS->readY(0)[0],0.2);
TS_ASSERT_DELTA(outputWS->readX(0)[9],492.7, 0.1);
TS_ASSERT_EQUALS(outputWS->readY(0)[9],0.2);
AnalysisDataService::Instance().remove("extractedTubes_test");
} }
void test_averageTube(){ void test_averageTube(){
return; int run = 6113;
auto data = mockData("CURVES","ALF",run,false);
m_model->setCurrentRun(run);
m_model->extractSingleTube();
// check original y values
auto tmpWS = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("extractedTubes_ALF6113");
TS_ASSERT_EQUALS(tmpWS->readY(0)[1],0.2);
TS_ASSERT_EQUALS(tmpWS->readY(0)[9],0.2);
// create another ws to add
std::set<long int> masks;
auto ws = WorkspaceCreationHelper::create2DWorkspaceWithValuesAndXerror(1, 10, false, 1.1, 2.2, 0.01,0.3,masks);
boost::shared_ptr<Instrument> inst = boost::make_shared<Instrument>();
inst->setName("ALF");
ws->mutableRun().addProperty("run_number", run,true);
ws->setInstrument(inst);
auto axis = ws->getAxis(0);
axis->setUnit("dSpacing");
AnalysisDataService::Instance().addOrReplace("CURVES", ws);
// check second WS y values
TS_ASSERT_DELTA(ws->readY(0)[1], 2.2, 0.001);
TS_ASSERT_DELTA(ws->readY(0)[9], 2.2, 0.001);
m_model->averageTube();
// check averages: (2.2+0.2)/2 = 2.4/2 = 1.2
auto outputWS = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("extractedTubes_ALF6113");
TS_ASSERT_DELTA(outputWS->readX(0)[1],34.4, 0.1);
TS_ASSERT_DELTA(outputWS->readY(0)[1], 1.2, 0.01);
TS_ASSERT_DELTA(outputWS->readX(0)[9],492.7, 0.1);
TS_ASSERT_DELTA(outputWS->readY(0)[9], 1.2, 0.01);
// create another ws to add
auto ws3 = WorkspaceCreationHelper::create2DWorkspaceWithValuesAndXerror(1, 10, false, 1.1, 3.2, 0.01,0.3,masks);
inst = boost::make_shared<Instrument>();
inst->setName("ALF");
ws3->mutableRun().addProperty("run_number", run,true);
ws3->setInstrument(inst);
axis = ws3->getAxis(0);
axis->setUnit("dSpacing");
AnalysisDataService::Instance().addOrReplace("CURVES", ws3);
// check second WS y values
TS_ASSERT_DELTA(ws3->readY(0)[1], 3.2, 0.001);
TS_ASSERT_DELTA(ws3->readY(0)[9], 3.2, 0.001);
m_model->averageTube();
// check averages: (2.2+0.2+3.2)/2 = 5.6/3 = 1.8666
outputWS = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>("extractedTubes_ALF6113");
TS_ASSERT_DELTA(outputWS->readX(0)[1],34.4, 0.1);
TS_ASSERT_DELTA(outputWS->readY(0)[1], 1.86, 0.01);
TS_ASSERT_DELTA(outputWS->readX(0)[9],492.7, 0.1);
TS_ASSERT_DELTA(outputWS->readY(0)[9], 1.86, 0.01);
AnalysisDataService::Instance().remove("extractedTubes_ALF6113");
} }
void test_hasTubeBeenExtracted(){ void test_hasTubeBeenExtracted(){
return; const std::string name = "test";
// not extracted -> false
TS_ASSERT(!m_model->hasTubeBeenExtracted(name));
// create data to store
auto data = mockData("CURVES","ALF",6113,false);
m_model->storeSingleTube(name);
// stored data -> true
TS_ASSERT(m_model->hasTubeBeenExtracted(name));
} }
void test_extractTubeCondition(){ void test_extractTubeCondition(){
return; std::map<std::string, bool> conditions = { {"plotStored", true}, {"hasCurve",true},{"isTube", true}};
TS_ASSERT(m_model->extractTubeConditon(conditions));
} }
void test_extractTubeConditionNotTube(){ void test_extractTubeConditionNotTube(){
return; std::map<std::string, bool> conditions = { {"plotStored", true}, {"hasCurve",true},{"isTube", false}};
TS_ASSERT(!m_model->extractTubeConditon(conditions));
} }
void test_extractTubeConditionNoPlot(){ void test_extractTubeConditionNoPlot(){
return; std::map<std::string, bool> conditions = { {"plotStored", false}, {"hasCurve",true},{"isTube", true}};
TS_ASSERT(m_model->extractTubeConditon(conditions));
}
void test_extractTubeConditionNoCurve(){
std::map<std::string, bool> conditions = { {"plotStored", true}, {"hasCurve",false},{"isTube", true}};
TS_ASSERT(m_model->extractTubeConditon(conditions));
}
void test_extractTubeConditionNoPlotOrCurve(){
std::map<std::string, bool> conditions = { {"plotStored", false}, {"hasCurve",false},{"isTube", true}};
TS_ASSERT(!m_model->extractTubeConditon(conditions));
} }
void test_averageTubeCondition(){ void test_averageTubeCondition(){
return; std::map<std::string, bool> conditions = { {"plotStored", true}, {"hasCurve",true},{"isTube", true}};
} int run = 6113;
auto data = mockData("CURVES","ALF",run,false);
m_model->setCurrentRun(run);
m_model->extractSingleTube();
TS_ASSERT(m_model->averageTubeConditon(conditions));
AnalysisDataService::Instance().remove("extractedTubes_ALF6113");
}
void test_averageTubeConditionNotTube(){ void test_averageTubeConditionNotTube(){
return; std::map<std::string, bool> conditions = { {"plotStored", true}, {"hasCurve",true},{"isTube", false}};
} int run = 6113;
auto data = mockData("CURVES","ALF",run,false);
m_model->setCurrentRun(run);
m_model->extractSingleTube();
TS_ASSERT(!m_model->averageTubeConditon(conditions));
AnalysisDataService::Instance().remove("extractedTubes_ALF6113");
}
void test_averageTubeConditionNoPlot(){ void test_averageTubeConditionNoPlot(){
return; std::map<std::string, bool> conditions = { {"plotStored", false}, {"hasCurve",false},{"isTube", true}};
int run = 6113;
auto data = mockData("CURVES","ALF",run,false);
m_model->setCurrentRun(run);
m_model->extractSingleTube();
TS_ASSERT(!m_model->averageTubeConditon(conditions));
AnalysisDataService::Instance().remove("extractedTubes_ALF6113");
} }
void test_averageTubeConditionNothingToAverage(){ void test_averageTubeConditionNothingToAverage(){
return; std::map<std::string, bool> conditions = { {"plotStored", true}, {"hasCurve",true},{"isTube", true}};
} int run = 6113;
// the extraced ws will exist but average will be 0 -> change runs
auto data = mockData("extractedTubes_ALF6113","ALF",run,false);
m_model->setCurrentRun(run);
void test_defaultFunction(){ TS_ASSERT(!m_model->averageTubeConditon(conditions));
return; }
void test_averageTubeConditionNoWSToAverage(){
std::map<std::string, bool> conditions = { {"plotStored", true}, {"hasCurve",true},{"isTube", true}};
int run = 6113;
auto data = mockData("CURVES","ALF",run,false);
m_model->setCurrentRun(run);
m_model->extractSingleTube();
// the extraced ws will not exist but average will be 1
AnalysisDataService::Instance().remove("extractedTubes_ALF6113");
TS_ASSERT(!m_model->averageTubeConditon(conditions));
} }
void test_defaultFunction(){
auto function = m_model->getDefaultFunction();
TS_ASSERT_DELTA(function->getParameter("f0.A0"), 0.0, 0.01);
TS_ASSERT_DELTA(function->getParameter("f1.Height"), 3.0, 0.01);
TS_ASSERT_DELTA(function->getParameter("f1.PeakCentre"), 0.0, 0.01);
TS_ASSERT_DELTA(function->getParameter("f1.Sigma"), 1.0, 0.01);
}
private: private:
ALFCustomInstrumentModel *m_model; ALFModelTest *m_model;
}; };
#endif /* MANTIDQT_ALFCUSTOMINSTRUMENTMODELTEST_H_ */ #endif /* MANTIDQT_ALFCUSTOMINSTRUMENTMODELTEST_H_ */
...@@ -8,17 +8,58 @@ ...@@ -8,17 +8,58 @@
#define MANTIDQT_ALFCUSTOMINSTRUMENTPRESENTERTEST_H_ #define MANTIDQT_ALFCUSTOMINSTRUMENTPRESENTERTEST_H_
#include <cxxtest/TestSuite.h> #include <cxxtest/TestSuite.h>
#include <gmock/gmock.h>
#include "ALFCustomInstrumentModel.h" #include "ALFCustomInstrumentModel.h"
#include "ALFCustomInstrumentView.h"
#include "ALFCustomInstrumentPresenter.h"
#include "MantidQtWidgets/InstrumentView/PlotFitAnalysisPanePresenter.h"
#include "MantidQtWidgets/InstrumentView/PlotFitAnalysisPaneView.h"
#include "MantidQtWidgets/InstrumentView/PlotFitAnalysisPaneModel.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/AnalysisDataService.h" #include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/FrameworkManager.h" #include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/MatrixWorkspace_fwd.h" #include "MantidAPI/MatrixWorkspace_fwd.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
#include "MantidGeometry/Instrument.h"
#include <string>