🔍 Code Extractor

function compute_edges

Maturity: 44

Converts bin centers to bin edges by computing midpoints between consecutive centers and extrapolating equidistant boundaries at the extremes.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
2189 - 2200
Complexity:
simple

Purpose

This function is used to compute histogram or binning edges from bin centers. Given a sequence of bin centers, it calculates the edges by finding midpoints between consecutive centers, and creates first and last boundaries that are equidistant from the first and last centers respectively. This is commonly used in data visualization and binning operations where you have bin centers but need to determine the actual bin boundaries.

Source Code

def compute_edges(edges):
    """
    Computes edges as midpoints of the bin centers.  The first and
    last boundaries are equidistant from the first and last midpoints
    respectively.
    """
    edges = np.asarray(edges)
    if edges.dtype.kind == 'i':
        edges = edges.astype('f')
    midpoints = (edges[:-1] + edges[1:])/2.0
    boundaries = (2*edges[0] - midpoints[0], 2*edges[-1] - midpoints[-1])
    return np.concatenate([boundaries[:1], midpoints, boundaries[-1:]])

Parameters

Name Type Default Kind
edges - - positional_or_keyword

Parameter Details

edges: Array-like sequence of bin centers (despite the parameter name 'edges', these are actually bin centers). Can be a list, tuple, or numpy array. If the input is an integer array, it will be converted to float for precise midpoint calculations. Must contain at least 2 elements for meaningful computation.

Return Value

Returns a numpy array of bin edges with length equal to len(edges) + 1. The array contains: [first_boundary, midpoint_1, midpoint_2, ..., midpoint_n-1, last_boundary], where first_boundary = 2*edges[0] - midpoint_1 and last_boundary = 2*edges[-1] - midpoint_n-1. The boundaries are extrapolated to maintain equal spacing at the extremes.

Dependencies

  • numpy

Required Imports

import numpy as np

Usage Example

import numpy as np

def compute_edges(edges):
    edges = np.asarray(edges)
    if edges.dtype.kind == 'i':
        edges = edges.astype('f')
    midpoints = (edges[:-1] + edges[1:])/2.0
    boundaries = (2*edges[0] - midpoints[0], 2*edges[-1] - midpoints[-1])
    return np.concatenate([boundaries[:1], midpoints, boundaries[-1:]])

# Example: Convert bin centers to bin edges
bin_centers = [1, 2, 3, 4, 5]
bin_edges = compute_edges(bin_centers)
print(bin_edges)
# Output: [0.5 1.5 2.5 3.5 4.5 5.5]

# Example with integer input (automatically converted to float)
bin_centers_int = np.array([10, 20, 30, 40])
bin_edges_int = compute_edges(bin_centers_int)
print(bin_edges_int)
# Output: [ 5. 15. 25. 35. 45.]

# Example with non-uniform spacing
bin_centers_nonuniform = [1.0, 2.5, 5.0, 10.0]
bin_edges_nonuniform = compute_edges(bin_centers_nonuniform)
print(bin_edges_nonuniform)
# Output: [-0.75  1.75  3.75  7.5  12.5 ]

Best Practices

  • Input should contain at least 2 elements for meaningful edge computation
  • The function automatically converts integer arrays to float to ensure precise midpoint calculations
  • The parameter is named 'edges' but actually expects bin centers as input - be mindful of this naming inconsistency
  • The returned array will always have one more element than the input array
  • The extrapolated boundaries assume uniform spacing at the extremes based on the first and last intervals
  • For non-uniform bin centers, the boundary extrapolation may not match expected behavior - verify results for irregular spacing

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function bound_range 50.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 find_minmax 45.0% 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_extents 44.0% 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 compute_density 42.9% similar

    Computes the density (samples per unit) of a grid given start and end boundaries and the number of samples, with special handling for datetime/timedelta types.

    From: /tf/active/vicechatdev/patches/util.py
  • function find_range 40.7% 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
← Back to Browse