mantidproject / mantid

Main repository for Mantid code
https://www.mantidproject.org
GNU General Public License v3.0
207 stars 122 forks source link

Add option to correct for attenuation post integration in ISIS single-crystal reduction classes #37575

Closed RichardWaiteSTFC closed 4 days ago

RichardWaiteSTFC commented 1 week ago

Description of work

Adds a method to apply attenuation correction post-integration - this is how SXD like to apply the correction, in addition it is quicker than applying the correction to every bin in the raw workspace.

Fixes #37235

To test:

(1) Run through script in docs

from mantid.simpleapi import *
from os import path
from Diffraction.single_crystal.base_sx import PEAK_TYPE, INTEGRATION_TYPE
from Diffraction.single_crystal.sxd import SXD

# Setup SXD
###########

sxd = SXD(vanadium_runno=32857, empty_runno=32856, detcal_path=detcal_path)
sxd.set_sample(Geometry={'Shape': 'CSG', 'Value': sxd.sphere_shape},
               Material={'ChemicalFormula': 'Na Cl', 'SampleNumberDensity': 0.0223})
sxd.set_goniometer_axes([0,1,0,1])  # ccw rotation around vertical

# load and normalise data
#########################

runs = range(32863,32865)
sxd.process_vanadium()
sxd.process_data(runs, "wccr") # wccr is the goniometer motor log name - one arg for each axis added
# if there was no log value then the angles have to be set with a sequence e.g.
# sxd.process_data(runs, [0,45])

# Find peaks and optimise UBs
##############################

for run in runs:
    ws = sxd.get_ws(run)
    peaks = sxd.find_sx_peaks(ws, nstd=8)
    sxd.remove_peaks_on_detector_edge(peaks, 2)
    sxd.set_peaks(run, peaks)
# find UB with consistent indexing across runs
sxd.find_ub_using_lattice_params(global_B=True, tol=0.15,
                                 a=5.6402, b=5.6402, c=5.6402,
                                 alpha=90, beta=90, gamma=90)
sxd.calibrate_sample_pos(tol=0.15)  # calibrates each run independently

# Integrate and save
####################

# input arguments for skew integration
save_dir = "/babylon/Public/UserName"
skew_args= {'UseNearestPeak': True, 'IntegrateIfOnEdge': False,
            'LorentzCorrection': True,
            'NVacanciesMax': 2, 'NPixPerVacancyMin': 2, 'NTOFBinsMin': 2,
            'UpdatePeakPosition': True, 'OptimiseMask': True,
            'GetTOFWindowFromBackToBackParams': False,
            'BackScatteringTOFResolution': 0.06, 'ThetaWidth': 1.5,
            'ScaleThetaWidthByWavelength': True,
            'OptimiseXWindowSize': True, 'ThresholdIoverSigma': 15}

# integrate and save each run
peak_type = PEAK_TYPE.FOUND
integration_type = INTEGRATION_TYPE.SKEW
for run in runs:
    skew_args = {**skew_args, 'OutputFile': path.join(save_dir, f"{run}_{peak_type.value}_int.pdf")}
    sxd.integrate_data(integration_type, peak_type, run=run, **skew_args)
    sxd.calc_absorption_weighted_path_lengths(peak_type, integration_type, run=run, ApplyCorrection=True)
    sxd.save_peak_table(run, peak_type, integration_type, save_dir, save_format='SHELX')

# save combined table
sxd.save_all_peaks(peak_type, integration_type, save_dir=save_dir, save_format=fmt)

Reviewer

Please comment on the points listed below (full description). Your comments will be used as part of the gatekeeper process, so please comment clearly on what you have checked during your review. If changes are made to the PR during the review process then your final comment will be the most important for gatekeepers. In this comment you should make it clear why any earlier review is still valid, or confirm that all requested changes have been addressed.

Code Review

Functional Tests

Does everything look good? Mark the review as Approve. A member of @mantidproject/gatekeepers will take care of it.

Gatekeeper

If you need to request changes to a PR then please add a comment and set the review status to "Request changes". This will stop the PR from showing up in the list for other gatekeepers.

RichardWaiteSTFC commented 4 days ago

Whilst running the script, there were some typos which I fixed in #37615

Other than that, the script ran successfully. Also checked that altering the calc_absorption_weigthed_path_length() function made the unit test fail, so that's good.

Would it be possible to add another unit test like test_process_data_with_scale_integrated to check for the if statement you added in the process_data function?

This if statement actually caused the system test SXDAnalysis.SXDProcessSampleData to fail, hence the need to specify scale_integrated=False here https://github.com/mantidproject/mantid/blob/6d2da87c679aade99d18ef0b1e2815d98247566c/Testing/SystemTests/tests/framework/SXDAnalysis.py#L95

but I can add another system test if you like?