Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 28, 2025

Variable supports comparison with scalar numbers (sc.scalar(2) == 2), but DataArray and Dataset return False instead of performing element-wise comparison. This inconsistency breaks the expected behavior where sc.DataArray(sc.scalar(2)) == 2 should return a DataArray with boolean result.

Changes

lib/python/dataset.cpp

  • Added bind_comparison_scalars(c) to DataArray bindings (line 111)
  • Added comparison bindings for Dataset (lines 280-283): bind_comparison<Dataset>, bind_comparison<DataArray>, bind_comparison<Variable>, and bind_comparison_scalars

tests/core/comparison_test.py

  • Added test coverage for DataArray and Dataset comparison operators (==, !=, <, <=, >, >=) with scalar numbers

Example

import scipp as sc

# Before: returns False
# After: returns DataArray with data=Variable(True)
sc.DataArray(sc.scalar(2)) == 2

# Before: returns False  
# After: returns Dataset with 'a': DataArray with data=Variable(True)
sc.Dataset({'a': sc.scalar(2)}) == 2

The implementation converts scalars to Variables via the existing bind_comparison_scalars mechanism, then uses the already-supported Variable comparison operators.

Original prompt

This section details on the original issue you should resolve

<issue_title>Inconsistent eq for variable and data array with number</issue_title>
<issue_description>The equality comparison operator behaves differently for variables and data arrays:

import scipp as sc

print(sc.scalar(2) == 2)
# <scipp.Variable> ()       bool        <no unit>  True

print(sc.DataArray(sc.scalar(2)) == 2)
# False

print(sc.Dataset({'a': sc.scalar(2)}) == 2)
# False

print((sc.DataGroup({'a': sc.scalar(2)}) == 2)['a'])
# <scipp.Variable> ()       bool        <no unit>  True

I would expect the 2nd and 3rd example to work like the first. Especially, since sc.DataArray(sc.scalar(2)) == sc.scalar(2) returns a data array containing True.

Looking at the code, I think we are simply missing some overloads.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits October 28, 2025 18:31
Co-authored-by: MridulS <5363860+MridulS@users.noreply.github.com>
Co-authored-by: MridulS <5363860+MridulS@users.noreply.github.com>
Co-authored-by: MridulS <5363860+MridulS@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix inconsistent __eq__ behavior for variable and data array Add scalar comparison operators for DataArray and Dataset Oct 28, 2025
Copilot AI requested a review from MridulS October 28, 2025 18:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent __eq__ for variable and data array with number

2 participants