From b7a9772927a303931f255f115a0ca79fe80a921a Mon Sep 17 00:00:00 2001
From: markus holzer <markus.holzer@fau.de>
Date: Sat, 8 Aug 2020 09:46:26 +0200
Subject: [PATCH] Generalised boolean_array_bounding_box

---
 pystencils/utils.py | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/pystencils/utils.py b/pystencils/utils.py
index 0c8f11ee3..f089dad1b 100644
--- a/pystencils/utils.py
+++ b/pystencils/utils.py
@@ -1,4 +1,5 @@
 import os
+import itertools
 from collections import Counter
 from contextlib import contextmanager
 from tempfile import NamedTemporaryFile
@@ -96,16 +97,21 @@ def fully_contains(l1, l2):
 
 
 def boolean_array_bounding_box(boolean_array):
-    """Returns bounding box around "true" area of boolean array"""
-    dim = len(boolean_array.shape)
+    """Returns bounding box around "true" area of boolean array
+
+    >>> a = np.zeros((4, 4), dtype=bool)
+    >>> a[1:-1, 1:-1] = True
+    >>> boolean_array_bounding_box(a)
+    [(1, 3), (1, 3)]
+    """
+    dim = boolean_array.ndim
+    shape = boolean_array.shape
+    assert 0 not in shape, "Shape must not contain zero"
     bounds = []
-    for i in range(dim):
-        for j in range(dim):
-            if i != j:
-                arr_1d = np.any(boolean_array, axis=j)
-        begin = np.argmax(arr_1d)
-        end = begin + np.argmin(arr_1d[begin:])
-        bounds.append((begin, end))
+    for ax in itertools.combinations(reversed(range(dim)), dim - 1):
+        nonzero = np.any(boolean_array, axis=ax)
+        t = np.where(nonzero)[0][[0, -1]]
+        bounds.append((t[0], t[1] + 1))
     return bounds
 
 
-- 
GitLab