[hkl] add the hkl_engine_dependencies_get method
[hkl.git] / tests / bindings / python.py
bloba847a9a2dc3e24f5bb68f21325f4b99c76f38cc8
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """
4 This file is part of the hkl library.
6 The hkl library is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 The hkl library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with the hkl library. If not, see <http://www.gnu.org/licenses/>.
19 Copyright (C) 2012-2013 Synchrotron SOLEIL
20 L'Orme des Merisiers Saint-Aubin
21 BP 48 91192 GIF-sur-YVETTE CEDEX
22 Authors: Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr>
23 """
25 import math
26 import unittest
27 from gi.repository import GLib
28 from gi.repository import Hkl
31 class TestAPI(unittest.TestCase):
32 """Test all the Hkl API, if something brakes here it means that API
33 has changed !!!
34 """
36 def test_vector_api(self):
37 """
38 enforce the Vector api
39 """
40 v = Hkl.Vector()
41 self.assertTrue(type(v) == Hkl.Vector)
42 self.assertTrue(type(v.data) == list)
43 self.assertTrue(3 == len(v.data))
45 del v
47 def test_quaternion_api(self):
48 """
49 enforce the Vector api
50 """
51 q = Hkl.Quaternion()
52 self.assertTrue(type(q) == Hkl.Quaternion)
53 self.assertTrue(type(q.data) == list)
54 self.assertTrue(4 == len(q.data))
56 del q
58 def test_factory_api(self):
59 """
60 enforce the Factory API
61 """
62 # factories dict <name, Factory>
63 factories = Hkl.factories()
64 for key, factory in factories.iteritems():
65 self.assertTrue(type(key) == str)
66 self.assertTrue(type(factory) == Hkl.Factory)
68 # create all the geometry and engines
69 geometry = factory.create_new_geometry()
70 self.assertTrue(type(geometry) == Hkl.Geometry)
71 engines = factory.create_new_engine_list()
72 self.assertTrue(type(engines) == Hkl.EngineList)
74 del factories
76 def test_detector_api(self):
77 """
78 enforce the detector API
79 """
81 # create an 0D HklDetector
82 detector = Hkl.Detector.factory_new(Hkl.DetectorType(0))
83 self.assertTrue(type(detector) is Hkl.Detector)
85 del detector
87 def test_geometry_api(self):
88 """
89 enforce the geometry API
90 """
92 # get the config for a given geometry and create the
93 # corresponding HklGeometry
94 factory = Hkl.factories()['K6C']
95 geometry = factory.create_new_geometry()
97 # source access
98 wavelength = 1.
99 geometry.wavelength_set(wavelength, Hkl.UnitEnum.USER)
100 self.assertTrue(wavelength == geometry.wavelength_get(Hkl.UnitEnum.USER)) # noqa
102 # set the geometry axes values
103 values_w = [0, 30, 0, 0, 0, 60]
104 geometry.axis_values_set(values_w, Hkl.UnitEnum.USER)
105 values_r = geometry.axis_values_get(Hkl.UnitEnum.USER)
107 # check that the read and write values of the geometry are
108 # almost equals
109 for r, w in zip(values_w, values_r):
110 self.assertAlmostEqual(r, w)
112 # check that we can access the axes
113 axis_names = geometry.axis_names_get()
114 for name in axis_names:
115 axis = geometry.axis_get(name)
116 axis.min_max_set(0, math.radians(180), Hkl.UnitEnum.USER)
117 v = axis.axis_v_get()
118 q = axis.quaternion_get()
119 geometry.axis_set(name, axis)
121 del geometry
123 def test_engine_api(self):
125 enforce the HklEngine API
128 detector = Hkl.Detector.factory_new(Hkl.DetectorType(0))
130 factory = Hkl.factories()['K6C']
131 geometry = factory.create_new_geometry()
132 values_w = [0., 30., 0., 0., 0., 60.]
133 geometry.axis_values_set(values_w, Hkl.UnitEnum.USER)
135 sample = Hkl.Sample.new("toto")
136 lattice = sample.lattice_get()
137 lattice.set(1.54, 1.54, 1.54, 90, 90, 90, Hkl.UnitEnum.USER)
138 sample.lattice_set(lattice)
140 # compute all the pseudo axes managed by all engines
141 engines = factory.create_new_engine_list()
142 engines.init(geometry, detector, sample)
143 engines.get()
145 # get the hkl engine and do a computation
146 hkl = engines.engine_get_by_name("hkl")
147 values = hkl.pseudo_axis_values_get(Hkl.UnitEnum.USER)
149 # check for all modes
150 for mode in hkl.modes_names_get():
151 self.assertTrue(type(mode) is str)
153 # set the hkl engine and get the results
154 for _ in range(100):
155 try:
156 solutions = hkl.pseudo_axis_values_set(values,
157 Hkl.UnitEnum.USER)
158 self.assertTrue(type(solutions) is Hkl.GeometryList)
159 for item in solutions.items():
160 self.assertTrue(type(item) is Hkl.GeometryListItem)
161 self.assertTrue(type(item.geometry_get()) is Hkl.Geometry)
162 values[1] += .01
163 except GLib.GError, err:
164 print values, err
166 # check that all the values computed are reachable
167 for engine in engines.engines_get():
168 self.assertTrue(type(engine) is Hkl.Engine)
169 self.assertTrue(type(engine.name_get()) is str)
170 self.assertTrue(type(engine.pseudo_axis_names_get()) is list)
171 self.assertTrue(type(engine.modes_names_get()) is list)
172 self.assertTrue(len(engine.modes_names_get()))
173 for mode in engine.modes_names_get():
174 self.assertTrue(type(mode) is str)
175 values = engine.pseudo_axis_values_get(Hkl.UnitEnum.USER)
176 self.assertTrue(type(values) is list)
177 for value in values:
178 self.assertTrue(type(value) is float)
180 # check that all engine parameters and axes are reachables
181 for engine in engines.engines_get():
182 for mode in engine.modes_names_get():
183 engine.current_mode_set(mode)
185 parameters = engine.parameters_names_get()
186 self.assertTrue(type(parameters) is list)
187 [self.assertTrue(type(_) is str) for _ in parameters]
188 # all together
189 values = engine.parameters_values_get(Hkl.UnitEnum.USER)
190 [self.assertTrue(type(_) is float) for _ in values]
191 engine.parameters_values_set(values, Hkl.UnitEnum.USER)
192 # one by one
193 for parameter in parameters:
194 p = engine.parameter_get(parameter)
195 self.assertTrue(type(p.description_get()) is str)
197 # check that parameters are writable.
198 values = engine.parameters_values_get(Hkl.UnitEnum.USER)
199 values = [1.] * len(values)
200 engine.parameters_values_set(values, Hkl.UnitEnum.USER)
201 [self.assertTrue(ref == v)
202 for ref, v in zip(values,
203 engine.parameters_values_get(Hkl.UnitEnum.USER))]
205 axes_r = engine.axis_names_get(Hkl.EngineAxisNamesGet.READ)
206 self.assertTrue(type(axes_r) is list)
207 [self.assertTrue(type(_) is str) for _ in axes_r]
208 axes_w = engine.axis_names_get(Hkl.EngineAxisNamesGet.WRITE)
209 self.assertTrue(type(axes_w) is list)
210 [self.assertTrue(type(_) is str) for _ in axes_w]
212 # check all the capabilities
213 for engine in engines.engines_get():
214 capabilities = engine.capabilities_get()
215 self.assertTrue(capabilities & Hkl.EngineCapabilities.READABLE)
216 self.assertTrue(capabilities & Hkl.EngineCapabilities.WRITABLE)
217 if engine.name_get() == "psi":
218 self.assertTrue(capabilities & Hkl.EngineCapabilities.INITIALIZABLE)
220 # check initialized_get/set
221 for engine in engines.engines_get():
222 initialized = engine.initialized_get()
223 capabilities = engine.capabilities_get()
224 if capabilities & Hkl.EngineCapabilities.INITIALIZABLE:
225 engine.initialized_set(False)
226 self.assertTrue(False == engine.initialized_get())
227 engine.initialized_set(True)
228 self.assertTrue(True == engine.initialized_get())
230 # check all the dependencies
231 for engine in engines.engines_get():
232 dependencies = engine.dependencies_get()
233 self.assertTrue(dependencies & Hkl.EngineDependencies.AXES)
235 @unittest.skip("for testing figures")
236 def test_doc_example(self):
237 # execfile("../../Documentation/sphinx/source/bindings/python.py")
238 execfile(
239 "../../Documentation/sphinx/source/pyplots/trajectory_simple.py")
240 execfile(
241 "../../Documentation/sphinx/source/pyplots/trajectory_full.py")
243 self.assertTrue(False)
245 def test_sample_api(self):
247 enforce the HklSample API
250 # create a sample
251 sample = Hkl.Sample.new("toto")
252 self.assertTrue(sample.name_get() == "toto")
254 # check that the copy constructor is working
255 copy = sample.copy()
256 self.assertTrue(copy.name_get() == sample.name_get())
257 # we can change the name of the copy without affecting the original
258 copy.name_set("titi")
259 self.assertTrue(copy.name_get() != sample.name_get())
261 # set the lattice parameters
262 lattice = Hkl.Lattice.new(1.54, 1.54, 1.54,
263 math.radians(90.),
264 math.radians(90.),
265 math.radians(90.))
266 sample.lattice_set(lattice)
268 # change the lattice parameter by expanding the tuple from
269 # the get method. the lattice should not change.
270 a, b, c, alpha, beta, gamma = lattice.get(Hkl.UnitEnum.DEFAULT)
271 lattice.set(a, b, c, alpha, beta, gamma, Hkl.UnitEnum.DEFAULT)
273 # this new lattice is identical to the one from the sample
274 v = lattice.get(Hkl.UnitEnum.DEFAULT)
275 self.assertTrue(v == sample.lattice_get().get(Hkl.UnitEnum.DEFAULT))
277 # now change the lattice parameter
278 lattice.set(1, 2, 3, 90, 90, 90, Hkl.UnitEnum.USER)
280 # this new lattice is different from the one in the sample
281 v = lattice.get(Hkl.UnitEnum.DEFAULT)
282 self.assertTrue(v != sample.lattice_get().get(Hkl.UnitEnum.DEFAULT))
284 # gives access to the ux, uy, uz part
285 ux = sample.ux_get()
286 uy = sample.uy_get()
287 uz = sample.uz_get()
288 sample.ux_set(ux)
289 sample.uy_set(uy)
290 sample.uz_set(uz)
292 # read and write the U matrix
293 U = sample.U_get()
294 UB = sample.UB_get()
295 sample.UB_set(UB)
297 # get the reciprocal lattice
298 reciprocal = lattice.copy()
299 lattice.reciprocal(reciprocal)
301 # get the lattice volume
302 lattice.volume_get()
304 del sample
305 del lattice
307 def test_reflection_api(self):
309 detector = Hkl.Detector.factory_new(Hkl.DetectorType(0))
311 factory = Hkl.factories()['K6C']
312 geometry = factory.create_new_geometry()
313 values_w = [0., 30., 0., 0., 0., 60.]
314 geometry.axis_values_set(values_w, Hkl.UnitEnum.USER)
316 sample = Hkl.Sample.new("toto")
318 # add reflection
319 r1 = sample.add_reflection(geometry, detector, 1, 1, 1)
320 r2 = sample.add_reflection(geometry, detector, 1, 1, 1)
322 # get the hkl part
323 self.assertTrue(r2.hkl_get() == (1.0, 1.0, 1.0))
324 r2.hkl_set(1, 0, 1)
325 self.assertTrue(r2.hkl_get() == (1.0, 0.0, 1.0))
327 # compute the angles
328 sample.get_reflection_measured_angle(r1, r2)
329 sample.get_reflection_theoretical_angle(r1, r2)
331 # remove all the reflections
332 reflections = sample.reflections_get()
333 for reflection in reflections:
334 sample.del_reflection(reflection)
336 del reflections
337 del sample
339 if __name__ == '__main__':
340 unittest.main(verbosity=2)