|
4 | 4 | from typing import Any, Dict, List, Optional |
5 | 5 |
|
6 | 6 | from snapred.backend.dao import RunMetadata |
7 | | -from snapred.backend.dao.indexing.Versioning import VersionState |
| 7 | +from snapred.backend.dao.indexing.Versioning import VERSION_START, VersionState |
8 | 8 | from snapred.backend.dao.ingredients import ( |
9 | 9 | GroceryListItem, |
10 | 10 | ReductionIngredients, |
@@ -129,46 +129,61 @@ def validateReduction(self, request: ReductionRequest): |
129 | 129 |
|
130 | 130 | # Check if a normalization is present |
131 | 131 | normalizationExists = self.dataFactoryService.normalizationExists(request.runNumber, request.useLiteMode, state) |
| 132 | + if not normalizationExists: |
| 133 | + continueFlags |= ContinueWarning.Type.MISSING_NORMALIZATION |
132 | 134 |
|
133 | | - calibrationExists = ( |
134 | | - self.dataFactoryService.calibrationExists(request.runNumber, request.useLiteMode, state) |
135 | | - or request.alternativeCalibrationFilePath is not None |
136 | | - ) |
| 135 | + # Notes: |
| 136 | + # * At this point, the state will be initialized. |
| 137 | + # * In an initialized state, `calVersion` will never be `None`. |
| 138 | + # * `MISSING_DIFFRACTION_CALIBRATION` is now the same as the former `DEFAULT_DIFFRACTION_CALIBRATION`. |
| 139 | + # * The default calibration uses `VERSION_START`. |
| 140 | + calibrationExists = True |
| 141 | + if request.alternativeCalibrationFilePath is None: |
| 142 | + calVersion = self.dataFactoryService.getLatestApplicableCalibrationVersion( |
| 143 | + request.runNumber, request.useLiteMode, state |
| 144 | + ) |
| 145 | + if calVersion is None: |
| 146 | + raise RuntimeError( |
| 147 | + "Usage error: for an initialized state, " |
| 148 | + "diffraction-calibration version should always be at least the default version (VERSION_START)." |
| 149 | + ) |
| 150 | + if calVersion == VERSION_START(): |
| 151 | + calibrationExists = False |
| 152 | + continueFlags |= ContinueWarning.Type.MISSING_DIFFRACTION_CALIBRATION |
137 | 153 |
|
138 | 154 | # Determine the action based on missing components |
139 | 155 | if not calibrationExists and normalizationExists: |
140 | 156 | # Case: No calibration but normalization exists |
141 | | - continueFlags |= ContinueWarning.Type.MISSING_DIFFRACTION_CALIBRATION |
142 | 157 | message = ( |
143 | | - "Warning: diffraction calibration is missing.If you continue, default instrument geometry will be used." |
| 158 | + "<p><b>Diffraction calibration is missing.</b></p>" |
| 159 | + "<p>Default calibration will be used in place of actual calibration.</p>" |
| 160 | + "<p>Would you like to continue anyway?</p>" |
144 | 161 | ) |
145 | 162 | elif calibrationExists and not normalizationExists: |
146 | 163 | # Case: Calibration exists but normalization is missing |
147 | | - continueFlags |= ContinueWarning.Type.MISSING_NORMALIZATION |
148 | 164 | message = ( |
149 | | - "Warning: Reduction is missing normalization data. " |
150 | | - "Artificial normalization will be created in place of actual normalization. " |
151 | | - "Would you like to continue?" |
| 165 | + "<p><b>Normalization calibration is missing.</b></p>" |
| 166 | + "<p>Artificial normalization will be created in place of actual normalization.</p>" |
| 167 | + "<p>Would you like to continue anyway?</p>" |
152 | 168 | ) |
153 | 169 | elif not calibrationExists and not normalizationExists: |
154 | 170 | # Case: No calibration and no normalization |
155 | | - continueFlags |= ( |
156 | | - ContinueWarning.Type.MISSING_DIFFRACTION_CALIBRATION | ContinueWarning.Type.MISSING_NORMALIZATION |
157 | | - ) |
158 | 171 | message = ( |
159 | | - "Warning: Reduction is missing both normalization and calibration data. " |
160 | | - "If you continue, default instrument geometry will be used and data will be artificially normalized. " |
| 172 | + "<p><b>Both normalization and diffraction calibrations are missing.</b></p>" |
| 173 | + "<p>Default calibration will be used in place of actual calibration.</p>" |
| 174 | + "<p>Artificial normalization will be created in place of actual normalization.</p>" |
| 175 | + "<p>Would you like to continue anyway?</p>" |
161 | 176 | ) |
162 | 177 |
|
163 | | - # Remove any continue flags that are present in the request by XOR-ing with the flags |
| 178 | + # Remove any continue flags that are also present in the request by XOR-ing with the request flags |
164 | 179 | if request.continueFlags: |
165 | 180 | continueFlags ^= request.continueFlags & continueFlags |
166 | 181 |
|
167 | 182 | # If there are any continue flags set, raise a ContinueWarning with the appropriate message |
168 | 183 | if continueFlags and message: |
169 | 184 | raise ContinueWarning(message, continueFlags) |
170 | 185 |
|
171 | | - # Ensure separate continue warnings for permission check |
| 186 | + # Reinitialized continue flags for the upcoming permissions check |
172 | 187 | continueFlags = ContinueWarning.Type.UNSET |
173 | 188 |
|
174 | 189 | # Check that the user has write permissions to the save directory |
@@ -248,12 +263,14 @@ def _createReductionRecord( |
248 | 263 | state, _ = self.dataFactoryService.constructStateId(request.runNumber) |
249 | 264 |
|
250 | 265 | if request.continueFlags is not None: |
251 | | - if ContinueWarning.Type.MISSING_DIFFRACTION_CALIBRATION not in request.continueFlags: |
252 | | - # If a diffraction calibration exists, |
253 | | - # its version will have been filled in by `fetchReductionGroceries`. |
254 | | - calibration = self.dataFactoryService.getCalibrationRecord( |
255 | | - request.runNumber, request.useLiteMode, request.versions.calibration, state |
256 | | - ) |
| 266 | + # Notes: |
| 267 | + # * If a diffraction calibration exists, |
| 268 | + # its version will have been filled in by `fetchReductionGroceries`. |
| 269 | + # * `MISSING_DIFFRACTION_CALIBRATION` now means that the default diffraction calibration |
| 270 | + # with `VERSION_START` is being applied. |
| 271 | + calibration = self.dataFactoryService.getCalibrationRecord( |
| 272 | + request.runNumber, request.useLiteMode, request.versions.calibration, state |
| 273 | + ) |
257 | 274 | if ContinueWarning.Type.MISSING_NORMALIZATION not in request.continueFlags: |
258 | 275 | normalization = self.dataFactoryService.getNormalizationRecord( |
259 | 276 | request.runNumber, request.useLiteMode, state, request.versions.normalization |
@@ -355,21 +372,23 @@ def prepCombinedMask(self, request: ReductionRequest) -> WorkspaceName: |
355 | 372 |
|
356 | 373 | # if there is a mask associated with the diffcal file, load it here |
357 | 374 | calVersion = request.versions.calibration |
358 | | - if ( |
359 | | - ContinueWarning.Type.MISSING_DIFFRACTION_CALIBRATION not in request.continueFlags |
360 | | - and calVersion is VersionState.LATEST |
361 | | - ): |
| 375 | + if calVersion is VersionState.LATEST: |
362 | 376 | calVersion = self.dataFactoryService.getLatestApplicableCalibrationVersion( |
363 | 377 | runNumber, useLiteMode, state=state |
364 | 378 | ) |
365 | 379 |
|
366 | | - if calVersion is not None: |
367 | | - self.groceryClerk.name("diffcalMaskWorkspace").diffcal_mask(state, calVersion, runNumber).useLiteMode( |
368 | | - useLiteMode |
| 380 | + if calVersion is None: |
| 381 | + raise RuntimeError( |
| 382 | + "Usage error: for an initialized state, " |
| 383 | + "diffraction-calibration version should always be at least the default version (VERSION_START)." |
369 | 384 | ) |
370 | | - if request.alternativeCalibrationFilePath is not None: |
371 | | - self.groceryClerk.diffCalFilePath(request.alternativeCalibrationFilePath) |
372 | | - self.groceryClerk.add() |
| 385 | + |
| 386 | + self.groceryClerk.name("diffcalMaskWorkspace").diffcal_mask(state, calVersion, runNumber).useLiteMode( |
| 387 | + useLiteMode |
| 388 | + ) |
| 389 | + if request.alternativeCalibrationFilePath is not None: |
| 390 | + self.groceryClerk.diffCalFilePath(request.alternativeCalibrationFilePath) |
| 391 | + self.groceryClerk.add() |
373 | 392 |
|
374 | 393 | # if the user specified masks to use, also pull those |
375 | 394 | residentMasks = {} |
@@ -476,10 +495,15 @@ def fetchReductionGroceries(self, request: ReductionRequest) -> Dict[str, Any]: |
476 | 495 | # If no alternativeState state is provided, use the sample's state. |
477 | 496 | state, _ = self.dataFactoryService.constructStateId(request.runNumber) |
478 | 497 |
|
479 | | - if ContinueWarning.Type.MISSING_DIFFRACTION_CALIBRATION not in request.continueFlags: |
480 | | - calVersion = self.dataFactoryService.getLatestApplicableCalibrationVersion( |
481 | | - request.runNumber, request.useLiteMode, state |
| 498 | + calVersion = self.dataFactoryService.getLatestApplicableCalibrationVersion( |
| 499 | + request.runNumber, request.useLiteMode, state |
| 500 | + ) |
| 501 | + if calVersion is None: |
| 502 | + raise RuntimeError( |
| 503 | + "Usage error: for an initialized state, " |
| 504 | + "diffraction-calibration version should always be at least the default version (VERSION_START)." |
482 | 505 | ) |
| 506 | + |
483 | 507 | if ContinueWarning.Type.MISSING_NORMALIZATION not in request.continueFlags: |
484 | 508 | normVersion = self.dataFactoryService.getLatestApplicableNormalizationVersion( |
485 | 509 | request.runNumber, request.useLiteMode, state |
@@ -551,7 +575,8 @@ def _markWorkspaceMetadata(self, request: ReductionRequest, workspace: Workspace |
551 | 575 | altDiffCalFilePath = str(request.alternativeCalibrationFilePath) |
552 | 576 | else: |
553 | 577 | calibrationState = DiffcalStateMetadata.EXISTS |
554 | | - # The reduction workflow will automatically create a "fake" vanadium, so it shouldnt ever be None? |
| 578 | + |
| 579 | + # The reduction workflow will automatically create a "fake" vanadium, so it shouldn't ever be None? |
555 | 580 | normalizationState = ( |
556 | 581 | NormalizationStateMetadata.FAKE |
557 | 582 | if ContinueWarning.Type.MISSING_NORMALIZATION in request.continueFlags |
|
0 commit comments