Commit 533929db authored by Whitfield, Ross's avatar Whitfield, Ross
Browse files

Update workflow to use CombineDiffCal

parent 480e2790
......@@ -180,12 +180,12 @@ the instrument in groups using a combination of :ref:`CrossCorrelate
from Calibration.tofpd.group_calibration import pdcalibration_groups
diffcal = pdcalibration_groups(ws,
groups,
cc_diffcal,
PeakPositions = [1.0, 2.0, 3.0],
PeakFunction='Gaussian',
PeakWindow=0.4)
diffcal, mask = pdcalibration_groups(ws,
groups,
cc_diffcal,
PeakPositions = [1.0, 2.0, 3.0],
PeakFunction='Gaussian',
PeakWindow=0.4)
print("DetID DIFC")
for detid, difc in zip(diffcal.column('detid'), diffcal.column('difc')):
......@@ -260,16 +260,16 @@ The same complete calibration can just be run with just ``group_calibration.do_g
from Calibration.tofpd.group_calibration import do_group_calibration
diffcal = do_group_calibration(ws,
groups,
cc_kwargs={
"DReference": 2.0,
"Xmin": 1.75,
"Xmax": 2.25},
pdcal_kwargs={
"PeakPositions": [1.0, 2.0, 3.0],
"PeakFunction": 'Gaussian',
"PeakWindow": 0.4})
diffcal, mask = do_group_calibration(ws,
groups,
cc_kwargs={
"DReference": 2.0,
"Xmin": 1.75,
"Xmax": 2.25},
pdcal_kwargs={
"PeakPositions": [1.0, 2.0, 3.0],
"PeakFunction": 'Gaussian',
"PeakWindow": 0.4})
print("DetID DIFC")
for detid, difc in zip(diffcal.column('detid'), diffcal.column('difc')):
......@@ -285,6 +285,16 @@ The same complete calibration can just be run with just ``group_calibration.do_g
5 2318.3
6 2269.5
The resulting diffcal can be saved with :ref:`SaveDiffCal
<algm-SaveDiffCal>`.
.. codeblock::
SaveDiffCal(CalibrationWorkspace=diffcal,
MaskWorkspace=mask,
Filename='calibration.h5')
Saving and Loading Calibration
##############################
......
......@@ -3,14 +3,14 @@ import os
import json
import datetime
import numpy as np
from mantid.simpleapi import (ConvertUnits, ExtractSpectra,
CloneWorkspace, Rebin, MaskDetectors,
ExtractUnmaskedSpectra, CrossCorrelate,
GetDetectorOffsets, ConvertDiffCal, mtd,
ApplyDiffCal, DiffractionFocussing,
PDCalibration, Load, LoadMask,
from mantid.simpleapi import (ConvertUnits, ExtractSpectra, Rebin,
MaskDetectors, ExtractUnmaskedSpectra,
CrossCorrelate, GetDetectorOffsets,
ConvertDiffCal, mtd, ApplyDiffCal,
DiffractionFocussing, PDCalibration,
Load, LoadMask, CombineDiffCal,
LoadDiffCal, LoadDetectorsGroupingFile,
SaveDiffCal, DeleteWorkspace, DeleteTableRows)
SaveDiffCal, DeleteWorkspace)
# Diamond peak positions in d-space
DIAMOND = (0.3117,0.3257,0.3499,0.4205,0.4645,
......@@ -126,7 +126,7 @@ def pdcalibration_groups(data_ws,
:param PeakFunction: PeakFunction parameter of PDCalibration, default 'IkedaCarpenterPV'
:param PeakWindow: PeakWindow parameter of PDCalibration, default 0.1
:param PeakWidthPercent: PeakWidthPercent parameter of PDCalibration, default None
:return: DiffCal from CrossCorrelate combined with DiffCal from PDCalibration of grouped workspace
:return: tuple of DiffCal and Mask from CrossCorrelate combined with DiffCal from PDCalibration of grouped workspace
"""
ApplyDiffCal(data_ws, CalibrationWorkspace=cc_diffcal)
......@@ -148,45 +148,15 @@ def pdcalibration_groups(data_ws,
OutputCalibrationTable=f'{output_basename}_pd_diffcal',
DiagnosticWorkspaces=f'{output_basename}_pd_diag')
# Everything below will all be replaced by be the new CombineDiffCal algorithm
cc_and_pd_diffcal = CloneWorkspace(f'{output_basename}_pd_diffcal', OutputWorkspace=f'{output_basename}_cc_pd_diffcal')
# remove PD masked detectors from CC diffcal
mask_ws = mtd[f'{output_basename}_pd_diffcal_mask']
rows_to_remove = []
detid_list = cc_diffcal.column('detid')
for n in range(mask_ws.getNumberHistograms()):
if mask_ws.readY(n)[0] == 1:
try:
rows_to_remove.append(detid_list.index(mask_ws.getSpectrum(n).getDetectorIDs()[0]))
except ValueError:
pass
if rows_to_remove:
DeleteTableRows(cc_diffcal, Rows=rows_to_remove)
cc_det_to_difc = dict(zip(cc_diffcal.column('detid'), cc_diffcal.column('difc')))
grouped = mtd['_tmp_data_aligned']
specInfo = grouped.spectrumInfo()
grouped_det_to_difc = {}
for detid in cc_and_pd_diffcal.column('detid'):
ind = list(grouped.getIndicesFromDetectorIDs([detid]))
if ind:
grouped_det_to_difc[detid] = specInfo.difcUncalibrated(ind[0])
for n, detid in enumerate(cc_and_pd_diffcal.column('detid')):
if detid in cc_det_to_difc and detid in grouped_det_to_difc:
cc_and_pd_diffcal.setCell(n, 1,
cc_and_pd_diffcal.cell(n, 1)
* cc_det_to_difc[detid]
/ grouped_det_to_difc[detid])
CombineDiffCal(PixelCalibration=cc_diffcal,
GroupedCalibration=f'{output_basename}_pd_diffcal',
CalibrationWorkspace='_tmp_data_aligned',
MaskWorkspace=f'{output_basename}_pd_diffcal_mask',
OutputWorkspace=f'{output_basename}_cc_pd_diffcal')
DeleteWorkspace('_tmp_data_aligned')
return mtd[f'{output_basename}_cc_pd_diffcal']
return mtd[f'{output_basename}_cc_pd_diffcal'], mtd[f'{output_basename}_pd_diffcal_mask']
def do_group_calibration(data_ws,
......@@ -213,14 +183,14 @@ def do_group_calibration(data_ws,
previous_calibration,
**cc_kwargs)
diffcal = pdcalibration_groups(data_ws,
group_ws,
cc_diffcal,
output_basename,
previous_calibration,
**pdcal_kwargs)
diffcal, mask = pdcalibration_groups(data_ws,
group_ws,
cc_diffcal,
output_basename,
previous_calibration,
**pdcal_kwargs)
return diffcal
return diffcal, mask
def process_json(json_filename):
......@@ -286,13 +256,15 @@ def process_json(json_filename):
MakeGroupingWorkspace=False,
MakeMaskWorkspace=False)
diffcal = do_group_calibration(ws,
groups,
previous_calibration,
cc_kwargs=cc_kwargs,
pdcal_kwargs=pdcal_kwargs)
diffcal, mask = do_group_calibration(ws,
groups,
previous_calibration,
cc_kwargs=cc_kwargs,
pdcal_kwargs=pdcal_kwargs)
SaveDiffCal(diffcal, calfilename)
SaveDiffCal(CalibrationWorkspace=diffcal,
MaskWorkspace=mask,
Filename=calfilename)
if __name__ == '__main__':
......
......@@ -10,7 +10,7 @@ from Calibration.tofpd import group_calibration
from mantid.simpleapi import (CreateSampleWorkspace, MaskDetectors,
MoveInstrumentComponent, ScaleX, Rebin, ConvertUnits,
CreateGroupingWorkspace, CreateEmptyTableWorkspace)
from numpy.testing import assert_allclose
from numpy.testing import assert_allclose, assert_equal
def create_test_ws_and_group():
......@@ -62,13 +62,13 @@ class TestGroupCalibration(unittest.TestCase):
starting_difc[5]/0.98,
starting_difc[6]/1.02], rtol=0.005)
diffcal = group_calibration.pdcalibration_groups(ws,
groups,
cc_diffcal,
output_workspace_basename,
PeakPositions = [1.0, 2.0, 4.0],
PeakFunction='Gaussian',
PeakWindow=0.4)
diffcal, mask = group_calibration.pdcalibration_groups(ws,
groups,
cc_diffcal,
output_workspace_basename,
PeakPositions = [1.0, 2.0, 4.0],
PeakFunction='Gaussian',
PeakWindow=0.4)
assert_allclose(diffcal.column('difc'),
[starting_difc[0],
......@@ -80,6 +80,15 @@ class TestGroupCalibration(unittest.TestCase):
starting_difc[6]/(0.95*1.02),
0], rtol=0.005)
assert_equal(mask.extractY(), [[0],
[0],
[0],
[1],
[0],
[0],
[0],
[1]])
def test_from_prev_cal(self):
ws, groups = create_test_ws_and_group()
......@@ -122,14 +131,14 @@ class TestGroupCalibration(unittest.TestCase):
starting_difc[6]*1.01/1.02,
starting_difc[7]*1.01], rtol=0.005)
diffcal = group_calibration.pdcalibration_groups(ws,
groups,
cc_diffcal,
output_workspace_basename,
previous_calibration=previous_diffcal,
PeakPositions = [1.0, 2.0, 4.0],
PeakFunction='Gaussian',
PeakWindow=0.4)
diffcal, mask = group_calibration.pdcalibration_groups(ws,
groups,
cc_diffcal,
output_workspace_basename,
previous_calibration=previous_diffcal,
PeakPositions = [1.0, 2.0, 4.0],
PeakFunction='Gaussian',
PeakWindow=0.4)
assert_allclose(diffcal.column('difc'),
[starting_difc[0],
......@@ -141,6 +150,15 @@ class TestGroupCalibration(unittest.TestCase):
starting_difc[6]/(0.95*1.02),
starting_difc[7]*1.01], rtol=0.005)
assert_equal(mask.extractY(), [[0],
[0],
[0],
[1],
[0],
[0],
[0],
[1]])
def test_di_group_calibration(self):
ws, groups = create_test_ws_and_group()
......@@ -162,21 +180,21 @@ class TestGroupCalibration(unittest.TestCase):
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_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})
diffcal, mask = group_calibration.do_group_calibration(ws,
groups,
previous_calibration=previous_diffcal,
output_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],
......@@ -188,6 +206,15 @@ class TestGroupCalibration(unittest.TestCase):
starting_difc[6]/(0.95*1.02),
starting_difc[7]*1.01], rtol=0.005)
assert_equal(mask.extractY(), [[0],
[0],
[0],
[1],
[0],
[0],
[0],
[1]])
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