Startup
This commit is contained in:
174
script/__Lib/diffcalc_old/test/diffcalc/ub/test_calculation.py
Normal file
174
script/__Lib/diffcalc_old/test/diffcalc/ub/test_calculation.py
Normal file
@@ -0,0 +1,174 @@
|
||||
###
|
||||
# Copyright 2008-2011 Diamond Light Source Ltd.
|
||||
# This file is part of Diffcalc.
|
||||
#
|
||||
# Diffcalc is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Diffcalc is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Diffcalc. If not, see <http://www.gnu.org/licenses/>.
|
||||
###
|
||||
|
||||
from diffcalc.hkl.you.calc import YouUbCalcStrategy
|
||||
from diffcalc.hkl.you.geometry import SixCircle, YouPosition
|
||||
from diffcalc.ub.calc import UBCalculation
|
||||
from diffcalc.ub.persistence import UBCalculationJSONPersister
|
||||
from math import pi
|
||||
from mock import Mock
|
||||
from nose.tools import eq_
|
||||
from test.tools import matrixeq_
|
||||
import tempfile
|
||||
import datetime
|
||||
|
||||
try:
|
||||
from numpy import matrix
|
||||
except ImportError:
|
||||
from numjy import matrix
|
||||
|
||||
|
||||
|
||||
def posFromI16sEuler(phi, chi, eta, mu, delta, gamma):
|
||||
return YouPosition(mu, delta, gamma, eta, chi, phi)
|
||||
|
||||
UB1 = matrix(
|
||||
((0.9996954135095477, -0.01745240643728364, -0.017449748351250637),
|
||||
(0.01744974835125045, 0.9998476951563913, -0.0003045864904520898),
|
||||
(0.017452406437283505, -1.1135499981271473e-16, 0.9998476951563912))
|
||||
) * (2 * pi)
|
||||
|
||||
EN1 = 12.39842
|
||||
REF1a = posFromI16sEuler(1, 1, 30, 0, 60, 0)
|
||||
REF1b = posFromI16sEuler(1, 91, 30, 0, 60, 0)
|
||||
|
||||
|
||||
class TestUBCalculationWithYouStrategy():
|
||||
"""Testing the math only here.
|
||||
"""
|
||||
|
||||
def setup_method(self):
|
||||
|
||||
self.tempdir = tempfile.mkdtemp()
|
||||
|
||||
geometry = SixCircle() # pass through
|
||||
hardware = Mock()
|
||||
names = 'm', 'd', 'n', 'e', 'c', 'p'
|
||||
hardware.get_axes_names.return_value = names
|
||||
self.tmpdir = tempfile.mkdtemp()
|
||||
print self.tmpdir
|
||||
self.ubcalc = UBCalculation(hardware,
|
||||
geometry,
|
||||
UBCalculationJSONPersister(self.tmpdir),
|
||||
YouUbCalcStrategy())
|
||||
|
||||
def testAgainstI16Results(self):
|
||||
self.ubcalc.start_new('cubcalc')
|
||||
self.ubcalc.set_lattice('latt', 1, 1, 1, 90, 90, 90)
|
||||
self.ubcalc.add_reflection(1, 0, 0, REF1a, EN1, '100', None)
|
||||
self.ubcalc.add_reflection(0, 0, 1, REF1b, EN1, '001', None)
|
||||
matrixeq_(self.ubcalc.UB, UB1)
|
||||
|
||||
def test_save_and_restore_empty_ubcalc_with_one_already_started(self):
|
||||
NAME = 'test_save_and_restore_empty_ubcalc_with_one_already_started'
|
||||
self.ubcalc.start_new(NAME)
|
||||
self.ubcalc.start_new(NAME)
|
||||
|
||||
|
||||
def test_save_and_restore_empty_ubcalc(self):
|
||||
NAME = 'test_save_and_restore_empty_ubcalc'
|
||||
self.ubcalc.start_new(NAME)
|
||||
|
||||
self.ubcalc.start_new(NAME + '2')
|
||||
self.ubcalc.load(NAME)
|
||||
|
||||
eq_(self.ubcalc.name, NAME)
|
||||
|
||||
def test_save_and_restore_ubcalc_with_lattice(self):
|
||||
NAME = 'test_save_and_restore_ubcalc_with_lattice'
|
||||
self.ubcalc.start_new(NAME)
|
||||
self.ubcalc.set_lattice('latt', 1, 1, 1, 90, 90, 90)
|
||||
|
||||
self.ubcalc.start_new(NAME + '2')
|
||||
self.ubcalc.load(NAME)
|
||||
|
||||
eq_(self.ubcalc._state.crystal.getLattice(), ('latt', 1, 1, 1, 90, 90, 90))
|
||||
|
||||
def test_save_and_restore_ubcalc_with_reflections(self):
|
||||
NAME = 'test_save_and_restore_ubcalc_with_reflections'
|
||||
self.ubcalc.start_new(NAME)
|
||||
now = datetime.datetime.now()
|
||||
self.ubcalc.add_reflection(1, 0, 0, REF1a, EN1, '100', now)
|
||||
self.ubcalc.add_reflection(0, 0, 1, REF1b, EN1, '001', now)
|
||||
self.ubcalc.add_reflection(0, 0, 1.5, REF1b, EN1, '001_5', now)
|
||||
ref1 = self.ubcalc.get_reflection(1)
|
||||
ref2 = self.ubcalc.get_reflection(2)
|
||||
ref3 = self.ubcalc.get_reflection(3)
|
||||
eq_(self.ubcalc.get_reflection(1), ref1)
|
||||
eq_(self.ubcalc.get_reflection(2), ref2)
|
||||
eq_(self.ubcalc.get_reflection(3), ref3)
|
||||
|
||||
self.ubcalc.start_new(NAME + '2')
|
||||
self.ubcalc.load(NAME)
|
||||
|
||||
eq_(self.ubcalc.get_reflection(1), ref1)
|
||||
eq_(self.ubcalc.get_reflection(2), ref2)
|
||||
eq_(self.ubcalc.get_reflection(3), ref3)
|
||||
|
||||
def test_save_and_restore_ubcalc_with_UB_from_two_ref(self):
|
||||
NAME = 'test_save_and_restore_ubcalc_with_UB_from_two_ref'
|
||||
self.ubcalc.start_new(NAME)
|
||||
self.ubcalc.set_lattice('latt', 1, 1, 1, 90, 90, 90)
|
||||
self.ubcalc.add_reflection(1, 0, 0, REF1a, EN1, '100', None)
|
||||
self.ubcalc.add_reflection(0, 0, 1, REF1b, EN1, '001', None)
|
||||
matrixeq_(self.ubcalc.UB, UB1)
|
||||
|
||||
self.ubcalc.start_new(NAME + '2')
|
||||
self.ubcalc.load(NAME)
|
||||
|
||||
matrixeq_(self.ubcalc.UB, UB1)
|
||||
|
||||
def test_save_and_restore_ubcalc_with_UB_from_one_ref(self):
|
||||
NAME = 'test_save_and_restore_ubcalc_with_UB_from_one_ref'
|
||||
self.ubcalc.start_new(NAME)
|
||||
self.ubcalc.set_lattice('latt', 1, 1, 1, 90, 90, 90)
|
||||
self.ubcalc.add_reflection(1, 0, 0, REF1a, EN1, '100', None)
|
||||
self.ubcalc.calculate_UB_from_primary_only()
|
||||
matrixeq_(self.ubcalc.UB, UB1, places=2)
|
||||
|
||||
self.ubcalc.start_new(NAME + '2')
|
||||
self.ubcalc.load(NAME)
|
||||
|
||||
matrixeq_(self.ubcalc.UB, UB1, places=2)
|
||||
|
||||
def test_save_and_restore_ubcalc_with_manual_ub(self):
|
||||
NAME = 'test_save_and_restore_ubcalc_with_manual_ub'
|
||||
UB = matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
|
||||
self.ubcalc.start_new(NAME)
|
||||
self.ubcalc.set_UB_manually(UB)
|
||||
matrixeq_(self.ubcalc.UB, UB)
|
||||
|
||||
self.ubcalc.start_new(NAME + '2')
|
||||
self.ubcalc.load(NAME)
|
||||
|
||||
matrixeq_(self.ubcalc.UB, UB)
|
||||
|
||||
def test_save_and_restore_ubcalc_with_manual_u(self):
|
||||
NAME = 'test_save_and_restore_ubcalc_with_manual_u'
|
||||
U = matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
|
||||
self.ubcalc.start_new(NAME)
|
||||
self.ubcalc.set_lattice('latt', 1, 1, 1, 90, 90, 90)
|
||||
self.ubcalc.set_U_manually(U)
|
||||
matrixeq_(self.ubcalc.UB, U * 2 * pi)
|
||||
|
||||
self.ubcalc.start_new(NAME + '2')
|
||||
self.ubcalc.load(NAME)
|
||||
|
||||
matrixeq_(self.ubcalc.UB, U * 2 * pi)
|
||||
|
||||
|
||||
@@ -0,0 +1,380 @@
|
||||
###
|
||||
# Copyright 2008-2011 Diamond Light Source Ltd.
|
||||
# This file is part of Diffcalc.
|
||||
#
|
||||
# Diffcalc is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Diffcalc is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Diffcalc. If not, see <http://www.gnu.org/licenses/>.
|
||||
###
|
||||
|
||||
import unittest
|
||||
from datetime import datetime
|
||||
from math import cos, sin, pi
|
||||
|
||||
from mock import Mock
|
||||
from nose.tools import raises
|
||||
try:
|
||||
from numpy import matrix
|
||||
except ImportError:
|
||||
from numjy import matrix
|
||||
|
||||
from diffcalc.hkl.vlieg.geometry import SixCircleGammaOnArmGeometry
|
||||
from diffcalc.hkl.vlieg.geometry import VliegPosition as Pos
|
||||
from test.tools import matrixeq_, mneq_
|
||||
from diffcalc.ub.calc import UBCalculation
|
||||
from diffcalc.ub.persistence import UbCalculationNonPersister
|
||||
from diffcalc.util import DiffcalcException
|
||||
from diffcalc.hkl.vlieg.calc import VliegUbCalcStrategy
|
||||
from test.diffcalc import scenarios
|
||||
|
||||
I = matrix('1 0 0; 0 1 0; 0 0 1')
|
||||
TORAD = pi / 180
|
||||
|
||||
|
||||
|
||||
class TestUBCalculationWithSixCircleGammaOnArm(object):
|
||||
|
||||
def setup_method(self):
|
||||
self.geometry = SixCircleGammaOnArmGeometry()
|
||||
mock_hardware = Mock()
|
||||
mock_hardware.get_axes_names.return_value = ('a', 'd', 'g', 'o', 'c', 'p')
|
||||
self.ubcalc = UBCalculation(
|
||||
mock_hardware, self.geometry, UbCalculationNonPersister(),
|
||||
VliegUbCalcStrategy())
|
||||
self.time = datetime.now()
|
||||
|
||||
### State ###
|
||||
|
||||
def testNewCalculation(self):
|
||||
self.ubcalc.start_new('testcalc')
|
||||
assert self.ubcalc.name, 'testcalc' == "Name not set by newCalcualtion"
|
||||
|
||||
@raises(DiffcalcException)
|
||||
def testNewCalculationHasNoU(self):
|
||||
self.ubcalc.start_new('testcalc')
|
||||
print self.ubcalc.U
|
||||
|
||||
@raises(DiffcalcException)
|
||||
def testNewCalculationHasNoUB(self):
|
||||
self.ubcalc.start_new('testcalc')
|
||||
print self.ubcalc.UB
|
||||
|
||||
### Lattice ###
|
||||
|
||||
def testSetLattice(self):
|
||||
# Not much to test, just make sure no exceptions
|
||||
self.ubcalc.start_new('testcalc')
|
||||
self.ubcalc.set_lattice('testlattice', 4.0004, 4.0004, 2.27, 90, 90, 90)
|
||||
|
||||
### Calculations ###
|
||||
|
||||
def testset_U_manually(self):
|
||||
|
||||
# Test the calculations with U=I
|
||||
U = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
|
||||
for sess in scenarios.sessions():
|
||||
self.setup_method()
|
||||
self.ubcalc.start_new('testcalc')
|
||||
self.ubcalc.set_lattice(sess.name, *sess.lattice)
|
||||
self.ubcalc.set_U_manually(U)
|
||||
# Check the U matrix
|
||||
mneq_(self.ubcalc.U, matrix(U), 4,
|
||||
note="wrong U after manually setting U")
|
||||
|
||||
# Check the UB matrix
|
||||
if sess.bmatrix is None:
|
||||
continue
|
||||
print "U: ", U
|
||||
print "actual ub: ", self.ubcalc.UB.tolist()
|
||||
print " desired b: ", sess.bmatrix
|
||||
mneq_(self.ubcalc.UB, matrix(sess.bmatrix), 4,
|
||||
note="wrong UB after manually setting U")
|
||||
|
||||
@raises(DiffcalcException)
|
||||
def testGetUMatrix(self):
|
||||
self.ubcalc.start_new('testcalc')
|
||||
print self.ubcalc.U
|
||||
|
||||
@raises(DiffcalcException)
|
||||
def testGetUBMatrix(self):
|
||||
self.ubcalc.start_new('testcalc')
|
||||
print self.ubcalc.UB
|
||||
|
||||
def testCalculateU(self):
|
||||
|
||||
for sess in scenarios.sessions():
|
||||
self.setup_method()
|
||||
self.ubcalc.start_new('testcalc')
|
||||
# Skip this test case unless it contains a umatrix
|
||||
if sess.umatrix is None:
|
||||
continue
|
||||
|
||||
self.ubcalc.set_lattice(sess.name, *sess.lattice)
|
||||
ref1 = sess.ref1
|
||||
ref2 = sess.ref2
|
||||
t = sess.time
|
||||
self.ubcalc.add_reflection(
|
||||
ref1.h, ref1.k, ref1.l, ref1.pos, ref1.energy, ref1.tag, t)
|
||||
self.ubcalc.add_reflection(
|
||||
ref2.h, ref2.k, ref2.l, ref2.pos, ref2.energy, ref2.tag, t)
|
||||
self.ubcalc.calculate_UB()
|
||||
returned = self.ubcalc.U.tolist()
|
||||
print "*Required:"
|
||||
print sess.umatrix
|
||||
print "*Returned:"
|
||||
print returned
|
||||
mneq_(self.ubcalc.U, matrix(sess.umatrix), 4,
|
||||
note="wrong U calulated for sess.name=" + sess.name)
|
||||
|
||||
def test__str__(self):
|
||||
sess = scenarios.sessions()[0]
|
||||
print "***"
|
||||
print self.ubcalc.__str__()
|
||||
|
||||
print "***"
|
||||
self.ubcalc.start_new('test')
|
||||
print self.ubcalc.__str__()
|
||||
|
||||
print "***"
|
||||
self.ubcalc.set_lattice(sess.name, *sess.lattice)
|
||||
print self.ubcalc.__str__()
|
||||
|
||||
print "***"
|
||||
ref1 = sess.ref1
|
||||
ref2 = sess.ref2
|
||||
t = sess.time
|
||||
self.ubcalc.add_reflection(
|
||||
ref1.h, ref1.k, ref1.l, ref1.pos, ref1.energy, ref1.tag, t)
|
||||
self.ubcalc.add_reflection(
|
||||
ref2.h, ref2.k, ref2.l, ref2.pos, ref2.energy, ref2.tag, t)
|
||||
print self.ubcalc.__str__()
|
||||
|
||||
print "***"
|
||||
self.ubcalc.calculate_UB()
|
||||
print self.ubcalc.__str__()
|
||||
|
||||
|
||||
def x_rotation(mu_or_alpha):
|
||||
mu_or_alpha *= TORAD
|
||||
return matrix(((1, 0, 0),
|
||||
(0, cos(mu_or_alpha), -sin(mu_or_alpha)),
|
||||
(0, sin(mu_or_alpha), cos(mu_or_alpha))))
|
||||
|
||||
|
||||
def y_rotation(chi):
|
||||
chi *= TORAD
|
||||
return matrix(((cos(chi), 0, sin(chi)),
|
||||
(0, 1, 0),
|
||||
(-sin(chi), 0, cos(chi))))
|
||||
|
||||
|
||||
def z_rotation(th):
|
||||
eta = -th * TORAD
|
||||
return matrix(((cos(eta), sin(eta), 0),
|
||||
(-sin(eta), cos(eta), 0),
|
||||
(0, 0, 1)))
|
||||
|
||||
CUBIC_EN = 12.39842
|
||||
CUBIC = (1, 1, 1, 90, 90, 90)
|
||||
ROT = 29
|
||||
|
||||
|
||||
class TestUBCalcWithCubic(object):
|
||||
|
||||
def setup_method(self):
|
||||
print "TestUBCalcWithCubic.setup_method"
|
||||
hardware = Mock()
|
||||
hardware.get_axes_names.return_value = \
|
||||
('a', 'd', 'g', 'o', 'c', 'p')
|
||||
self.ubcalc = UBCalculation(hardware,
|
||||
SixCircleGammaOnArmGeometry(),
|
||||
UbCalculationNonPersister(),
|
||||
VliegUbCalcStrategy())
|
||||
self.ubcalc.start_new('xtalubcalc')
|
||||
self.ubcalc.set_lattice("xtal", *CUBIC)
|
||||
self.energy = CUBIC_EN
|
||||
|
||||
def addref(self, hklref):
|
||||
hkl, position = hklref
|
||||
now = datetime.now()
|
||||
self.ubcalc.add_reflection(
|
||||
hkl[0], hkl[1], hkl[2], position, self.energy, "ref", now)
|
||||
|
||||
|
||||
class TestUBCalcWithCubicTwoRef(TestUBCalcWithCubic):
|
||||
|
||||
def check(self, testname, hklref1, hklref2, expectedUMatrix):
|
||||
self.addref(hklref1)
|
||||
self.addref(hklref2)
|
||||
matrixeq_(expectedUMatrix, self.ubcalc.U)
|
||||
|
||||
def test_with_squarely_mounted(self):
|
||||
href = ((1, 0, 0),
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30, chi=0, phi=0))
|
||||
lref = ((0, 0, 1),
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30, chi=90, phi=0))
|
||||
pairs = (("hl", href, lref, I),
|
||||
("lh", lref, href, I))
|
||||
for testname, ref1, ref2, u in pairs:
|
||||
yield self.check, testname, ref1, ref2, u
|
||||
|
||||
def test_with_x_mismount(self):
|
||||
U = x_rotation(ROT)
|
||||
href = ((1, 0, 0),
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30, chi=0, phi=0))
|
||||
kref = ((0, 1, 0),
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30 - ROT + 90, chi=90,
|
||||
phi=0))
|
||||
lref = ((0, 0, 1),
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30 - ROT, chi=90, phi=0))
|
||||
pairs = (("hk", href, kref, U),
|
||||
("hl", href, lref, U),
|
||||
("kh", kref, href, U),
|
||||
("kl", kref, lref, U),
|
||||
("lk", lref, kref, U),
|
||||
("lh", lref, href, U))
|
||||
for testname, ref1, ref2, u in pairs:
|
||||
yield self.check, testname, ref1, ref2, u
|
||||
|
||||
def test_with_y_mismount(self):
|
||||
U = y_rotation(ROT)
|
||||
href = ((1, 0, 0),
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30, chi=0 - ROT, phi=0))
|
||||
lref = ((0, 0, 1),
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30, chi=90 - ROT, phi=0))
|
||||
pairs = (("hl", href, lref, U),
|
||||
("lh", lref, href, U))
|
||||
for testname, ref1, ref2, u in pairs:
|
||||
yield self.check, testname, ref1, ref2, u
|
||||
|
||||
def test_with_z_mismount(self):
|
||||
U = z_rotation(ROT)
|
||||
href = ((1, 0, 0),
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30, chi=0, phi=0 + ROT))
|
||||
lref = ((0, 0, 1), # phi degenerate
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30, chi=90, phi=67))
|
||||
pairs = (("hl", href, lref, U),
|
||||
("lh", lref, href, U))
|
||||
for testname, ref1, ref2, u in pairs:
|
||||
yield self.check, testname, ref1, ref2, u
|
||||
|
||||
def test_with_zy_mismount(self):
|
||||
U = z_rotation(ROT) * y_rotation(ROT)
|
||||
href = ((1, 0, 0),
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30, chi=0 - ROT,
|
||||
phi=0 + ROT))
|
||||
lref = ((0, 0, 1),
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30, chi=90 - ROT,
|
||||
phi=ROT)) # chi degenerate
|
||||
pairs = (("hl", href, lref, U),
|
||||
("lh", lref, href, U))
|
||||
for testname, ref1, ref2, u in pairs:
|
||||
yield self.check, testname, ref1, ref2, u
|
||||
|
||||
|
||||
class TestUBCalcWithcubicOneRef(TestUBCalcWithCubic):
|
||||
|
||||
def check(self, testname, hklref, expectedUMatrix):
|
||||
print testname
|
||||
self.addref(hklref)
|
||||
self.ubcalc.calculate_UB_from_primary_only()
|
||||
matrixeq_(expectedUMatrix, self.ubcalc.U)
|
||||
|
||||
def test_with_squarely_mounted(self):
|
||||
href = ((1, 0, 0),
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30, chi=0, phi=0))
|
||||
href_b = ((1, 0, 0),
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30 + 90, chi=90,
|
||||
phi=-90))
|
||||
lref = ((0, 0, 1), # degenerate in phi
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30, chi=90, phi=67))
|
||||
pairs = (("h", href, I),
|
||||
("hb", href_b, I),
|
||||
("l", lref, I))
|
||||
for testname, ref, u in pairs:
|
||||
yield self.check, testname, ref, u
|
||||
|
||||
def test_with_x_mismount_h(self):
|
||||
U = x_rotation(ROT)
|
||||
href = ((1, 0, 0),
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30, chi=0, phi=0))
|
||||
self.check("h", href, I)
|
||||
|
||||
def test_with_x_mismount_k(self):
|
||||
U = x_rotation(ROT)
|
||||
kref = ((0, 1, 0),
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30 - ROT + 90, chi=90,
|
||||
phi=0))
|
||||
self.check("k", kref, U)
|
||||
|
||||
def test_with_x_mismount_l(self):
|
||||
U = x_rotation(ROT)
|
||||
lref = ((0, 0, 1),
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30 - ROT, chi=90, phi=0))
|
||||
self.check("l", lref, U)
|
||||
|
||||
def test_with_y_mismount_h(self):
|
||||
U = y_rotation(ROT)
|
||||
href = ((1, 0, 0),
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30, chi=0 - ROT, phi=0))
|
||||
self.check("h", href, U)
|
||||
|
||||
def test_with_y_mismount_k(self):
|
||||
U = y_rotation(ROT)
|
||||
kref = ((0, 1, 0),
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30 + 90, chi=90, phi=0))
|
||||
self.check("k", kref, I) # TODO: can't pass - word instructions
|
||||
|
||||
def test_with_y_mismount_l(self):
|
||||
U = y_rotation(ROT)
|
||||
lref = ((0, 0, 1),
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30, chi=90 - ROT, phi=0))
|
||||
self.check("l", lref, U)
|
||||
|
||||
def test_with_z_mismount_h(self):
|
||||
U = z_rotation(ROT)
|
||||
|
||||
href = ((1, 0, 0),
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30, chi=0, phi=0 + ROT))
|
||||
self.check("h", href, U)
|
||||
|
||||
def test_with_z_mismount_k(self):
|
||||
U = z_rotation(ROT)
|
||||
kref = ((0, 1, 0),
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30 + 90, chi=0,
|
||||
phi=0 + ROT))
|
||||
self.check("k", kref, U),
|
||||
|
||||
def test_with_z_mismount_l(self):
|
||||
U = z_rotation(ROT)
|
||||
lref = ((0, 0, 1), # phi degenerate
|
||||
Pos(alpha=0, delta=60, gamma=0, omega=30, chi=90, phi=67))
|
||||
self.check("l", lref, I) # TODO: can't pass - word instructions
|
||||
|
||||
#Probably lost cause, conclusion is be careful and return the angle and
|
||||
#direction of resulting u matrix
|
||||
# def skip_test_with_zy_mismount(self):
|
||||
# U = z_rotation(ROT) * y_rotation(ROT)
|
||||
# href = ((1, 0, 0),
|
||||
# Pos(alpha=0, delta=60, gamma=0, omega=30, chi=0 - ROT,
|
||||
# phi=0 + ROT))
|
||||
# kref = ((0, 1, 0),
|
||||
# Pos(alpha=0, delta=60, gamma=0, omega=30 + 90, chi=90 - ROT,
|
||||
# phi=0 + ROT))
|
||||
# lref = ((0, 0, 1),
|
||||
# Pos(alpha=0, delta=60, gamma=0, omega=30, chi=90 - ROT,
|
||||
# phi=ROT)) # chi degenerate
|
||||
# pairs = (("h", href, U),
|
||||
# ("k", kref, U),
|
||||
# ("l", lref, U))
|
||||
# for testname, ref, u in pairs:
|
||||
# yield self.check, testname, ref, u
|
||||
@@ -0,0 +1,83 @@
|
||||
###
|
||||
# Copyright 2008-2011 Diamond Light Source Ltd.
|
||||
# This file is part of Diffcalc.
|
||||
#
|
||||
# Diffcalc is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Diffcalc is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Diffcalc. If not, see <http://www.gnu.org/licenses/>.
|
||||
###
|
||||
|
||||
from math import pi
|
||||
from mock import Mock
|
||||
|
||||
try:
|
||||
from numpy import matrix
|
||||
except ImportError:
|
||||
from numjy import matrix
|
||||
|
||||
from diffcalc.hkl.you.geometry import SixCircle
|
||||
from diffcalc.hkl.you.geometry import YouPosition
|
||||
from diffcalc.hkl.you.calc import YouUbCalcStrategy
|
||||
from test.tools import matrixeq_
|
||||
from diffcalc.ub.calc import UBCalculation
|
||||
from diffcalc.ub.persistence import UbCalculationNonPersister
|
||||
|
||||
#newub 'cubic' <--> reffile('cubic)
|
||||
#setlat 'cubic' 1 1 1 90 90 90 <--> latt([1,1,1,90,90,90])
|
||||
#pos wl 1 <--> BLi.setWavelength(1)
|
||||
# <--> c2th([0,0,1]) --> 60
|
||||
#pos sixc [0 60 0 30 1 1] <--> pos euler [1 1 30 0 60 0]
|
||||
#addref 1 0 0 <--> saveref('100',[1, 0, 0])
|
||||
#pos chi 91 <-->
|
||||
#addref 0 0 1 <--> saveref('100',[1, 0, 0]) ; showref()
|
||||
# ubm('100','001')
|
||||
# ubm() ->
|
||||
#array('d', [0.9996954135095477, -0.01745240643728364, -0.017449748351250637,
|
||||
#0.01744974835125045, 0.9998476951563913, -0.0003045864904520898,
|
||||
#0.017452406437283505, -1.1135499981271473e-16, 0.9998476951563912])
|
||||
|
||||
|
||||
def posFromI16sEuler(phi, chi, eta, mu, delta, gamma):
|
||||
return YouPosition(mu, delta, gamma, eta, chi, phi)
|
||||
|
||||
UB1 = matrix(
|
||||
((0.9996954135095477, -0.01745240643728364, -0.017449748351250637),
|
||||
(0.01744974835125045, 0.9998476951563913, -0.0003045864904520898),
|
||||
(0.017452406437283505, -1.1135499981271473e-16, 0.9998476951563912))
|
||||
) * (2 * pi)
|
||||
|
||||
EN1 = 12.39842
|
||||
REF1a = posFromI16sEuler(1, 1, 30, 0, 60, 0)
|
||||
REF1b = posFromI16sEuler(1, 91, 30, 0, 60, 0)
|
||||
|
||||
|
||||
class TestUBCalculationWithYouStrategy():
|
||||
"""Testing the math only here.
|
||||
"""
|
||||
|
||||
def setup_method(self):
|
||||
geometry = SixCircle() # pass through
|
||||
hardware = Mock()
|
||||
names = 'm', 'd', 'n', 'e', 'c', 'p'
|
||||
hardware.get_axes_names.return_value = names
|
||||
self.ubcalc = UBCalculation(hardware,
|
||||
geometry,
|
||||
UbCalculationNonPersister(),
|
||||
YouUbCalcStrategy())
|
||||
|
||||
def testAgainstI16Results(self):
|
||||
self.ubcalc.start_new('cubcalc')
|
||||
self.ubcalc.set_lattice('latt', 1, 1, 1, 90, 90, 90)
|
||||
self.ubcalc.add_reflection(1, 0, 0, REF1a, EN1, '100', None)
|
||||
self.ubcalc.add_reflection(0, 0, 1, REF1b, EN1, '001', None)
|
||||
self.ubcalc.calculate_UB()
|
||||
matrixeq_(self.ubcalc.UB, UB1)
|
||||
62
script/__Lib/diffcalc_old/test/diffcalc/ub/test_crystal.py
Normal file
62
script/__Lib/diffcalc_old/test/diffcalc/ub/test_crystal.py
Normal file
@@ -0,0 +1,62 @@
|
||||
###
|
||||
# Copyright 2008-2011 Diamond Light Source Ltd.
|
||||
# This file is part of Diffcalc.
|
||||
#
|
||||
# Diffcalc is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Diffcalc is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Diffcalc. If not, see <http://www.gnu.org/licenses/>.
|
||||
###
|
||||
|
||||
import unittest
|
||||
|
||||
try:
|
||||
from numpy import matrix
|
||||
except ImportError:
|
||||
from numjy import matrix
|
||||
|
||||
from test.tools import assert_dict_almost_equal, mneq_
|
||||
from diffcalc.ub.crystal import CrystalUnderTest
|
||||
from test.diffcalc import scenarios
|
||||
|
||||
|
||||
class TestCrystalUnderTest(object):
|
||||
|
||||
def setup_method(self):
|
||||
self.tclatt = []
|
||||
self.tcbmat = []
|
||||
|
||||
# From the dif_init.mat next to dif_dos.exe on Vlieg's cd
|
||||
#self.tclatt.append([4.0004, 4.0004, 2.270000, 90, 90, 90])
|
||||
#self.tcbmat.append([[1.570639, 0, 0] ,[0.0, 1.570639, 0] ,
|
||||
# [0.0, 0.0, 2.767923]])
|
||||
|
||||
# From b16 on 27June2008 (From Chris Nicklin)
|
||||
# self.tclatt.append([3.8401, 3.8401, 5.43072, 90, 90, 90])
|
||||
# self.tcbmat.append([[1.636204, 0, 0],[0, 1.636204, 0],
|
||||
# [0, 0, 1.156971]])
|
||||
|
||||
def testGetBMatrix(self):
|
||||
# Check the calculated B Matrix
|
||||
for sess in scenarios.sessions():
|
||||
if sess.bmatrix is None:
|
||||
continue
|
||||
cut = CrystalUnderTest('tc', *sess.lattice)
|
||||
desired = matrix(sess.bmatrix)
|
||||
print desired.tolist()
|
||||
answer = cut.B
|
||||
print answer.tolist()
|
||||
note = "Incorrect B matrix calculation for scenario " + sess.name
|
||||
mneq_(answer, desired, 4, note=note)
|
||||
|
||||
def test__str__(self):
|
||||
cut = CrystalUnderTest("HCl", 1, 2, 3, 4, 5, 6)
|
||||
print cut.__str__()
|
||||
100
script/__Lib/diffcalc_old/test/diffcalc/ub/test_persistence.py
Normal file
100
script/__Lib/diffcalc_old/test/diffcalc/ub/test_persistence.py
Normal file
@@ -0,0 +1,100 @@
|
||||
###
|
||||
# Copyright 2008-2011 Diamond Light Source Ltd.
|
||||
# This file is part of Diffcalc.
|
||||
#
|
||||
# Diffcalc is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Diffcalc is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Diffcalc. If not, see <http://www.gnu.org/licenses/>.
|
||||
###
|
||||
|
||||
import os
|
||||
import shutil
|
||||
import unittest
|
||||
import tempfile
|
||||
import time
|
||||
from nose.tools import eq_ # @UnresolvedImport
|
||||
|
||||
try:
|
||||
from gda.configuration.properties import LocalProperties
|
||||
except ImportError:
|
||||
print "Could not import LocalProperties to configure database locations."
|
||||
|
||||
from diffcalc.ub.persistence import UbCalculationNonPersister, UBCalculationJSONPersister
|
||||
|
||||
|
||||
def prepareEmptyGdaVarFolder():
|
||||
vartest_dir = os.path.join(os.getcwd(), 'var_test')
|
||||
LocalProperties.set('gda.var', vartest_dir)
|
||||
if os.path.exists(vartest_dir):
|
||||
print "Removing existing gda.var: ", vartest_dir
|
||||
shutil.rmtree(vartest_dir)
|
||||
print "Creating gda.var: ", vartest_dir
|
||||
os.mkdir(vartest_dir)
|
||||
|
||||
|
||||
class TestUBCalculationNonPersister(object):
|
||||
|
||||
def setup_method(self):
|
||||
self.persister = UbCalculationNonPersister()
|
||||
|
||||
def testSaveAndLoad(self):
|
||||
self.persister.save('string1', 'ub1')
|
||||
|
||||
|
||||
class TestUBCalculationJSONPersister(object):
|
||||
|
||||
def setup_method(self):
|
||||
self.tmpdir = tempfile.mkdtemp()
|
||||
print self.tmpdir
|
||||
self.persister = UBCalculationJSONPersister(self.tmpdir)
|
||||
f = open(os.path.join(self.tmpdir, 'unexpected_file'), 'w')
|
||||
f.close()
|
||||
|
||||
def test_list_with_empty_dir(self):
|
||||
eq_(self.persister.list(), [])
|
||||
|
||||
def test_save_load(self):
|
||||
d = {'a' : 1, 'b': 2}
|
||||
self.persister.save(d, 'first')
|
||||
eq_(self.persister.load('first'), d)
|
||||
|
||||
def test_save_overwites(self):
|
||||
d1 = {'a' : 1, 'b': 2}
|
||||
self.persister.save(d1, 'first')
|
||||
eq_(self.persister.load('first'), d1)
|
||||
|
||||
d2 = {'a' : 3, 'b': 4, 'c' : 5}
|
||||
self.persister.save(d2, 'first')
|
||||
eq_(self.persister.load('first'), d2)
|
||||
|
||||
def test_list(self):
|
||||
d = {'a' : 1, 'b': 2}
|
||||
self.persister.save(d, 'first')
|
||||
eq_(self.persister.list(), ['first'])
|
||||
|
||||
def test_multiple_list(self):
|
||||
d = {'a' : 1, 'b': 2}
|
||||
self.persister.save(d, 'first_written')
|
||||
time.sleep(.5)
|
||||
self.persister.save(d, 'second_written')
|
||||
time.sleep(.5)
|
||||
self.persister.save(d, 'third_written')
|
||||
eq_(self.persister.list(), ['third_written', 'second_written', 'first_written'])
|
||||
|
||||
def test_remove_list(self):
|
||||
d = {'a' : 1, 'b': 2}
|
||||
self.persister.save(d, 'first')
|
||||
self.persister.remove('first')
|
||||
eq_(self.persister.list(), [])
|
||||
|
||||
|
||||
|
||||
61
script/__Lib/diffcalc_old/test/diffcalc/ub/test_reference.py
Normal file
61
script/__Lib/diffcalc_old/test/diffcalc/ub/test_reference.py
Normal file
@@ -0,0 +1,61 @@
|
||||
###
|
||||
# Copyright 2008-2011 Diamond Light Source Ltd.
|
||||
# This file is part of Diffcalc.
|
||||
#
|
||||
# Diffcalc is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Diffcalc is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Diffcalc. If not, see <http://www.gnu.org/licenses/>.
|
||||
###
|
||||
|
||||
from mock import Mock
|
||||
from diffcalc.ub.reference import YouReference
|
||||
from test.tools import assert_array_almost_equal, assert_2darray_almost_equal
|
||||
|
||||
try:
|
||||
from numpy import matrix, hstack
|
||||
from numpy.linalg import norm
|
||||
except ImportError:
|
||||
from numjy import matrix, hstack
|
||||
from numjy.linalg import norm
|
||||
|
||||
|
||||
class TestYouReference():
|
||||
|
||||
def setup_method(self):
|
||||
self.get_UB = Mock()
|
||||
self.reference = YouReference(self.get_UB)
|
||||
self.get_UB.return_value = matrix('1 0 0; 0 1 0; 0 0 1')
|
||||
|
||||
def test_default_n_phi(self):
|
||||
assert_2darray_almost_equal(self.reference.n_phi.tolist(), matrix('0; 0; 1').tolist())
|
||||
|
||||
def test__str__with_phi_configured(self):
|
||||
print self.reference
|
||||
|
||||
def test__str__with_hkl_configured(self):
|
||||
self.reference.n_hkl_configured = matrix('0; 1; 1')
|
||||
print self.reference
|
||||
|
||||
def test_n_phi_from_hkl_with_unity_matrix_001(self):
|
||||
self.get_UB.return_value = matrix('1 0 0; 0 1 0; 0 0 1')
|
||||
self.reference.n_hkl_configured = matrix('0; 0; 1')
|
||||
assert_2darray_almost_equal(self.reference.n_phi.tolist(), matrix('0; 0; 1').tolist())
|
||||
|
||||
def test_n_phi_from_hkl_with_unity_matrix_010(self):
|
||||
self.get_UB.return_value = matrix('1 0 0; 0 1 0; 0 0 1')
|
||||
self.reference.n_hkl_configured = matrix('0; 1; 0')
|
||||
assert_2darray_almost_equal(self.reference.n_phi.tolist(), matrix('0; 1; 0').tolist())
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
###
|
||||
# Copyright 2008-2011 Diamond Light Source Ltd.
|
||||
# This file is part of Diffcalc.
|
||||
#
|
||||
# Diffcalc is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Diffcalc is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Diffcalc. If not, see <http://www.gnu.org/licenses/>.
|
||||
###
|
||||
|
||||
from datetime import datetime
|
||||
from diffcalc.hkl.vlieg.geometry import SixCircleGammaOnArmGeometry
|
||||
from diffcalc.ub.reflections import ReflectionList
|
||||
from diffcalc.hkl.vlieg.geometry import VliegPosition as Pos
|
||||
import unittest
|
||||
|
||||
|
||||
class TestReflectionList(object):
|
||||
|
||||
def setup_method(self):
|
||||
self._geometry = SixCircleGammaOnArmGeometry()
|
||||
self.reflist = ReflectionList(self._geometry,
|
||||
['a', 'd', 'g', 'o', 'c', 'p'])
|
||||
self.time = datetime.now()
|
||||
pos = Pos(0.1, 0.2, 0.3, 0.4, 0.5, 0.6)
|
||||
self.reflist.add_reflection(1, 2, 3, pos, 1000, "ref1", self.time)
|
||||
pos = Pos(0.11, 0.22, 0.33, 0.44, 0.55, 0.66)
|
||||
self.reflist.add_reflection(1.1, 2.2, 3.3, pos, 1100, "ref2", self.time)
|
||||
|
||||
def test_add_reflection(self):
|
||||
assert len(self.reflist) == 2
|
||||
|
||||
def testGetReflection(self):
|
||||
answered = self.reflist.getReflection(1)
|
||||
pos = Pos(0.1, 0.2, 0.3, 0.4, 0.5, 0.6)
|
||||
desired = ([1, 2, 3], pos, 1000, "ref1", self.time)
|
||||
assert answered == desired
|
||||
|
||||
def testRemoveReflection(self):
|
||||
self.reflist.removeReflection(1)
|
||||
answered = self.reflist.getReflection(1)
|
||||
pos = Pos(0.11, 0.22, 0.33, 0.44, 0.55, 0.66)
|
||||
desired = ([1.1, 2.2, 3.3], pos, 1100, "ref2", self.time)
|
||||
assert answered == desired
|
||||
|
||||
def testedit_reflection(self):
|
||||
ps = Pos(0.1, 0.2, 0.3, 0.4, 0.5, 0.6)
|
||||
self.reflist.edit_reflection(1, 10, 20, 30, ps, 1000, "new1", self.time)
|
||||
assert (self.reflist.getReflection(1)
|
||||
== ([10, 20, 30], ps, 1000, "new1", self.time))
|
||||
pos = Pos(0.11, 0.22, 0.33, 0.44, 0.55, 0.66)
|
||||
assert (self.reflist.getReflection(2)
|
||||
== ([1.1, 2.2, 3.3], pos, 1100, "ref2", self.time))
|
||||
|
||||
def testSwapReflection(self):
|
||||
self.reflist.swap_reflections(1, 2)
|
||||
pos = Pos(0.11, 0.22, 0.33, 0.44, 0.55, 0.66)
|
||||
assert (self.reflist.getReflection(1)
|
||||
== ([1.1, 2.2, 3.3], pos, 1100, "ref2", self.time))
|
||||
pos = Pos(0.1, 0.2, 0.3, 0.4, 0.5, 0.6)
|
||||
assert (self.reflist.getReflection(2)
|
||||
== ([1, 2, 3], pos, 1000, "ref1", self.time))
|
||||
|
||||
def createRefStateDicts(self):
|
||||
ref_0 = {
|
||||
'h': 1,
|
||||
'k': 2,
|
||||
'l': 3,
|
||||
'position': (0.1, 0.2, 0.3, 0.4, 0.5, 0.6),
|
||||
'energy': 1000,
|
||||
'tag': "ref1",
|
||||
'time': repr(self.time)
|
||||
}
|
||||
ref_1 = {
|
||||
'h': 1.1,
|
||||
'k': 2.2,
|
||||
'l': 3.3,
|
||||
'position': (0.11, 0.22, 0.33, 0.44, 0.55, 0.66),
|
||||
'energy': 1100,
|
||||
'tag': "ref2",
|
||||
'time': repr(self.time)
|
||||
}
|
||||
return ref_0, ref_1
|
||||
465
script/__Lib/diffcalc_old/test/diffcalc/ub/test_ub.py
Normal file
465
script/__Lib/diffcalc_old/test/diffcalc/ub/test_ub.py
Normal file
@@ -0,0 +1,465 @@
|
||||
###
|
||||
# Copyright 2008-2011 Diamond Light Source Ltd.
|
||||
# This file is part of Diffcalc.
|
||||
#
|
||||
# Diffcalc is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Diffcalc is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Diffcalc. If not, see <http://www.gnu.org/licenses/>.
|
||||
###
|
||||
|
||||
from nose.tools import eq_ # @UnresolvedImport
|
||||
import tempfile
|
||||
import os.path
|
||||
import pytest
|
||||
|
||||
try:
|
||||
from numpy import matrix
|
||||
except ImportError:
|
||||
from numjy import matrix
|
||||
|
||||
import diffcalc.util # @UnusedImport
|
||||
from diffcalc.hkl.vlieg.geometry import SixCircleGammaOnArmGeometry
|
||||
from diffcalc.hardware import DummyHardwareAdapter
|
||||
from test.tools import assert_iterable_almost_equal, mneq_, arrayeq_
|
||||
from diffcalc.ub.persistence import UbCalculationNonPersister,\
|
||||
UBCalculationJSONPersister
|
||||
from diffcalc.util import DiffcalcException, MockRawInput
|
||||
from diffcalc.ub.calc import UBCalculation
|
||||
from diffcalc.hkl.vlieg.calc import VliegUbCalcStrategy
|
||||
from test.diffcalc import scenarios
|
||||
import diffcalc.hkl.vlieg.calc
|
||||
|
||||
|
||||
diffcalc.util.DEBUG = True
|
||||
|
||||
|
||||
def prepareRawInput(listOfStrings):
|
||||
diffcalc.util.raw_input = MockRawInput(listOfStrings)
|
||||
|
||||
prepareRawInput([])
|
||||
|
||||
from diffcalc import settings
|
||||
from diffcalc.hkl.you.geometry import SixCircle
|
||||
from mock import Mock
|
||||
from diffcalc.ub.persistence import UbCalculationNonPersister
|
||||
|
||||
|
||||
|
||||
class TestUBCommandsBase():
|
||||
|
||||
def setup_method(self):
|
||||
names = 'alpha', 'delta', 'gamma', 'omega', 'chi', 'phi'
|
||||
self.hardware = DummyHardwareAdapter(names)
|
||||
_geometry = SixCircleGammaOnArmGeometry()
|
||||
_ubcalc_persister = self._createPersister()
|
||||
settings.hardware = self.hardware
|
||||
settings.geometry = _geometry
|
||||
settings.ubcalc_persister = _ubcalc_persister
|
||||
#settings.set_engine_name('vlieg')
|
||||
settings.ubcalc_strategy = diffcalc.hkl.vlieg.calc.VliegUbCalcStrategy()
|
||||
settings.angles_to_hkl_function = diffcalc.hkl.vlieg.calc.vliegAnglesToHkl
|
||||
|
||||
from diffcalc.ub import ub
|
||||
reload(ub)
|
||||
self.ub = ub
|
||||
#self.ub.ubcalc = ub.ubcalc
|
||||
prepareRawInput([])
|
||||
diffcalc.util.RAISE_EXCEPTIONS_FOR_ALL_ERRORS = True
|
||||
|
||||
|
||||
class TestUbCommands(TestUBCommandsBase):
|
||||
|
||||
def _createPersister(self):
|
||||
return UbCalculationNonPersister()
|
||||
|
||||
def testNewUb(self):
|
||||
self.ub.newub('test1')
|
||||
eq_(self.ub.ubcalc._state.name, 'test1')
|
||||
with pytest.raises(TypeError):
|
||||
self.ub.newub(1)
|
||||
|
||||
def testNewUbInteractively(self):
|
||||
prepareRawInput(['ubcalcname', 'xtal', '1', '2', '3', '91', '92',
|
||||
'93'])
|
||||
self.ub.newub()
|
||||
|
||||
def testLoadub(self):
|
||||
with pytest.raises(TypeError):
|
||||
self.ub.loadub((1, 2))
|
||||
|
||||
def testSaveubcalcas(self):
|
||||
with pytest.raises(TypeError):
|
||||
self.ub.saveubas(1)
|
||||
with pytest.raises(TypeError):
|
||||
self.ub.saveubas((1, 2))
|
||||
self.ub.saveubas('blarghh')
|
||||
|
||||
def testUb(self):
|
||||
with pytest.raises(TypeError):
|
||||
self.ub.showref((1))
|
||||
self.ub.ub()
|
||||
self.ub.newub('testubcalc')
|
||||
self.ub.ub()
|
||||
|
||||
def testSetlat(self):
|
||||
# "Exception should result if no UBCalculation started")
|
||||
with pytest.raises(DiffcalcException):
|
||||
self.ub.setlat('HCl', 2)
|
||||
|
||||
self.ub.newub('testing_setlat')
|
||||
with pytest.raises(TypeError):
|
||||
self.ub.setlat(1)
|
||||
with pytest.raises(TypeError):
|
||||
self.ub.setlat(1, 2)
|
||||
with pytest.raises(TypeError):
|
||||
self.ub.setlat('HCl')
|
||||
self.ub.setlat('NaCl', 1.1)
|
||||
ubcalc = self.ub.ubcalc
|
||||
eq_(('NaCl', 1.1, 1.1, 1.1, 90, 90, 90), ubcalc._state.crystal.getLattice())
|
||||
self.ub.setlat('NaCl', 1.1, 2.2)
|
||||
eq_(('NaCl', 1.1, 1.1, 2.2, 90, 90, 90), ubcalc._state.crystal.getLattice())
|
||||
self.ub.setlat('NaCl', 1.1, 2.2, 3.3)
|
||||
eq_(('NaCl', 1.1, 2.2, 3.3, 90, 90, 90), ubcalc._state.crystal.getLattice())
|
||||
self.ub.setlat('NaCl', 1.1, 2.2, 3.3, 91)
|
||||
eq_(('NaCl', 1.1, 2.2, 3.3, 90, 90, 91), ubcalc._state.crystal.getLattice())
|
||||
with pytest.raises(TypeError):
|
||||
self.ub.setlat(('NaCl', 1.1, 2.2, 3.3, 91, 92))
|
||||
self.ub.setlat('NaCl', 1.1, 2.2, 3.3, 91, 92, 93)
|
||||
assert_iterable_almost_equal(
|
||||
('NaCl', 1.1, 2.2, 3.3, 91, 92, 92.99999999999999),
|
||||
ubcalc._state.crystal.getLattice())
|
||||
|
||||
def testSetlatInteractive(self):
|
||||
self.ub.newub('testing_setlatinteractive')
|
||||
prepareRawInput(['xtal', '1', '2', '3', '91', '92', '93'])
|
||||
self.ub.setlat()
|
||||
getLattice = self.ub.ubcalc._state.crystal.getLattice
|
||||
assert_iterable_almost_equal(getLattice(),
|
||||
('xtal', 1., 2., 3., 91, 92, 92.999999999999986))
|
||||
|
||||
#Defaults:
|
||||
prepareRawInput(['xtal', '', '', '', '', '', ''])
|
||||
self.ub.setlat()
|
||||
getLattice = self.ub.ubcalc._state.crystal.getLattice
|
||||
eq_(getLattice(), ('xtal', 1., 1., 1., 90, 90, 90))
|
||||
|
||||
def testShowref(self):
|
||||
with pytest.raises(TypeError):
|
||||
self.ub.showref((1))
|
||||
eq_(self.ub.showref(), None) # No UBCalculation loaded
|
||||
# will be tested, for exceptions at least, implicitely below
|
||||
self.ub.newub('testing_showref')
|
||||
eq_(self.ub.showref(), None) # No UBCalculation loaded"
|
||||
|
||||
def testAddref(self):
|
||||
with pytest.raises(TypeError):
|
||||
self.ub.addref(1)
|
||||
with pytest.raises(TypeError):
|
||||
self.ub.addref(1, 2)
|
||||
with pytest.raises(TypeError):
|
||||
self.ub.addref(1, 2, 'blarghh')
|
||||
# start new ubcalc
|
||||
self.ub.newub('testing_addref')
|
||||
reflist = self.ub.ubcalc._state.reflist # for conveniance
|
||||
|
||||
pos1 = (1.1, 1.2, 1.3, 1.4, 1.5, 1.6)
|
||||
pos2 = (2.1, 2.2, 2.3, 2.4, 2.5, 2.6)
|
||||
pos3 = (3.1, 3.2, 3.3, 3.4, 3.5, 3.6)
|
||||
pos4 = (4.1, 4.2, 4.3, 4.4, 4.5, 4.6)
|
||||
#
|
||||
self.hardware.energy = 1.10
|
||||
self.hardware.position = pos1
|
||||
self.ub.addref([1.1, 1.2, 1.3])
|
||||
result = reflist.get_reflection_in_external_angles(1)
|
||||
eq_(result[:-1], ([1.1, 1.2, 1.3], pos1, 1.10, None))
|
||||
|
||||
self.hardware.energy = 2.10
|
||||
self.hardware.position = pos2
|
||||
self.ub.addref([2.1, 2.2, 2.3], 'atag')
|
||||
result = reflist.get_reflection_in_external_angles(2)
|
||||
eq_(result[:-1], ([2.1, 2.2, 2.3], pos2, 2.10, 'atag'))
|
||||
|
||||
self.ub.addref([3.1, 3.2, 3.3], pos3, 3.10)
|
||||
result = reflist.get_reflection_in_external_angles(3)
|
||||
eq_(result[:-1], ([3.1, 3.2, 3.3], pos3, 3.10, None))
|
||||
|
||||
self.ub.addref([4.1, 4.2, 4.3], pos4, 4.10, 'tag2')
|
||||
result = reflist.get_reflection_in_external_angles(4)
|
||||
eq_(result[:-1], ([4.1, 4.2, 4.3], pos4, 4.10, 'tag2'))
|
||||
|
||||
def testAddrefInteractively(self):
|
||||
prepareRawInput([])
|
||||
# start new ubcalc
|
||||
self.ub.newub('testing_addref')
|
||||
reflist = self.ub.ubcalc._state.reflist # for conveniance
|
||||
|
||||
pos1 = (1.1, 1.2, 1.3, 1.4, 1.5, 1.6)
|
||||
pos2 = (2.1, 2.2, 2.3, 2.4, 2.5, 2.6)
|
||||
pos3 = (3.1, 3.2, 3.3, 3.4, 3.5, 3.6)
|
||||
pos3s = ['3.1', '3.2', '3.3', '3.4', '3.5', '3.6']
|
||||
pos4 = (4.1, 4.2, 4.3, 4.4, 4.5, 4.6)
|
||||
pos4s = ['4.1', '4.2', '4.3', '4.4', '4.5', '4.6']
|
||||
#
|
||||
self.hardware.energy = 1.10
|
||||
self.hardware.position = pos1
|
||||
prepareRawInput(['1.1', '1.2', '1.3', '', ''])
|
||||
self.ub.addref()
|
||||
result = reflist.get_reflection_in_external_angles(1)
|
||||
eq_(result[:-1], ([1.1, 1.2, 1.3], pos1, 1.10, None))
|
||||
|
||||
self.hardware.energy = 2.10
|
||||
self.hardware.position = pos2
|
||||
prepareRawInput(['2.1', '2.2', '2.3', '', 'atag'])
|
||||
self.ub.addref()
|
||||
result = reflist.get_reflection_in_external_angles(2)
|
||||
eq_(result[:-1], ([2.1, 2.2, 2.3], pos2, 2.10, 'atag'))
|
||||
|
||||
prepareRawInput(['3.1', '3.2', '3.3', 'n'] + pos3s + ['3.10', ''])
|
||||
self.ub.addref()
|
||||
result = reflist.get_reflection_in_external_angles(3)
|
||||
eq_(result[:-1], ([3.1, 3.2, 3.3], pos3, 3.10, None))
|
||||
|
||||
prepareRawInput(['4.1', '4.2', '4.3', 'n'] + pos4s + ['4.10', 'tag2'])
|
||||
self.ub.addref()
|
||||
result = reflist.get_reflection_in_external_angles(4)
|
||||
eq_(result[:-1], ([4.1, 4.2, 4.3], pos4, 4.10, 'tag2'))
|
||||
|
||||
def testEditRefInteractivelyWithCurrentPosition(self):
|
||||
pos1 = (1.1, 1.2, 1.3, 1.4, 1.5, 1.6)
|
||||
pos2 = (2.1, 2.2, 2.3, 2.4, 2.5, 2.6)
|
||||
self.ub.newub('testing_editref')
|
||||
self.ub.addref([1, 2, 3], pos1, 10, 'tag1')
|
||||
self.hardware.energy = 11
|
||||
self.hardware.position = pos2
|
||||
prepareRawInput(['1.1', '', '3.1', 'y', ''])
|
||||
self.ub.editref(1)
|
||||
|
||||
reflist = self.ub.ubcalc._state.reflist # for conveniance
|
||||
result = reflist.get_reflection_in_external_angles(1)
|
||||
eq_(result[:-1], ([1.1, 2, 3.1], pos2, 11, 'tag1'))
|
||||
|
||||
def testEditRefInteractivelyWithEditedPosition(self):
|
||||
pos1 = (1.1, 1.2, 1.3, 1.4, 1.5, 1.6)
|
||||
pos2 = (2.1, 2.2, 2.3, 2.4, 2.5, 2.6)
|
||||
pos2s = ['2.1', '2.2', '2.3', '2.4', '2.5', '2.6']
|
||||
self.ub.newub('testing_editref')
|
||||
self.ub.addref([1, 2, 3], pos1, 10, 'tag1')
|
||||
prepareRawInput(['1.1', '', '3.1', 'n'] + pos2s + ['12', 'newtag'])
|
||||
self.ub.editref(1)
|
||||
|
||||
reflist = self.ub.ubcalc._state.reflist
|
||||
result = reflist.get_reflection_in_external_angles(1)
|
||||
eq_(result[:-1], ([1.1, 2, 3.1], pos2, 12, 'newtag'))
|
||||
|
||||
def testSwapref(self):
|
||||
with pytest.raises(TypeError):
|
||||
self.ub.swapref(1)
|
||||
with pytest.raises(TypeError):
|
||||
self.ub.swapref(1, 2, 3)
|
||||
with pytest.raises(TypeError):
|
||||
self.ub.swapref(1, 1.1)
|
||||
self.ub.newub('testing_swapref')
|
||||
pos = (1.1, 1.2, 1.3, 1.4, 1.5, 1.6)
|
||||
self.ub.addref([1, 2, 3], pos, 10, 'tag1')
|
||||
self.ub.addref([1, 2, 3], pos, 10, 'tag2')
|
||||
self.ub.addref([1, 2, 3], pos, 10, 'tag3')
|
||||
self.ub.swapref(1, 3)
|
||||
self.ub.swapref(1, 3)
|
||||
self.ub.swapref(3, 1) # end flipped
|
||||
reflist = self.ub.ubcalc._state.reflist
|
||||
tag1 = reflist.get_reflection_in_external_angles(1)[3]
|
||||
tag2 = reflist.get_reflection_in_external_angles(2)[3]
|
||||
tag3 = reflist.get_reflection_in_external_angles(3)[3]
|
||||
eq_(tag1, 'tag3')
|
||||
eq_(tag2, 'tag2')
|
||||
eq_(tag3, 'tag1')
|
||||
self.ub.swapref()
|
||||
tag1 = reflist.get_reflection_in_external_angles(1)[3]
|
||||
tag2 = reflist.get_reflection_in_external_angles(2)[3]
|
||||
eq_(tag1, 'tag2')
|
||||
eq_(tag2, 'tag3')
|
||||
|
||||
def testDelref(self):
|
||||
self.ub.newub('testing_swapref')
|
||||
pos = (1.1, 1.2, 1.3, 1.4, 1.5, 1.6)
|
||||
self.ub.addref([1, 2, 3], pos, 10, 'tag1')
|
||||
reflist = self.ub.ubcalc._state.reflist
|
||||
reflist.get_reflection_in_external_angles(1)
|
||||
self.ub.delref(1)
|
||||
with pytest.raises(IndexError):
|
||||
reflist.get_reflection_in_external_angles(1)
|
||||
|
||||
def testSetu(self):
|
||||
# just test calling this method
|
||||
#self.ub.setu([[1,2,3],[1,2,3],[1,2,3]])
|
||||
self.ub.newub('testsetu')
|
||||
setu = self.ub.setu
|
||||
with pytest.raises(TypeError):
|
||||
setu(1, 2)
|
||||
with pytest.raises(TypeError):
|
||||
setu(1)
|
||||
with pytest.raises(TypeError):
|
||||
setu('a')
|
||||
with pytest.raises(TypeError):
|
||||
setu([1, 2, 3])
|
||||
with pytest.raises(TypeError):
|
||||
setu([[1, 2, 3], [1, 2, 3], [1, 2]])
|
||||
# diffCalcException expected if no lattice set yet
|
||||
with pytest.raises(DiffcalcException):
|
||||
setu([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
|
||||
self.ub.setlat('NaCl', 1.1)
|
||||
setu([[1, 2, 3], [1, 2, 3], [1, 2, 3]]) # check no exceptions only
|
||||
setu(((1, 2, 3), (1, 2, 3), (1, 2, 3))) # check no exceptions only
|
||||
|
||||
def testSetuInteractive(self):
|
||||
self.ub.newub('testsetu')
|
||||
self.ub.setlat('NaCl', 1.1)
|
||||
|
||||
prepareRawInput(['1 2 3', '4 5 6', '7 8 9'])
|
||||
self.ub.setu()
|
||||
a = self.ub.ubcalc.U.tolist()
|
||||
eq_([list(a[0]), list(a[1]), list(a[2])],
|
||||
[[1, 2, 3], [4, 5, 6], [7, 8, 9]])
|
||||
|
||||
prepareRawInput(['', ' 9 9.9 99', ''])
|
||||
self.ub.setu()
|
||||
|
||||
a = self.ub.ubcalc.U.tolist()
|
||||
eq_([list(a[0]), list(a[1]), list(a[2])],
|
||||
[[1, 0, 0], [9, 9.9, 99], [0, 0, 1]])
|
||||
|
||||
def testSetub(self):
|
||||
# just test calling this method
|
||||
self.ub.newub('testsetub')
|
||||
setub = self.ub.setub
|
||||
with pytest.raises(TypeError):
|
||||
setub(1, 2)
|
||||
with pytest.raises(TypeError):
|
||||
setub(1)
|
||||
with pytest.raises(TypeError):
|
||||
setub('a')
|
||||
with pytest.raises(TypeError):
|
||||
setub([1, 2, 3])
|
||||
with pytest.raises(TypeError):
|
||||
setub([[1, 2, 3], [1, 2, 3], [1, 2]])
|
||||
setub([[1, 2, 3], [1, 2, 3], [1, 2, 3]]) # check no exceptions only
|
||||
setub(((1, 2, 3), (1, 2, 3), (1, 2, 3))) # check no exceptions only
|
||||
|
||||
def testSetUbInteractive(self):
|
||||
self.ub.newub('testsetu')
|
||||
self.ub.setlat('NaCl', 1.1)
|
||||
|
||||
prepareRawInput(['1 2 3', '4 5 6', '7 8 9'])
|
||||
self.ub.setub()
|
||||
a = self.ub.ubcalc.UB.tolist()
|
||||
eq_([list(a[0]), list(a[1]), list(a[2])],
|
||||
[[1, 2, 3], [4, 5, 6], [7, 8, 9]])
|
||||
|
||||
prepareRawInput(['', ' 9 9.9 99', ''])
|
||||
self.ub.setub()
|
||||
a = self.ub.ubcalc.UB.tolist()
|
||||
eq_([list(a[0]), list(a[1]), list(a[2])],
|
||||
[[1, 0, 0], [9, 9.9, 99], [0, 0, 1]])
|
||||
|
||||
def testCalcub(self):
|
||||
with pytest.raises(TypeError):
|
||||
self.ub.calcub(1) # wrong input
|
||||
# no ubcalc started:
|
||||
with pytest.raises(DiffcalcException):
|
||||
self.ub.calcub()
|
||||
self.ub.newub('testcalcub')
|
||||
# not enougth reflections:
|
||||
with pytest.raises(DiffcalcException):
|
||||
self.ub.calcub()
|
||||
|
||||
s = scenarios.sessions()[0]
|
||||
self.ub.setlat(s.name, *s.lattice)
|
||||
r = s.ref1
|
||||
self.ub.addref(
|
||||
[r.h, r.k, r.l], r.pos.totuple(), r.energy, r.tag)
|
||||
r = s.ref2
|
||||
self.ub.addref(
|
||||
[r.h, r.k, r.l], r.pos.totuple(), r.energy, r.tag)
|
||||
self.ub.calcub()
|
||||
mneq_(self.ub.ubcalc.UB, matrix(s.umatrix) * matrix(s.bmatrix),
|
||||
4, note="wrong UB matrix after calculating U")
|
||||
|
||||
def testC2th(self):
|
||||
self.ub.newub('testcalcub')
|
||||
self.ub.setlat('cube', 1, 1, 1, 90, 90, 90)
|
||||
assert self.ub.c2th((0, 0, 1)) == pytest.approx(60)
|
||||
|
||||
def testSigtau(self):
|
||||
# sigtau [sig tau]
|
||||
with pytest.raises(TypeError):
|
||||
self.ub.sigtau(1)
|
||||
with pytest.raises(ValueError):
|
||||
self.ub.sigtau(1, 'a')
|
||||
self.ub.sigtau(1, 2)
|
||||
self.ub.sigtau(1, 2.0)
|
||||
eq_(self.ub.ubcalc.sigma, 1)
|
||||
eq_(self.ub.ubcalc.tau, 2.0)
|
||||
|
||||
def testSigtauInteractive(self):
|
||||
prepareRawInput(['1', '2.'])
|
||||
self.ub.sigtau()
|
||||
eq_(self.ub.ubcalc.sigma, 1)
|
||||
eq_(self.ub.ubcalc.tau, 2.0)
|
||||
#Defaults:
|
||||
prepareRawInput(['', ''])
|
||||
self.hardware.position = [None, None, None, None, 3, 4.]
|
||||
self.ub.sigtau()
|
||||
eq_(self.ub.ubcalc.sigma, -3.)
|
||||
eq_(self.ub.ubcalc.tau, -4.)
|
||||
|
||||
def testSetWithString(self):
|
||||
with pytest.raises(TypeError):
|
||||
self.ub.setlat('alpha', 'a')
|
||||
with pytest.raises(TypeError):
|
||||
self.ub.setlat('alpha', 1, 'a')
|
||||
|
||||
def test_setnphihkl_at_various_phases(self):
|
||||
self.ub.setnphi([1, 0, 1])
|
||||
self.ub.setnhkl([1, 0, 1])
|
||||
self.ub.newub('test')
|
||||
self.ub.setnphi([1, 0, 1])
|
||||
self.ub.setnhkl([1, 0, 1])
|
||||
self.ub.setlat('cube', 1, 1, 1, 90, 90, 90)
|
||||
self.ub.setnphi([1, 0, 1])
|
||||
self.ub.setnhkl([1, 0, 1])
|
||||
self.ub.setu([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
|
||||
self.ub.setnphi([1, 0, 1])
|
||||
self.ub.setnhkl([1, 0, 1])
|
||||
|
||||
|
||||
class TestUbCommandsJsonPersistence(TestUBCommandsBase):
|
||||
|
||||
def _createPersister(self):
|
||||
self.tmpdir = tempfile.mkdtemp()
|
||||
print self.tmpdir
|
||||
self.persister = UBCalculationJSONPersister(self.tmpdir)
|
||||
f = open(os.path.join(self.tmpdir, 'unexpected_file'), 'w')
|
||||
f.close()
|
||||
return self.persister
|
||||
|
||||
def testNewUb(self):
|
||||
self.ub.newub('test1')
|
||||
self.ub.loadub('test1')
|
||||
self.ub.ub()
|
||||
|
||||
def test_n_phi_persistance(self):
|
||||
self.ub.newub('test1')
|
||||
self.ub.setnphi([0, 1, 0])
|
||||
arrayeq_(self.ub.ubcalc.n_phi.T.tolist()[0], [0, 1, 0])
|
||||
self.ub.loadub('test1')
|
||||
arrayeq_(self.ub.ubcalc.n_phi.T.tolist()[0], [0, 1, 0])
|
||||
|
||||
Reference in New Issue
Block a user