function find_minmax
Computes the minimum of the first elements and maximum of the second elements from two tuples of numeric values, handling NaN values gracefully.
/tf/active/vicechatdev/patches/util.py
906 - 917
simple
Purpose
This function is designed to merge and expand numeric ranges by finding the overall minimum and maximum values across two limit tuples. It's particularly useful for calculating combined bounds when aggregating multiple data ranges, such as determining axis limits in plotting or finding overall data bounds across multiple datasets. The function uses NaN-aware comparison functions to handle missing or invalid data.
Source Code
def find_minmax(lims, olims):
"""
Takes (a1, a2) and (b1, b2) as input and returns
(np.nanmin(a1, b1), np.nanmax(a2, b2)). Used to calculate
min and max values of a number of items.
"""
try:
limzip = zip(list(lims), list(olims), [np.nanmin, np.nanmax])
limits = tuple([float(fn([l, ol])) for l, ol, fn in limzip])
except:
limits = (np.NaN, np.NaN)
return limits
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
lims |
- | - | positional_or_keyword |
olims |
- | - | positional_or_keyword |
Parameter Details
lims: A tuple or list-like object containing two numeric values (a1, a2) representing a range. Typically a1 is a minimum value and a2 is a maximum value. Can contain NaN values which will be handled appropriately.
olims: A tuple or list-like object containing two numeric values (b1, b2) representing another range. Typically b1 is a minimum value and b2 is a maximum value. Can contain NaN values which will be handled appropriately.
Return Value
Returns a tuple of two float values: (minimum, maximum) where minimum is the smallest value between the first elements of both input tuples (using np.nanmin), and maximum is the largest value between the second elements of both input tuples (using np.nanmax). If an error occurs during processing (e.g., invalid input types), returns (np.NaN, np.NaN).
Dependencies
numpy
Required Imports
import numpy as np
Usage Example
import numpy as np
def find_minmax(lims, olims):
try:
limzip = zip(list(lims), list(olims), [np.nanmin, np.nanmax])
limits = tuple([float(fn([l, ol])) for l, ol, fn in limzip])
except:
limits = (np.NaN, np.NaN)
return limits
# Example 1: Basic usage with two ranges
range1 = (5, 10)
range2 = (3, 8)
result = find_minmax(range1, range2)
print(result) # Output: (3.0, 10.0)
# Example 2: Handling NaN values
range3 = (np.nan, 15)
range4 = (2, 12)
result = find_minmax(range3, range4)
print(result) # Output: (2.0, 15.0)
# Example 3: Both ranges with NaN
range5 = (1, np.nan)
range6 = (np.nan, 20)
result = find_minmax(range5, range6)
print(result) # Output: (1.0, 20.0)
# Example 4: Invalid input returns NaN tuple
result = find_minmax(None, (1, 2))
print(result) # Output: (nan, nan)
Best Practices
- Ensure both input parameters are tuple-like or list-like objects with exactly two numeric elements each
- The function assumes the first element of each tuple represents a minimum-like value and the second represents a maximum-like value
- The function silently returns (np.NaN, np.NaN) on any exception - consider adding logging or more specific error handling for production use
- Input values can be NaN and will be handled correctly by np.nanmin and np.nanmax
- The bare except clause catches all exceptions which may hide bugs - consider using more specific exception handling
- The function converts results to float, so ensure your use case can handle floating-point precision
- This function is useful for iteratively expanding bounds across multiple datasets or ranges
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function find_range 76.4% similar
-
function max_range 64.7% similar
-
function max_extents 57.1% similar
-
function bound_range 55.2% similar
-
function dimension_range 49.3% similar