Newer
Older
#ifndef DYNAMIC_REBINNING_FROM_XML_TEST_H_
#define DYNAMIC_REBINNING_FROM_XML_TEST_H_
#include "Poco/DOM/DOMParser.h"
#include "Poco/DOM/Document.h"
#include "Poco/DOM/Element.h"
#include "Poco/DOM/NodeList.h"
#include "Poco/DOM/NodeIterator.h"
#include "Poco/DOM/NodeFilter.h"
#include <cxxtest/TestSuite.h>
#include <boost/scoped_ptr.hpp>
Owen Arnold
committed
#include "MantidMDAlgorithms/CompositeImplicitFunction.h"
#include "MantidMDAlgorithms/DynamicRebinFromXML.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/WorkspaceFactory.h"
Owen Arnold
committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MDDataObjects/MDWorkspace.h"
#include "MDDataObjects/MD_FileFormatFactory.h"
using namespace Mantid::API;
using namespace Mantid::Kernel;
using namespace Mantid::MDAlgorithms;
using namespace Mantid::Geometry;
class DynamicRebinFromXMLAlgorithmTest: public CxxTest::TestSuite
{
private:
//Helper method to create the geometry.
static Mantid::Geometry::MDGeometry* constructMDGeometry()
{
using namespace Mantid::Geometry;
std::set<MDBasisDimension> basisDimensions;
basisDimensions.insert(MDBasisDimension("q1", true, 1));
basisDimensions.insert(MDBasisDimension("q2", true, 2));
basisDimensions.insert(MDBasisDimension("q3", true, 3));
basisDimensions.insert(MDBasisDimension("u1", false, 4));
UnitCell cell;
return new MDGeometry(MDGeometryBasis(basisDimensions, cell));
}
//Helper stock constructional method.
static Mantid::MDDataObjects::MDWorkspace* constructMDWorkspace()
{
using namespace Mantid::MDDataObjects;
using namespace Mantid::Geometry;
MDWorkspace* workspace = new MDWorkspace;
std::auto_ptr<IMD_FileFormat> pFile = MD_FileFormatFactory::getFileReader("../../../../Test/VATES/fe_demo.sqw");
workspace->load_workspace(boost::shared_ptr<IMD_FileFormat>(pFile.release()));
return workspace;
}
//Helper class exposes a few protected functions as an aid to development.
class ExposedDynamicRebinFromXML : public DynamicRebinFromXML
{
public:
std::string getWorkspaceName(Poco::XML::Element* pRootElem) const
{
//call method on base class.
return DynamicRebinFromXML::getWorkspaceName(pRootElem);
}
std::string getWorkspaceLocation(Poco::XML::Element* pRootElem) const
{
//call method on base class.
return DynamicRebinFromXML::getWorkspaceLocation(pRootElem);
}
Mantid::API::ImplicitFunction* getImplicitFunction(Poco::XML::Element* pRootElem) const
{
//call method on base class.
return DynamicRebinFromXML::getImplicitFunction(pRootElem);
}
Mantid::Geometry::MDGeometryDescription* getMDGeometryDescriptionWithoutCuts(Poco::XML::Element* pRootElem) const
{
//call method on base class.
return DynamicRebinFromXML::getMDGeometryDescriptionWithoutCuts(pRootElem);
}
void ApplyImplicitFunctionToMDGeometryDescription(Mantid::Geometry::MDGeometryDescription* description, Mantid::API::ImplicitFunction* impFunction) const
{
//call method on base class.
return DynamicRebinFromXML::ApplyImplicitFunctionToMDGeometryDescription(description, impFunction);
}
};
//Helper method provides sample instructional xml.
static std::string MDInstructionXMLString()
{
return std::string("<?xml version=\"1.0\" encoding=\"utf-8\"?>") +
"<MDInstruction>" +
"<MDWorkspaceName>Input</MDWorkspaceName>" +
"<MDWorkspaceLocation>c:/Manid/Test/VATES/fe_demo.sqw</MDWorkspaceLocation>" +
"<DimensionSet>" +
"<Dimension ID=\"en\">" +
"<Name>Energy</Name>" +
"<UpperBounds>10</UpperBounds>" +
"<LowerBounds>0</LowerBounds>" +
"<NumberOfBins>4</NumberOfBins>" +
"<Integrated>" +
"<UpperLimit>0</UpperLimit>" +
"<LowerLimit>0</LowerLimit>" +
"</Integrated>" +
"</Dimension>" +
"<Dimension ID=\"qx\">" +
"<Name>Qx</Name>" +
"<UpperBounds>20</UpperBounds>" +
"<LowerBounds>0</LowerBounds>" +
"<NumberOfBins>5</NumberOfBins>" +
"<ReciprocalDimensionMapping>q1</ReciprocalDimensionMapping>" +
"</Dimension>" +
"<Dimension ID=\"qy\">" +
"<Name>Qy</Name>" +
"<UpperBounds>20</UpperBounds>" +
"<LowerBounds>0</LowerBounds>" +
"<NumberOfBins>7</NumberOfBins>" +
"<ReciprocalDimensionMapping>q2</ReciprocalDimensionMapping>" +
"</Dimension>" +
"<Dimension ID=\"qz\">" +
"<Name>Qz</Name>" +
"<UpperBounds>20</UpperBounds>" +
"<LowerBounds>6</LowerBounds>" +
"<NumberOfBins>6</NumberOfBins>" +
"<ReciprocalDimensionMapping>q3</ReciprocalDimensionMapping>" +
"</Dimension>" +
"<XDimension>" +
"<RefDimensionId>qy</RefDimensionId>" +
"</XDimension>" +
"<YDimension>" +
"<RefDimensionId>qx</RefDimensionId>" +
"</YDimension>" +
"<ZDimension>" +
"<RefDimensionId>qz</RefDimensionId>" +
"</ZDimension>" +
"<TDimension>" +
"<RefDimensionId>en</RefDimensionId>" +
"</TDimension>" +
"</DimensionSet>" +
"<Function>" +
"<Type>CompositeImplicitFunction</Type>" +
"<ParameterList/>" +
"<Function>" +
"<Type>BoxImplicitFunction</Type>" +
"<ParameterList>" +
"<Parameter>" +
"<Type>HeightParameter</Type>" +
"<Value>8</Value>" +
"</Parameter>" +
"<Parameter>" +
"<Type>WidthParameter</Type>" +
"<Value>6</Value>" +
"</Parameter>" +
"<Parameter>" +
"<Type>DepthParameter</Type>" +
"<Value>6</Value>" +
"</Parameter>" +
"<Parameter>" +
"<Type>OriginParameter</Type>" +
"<Value>0, 0, 0</Value>" +
"</Parameter>" +
"</ParameterList>" +
"</Function>" +
"<Function>" +
"<Type>CompositeImplicitFunction</Type>" +
"<ParameterList/>" +
"<Function>" +
"<Type>BoxImplicitFunction</Type>" +
"<ParameterList>" +
"<Parameter>" +
"<Type>WidthParameter</Type>" +
"<Value>4</Value>" +
"</Parameter>" +
"<Parameter>" +
"<Type>HeightParameter</Type>" +
"<Value>6</Value>" +
"</Parameter>" +
"<Parameter>" +
"<Type>DepthParameter</Type>" +
"<Value>6</Value>" +
"</Parameter>" +
"<Parameter>" +
"<Type>OriginParameter</Type>" +
"<Value>1, 1, 1</Value>" +
"</Parameter>" +
"</ParameterList>" +
"</Function>" +
"</Function>"
"</Function>" +
"</MDInstruction>";
}
static Poco::XML::Element* MDInstructionXML()
{
Poco::XML::DOMParser pParser;
std::string xmlToParse = MDInstructionXMLString();
Poco::XML::Document* pDoc = pParser.parseString(xmlToParse);
return pDoc->documentElement();
}
public:
void testName()
{
Mantid::MDAlgorithms::DynamicRebinFromXML xmlRebinAlg;
TSM_ASSERT_EQUALS ("Algorithm name should be Power", xmlRebinAlg.name(), "DynamicRebinFromXML" )
}
void testVersion()
{
Mantid::MDAlgorithms::DynamicRebinFromXML xmlRebinAlg;
TSM_ASSERT_EQUALS("Expected version is 1", xmlRebinAlg.version(), 1 )
}
void testInit()
{
Mantid::MDAlgorithms::DynamicRebinFromXML xmlRebinAlg;
xmlRebinAlg.initialize();
TS_ASSERT( xmlRebinAlg.isInitialized() )
const std::vector<Property*> props = xmlRebinAlg.getProperties();
TSM_ASSERT_EQUALS("There should only be 2 properties for this dyamic rebinning algorithm", props.size(), 2);
TS_ASSERT_EQUALS( props[0]->name(), "XMLInputString" );
TS_ASSERT( props[0]->isDefault() );
TS_ASSERT( dynamic_cast<PropertyWithValue<std::string>* >(props[0]) );
TS_ASSERT_EQUALS( props[1]->name(), "OutputWorkspace" );
TS_ASSERT( props[1]->isDefault() );
}
void testSetProperties()
{
Mantid::MDAlgorithms::DynamicRebinFromXML xmlRebinAlg;
xmlRebinAlg.initialize();
std::string xmlString = "ss";
xmlRebinAlg.setPropertyValue("XMLInputString", xmlString);
TSM_ASSERT_EQUALS("Property XMLInputString cannot be set and fetched correctly.", xmlString, xmlRebinAlg.getPropertyValue("XMLInputString"));
}
void testGetWorkspaceName()
{
ExposedDynamicRebinFromXML xmlRebinAlg;
TSM_ASSERT_EQUALS("The workspace name is not correctly extracted", "Input", xmlRebinAlg.getWorkspaceName(MDInstructionXML()));
}
void testGetWorkspaceLocation()
{
ExposedDynamicRebinFromXML xmlRebinAlg;
TSM_ASSERT_EQUALS("The workspace location is not correctly extracted", "c:/Manid/Test/VATES/fe_demo.sqw", xmlRebinAlg.getWorkspaceLocation(MDInstructionXML()));
}
void testGetImplicitFunction()
{
ExposedDynamicRebinFromXML xmlRebinAlg;
boost::scoped_ptr<Mantid::API::ImplicitFunction> impFunction(xmlRebinAlg.getImplicitFunction(MDInstructionXML()));
CompositeImplicitFunction* compFunction = dynamic_cast<CompositeImplicitFunction*>(impFunction.get());
TSM_ASSERT("Has not parsed implicit function(s) correctly", NULL != compFunction);
TSM_ASSERT_EQUALS("Has not parsed implicit function(s) correctly", 2, compFunction->getNFunctions());
}
void testGetMDDimensionDescription()
{
ExposedDynamicRebinFromXML xmlRebinAlg;
boost::scoped_ptr<Mantid::Geometry::MDGeometryDescription> geomDescription(xmlRebinAlg.getMDGeometryDescriptionWithoutCuts(MDInstructionXML()));
//Characteristic of existing behaviour rather than correct behaviour!
TSM_ASSERT_EQUALS("The xml generated from the dimension description did not match the expectation.", geomDescription->toXMLstring(), "TEST PROPERTY");
//Note that the MDGeometry description orders dimensions passed to it internally.
Alex Buts
committed
TSM_ASSERT_EQUALS("Wrong number of bins returned for first dimension", 7, geomDescription->dimDescription(0).nBins);
TSM_ASSERT_EQUALS("Wrong number of bins returned for second dimension", 5, geomDescription->dimDescription(1).nBins);
TSM_ASSERT_EQUALS("Wrong number of bins returned for third dimension", 6, geomDescription->dimDescription(2).nBins);
TSM_ASSERT_EQUALS("Wrong number of bins returned for fourth dimension", 4, geomDescription->dimDescription(3).nBins);
Alex Buts
committed
TSM_ASSERT_EQUALS("Incorrect axis name for first dimension", "Qy", geomDescription->dimDescription(0).AxisName);
TSM_ASSERT_EQUALS("Incorrect axis name for second dimension", "Qx", geomDescription->dimDescription(1).AxisName);
TSM_ASSERT_EQUALS("Incorrect axis name for third dimension", "Qz", geomDescription->dimDescription(2).AxisName);
TSM_ASSERT_EQUALS("Incorrect axis name for fourth dimension", "Energy", geomDescription->dimDescription(3).AxisName);
Alex Buts
committed
TSM_ASSERT_EQUALS("Incorrect id for first dimension", "qy", geomDescription->dimDescription(0).Tag);
TSM_ASSERT_EQUALS("Incorrect id for second dimension", "qx", geomDescription->dimDescription(1).Tag);
TSM_ASSERT_EQUALS("Incorrect id for third dimension", "qz", geomDescription->dimDescription(2).Tag);
TSM_ASSERT_EQUALS("Incorrect id for fourth dimension", "en", geomDescription->dimDescription(3).Tag);
}
void testIncorrectRootNode()
{
Mantid::MDAlgorithms::DynamicRebinFromXML xmlRebinAlg;
xmlRebinAlg.setRethrows(true);
xmlRebinAlg.initialize();
xmlRebinAlg.setPropertyValue("OutputWorkspace","WSCor");
std::string xmlString = "<Other></Other>";
xmlRebinAlg.setPropertyValue("XMLInputString", xmlString);
TSM_ASSERT_THROWS("Root node must be an MDInstruction", xmlRebinAlg.execute(), std::invalid_argument);
}
void testApplyImplicitFunctionToMDGeometryDescription()
{
ExposedDynamicRebinFromXML xmlRebinAlg;
MDGeometryDescription* description = xmlRebinAlg.getMDGeometryDescriptionWithoutCuts(MDInstructionXML());
ImplicitFunction* impFunction = xmlRebinAlg.getImplicitFunction(MDInstructionXML());
xmlRebinAlg.ApplyImplicitFunctionToMDGeometryDescription(description, impFunction);
Alex Buts
committed
TSM_ASSERT_EQUALS("Wrong x-min set via cut box.", -3, description->dimDescription(0).cut_min);
TSM_ASSERT_EQUALS("Wrong x-min set via cut box.", 3, description->dimDescription(0).cut_max);
TSM_ASSERT_EQUALS("Wrong y-min set via cut box.", -4, description->dimDescription(1).cut_min);
TSM_ASSERT_EQUALS("Wrong y-min set via cut box.", 4, description->dimDescription(1).cut_max);
TSM_ASSERT_EQUALS("Wrong z-min set via cut box.", -3, description->dimDescription(2).cut_min);
TSM_ASSERT_EQUALS("Wrong z-min set via cut box.", 3, description->dimDescription(2).cut_max);
}
void testExecute()
{
using namespace Mantid::MDDataObjects;
Owen Arnold
committed
MDWorkspace_sptr baseWs = MDWorkspace_sptr(constructMDWorkspace());
AnalysisDataService::Instance().add("Input", baseWs);
Mantid::MDAlgorithms::DynamicRebinFromXML xmlRebinAlg;
xmlRebinAlg.setRethrows(true);
xmlRebinAlg.initialize();
xmlRebinAlg.setPropertyValue("OutputWorkspace","MyOutputWS");
std::string xmlString = MDInstructionXMLString();
xmlRebinAlg.setPropertyValue("XMLInputString", xmlString);
xmlRebinAlg.execute();
MDWorkspace_sptr output = boost::dynamic_pointer_cast<MDWorkspace>(AnalysisDataService::Instance().retrieve("MyOutputWS"));
//Check that the rebinning worked as specified in the request.
TSM_ASSERT_EQUALS("Wrong number of bins in rebinned workspace x-dimension.", 7, output->getXDimension()->getNBins());
TSM_ASSERT_EQUALS("Wrong number of bins in rebinned workspace y-dimension.", 5, output->getYDimension()->getNBins());
TSM_ASSERT_EQUALS("Wrong number of bins in rebinned workspace z-dimension.", 6, output->getZDimension()->getNBins());
TSM_ASSERT_EQUALS("Wrong number of bins in rebinned workspace t-dimension.", 4, output->gettDimension()->getNBins());
Owen Arnold
committed
//840 = 7 * 5* 6 * 4
TSM_ASSERT_EQUALS("The image size should be the product of the number of bins accross dimensions", 840, output->get_spMDImage()->getDataSize());
}
};
#endif