Skip to content
Snippets Groups Projects

Added custom LofarDeviceProxy to enhance support for high-dimensional arrays in attribtues

Merged Jan David Mol requested to merge reshape-highdim-arrays into main
All threads resolved!
Compare and
6 files
+ 142
6
Compare changes
  • Side-by-side
  • Inline
Files
6
+ 47
0
""" Enhanced interfaces towards the station's Tango devices. """
import ast
from functools import lru_cache
import numpy
from tango import DeviceProxy
from tango import ExtractAs
class LofarDeviceProxy(DeviceProxy):
"""A LOFAR-specific tango.DeviceProxy that provides
a richer experience."""
@lru_cache()
def get_attribute_config(self, name):
"""Get cached attribute configurations, as they are not expected to change."""
return super().get_attribute_config(name)
def read_attribute(self, name, extract_as=ExtractAs.Numpy):
"""Read an attribute from the server."""
attr = super().read_attribute(name, extract_as)
# "format" property describes actual dimensions as a tuple (x, y, z, ...),
# so reshape the value accordingly.
fmt = self.get_attribute_config(name).format
if fmt and fmt[0] == "(" and isinstance(attr.value, numpy.ndarray):
shape = ast.literal_eval(fmt)
attr.value = attr.value.reshape(shape)
return attr
def write_attribute(self, name, value):
"""Write an attribute to the server."""
config = self.get_attribute_config(name)
# 2D arrays also represent arrays of higher dimensionality. reshape them
# to fit their original Tango shape before writing.
if config.max_dim_y > 0:
value = numpy.array(value)
value = value.reshape((config.max_dim_y, config.max_dim_x))
return super().write_attribute(name, value)
Loading