From 61d3b9a456446718228256ad56ad5f38ab20bc6f Mon Sep 17 00:00:00 2001 From: Richard Waite Date: Thu, 12 Sep 2024 17:10:25 +0100 Subject: [PATCH 1/2] Refactor into smaller functions --- scripts/Diffraction/isis_powder/hrpd.py | 107 ++++++++++++++---------- 1 file changed, 64 insertions(+), 43 deletions(-) diff --git a/scripts/Diffraction/isis_powder/hrpd.py b/scripts/Diffraction/isis_powder/hrpd.py index 1ca05a7fcff3..817aa69d2660 100644 --- a/scripts/Diffraction/isis_powder/hrpd.py +++ b/scripts/Diffraction/isis_powder/hrpd.py @@ -202,7 +202,7 @@ def _mask_prompt_pulses(self, ws, ispecs=None): **mask_kwargs, ) - def _subtract_prompt_pulses(self, ws, tof_res=0.002): + def _subtract_prompt_pulses(self, ws): """ HRPD has a long flight path from the moderator resulting in sharp peaks from the proton pulse that maintain their @@ -235,15 +235,7 @@ def _subtract_prompt_pulses(self, ws, tof_res=0.002): cen = npulse * PROMPT_PULSE_INTERVAL xlo = cen - PROMPT_PULSE_LEFT_WIDTH - 10 # add extra to ensure get background on both sides of peak xhi = cen + PROMPT_PULSE_RIGHT_WIDTH + 10 - comp_func = FunctionFactory.createInitialized( - f"name=PearsonIV, Centre={8}, Intensity=1,Sigma=8.5, Exponent=1.5, Skew=-5," - f"constraints=(0.2 0 else "" @@ -251,55 +243,29 @@ def _subtract_prompt_pulses(self, ws, tof_res=0.002): fit_kwargs["StartX" + key_suffix] = xlo fit_kwargs["EndX" + key_suffix] = xhi fit_kwargs["WorkspaceIndex" + key_suffix] = ispec - # work out how to exclude - exclude = [] - diff_consts = si.diffractometerConstants(ispec) - for dpk in dpks_bragg: - tof_pk = UnitConversion.run("dSpacing", "TOF", dpk, 0, DeltaEModeType.Elastic, diff_consts) - tof_pk_lo = tof_pk * (1 - tof_res) - tof_pk_hi = tof_pk * (1 + tof_res) - if xlo < tof_pk_lo < xhi or xlo < tof_pk_hi < xhi or (tof_pk_lo < xlo and tof_pk_hi > xhi): - exclude.extend([tof_pk_lo, tof_pk_hi]) + # exclude x-ranges where Bragg peaks overlap prompt pulse within some fractional resolution + exclude = self._get_tofs_to_exclude(si, ispec, dpks_bragg, xlo, xhi) if exclude: func.fixParameter(f"f{ipulse}.f1.A0") # fix constant background - Bragg peak can mess with bg fit_kwargs["Exclude" + key_suffix] = exclude # check that at least one fit region has no overlapping prompt pulse if len([key for key in fit_kwargs.keys() if "Exclude" in key]) < len(npulses): - # tie peak parameters to be common for all prompt pulses - for idomain in range(len(npulses) - 1): - for param_name in ["Intensity", "Sigma", "Exponent", "Skew", "Centre"]: - func.tie(f"f{idomain}.f0.{param_name}", f"f{len(npulses) - 1}.f0.{param_name}") # tie to first - # fix some peak parameters - for param_name in ["Sigma", "Exponent", "Skew"]: - func.fixParameter(f"f{len(npulses) - 1}.f0.{param_name}") + self._add_ties_to_multidomain_prompt_pulse_func(func) fit_output = mantid.Fit(Function=func, **fit_kwargs) # subtract prompt pulse only from spectrum success = fit_output.OutputStatus == "success" or "Changes in function value are too small" in fit_output.OutputStatus if success: func = fit_output.Function.function - for ipulse in range(fit_output.Function.nDomains): - func.removeTie(f"f{ipulse}.f0.Centre") - key_suffix = f"_{ipulse}" if ipulse > 0 else "" - if "Exclude" + key_suffix in fit_kwargs: - func.fixParameter(f"f{ipulse}.f0.Centre") # fix centre as can't fit independently with bragg - else: - fitted_cen = func[ipulse][0]["Centre"] - func[ipulse][0].addConstraints( - f"{fitted_cen - 5} 0 else "" + if "Exclude" + key_suffix in fit_kwargs: + func.fixParameter(f"f{ipulse}.f0.Centre") # fix centre as can't fit independently with bragg + else: + fitted_cen = func[ipulse][0]["Centre"] + func[ipulse][0].addConstraints(f"{fitted_cen - 5} xhi): + exclude.extend([tof_pk_lo, tof_pk_hi]) + return exclude + + @staticmethod + def _setup_single_prompt_pulse_function(ws, ispec, cen, xlo, xhi): + comp_func = FunctionFactory.createInitialized( + "name=PearsonIV, Centre=8, Intensity=1,Sigma=8.5, Exponent=1.5, Skew=-5," + "constraints=(0.2 Date: Wed, 2 Oct 2024 17:31:44 +0100 Subject: [PATCH 2/2] Add variables to shorten condition statements As requested during PR review --- scripts/Diffraction/isis_powder/hrpd.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scripts/Diffraction/isis_powder/hrpd.py b/scripts/Diffraction/isis_powder/hrpd.py index 817aa69d2660..9d3c49a72c49 100644 --- a/scripts/Diffraction/isis_powder/hrpd.py +++ b/scripts/Diffraction/isis_powder/hrpd.py @@ -249,12 +249,14 @@ def _subtract_prompt_pulses(self, ws): func.fixParameter(f"f{ipulse}.f1.A0") # fix constant background - Bragg peak can mess with bg fit_kwargs["Exclude" + key_suffix] = exclude # check that at least one fit region has no overlapping prompt pulse - if len([key for key in fit_kwargs.keys() if "Exclude" in key]) < len(npulses): + excluded_keys = [key for key in fit_kwargs.keys() if "Exclude" in key] + if len(excluded_keys) < len(npulses): self._add_ties_to_multidomain_prompt_pulse_func(func) fit_output = mantid.Fit(Function=func, **fit_kwargs) # subtract prompt pulse only from spectrum - success = fit_output.OutputStatus == "success" or "Changes in function value are too small" in fit_output.OutputStatus - if success: + is_success = fit_output.OutputStatus == "success" + is_small_change = "Changes in function value are too small" in fit_output.OutputStatus + if is_success or is_small_change: func = fit_output.Function.function self._free_ties_of_multidomain_prompt_pulse_func(func, fit_kwargs) fit_output_final = mantid.Fit(Function=func, **fit_kwargs)