File: tango_numpy.py

package info (click to toggle)
pytango 10.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 10,216 kB
  • sloc: python: 28,206; cpp: 16,380; sql: 255; sh: 82; makefile: 43
file content (131 lines) | stat: -rw-r--r-- 4,180 bytes parent folder | download | duplicates (3)
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
# SPDX-FileCopyrightText: All Contributors to the PyTango project
# SPDX-License-Identifier: LGPL-3.0-or-later

"""
This is an internal PyTango module.
"""

__all__ = ("NumpyType", "numpy_type", "numpy_spectrum", "numpy_image")

__docformat__ = "restructuredtext"

import collections.abc
import numpy

from tango._tango import Except, Attribute, AttributeInfo
from tango._tango import CmdArgType as ArgType

from tango.attribute_proxy import AttributeProxy


def _numpy_invalid(*args, **kwds):
    Except.throw_exception(
        "PyTango_InvalidConversion",
        "There's no registered conversor to numpy.",
        "NumpyType.tango_to_numpy",
    )


class NumpyType:
    DevShort = numpy.int16
    DevLong = numpy.int32
    DevDouble = numpy.float64
    DevFloat = numpy.float32
    DevBoolean = numpy.bool_
    DevUShort = numpy.uint16
    DevULong = numpy.uint32
    DevUChar = numpy.ubyte
    DevLong64 = numpy.int64
    DevULong64 = numpy.uint64

    mapping = {
        ArgType.DevShort: DevShort,
        ArgType.DevLong: DevLong,
        ArgType.DevDouble: DevDouble,
        ArgType.DevFloat: DevFloat,
        ArgType.DevBoolean: DevBoolean,
        ArgType.DevUShort: DevUShort,
        ArgType.DevULong: DevULong,
        ArgType.DevUChar: DevUChar,
        ArgType.DevLong64: DevLong64,
        ArgType.DevULong: DevULong64,
    }

    @staticmethod
    def tango_to_numpy(param):
        if isinstance(param, ArgType):
            tg_type = param
        if isinstance(param, AttributeInfo):  # or AttributeInfoEx
            tg_type = param.data_type
        elif isinstance(param, Attribute):
            tg_type = param.get_data_type()
        elif isinstance(param, AttributeProxy):
            tg_type = param.get_config().data_type
        else:
            tg_type = param
        try:
            return NumpyType.mapping[tg_type]
        except Exception:
            _numpy_invalid()

    @staticmethod
    def spectrum(tg_type, dim_x):
        """
        numpy_spectrum(self, tg_type, dim_x, dim_y) -> numpy.array
        numpy_spectrum(self, tg_type, sequence) -> numpy.array

                Get a square numpy array to be used with tango.
                One version gets dim_x and creates an object with
                this size. The other version expects any sequence to
                convert.

            Parameters:
                - tg_type : (ArgType): The tango type. For convenience, it
                            can also extract this information from an
                            Attribute, AttributeInfo or AttributeProxy
                            object.
                - dim_x : (int)
                - sequence:
        """
        np_type = NumpyType.tango_to_numpy(tg_type)
        if isinstance(dim_x, collections.abc.Sequence):
            return numpy.array(dim_x, dtype=np_type)
        else:
            return numpy.ndarray(shape=(dim_x,), dtype=np_type)

    @staticmethod
    def image(tg_type, dim_x, dim_y=None):
        """
        numpy_image(self, tg_type, dim_x, dim_y) -> numpy.array
        numpy_image(self, tg_type, sequence) -> numpy.array

                Get a square numpy array to be used with tango.
                One version gets dim_x and dim_y and creates an object with
                this size. The other version expects a square sequence of
                sequences to convert.

            Parameters:
                - tg_type : (ArgType): The tango type. For convenience, it
                            can also extract this information from an
                            Attribute, AttributeInfo or AttributeProxy
                            object.
                - dim_x : (int)
                - dim_y : (int)
                - sequence:
        """
        np_type = NumpyType.tango_to_numpy(tg_type)
        if dim_y is None:
            return numpy.array(dim_x, dtype=np_type)
        else:
            return numpy.ndarray(
                shape=(
                    dim_y,
                    dim_x,
                ),
                dtype=np_type,
            )


numpy_spectrum = NumpyType.spectrum
numpy_image = NumpyType.image
numpy_type = NumpyType.tango_to_numpy