-
Notifications
You must be signed in to change notification settings - Fork 122
/
SaveMDTest.h
290 lines (220 loc) · 8.43 KB
/
SaveMDTest.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
285
286
287
288
289
#ifndef MANTID_MDEVENTS_SAVEMDEWTEST_H_
#define MANTID_MDEVENTS_SAVEMDEWTEST_H_
#include "MantidKernel/System.h"
#include "MantidKernel/Timer.h"
#include "MantidMDEvents/MDEventFactory.h"
#include "MantidMDAlgorithms/SaveMD.h"
#include "MantidTestHelpers/MDEventsTestHelper.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/IMDEventWorkspace.h"
#include <cxxtest/TestSuite.h>
#include <iomanip>
#include <iostream>
#include "MantidKernel/CPUTimer.h"
#include <Poco/File.h>
using namespace Mantid::MDEvents;
using namespace Mantid::MDAlgorithms;
using namespace Mantid::API;
using Mantid::Kernel::CPUTimer;
class SaveMDTester: public SaveMD
{
public:
void saveExperimentInfos(::NeXus::File * const file, IMDEventWorkspace_const_sptr ws)
{
this->saveExperimentInfos(file, ws);
}
};
/** Note: See the LoadMDTest class
* for a more thorough test that does
* a round-trip.
*/
class SaveMDTest : public CxxTest::TestSuite
{
public:
void test_Init()
{
SaveMD alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
}
void test_exec()
{
do_test_exec(23, "SaveMDTest.nxs");
}
void xest_exec_noEvents()
{
// do_test_exec(0, "SaveMDTest_noEvents.nxs");
}
void test_MakeFileBacked()
{
do_test_exec(23, "SaveMDTest.nxs", true);
}
void test_MakeFileBacked_then_UpdateFileBackEnd()
{
do_test_exec(23, "SaveMDTest_updating.nxs", true, true);
}
void do_test_exec(size_t numPerBox, std::string filename, bool MakeFileBacked = false, bool UpdateFileBackEnd = false)
{
// Make a 1D MDEventWorkspace
MDEventWorkspace1Lean::sptr ws = MDEventsTestHelper::makeMDEW<1>(10, 0.0, 10.0, numPerBox);
// Make sure it is split
ws->splitBox();
AnalysisDataService::Instance().addOrReplace("SaveMDTest_ws", ws);
ws->refreshCache();
// There are this many boxes, so this is the max ID.
TS_ASSERT_EQUALS( ws->getBoxController()->getMaxId(), 11);
IMDEventWorkspace_sptr iws = ws;
CPUTimer tim;
SaveMD alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("InputWorkspace", "SaveMDTest_ws") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Filename", filename) );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("MakeFileBacked", MakeFileBacked) );
// clean up possible rubbish from the previous runs
std::string fullName = alg.getPropertyValue("Filename");
if (fullName!="")
if(Poco::File(fullName).exists()) Poco::File(fullName).remove();
alg.execute();
TS_ASSERT( alg.isExecuted() );
std::cout << tim << " to save " << ws->getBoxController()->getMaxId() << " boxes." << std::endl;
std::string this_filename = alg.getProperty("Filename");
TSM_ASSERT( "File was indeed created", Poco::File(this_filename).exists());
if (MakeFileBacked)
{
TSM_ASSERT("Workspace was made file-backed", ws->isFileBacked() );
TSM_ASSERT("File back-end no longer needs updating.", !ws->fileNeedsUpdating() );
}
// Continue the test
if (UpdateFileBackEnd)
do_test_UpdateFileBackEnd(ws, filename);
else
{
ws->getBoxController()->closeFile(true);
if (Poco::File(this_filename).exists()) Poco::File(this_filename).remove();
}
}
/// Add some data and update the back-end
void do_test_UpdateFileBackEnd(MDEventWorkspace1Lean::sptr ws, std::string filename)
{
size_t initial_numEvents = ws->getNPoints();
TSM_ASSERT_EQUALS("Starting off with 230 events.", initial_numEvents, 230);
// Add 100 events
for (size_t i=0; i<100; i++)
{
MDLeanEvent<1> ev(1.0, 1.0);
ev.setCenter(0, double(i) * 0.01 + 0.4);
ws->addEvent(ev);
}
ws->refreshCache();
// Manually set the flag that the algo would set
ws->setFileNeedsUpdating(true);
TSM_ASSERT_EQUALS("Correctly added 100 events to original 230.", ws->getNPoints(), 230+100);
SaveMD alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("InputWorkspace", "SaveMDTest_ws") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Filename", filename) );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("UpdateFileBackEnd", true) );
alg.execute();
TS_ASSERT( alg.isExecuted() );
// ws->getBoxController()->closeFile();
// Since there are 330 events, the file needs to be that big (or bigger).
TS_ASSERT_LESS_THAN( 330, ws->getBoxController()->getFile()->getInfo().dims[0]);
TSM_ASSERT("File back-end no longer needs updating.", !ws->fileNeedsUpdating() );
// Clean up file
ws->getBoxController()->closeFile(true);
//std::string fullPath = alg.getPropertyValue("Filename");
//if (Poco::File(fullPath).exists()) Poco::File(fullPath).remove();
}
void test_saveExpInfo()
{
std::string filename("MultiExperSaveTest.nxs");
// Make a 1D MDEventWorkspace
MDEventWorkspace1Lean::sptr ws = MDEventsTestHelper::makeMDEW<1>(10, 0.0, 10.0, 2);
// Make sure it is split
ws->splitBox();
Mantid::Geometry::Goniometer gon;
gon.pushAxis("Psi",0,1,0);
// add experiment infos
for(int i=0;i<80;i++)
{
ExperimentInfo_sptr ei = boost::shared_ptr<ExperimentInfo>(new ExperimentInfo());
ei->mutableRun().addProperty("Psi",double(i));
ei->mutableRun().addProperty("Ei",400.);
ei->mutableRun().setGoniometer(gon,true);
ws->addExperimentInfo(ei);
}
AnalysisDataService::Instance().addOrReplace("SaveMDTest_ws", ws);
ws->refreshCache();
// There are this many boxes, so this is the max ID.
TS_ASSERT_EQUALS( ws->getBoxController()->getMaxId(), 11);
IMDEventWorkspace_sptr iws = ws;
CPUTimer tim;
SaveMD alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("InputWorkspace", "SaveMDTest_ws") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Filename", filename) );
TS_ASSERT_THROWS_NOTHING( alg.setProperty("MakeFileBacked","0") );
alg.execute();
TS_ASSERT( alg.isExecuted() );
ws->getBoxController()->closeFile(true);
// if (Poco::File(filename).exists()) Poco::File(filename).remove();
}
/** Run SaveMD with the MDHistoWorkspace */
void doTestHisto(MDHistoWorkspace_sptr ws)
{
std::string filename = "SaveMDTestHisto.nxs";
SaveMD alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setProperty("InputWorkspace", ws) );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Filename", filename) );
alg.execute();
TS_ASSERT( alg.isExecuted() );
filename = alg.getPropertyValue("Filename");
TSM_ASSERT( "File was indeed created", Poco::File(filename).exists());
if (Poco::File(filename).exists())
Poco::File(filename).remove();
}
void test_histo2()
{
MDHistoWorkspace_sptr ws = MDEventsTestHelper::makeFakeMDHistoWorkspace(2.5, 2, 10, 10.0, 3.5, "histo2", 4.5);
doTestHisto(ws);
}
};
class SaveMDTestPerformance : public CxxTest::TestSuite
{
public:
MDEventWorkspace3Lean::sptr ws;
void setUp()
{
CPUTimer tim;
// Make a 1D MDEventWorkspace
ws = MDEventsTestHelper::makeMDEW<3>(10, 0.0, 10.0, 0);
ws->getBoxController()->setSplitInto(5);
ws->getBoxController()->setSplitThreshold(2000);
AnalysisDataService::Instance().addOrReplace("SaveMDTestPerformance_ws", ws);
FrameworkManager::Instance().exec("FakeMDEventData", 4,
"InputWorkspace", "SaveMDTestPerformance_ws", "UniformParams", "10000000");
std::cout << tim << " to fake the data." << std::endl;
ws->refreshCache();
std::cout << tim << " to refresh cache." << std::endl;
// // There are this many boxes, so this is the max ID.
// TS_ASSERT_EQUALS( ws->getBoxController()->getMaxId(), 11111);
}
void test_exec_3D()
{
CPUTimer tim;
SaveMD alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("InputWorkspace", "SaveMDTestPerformance_ws") );
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("Filename", "SaveMDTestPerformance.nxs") );
alg.execute();
TS_ASSERT( alg.isExecuted() );
std::cout << tim << " to save " << ws->getBoxController()->getMaxId() << " boxes with " << double(ws->getNPoints())/1e6 << " million events." << std::endl;
}
};
#endif /* MANTID_MDEVENTS_SAVEMDEWTEST_H_ */