🔍 Code Extractor

function find_minmax

Maturity: 46

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

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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function find_range 76.4% similar

    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.

    From: /tf/active/vicechatdev/patches/util.py
  • function max_range 64.7% 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 max_extents 57.1% 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
  • function bound_range 55.2% 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 49.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
← Back to Browse