Skip to content

Commit

Permalink
Config 'reduction.output.useEffectiveInstrument' now allows the reduc…
Browse files Browse the repository at this point in the history
…ed-instrument substitution to be turned off. That is, not just for output format.
  • Loading branch information
Kort Travis committed Nov 14, 2024
1 parent 03e3f09 commit 353be09
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 27 deletions.
15 changes: 7 additions & 8 deletions src/snapred/backend/data/LocalDataService.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,11 +661,9 @@ def writeReductionData(self, record: ReductionRecord):
# instrument-I/O implementation (for non XML-based instruments).
#
# 2) For SNAPRed internal use:
# if `reduction.output.useLegacyInstrument` is set in "application.yml"
# output workspaces including an instrument with embedded instrument XML,
# will then be saved using `SaveNexus`. This case is retained to allow some
# flexibility in what reduction data SNAPRed saves. However, please note that this case cannot
# correctly save and restore workspaces with the effective-instrument (reduced) geometry as in (1).
# if `reduction.output.useEffectiveInstrument` is set to false in "application.yml",
# output workspaces will be saved without converting their instruments to the reduced form.
# This case is retained to allow some flexibility in what specifically is saved with the reduction data.
#

runNumber, useLiteMode, timestamp = record.runNumber, record.useLiteMode, record.timestamp
Expand All @@ -678,13 +676,14 @@ def writeReductionData(self, record: ReductionRecord):
# WARNING: `writeReductionRecord` must be called before `writeReductionData`.
raise RuntimeError(f"reduction version directories {filePath.parent} do not exist")

useLegacyInstrument = Config["reduction.output.useLegacyInstrument"]
useEffectiveInstrument = Config["reduction.output.useEffectiveInstrument"]

for ws in record.workspaceNames:
# Append workspaces to hdf5 file, in order of the `workspaces` list

if ws.tokens("workspaceType") == wngt.REDUCTION_PIXEL_MASK:
# The mask workspace always uses the legacy instrument.

# The mask workspace always uses the non-reduced instrument.
self.mantidSnapper.SaveNexus(
f"Append workspace '{ws}' to reduction output",
InputWorkspace=ws,
Expand All @@ -697,7 +696,7 @@ def writeReductionData(self, record: ReductionRecord):
maskFilename = ws + ".h5"
self.writePixelMask(filePath.parent, Path(maskFilename), ws)
else:
if not useLegacyInstrument:
if useEffectiveInstrument:
self.mantidSnapper.SaveNexusESS(
f"Append workspace '{ws}' to reduction output",
InputWorkspace=ws,
Expand Down
12 changes: 7 additions & 5 deletions src/snapred/backend/recipe/ReductionRecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from snapred.backend.recipe.ReductionGroupProcessingRecipe import ReductionGroupProcessingRecipe
from snapred.meta.mantid.WorkspaceNameGenerator import ValueFormatter as wnvf
from snapred.meta.mantid.WorkspaceNameGenerator import WorkspaceNameGenerator as wng
from snapred.meta.Config import Config

logger = snapredLogger.getLogger(__name__)

Expand Down Expand Up @@ -276,11 +277,12 @@ def execute(self):
self._cloneIntermediateWorkspace(sampleClone, f"sample_ApplyNormalization_{groupingIndex}")

# 5. Replace the instrument with the effective instrument for this grouping
self._applyRecipe(
EffectiveInstrumentRecipe,
self.ingredients.effectiveInstrument(groupingIndex),
inputWorkspace=sampleClone,
)
if Config["reduction.output.useEffectiveInstrument"]:
self._applyRecipe(
EffectiveInstrumentRecipe,
self.ingredients.effectiveInstrument(groupingIndex),
inputWorkspace=sampleClone,
)

# Cleanup
outputs.append(sampleClone)
Expand Down
4 changes: 2 additions & 2 deletions src/snapred/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ calibration:
reduction:
output:
extension: .nxs
# set the following flag if saving non-reduced instruments
useLegacyInstrument: false
# convert the instrument for the output workspaces into the reduced form
useEffectiveInstrument: true

mantid:
workspace:
Expand Down
4 changes: 2 additions & 2 deletions tests/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ calibration:
reduction:
output:
extension: .nxs
# set the following flag if saving non-reduced instruments
useLegacyInstrument: true
# convert the instrument for the output workspaces into the reduced form
useEffectiveInstrument: true

mantid:
workspace:
Expand Down
17 changes: 7 additions & 10 deletions tests/unit/backend/data/test_LocalDataService.py
Original file line number Diff line number Diff line change
Expand Up @@ -1587,7 +1587,7 @@ def _createWorkspaces(wss: List[WorkspaceName]):
mask = mtd.unique_hidden_name()
createCompatibleMask(mask, src)

if not Config["reduction.output.useLegacyInstrument"]:
if Config["reduction.output.useEffectiveInstrument"]:
# Convert the source workspace's instrument to the reduced form:
# * no monitors;
# * only one bank of detectors;
Expand All @@ -1612,15 +1612,12 @@ def _createWorkspaces(wss: List[WorkspaceName]):
l2s.append(l2)
twoThetas.append(twoTheta)
azimuths.append(azimuth)

ls2 = np.rad2deg(l2s)
twoThetas = np.rad2deg(twoThetas)
azimuths = np.rad2deg(azimuths)

EditInstrumentGeometry(
Workspace=src,
L2=l2s,
Polar=twoThetas,
Azimuthal=azimuths
L2=np.rad2deg(l2s),
Polar=np.rad2deg(twoThetas),
Azimuthal=np.rad2deg(azimuths)
)
assert mtd.doesExist(src)

Expand Down Expand Up @@ -1684,7 +1681,7 @@ def test_writeReductionData_legacy_instrument(readSyntheticReductionRecord, crea
fileName = wng.reductionOutputGroup().runNumber(runNumber).timestamp(timestamp).build()
fileName += Config["nexus.file.extension"]

with Config_override("reduction.output.useLegacyInstrument", True):
with Config_override("reduction.output.useEffectiveInstrument", False):
wss = createReductionWorkspaces(testRecord.workspaceNames) # noqa: F841
localDataService = LocalDataService()
with reduction_root_redirect(localDataService, stateId=stateId):
Expand Down Expand Up @@ -1829,7 +1826,7 @@ def test_readWriteReductionData_legacy_instrument(readSyntheticReductionRecord,
fileName = wng.reductionOutputGroup().runNumber(runNumber).timestamp(timestamp).build()
fileName += Config["reduction.output.extension"]

with Config_override("reduction.output.useLegacyInstrument", True):
with Config_override("reduction.output.useEffectiveInstrument", False):
wss = createReductionWorkspaces(testRecord.workspaceNames) # noqa: F841
localDataService = LocalDataService()
with reduction_root_redirect(localDataService, stateId=stateId):
Expand Down

0 comments on commit 353be09

Please sign in to comment.