File: trajectory.py

package info (click to toggle)
hkl 5.1.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,992 kB
  • sloc: ansic: 57,080; haskell: 5,996; sh: 4,991; cpp: 1,846; makefile: 1,129; python: 925; xml: 76; lisp: 55
file content (156 lines) | stat: -rwxr-xr-x 4,472 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python3

import math
import numpy
import matplotlib

matplotlib.use("Agg")

import matplotlib.pyplot as plt

from matplotlib.backends.backend_pdf import PdfPages
from matplotlib import rcParams
from gi.repository import GLib
import gi

gi.require_version("Hkl", "5.0")
from gi.repository import Hkl


def compute_hkl_trajectories(engines, engine, hkl1=None, hkl2=None, n=100):
    """
    compute all the trajectories for a given engine already configured
    """
    if not hkl1:
        hkl1 = [0, 0, 1]
    if not hkl2:
        hkl2 = [0, 1, 1]

    h = numpy.linspace(hkl1[0], hkl2[0], n + 1)
    k = numpy.linspace(hkl1[1], hkl2[1], n + 1)
    l = numpy.linspace(hkl1[2], hkl2[2], n + 1)

    # set the hkl engine and get the results
    trajectories = []
    for hh, kk, ll in zip(h, k, l):
        try:
            solutions = engine.pseudo_axis_values_set([hh, kk, ll], Hkl.UnitEnum.USER)
            first_solution = solutions.items()[0]
            for i, item in enumerate(solutions.items()):
                try:
                    trajectories[i]
                except IndexError:
                    trajectories.append([])
                values = item.geometry_get().axis_values_get(Hkl.UnitEnum.USER)
                trajectories[i].append(values)
            engines.select_solution(first_solution)
        except GLib.GError as err:
            pass

    return trajectories


def _plot_legend(axes):
    plt.subplot(3, 4, 1)
    plt.title("legend")
    print("legende", 1)
    for name in axes:
        plt.plot([0, 0], label=name)
    plt.legend()


def plot_hkl_trajectory(filename, geometry, engines, hkl1=None, hkl2=None, n=100):
    """
    plot the trajectory for a engine. It is possible to limit the
    number of trajectory using the max_traj keyword
    """
    axis_names = geometry.axis_names_get()

    hkl = engines.engine_get_by_name("hkl")
    page = 1
    plt.clf()
    plt.suptitle(
        '"' + filename + '" ' + repr(hkl1) + " -> " + repr(hkl2) + " page " + str(page)
    )
    _plot_legend(axis_names)
    idx = 2
    for mode in hkl.modes_names_get():
        hkl.current_mode_set(mode)
        trajectories = compute_hkl_trajectories(engines, hkl, hkl1=hkl1, hkl2=hkl2, n=n)
        print('"' + filename + '"', idx, mode, len(trajectories))

        plt.subplot(3, 4, idx)
        plt.title(mode)
        if not len(trajectories):
            plt.text(
                0.5,
                0.5,
                "Failed",
                size=20,
                rotation=0.0,
                ha="center",
                va="center",
                bbox=dict(
                    boxstyle="round",
                    ec=(1.0, 0.5, 0.5),
                    fc=(1.0, 0.8, 0.8),
                ),
            )
            plt.draw()
        else:
            plt.ylim(-180, 180)
            if len(trajectories[0]) == 1:
                plt.plot(trajectories[0], "o-")
            else:
                plt.plot(trajectories[0], "-")

        idx += 1
        if idx > 12:
            pp.savefig()
            plt.clf()
            page += 1
            _plot_legend(axis_names)
            plt.suptitle(
                filename + " " + repr(hkl1) + " -> " + repr(hkl2) + " page " + str(page)
            )
            idx = 2
    pp.savefig()


pp = PdfPages("trajectories.pdf")
rcParams["font.size"] = 6


def main():
    sample = Hkl.Sample.new("toto")
    lattice = Hkl.Lattice.new(
        1.54, 1.54, 1.54, math.radians(90.0), math.radians(90.0), math.radians(90.0)
    )
    sample.lattice_set(lattice)

    detector = Hkl.Detector.factory_new(Hkl.DetectorType(0))

    for key, factory in Hkl.factories().items():
        geometry = factory.create_new_geometry()
        engines = factory.create_new_engine_list()

        # here we set the detector arm with only positiv values for
        # now tth or delta arm
        for axis in geometry.axis_names_get():
            if axis in ["tth", "delta"]:
                tmp = geometry.axis_get(axis)
                tmp.min_max_set(0, 180.0, Hkl.UnitEnum.USER)
                geometry.axis_set(axis, tmp)

        engines.init(geometry, detector, sample)

        engines_names = [engine.name_get() for engine in engines.engines_get()]
        if "hkl" in engines_names:
            plot_hkl_trajectory(
                key, geometry, engines, hkl1=[0, 0, 1], hkl2=[0, 1, 1], n=100
            )
    pp.close()


if __name__ == "__main__":
    main()