Skip to content

Commit

Permalink
Refs #10450. Added a new property to algorithm.
Browse files Browse the repository at this point in the history
to override the log value from workspace.  and
a new unit test is added for the new feature.

On branch feature/10450_record_order_by_runnumber

      modified:   ../Mantid/Framework/PythonInterface/plugins/algorithms/ExportExperimentLog.py
      modified:   ../Mantid/Framework/PythonInterface/test/python/plugins/algorithms/ExportExperimentLogTest.py
  • Loading branch information
wdzhou committed Nov 7, 2014
1 parent e8de669 commit aaa91ef
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 9 deletions.
Expand Up @@ -44,6 +44,9 @@ def PyInit(self):

self.declareProperty("OrderByTitle", "", "Log file will be ordered by the value of this title from low to high.")

overrideprop = StringArrayProperty("OverrideLogValue", values=[], direction=Direction.Input)
self.declareProperty(overrideprop, "List of paired strings as log title and value to override values from workspace.")

# Time zone
timezones = ["UTC", "America/New_York"]
self.declareProperty("TimeZone", "America/New_York", StringListValidator(timezones))
Expand Down Expand Up @@ -149,7 +152,6 @@ def _processInputs(self):
# This is left for a feature that might be needed in future.
self._reorderOld = False


self._timezone = self.getProperty("TimeZone").value

# Determine whether output log-record file should be ordered by value of some log
Expand All @@ -163,6 +165,19 @@ def _processInputs(self):
else:
self.log().warning("Specified title to order by (%s) is not in given log titles." % (ordertitle))

# Override log values: it will not work in fastappend mode to override
overridelist = self.getProperty("OverrideLogValue").value
if len(self._headerTitles) > 0:
if len(overridelist) % 2 != 0:
raise NotImplementedError("Number of items in OverrideLogValue must be even.")
self._ovrdTitleValueDict = {}
for i in xrange(len(overridelist)/2):
title = overridelist[2*i]
if title in self._headerTitles:
self._ovrdTitleValueDict[title] = overridelist[2*i+1]
else:
self.log().warning("Override title %s is not recognized. " % (title))

return

def _createLogFile(self):
Expand Down Expand Up @@ -210,10 +225,14 @@ def _examineLogFile(self):

# Examine
titles = titleline.split()
self.log().notice("[DB] Examine finds titles: %s" % (titles))

same = True
if len(titles) != len(self._headerTitles):
same = False
if len(self._headerTitles) == 0:
self._headerTitles = titles[:]
else:
same = False
for ititle in xrange(len(titles)):
title1 = titles[ititle]
title2 = self._headerTitles[ititle]
Expand Down Expand Up @@ -258,14 +277,33 @@ def _appendExpLog(self, logvaluedict):
# Write to a buffer
wbuf = ""

self.log().notice("[DB] Samlpe Log Names: %s" % (self._sampleLogNames))
self.log().notice("[DB] Title Names: %s" % (self._headerTitles))

if len(self._headerTitles) == 0:
skip = True
else:
skip = False

headertitle = None
for il in xrange(len(self._sampleLogNames)):
logname = self._sampleLogNames[il]
optype = self._sampleLogOperations[il]
key = logname + "-" + optype
if key in logvaluedict.keys():
value = logvaluedict[key]
elif logname in logvaluedict.keys():
value = logvaluedict[logname]
if skip is False:
headertitle = self._headerTitles[il]
if headertitle is not None and headertitle in self._ovrdTitleValueDict.keys():
# overriden
value = self._ovrdTitleValueDict[headertitle]

else:
# from input workspace
logname = self._sampleLogNames[il]
optype = self._sampleLogOperations[il]
key = logname + "-" + optype
if key in logvaluedict.keys():
value = logvaluedict[key]
elif logname in logvaluedict.keys():
value = logvaluedict[logname]
# ENDIFELSE

wbuf += "%s%s" % (str(value), self._valuesep)
wbuf = wbuf[0:-1]

Expand Down
Expand Up @@ -410,6 +410,101 @@ def test_sortRecordFile(self):

return

def test_sortRecordFileOverride(self):
""" Test to append logs and sort the log record file
"""
# Record 0
ws1 = self.createTestWorkspace(run=10000)
AnalysisDataService.addOrReplace("TestMatrixWS1", ws1)

alg_test = run_algorithm("ExportExperimentLog",
InputWorkspace = "TestMatrixWS1",
OutputFilename = "TestRecord10.txt",
SampleLogNames = ["run_number", "duration", "proton_charge", "proton_charge"],
SampleLogTitles = ["RUN", "Duration", "ProtonCharge", "ProtonCharge-Avg"],
SampleLogOperation = [None, None, "min", "average"],
FileMode = "new",
FileFormat = "tab",
OverrideLogValue = ["Duration", "12345", "ProtonCharge-Avg", "32.921"],
OrderByTitle = 'RUN')


# Record 1
ws2 = self.createTestWorkspace(run=11000)
AnalysisDataService.addOrReplace("TestMatrixWS2", ws2)

alg_test = run_algorithm("ExportExperimentLog",
InputWorkspace = "TestMatrixWS2",
OutputFilename = "TestRecord10.txt",
SampleLogNames = ["run_number", "duration", "proton_charge", "proton_charge"],
SampleLogTitles = ["RUN", "Duration", "ProtonCharge", "ProtonCharge-Avg"],
SampleLogOperation = [None, None, "min", "average"],
FileMode = "fastappend",
FileFormat = "tab",
OverrideLogValue = ["Duration", "23456", "ProtonCharge-Avg", "22.921"],
OrderByTitle = 'RUN')

# Record 2
ws3 = self.createTestWorkspace(run=10023)
AnalysisDataService.addOrReplace("TestMatrixWS3", ws3)

alg_test = run_algorithm("ExportExperimentLog",
InputWorkspace = "TestMatrixWS3",
OutputFilename = "TestRecord10.txt",
SampleLogNames = ["run_number", "duration", "proton_charge", "proton_charge"],
SampleLogTitles = ["RUN", "Duration", "ProtonCharge", "ProtonCharge-Avg"],
SampleLogOperation = [None, None, "min", "average"],
FileMode = "fastappend",
FileFormat = "tab",
OverrideLogValue = ["Duration", "34567", "ProtonCharge-Avg", "12.921"],
OrderByTitle = 'RUN')

# Verify
# Locate file
outfilename = alg_test.getProperty("OutputFilename").value
try:
ifile = open(outfilename)
lines = ifile.readlines()
ifile.close()
except IOError as err:
print "Unable to open file %s. " % (outfilename)
self.assertTrue(False)
return

# Last line cannot be empty, i.e., before EOF '\n' is not allowed
lastline = lines[-1]
self.assertTrue(len(lastline.strip()) > 0)

# Number of lines
self.assertEquals(len(lines), 4)

# Check value
for i in xrange(1, 3):
currline = lines[i]
curr_run = int(currline.split("\t")[0])
curr_min = float(currline.split("\t")[2])
nextline = lines[i+1]
next_run = int(nextline.split('\t')[0])
next_min = float(nextline.split('\t')[2])
self.assertTrue(curr_run < next_run)
self.assertTrue(curr_min < next_min)

line2 = lines[2]
terms = line2.split("\t")
duration = int(terms[1])
self.assertEquals(duration, 34567)
pchargeavg = float(terms[3])
self.assertAlmostEqual(pchargeavg, 12.921)


# Remove generated files
os.remove(outfilename)
AnalysisDataService.remove("TestMatrixWS1")
AnalysisDataService.remove("TestMatrixWS2")
AnalysisDataService.remove("TestMatrixWS3")

return


def createTestWorkspace(self, run=23456):
""" Create a workspace for testing against with ideal log values
Expand Down

0 comments on commit aaa91ef

Please sign in to comment.