pycortex icon indicating copy to clipboard operation
pycortex copied to clipboard

add_cutdata seems to overwrite data for left hemisphere with data from right hemisphere.

Open N-HEDGER opened this issue 4 years ago • 1 comments

Hi pycortex. I seem to have uncovered an issue that doesn't seem to have been raised so far. It overlaps somewhat with https://github.com/gallantlab/pycortex/issues/398

Problem

To illustrate this problem clearly, here I add some data that should show blue for the left hemisphere and red for the right hemisphere.

hemdat=np.repeat([0,1], 10242) # 10242 vertices in fsaverage5

hemdatv=cortex.Vertex(hemdat,subject='fsaverage5',vmin=0,vmax=1,cmap='BuBkRd')
cortex.webshow(hemdatv)

Here is the proof, in cortex.webshow() image (80)

However. When I call the cut_surface command, specifying the left hemisphere:

my_retinotopy=cortex.Dataset()
my_retinotopy.views['hem']=hemdatv
my_retinotopy.description='my_retinotopy'  
cortex.segment.cut_surface(cx_subject='fsaverage5',hemi='lh',name='retinotopy_flat',fs_subject='fsaverage5',data=my_retinotopy)

we see that the left hemisphere is coloured red, not blue as in the webviewer.

Screenshot 2021-09-15 at 11 49 29

Culprit lines

My guess is that the problem lies here:

https://github.com/gallantlab/pycortex/blob/499965a9145bc42b100a7c1bdffdb12a964fcba2/cortex/blender/init.py#L97-L100

Here, despite the fact that we are only cutting the left hemisphere, it seems as though data for both hemispheres are added to blender. Presumably this is what causes the right hemispheres data to appear on the left (perhaps the left data is over-written by the right data).

Proposed solution.

  1. in segment.py, hemi could be added as an argument to add_cutdata, as follows

https://github.com/gallantlab/pycortex/blob/499965a9145bc42b100a7c1bdffdb12a964fcba2/cortex/segment.py#L185

blender.add_cutdata(fname, data,hemi, name=data.description)
  1. The hem input can then be added accordingly in add_cutdata itself.

a. https://github.com/gallantlab/pycortex/blob/499965a9145bc42b100a7c1bdffdb12a964fcba2/cortex/blender/init.py#L43

def add_cutdata(fname, braindata,hemi, name="retinotopy", projection="nearest", mesh="hemi"):

b. https://github.com/gallantlab/pycortex/blob/499965a9145bc42b100a7c1bdffdb12a964fcba2/cortex/blender/init.py#L65

add_cutdata(fname, data,hemi, name=view_name, projection=projection, mesh=mesh)
  1. Then, we replace the end of the add_cutdata function

https://github.com/gallantlab/pycortex/blob/499965a9145bc42b100a7c1bdffdb12a964fcba2/cortex/blender/init.py#L86-L104

with something like this, to only add one set of data depending on the hemi argument.

    if hemi=='lh':
        dat2add=lcolor
    else:
        dat2add=rcolor

    p.pack_array(dat2add.ravel(), p.pack_double)


    with tempfile.NamedTemporaryFile() as tf:
        tf.write(p.get_buffer())
        tf.flush()
        code = """with open('{tfname}', 'rb') as fp:
            u = xdrlib.Unpacker(fp.read())
            mesh = u.unpack_string().decode('utf-8')
            name = u.unpack_string().decode('utf-8')
            hemdat= u.unpack_array(u.unpack_double)
            ccolor = blendlib._repack(hemdat)
            blendlib.add_vcolor(ccolor, mesh, name)
        """.format(tfname=tf.name)

        print(code)
        _call_blender(fname, code)

    return 

This seems to produce the desired result.

Screenshot 2021-09-15 at 12 11 35 Screenshot 2021-09-15 at 12 12 26

Since the code that makes python talk to blender is a little confusing to me - these are tentative suggestions, but they seem to do the job.

Happy to address this myself - but any comments welcome.

N-HEDGER avatar Sep 15 '21 10:09 N-HEDGER

Pardon the late reply here - the root issue is that no one in the original pycortex development team ever tried to make cuts based on anything besides volumetric functional localizer (mostly retinotopy) data, so this is all good-enough code. These fixes seem reasonable; I'd suggest you submit a pull request and we can test them out. Thanks!

marklescroart avatar Sep 24 '21 07:09 marklescroart