From 4eb593ae0360a8fabda24d3871b5f3d425753060 Mon Sep 17 00:00:00 2001 From: Lili Karashchuk Date: Mon, 23 Oct 2023 16:36:29 -0700 Subject: [PATCH] support loading slp files with non-compound types and str in metadata --- sleap/io/format/hdf5.py | 51 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/sleap/io/format/hdf5.py b/sleap/io/format/hdf5.py index 353f88e3a..55a30d74f 100644 --- a/sleap/io/format/hdf5.py +++ b/sleap/io/format/hdf5.py @@ -81,7 +81,10 @@ def read_headers( # Extract the Labels JSON metadata and create Labels object with just this # metadata. - dicts = json_loads(f.require_group("metadata").attrs["json"].tobytes().decode()) + json = f.require_group("metadata").attrs["json"] + if not isinstance(json, str): + json = json.tobytes().decode() + dicts = json_loads(json) # These items are stored in separate lists because the metadata group got to be # too big. @@ -151,6 +154,45 @@ def read( points_dset[:]["x"] -= 0.5 points_dset[:]["y"] -= 0.5 + def cast_as_compound(arr, dtype): + out = np.empty(shape=(len(arr),), dtype=dtype) + if out.size == 0: + return out + for i, (name, _) in enumerate(dtype): + out[name] = arr[:, i] + return out + + # cast points, instances, and frames into complex dtype if not already + dtype_points = [("x", "