class TestDummyHardwareAdapter(object): def setup_method(self): self.hardware = DummyHardwareAdapter( ['alpha', 'delta', 'gamma', 'omega', 'chi', 'phi']) def test__init__(self): assert self.hardware.get_position() == [0.] * 6 assert self.hardware.get_energy() == 12.39842 assert self.hardware.get_wavelength() == 1. assert (self.hardware.get_axes_names() == ('alpha', 'delta', 'gamma', 'omega', 'chi', 'phi')) def test__repr__(self): print self.hardware.__repr__() def testSetGetPosition(self): pass def testSetGetEnergyWavelength(self): pass def testget_position_by_name(self): self.hardware.position = [1., 2., 3., 4., 5., 6.] assert self.hardware.get_position_by_name('gamma') == 3 with pytest.raises(ValueError): self.hardware.get_position_by_name('not an angle name')
class TestDummyHardwareAdapter(unittest.TestCase): def setUp(self): self.hardware = DummyHardwareAdapter( ['alpha', 'delta', 'gamma', 'omega', 'chi', 'phi']) def test__init__(self): self.assertEquals(self.hardware.get_position(), [0.] * 6) self.assertEquals(self.hardware.get_energy(), 12.39842) self.assertEquals(self.hardware.get_wavelength(), 1.) self.assertEquals(self.hardware.get_axes_names(), ('alpha', 'delta', 'gamma', 'omega', 'chi', 'phi')) def test__repr__(self): print self.hardware.__repr__() def testSetGetPosition(self): pass def testSetGetEnergyWavelength(self): pass def testget_position_by_name(self): self.hardware.position = [1., 2., 3., 4., 5., 6.] self.assertEqual(self.hardware.get_position_by_name('gamma'), 3.) self.assertRaises(ValueError, self.hardware.get_position_by_name, 'not an angle name')
class BaseTestDiffractionCalculatorWithData(object): def setSessionAndCalculation(self): raise Exception("Abstract") def setup_method(self): self.geometry = SixCircleGammaOnArmGeometry() self.hardware = DummyHardwareAdapter( ('alpha', 'delta', 'gamma', 'omega', 'chi', 'phi')) settings.hardware = self.hardware settings.geometry = self.geometry settings.ubcalc_persister = UbCalculationNonPersister() from diffcalc.dc import dcvlieg as dc reload(dc) self.dc = dc self.setSessionAndCalculation() prepareRawInput([]) def setDataAndReturnObject(self, sessionScenario, calculationScenario): self.sess = sessionScenario self.calc = calculationScenario return self def test_angles_to_hkl(self): with pytest.raises(DiffcalcException): self.dc.angles_to_hkl((1, 2, 3, 4, 5, 6)) # no energy set settings.hardware.energy = 10 with pytest.raises(DiffcalcException): self.dc.angles_to_hkl((1, 2, 3, 4, 5, 6)) # no ub calculated s = self.sess c = self.calc # setup session info self.dc.newub(s.name) self.dc.setlat(s.name, *s.lattice) r = s.ref1 self.dc.addref([r.h, r.k, r.l], r.pos.totuple(), r.energy, r.tag) r = s.ref2 self.dc.addref([r.h, r.k, r.l], r.pos.totuple(), r.energy, r.tag) self.dc.calcub() # check the ubcalculation is okay before continuing # (useful to check for typos!) mneq_(self.dc.ubcalc.UB, matrix(s.umatrix) * matrix(s.bmatrix), 4, note="wrong UB matrix after calculating U") # Test each hkl/position pair for idx in range(len(c.hklList)): hkl = c.hklList[idx] pos = c.posList[idx] # 1) specifying energy explicitely ((h, k, l), params) = self.dc.angles_to_hkl(pos.totuple(), c.energy) msg = ("wrong hkl calc for TestScenario=%s, AngleTestScenario" "=%s, pos=%s):\n expected hkl=%f %f %f\n returned hkl=" "%f %f %f " % (s.name, c.tag, str(pos), hkl[0], hkl[1], hkl[2], h, k, l)) assert [h, k, l] == pytest.approx(hkl, abs=.001) # self.assert_((abs(h - hkl[0]) < .001) & (abs(k - hkl[1]) < .001) # & (abs(l - hkl[2]) < .001), msg) # 2) specifying energy via hardware settings.hardware.energy = c.energy msg = ("wrong hkl calcualted for TestScenario=%s, " "AngleTestScenario=%s, pos=%s):\n expected hkl=%f %f %f\n" " returned hkl=%f %f %f " % (s.name, c.tag, str(pos), hkl[0], hkl[1], hkl[2], h, k, l)) ((h, k, l), params) = self.dc.angles_to_hkl(pos.totuple()) assert [h, k, l] == pytest.approx(hkl, abs=.001) del params def test_hkl_to_angles(self): s = self.sess c = self.calc ## setup session info self.dc.newub(s.name) self.dc.setlat(s.name, *s.lattice) r = s.ref1 self.dc.addref([r.h, r.k, r.l], r.pos.totuple(), r.energy, r.tag) r = s.ref2 self.dc.addref([r.h, r.k, r.l], r.pos.totuple(), r.energy, r.tag) self.dc.calcub() # check the ubcalculation is okay before continuing # (useful to check for typos !) mneq_(self.dc.ubcalc.UB, matrix(s.umatrix) * matrix(s.bmatrix), 4, note="wrong UB matrix after calculating U") ## setup calculation info self.dc.hklmode(c.modeNumber) # Set fixed parameters if c.alpha != None: self.dc.setpar('alpha', c.alpha) if c.gamma != None: self.dc.setpar('gamma', c.alpha) # Test each hkl/position pair for idx in range(len(c.hklList)): (h, k, l) = c.hklList[idx] expectedangles = \ self.geometry.internal_position_to_physical_angles(c.posList[idx]) (angles, params) = self.dc.hkl_to_angles(h, k, l, c.energy) expectedAnglesStr = ("%f " * len(expectedangles)) % expectedangles anglesString = ("%f " * len(angles)) % angles namesString = (("%s " * len(self.hardware.get_axes_names())) % self.hardware.get_axes_names()) note = ("wrong position calcualted for TestScenario=%s, " "AngleTestScenario=%s, hkl=%f %f %f:\n" " { %s }\n" " expected pos=%s\n returned pos=%s " % (s.name, c.tag, h, k, l, namesString, expectedAnglesStr, anglesString)) mneq_(matrix([list(expectedangles)]), matrix([list(angles)]), 2, note=note) del params def testSimWithHklAndSixc(self): dummyAxes = createDummyAxes( ['alpha', 'delta', 'gamma', 'omega', 'chi', 'phi']) dummySixcScannableGroup = ScannableGroup('sixcgrp', dummyAxes) sixcdevice = DiffractometerScannableGroup( 'SixCircleGammaOnArmGeometry', self.dc, dummySixcScannableGroup) hkldevice = Hkl('hkl', sixcdevice, self.dc) with pytest.raises(TypeError): sim() with pytest.raises(TypeError): sim(hkldevice) with pytest.raises(TypeError): sim(hkldevice, 1, 2, 3) with pytest.raises(TypeError): sim('not a proper scannable', 1) with pytest.raises(TypeError): sim(hkldevice, (1, 2, 'not a number')) with pytest.raises(TypeError): sim(hkldevice, 1) with pytest.raises(ValueError): sim(hkldevice, (1, 2, 3, 4)) s = self.sess c = self.calc # setup session info self.dc.newub(s.name) self.dc.setlat(s.name, *s.lattice) r = s.ref1 self.dc.addref([r.h, r.k, r.l], r.pos.totuple(), r.energy, r.tag) r = s.ref2 self.dc.addref([r.h, r.k, r.l], r.pos.totuple(), r.energy, r.tag) self.dc.calcub() # check the ubcalculation is okay before continuing # (useful to check for typos!) mneq_(self.dc.ubcalc.UB, (matrix(s.umatrix) * (matrix(s.bmatrix))), 4, note="wrong UB matrix after calculating U") self.hardware.energy = c.energy # Test each hkl/position pair for idx in range(len(c.hklList)): hkl = c.hklList[idx] pos = c.posList[idx].totuple() sim(sixcdevice, pos) sim(hkldevice, hkl) def testCheckub(self): ## setup session info s = self.sess self.dc.newub(s.name) self.dc.setlat(s.name, *s.lattice) r = s.ref1 self.dc.addref([r.h, r.k, r.l], r.pos.totuple(), r.energy, r.tag) r = s.ref2 self.dc.addref([r.h, r.k, r.l], r.pos.totuple(), r.energy, r.tag) self.dc.calcub() print "*** checkub ***" print self.dc.checkub() print "***************"
class TestDummyHardwareAdapter(object): def setup_method(self): self.hardware = DummyHardwareAdapter( ['alpha', 'delta', 'gamma', 'omega', 'chi', 'phi']) def test__init__(self): assert self.hardware.get_position() == [0.] * 6 assert self.hardware.get_energy() == 12.39842 assert self.hardware.get_wavelength() == 1. assert (self.hardware.get_axes_names() == ('alpha', 'delta', 'gamma', 'omega', 'chi', 'phi')) def test__repr__(self): print self.hardware.__repr__() def testSetGetPosition(self): pass def testSetGetEnergyWavelength(self): pass def testget_position_by_name(self): self.hardware.position = [1., 2., 3., 4., 5., 6.] assert self.hardware.get_position_by_name('gamma') == 3 with pytest.raises(ValueError): self.hardware.get_position_by_name('not an angle name') def testLowerLimitSetAndGet(self): self.hardware.set_lower_limit('alpha', -1) self.hardware.set_lower_limit('delta', -2) self.hardware.set_lower_limit('gamma', -3) with pytest.raises(ValueError): self.hardware.set_lower_limit('not an angle', 1) self.hardware.set_lower_limit('delta', None) print "Should print WARNING:" self.hardware.set_lower_limit('delta', None) assert self.hardware.get_lower_limit('alpha') == -1 assert self.hardware.get_lower_limit('gamma') == -3 def testUpperLimitSetAndGet(self): self.hardware.set_upper_limit('alpha', 1) self.hardware.set_upper_limit('delta', 2) self.hardware.set_upper_limit('gamma', 3) with pytest.raises(ValueError): self.hardware.set_upper_limit('not an angle', 1) self.hardware.set_upper_limit('delta', None) print "Should print WARNING:" self.hardware.set_upper_limit('delta', None) assert self.hardware.get_upper_limit('alpha') == 1 assert self.hardware.get_upper_limit('gamma') == 3 def testis_position_within_limits(self): self.hardware.set_upper_limit('alpha', 1) self.hardware.set_upper_limit('delta', 2) self.hardware.set_lower_limit('alpha', -1) assert self.hardware.is_position_within_limits([0, 0, 999]) assert self.hardware.is_position_within_limits([1, 2, 999]) assert self.hardware.is_position_within_limits([-1, -999, 999]) assert not self.hardware.is_position_within_limits([1.01, 0, 999]) assert not self.hardware.is_position_within_limits([0, 2.01, 999]) assert not self.hardware.is_position_within_limits([-1.01, 0, 999]) def testIsAxisWithinLimits(self): self.hardware.set_upper_limit('alpha', 1) self.hardware.set_upper_limit('delta', 2) self.hardware.set_lower_limit('gamma', -1) assert self.hardware.is_axis_value_within_limits('alpha', 0) assert self.hardware.is_axis_value_within_limits('delta', 0) assert self.hardware.is_axis_value_within_limits('gamma', 999) assert self.hardware.is_axis_value_within_limits('alpha', 1) assert self.hardware.is_axis_value_within_limits('delta', 2) assert self.hardware.is_axis_value_within_limits('gamma', 999) assert self.hardware.is_axis_value_within_limits('alpha', -1) assert self.hardware.is_axis_value_within_limits('delta', -999) assert not self.hardware.is_axis_value_within_limits('alpha', 1.01) assert not self.hardware.is_axis_value_within_limits('delta', 2.01) assert not self.hardware.is_axis_value_within_limits('alpha', 1.01)
class BaseTestDiffractionCalculatorWithData(object): def setSessionAndCalculation(self): raise Exception("Abstract") def setup_method(self): self.geometry = SixCircleGammaOnArmGeometry() self.hardware = DummyHardwareAdapter( ('alpha', 'delta', 'gamma', 'omega', 'chi', 'phi')) settings.hardware = self.hardware settings.geometry = self.geometry settings.ubcalc_persister = UbCalculationNonPersister() from diffcalc.dc import dcvlieg as dc reload(dc) self.dc = dc self.setSessionAndCalculation() prepareRawInput([]) def setDataAndReturnObject(self, sessionScenario, calculationScenario): self.sess = sessionScenario self.calc = calculationScenario return self def test_angles_to_hkl(self): with pytest.raises(DiffcalcException): self.dc.angles_to_hkl((1, 2, 3, 4, 5, 6)) # no energy set settings.hardware.energy = 10 with pytest.raises(DiffcalcException): self.dc.angles_to_hkl((1, 2, 3, 4, 5, 6)) # no ub calculated s = self.sess c = self.calc # setup session info self.dc.newub(s.name) self.dc.setlat(s.name, *s.lattice) r = s.ref1 self.dc.addref( [r.h, r.k, r.l], r.pos.totuple(), r.energy, r.tag) r = s.ref2 self.dc.addref( [r.h, r.k, r.l], r.pos.totuple(), r.energy, r.tag) self.dc.calcub() # check the ubcalculation is okay before continuing # (useful to check for typos!) mneq_(self.dc.ubcalc.UB, matrix(s.umatrix) * matrix(s.bmatrix), 4, note="wrong UB matrix after calculating U") # Test each hkl/position pair for idx in range(len(c.hklList)): hkl = c.hklList[idx] pos = c.posList[idx] # 1) specifying energy explicitely ((h, k, l), params) = self.dc.angles_to_hkl(pos.totuple(), c.energy) msg = ("wrong hkl calc for TestScenario=%s, AngleTestScenario" "=%s, pos=%s):\n expected hkl=%f %f %f\n returned hkl=" "%f %f %f " % (s.name, c.tag, str(pos), hkl[0], hkl[1], hkl[2], h, k, l) ) assert [h, k, l] == pytest.approx(hkl, abs=.001) # self.assert_((abs(h - hkl[0]) < .001) & (abs(k - hkl[1]) < .001) # & (abs(l - hkl[2]) < .001), msg) # 2) specifying energy via hardware settings.hardware.energy = c.energy msg = ("wrong hkl calcualted for TestScenario=%s, " "AngleTestScenario=%s, pos=%s):\n expected hkl=%f %f %f\n" " returned hkl=%f %f %f " % (s.name, c.tag, str(pos), hkl[0], hkl[1], hkl[2], h, k, l)) ((h, k, l), params) = self.dc.angles_to_hkl(pos.totuple()) assert [h, k, l] == pytest.approx(hkl, abs=.001) del params def test_hkl_to_angles(self): s = self.sess c = self.calc ## setup session info self.dc.newub(s.name) self.dc.setlat(s.name, *s.lattice) r = s.ref1 self.dc.addref( [r.h, r.k, r.l], r.pos.totuple(), r.energy, r.tag) r = s.ref2 self.dc.addref( [r.h, r.k, r.l], r.pos.totuple(), r.energy, r.tag) self.dc.calcub() # check the ubcalculation is okay before continuing # (useful to check for typos !) mneq_(self.dc.ubcalc.UB, matrix(s.umatrix) * matrix(s.bmatrix), 4, note="wrong UB matrix after calculating U") ## setup calculation info self.dc.hklmode(c.modeNumber) # Set fixed parameters if c.alpha != None: self.dc.setpar('alpha', c.alpha) if c.gamma != None: self.dc.setpar('gamma', c.alpha) # Test each hkl/position pair for idx in range(len(c.hklList)): (h, k, l) = c.hklList[idx] expectedangles = \ self.geometry.internal_position_to_physical_angles(c.posList[idx]) (angles, params) = self.dc.hkl_to_angles(h, k, l, c.energy) expectedAnglesStr = ("%f " * len(expectedangles)) % expectedangles anglesString = ("%f " * len(angles)) % angles namesString = (("%s " * len(self.hardware.get_axes_names())) % self.hardware.get_axes_names()) note = ("wrong position calcualted for TestScenario=%s, " "AngleTestScenario=%s, hkl=%f %f %f:\n" " { %s }\n" " expected pos=%s\n returned pos=%s " % (s.name, c.tag, h, k, l, namesString, expectedAnglesStr, anglesString)) mneq_(matrix([list(expectedangles)]), matrix([list(angles)]), 2, note=note) del params def testSimWithHklAndSixc(self): dummyAxes = createDummyAxes( ['alpha', 'delta', 'gamma', 'omega', 'chi', 'phi']) dummySixcScannableGroup = ScannableGroup('sixcgrp', dummyAxes) sixcdevice = DiffractometerScannableGroup( 'SixCircleGammaOnArmGeometry', self.dc, dummySixcScannableGroup) hkldevice = Hkl('hkl', sixcdevice, self.dc) with pytest.raises(TypeError): sim() with pytest.raises(TypeError): sim(hkldevice) with pytest.raises(TypeError): sim(hkldevice, 1, 2, 3) with pytest.raises(TypeError): sim('not a proper scannable', 1) with pytest.raises(TypeError): sim(hkldevice, (1, 2, 'not a number')) with pytest.raises(TypeError): sim(hkldevice, 1) with pytest.raises(ValueError): sim(hkldevice, (1, 2, 3, 4)) s = self.sess c = self.calc # setup session info self.dc.newub(s.name) self.dc.setlat(s.name, *s.lattice) r = s.ref1 self.dc.addref( [r.h, r.k, r.l], r.pos.totuple(), r.energy, r.tag) r = s.ref2 self.dc.addref( [r.h, r.k, r.l], r.pos.totuple(), r.energy, r.tag) self.dc.calcub() # check the ubcalculation is okay before continuing # (useful to check for typos!) mneq_(self.dc.ubcalc.UB, (matrix(s.umatrix) * (matrix(s.bmatrix))), 4, note="wrong UB matrix after calculating U") self.hardware.energy = c.energy # Test each hkl/position pair for idx in range(len(c.hklList)): hkl = c.hklList[idx] pos = c.posList[idx].totuple() sim(sixcdevice, pos) sim(hkldevice, hkl) def testCheckub(self): ## setup session info s = self.sess self.dc.newub(s.name) self.dc.setlat(s.name, *s.lattice) r = s.ref1 self.dc.addref( [r.h, r.k, r.l], r.pos.totuple(), r.energy, r.tag) r = s.ref2 self.dc.addref( [r.h, r.k, r.l], r.pos.totuple(), r.energy, r.tag) self.dc.calcub() print "*** checkub ***" print self.dc.checkub() print "***************"