Skip to content

Commit

Permalink
Add configuration parameter transmissionErrorTolerance and throw exce…
Browse files Browse the repository at this point in the history
…ption when the transmission error tolerance is exceeded
  • Loading branch information
backmari committed Jan 28, 2025
1 parent 6fc28bd commit 1db5043
Show file tree
Hide file tree
Showing 21 changed files with 246 additions and 20 deletions.
3 changes: 3 additions & 0 deletions scripts/prepare_sensitivities_biosans.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
SOLID_ANGLE_CORRECTION = True
# Flag to do dependent correction with transmission correction
THETA_DEPENDENT_CORRECTION = True
# Maximum relative transmission error
TRANSMISSION_ERROR_TOLERANCE = 0.01

# Single or multiple Flood measurements
MOVING_DETECTORS = False
Expand Down Expand Up @@ -102,6 +104,7 @@
transmission_flood_runs=TRANSMISSION_FLOOD_RUNS,
transmission_reference_runs=TRANSMISSION_REFERENCE_RUNS,
beam_trap_factor=BEAM_TRAP_SIZE_FACTOR,
transmission_error_tolerance=TRANSMISSION_ERROR_TOLERANCE,
)
preparer.set_theta_dependent_correction_flag(THETA_DEPENDENT_CORRECTION)

Expand Down
4 changes: 4 additions & 0 deletions src/drtsans/configuration/schema/BIOSANS.json
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@
},
"wedge2overlapStitchQmax": {
"$ref": "common.json#/definitions/safeStitchQboundsSpecs"
},
"transmissionErrorTolerance": {
"$ref": "common.json#/configuration/transmissionErrorTolerance",
"default": 0.01
}
},
"required": ["outputDir", "wavelength", "wavelengthSpread", "useTimeSlice", "timeSliceInterval",
Expand Down
4 changes: 4 additions & 0 deletions src/drtsans/configuration/schema/EQSANS.json
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@
"type": "boolean",
"default": false,
"description": "use smallest wavelength as reference wavelength"
},
"transmissionErrorTolerance": {
"$ref": "common.json#/configuration/transmissionErrorTolerance",
"default": 0.01
}
},
"required": [
Expand Down
3 changes: 2 additions & 1 deletion src/drtsans/configuration/schema/GPSANS.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@
"smearingPixelSizeY": {"$ref": "common.json#/configuration/smearingPixelSizeY"},
"useSubpixels": {"$ref": "common.json#/configuration/useSubpixels", "default": true},
"subpixelsX": {"$ref": "common.json#/configuration/subpixelsX", "default": 5},
"subpixelsY": {"$ref": "common.json#/configuration/subpixelsY", "default": 5}
"subpixelsY": {"$ref": "common.json#/configuration/subpixelsY", "default": 5},
"transmissionErrorTolerance": {"$ref": "common.json#/configuration/transmissionErrorTolerance", "default": 0.01}
},
"required": ["outputDir", "wavelength", "wavelengthSpread", "useTimeSlice", "timeSliceInterval",
"timeSliceOffset", "timeSlicePeriod", "useLogSlice", "logSliceName", "logSliceInterval",
Expand Down
4 changes: 4 additions & 0 deletions src/drtsans/configuration/schema/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,10 @@
"wing_detector": ""
}
]
},
"transmissionErrorTolerance": {
"$ref": "common.json#/definitions/safeStringPositiveFloat",
"description": "Maximum relative error in the transmission to calculate the transmission correction"
}
}
}
6 changes: 5 additions & 1 deletion src/drtsans/mono/biosans/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from drtsans.stitch import stitch_binned_profiles
from drtsans.reductionlog import savereductionlog
from drtsans.thickness_normalization import normalize_by_thickness
from drtsans.transmission import TransmissionErrorToleranceError

# third party imports
from mantid.dataobjects import Workspace2D
Expand Down Expand Up @@ -1196,6 +1197,7 @@ def reduce_single_configuration(
sample_trans_value = reduction_input["sample"]["transmission"]["value"]
bkg_trans_value = reduction_input["background"]["transmission"]["value"]
theta_dependent_transmission = reduction_config["useThetaDepTransCorrection"]
transmission_error_tolerance = reduction_config["transmissionErrorTolerance"]
mask_panel = None
if reduction_config["useMaskBackTubes"] is True:
mask_panel = "back"
Expand Down Expand Up @@ -1330,6 +1332,7 @@ def reduce_single_configuration(
empty_trans_ws,
radius=transmission_radius,
radius_unit="mm",
transmission_error_tolerance=transmission_error_tolerance,
)
logger.notice(f"Background transmission = {bkgd_trans_ws.extractY()[0, 0]}")

Expand Down Expand Up @@ -1369,6 +1372,7 @@ def _prepare_sample_transmission_ws(_sample_transmission):
empty_trans_ws,
radius=transmission_radius,
radius_unit="mm",
transmission_error_tolerance=transmission_error_tolerance,
)

