Skip to content

Commit 86f8e42

Browse files
authored
Merge pull request #77 from GuiMacielPereira/check-ws-metadata
Check ws metadata before locally loading workspace
2 parents a0101e7 + 20bc53f commit 86f8e42

File tree

2 files changed

+62
-33
lines changed

2 files changed

+62
-33
lines changed

EVSVesuvio/config/analysis_inputs.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ def __init__(self, ipFilesPath):
77

88
runs="43066-43076" # 77K # The numbers of the runs to be analysed
99
empty_runs="41876-41923" # 77K # The numbers of the empty runs to be subtracted
10-
spectra='3-134' # Spectra to be analysed
1110
mode='DoubleDifference'
1211

1312
subEmptyFromRaw = True # Flag to control wether empty ws gets subtracted from raw
@@ -21,7 +20,6 @@ def __init__(self, ipFilesPath):
2120

2221
runs='43066-43076' # 100K # The numbers of the runs to be analysed
2322
empty_runs='43868-43911' # 100K # The numbers of the empty runs to be subtracted
24-
spectra='144-182' # Spectra to be analysed
2523
mode='SingleDifference'
2624

2725
subEmptyFromRaw = False # Flag to control wether empty ws gets subtracted from raw

EVSVesuvio/vesuvio_analysis/ICHelpers.py

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from mantid.simpleapi import LoadVesuvio, SaveNexus
1+
from mantid.simpleapi import Load, LoadVesuvio, SaveNexus
22
from pathlib import Path
33
from EVSVesuvio.scripts import handle_config
44

@@ -40,10 +40,7 @@ def completeICFromInputs(IC, scriptName, wsIC):
4040
IC.InstrParsPath = wsIC.ipfile
4141

4242
# Sort out input and output paths
43-
rawPath, emptyPath = inputDirsForSample(wsIC, scriptName)
44-
if not(rawPath.is_file()) or not(emptyPath.is_file()):
45-
print(f"\nWorkspaces not found.\nSaving Workspaces:\n{rawPath.name}\n{emptyPath.name}")
46-
saveWSFromLoadVesuvio(wsIC, rawPath, emptyPath)
43+
rawPath, emptyPath = setInputWSForSample(wsIC, scriptName)
4744

4845
IC.userWsRawPath = rawPath
4946
IC.userWsEmptyPath = emptyPath
@@ -79,25 +76,37 @@ def completeICFromInputs(IC, scriptName, wsIC):
7976
return
8077

8178

82-
def inputDirsForSample(wsIC, sampleName):
79+
def setInputWSForSample(wsIC, sampleName):
8380
inputWSPath = experimentsPath / sampleName / "input_ws"
8481
inputWSPath.mkdir(parents=True, exist_ok=True)
8582

86-
if int(wsIC.spectra.split("-")[1])<135:
87-
runningMode = "backward"
88-
elif int(wsIC.spectra.split("-")[0])>=135:
89-
runningMode = "forward"
90-
else:
91-
print("Problem in loading workspaces: invalid range of spectra.")
83+
runningMode = getRunningMode(wsIC)
9284

9385
rawWSName = sampleName + "_" + "raw" + "_" + runningMode + ".nxs"
9486
emptyWSName = sampleName + "_" + "empty" + "_" + runningMode + ".nxs"
9587

9688
rawPath = inputWSPath / rawWSName
9789
emptyPath = inputWSPath / emptyWSName
90+
91+
if not wsHistoryMatchesInputs(wsIC.runs, wsIC.mode, wsIC.ipfile, rawPath):
92+
saveWSFromLoadVesuvio(wsIC.runs, wsIC.mode, wsIC.ipfile, rawPath)
93+
94+
if not wsHistoryMatchesInputs(wsIC.empty_runs, wsIC.mode, wsIC.ipfile, emptyPath):
95+
saveWSFromLoadVesuvio(wsIC.empty_runs, wsIC.mode, wsIC.ipfile, emptyPath)
96+
9897
return rawPath, emptyPath
9998

10099

100+
def getRunningMode(wsIC):
101+
if wsIC.__class__.__name__ == "LoadVesuvioBackParameters":
102+
runningMode = "backward"
103+
elif wsIC.__class__.__name__ == "LoadVesuvioFrontParameters":
104+
runningMode = "forward"
105+
else:
106+
raise ValueError(f"Input class for loading workspace not valid: {wsIC.__class__.__name__}")
107+
return runningMode
108+
109+
101110
def setOutputDirsForSample(IC, sampleName):
102111
outputPath = experimentsPath / sampleName / "output_files"
103112
outputPath.mkdir(parents=True, exist_ok=True)
@@ -117,31 +126,53 @@ def setOutputDirsForSample(IC, sampleName):
117126
return
118127

119128

120-
def saveWSFromLoadVesuvio(wsIC, rawPath, emptyPath):
129+
def wsHistoryMatchesInputs(runs, mode, ipfile, localPath):
121130

122-
print(f"\nLoading and storing workspace sample runs: {wsIC.runs}\n")
131+
if not(localPath.is_file()):
132+
return False
123133

124-
rawVesuvio = LoadVesuvio(
125-
Filename=wsIC.runs,
126-
SpectrumList=wsIC.spectra,
127-
Mode=wsIC.mode,
128-
InstrumentParFile=str(wsIC.ipfile),
129-
OutputWorkspace=rawPath.name
130-
)
134+
local_ws = Load(Filename=str(localPath))
135+
ws_history = local_ws.getHistory()
136+
metadata = ws_history.getAlgorithmHistory(0)
137+
138+
saved_runs = metadata.getPropertyValue("Filename")
139+
if saved_runs != runs:
140+
print(f"Filename in saved workspace did not match: {saved_runs} and {runs}")
141+
return False
142+
143+
saved_mode = metadata.getPropertyValue("Mode")
144+
if saved_mode != mode:
145+
print(f"Mode in saved workspace did not match: {saved_mode} and {mode}")
146+
return False
131147

132-
SaveNexus(rawVesuvio, str(rawPath))
133-
print("\nRaw workspace stored locally.\n")
148+
saved_ipfile_name = Path(metadata.getPropertyValue("InstrumentParFile")).name
149+
if saved_ipfile_name != ipfile.name:
150+
print(f"IP files in saved workspace did not match: {saved_ipfile_name} and {ipfile.name}")
151+
return False
134152

135-
emptyVesuvio = LoadVesuvio(
136-
Filename=wsIC.empty_runs,
137-
SpectrumList=wsIC.spectra,
138-
Mode=wsIC.mode,
139-
InstrumentParFile=str(wsIC.ipfile),
140-
OutputWorkspace=emptyPath.name
153+
return True
154+
155+
156+
def saveWSFromLoadVesuvio(runs, mode, ipfile, localPath):
157+
158+
if "backward" in localPath.name:
159+
spectra = '3-134'
160+
elif "forward" in localPath.name:
161+
spectra = '135-198'
162+
else:
163+
raise ValueError(f"Invalid name to save workspace: {localPath.name}")
164+
165+
vesuvio_ws = LoadVesuvio(
166+
Filename=runs,
167+
SpectrumList=spectra,
168+
Mode=mode,
169+
InstrumentParFile=str(ipfile),
170+
OutputWorkspace=localPath.name,
171+
LoadLogFiles=False
141172
)
142173

143-
SaveNexus(emptyVesuvio, str(emptyPath))
144-
print("\nEmpty workspace stored locally.\n")
174+
SaveNexus(vesuvio_ws, str(localPath))
175+
print(f"Workspace saved locally at: {localPath}")
145176
return
146177

147178

0 commit comments

Comments
 (0)