🔍 Code Extractor

function expand_grid_coords

Maturity: 42

Expands coordinates along a specified dimension of a gridded dataset into an N-dimensional array that matches the full dimensionality of the dataset.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
1980 - 1996
Complexity:
moderate

Purpose

This function is used to broadcast coordinates from a single dimension into a full N-dimensional grid structure. It handles both irregular grids (where coordinates vary along multiple dimensions) and regular grids (where coordinates can be computed via Cartesian product). This is commonly needed when working with gridded scientific data where you need coordinate arrays that match the shape of your data arrays.

Source Code

def expand_grid_coords(dataset, dim):
    """
    Expand the coordinates along a dimension of the gridded
    dataset into an ND-array matching the dimensionality of
    the dataset.
    """
    irregular = [d.name for d in dataset.kdims
                 if d is not dim and dataset.interface.irregular(dataset, d)]
    if irregular:
        array = dataset.interface.coords(dataset, dim, True)
        example = dataset.interface.values(dataset, irregular[0], True, False)
        return array * np.ones_like(example)
    else:
        arrays = [dataset.interface.coords(dataset, d.name, True)
                  for d in dataset.kdims]
        idx = dataset.get_dimension_index(dim)
        return cartesian_product(arrays, flat=False)[idx].T

Parameters

Name Type Default Kind
dataset - - positional_or_keyword
dim - - positional_or_keyword

Parameter Details

dataset: A gridded dataset object that has an interface attribute for accessing data, kdims (key dimensions) attribute listing dimensions, and methods like get_dimension_index(). Expected to follow a specific dataset interface pattern with methods for coords() and values() access.

dim: The dimension object or name whose coordinates should be expanded. This should be one of the dimensions present in dataset.kdims. The coordinates along this dimension will be broadcast to match the full dataset dimensionality.

Return Value

Returns a NumPy ndarray containing the expanded coordinates. For irregular grids, returns the coordinate array multiplied by an array of ones matching the shape of other irregular dimensions. For regular grids, returns the appropriate slice from a Cartesian product of all dimension coordinates, transposed to match expected orientation. The returned array has the same number of dimensions as the dataset.

Dependencies

  • numpy

Required Imports

import numpy as np

Usage Example

# Note: This function requires a specific dataset interface implementation
# Example assumes a HoloViews-like dataset structure

import numpy as np

# Assuming dataset is a gridded dataset object with proper interface
# and dim is one of its dimensions:

# For a 2D gridded dataset with x and y dimensions:
# expanded_x = expand_grid_coords(dataset, dataset.kdims[0])
# This would return a 2D array where x coordinates are repeated along y axis

# Simplified conceptual example of what the function does:
x_coords = np.array([1, 2, 3])
y_coords = np.array([10, 20])
# If expanding x dimension, result would be:
# [[1, 2, 3],
#  [1, 2, 3]]
# matching the 2D grid shape

Best Practices

  • This function is designed to work with a specific dataset interface (likely HoloViews or similar). Ensure your dataset object implements the required interface methods before calling.
  • The function handles two distinct cases: irregular grids (where coordinates vary non-uniformly) and regular grids (Cartesian product). Understanding which case applies to your data is important.
  • The cartesian_product function must be available in scope - this is likely defined elsewhere in the same module.
  • For irregular grids, the function assumes at least one irregular dimension exists and uses the first one as a template for broadcasting.
  • The returned array will have the same dtype as the input coordinates and should be memory-efficient for regular grids but may create large arrays for irregular grids.

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function cartesian_product 60.6% similar

    Computes the Cartesian product of multiple 1D arrays, returning expanded array views for each dimension with optional flattening and copying.

    From: /tf/active/vicechatdev/patches/util.py
  • class GridSpace 55.0% similar

    GridSpace is a container class for organizing elements in a 1D or 2D grid structure with floating-point keys, ensuring all contained elements are of the same type.

    From: /tf/active/vicechatdev/patches/spaces.py
  • class GridMatrix 52.9% similar

    GridMatrix is a container class for heterogeneous Element types arranged in a grid layout where axes don't represent coordinate space but are used to plot various dimensions against each other.

    From: /tf/active/vicechatdev/patches/spaces.py
  • function max_extents 47.3% 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 47.1% 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
← Back to Browse