NDCollection does not support the internal representation of `aligned_axes` in the constructor

Open
#846 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
55/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
numpy, python
Domain
data

Research direction

Start in ndcube/utils/collection.py, especially _sanitize_aligned_axes and _sanitize_user_aligned_axes, using the traceback as the entry point. Reproduce the constructor call with a dict aligned_axes and add coverage for this representation; done means NDCollection(new_items, aligned_axes=spice.aligned_axes) no longer raises KeyError while existing aligned_axes forms continue to work.

Written by the indexing model from the issue text.

Description

Bug
Describe the bug

While I am loathed to support more permutations of the aligned_axes= keyword argument seeing as we apparently already support nearly every conceivable option, the one we don't support is the one we use internally (the dict) representation.

>>> spice.aligned_axes
{'Mg IX 706 - Peak': (np.int64(1), np.int64(2)),
 'N IV 765 - Peak': (np.int64(1), np.int64(2)),
 'Ne VIII 770 - Peak': (np.int64(1), np.int64(2)),
 'Ly-gamma-CIII group (Merged)': (np.int64(1), np.int64(2)),
 'Ly Beta 1025 (Merged)': (np.int64(1), np.int64(2)),
 'O VI 1032 - Peak': (np.int64(1), np.int64(2))}
>>> new_collection = NDCollection(new_cubes, aligned_axes=spice.aligned_axes)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[23], line 9
      7     c = NDCube(cube, meta=dict(cube.meta))
      8     new_items.append((key, c))
----> 9 spice = NDCollection(new_items, aligned_axes=spice.aligned_axes)
     10 spice

File ~/.virtualenvs/hinode-talk/lib/python3.13/site-packages/ndcube/ndcollection.py:69, in NDCollection.__init__(self, key_data_pairs, aligned_axes, meta, **kwargs)
     67 # Sanitize aligned axes unless hidden kwarg indicates not to.
     68 if sanitize_inputs:
---> 69     aligned_axes = collection_utils._sanitize_aligned_axes(keys, data, aligned_axes)
     70 else:
     71     aligned_axes = dict(zip(keys, aligned_axes))

File ~/.virtualenvs/hinode-talk/lib/python3.13/site-packages/ndcube/utils/collection.py:23, in _sanitize_aligned_axes(keys, data, aligned_axes)
     20     sanitized_axes = tuple([tuple(range(len(cube0_dims)))] * len(data))
     21 else:
     22     # Else, sanitize user-supplied aligned axes.
---> 23     sanitized_axes = _sanitize_user_aligned_axes(data, aligned_axes)
     25 return dict(zip(keys, sanitized_axes))

File ~/.virtualenvs/hinode-talk/lib/python3.13/site-packages/ndcube/utils/collection.py:43, in _sanitize_user_aligned_axes(data, aligned_axes)
     40 aligned_axes_error_message = ("aligned_axes must contain ints or "
     41                               "a tuple of ints for each element in data.")
     42 if isinstance(data[0].shape, tuple):
---> 43     cube0_dims = np.array(data[0].shape, dtype=object)[np.array(aligned_axes[0])]
     44 else:
     45     cube0_dims = data[0].shape[np.array(aligned_axes[0])]

KeyError: 0
Dominant language
Python
Stars
49
Forks
56
Avg merge
5h 54m
Merged PRs (30d)
9

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sunpy/ndcube

All issues in sunpy/ndcube

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.