Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions Framework/PythonInterface/plugins/algorithms/GetIPTS.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# NScD Oak Ridge National Laboratory, European Spallation Source,
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
from functools import lru_cache
from mantid.api import AlgorithmFactory, FileFinder, PythonAlgorithm
from mantid.kernel import ConfigService, Direction, IntBoundedValidator, StringListValidator

Expand All @@ -28,7 +29,13 @@ def getValidInstruments(self):

return instruments

def findFile(self, instrument, runnumber):
@lru_cache
@staticmethod
def findFile(instrument, runnumber):
"""Static method to get the path for an instrument/runnumber.
This assumes that within the runtime of mantid the mapping will be consistent.

The lru_cache will allow for skipping this function if the same run number is supplied"""
# start with run and check the five before it
runIds = list(range(runnumber, runnumber - 6, -1))
# check for one after as well
Expand All @@ -42,7 +49,7 @@ def findFile(self, instrument, runnumber):

# look for a file
for runId in runIds:
self.log().information("Looking for '%s'" % runId)
# use filefinder to look
try:
return FileFinder.findRuns(runId)[0]
except RuntimeError:
Expand All @@ -52,7 +59,12 @@ def findFile(self, instrument, runnumber):
raise RuntimeError("Cannot find IPTS directory for '%s'" % runnumber)

def getIPTSLocal(self, instrument, runnumber):
filename = self.findFile(instrument, runnumber)
# prepend non-empty instrument name for FileFinder
if len(instrument) == 0:
instrument_default = ConfigService.getInstrument().name()
self.log().information(f"Using default instrument: {instrument_default}")

filename = __class__.findFile(instrument, runnumber)

# convert to the path to the proposal
location = filename.find("IPTS")
Expand All @@ -73,13 +85,18 @@ def PyInit(self):

instruments = self.getValidInstruments()
self.declareProperty("Instrument", "", StringListValidator(instruments), "Empty uses default instrument")
self.declareProperty("ClearCache", False, "Remove internal cache of run descriptions to file paths")

self.declareProperty("Directory", "", direction=Direction.Output)

def PyExec(self):
instrument = self.getProperty("Instrument").value
runnumber = self.getProperty("RunNumber").value

if self.getProperty("ClearCache").value:
# drop the local cache of file information
self.findFile.cache_clear()

direc = self.getIPTSLocal(instrument, runnumber)
self.setPropertyValue("Directory", direc)
self.log().notice("IPTS directory is: %s" % direc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ set(TEST_PY_FILES
GenerateGroupingSNSInelasticTest.py
GenerateLogbookTest.py
GetEiT0atSNSTest.py
GetIPTSTest.py
GroupBySampleChangerPositionTest.py
HB2AReduceTest.py
HB3AAdjustSampleNormTest.py
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source,
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
import unittest
from unittest import mock
from mantid.simpleapi import GetIPTS


class GetIPTSTest(unittest.TestCase):
@mock.patch("plugins.algorithms.GetIPTS.GetIPTS.findFile")
def test_getIPTS_bad(self, mockFindFile):
mockFindFile.return_value = "nowhere/"
runNumber = "123456"
with self.assertRaises(RuntimeError) as context:
GetIPTS(RunNumber=runNumber, Instrument="SNAP")
assert "Failed to determine IPTS directory" in str(context.exception)

@mock.patch("plugins.algorithms.GetIPTS.GetIPTS.findFile")
def test_getIPTS_good(self, mockFindFile):
mockFindFile.return_value = "somewhere/IPTS/folder"
runNumber = "123456"
res = GetIPTS(RunNumber=runNumber, Instrument="SNAP")
assert res == "somewhere/IPTS/"

@mock.patch("plugins.algorithms.GetIPTS.FileFinder")
def test_getIPTS_cache(self, mockFileFinder):
mockFileFinder.findRuns.return_value = ["somewhere/IPTS/folder"]
runNumber = "123456"
res = GetIPTS(RunNumber=runNumber, Instrument="SNAP", ClearCache=True)
assert res == "somewhere/IPTS/"
mockFileFinder.findRuns.assert_called_once_with(f"SNAP_{runNumber}")

# call again -- still only called once
res = GetIPTS(RunNumber=runNumber, Instrument="SNAP")
mockFileFinder.findRuns.assert_called_once_with(f"SNAP_{runNumber}")

@mock.patch("plugins.algorithms.GetIPTS.FileFinder")
def test_getIPTS_cache_clear(self, mockFileFinder):
mockFileFinder.findRuns.return_value = ["somewhere/IPTS/folder"]
runNumber = "123456"
res = GetIPTS(RunNumber=runNumber, Instrument="SNAP", ClearCache=True)
assert res == "somewhere/IPTS/"
mockFileFinder.findRuns.assert_called_once_with(f"SNAP_{runNumber}")

# call again -- still only called once
res = GetIPTS(RunNumber=runNumber, Instrument="SNAP")
mockFileFinder.findRuns.assert_called_once_with(f"SNAP_{runNumber}")

# clear cache
res = GetIPTS(RunNumber=runNumber, Instrument="SNAP", ClearCache=True)
with self.assertRaises(AssertionError):
mockFileFinder.findRuns.assert_called_once_with(f"SNAP_{runNumber}")
assert mockFileFinder.findRuns.call_count == 2


if __name__ == "__main__":
unittest.main()
3 changes: 3 additions & 0 deletions docs/source/algorithms/GetIPTS-v1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Description
This returns a string the full path to the IPTS shared folder to allow
for saving of files in accessible user folders (e.g. ``shared``).

The algorithm has a cache of runnumbers that is stores while mantid is running.
This cache can be reset using the ``ClearCache`` argument or by restarting mantid.

.. warning::

This only works at ORNL.
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- New option in :ref:`algm-GetIPTS` to maintain a list of IPTS for supplied runs. This does not persist across sessions.