Skip to content

NDCollection does not support the internal representation of aligned_axes in the constructor #846

Open

Description

@Cadair

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

Activity

  1. Cadair commented on Jun 11, 2025

    @Cadair
    SponsorMemberAuthor

    The solution is to use tuple(spice.aligned_axes.values()) which is obtuse.

  2. DanRyanIrish commented on Jun 11, 2025

    @DanRyanIrish
    Member

    Adding this support sounds reasonable to me.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugProbably a bug.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions