Skip to content

Commit

Permalink
Refs #12678 Fix flake8 warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
samueljackson92 committed Jan 10, 2017
1 parent 630f50c commit ad95516
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 54 deletions.
48 changes: 24 additions & 24 deletions Testing/SystemTests/tests/analysis/ISIS_WISHCalibration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
System test for WISH calibration
This makes use of the tube calibration python package. This also includes
This makes use of the tube calibration python package. This also includes
additional processing to correct "bad" peaks fits in some tubes.
Note that this is a test for the newer configuration of WISH. Previously this
Expand All @@ -11,15 +11,14 @@
5 Gaussian peaks for calibration.
"""
from mantid.simpleapi import *
from mantid.simpleapi import (Load, ApplyCalibration)
from tube_calib_fit_params import TubeCalibFitParams
from tube_spec import TubeSpec
from ideal_tube import IdealTube
import tube

import stresstesting
import numpy as np
import os


class WISHCalibrationTest(stresstesting.MantidStressTest):
Expand All @@ -34,39 +33,40 @@ def cleanup(self):
pass

def runTest(self):
ws = Load("WISH30541_integrated.nxs", OutputWorkspace="30541")
workspace = Load("WISH30541_integrated.nxs", OutputWorkspace="30541")

# define the positions of each of calibration bands on WISH
lower_tube = np.array([-274.81703361, -131.75052481, 0.,
131.75052481, 274.81703361])
upper_tube = lower_tube # assume upper and lower tubes are the same
funcForm = len(lower_tube)*[1] # 5 gaussian peaks
margin = 20
tube_positions = np.array([-274.81703361, -131.75052481, 0.,
131.75052481, 274.81703361])
func_form = len(tube_positions)*[1] # 5 gaussian peaks
margin = 15

# start position of each of the peaks to fit
fitPar = TubeCalibFitParams( [59, 161, 258, 353, 448])
fitPar.setAutomatic(True)
fit_params = TubeCalibFitParams([59, 161, 258, 353, 448])
fit_params.setAutomatic(True)

instrument = ws.getInstrument()
spec = TubeSpec(ws)
instrument = workspace.getInstrument()
spec = TubeSpec(workspace)
spec.setTubeSpecByString(instrument.getFullName())

idealTube = IdealTube()
idealTube.setArray(lower_tube)
ideal_tube = IdealTube()
ideal_tube.setArray(tube_positions)

# First calibrate all of the detectors
calibrationTable, peaks = tube.calibrate(ws, spec, lower_tube,
funcForm, margin=15, outputPeak=True, fitPar=fitPar)
calibration_table, peaks = \
tube.calibrate(workspace, spec, tube_positions, func_form,
margin=margin, outputPeak=True,
fitPar=fit_params)

ApplyCalibration(ws, calibrationTable)
ApplyCalibration(workspace, calibration_table)
# should have # detectors to update
self.assertEqual(calibrationTable.rowCount(), 778240)
self.assertEqual(calibration_table.rowCount(), 778240)

# now correct badly aligned tubes
corrected_calibration_table = tube.correctMisalignedTubes(ws,
calibrationTable, peaks, spec, idealTube, fitPar)
ApplyCalibration(ws, corrected_calibration_table)
corrected_calibration_table = \
tube.correctMisalignedTubes(workspace, calibration_table, peaks, spec,
ideal_tube, fit_params)

ApplyCalibration(workspace, corrected_calibration_table)
# should have 12288 detectors to correct after refitting
self.assertEqual(corrected_calibration_table.rowCount(), 12288)


71 changes: 41 additions & 30 deletions scripts/Calibration/tube.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from mantid.kernel import V3D
from mantid.api import (MatrixWorkspace, ITableWorkspace)
from mantid.simpleapi import (mtd, CreateEmptyTableWorkspace, DeleteWorkspace,
from mantid.simpleapi import (mtd, CreateEmptyTableWorkspace, DeleteWorkspace,
FindPeaks, config)
from tube_spec import TubeSpec
from ideal_tube import IdealTube
Expand Down Expand Up @@ -755,18 +755,19 @@ def readCalibrationFile(table_name, in_path):

calibTable.addRow(nextRow)


def findBadPeakFits(peaksTable, threshold=10):
""" Find peaks whose fit values fall outside of a given tolerance
of the mean peak centers across all tubes.
Tubes are defined as have a bad fit if the absolute difference
between the fitted peak centers for a specific tube and the
between the fitted peak centers for a specific tube and the
mean of the fitted peak centers for all tubes differ more than
the threshold parameter.
@param peakTable: the table containing fitted peak centers
@param threshold: the tolerance on the difference from the mean value
@return A list of expected peak positions and a list of indicies of tubes
@return A list of expected peak positions and a list of indicies of tubes
to correct
"""
n = len(peaksTable)
Expand All @@ -776,71 +777,81 @@ def findBadPeakFits(peaksTable, threshold=10):
for i, row in enumerate(peaksTable):
data_row = [row[name] for name in column_names]
data[i,:] = data_row

# data now has all the peaks positions for each tube
# the mean value is the expected value for the peak position for each tube
expected_peak_pos = numpy.mean(data,axis=0)

#calculate how far from the expected position each peak position is
distance_from_expected = numpy.abs(data - expected_peak_pos)
check = numpy.where(distance_from_expected > threshold)[0]
problematic_tubes = list(set(check))
return expected_peak_pos, problematic_tubes


def cleanUpFit():
"""Clean up workspaces created by calibration fitting """
for ws_name in ('TubePlot', 'RefittedPeaks', 'PolyFittingWorkspace',
'QF_NormalisedCovarianceMatrix',
'QF_Parameters', 'QF_Workspace'):
'QF_NormalisedCovarianceMatrix', 'QF_Parameters',
'QF_Workspace'):
try:
DeleteWorkspace(ws_name)
except:
pass

def correctMisalignedTubes(ws, calibrationTable, peaksTable, spec, idealTube, fitPar, threshold=10):
""" Correct misaligned tubes due to poor fitting results

