function max_extents
Computes the maximal extent (bounding box) from a list of extent tuples, supporting both 2D (4-tuples) and 3D (6-tuples) coordinate systems with special handling for datetime and string values.
/tf/active/vicechatdev/patches/util.py
1045 - 1082
moderate
Purpose
This function aggregates multiple extent tuples to find the overall minimum and maximum bounds across all dimensions. It's designed for spatial data visualization and analysis, where you need to determine the combined bounding box of multiple objects or datasets. The function handles mixed data types including numeric values, datetime objects, and strings, and can optionally work in 3D space by enabling the zrange parameter.
Source Code
def max_extents(extents, zrange=False):
"""
Computes the maximal extent in 2D and 3D space from
list of 4-tuples or 6-tuples. If zrange is enabled
all extents are converted to 6-tuples to compute
x-, y- and z-limits.
"""
if zrange:
num = 6
inds = [(0, 3), (1, 4), (2, 5)]
extents = [e if len(e) == 6 else (e[0], e[1], None,
e[2], e[3], None)
for e in extents]
else:
num = 4
inds = [(0, 2), (1, 3)]
arr = list(zip(*extents)) if extents else []
extents = [np.NaN] * num
if len(arr) == 0:
return extents
with warnings.catch_warnings():
warnings.filterwarnings('ignore', r'All-NaN (slice|axis) encountered')
for lidx, uidx in inds:
lower = [v for v in arr[lidx] if v is not None and not is_nan(v)]
upper = [v for v in arr[uidx] if v is not None and not is_nan(v)]
if lower and isinstance(lower[0], datetime_types):
extents[lidx] = np.min(lower)
elif any(isinstance(l, str) for l in lower):
extents[lidx] = np.sort(lower)[0]
elif lower:
extents[lidx] = np.nanmin(lower)
if upper and isinstance(upper[0], datetime_types):
extents[uidx] = np.max(upper)
elif any(isinstance(u, str) for u in upper):
extents[uidx] = np.sort(upper)[-1]
elif upper:
extents[uidx] = np.nanmax(upper)
return tuple(extents)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
extents |
- | - | positional_or_keyword |
zrange |
- | False | positional_or_keyword |
Parameter Details
extents: A list of tuples representing spatial extents. Each tuple should be either a 4-tuple (x_min, y_min, x_max, y_max) for 2D or a 6-tuple (x_min, y_min, z_min, x_max, y_max, z_max) for 3D. Can contain None values or NaN values which will be filtered out during computation. Empty list is acceptable and will return NaN-filled tuple.
zrange: Boolean flag (default: False). When True, enables 3D mode and converts all 4-tuples to 6-tuples by adding None for z-coordinates, then computes x, y, and z limits. When False, operates in 2D mode computing only x and y limits.
Return Value
Returns a tuple representing the maximal extent across all input extents. In 2D mode (zrange=False), returns a 4-tuple (x_min, y_min, x_max, y_max). In 3D mode (zrange=True), returns a 6-tuple (x_min, y_min, z_min, x_max, y_max, z_max). Values are computed using np.nanmin/np.nanmax for numeric data, np.min/np.max for datetime objects, and np.sort for strings. Returns tuple of np.NaN values if input is empty or all values are None/NaN.
Dependencies
numpywarnings
Required Imports
import numpy as np
import warnings
Usage Example
import numpy as np
import warnings
from datetime import datetime
# Assuming datetime_types and is_nan are defined:
# datetime_types = (datetime,)
# def is_nan(val):
# try:
# return np.isnan(val)
# except (TypeError, ValueError):
# return False
# 2D example with numeric extents
extents_2d = [(0, 0, 10, 10), (5, 5, 15, 15), (-2, -2, 8, 8)]
max_extent_2d = max_extents(extents_2d, zrange=False)
print(max_extent_2d) # Output: (-2, -2, 15, 15)
# 3D example with mixed 4-tuples and 6-tuples
extents_3d = [(0, 0, 10, 10), (5, 5, 2, 15, 15, 8)]
max_extent_3d = max_extents(extents_3d, zrange=True)
print(max_extent_3d) # Output: (0, 0, 2, 15, 15, 8)
# Example with None and NaN values
extents_with_nans = [(0, 0, 10, 10), (None, 5, np.nan, 15), (2, 2, 8, 8)]
max_extent_filtered = max_extents(extents_with_nans, zrange=False)
print(max_extent_filtered) # Output: (0, 0, 10, 15)
# Empty extents
empty_result = max_extents([], zrange=False)
print(empty_result) # Output: (nan, nan, nan, nan)
Best Practices
- Ensure that all extent tuples in the input list have consistent dimensionality (all 4-tuples or all 6-tuples) unless using zrange=True which handles mixed dimensions
- The function expects extents in the format (min_coords..., max_coords...) where lower bounds come first, followed by upper bounds
- None and NaN values are automatically filtered out, so they can be safely included in input extents
- When working with datetime objects, ensure they are of compatible types as the function uses isinstance checks against datetime_types
- The function suppresses 'All-NaN slice/axis encountered' warnings internally, so be aware that completely empty dimensions will return NaN without warning
- For string extents, the function uses lexicographic sorting (np.sort) to determine min/max values
- The function depends on module-level variables 'datetime_types' and 'is_nan' which must be defined in the calling context
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function max_range 59.9% similar
-
function find_minmax 57.1% similar
-
function bound_range 55.2% similar
-
function find_range 53.7% similar
-
function dimension_range 51.7% similar