Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
N-HEDGER opened this issue Sep 15, 2021 · 1 comment

Comments

@N-HEDGER
Copy link

N-HEDGER commented Sep 15, 2021

Hi pycortex. I seem to have uncovered an issue that doesn't seem to have been raised so far. It overlaps somewhat with #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:

lcolor = blendlib._repack(left)
rcolor = blendlib._repack(right)
print(len(lcolor), len(rcolor))
blendlib.add_vcolor((lcolor, rcolor), mesh, name)

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

blender.add_cutdata(fname, data, name=data.description)

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

a.

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

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

b.

add_cutdata(fname, data, name=view_name, projection=projection, mesh=mesh)

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

p.pack_array(lcolor.ravel(), p.pack_double)
p.pack_array(rcolor.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')
left = u.unpack_array(u.unpack_double)
right = u.unpack_array(u.unpack_double)
lcolor = blendlib._repack(left)
rcolor = blendlib._repack(right)
print(len(lcolor), len(rcolor))
blendlib.add_vcolor((lcolor, rcolor), mesh, name)
""".format(tfname=tf.name)
_call_blender(fname, code)
return

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.

@marklescroart
Copy link
Contributor

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!

sumiyaabdi added a commit to sumiyaabdi/pycortex that referenced this issue Apr 22, 2022
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

No branches or pull requests

2 participants