Skip to content

focal_stats() does not validate stats_funcs before dispatch #2770

@brendancol

Description

@brendancol

Description

focal_stats() doesn't validate stats_funcs before it dispatches to a backend, so two bad inputs fail in confusing ways:

  1. An unknown stat name (e.g. stats_funcs=['mena']) falls through to a backend mapper dict lookup and surfaces as a bare KeyError. Nothing tells you which names are valid.
  2. A bare string instead of a list (e.g. stats_funcs='mean') gets iterated character by character, so the first lookup is for 'm' and it dies there.

The dispatch happens around xrspatial/focal.py:1115 in _focal_stats_cpu and the other backend helpers.

Expected behavior

Validate stats_funcs up front, before dispatch:

  • An unknown stat name raises a ValueError that lists the valid options.
  • A bare string is accepted as a single stat name (wrapped into a one-element list).

Steps to reproduce

import numpy as np, xarray as xr
from xrspatial.convolution import circle_kernel
from xrspatial.focal import focal_stats

agg = xr.DataArray(np.ones((5, 5), dtype=float))
kernel = circle_kernel(1, 1, 1)

focal_stats(agg, kernel, stats_funcs=['mena'])  # raw KeyError
focal_stats(agg, kernel, stats_funcs='mean')    # KeyError on 'm'

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfocal toolsFocal statistics and hotspot analysis

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions