🔍 Code Extractor

function find_range

Maturity: 46

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.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
920 - 941
Complexity:
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

  • numpy
  • warnings

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.

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function find_minmax 76.4% similar

    Computes the minimum of the first elements and maximum of the second elements from two tuples of numeric values, handling NaN values gracefully.

    From: /tf/active/vicechatdev/patches/util.py
  • function max_range 62.3% similar

    Computes the maximal lower and upper bounds from a list of range tuples, handling various data types including numeric, datetime, and string values.

    From: /tf/active/vicechatdev/patches/util.py
  • function bound_range 61.8% similar

    Computes a bounding range and density from evenly spaced samples, extending the range by half the density on each side and detecting if values are inverted.

    From: /tf/active/vicechatdev/patches/util.py
  • function dimension_range 56.3% similar

    Computes the effective range along a dimension by combining data bounds with soft and hard range constraints, optionally applying padding and logarithmic scaling.

    From: /tf/active/vicechatdev/patches/util.py
  • function max_extents 53.7% similar

    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.

    From: /tf/active/vicechatdev/patches/util.py
← Back to Browse