Skip to content

Commit

Permalink
Merge pull request #38027 from mantidproject/fix-enginx-data-cache
Browse files Browse the repository at this point in the history
Fix ENGINX data cache search on IDAaaS
  • Loading branch information
cailafinn authored Sep 27, 2024
2 parents f50390a + b67e391 commit 98cb2d3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Framework/API/inc/MantidAPI/ISISInstrumentDataCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class MANTID_API_DLL ISISInstrumentDataCache {
std::pair<Mantid::Kernel::InstrumentInfo, std::string> validateInstrumentAndNumber(const std::string &filename) const;
std::filesystem::path makeIndexFilePath(const std::string &instrumentName) const;
std::pair<std::string, std::string> splitIntoInstrumentAndNumber(const std::string &filename) const;
[[nodiscard]] std::pair<std::string, Json::Value>
[[nodiscard]] std::pair<std::filesystem::path, Json::Value>
openCacheJsonFile(const Mantid::Kernel::InstrumentInfo &instrument) const;
[[nodiscard]] Mantid::Kernel::InstrumentInfo getInstrumentFromName(const std::string &instName) const;
std::string m_dataCachePath;
Expand Down
3 changes: 2 additions & 1 deletion Framework/API/src/FileFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,8 @@ const API::Result<std::string> FileFinderImpl::getPath(const std::vector<IArchiv
errors += cacheFilePath.errors();

} else {
errors += "Could not find data cache directory: " + cachePathToSearch.string();
g_log.debug() << "Data cache directory not found, proceeding with the search." << std::endl;
errors += "Could not find data cache directory: " + cachePathToSearch.string() + '\n';
}

// Search the archive
Expand Down
18 changes: 10 additions & 8 deletions Framework/API/src/ISISInstrumentDataCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ std::string ISISInstrumentDataCache::getFileParentDirectoryPath(const std::strin
g_log.debug() << "ISISInstrumentDataCache::getFileParentDirectoryPath(" << fileName << ")" << std::endl;

auto [instrumentInfo, runNumber] = validateInstrumentAndNumber(fileName);
std::string instrName = instrumentInfo.name();

auto const [jsonPath, json] = openCacheJsonFile(instrumentInfo);

std::string relativePath = json[runNumber].asString();

if (relativePath.empty()) {
throw std::invalid_argument("Run number " + runNumber + " not found for instrument " + instrName + ".");
throw std::invalid_argument("Run number " + runNumber + " not found in index file " + jsonPath.filename().string() +
".");
}

std::string dirPath = m_dataCachePath + "/" + instrName + "/" + relativePath;
std::filesystem::path dirPath = jsonPath.parent_path() / relativePath;
std::string dirPathString = dirPath.make_preferred().string();

g_log.debug() << "Opened instrument index file: " << jsonPath << ". Found path to search: " << dirPath << "."
<< std::endl;
return dirPath;
g_log.debug() << "Found path to search: " << dirPathString << std::endl;
return dirPathString;
}

/**
Expand All @@ -57,7 +57,8 @@ std::vector<std::string> ISISInstrumentDataCache::getRunNumbersInCache(const std
* @return A pair containing the path and the contents of the json file.
* @throws std::invalid_argument if the file could not be found.
*/
std::pair<std::string, Json::Value> ISISInstrumentDataCache::openCacheJsonFile(const InstrumentInfo &instrument) const {
std::pair<std::filesystem::path, Json::Value>
ISISInstrumentDataCache::openCacheJsonFile(const InstrumentInfo &instrument) const {
// Open index json file
std::filesystem::path jsonPath = makeIndexFilePath(instrument.name());
std::ifstream ifstrm{jsonPath};
Expand All @@ -71,7 +72,8 @@ std::pair<std::string, Json::Value> ISISInstrumentDataCache::openCacheJsonFile(c
// Read directory path from json file
Json::Value json;
ifstrm >> json;
return {jsonPath.string(), json};
g_log.debug() << "Opened instrument index file: " << jsonPath << std::endl;
return {jsonPath, json};
}

std::pair<InstrumentInfo, std::string>
Expand Down
26 changes: 19 additions & 7 deletions Framework/API/test/ISISInstrumentDataCacheTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ class ISISInstrumentDataCacheTest : public CxxTest::TestSuite {
std::string sansJson = R"({"101115": "2018/RB1800009-2"})";
std::string pg3Json = R"({"11111": "mock/path"})";
std::string wishJson = R"({"12345": "subdir1/subdir2"})";
std::string enginxJson = R"({"55555": "subdir1/subdir2"})";

// Create test JSON file
std::filesystem::create_directory(m_dataCacheDir);

std::unordered_map<std::string, std::string> instrFiles = {
{"MARI", marJson}, {"SANS2D", sansJson}, {"POWGEN", pg3Json}, {"WISH", wishJson}};
{"MARI", marJson}, {"SANS2D", sansJson}, {"POWGEN", pg3Json}, {"WISH", wishJson}, {"ENGINX", enginxJson}};
for (const auto &[instrName, instrIndex] : instrFiles) {

std::filesystem::create_directory(m_dataCacheDir + "/" + instrName);
Expand All @@ -56,26 +56,38 @@ class ISISInstrumentDataCacheTest : public CxxTest::TestSuite {
void testInstrNameExpanded() {
ISISInstrumentDataCache dc(m_dataCacheDir);
std::string actualPath = dc.getFileParentDirectoryPath("MAR25054");
TS_ASSERT_EQUALS(actualPath, m_dataCacheDir + "/MARI/2019/RB1868000-1");
std::filesystem::path expectedPath = m_dataCacheDir + "/MARI/2019/RB1868000-1";
TS_ASSERT_EQUALS(actualPath, expectedPath.make_preferred().string());
}

void testLowerCaseInstrName() {
ISISInstrumentDataCache dc(m_dataCacheDir);
std::string actualPath = dc.getFileParentDirectoryPath("mar25054");
TS_ASSERT_EQUALS(actualPath, m_dataCacheDir + "/MARI/2019/RB1868000-1");
std::filesystem::path expectedPath = m_dataCacheDir + "/MARI/2019/RB1868000-1";
TS_ASSERT_EQUALS(actualPath, expectedPath.make_preferred().string());
}

void testCorrectInstrRunSplit() {
ISISInstrumentDataCache dc(m_dataCacheDir);
std::string actualPath = dc.getFileParentDirectoryPath("SANS2D101115");
TS_ASSERT_EQUALS(actualPath, m_dataCacheDir + "/SANS2D/2018/RB1800009-2");
std::filesystem::path expectedPath = m_dataCacheDir + "/SANS2D/2018/RB1800009-2";
TS_ASSERT_EQUALS(actualPath, expectedPath.make_preferred().string());
}

void testInstrWithDelimiter() {
// Checks short name + delimiter gets correctly identified
ISISInstrumentDataCache dc(m_dataCacheDir);
std::string actualPath = dc.getFileParentDirectoryPath("PG3_11111");
TS_ASSERT_EQUALS(actualPath, m_dataCacheDir + "/POWGEN/mock/path");
std::filesystem::path expectedPath = m_dataCacheDir + "/POWGEN/mock/path";
TS_ASSERT_EQUALS(actualPath, expectedPath.make_preferred().string());
}

void testShortNameIsTried() {
// Name ENGIN-X is tried first and if it fails it tries ENGINX
ISISInstrumentDataCache dc(m_dataCacheDir);
std::string actualPath = dc.getFileParentDirectoryPath("ENGINX55555");
std::filesystem::path expectedPath = m_dataCacheDir + "/ENGINX/subdir1/subdir2";
TS_ASSERT_EQUALS(actualPath, expectedPath.make_preferred().string());
}

void testInstrWithSuffix() {
Expand Down Expand Up @@ -106,7 +118,7 @@ class ISISInstrumentDataCacheTest : public CxxTest::TestSuite {
void testRunNumberNotFound() {
ISISInstrumentDataCache dc(m_dataCacheDir);
TS_ASSERT_THROWS_EQUALS(dc.getFileParentDirectoryPath("SANS2D1234"), const std::invalid_argument &e,
std::string(e.what()), "Run number 1234 not found for instrument SANS2D.");
std::string(e.what()), "Run number 1234 not found in index file SANS2D_index.json.");
}

void testIndexFileExistsWhenExists() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Loading ``ENGIN-X`` data on IDAaaS from the instrument data cache no longer throws a "path not found" error.

0 comments on commit 98cb2d3

Please sign in to comment.