SaveDetectorsGroupingTest.h 7.11 KB
Newer Older
1
2
3
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4
5
//   NScD Oak Ridge National Laboratory, European Spallation Source,
//   Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6
// SPDX - License - Identifier: GPL - 3.0 +
7
#pragma once
8

9
#include "MantidAPI/AlgorithmManager.h"
10
#include "MantidAPI/AnalysisDataService.h"
11
#include "MantidAPI/Run.h"
LamarMoore's avatar
LamarMoore committed
12
13
14
#include "MantidKernel/System.h"
#include "MantidKernel/Timer.h"
#include <cxxtest/TestSuite.h>
15

16
#include "MantidDataHandling/LoadDetectorsGroupingFile.h"
LamarMoore's avatar
LamarMoore committed
17
#include "MantidDataHandling/SaveDetectorsGrouping.h"
18
#include "Poco/File.h"
19
20
21

using namespace Mantid;
using namespace Mantid::API;
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
using namespace Mantid::DataHandling;
using namespace Mantid::DataObjects;

namespace {

const std::string TEMP_SAVED_MAP_WS = "test_grouping_workspace";
const std::string TEMP_LOADED_MAP_WS = "test_grouping_loaded";
const std::string TEMP_MAP_FILE_NAME = "temp_grouping.map";

void createGroupingWorkspace(const std::string &instrumentName,
                             const std::string &analyser,
                             const std::string &customGrouping) {
  auto creator = AlgorithmManager::Instance().create("CreateGroupingWorkspace");
  creator->initialize();
  creator->setProperty("InstrumentName", instrumentName);
  creator->setProperty("ComponentName", analyser);
  creator->setProperty("CustomGroupingString", customGrouping);
  creator->setProperty("OutputWorkspace", TEMP_SAVED_MAP_WS);
  creator->execute();
}

ITableWorkspace_sptr compareWorkspaces(const MatrixWorkspace_sptr &workspace1,
                                       const MatrixWorkspace_sptr &workspace2,
                                       double tolerance = 0.000001) {
  auto compareAlg = AlgorithmManager::Instance().create("CompareWorkspaces");
  compareAlg->setProperty("Workspace1", workspace1);
  compareAlg->setProperty("Workspace2", workspace2);
  compareAlg->setProperty("Tolerance", tolerance);
  compareAlg->execute();
  return compareAlg->getProperty("Messages");
}

} // namespace
55

