Skip to content
Snippets Groups Projects
Commit c17d9727 authored by Chad Hanna's avatar Chad Hanna Committed by Chad Hanna
Browse files

Patrick Godwin's code: work around backwards incompatible pygobject handling of arrays

parent 6e67f141
No related branches found
No related tags found
1 merge request!449Pygobject workaround
......@@ -31,6 +31,8 @@
# =============================================================================
#
from collections import Iterable
import numpy
......@@ -46,6 +48,7 @@ Gst.init(None)
import lal
from ligo.segments import segment
__author__ = "Kipp Cannon <kipp.cannon@ligo.org>, Chad Hanna <chad.hanna@ligo.org>, Drew Keppel <drew.keppel@ligo.org>"
......@@ -240,3 +243,74 @@ def parse_framesrc_tags(taglist):
"channel-name": channel_name,
"sample-units": sample_units
}
# From Patrick Godwin
# https://git.ligo.org/lscsoft/spiir/-/issues/70#note_602061
def format_property(prop):
"""
Formats a property suitable for use in a GStreamer element.
Used to convert 2-dimensional data structures to an appropriate
type as needed, since the mechanics of how they are treated differ
between versions of pygobject. Acts as a no-op depending on the
property type and version of pygobject.
"""
# NOTE FIXME hopefully pygobject will get fixed and we can add a greater
# than in this test
if GObject.pygobject_version < (3, 29, 0):
return prop
elif is_nested_listlike(prop):
if isinstance(prop, numpy.ndarray):
prop = prop.tolist()
return [to_gvalue_array(row) for row in prop]
elif is_listlike(prop):
return to_gvalue_array(prop)
else:
return prop
# From Patrick Godwin
# https://git.ligo.org/lscsoft/spiir/-/issues/70#note_602061
def to_gvalue_array(arr):
"""
Converts a list-like object to a GValueArray.
"""
# handle segments as guint64
if isinstance(arr, segment):
st = Gst.Structure(f"converter, array=(guint64) < {arr[0]:d}, {arr[1]:d} >")
else:
st = Gst.Structure.new_empty("converter")
st["array"] = Gst.ValueArray(list(arr))
result, val = st.get_array("array")
if not result:
raise ValueError("could not convert input to GValueArray")
return val
# From Patrick Godwin
# https://git.ligo.org/lscsoft/spiir/-/issues/70#note_602061
def is_nested_listlike(obj):
"""
Check if object is a nested list-like object.
"""
if isinstance(obj, numpy.ndarray) and obj.ndim == 2:
return True
elif is_listlike(obj):
return any(is_listlike(row) for row in obj)
else:
return False
# From Patrick Godwin
# https://git.ligo.org/lscsoft/spiir/-/issues/70#note_602061
def is_listlike(obj):
"""
Check if object is a list-like object.
"""
if isinstance(obj, numpy.ndarray) and obj.ndim == 1:
return True
else:
return isinstance(obj, Iterable) and not isinstance(obj, str)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment