Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Stephan Seitz
pystencils
Commits
b7a97729
Commit
b7a97729
authored
Aug 08, 2020
by
Markus Holzer
Browse files
Generalised boolean_array_bounding_box
parent
24ef1d2f
Changes
1
Hide whitespace changes
Inline
Side-by-side
pystencils/utils.py
View file @
b7a97729
import
os
import
os
import
itertools
from
collections
import
Counter
from
collections
import
Counter
from
contextlib
import
contextmanager
from
contextlib
import
contextmanager
from
tempfile
import
NamedTemporaryFile
from
tempfile
import
NamedTemporaryFile
...
@@ -96,16 +97,21 @@ def fully_contains(l1, l2):
...
@@ -96,16 +97,21 @@ def fully_contains(l1, l2):
def
boolean_array_bounding_box
(
boolean_array
):
def
boolean_array_bounding_box
(
boolean_array
):
"""Returns bounding box around "true" area of boolean array"""
"""Returns bounding box around "true" area of boolean array
dim
=
len
(
boolean_array
.
shape
)
>>> 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
=
[]
bounds
=
[]
for
i
in
range
(
dim
):
for
ax
in
itertools
.
combinations
(
reversed
(
range
(
dim
)),
dim
-
1
):
for
j
in
range
(
dim
):
nonzero
=
np
.
any
(
boolean_array
,
axis
=
ax
)
if
i
!=
j
:
t
=
np
.
where
(
nonzero
)[
0
][[
0
,
-
1
]]
arr_1d
=
np
.
any
(
boolean_array
,
axis
=
j
)
bounds
.
append
((
t
[
0
],
t
[
1
]
+
1
))
begin
=
np
.
argmax
(
arr_1d
)
end
=
begin
+
np
.
argmin
(
arr_1d
[
begin
:])
bounds
.
append
((
begin
,
end
))
return
bounds
return
bounds
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment