Skip to content

fix: IndexError in get_topk_rois when indexing dict_keys with numpy#30

Open
Shubham0209 wants to merge 1 commit intofacebookresearch:mainfrom
Shubham0209:fix/get-topk-rois-dict-keys-indexerror
Open

fix: IndexError in get_topk_rois when indexing dict_keys with numpy#30
Shubham0209 wants to merge 1 commit intofacebookresearch:mainfrom
Shubham0209:fix/get-topk-rois-dict-keys-indexerror

Conversation

@Shubham0209
Copy link
Copy Markdown

What's the bug?

When calling get_topk_rois with the default hemi="both" (or any value other than "both_separate"), the function crashes with:

IndexError: too many indices for array: array is 0-dimensional, but 1 were indexed

Root cause

The else branch assigns labels from a raw .keys() call:

labels = get_hcp_labels(mesh=mesh, combine=False, hemi=hemi).keys()

dict.keys() returns a view object, not a sequence. When passed to np.array(), numpy wraps the entire object into a 0-dimensional array instead of a 1D string array. Fancy-indexing that 0-d array with top_k then raises the IndexError.

Interestingly, the both_separate branch right above it already handles this correctly by constructing a plain Python list — the else branch was just missing the same treatment.

Fix

One character change — wrap .keys() in list():

# before
labels = get_hcp_labels(mesh=mesh, combine=False, hemi=hemi).keys()

# after
labels = list(get_hcp_labels(mesh=mesh, combine=False, hemi=hemi).keys())

This gives numpy a proper 1D sequence it can convert to a string array and fancy-index correctly.

Testing

Verified on a real brain encoding run with both text and video stimuli using the default hemi="both". get_topk_rois now returns the correct top-k parcel names without error.

@meta-cla
Copy link
Copy Markdown

meta-cla bot commented Apr 6, 2026

Hi @Shubham0209!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Meta Open Source bot. label Apr 6, 2026
@meta-cla
Copy link
Copy Markdown

meta-cla bot commented Apr 6, 2026

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

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

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant