LogManagerTest.h 16.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#ifndef LOG_MANAGER_TEST_H_
#define LOG_MANAGER_TEST_H_

#include "MantidAPI/LogManager.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/Matrix.h"
#include "MantidKernel/Property.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidKernel/V3D.h"
#include <cxxtest/TestSuite.h>
11
#include "MantidTestHelpers/NexusTestHelper.h"
12
13
14
15
16
17
18
#include "MantidGeometry/Instrument/Goniometer.h"

using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::Geometry;

// Helper class
19
20
21
22
namespace {
class ConcreteProperty : public Property {
public:
  ConcreteProperty() : Property("Test", typeid(int)) {}
23
24
25
26
27
  ConcreteProperty *clone() const override {
    return new ConcreteProperty(*this);
  }
  bool isDefault() const override { return true; }
  std::string getDefault() const override {
28
    return "getDefault() is not implemented in this class";
29
  }
30
31
32
33
34
35
36
  std::string value() const override { return "Nothing"; }
  std::string setValue(const std::string &) override { return ""; }
  std::string setValueFromProperty(const Property &) override { return ""; }
  std::string setDataItem(const boost::shared_ptr<DataItem>) override {
    return "";
  }
  Property &operator+=(Property const *) override { return *this; }
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
};

void addTestTimeSeries(LogManager &run, const std::string &name) {
  auto timeSeries = new TimeSeriesProperty<double>(name);
  timeSeries->addValue("2012-07-19T16:17:00", 2);
  timeSeries->addValue("2012-07-19T16:17:10", 3);
  timeSeries->addValue("2012-07-19T16:17:20", 4);
  timeSeries->addValue("2012-07-19T16:17:30", 5);
  timeSeries->addValue("2012-07-19T16:17:40", 6);
  timeSeries->addValue("2012-07-19T16:17:50", 20);
  timeSeries->addValue("2012-07-19T16:18:00", 21);
  timeSeries->addValue("2012-07-19T16:18:10", 22);
  timeSeries->addValue("2012-07-19T16:19:20", 23);
  timeSeries->addValue("2012-07-19T16:19:20", 24);
  run.addProperty(timeSeries);
}
53
54
}

55
56
57
58
59
60
61
62
void addTimeSeriesEntry(LogManager &runInfo, std::string name, double val) {
  TimeSeriesProperty<double> *tsp;
  tsp = new TimeSeriesProperty<double>(name);
  tsp->addValue("2011-05-24T00:00:00", val);
  runInfo.addProperty(tsp);
}

class LogManagerTest : public CxxTest::TestSuite {
63
public:
64
  void testAddGetData() {
65
66
67
    LogManager runInfo;

    Property *p = new ConcreteProperty();
68
    TS_ASSERT_THROWS_NOTHING(runInfo.addProperty(p));
69
70

    Property *pp = NULL;
71
72
73
74
75
76
77
78
79
80
81
82
    TS_ASSERT_THROWS_NOTHING(pp = runInfo.getProperty("Test"));
    TS_ASSERT_EQUALS(p, pp);
    TS_ASSERT(!pp->name().compare("Test"));
    TS_ASSERT(dynamic_cast<ConcreteProperty *>(pp));
    TS_ASSERT_THROWS(pp = runInfo.getProperty("NotThere"),
                     Exception::NotFoundError);

    std::vector<Property *> props = runInfo.getProperties();
    TS_ASSERT(!props.empty());
    TS_ASSERT_EQUALS(props.size(), 1);
    TS_ASSERT(!props[0]->name().compare("Test"));
    TS_ASSERT(dynamic_cast<ConcreteProperty *>(props[0]));
83
84
  }

85
  void testRemoveLogData() {
86
    LogManager runInfo;
87

88
    Property *p = new ConcreteProperty();
89
90
91
    TS_ASSERT_THROWS_NOTHING(runInfo.addProperty(p));
    TS_ASSERT_THROWS_NOTHING(runInfo.removeProperty("Test"));
    TS_ASSERT_EQUALS(runInfo.getProperties().size(), 0);
92
93
  }

94
  void testStartTime() {
95
96
    LogManager runInfo;
    // Nothing there yet
97
    TS_ASSERT_THROWS(runInfo.startTime(), std::runtime_error);
98
99
    // Add run_start and see that get picked up
    const std::string run_start("2013-12-19T13:38:00");
100
101
    auto run_start_prop =
        new PropertyWithValue<std::string>("run_start", run_start);
102
    runInfo.addProperty(run_start_prop);
103
    TS_ASSERT_EQUALS(runInfo.startTime(), DateAndTime(run_start));
104
105
    // Add start_time and see that get picked up in preference
    const std::string start_time("2013-12-19T13:40:00");
106
107
    auto start_time_prop =
        new PropertyWithValue<std::string>("start_time", start_time);
108
    runInfo.addProperty(start_time_prop);
109
    TS_ASSERT_EQUALS(runInfo.startTime(), DateAndTime(start_time));
110
111
112
    // But get back run_start again if start_time is equal to the epoch
    const std::string epoch("1990-01-01T00:00:00");
    start_time_prop->setValue(epoch);
113
    TS_ASSERT_EQUALS(runInfo.startTime(), DateAndTime(run_start));
114
115
    // And back to failure if they're both that
    run_start_prop->setValue(epoch);
116
    TS_ASSERT_THROWS(runInfo.startTime(), std::runtime_error);
117
118
119
120

    // Set run_start back to valid value and make start_time contain nonsense
    run_start_prop->setValue(run_start);
    start_time_prop->setValue("__");
121
    TS_ASSERT_EQUALS(runInfo.startTime(), DateAndTime(run_start));
122
123
    // Now make start_time a completely different property type
    runInfo.removeProperty("start_time");
124
125
    runInfo.addProperty(new PropertyWithValue<double>("start_time", 3.33));
    TS_ASSERT_EQUALS(runInfo.startTime(), DateAndTime(run_start));
126
127
    // Now make run_start something invalid
    run_start_prop->setValue("notADate");
128
    TS_ASSERT_THROWS(runInfo.startTime(), std::runtime_error);
129
130
    // And check things if it's the wrong property type
    runInfo.removeProperty("run_start");
131
132
    addTimeSeriesEntry(runInfo, "run_start", 4.44);
    TS_ASSERT_THROWS(runInfo.startTime(), std::runtime_error);
133
  }
134

135
  void testEndTime() {
136
137
    LogManager runInfo;
    // Nothing there yet
138
    TS_ASSERT_THROWS(runInfo.endTime(), std::runtime_error);
139
140
    // Add run_end and see that get picked up
    const std::string run_end("2013-12-19T13:38:00");
141
    auto run_end_prop = new PropertyWithValue<std::string>("run_end", run_end);
142
    runInfo.addProperty(run_end_prop);
143
    TS_ASSERT_EQUALS(runInfo.endTime(), DateAndTime(run_end));
144
145
    // Add end_time and see that get picked up in preference
    const std::string end_time("2013-12-19T13:40:00");
146
147
    auto end_time_prop =
        new PropertyWithValue<std::string>("end_time", end_time);
148
    runInfo.addProperty(end_time_prop);
149
    TS_ASSERT_EQUALS(runInfo.endTime(), DateAndTime(end_time));
150
151
152
153

    // Set run_end back to valid value and make end_time contain nonsense
    run_end_prop->setValue(run_end);
    end_time_prop->setValue("__");
154
    TS_ASSERT_EQUALS(runInfo.endTime(), DateAndTime(run_end));
155
156
    // Now make end_time a completely different property type
    runInfo.removeProperty("end_time");
157
158
    runInfo.addProperty(new PropertyWithValue<double>("end_time", 3.33));
    TS_ASSERT_EQUALS(runInfo.endTime(), DateAndTime(run_end));
159
160
    // Now make run_end something invalid
    run_end_prop->setValue("notADate");
161
    TS_ASSERT_THROWS(runInfo.endTime(), std::runtime_error);
162
163
    // And check things if it's the wrong property type
    runInfo.removeProperty("run_end");
164
165
    addTimeSeriesEntry(runInfo, "run_end", 4.44);
    TS_ASSERT_THROWS(runInfo.endTime(), std::runtime_error);
166
167
  }

168
  void testMemory() {
169
    LogManager runInfo;
170
171
    TS_ASSERT_EQUALS(runInfo.getMemorySize(), 0);

172
173
174
    Property *p = new ConcreteProperty();
    runInfo.addProperty(p);

175
176
    TS_ASSERT_EQUALS(runInfo.getMemorySize(),
                     sizeof(ConcreteProperty) + sizeof(void *));
177
178
  }

179
  void test_GetTimeSeriesProperty_Returns_TSP_When_Log_Exists() {
180
    LogManager runInfo;
181
    const std::string &name = "double_time_series";
182
183
184
    const double value = 10.9;
    addTimeSeriesEntry(runInfo, name, value);

185
    TimeSeriesProperty<double> *tsp(NULL);
186
187
188
189
    TS_ASSERT_THROWS_NOTHING(tsp = runInfo.getTimeSeriesProperty<double>(name));
    TS_ASSERT_DELTA(tsp->firstValue(), value, 1e-12);
  }

190
  void test_GetTimeSeriesProperty_Throws_When_Log_Does_Not_Exist() {
191
    LogManager runInfo;
192
193
    TS_ASSERT_THROWS(runInfo.getTimeSeriesProperty<double>("not_a_log"),
                     Exception::NotFoundError);
194
195
  }

196
197
  void
  test_GetTimeSeriesProperty_Throws_When_Log_Exists_But_Is_Not_Correct_Type() {
198
    LogManager runInfo;
199
    const std::string &name = "double_prop";
200
201
    runInfo.addProperty(name, 5.6); // Standard double property

202
203
    TS_ASSERT_THROWS(runInfo.getTimeSeriesProperty<double>(name),
                     std::invalid_argument);
204
205
  }

206
  void test_GetPropertyAsType_Throws_When_Property_Does_Not_Exist() {
207
    LogManager runInfo;
208
209
    TS_ASSERT_THROWS(runInfo.getPropertyValueAsType<double>("not_a_log"),
                     Exception::NotFoundError);
210
211
  }

212
  void test_GetPropertyAsType_Returns_Expected_Value_When_Type_Is_Correct() {
213
    LogManager runInfo;
214
    const std::string &name = "double_prop";
215
216
217
218
    const double value = 5.6;
    runInfo.addProperty(name, value); // Standard double property

    double retrieved(0.0);
219
220
    TS_ASSERT_THROWS_NOTHING(retrieved =
                                 runInfo.getPropertyValueAsType<double>(name));
221
222
223
    TS_ASSERT_DELTA(retrieved, value, 1e-12);
  }

224
  void test_GetPropertyAsType_Throws_When_Requested_Type_Does_Not_Match() {
225
226
227
    LogManager runInfo;
    runInfo.addProperty("double_prop", 6.7); // Standard double property

228
229
    TS_ASSERT_THROWS(runInfo.getPropertyValueAsType<int>("double_prop"),
                     std::invalid_argument);
230
231
  }

232
233
  void
  test_GetPropertyAsSingleValue_Throws_If_Type_Is_Not_Double_Or_TimeSeries_Double() {
234
235
236
237
    LogManager runInfo;
    const std::string name = "int_prop";
    runInfo.addProperty(name, 1); // Adds an int property

238
239
    TS_ASSERT_THROWS(runInfo.getPropertyAsSingleValue(name),
                     std::invalid_argument);
240
241
  }

242
243
  void
  test_GetPropertyAsSingleValue_Returns_Simple_Mean_By_Default_For_Time_Series() {
244
245
246
247
248
    LogManager runInfo;
    const std::string name = "series";
    addTestTimeSeries(runInfo, name);

    const double expectedValue(13.0);
249
250
    TS_ASSERT_DELTA(runInfo.getPropertyAsSingleValue(name), expectedValue,
                    1e-12);
251
252
  }

253
254
  void
  test_GetPropertyAsSingleValue_Returns_Correct_SingleValue_For_Each_StatisticType() {
255
256
257
    LogManager runInfo;
    const std::string name = "series";
    addTestTimeSeries(runInfo, name);
258
259
260
261
262
263
264
265
266
267
268
269
270

    TS_ASSERT_DELTA(runInfo.getPropertyAsSingleValue(name, Math::Mean), 13.0,
                    1e-12);
    TS_ASSERT_DELTA(runInfo.getPropertyAsSingleValue(name, Math::Minimum), 2.0,
                    1e-12);
    TS_ASSERT_DELTA(runInfo.getPropertyAsSingleValue(name, Math::Maximum), 24.0,
                    1e-12);
    TS_ASSERT_DELTA(runInfo.getPropertyAsSingleValue(name, Math::FirstValue),
                    2.0, 1e-12);
    TS_ASSERT_DELTA(runInfo.getPropertyAsSingleValue(name, Math::LastValue),
                    24.0, 1e-12);
    TS_ASSERT_DELTA(runInfo.getPropertyAsSingleValue(name, Math::Median), 13.0,
                    1e-12);
271
272
  }

273
274
  void
  test_GetPropertyAsSingleValue_Returns_Expected_Single_Value_On_Successive_Calls_With_Different_Stat_Types() {
275
276
277
278
    LogManager run;
    const std::string name = "series";
    addTestTimeSeries(run, name);

279
280
281
282
    TS_ASSERT_EQUALS(run.getPropertyAsSingleValue(name, Math::Mean), 13.0);
    TS_ASSERT_EQUALS(run.getPropertyAsSingleValue(name, Math::Mean), 13.0);
    TS_ASSERT_EQUALS(run.getPropertyAsSingleValue(name, Math::Minimum), 2.0);
    TS_ASSERT_EQUALS(run.getPropertyAsSingleValue(name, Math::Minimum), 2.0);
283
284
  }

285
286
  void
  test_GetPropertyAsSingleValue_Returns_Correct_Value_On_Second_Call_When_Log_Has_Been_Replaced() {
287
288
289
290
291
292
293
294
295
    LogManager runInfo;
    const std::string name = "double";
    double value(5.1);
    runInfo.addProperty(name, value);

    TS_ASSERT_EQUALS(runInfo.getPropertyAsSingleValue(name), value);

    // Replace the log with a different value
    value = 10.3;
296
    runInfo.addProperty(name, value, /*overwrite*/ true);
297
298
299
300

    TS_ASSERT_EQUALS(runInfo.getPropertyAsSingleValue(name), value);
  }

301
302
303
  void test_clear() {
    // Set up a Run object with 3 properties in it (1 time series, 2 single
    // value)
304
305
306
    LogManager runInfo;
    const std::string stringProp("aStringProp");
    const std::string stringVal("testing");
307
    runInfo.addProperty(stringProp, stringVal);
308
    const std::string intProp("anIntProp");
309
    runInfo.addProperty(intProp, 99);
310
    const std::string tspProp("tsp");
311
    addTestTimeSeries(runInfo, "tsp");
312
313

    // Check it's set up right
314
    TS_ASSERT_EQUALS(runInfo.getProperties().size(), 3);
315
    auto tsp = runInfo.getTimeSeriesProperty<double>(tspProp);
316
    TS_ASSERT_EQUALS(tsp->realSize(), 10)
317
318

    // Do the clearing work
319
    TS_ASSERT_THROWS_NOTHING(runInfo.clearTimeSeriesLogs());
320
321

    // Check the time-series property is empty, but not the others
322
323
324
325
326
    TS_ASSERT_EQUALS(runInfo.getProperties().size(), 3);
    TS_ASSERT_EQUALS(tsp->realSize(), 0)
    TS_ASSERT_EQUALS(runInfo.getPropertyValueAsType<std::string>(stringProp),
                     stringVal);
    TS_ASSERT_EQUALS(runInfo.getPropertyValueAsType<int>(intProp), 99);
327
328
  }

329
330
331
  void clearOutdatedTimeSeriesLogValues() {
    // Set up a Run object with 3 properties in it (1 time series, 2 single
    // value)
332
333
334
    LogManager runInfo;
    const std::string stringProp("aStringProp");
    const std::string stringVal("testing");
335
    runInfo.addProperty(stringProp, stringVal);
336
    const std::string intProp("anIntProp");
337
    runInfo.addProperty(intProp, 99);
338
    const std::string tspProp("tsp");
339
    addTestTimeSeries(runInfo, "tsp");
340

341
    // Check it's set up right
342
    TS_ASSERT_EQUALS(runInfo.getProperties().size(), 3);
343
    auto tsp = runInfo.getTimeSeriesProperty<double>(tspProp);
344
    TS_ASSERT_EQUALS(tsp->realSize(), 10);
345
346
347
348
349

    auto lastTime = tsp->lastTime();
    auto lastValue = tsp->lastValue();

    // Do the clearing work
350
    TS_ASSERT_THROWS_NOTHING(runInfo.clearOutdatedTimeSeriesLogValues());
351
352

    // Check the time-series property has 1 entry, & the others are unchanged
353
354
355
356
357
358
359
    TS_ASSERT_EQUALS(runInfo.getProperties().size(), 3);
    TS_ASSERT_EQUALS(tsp->realSize(), 1);
    TS_ASSERT_EQUALS(tsp->firstTime(), lastTime);
    TS_ASSERT_EQUALS(tsp->firstValue(), lastValue);
    TS_ASSERT_EQUALS(runInfo.getPropertyValueAsType<std::string>(stringProp),
                     stringVal);
    TS_ASSERT_EQUALS(runInfo.getPropertyValueAsType<int>(intProp), 99);
360
  }
361
362

  /** Save and load to NXS file */
363
  void test_nexus() {
364
365
366
367
368
    NexusTestHelper th(true);
    th.createFile("LogManagerTest.nxs");

    LogManager run1;
    addTimeSeriesEntry(run1, "double_series", 45.0);
369
370
371
372
    run1.addProperty(new PropertyWithValue<int>("int_val", 1234));
    run1.addProperty(new PropertyWithValue<std::string>(
        "string_val", "help_im_stuck_in_a_log_file"));
    run1.addProperty(new PropertyWithValue<double>("double_val", 5678.9));
373
374
375
376
377
378
379
380
381
382
383
384
385
386
    addTimeSeriesEntry(run1, "phi", 12.3);
    addTimeSeriesEntry(run1, "chi", 45.6);
    addTimeSeriesEntry(run1, "omega", 78.9);
    addTimeSeriesEntry(run1, "proton_charge", 78.9);

    run1.saveNexus(th.file, "logs");
    th.file->openGroup("logs", "NXgroup");
    th.file->makeGroup("junk_to_ignore", "NXmaterial");
    th.file->makeGroup("more_junk_to_ignore", "NXsample");

    // ---- Now re-load the same and compare ------
    th.reopenFile();
    LogManager run2;
    run2.loadNexus(th.file, "logs");
387
388
389
390
    TS_ASSERT(run2.hasProperty("double_series"));
    TS_ASSERT(run2.hasProperty("int_val"));
    TS_ASSERT(run2.hasProperty("string_val"));
    TS_ASSERT(run2.hasProperty("double_val"));
391
392
    // This test both uses the goniometer axes AND looks up some values.

393
394
    // Reload without opening the group (for backwards-compatible reading of old
    // files)
395
396
397
    LogManager run3;
    th.file->openGroup("logs", "NXgroup");
    run3.loadNexus(th.file, "");
398
399
400
401
    TS_ASSERT(run3.hasProperty("double_series"));
    TS_ASSERT(run3.hasProperty("int_val"));
    TS_ASSERT(run3.hasProperty("string_val"));
    TS_ASSERT(run3.hasProperty("double_val"));
402
403
404
  }

  /** Check for loading the old way of saving proton_charge */
405
  void test_legacy_nexus() {
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
    NexusTestHelper th(true);
    th.createFile("LogManagerTest.nxs");
    th.file->makeGroup("sample", "NXsample", 1);
    th.file->writeData("proton_charge", 1.234);
    th.reopenFile();
    th.file->openGroup("sample", "NXsample");
    LogManager run3;
    run3.loadNexus(th.file, "");
  }
};

//---------------------------------------------------------------------------------------
// Performance test
//---------------------------------------------------------------------------------------

421
class LogManagerTestPerformance : public CxxTest::TestSuite {
422
423
424
public:
  // This pair of boilerplate methods prevent the suite being created statically
  // This means the constructor isn't called when running other tests
425
426
427
428
  static LogManagerTestPerformance *createSuite() {
    return new LogManagerTestPerformance();
  }
  static void destroySuite(LogManagerTestPerformance *suite) { delete suite; }
429

430
  LogManagerTestPerformance() : m_testRun(), m_propName("test") {
431
432
433
    addTestTimeSeries(m_testRun, m_propName);
  }

434
  void test_Accessing_Single_Value_From_Times_Series_A_Large_Number_Of_Times() {
435
    double value(0.0);
436
    for (size_t i = 0; i < 20000; ++i) {
437
438
439
440
441
442
443
444
445
446
447
      value = m_testRun.getPropertyAsSingleValue(m_propName);
    }
    // Enure variable is used so that it is not optimised away by the compiler
    value += 1.0;
  }

  LogManager m_testRun;
  std::string m_propName;
};

#endif