Skip to content

[BUG]: interval() raises IndexError for single-element or empty input #1411

Description

@gaoflow

Description

colour.utilities.interval() (and the dependent is_uniform()) raise an
IndexError when passed a distribution with fewer than two elements (single
element or empty array).

Code for Reproduction

import numpy as np
from colour.utilities import interval, is_uniform

interval(np.array([1.0]))   # IndexError: index 0 is out of bounds for axis 0 with size 0
is_uniform(np.array([1.0])) # same error, via interval()

Exception Message

IndexError: index 0 is out of bounds for axis 0 with size 0

The crash happens at:

differences = np.abs(distribution[1:] - distribution[:-1])
if unique and np.all(differences == differences[0]):  # differences is empty -> crash

Fix

I have a fix on the branch gaoflow:fix/interval-single-element-indexerror — guard with if differences.size == 0 before accessing differences[0], returning the empty array directly. Also update is_uniform() to treat a zero-interval distribution as uniform (the correct mathematical interpretation for a single point or empty set).

Environment Information

colour-science 0.4.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions