gallantlab / pycortex

Pycortex is a python-based toolkit for surface visualization of fMRI data
https://gallantlab.github.io/pycortex
BSD 2-Clause "Simplified" License
581 stars 137 forks source link

FIX avoid using jQuery for getting numpy arrays #544

Closed mvdoc closed 2 months ago

mvdoc commented 2 months ago

This was a nasty 🪲. To visualize a Vertex/VertexRGB object in a static viewer, the NParray.fromURL method in datamodel.js was using jQuery to fetch the numpy array with the data. However, jQuery by default serializes any type of data and so the binary buffer was automatically encoded as a string. This automatic serialization caused issues when a float32 array was saved (see below for an example.)

This PR completely rewrites the NParray.fromURL method to use an XMLHttpRequest instead of jQuery. In this way the buffer can be fetched as an arraybuffer, and all the portions of the buffer (header/data) can be decoded appropriately.

Example code

import cortex

vtx = cortex.Vertex.random("fsaverage")
cortex.webgl.make_static(
    outpath="test-viewer",
    data=vtx,
    recache=True,
)
python minimal-example-error.py && cd test-viewer && python -m http.server 23423

Before

image

After

image