-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
re #6669 Dumped branch and started again with fresh master
- Loading branch information
1 parent
3748c1a
commit 1a03da3
Showing
3 changed files
with
218 additions
and
1 deletion.
There are no files selected for viewing
152 changes: 152 additions & 0 deletions
152
...tid/Framework/PythonInterface/PythonAlgorithms/WorkflowAlgorithms/LoadLogPropertyTable.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
"""*WIKI* | ||
Creates a table workspace of the average values of log values against the run number. | ||
There are special cases for: | ||
* beamlog_(counts, frames, etc): last few points end up in next run's log. Find Maximum. | ||
* comment (separate function) | ||
* time series, take average for t>0 (if available) | ||
It should: | ||
# Load any file type that [[Load]] can handle. | ||
# Not crash with multiperiod data - although values will be from period 1 | ||
# Handle gaps in the file structure (although this can be slow over a network if you choose a range of 100s) | ||
# Load only a single spectra of the data (if the file loader supports this). | ||
# Print out the list of acceptable log names if one is entered incorrectly. | ||
# Use a hidden workspace for the temporary loaded workspaces, and clean up after itself. | ||
*WIKI*""" | ||
|
||
import time | ||
import datetime | ||
import numbers | ||
import bisect | ||
import numpy | ||
from mantid.api import * # PythonAlgorithm, registerAlgorithm, WorkspaceProperty | ||
from mantid.kernel import * # StringArrayProperty | ||
from mantid.simpleapi import * # needed for Load | ||
|
||
class LoadLogPropertyTable(PythonAlgorithm): | ||
# same concept as built in "CreateLogPropertyTable" but loads its own workspaces and needn't hold all in memory at once | ||
# select log values to put in table (list) | ||
# special cases for: | ||
# beamlog_(counts, frames, etc): last few points end up in next run's log. Find Maximum. | ||
# comment (separate function) | ||
# time series, take average for t>0 (if available) | ||
def PyInit(self): | ||
self.declareProperty(FileProperty(name="FirstFile",defaultValue="",action=FileAction.Load,extensions = ["nxs","raw"]),"The first file to load from") | ||
self.declareProperty(FileProperty(name="LastFile",defaultValue="",action=FileAction.Load,extensions = ["nxs","raw"]),"The Last file to load from, must be in the same directory, all files in between will also be used") | ||
self.declareProperty(StringArrayProperty("LogNames",direction=Direction.Input),"The comma seperated list of properties to include. \nThe full list will be printed if an invalid value is used.") | ||
self.declareProperty(WorkspaceProperty("OutputWorkspace","",Direction.Output),"Table of results") | ||
|
||
def category(self): | ||
return "Utility;Muon" | ||
|
||
def getGeneralLogValue(self,ws,name,begin): | ||
# get log value | ||
# average time series over run | ||
# for beamlog, etc return flag=true and value to push into previous run | ||
if(name=="comment"): | ||
return (ws.getComment(),False,0) | ||
|
||
try: | ||
v=ws.getRun().getProperty(name) | ||
except: | ||
possibleLogs = ws.getRun().keys() | ||
possibleLogs.insert(0,'comment') | ||
message = "The log name '" + name + "' was not found, possible choices are: " + str(possibleLogs) | ||
print message | ||
raise ValueError(message) | ||
try: | ||
times2=[] | ||
if (hasattr(v,"unfiltered")): | ||
v=v.unfiltered() | ||
for tt in v.times: | ||
times2.append((datetime.datetime(*(time.strptime(str(tt),"%Y-%m-%dT%H:%M:%S")[0:6]))-begin).total_seconds()) | ||
except: | ||
#print "probably not a time series" | ||
pass | ||
|
||
if(name[0:8]=="Beamlog_" and (name.find("Counts")>0 or name.find("Frames")>0)): | ||
i=bisect.bisect_right(times2,2) # allowance for "slow" clearing of DAE | ||
#print "returning max beam log, list cut 0:",i,":",len(times2) | ||
return (numpy.amax(v.value[i:]),True,numpy.amax(v.value[:i])) | ||
if(v.__class__.__name__ =="TimeSeriesProperty_dbl" or v.__class__.__name__ =="FloatTimeSeriesProperty"): | ||
i=bisect.bisect_left(times2,0) | ||
return (numpy.average(v.value[i:]),False,0) | ||
return (v.value,False,0) | ||
|
||
def PyExec(self): | ||
|
||
file1=self.getProperty("FirstFile").value | ||
file9=self.getProperty("LastFile").value | ||
i1=file1.rindex('.') | ||
j1=i1-1 | ||
while file1[j1-1].isdigit(): | ||
j1=j1-1 | ||
firstnum=int(file1[j1:i1]) | ||
i9=file9.rindex('.') | ||
j9=i9-1 | ||
while file9[j9-1].isdigit(): | ||
j9=j9-1 | ||
lastnum=int(file9[j9:i9]) | ||
if(file1[:j9] != file9[:j9]): | ||
raise Exception("Files from different directories or instruments") | ||
if(file1[i1:] != file9[i9:]): | ||
raise Exception("Files of different types") | ||
if(i1-j1 != i9-j9): | ||
raise Exception("File numbering error") | ||
if(lastnum < firstnum): | ||
raise Exception("Run numbers must increase") | ||
|
||
# table. Rows=runs, columns=logs (col 0 = run number) | ||
collist=self.getProperty("LogNames").value | ||
ows=WorkspaceFactory.createTable() | ||
ows.addColumn("int","RunNumber") | ||
|
||
# loop and load files. Absolute numbers for now. | ||
for ff in range(firstnum,lastnum+1): | ||
thispath=file1[:j1]+str(ff).zfill(i1-j1)+file1[i1:] | ||
returnTuple=None | ||
try: | ||
returnTuple=Load(Filename=thispath,OutputWorkspace="__CopyLogsTmp",SpectrumMin=1, SpectrumMax=1) | ||
except: | ||
print "Cannot load file " + thispath + " - skipping" | ||
continue | ||
|
||
#check if the return type is atuple | ||
if (type(returnTuple) == tuple): | ||
loadedWs=returnTuple[0] | ||
else: | ||
loadedWs = returnTuple | ||
|
||
#check if the ws is a group | ||
ws = loadedWs | ||
if (ws.id() == 'WorkspaceGroup'): | ||
print "Multiperiod File: Logs will be from the first period, but unfiltered. ", | ||
ws=ws[0] | ||
|
||
begin=datetime.datetime(*(time.strptime(ws.getRun().getProperty("run_start").value,"%Y-%m-%dT%H:%M:%S")[0:6])) # start of day | ||
vallist=[ff] | ||
for cc in collist: | ||
try: | ||
(cv,leftover,lval)=self.getGeneralLogValue(ws,cc,begin) | ||
except ValueError: | ||
#this is a failure to find the named log | ||
DeleteWorkspace(loadedWs) | ||
raise | ||
vallist.append(cv) | ||
if(ff==firstnum): | ||
if(isinstance(cv, numbers.Number)): | ||
ows.addColumn("double",cc) | ||
else: | ||
ows.addColumn("str",cc) | ||
if(leftover and ff>firstnum): | ||
if(lval>ows.cell(cc,ff-firstnum-1)): | ||
ows.setCell(cc,ff-firstnum-1,lval) | ||
ows.addRow(vallist) | ||
print "Finished file ",thispath | ||
DeleteWorkspace(loadedWs) | ||
|
||
|
||
self.setProperty("OutputWorkspace",ows) | ||
|
||
AlgorithmFactory.subscribe(LoadLogPropertyTable()) |
65 changes: 65 additions & 0 deletions
65
...ntid/Framework/PythonInterface/test/python/plugins/algorithms/LoadLogPropertyTableTest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import unittest | ||
import numpy | ||
from mantid.kernel import * | ||
from mantid.api import * | ||
from testhelpers import run_algorithm | ||
from mantid.api import AnalysisDataService | ||
|
||
import os | ||
|
||
class LoadLogPropertyTableTest(unittest.TestCase): | ||
|
||
def test_LoadValidFilesComments(self): | ||
""" Test to load valid files that are all present | ||
""" | ||
autoTestDir="../../../../../Test/AutoTestData/" | ||
outputWorskapceName = "LoadLogPropertyTableTest_Test1" | ||
|
||
alg_test = run_algorithm("LoadLogPropertyTable", FirstFile = autoTestDir + "MUSR00015189.nxs", | ||
LastFile = autoTestDir + "MUSR00015194.nxs", LogNames="comment", OutputWorkspace = outputWorskapceName) | ||
|
||
self.assertTrue(alg_test.isExecuted()) | ||
|
||
Verify some values | ||
tablews = AnalysisDataService.retrieve(outputWorskapceName) | ||
self.assertEqual(6, tablews.rowCount()) | ||
self.assertEqual(2, tablews.columnCount()) | ||
|
||
self.assertEqual("18.95MHz 100W", print output.cell(0,1)) | ||
self.assertEqual("15189", print output.cell(0,0)) | ||
self.assertEqual("15194", print output.cell(5,0)) | ||
|
||
run_algorithm("DeleteWorkspace", Workspace = outputWorskapceName) | ||
|
||
|
||
return | ||
|
||
def test_LoadPartiallyValidFilesLogValues(self): | ||
""" Test to load valid files that are all present | ||
""" | ||
autoTestDir="../../../../../Test/AutoTestData/" | ||
outputWorskapceName = "LoadLogPropertyTableTest_Test2" | ||
|
||
alg_test = run_algorithm("LoadLogPropertyTable", FirstFile = autoTestDir + "argus0026287.nxs", | ||
LastFile = autoTestDir + "argus0026577.nxs", LogNames="temperature_1_log", OutputWorkspace = outputWorskapceName) | ||
|
||
self.assertTrue(alg_test.isExecuted()) | ||
|
||
Verify some values | ||
tablews = AnalysisDataService.retrieve(outputWorskapceName) | ||
self.assertEqual(2, tablews.rowCount()) | ||
self.assertEqual(2, tablews.columnCount()) | ||
|
||
self.assertEqual("26287", print output.cell(0,0)) | ||
self.assertEqual("180", print output.cell(0,1)) | ||
self.assertEqual("26287", print output.cell(1,0)) | ||
self.assertEqual("7.32055", print output.cell(1,1)) | ||
|
||
run_algorithm("DeleteWorkspace", Workspace = outputWorskapceName) | ||
|
||
|
||
return | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters