Commit 23a33cbd authored by Whitfield, Ross's avatar Whitfield, Ross
Browse files

Start complete workflow

parent a6e22a3e
import numpy as np
from mantid.simpleapi import (ConvertUnits, ExtractSpectra, CloneWorkspace, Rebin,
ExtractUnmaskedSpectra, CrossCorrelate, GetDetectorOffsets,
ConvertDiffCal, mtd, ApplyDiffCal, DiffractionFocussing, PDCalibration)
from mantid.simpleapi import (ConvertUnits, ExtractSpectra,
CloneWorkspace, Rebin,
ExtractUnmaskedSpectra, CrossCorrelate,
GetDetectorOffsets, ConvertDiffCal, mtd,
ApplyDiffCal, DiffractionFocussing,
PDCalibration)
def cc_calibrate_groups(data_ws,
group_ws,
output_basename,
previous_calibration=None,
Step = 0.001,
Step=0.001,
DReference=1.2615,
Xmin=1.22,
Xmax=1.30):
Xmax=1.30,
MaxDSpaceShift=None):
if previous_calibration:
ApplyDiffCal(data_ws, CalibrationWorkspace=previous_calibration)
data_d = ConvertUnits(data_ws, Target='dSpacing')
# skip group 0 because this is eveything that wasn't included in a group
group_list = np.unique(group_ws.extractY())
for group in group_list:
indexes = np.where(group_ws.extractY().flatten() == group)[0]
ExtractSpectra(data_d, WorkspaceIndexList=indexes, OutputWorkspace=f'_tmp_group_{group}')
ExtractUnmaskedSpectra(f'_tmp_group_{group}', OutputWorkspace=f'_tmp_group_{group}')
if mtd[f'_tmp_group_{group}'].getNumberHistograms() < 2:
continue
Rebin(f'_tmp_group_{group}', Params=f'{Xmin},{Step},{Xmax}', OutputWorkspace=f'_tmp_group_{group}')
CrossCorrelate(f'_tmp_group_{group}',
Xmin=Xmin, XMax=Xmax,
MaxDSpaceShift=MaxDSpaceShift,
WorkspaceIndexMin=0,
WorkspaceIndexMax=mtd[f'_tmp_group_{group}'].getNumberHistograms()-1,
OutputWorkspace=f'_tmp_group_{group}_cc')
......@@ -55,7 +61,7 @@ def pdcalibration_groups(data_ws,
0.5947,0.6307,0.6866,0.7283,0.8185,
0.8920,1.0758,1.2615,2.0599), # diamond
TofBinning=(300,-.001,16666.7),
PeakFunction='Gaussian',
PeakFunction='IkedaCarpenterPV',
PeakWindow=0.1,
PeakWidthPercent=None):
......@@ -99,3 +105,26 @@ def pdcalibration_groups(data_ws,
/ grouped_det_to_difc[detid])
return mtd[f'{output_basename}_cc_pd_diffcal']
def do_group_calibration(ws,
groups,
previous_calibration=None,
output_workspace_basename = "_tmp_group_calibration",
cc_kwargs={},
pdcal_kwargs={}):
cc_diffcal = cc_calibrate_groups(ws,
groups,
output_workspace_basename,
previous_calibration,
**cc_kwargs)
diffcal = pdcalibration_groups(ws,
groups,
cc_diffcal,
output_workspace_basename,
previous_calibration,
**pdcal_kwargs)
return diffcal
......@@ -58,13 +58,11 @@ class TestGroupCalibration(unittest.TestCase):
output_workspace_basename = 'test_from_eng'
# same for all spectra in group
starting_difc = [ws.spectrumInfo().difcUncalibrated(i) for i in range(ws.getNumberHistograms())]
cc_diffcal = group_calibration.cc_calibrate_groups(ws,
groups,
output_workspace_basename,
Step=0.001,
DReference=2.0,
Xmin=1.75,
Xmax=2.25)
......@@ -82,6 +80,7 @@ class TestGroupCalibration(unittest.TestCase):
cc_diffcal,
output_workspace_basename,
PeakPositions = [1.0, 2.0, 4.0],
PeakFunction='Gaussian',
PeakWindow=0.4)
assert_allclose(diffcal.column('difc'),
......@@ -100,7 +99,6 @@ class TestGroupCalibration(unittest.TestCase):
output_workspace_basename = 'test_from_eng_prev_cal'
# same for all spectra in group
starting_difc = [ws.spectrumInfo().difcUncalibrated(i) for i in range(ws.getNumberHistograms())]
previous_diffcal = CreateEmptyTableWorkspace()
......@@ -123,7 +121,6 @@ class TestGroupCalibration(unittest.TestCase):
groups,
output_workspace_basename,
previous_calibration=previous_diffcal,
Step=0.001,
DReference=2.0,
Xmin=1.75,
Xmax=2.25)
......@@ -144,6 +141,7 @@ class TestGroupCalibration(unittest.TestCase):
output_workspace_basename,
previous_calibration=previous_diffcal,
PeakPositions = [1.0, 2.0, 4.0],
PeakFunction='Gaussian',
PeakWindow=0.4)
assert_allclose(diffcal.column('difc'),
......@@ -156,6 +154,53 @@ class TestGroupCalibration(unittest.TestCase):
starting_difc[6]/(0.95*1.02),
starting_difc[7]*1.01], rtol=0.005)
def test_di_group_calibration(self):
ws, groups = create_test_ws_and_group()
starting_difc = [ws.spectrumInfo().difcUncalibrated(i) for i in range(ws.getNumberHistograms())]
previous_diffcal = CreateEmptyTableWorkspace()
previous_diffcal.addColumn("int", "detid")
previous_diffcal.addColumn("double", "difc")
previous_diffcal.addColumn("double", "difa")
previous_diffcal.addColumn("double", "tzero")
previous_diffcal.addRow([1, starting_difc[0]*1.01, 0, 0])
previous_diffcal.addRow([2, starting_difc[1]*1.01, 0, 0])
previous_diffcal.addRow([3, starting_difc[2]*1.01, 0, 0])
previous_diffcal.addRow([4, starting_difc[3]*1.01, 0, 0])
previous_diffcal.addRow([5, starting_difc[4]*1.01, 0, 0])
previous_diffcal.addRow([6, starting_difc[5]*1.01, 0, 0])
previous_diffcal.addRow([7, starting_difc[6]*1.01, 0, 0])
previous_diffcal.addRow([8, starting_difc[7]*1.01, 0, 0])
diffcal = group_calibration.do_group_calibration(ws,
groups,
previous_calibration=previous_diffcal,
output_workspace_basename = "group_calibration",
cc_kwargs={
"Step": 0.001,
"DReference": 2.0,
"Xmin": 1.75,
"Xmax": 2.25},
pdcal_kwargs={
"PeakPositions": [1.0, 2.0, 4.0],
"TofBinning": [300,-.001,16666.7],
"PeakFunction": 'Gaussian',
"PeakWindow": 0.4,
"PeakWidthPercent": None})
assert_allclose(diffcal.column('difc'),
[starting_difc[0],
starting_difc[1]/0.95,
starting_difc[2]/1.05,
starting_difc[3]*1.01,
starting_difc[4]/0.95,
starting_difc[5]/(0.95*0.98),
starting_difc[6]/(0.95*1.02),
starting_difc[7]*1.01], rtol=0.005)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment