From 287db15d7995fa98d9a42e3b7670d34e43acea51 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Al=C3=A1n=20Mu=C3=B1oz?= <alan.munoz@ed.ac.uk>
Date: Thu, 23 Mar 2023 16:39:46 +0000
Subject: [PATCH] fix(pp): squash typo bugs and indexing

---
 src/agora/io/signal.py                   |  2 +-
 src/agora/utils/indexing.py              | 19 +++++++++++++++----
 src/postprocessor/core/lineageprocess.py |  2 +-
 3 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/src/agora/io/signal.py b/src/agora/io/signal.py
index 1da5ab10..42b5c9d5 100644
--- a/src/agora/io/signal.py
+++ b/src/agora/io/signal.py
@@ -139,7 +139,7 @@ class Signal(BridgeH5):
             # if lineage_location not in f:
             #     lineage_location = lineage_location.split("_")[0]
             if lineage_location not in f:
-                lineage_location = "postprocessor/lineage"
+                lineage_location = "postprocessing/lineage"
             tile_mo_da = f[lineage_location]
 
             if isinstance(tile_mo_da, h5py.Dataset):
diff --git a/src/agora/utils/indexing.py b/src/agora/utils/indexing.py
index 5ca4494d..789ca5a0 100644
--- a/src/agora/utils/indexing.py
+++ b/src/agora/utils/indexing.py
@@ -114,10 +114,21 @@ def _assoc_indices_to_3d(ndarray: np.ndarray):
     """
     result = ndarray
     if len(ndarray) and ndarray.ndim > 1:
-        result = np.transpose(
-            np.hstack((ndarray[:, [0]], ndarray)).reshape(-1, 2, 2),
-            axes=[0, 2, 1],
-        )
+        if ndarray.shape[1] == 3:  # Faster indexing for single positions
+            result = np.transpose(
+                np.hstack((ndarray[:, [0]], ndarray)).reshape(-1, 2, 2),
+                axes=[0, 2, 1],
+            )
+        else:  # 20% slower but more general indexing
+            columns = np.arange(ndarray.shape[1])
+
+            result = np.stack(
+                (
+                    ndarray[:, np.delete(columns, -1)],
+                    ndarray[:, np.delete(columns, -2)],
+                ),
+                axis=1,
+            )
     return result
 
 
diff --git a/src/postprocessor/core/lineageprocess.py b/src/postprocessor/core/lineageprocess.py
index 8c2874e2..ff332fab 100644
--- a/src/postprocessor/core/lineageprocess.py
+++ b/src/postprocessor/core/lineageprocess.py
@@ -60,7 +60,7 @@ class LineageProcess(PostProcessABC):
         elif hasattr(self, "lineage"):
             lineage = self.lineage
         elif hasattr(self, "cells"):
-            with h5py.File(self.cells.filename, "a") as f:
+            with h5py.File(self.cells.filename, "r") as f:
                 if (lineage_loc := "modifiers/lineage_merged") in f and merged:
                     lineage = f.get(lineage_loc)[()]
                 elif (lineage_loc := "modifiers/lineage)") in f:
-- 
GitLab