Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 12, 2025

WebGL data hashing failed on NumPy ≥2.2 because ndarray.tostring() was removed, causing WebGL view initialization to crash when hashing brain data arrays.

  • Hashing fix: Coerce inputs to NumPy arrays and hash via tobytes() in braindata._hash to restore compatibility with modern NumPy.
  • Regression coverage: Added a focused test in cortex/tests/test_braindata.py that asserts _hash matches hashlib.sha1(array.tobytes()) for NumPy arrays.

Example:

import numpy as np
from cortex.dataset.braindata import _hash

arr = np.arange(12, dtype=np.float32).reshape(3, 4)
print(_hash(arr))  # hashes using arr.tobytes()
Original prompt

This section details on the original issue you should resolve

<issue_title>numpy.ndarray has no attribute 'tostring' when using webgl</issue_title>
<issue_description>Hi,
I encountered this error while attempting to use the WebGL view with Pycortex v1.2.11 and NumPy 2.3.4:

  File "/home/ftravi/Documents/Projects/fmri-context/decoding/plot_rois.py", line 30, in <module>
    webgl.show(vol, with_colorbar=True, linewidth=4, thick=1, with_curvature=True, roi_list=ROIS_TO_PLOT, open_browser=True)
  File "/home/ftravi/.local/lib/python3.12/site-packages/cortex/utils.py", line 40, in __call__
    return self._load()(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ftravi/.local/lib/python3.12/site-packages/cortex/webgl/view.py", line 399, in show
    package = Package(data)
              ^^^^^^^^^^^^^
  File "/home/ftravi/.local/lib/python3.12/site-packages/cortex/webgl/data.py", line 24, in __init__
    self.uniques = list(data.uniques(collapse=True))
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ftravi/.local/lib/python3.12/site-packages/cortex/dataset/dataset.py", line 120, in uniques
    uniques.add(sv)
  File "/home/ftravi/.local/lib/python3.12/site-packages/cortex/dataset/braindata.py", line 63, in __hash__
    return hash(_hash(self.data))
                ^^^^^^^^^^^^^^^^
  File "/home/ftravi/.local/lib/python3.12/site-packages/cortex/dataset/braindata.py", line 631, in _hash
    return hashlib.sha1(array.tostring()).hexdigest()
                        ^^^^^^^^^^^^^^
AttributeError: 'numpy.ndarray' object has no attribute 'tostring'

As stated in the traceback, it occurs in line 631 of dataset/braindata.py.

Minimal code to reproduce the error:

  • Have Numpy > 2.2 installed
import cortex
vol = cortex.Volume.random(subject='S1', xfmname='fullhead')
cortex.webshow(vol)

Quick fix:
Simply replacing line 631 in dataset/braindata.py from:
return hashlib.sha1(array.tostring()).hexdigest()
to:
return hashlib.sha1(array.tobytes()).hexdigest()

If needed, I can open a pull request with this fix.

Thank you!</issue_description>

<agent_instructions>Make sure to check other places that have .tostring() and ensure compatibility with recent numpy versions.</agent_instructions>

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


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: mvdoc <6150554+mvdoc@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix numpy.ndarray attribute error in WebGL usage Handle NumPy 2.x hash compatibility for WebGL data Dec 12, 2025
Copilot AI requested a review from mvdoc December 12, 2025 22:58
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename this to test_braindata.py; hopefully we can add more tests in the future

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to test_braindata.py in bfb0e22 to keep room for additional cases.

Co-authored-by: mvdoc <6150554+mvdoc@users.noreply.github.com>
Copilot AI requested a review from mvdoc December 12, 2025 23:11
@mvdoc mvdoc marked this pull request as ready for review December 12, 2025 23:16
Copilot AI review requested due to automatic review settings December 12, 2025 23:16
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a compatibility issue with NumPy ≥2.2 where ndarray.tostring() was removed, causing WebGL view initialization to crash when hashing brain data arrays.

  • Updated _hash() function to use tobytes() instead of the deprecated tostring() method
  • Added np.asarray() coercion to ensure inputs are converted to NumPy arrays before hashing
  • Added regression test to verify correct hashing behavior

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
cortex/dataset/braindata.py Fixed _hash() function to use tobytes() and added array coercion with np.asarray() for NumPy 2.x compatibility
cortex/tests/test_braindata.py Added test to verify _hash() correctly uses tobytes() and matches expected SHA1 hash output

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mvdoc mvdoc merged commit d17c5c1 into main Dec 12, 2025
36 checks passed
@mvdoc mvdoc deleted the copilot/fix-numpy-attribute-error branch December 12, 2025 23:29
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.

numpy.ndarray has no attribute 'tostring' when using webgl

2 participants