def correctMisalignedTubes(ws, calibrationTable, peaksTable, spec, idealTube,
fitPar, threshold=10):
""" Correct misaligned tubes due to poor fitting results
during the first round of calibration.
Misaligned tubes are first identified according to a tolerance
applied to the absolute difference between the fitted tube
applied to the absolute difference between the fitted tube
positions and the mean across all tubes.
The FindPeaks algorithm is then used to find a better fit
with the ideal tube positions as starting parameters
The FindPeaks algorithm is then used to find a better fit
with the ideal tube positions as starting parameters
for the peak centers.
From the refitted peaks the positions of the detectors in the
tube are recalculated.
@param ws: the workspace to get the tube geometry from
@param calibrationTable: the calibration table ouput from running calibration
@param peaksTable: the table containing the fitted peak centers from calibration
@param calibrationTable: the calibration table ouput from running
calibration
@param peaksTable: the table containing the fitted peak centers from
calibration
@param spec: the tube spec for the instrument
@param idealTube: the ideal tube for the instrument
@param fitPar: the fitting parameters for calibration
@param threshold: tolerance defining is a peak is outside of the acceptable range
@param threshold: tolerance defining is a peak is outside of the acceptable
range
@return table of corrected detector positions
"""
table_name = calibrationTable.name() + 'Corrected'
corrections_table = CreateEmptyTableWorkspace(OutputWorkspace=table_name)
corrections_table.addColumn('int', "Detector ID")
corrections_table.addColumn('V3D', "Detector Position")

mean_peaks, bad_tubes = findBadPeakFits(peaksTable, threshold)

for index in bad_tubes:
print "Refitting tube %s" % spec.getTubeName(index)
tube_dets, _ = spec.getTube(index)
actualTube = getPoints(ws, idealTube.getFunctionalForms(), fitPar, tube_dets)
getPoints(ws, idealTube.getFunctionalForms(), fitPar, tube_dets)
tube_ws = mtd['TubePlot']
fit_ws = FindPeaks(InputWorkspace=tube_ws, WorkspaceIndex=0,
PeakPositions=fitPar.getPeaks(), PeaksList='RefittedPeaks')
fit_ws = FindPeaks(InputWorkspace=tube_ws, WorkspaceIndex=0,
PeakPositions=fitPar.getPeaks(),
PeaksList='RefittedPeaks')
centers = [row['centre'] for row in fit_ws]
detIDList, detPosList = getCalibratedPixelPositions(ws, centers, idealTube.getArray(), tube_dets)

detIDList, detPosList = \
getCalibratedPixelPositions(ws, centers, idealTube.getArray(),
tube_dets)

for id, pos in zip(detIDList, detPosList):
corrections_table.addRow({'Detector ID': id, 'Detector Position': V3D(*pos)})

corrections_table.addRow({'Detector ID': id,
'Detector Position': V3D(*pos)})

cleanUpFit()

return corrections_table

0 comments on commit ad95516

Please sign in to comment.