56
class SaveDetectorsGroupingTest : public CxxTest::TestSuite {
57
58
59
public:
  // This pair of boilerplate methods prevent the suite being created statically
  // This means the constructor isn't called when running other tests
60
61
62
63
  static SaveDetectorsGroupingTest *createSuite() {
    return new SaveDetectorsGroupingTest();
  }
  static void destroySuite(SaveDetectorsGroupingTest *suite) { delete suite; }
64

65
  void test_Initialize() {
66
67
68
69
70
    SaveDetectorsGrouping savegroup;
    savegroup.initialize();
    TS_ASSERT(savegroup.isInitialized());
  }

71
  void test_SaveXMLFile() {
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

    // 1. Get an object for SaveDetectorsGrouping
    SaveDetectorsGrouping savegroup;
    savegroup.initialize();

    // 2. Create a Workspace from an XML file
    LoadDetectorsGroupingFile load;
    load.initialize();

    TS_ASSERT(load.setProperty("InputFile", "vulcangroup.xml"));
    TS_ASSERT(load.setProperty("OutputWorkspace", "Vulcan_Group"));

    load.execute();
    TS_ASSERT(load.isExecuted());

87
    DataObjects::GroupingWorkspace_sptr gws =
88
        std::dynamic_pointer_cast<DataObjects::GroupingWorkspace>(
89
            API::AnalysisDataService::Instance().retrieve("Vulcan_Group"));
90
91
92
93
94
95
96
97

    // 3. Save
    savegroup.setProperty("InputWorkspace", gws);
    savegroup.setProperty("OutputFile", "grouptemp.xml");

    savegroup.execute();
    TS_ASSERT(savegroup.isExecuted());

98
99
100
    // Retrieve the full path to file
    std::string file1 = savegroup.getPropertyValue("OutputFile");

101
102
103
104
    // 4. Verify
    LoadDetectorsGroupingFile load2;
    load2.initialize();

105
    TS_ASSERT(load2.setProperty("InputFile", file1));
106
107
108
109
110
    TS_ASSERT(load2.setProperty("OutputWorkspace", "Vulcan_Group2"));

    load2.execute();
    TS_ASSERT(load2.isExecuted());

111
    DataObjects::GroupingWorkspace_sptr gws2 =
112
        std::dynamic_pointer_cast<DataObjects::GroupingWorkspace>(
113
            API::AnalysisDataService::Instance().retrieve("Vulcan_Group2"));
114

115
    TS_ASSERT_DELTA(gws2->y(0)[0], 1.0, 1.0E-5);
116
    // TS_ASSERT_DELTA(gws2->y(3695)[0], 2.0, 1.0E-5);
117
    TS_ASSERT_DELTA(gws2->y(3696)[0], 2.0, 1.0E-5);
118
    // TS_ASSERT_DELTA(gws2->y(7000)[0], 0.0, 1.0E-5);
119
120

    // 5. Clear
121
    Poco::File file(file1);
122
123
    file.remove(false);

124
125
    API::AnalysisDataService::Instance().remove("Vulcan_Group");
    API::AnalysisDataService::Instance().remove("Vulcan_Group2");
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
  void test_saving_a_MAP_file() {
    auto &ads = AnalysisDataService::Instance();

    createGroupingWorkspace("IRIS", "graphite", "3-5,6+7,8:10");

    TS_ASSERT(ads.doesExist(TEMP_SAVED_MAP_WS));
    const auto groupingToSave =
        ads.retrieveWS<GroupingWorkspace>(TEMP_SAVED_MAP_WS);

    SaveDetectorsGrouping saver;
    saver.initialize();
    saver.setProperty("InputWorkspace", groupingToSave);
    saver.setProperty("OutputFile", TEMP_MAP_FILE_NAME);
    saver.execute();
    TS_ASSERT(saver.isExecuted());

    LoadDetectorsGroupingFile loader;
    loader.initialize();
    loader.setProperty("InputFile", TEMP_MAP_FILE_NAME);
    loader.setProperty("OutputWorkspace", TEMP_LOADED_MAP_WS);
    loader.execute();
    TS_ASSERT(loader.isExecuted());

    const auto groupingLoaded =
        ads.retrieveWS<GroupingWorkspace>(TEMP_LOADED_MAP_WS);

    TS_ASSERT(!compareWorkspaces(groupingToSave, groupingLoaded));

    Poco::File file(TEMP_MAP_FILE_NAME);
    file.remove(false);
    ads.remove(TEMP_SAVED_MAP_WS);
    ads.remove(TEMP_LOADED_MAP_WS);
  }

162
  void test_SaveNamingAndDescription() {
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
    std::string testWs = "GroupingWorkspace";

    // Load the grouping to test with
    LoadDetectorsGroupingFile load;
    load.initialize();

    TS_ASSERT(load.setProperty("InputFile", "MUSRGrouping.xml"));
    TS_ASSERT(load.setProperty("OutputWorkspace", testWs));

    load.execute();
    TS_ASSERT(load.isExecuted());

    // Save the test workspace
    SaveDetectorsGrouping save;
    save.initialize();

    TS_ASSERT(save.setProperty("InputWorkspace", testWs));
    TS_ASSERT(save.setProperty("OutputFile", "grouptemp.xml"));

    save.execute();
    TS_ASSERT(save.isExecuted());

    // Get full path of the file
    std::string testFile = save.getPropertyValue("OutputFile");

    // Remove saved workspace
    API::AnalysisDataService::Instance().remove(testWs);

    // Load it again to verify
    LoadDetectorsGroupingFile load2;
    load2.initialize();

    TS_ASSERT(load2.setProperty("InputFile", testFile));
    TS_ASSERT(load2.setProperty("OutputWorkspace", testWs));

    load2.execute();
    TS_ASSERT(load2.isExecuted());

    // Get GroupingWorkspace instance
202
    auto gws = std::dynamic_pointer_cast<DataObjects::GroupingWorkspace>(
203
        API::AnalysisDataService::Instance().retrieve(testWs));
204
205

    // Check that description was saved
206
207
    TS_ASSERT_EQUALS(gws->run().getProperty("Description")->value(),
                     "musr longitudinal (64 detectors)");
208
209
210
211
212
213
214
215
216
217
218

    // Check that group names were saved
    TS_ASSERT_EQUALS(gws->run().getProperty("GroupName_1")->value(), "fwd");
    TS_ASSERT_EQUALS(gws->run().getProperty("GroupName_2")->value(), "bwd");

    // Clean-up
    API::AnalysisDataService::Instance().remove(testWs);

    Poco::File file(testFile);
    file.remove();
  }
219
};