Commit d619c8be authored by Robert Applin's avatar Robert Applin
Browse files

Refs #29984. Save groupings as xml files.

parent 81712537
......@@ -55,9 +55,6 @@ private:
/// Print Grouping to XML file
void printToXML(const std::map<int, std::vector<detid_t>> &groupdetidrangemap,
const std::string &xmlfilename);
/// Print Grouping to MAP file
void printToMap(std::map<int, std::vector<detid_t>> &groupToDetectorIDsMap,
const std::string &mapFilename);
// GroupingWorkspace
DataObjects::GroupingWorkspace_const_sptr mGroupWS;
......
......@@ -562,7 +562,8 @@ void GroupDetectors2::processXMLFile(
// 2. Load XML file
DataHandling::LoadGroupXMLFile loader;
loader.setDefaultStartingGroupID(0);
// A Group with ID = 0 in an XML file means the detectors are excluded.
loader.setDefaultStartingGroupID(1);
loader.loadXMLFile(fname);
std::map<int, std::vector<detid_t>> mGroupDetectorsMap =
loader.getGroupDetectorsMap();
......
......@@ -22,44 +22,10 @@
#include <fstream>
#include <sstream>
#include <boost/algorithm/string.hpp>
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Poco::XML;
namespace {
bool hasExtention(std::string filename, const std::string &extention) {
if (filename.length() < extention.length())
return false;
std::transform(filename.begin(), filename.end(), filename.begin(),
[](unsigned char c) { return std::tolower(c); });
return filename.compare(filename.length() - extention.length(),
extention.length(), extention) == 0;
}
int numberOfDetectorIDs(const std::vector<Mantid::detid_t> &detectorIDs) {
return detectorIDs[1] - detectorIDs[0] + 1;
}
std::string
spaceSeparatedDetectorString(const std::vector<Mantid::detid_t> &detectorIDs) {
if (detectorIDs[0] == detectorIDs[1]) {
return std::to_string(detectorIDs[0]);
} else {
std::string detectorString;
for (auto id = detectorIDs[0]; id <= detectorIDs[1]; ++id)
detectorString += std::to_string(id) + " ";
boost::algorithm::trim(detectorString);
return detectorString;
}
}
} // namespace
namespace Mantid {
namespace DataHandling {
......@@ -67,23 +33,20 @@ DECLARE_ALGORITHM(SaveDetectorsGrouping)
/// Define input parameters
void SaveDetectorsGrouping::init() {
const std::initializer_list<std::string> extentions = {".xml", ".map"};
declareProperty(
std::make_unique<API::WorkspaceProperty<DataObjects::GroupingWorkspace>>(
"InputWorkspace", "", Direction::Input),
"GroupingWorkspace to output to a XML or MAP file.");
declareProperty(
std::make_unique<FileProperty>("OutputFile", "", FileProperty::Save,
extentions),
"File to save the detectors grouping to. Default is an XML file.");
"GroupingWorkspace to output to XML file (GroupingWorkspace)");
declareProperty(std::make_unique<FileProperty>("OutputFile", "",
FileProperty::Save, ".xml"),
"File to save the detectors mask in XML format");
}
/// Main body to execute algorithm
void SaveDetectorsGrouping::exec() {
// 1. Get Input
const std::string outputFilename = this->getProperty("OutputFile");
const std::string xmlfilename = this->getProperty("OutputFile");
mGroupWS = this->getProperty("InputWorkspace");
// 2. Create Map(group ID, workspace-index vector)
......@@ -96,10 +59,7 @@ void SaveDetectorsGrouping::exec() {
this->convertToDetectorsRanges(groupIDwkspIDMap, groupIDdetectorRangeMap);
// 4. Print out
if (hasExtention(outputFilename, "map"))
printToMap(groupIDdetectorRangeMap, outputFilename);
else
printToXML(groupIDdetectorRangeMap, outputFilename);
this->printToXML(groupIDdetectorRangeMap, xmlfilename);
}
/*
......@@ -278,35 +238,5 @@ void SaveDetectorsGrouping::printToXML(
ofs.close();
}
/** Creates a MAP file using the detector IDs found in a grouping workspace. The
* format of a MAP file is as follows:
*
* Line 1 - Number of groups.
*
* For N = 2 to N = Number of groups:
*
* Line N - Group ID.
* Line N + 1 - Number of detectors in group.
* Line N + 2 - Space separated list of Detector IDs.
*/
void SaveDetectorsGrouping::printToMap(
std::map<int, std::vector<detid_t>> &groupToDetectorIDsMap,
const std::string &mapFilename) {
const auto removeIter = groupToDetectorIDsMap.find(0);
if (removeIter != groupToDetectorIDsMap.cend())
groupToDetectorIDsMap.erase(removeIter);
std::ofstream ofs;
ofs.open(mapFilename.c_str(), std::fstream::out);
ofs << std::to_string(groupToDetectorIDsMap.size()) + "\n";
for (auto detectorIds : groupToDetectorIDsMap) {
ofs << std::to_string(detectorIds.first) + "\n";
ofs << std::to_string(numberOfDetectorIDs(detectorIds.second)) + "\n";
ofs << spaceSeparatedDetectorString(detectorIds.second) + "\n";
}
ofs.close();
}
} // namespace DataHandling
} // namespace Mantid
......@@ -6,7 +6,6 @@
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/Run.h"
#include "MantidKernel/System.h"
......@@ -18,40 +17,8 @@
#include "Poco/File.h"
using namespace Mantid;
using namespace Mantid::API;
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
using namespace Mantid::API;
class SaveDetectorsGroupingTest : public CxxTest::TestSuite {
public:
......@@ -125,40 +92,6 @@ public:
API::AnalysisDataService::Instance().remove("Vulcan_Group2");
}
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);
}
void test_SaveNamingAndDescription() {
std::string testWs = "GroupingWorkspace";
......
......@@ -9,19 +9,8 @@
Description
-----------
This algorithm is used to save a GroupingWorkspace to an XML file or MAP file.
The XML file format is the same as that accepted by :ref:`algm-LoadDetectorsGroupingFile`.
The MAP file format for detector grouping is as follows:
.. code-block:: sh
Line 1 The number of groups.
For N = 2 to N = Number of groups:
Line N The Group ID.
Line N + 1 The number of Detector IDs in the group.
Line N + 2 A space-separated list of Detector IDs in this group.
This algorithm is used to save a GroupingWorkspace to XML file in a
format which is accepted by :ref:`algm-LoadDetectorsGroupingFile`.
Usage
-----
......@@ -74,58 +63,6 @@ Output:
os.remove(save_path)
**Example - Saving a custom grouping to a MAP file:**
.. testcode:: ExMAPFileGrouping
import os
# Create the custom detector grouping workspace
grouping_ws, _, _ = CreateGroupingWorkspace(InstrumentName='IRIS', ComponentName='graphite', CustomGroupingString='3:5,6+7+8,9,10-20,21-35,36-53')
# Specify the save location
save_path = os.path.join(config["defaultsave.directory"], "iris_detector_grouping.map")
# Save the detector grouping to a MAP file
SaveDetectorsGrouping(InputWorkspace="grouping_ws", OutputFile=save_path)
with open(save_path, 'r') as f:
print(f.read().replace('\t', ' ').strip())
Output:
.. testoutput:: ExMAPFileGrouping
8
1
1
3
2
1
4
3
1
5
4
3
6 7 8
5
1
9
6
11
10 11 12 13 14 15 16 17 18 19 20
7
15
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
8
18
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
.. testcleanup:: ExMAPFileGrouping
os.remove(save_path)
.. categories::
.. sourcelink::
......@@ -24,7 +24,6 @@ Algorithms
- Added the ability to specify the packing fraction and effective number density to :ref:`SetSample <algm-SetSample>` and :ref:`SetSampleMaterial <algm-SetSampleMaterial>`.
- :ref:`CropWorkspaceRagged <algm-CropWorkspaceRagged>` now produces ragged workspace and can now be used on large data sets.
- Added the ability to specify a ``CustomGroupingString`` when creating a detector grouping workspace using :ref:`CreateGroupingWorkspace <algm-CreateGroupingWorkspace>`.
- Added the ability to save a detector grouping to a MAP file using :ref:`SaveDetectorsGrouping <algm-SaveDetectorsGrouping>`.
Fitting
-------
......
......@@ -11,7 +11,7 @@ Indirect Geometry Changes
New Features
############
- Added the ability to save a detector grouping to a MAP file from a Custom Grouping String on Indirect Data Reduction.
- Added the ability to save a detector grouping to a XML file from a Custom Grouping String on Indirect Data Reduction.
Improvements
############
......
......@@ -25,7 +25,7 @@ using MantidQt::API::BatchAlgorithmRunner;
namespace {
Mantid::Kernel::Logger g_log("ISISEnergyTransfer");
const std::string DEFAULT_MAP_FILENAME = "custom_detector_grouping.map";
const std::string DEFAULT_GROUPING_FILENAME = "custom_detector_grouping.xml";
const std::string GROUPING_WS_NAME = "Custom_grouping_workspace";
bool doesExistInADS(std::string const &workspaceName) {
......@@ -619,7 +619,7 @@ void ISISEnergyTransfer::handleSaveCustomGroupingClicked() {
if (doesExistInADS(GROUPING_WS_NAME)) {
QHash<QString, QString> props;
props["InputWorkspace"] = QString::fromStdString(GROUPING_WS_NAME);
props["OutputFile"] = QString::fromStdString(DEFAULT_MAP_FILENAME);
props["OutputFile"] = QString::fromStdString(DEFAULT_GROUPING_FILENAME);
InterfaceManager interfaceManager;
auto *dialog = interfaceManager.createDialogFromName(
......
......@@ -273,6 +273,7 @@
<property name="fileExtensions" stdset="0">
<stringlist>
<string>.map</string>
<string>.xml</string>
</stringlist>
</property>
</widget>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment