Skip to content

Commit

Permalink
Refs #10450. Added an optional feature to remove records.
Browse files Browse the repository at this point in the history
Optional feature to have remove records with duplicated value in sample
log that is to be ordered by.
  • Loading branch information
wdzhou committed Nov 10, 2014
1 parent 686c162 commit 995991d
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 2 deletions.
Expand Up @@ -370,13 +370,29 @@ def _orderRecordFile(self):

# log entry line
numlines = 0

# consider the mode to remove duplicate
if self._removeDupRecord is True:
totnumlines = len(linedict.keys())

for ivalue in sorted(linedict.keys()):
for line in linedict[ivalue]:
if self._removeDupRecord is True:
# If duplicated records is to be removed, only consider the last record
line = linedict[ivalue][-1]
wbuf += line
# Add extra \n in case reordered
if numlines != totnumlines-1 and wbuf[-1] != '\n':
wbuf += '\n'
numlines += 1

else:
# Consider all!
for line in linedict[ivalue]:
wbuf += line
# Add extra \n in case reordered
if numlines != totnumlines-1 and wbuf[-1] != '\n':
wbuf += '\n'
numlines += 1
# ENDFOR
# ENDFOR
# ENDFOR

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

return

def test_removeDupRecord(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 = "TestRecord11.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"],
RemoveDuplicateRecord = True,
OrderByTitle = 'RUN')

# Record 0B
alg_test = run_algorithm("ExportExperimentLog",
InputWorkspace = "TestMatrixWS1",
OutputFilename = "TestRecord11.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", "12345", "ProtonCharge-Avg", "32.921"],
RemoveDuplicateRecord = True,
OrderByTitle = 'RUN')

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

alg_test = run_algorithm("ExportExperimentLog",
InputWorkspace = "TestMatrixWS2",
OutputFilename = "TestRecord11.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"],
RemoveDuplicateRecord = True,
OrderByTitle = 'RUN')

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

alg_test = run_algorithm("ExportExperimentLog",
InputWorkspace = "TestMatrixWS3",
OutputFilename = "TestRecord11.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"],
RemoveDuplicateRecord = True,
OrderByTitle = 'RUN')

# Record 2B
alg_test = run_algorithm("ExportExperimentLog",
InputWorkspace = "TestMatrixWS3",
OutputFilename = "TestRecord11.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"],
RemoveDuplicateRecord = True,
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 test_sortRecordFileOverride(self):
""" Test to append logs and sort the log record file
"""
Expand Down

0 comments on commit 995991d

Please sign in to comment.