sample_trans_ws = None
Expand Down Expand Up @@ -1488,7 +1492,7 @@ def _prepare_sample_transmission_ws(_sample_transmission):
"background": None,
},
)
except ZeroMonitorCountsError as e:
except (ZeroMonitorCountsError, TransmissionErrorToleranceError) as e:
if time_slice:
logger.warning(f"Skipping slice {sample_name}: {e}.")
continue
Expand Down
18 changes: 16 additions & 2 deletions src/drtsans/mono/biosans/prepare_sensitivities_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,13 @@ def _mask_beam_center(self, flood_ws: MantidWorkspace, beam_center: str) -> Mant

return masked_flood_ws

def set_transmission_correction(self, transmission_flood_runs, transmission_reference_runs, beam_trap_factor=2):
def set_transmission_correction(
self,
transmission_flood_runs,
transmission_reference_runs,
beam_trap_factor=2,
transmission_error_tolerance=None,
):
"""Set transmission beam run and transmission flood runs
Parameters
Expand All @@ -191,6 +197,8 @@ def set_transmission_correction(self, transmission_flood_runs, transmission_refe
transmission reference runs
beam_trap_factor : float, int
factor to beam trap size for masking angle
transmission_error_tolerance: float
Maximum relative transmission error
Returns
-------
Expand Down Expand Up @@ -228,6 +236,8 @@ def format_run_or_runs(run_s):
# Set the beam trap factor for transmission reference and flood run to mask angle
self._biosans_beam_trap_factor = beam_trap_factor

self._transmission_error_tolerance = transmission_error_tolerance

def set_theta_dependent_correction_flag(self, flag):
"""Set the flag to do theta dep with transmission correction
Expand Down Expand Up @@ -320,7 +330,11 @@ def _apply_transmission_correction(self, flood_ws, transmission_beam_run, transm
)

# Zero-Angle Transmission Co-efficients
transmission_corr_ws = calculate_transmission(transmission_flood_ws, transmission_workspace)
transmission_corr_ws = calculate_transmission(
transmission_flood_ws,
transmission_workspace,
transmission_error_tolerance=self._transmission_error_tolerance,
)
average_zero_angle = np.mean(transmission_corr_ws.readY(0))
average_zero_angle_error = np.linalg.norm(transmission_corr_ws.readE(0))
logger.notice(
Expand Down
3 changes: 3 additions & 0 deletions src/drtsans/mono/gpsans/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,7 @@ def reduce_single_configuration(loaded_ws, reduction_input, prefix="", skip_nan=
sample_trans_value = reduction_input["sample"]["transmission"]["value"]
bkg_trans_value = reduction_input["background"]["transmission"]["value"]
theta_deppendent_transmission = reduction_config["useThetaDepTransCorrection"]
transmission_error_tolerance = reduction_config["transmissionErrorTolerance"]
mask_panel = None
if reduction_config["useMaskBackTubes"]:
mask_panel = "back"
Expand Down Expand Up @@ -1218,6 +1219,7 @@ def reduce_single_configuration(loaded_ws, reduction_input, prefix="", skip_nan=
empty_trans_ws,
radius=transmission_radius,
radius_unit="mm",
transmission_error_tolerance=transmission_error_tolerance,
)
logger.notice(f"Background transmission = {bkgd_trans_ws.extractY()[0, 0]}")
background_transmission_dict = {
Expand Down Expand Up @@ -1246,6 +1248,7 @@ def reduce_single_configuration(loaded_ws, reduction_input, prefix="", skip_nan=
empty_trans_ws,
radius=transmission_radius,
radius_unit="mm",
transmission_error_tolerance=transmission_error_tolerance,
)
logger.notice(f"Sample transmission = {sample_trans_ws.extractY()[0, 0]}")
sample_transmission_dict = {
Expand Down
41 changes: 40 additions & 1 deletion src/drtsans/mono/transmission.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,45 @@
__all__ = ["calculate_transmission", "apply_transmission_correction"]


def calculate_transmission(input_sample, input_reference, radius=None, radius_unit="mm", output_workspace=None):
def calculate_transmission(
input_sample,
input_reference,
radius=None,
radius_unit="mm",
transmission_error_tolerance=None,
output_workspace=None,
):
"""
Calculate the raw transmission coefficients at zero scattering angle
from already prepared sample and reference data.
Parameters
----------
input_sample: str, ~mantid.api.MatrixWorkspace, ~mantid.api.IEventWorkspace
Prepared sample workspace (possibly obtained with an attenuated beam)
input_reference: str, ~mantid.api.MatrixWorkspace, ~mantid.api.IEventWorkspace
Prepared direct beam workspace (possibly obtained with an attenuated beam)
radius: float
Radius around the beam center for pixel integration, in millimeters.
If None, radius will be obtained or calculated using `input_reference` workspace.
radius_unit: str
Either 'mm' or 'm', and only used in conjunction with option `radius`.
transmission_error_tolerance: float
Maximum relative error for transmission
output_workspace: str
Name of the output workspace containing the raw transmission values.
If None, a hidden random name will be provided.
Returns
-------
~mantid.api.MatrixWorkspace
Workspace containing the raw transmission values
Raises
------
TransmissionToleranceError
If there is insufficient statistics to calculate the transmission correction
"""
if radius is None:
logger.information("Calculating beam radius from sample logs")
radius = beam_radius(input_reference, unit="mm")
Expand All @@ -17,6 +55,7 @@ def calculate_transmission(input_sample, input_reference, radius=None, radius_un
input_reference,
radius,
radius_unit=radius_unit,
transmission_error_tolerance=transmission_error_tolerance,
output_workspace=output_workspace,
)

Expand Down
5 changes: 5 additions & 0 deletions src/drtsans/tof/eqsans/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ def reduce_single_configuration(
sample_trans_value = reduction_input["sample"]["transmission"]["value"]
bkg_trans_value = reduction_input["background"]["transmission"]["value"]
theta_dependent_transmission = reduction_config["useThetaDepTransCorrection"]
transmission_error_tolerance = reduction_config["transmissionErrorTolerance"]
mask_panel = "back" if reduction_config["useMaskBackTubes"] is True else None
output_suffix = ""

Expand Down Expand Up @@ -769,6 +770,7 @@ def reduce_single_configuration(
"sample",
output_dir,
outputFilename,
transmission_error_tolerance,
)
(sample_trans_ws, sample_transmission_dict, sample_transmission_raw_dict) = sample_returned

Expand All @@ -792,6 +794,7 @@ def reduce_single_configuration(
"bkgd",
output_dir,
base_out_name,
transmission_error_tolerance,
)
else:
bkgd_returned = (None, None, None)
Expand Down Expand Up @@ -826,6 +829,7 @@ def reduce_single_configuration(
"elasticref",
None,
None,
transmission_error_tolerance,
)
else:
output = (None, None, None)
Expand All @@ -846,6 +850,7 @@ def reduce_single_configuration(
"elasticrefbkgd",
None,
None,
transmission_error_tolerance,
)
else:
output = (None, None, None)
Expand Down
41 changes: 41 additions & 0 deletions src/drtsans/tof/eqsans/reduction_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,46 @@ def process_transmission(
type_name,
output_dir,
output_file_name,
transmission_error_tolerance=None,
):
"""
Prepares workspaces and calculates transmission
Parameters
----------
transmission_ws
Sample transmission workspace
empty_trans_ws
Empty beam transmission workspace
transmission_radius
Radius around the beam center for pixel integration, in millimeters.
If None, radius will be obtained or calculated using `input_reference` workspace.
transmission_error_tolerance: float
Maximum relative error for transmission
sensitivity_ws
Sensitivity workspace
flux_method: str
Method for flux normalization. Either 'monitor', or 'time'.
flux: str
if ``flux_method`` is proton charge, then path to file containing the
wavelength distribution of the neutron flux. If ``flux method`` is
monitor, then path to file containing the flux-to-monitor ratios.
if ``flux_method`` is time, then pass one log entry name such
as ``duration`` or leave it as :py:obj:`None` for automatic log search.
prefix: str
Prefix for transmission workspace name
type_name: str
String used in transmission workspace name to denote type (e.g. sample/background)
output_dir: str
Output directory for transmission output
output_file_name: str
Output filename prefix for transmission output
Returns
-------
(~mantid.api.MatrixWorkspace, dict, dict)
transmission workspace, processed transmission dictionary, raw transmission dictionary
"""
# sample transmission
processed_transmission_dict = {} # for output log
raw_transmission_dict = {} # for output log
Expand All @@ -166,6 +205,7 @@ def process_transmission(
empty_trans_ws,
radius=transmission_radius,
radius_unit="mm",
transmission_error_tolerance=transmission_error_tolerance,
)
print(f"{type_name} transmission =", calculated_trans_ws.extractY()[0, 0])

Expand All @@ -186,6 +226,7 @@ def process_transmission(
radius=transmission_radius,
radius_unit="mm",
fit_function="",
transmission_error_tolerance=transmission_error_tolerance,
)

raw_tr_fn = os.path.join(output_dir, f"{output_file_name}_raw_trans.txt")
Expand Down
4 changes: 4 additions & 0 deletions src/drtsans/tof/eqsans/transmission.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def calculate_transmission(
input_reference,
radius=None,
radius_unit="mm",
transmission_error_tolerance=None,
fit_function="name=LinearBackground",
output_workspace=None,
output_raw_transmission=None,
Expand All @@ -54,6 +55,8 @@ def calculate_transmission(
If None, radius will be obtained or calculated using ``input_reference``.
radius_unit: str
Either 'mm' or 'm', and only used in conjunction with option ``radius``.
transmission_error_tolerance: float
Maximum relative error for transmission
fit_function: str
String representation of the fit function. See Mantid's
:ref:`UserFunction <func-UserFunction>` or any of Mantid's built-in functions.
Expand Down Expand Up @@ -90,6 +93,7 @@ def calculate_transmission(
input_reference,
radius=radius,
radius_unit=radius_unit,
transmission_error_tolerance=transmission_error_tolerance,
output_workspace=output_raw_transmission,
)
if bool(fit_function) is True:
Expand Down
Loading

0 comments on commit 1db5043

Please sign in to comment.