2 # -*- coding: utf-8 -*-
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>
27 from gi
.repository
import GLib
28 from gi
.repository
import Hkl
31 class TestAPI(unittest
.TestCase
):
33 def test_detector_api(self
):
35 enforce the detector API
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 !
44 self
.assertTrue(detector
.idx
== 1)
46 def test_geometry_api(self
):
48 enforce the geometry API
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
59 isinstance([axis
.parameter
.name
for axis
in geometry
.axes()],
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
69 for r
, w
in zip(values_w
, values_r
):
70 self
.assertAlmostEqual(r
, w
)
72 def test_mode_api(self
):
74 enforce the HklMode API
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")
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
):
94 enforce the HklEngine API
97 detector
= Hkl
.Detector().factory_new(Hkl
.DetectorType(0))
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,
112 # compute all the pseudo axes managed by all engines
113 engines
= Hkl
.EngineList
.factory(config
)
114 engines
.init(geometry
, detector
, sample
)
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
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
:
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
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,
178 if __name__
== '__main__':