switch to c-tap-harness 2.1
[hkl.git] / tests / bindings / python.py
blobb92ae92f771d8b7057fa2b9d5a605b2eb345a42d
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):
33 def test_detector_api(self):
34 """
35 enforce the detector API
36 """
38 # create an 0D HklDetector
39 detector = Hkl.Detector().factory_new(Hkl.DetectorType(0))
41 # mount the detector on the 2nd holder of the geometry
42 # yes numbering follow the C convention !
43 detector.idx = 1
44 self.assertTrue(detector.idx == 1)
46 def test_geometry_api(self):
47 """
48 enforce the geometry API
49 """
51 # get the config for a given geometry and create the
52 # corresponding HklGeometry
53 config = Hkl.geometry_factory_get_config_from_type(
54 Hkl.GeometryType.KAPPA6C)
55 geometry = Hkl.Geometry.factory_newv(config, [50. * math.pi / 180.])
57 # axes names are accessible
58 self.assertTrue(
59 isinstance([axis.parameter.name for axis in geometry.axes()],
60 list))
62 # set the geometry axes values
63 values_w = [0, 30, 0, 0, 0, 60]
64 geometry.set_axes_values_unit(values_w)
65 values_r = geometry.get_axes_values_unit()
67 # check that the read and write values of the geometry are
68 # almost equals
69 for r, w in zip(values_w, values_r):
70 self.assertAlmostEqual(r, w)
72 def test_mode_api(self):
73 """
74 enforce the HklMode API
75 """
76 config = Hkl.geometry_factory_get_config_from_type(
77 Hkl.GeometryType.KAPPA6C)
78 engines = Hkl.EngineList.factory(config)
79 engine = engines.get_by_name("hkl")
81 # check for all modes
82 for mode in engine.modes():
83 self.assertTrue(type(mode) is Hkl.Mode)
84 self.assertTrue(type(mode.name()) is str)
86 # check the parameters
87 parameters = mode.parameters()
88 self.assertTrue(type(parameters) is Hkl.ParameterList)
89 for parameter in parameters.parameters():
90 self.assertTrue(type(parameter) is Hkl.Parameter)
92 def test_engine_api(self):
93 """
94 enforce the HklEngine API
95 """
97 detector = Hkl.Detector().factory_new(Hkl.DetectorType(0))
98 detector.idx = 1
100 config = Hkl.geometry_factory_get_config_from_type(
101 Hkl.GeometryType.KAPPA6C)
102 geometry = Hkl.Geometry.factory_newv(config, [math.radians(50.)])
103 values_w = [0., 30., 0., 0., 0., 60.]
104 geometry.set_axes_values_unit(values_w)
106 sample = Hkl.Sample.new("toto", Hkl.SampleType.MONOCRYSTAL)
107 sample.set_lattice(1.54, 1.54, 1.54,
108 math.radians(90.0),
109 math.radians(90.0),
110 math.radians(90.0))
112 # compute all the pseudo axes managed by all engines
113 engines = Hkl.EngineList.factory(config)
114 engines.init(geometry, detector, sample)
115 engines.get()
117 # get the hkl engine and do a computation
118 hkl = engines.get_by_name("hkl")
119 values = hkl.pseudo_axes().get_values_unit()
121 # check for all modes
122 for mode in hkl.modes():
123 self.assertTrue(type(mode) is Hkl.Mode)
125 # set the hkl engine and get the results
126 for _ in range(100):
127 try:
128 hkl.pseudo_axes().set_values_unit(values)
129 solutions = engines.geometries()
130 for item in solutions.items():
131 item.geometry.get_axes_values_unit()
132 except GLib.GError, err:
133 print values, err
134 values[1] += .01
136 # for item in engines.geometries.items():
137 # print item.geometry.get_axes_values_unit()
139 # check that all the values computed are reachable
140 for engine in engines.engines():
141 self.assertTrue(type(engine) is Hkl.Engine)
142 self.assertTrue(type(engine.name()) is str)
143 for parameter in engine.pseudo_axes().parameters():
144 self.assertTrue(type(parameter) is Hkl.Parameter)
145 self.assertTrue(type(parameter.get_value()) is float)
147 # check the set result
148 for item in engines.geometries().items():
149 self.assertTrue(type(item) is Hkl.GeometryListItem)
150 self.assertTrue(type(item.geometry) is Hkl.Geometry)
152 self.assertTrue(True)
154 @unittest.skip("for testing figures")
155 def test_doc_exemple(self):
156 #execfile("../../Documentation/sphinx/source/bindings/python.py")
157 execfile("../../Documentation/sphinx/source/pyplots/trajectory_simple.py")
158 execfile("../../Documentation/sphinx/source/pyplots/trajectory_full.py")
160 self.assertTrue(False)
162 def test_sample_api(self):
164 enforce the HklSample API
167 # create a sample
168 sample = Hkl.Sample.new("toto", Hkl.SampleType.MONOCRYSTAL)
169 self.assertTrue(sample.name == "toto")
171 #set the lattice parameters
172 sample.set_lattice(1.54, 1.54, 1.54,
173 math.radians(90.),
174 math.radians(90.),
175 math.radians(90.))
178 if __name__ == '__main__':
179 unittest.main()