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 Show latest version
6 files
+ 148
37
Compare changes
  • Side-by-side
  • Inline
Files
6
@@ -19,16 +19,37 @@ class LofarDeviceProxy(DeviceProxy):
return super().get_attribute_config(name)
@lru_cache()
def get_attribute_shape(self, name):
"""Get the shape of the requested attribute, as a tuple."""
config = self.get_attribute_config(name)
if config.format and config.format[0] == "(":
# For >2D arrays, the "format" property describes actual
# the dimensions as a tuple (x, y, z, ...),
# so reshape the value accordingly.
shape = ast.literal_eval(config.format)
elif config.max_dim_y > 0:
# 2D array
shape = (config.max_dim_y, config.max_dim_x)
elif config.max_dim_x > 1:
# 1D array
shape = (config.max_dim_x,)
else:
# scalar
shape = ()
return shape
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)
# convert non-scalar values into their actual shape
shape = self.get_attribute_shape(name)
if shape != ():
attr.value = attr.value.reshape(shape)
return attr
@@ -37,11 +58,20 @@ class LofarDeviceProxy(DeviceProxy):
"""Write an attribute to the server."""
config = self.get_attribute_config(name)
shape = self.get_attribute_shape(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:
if shape != ():
value = numpy.array(value)
value = value.reshape((config.max_dim_y, config.max_dim_x))
if value.shape != shape:
raise ValueError(
f"Invalid shape. Given: {value.shape} Expected: {shape}"
)
if len(shape) > 2:
# >2D arrays collapse into 2D
value = value.reshape((config.max_dim_y, config.max_dim_x))
return super().write_attribute(name, value)
Loading