Skip to content

Commit

Permalink
Refs #9343 Checkpointing work.
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Jackson committed Jul 28, 2014
1 parent 062a749 commit e9d65a4
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 12 deletions.
12 changes: 6 additions & 6 deletions Code/Mantid/scripts/Inelastic/IndirectCommon.py
Expand Up @@ -231,7 +231,7 @@ def CheckXrange(x_range,type):
if math.fabs(upper) < 1e-5:
raise ValueError(type + ' - input maximum ('+str(upper)+') is Zero')
if upper < lower:
raise ValueError(type + ' - input max ('+str(upper)+') < min ('+lower+')')
raise ValueError(type + ' - input max ('+str(upper)+') < min ('+str(lower)+')')

def CheckElimits(erange,Xin):
nx = len(Xin)-1
Expand All @@ -245,7 +245,7 @@ def CheckElimits(erange,Xin):
if erange[1] > Xin[nx]:
raise ValueError('Elimits - input emax ( '+str(erange[1])+' ) > data emax ( '+str(Xin[nx])+' )')
if erange[1] < erange[0]:
raise ValueError('Elimits - input emax ( '+str(erange[1])+' ) < emin ( '+erange[0]+' )')
raise ValueError('Elimits - input emax ( '+str(erange[1])+' ) < emin ( '+str(erange[0])+' )')

def plotSpectra(ws, y_axis_title, indicies=[]):
"""
Expand Down Expand Up @@ -304,13 +304,11 @@ def convertToElasticQ(input_ws, output_ws=None):
elif axis.isNumeric():
#check that units are Momentum Transfer
if axis.getUnit().unitID() != 'MomentumTransfer':
logger.error('Input must have axis values of Q')
sys.exit()
raise RuntimeError('Input must have axis values of Q')

CloneWorkspace(input_ws, OutputWorkspace=output_ws)
else:
logger.error('Input workspace must have either spectra or numeric axis.')
sys.exit()
raise RuntimeError('Input workspace must have either spectra or numeric axis.')

def transposeFitParametersTable(params_table, output_table=None):
"""
Expand Down Expand Up @@ -354,6 +352,8 @@ def transposeFitParametersTable(params_table, output_table=None):
row_errors = param_errors[i:i+num_params]
row = [value for pair in zip(row_values, row_errors) for value in pair]
row = [i/num_params] + row
print table_ws.columnCount()
print row
table_ws.addRow(row)

if output_table is None:
Expand Down
166 changes: 160 additions & 6 deletions Code/Mantid/scripts/test/IndirectCommonTests.py
Expand Up @@ -131,6 +131,138 @@ def test_PadArray(self):
actual_result = indirect_common.PadArray(data, 10)
self.assert_lists_match(expected_result, actual_result)

def test_CheckAnalysers(self):
ws1 = self.make_dummy_QENS_workspace(output_name="ws1")
ws2 = self.make_dummy_QENS_workspace(output_name="ws2")

self.assert_does_not_raise(ValueError, indirect_common.CheckAnalysers, ws1, ws2, True)

def test_CheckAnalysers_fails_on_analyser_mismatch(self):
ws1 = self.make_dummy_QENS_workspace(output_name="ws1", analyser='graphite')
ws2 = self.make_dummy_QENS_workspace(output_name="ws2", analyser='fmica')

self.assertRaises(ValueError, indirect_common.CheckAnalysers, ws1, ws2, True)

def test_CheckAnalysers_fails_on_reflection_mismatch(self):
ws1 = self.make_dummy_QENS_workspace(output_name="ws1", reflection='002')
ws2 = self.make_dummy_QENS_workspace(output_name="ws2", reflection='004')

self.assertRaises(ValueError, indirect_common.CheckAnalysers, ws1, ws2, True)

def test_CheckHistZero(self):
ws = self.make_dummy_QENS_workspace()
self.assert_does_not_raise(ValueError, indirect_common.CheckHistZero, ws)

def test_CheckHistSame(self):
ws1 = self.make_dummy_QENS_workspace(output_name='ws1')
ws2 = self.make_dummy_QENS_workspace(output_name='ws2')
self.assert_does_not_raise(ValueError, indirect_common.CheckHistSame, ws1, 'ws1', ws2, 'ws2')

def test_CheckHistSame_fails_on_x_range_mismatch(self):
ws1 = self.make_dummy_QENS_workspace(output_name='ws1')
ws2 = self.make_dummy_QENS_workspace(output_name='ws2')
CropWorkspace(ws2, XMin=10, OutputWorkspace=ws2)

self.assertRaises(ValueError, indirect_common.CheckHistSame, ws1, 'ws1', ws2, 'ws2')

def test_CheckHistSame_fails_on_spectrum_range_mismatch(self):
ws1 = self.make_dummy_QENS_workspace(output_name='ws1')
ws2 = self.make_dummy_QENS_workspace(output_name='ws2')
CropWorkspace(ws2, StartWorkspaceIndex=2, OutputWorkspace=ws2)

self.assertRaises(ValueError, indirect_common.CheckHistSame, ws1, 'ws1', ws2, 'ws2')

def test_CheckXrange(self):
x_range = [1,10]
self.assert_does_not_raise(ValueError, indirect_common.CheckXrange, x_range, 'A Range')

def test_CheckXrange_with_two_ranges(self):
x_range = [1,10,15,20]
self.assert_does_not_raise(ValueError, indirect_common.CheckXrange, x_range, 'A Range')

def test_CheckXrange_lower_close_to_zero(self):
x_range = [-5,0]
self.assertRaises(ValueError, indirect_common.CheckXrange, x_range, 'A Range')

def test_CheckXrange_upper_close_to_zero(self):
x_range = [0,5]
self.assertRaises(ValueError, indirect_common.CheckXrange, x_range, 'A Range')

def test_CheckXrange_invalid_range(self):
x_range = [10,5]
self.assertRaises(ValueError, indirect_common.CheckXrange, x_range, 'A Range')

def test_CheckElimits(self):
energy_range = [-0.5, 0.5]
x_range = np.arange(-0.6, 0.61, 0.01)
self.assert_does_not_raise(ValueError, indirect_common.CheckElimits, energy_range, x_range)

def test_CheckElimits_lower_bound(self):
energy_range = [-0.5, 0.4]
x_range = np.arange(-0.49, 0.5, 0.01)
self.assertRaises(ValueError, indirect_common.CheckElimits, energy_range, x_range)

def test_CheckElimits_upper_bound(self):
energy_range = [-0.5, 0.5]
x_range = np.arange(-0.5, 0.5, 0.01)
self.assertRaises(ValueError, indirect_common.CheckElimits, energy_range, x_range)

def test_CheckElimits_invalid_range(self):
energy_range = [0.5, -0.5]
x_range = np.arange(-0.5, 0.51, 0.01)
self.assertRaises(ValueError, indirect_common.CheckElimits, energy_range, x_range)

def test_convertToElasticQ(self):
ws = self.make_dummy_QENS_workspace()
indirect_common.convertToElasticQ(ws)
self.assert_workspace_units_match_expected('MomentumTransfer', ws)
self.assert_has_numeric_axis(ws)

def test_convertToElasticQ_output_in_different_workspace(self):
ws = self.make_dummy_QENS_workspace()
output_workspace = 'ws2'
indirect_common.convertToElasticQ(ws, output_ws=output_workspace)

#check original wasn't modifed
self.assert_workspace_units_match_expected('Label', ws)
self.assert_has_spectrum_axis(ws)

#check new workspace matches what we expect
self.assert_workspace_units_match_expected('MomentumTransfer', output_workspace)
self.assert_has_numeric_axis(output_workspace)

def test_convertToElasticQ_workspace_already_in_Q(self):
ws = self.make_dummy_QENS_workspace()
e_fixed = indirect_common.getEfixed(ws)
ConvertSpectrumAxis(ws,Target='ElasticQ',EMode='Indirect',EFixed=e_fixed,OutputWorkspace=ws)

indirect_common.convertToElasticQ(ws)

self.assert_workspace_units_match_expected('MomentumTransfer', ws)
self.assert_has_numeric_axis(ws)

def test_convertToElasticQ_with_numeric_axis_not_in_Q(self):
ws = self.make_dummy_QENS_workspace()

#convert spectrum axis to units of Q
e_fixed = indirect_common.getEfixed(ws)
ConvertSpectrumAxis(ws,Target='ElasticQ',EMode='Indirect',EFixed=e_fixed,OutputWorkspace=ws)
#set the units to be something we didn't expect
unit = mtd[ws].getAxis(1).setUnit("Label")
unit.setLabel('Random Units', '')

self.assertRaises(RuntimeError, indirect_common.convertToElasticQ, ws)

def test_transposeFitParametersTable(self):
ws = self.make_dummy_QENS_workspace()
#make a parameter table to transpose
Fit(InputWorkspace=ws, Function="name=LinearBackground, A0=0, A1=0", Output=ws)
params_table = "%s_Parameters" % ws

indirect_common.transposeFitParametersTable(params_table)
self.assertEquals(200, mtd[params_table].rowCount())
self.assertEquals(2, mtd[params_table].columnCount())


#-----------------------------------------------------------
# Test helper functions
Expand All @@ -140,24 +272,46 @@ def assert_lists_match(self, expected, actual):
self.assertTrue(isinstance(expected, list))
np.testing.assert_array_equal(expected, actual, "The results do not match")

def make_dummy_QENS_workspace(self, instrument_name='IRIS', add_logs=True):
def assert_does_not_raise(self, exception_type, func, *args):
""" Check if this function raises the expected exception """
try:
func(*args)
except exception_type:
self.fail("%s should not of raised anything but it did." % func.__name__)

def assert_workspace_units_match_expected(self, expected_unit_ID, ws, axis_number=1):
axis = mtd[ws].getAxis(axis_number)
actual_unit_ID = axis.getUnit().unitID()
self.assertEquals(expected_unit_ID, actual_unit_ID)

def assert_has_spectrum_axis(self, ws, axis_number=1):
axis = mtd[ws].getAxis(axis_number)
self.assertTrue(axis.isSpectra())

def assert_has_numeric_axis(self, ws, axis_number=1):
axis = mtd[ws].getAxis(axis_number)
self.assertTrue(axis.isNumeric())

def make_dummy_QENS_workspace(self, output_name="ws", instrument_name='IRIS',
analyser='graphite', reflection='002', add_logs=True):
""" Make a workspace that looks like QENS data """
ws = CreateSampleWorkspace(OutputWorkspace="ws")
self.load_instrument(ws, instrument_name)
ws = CropWorkspace(ws, StartWorkspaceIndex=3, EndWorkspaceIndex=7)
ws = CreateSampleWorkspace(OutputWorkspace=output_name)
self.load_instrument(ws, instrument_name, analyser, reflection)
ws = CropWorkspace(ws, StartWorkspaceIndex=3, EndWorkspaceIndex=7, OutputWorkspace=output_name)

if add_logs:
AddSampleLog(ws, LogName='run_number', LogType='Number', LogText='00001')

return ws.name()

def load_instrument(self, ws, instrument):
def load_instrument(self, ws, instrument, analyser='graphite', reflection='002'):
"""Load an instrument parameter from the ipf directory"""
LoadInstrument(ws, InstrumentName=instrument)

if config['default.facility'] != 'ILL':
parameter_file_name = '%s_%s_%s_Parameters.xml' % (instrument, analyser, reflection)
ipf = os.path.join(config['instrumentDefinition.directory'],
instrument+'_graphite_002_Parameters.xml')
parameter_file_name)
LoadParameterFile(ws, Filename=ipf)

return ws
Expand Down

0 comments on commit e9d65a4

Please sign in to comment.