function find_range
Robustly computes the minimum and maximum values from a collection, with fallback mechanisms for edge cases and support for extending the range with soft bounds.
/tf/active/vicechatdev/patches/util.py
920 - 941
moderate
Purpose
This function provides a fault-tolerant way to find the range (min, max) of a dataset. It handles various data types including numpy arrays, datetime objects, and generic iterables. It uses numpy's nanmin/nanmax to ignore NaN values, supports datetime types, and includes a soft_range parameter to extend the computed range. If numerical operations fail, it falls back to sorting and taking first/last elements. If all operations fail, it returns (None, None).
Source Code
def find_range(values, soft_range=[]):
"""
Safely finds either the numerical min and max of
a set of values, falling back to the first and
the last value in the sorted list of values.
"""
try:
values = np.array(values)
values = np.squeeze(values) if len(values.shape) > 1 else values
if len(soft_range):
values = np.concatenate([values, soft_range])
if values.dtype.kind == 'M':
return values.min(), values.max()
with warnings.catch_warnings():
warnings.filterwarnings('ignore', r'All-NaN (slice|axis) encountered')
return np.nanmin(values), np.nanmax(values)
except:
try:
values = sorted(values)
return (values[0], values[-1])
except:
return (None, None)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
values |
- | - | positional_or_keyword |
soft_range |
- | [] | positional_or_keyword |
Parameter Details
values: An iterable collection of values (list, numpy array, pandas series, etc.) from which to find the minimum and maximum. Can contain numeric values, datetime objects, or any sortable type. May contain NaN values which will be ignored in numerical computations.
soft_range: Optional list or array of values to concatenate with the input values before computing the range. Defaults to an empty list. This allows extending the computed range to include additional boundary values without modifying the original data.
Return Value
Returns a tuple of two elements: (minimum_value, maximum_value). For numeric data, returns the smallest and largest non-NaN values. For datetime data, returns the earliest and latest timestamps. If computation fails but values are sortable, returns the first and last sorted values. If all operations fail, returns (None, None).
Dependencies
numpywarnings
Required Imports
import numpy as np
import warnings
Usage Example
import numpy as np
import warnings
def find_range(values, soft_range=[]):
try:
values = np.array(values)
values = np.squeeze(values) if len(values.shape) > 1 else values
if len(soft_range):
values = np.concatenate([values, soft_range])
if values.dtype.kind == 'M':
return values.min(), values.max()
with warnings.catch_warnings():
warnings.filterwarnings('ignore', r'All-NaN (slice|axis) encountered')
return np.nanmin(values), np.nanmax(values)
except:
try:
values = sorted(values)
return (values[0], values[-1])
except:
return (None, None)
# Example 1: Basic numeric range
data = [1, 5, 3, 9, 2]
min_val, max_val = find_range(data)
print(f"Range: {min_val} to {max_val}") # Output: Range: 1 to 9
# Example 2: With NaN values
data_with_nan = [1, np.nan, 5, 3, np.nan, 9]
min_val, max_val = find_range(data_with_nan)
print(f"Range: {min_val} to {max_val}") # Output: Range: 1.0 to 9.0
# Example 3: With soft_range to extend bounds
data = [2, 3, 4]
min_val, max_val = find_range(data, soft_range=[0, 10])
print(f"Range: {min_val} to {max_val}") # Output: Range: 0 to 10
# Example 4: Datetime values
import datetime as dt
dates = [dt.datetime(2020, 1, 1), dt.datetime(2021, 6, 15), dt.datetime(2019, 3, 10)]
min_date, max_date = find_range(dates)
print(f"Date range: {min_date} to {max_date}")
Best Practices
- The function silently catches all exceptions, which may hide errors. Consider logging or handling specific exceptions in production code.
- The soft_range parameter should be a list or array-like object compatible with numpy.concatenate.
- NaN values are automatically ignored in numerical computations, which is useful for datasets with missing values.
- The function attempts multiple strategies (numpy operations, sorting, fallback to None), making it robust but potentially masking data quality issues.
- For datetime data (dtype.kind == 'M'), the function uses standard min/max instead of nanmin/nanmax.
- The function squeezes multi-dimensional arrays to 1D, which may not be appropriate for all use cases with structured data.
- Empty soft_range is checked with len() rather than truthiness to distinguish between empty list and None.
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function find_minmax 76.4% similar
-
function max_range 62.3% similar
-
function bound_range 61.8% similar
-
function dimension_range 56.3% similar
-
function max_extents 53.7% similar