ScalarDistribution behaves weirdly · Issue #20455 · astropy/astropy · GitHub
Skip to content

ScalarDistribution behaves weirdly #20455

Open

Description

@mhvk

Description

astropy.uncertainty.Distribution has an associated ScalarDistribution type that is used for single elements (with still multiple samples). It was noted in numpy/numpy#32744 (review) that indexing this scalar with ... returns an array, not a distribution.

The docstring of ScalarDistribution notes that it exists mostly to help make array2print possible for all subclasses. We perhaps should revisit that, since for other ndarray subclasses (Masked, Quantity), we do not have specific scalar types.

Alternatively (but less preferred), we need to make sure the indexing works correctly.

Expected behavior

d[...] should return a Distribution independent of whether d is scalar or array.

How to Reproduce

# create scalar
s = Distribution(np.arange(12.).reshape(3, 4))[0]
s
# astropy.uncertainty.core.ScalarDistribution(([(0.0,), (1.0,), (2.0,), (3.0,)],), dtype=|V8)
# tried to get back to array, expecting Distribution
s[...]
# array(([(0.,), (1.,), (2.,), (3.,)],),
#       dtype=(astropy.uncertainty.core.ScalarDistribution, [('samples', [('sample', '<f8')], (4,))]))

Activity

  1. pllim commented on Sep 22, 2026

    @pllim
    Member

    Looks like @eteq is a co-maintainer of uncertainty along with you, so maybe he can comment also? Thanks!

  2. mhvk commented on Sep 22, 2026

    @mhvk
    ContributorAuthor

    Indeed, though I'm pretty sure I'm the one to blame for introducing ScalarDistribution...

  3. Onkar-Raskar commented on Oct 15, 2026

    @Onkar-Raskar

    @mhvk , I would like to work on issue. I have reproduced the bug locally. my curent plan is to remove scalarDistribution completely and see how test suite behaves around array2print, can i start working on issue ?

    Image
  4. mhvk commented on Oct 18, 2026

    @mhvk
    ContributorAuthor

    @Onkar-Raskar - we do not assign issues. I think this one will require some fairly detailed digging in to how array2print works.

  5. Onkar-Raskar commented on Oct 21, 2026

    @Onkar-Raskar

    @mhvk , Itried both the approaches mentioned by you on removing ScalarDistribution class the test breaks at 6 test in distribution.py file giving Valueerror and indexerror, we will need to fix it 2nd on adding handler in ScalarDistribution.getitem the bug got sucessfully fixed and it didn't even break on any test.
    so, what would you suggest should i go with approach 1 or 2 ?

    Image
  6. mhvk commented on Oct 21, 2026

    @mhvk
    ContributorAuthor

    @Onkar-Raskar - I'd prefer getting rid of ScalarDistribution eventually, but I'm OK with having a work-around for now of just introducing __getitem__

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions