the-virtual-brain / tvb-root

Main TVB codebase
https://thevirtualbrain.org
Other
122 stars 105 forks source link

Computation of Local Connectivity matrix with own cortex data #694

Open michefabb opened 1 year ago

michefabb commented 1 year ago

Describe the bug

Dear all, I am trying to use my own data for the cortex (not the default values), compute the local connectivity matrix with different parameters and save it to a matlab file. For the default cortex it was shown how to do it at (https://groups.google.com/g/tvb-users/c/cyHKATuXfTU/m/CI-H9_kKBgAJ) . In order to make it work with my data it was suggested at this post (https://groups.google.com/g/tvb-users/c/8RNS6cNxUzs) to use the class "surfaces.CorticalSurface" to load my cortex. So I am trying with the below code, derived from the link above, however I get an error 'CorticalSurface' object has no attribute 'local_connectivity'

Steps to reproduce

*%matplotlib widget import matplotlib.pyplot as plt import numpy from tvb.simulator.plot.head_plotter_3d import HeadPlotter3D from tvb.datatypes import surfaces, connectivity, local_connectivity from scipy import io as sio

ctx = surfaces.CorticalSurface.from_file("/sub-01_cortex.zip") loc_conn = local_connectivity.LocalConnectivity(cutoff=20.0, surface=ctx) loc_conn.equation.parameters['sigma'] = 10.0 loc_conn.equation.parameters['amp'] = 1.0 loc_conn.equation.parameters['midpoint'] = 0.0 loc_conn.equation.parameters['offset'] = 0.0 ctx.local_connectivity.matrix = None ctx.local_connectivity = loc_conn ctx.coupling_strength = numpy.array([0.0115]) ctx.configure() ctx.compute_local_connectivity() sio.savemat('local_connectivity'+ '.mat', {'local_connectivity': ctx.local_connectivity.matrix})*

Expected results

Compute the local connectivity matrix with my cortex and the parameters above and save the computed matrix to a file in matlab format

Actual results

See screenshot of code and error. Screenshot

Additional information

TVB2.7.2 MacOS12.6.8 I can provide the cortex.zip file if you provide an email

maedoc commented 1 year ago

Thanks for the code sample, the issue is that a CorticalSurface only represents the geometry of a surface and is actually an attribute of a LocalConnectivity instance and not vice versa. The LocalConnectivity instance should then become an attribute of the Cortex. With 10+ years hindsight we can say this is not the obvious way to do it, but once you are aware of TVB's hierarchical classes, you can just check the classes and see how to compose the pieces. Please give it a try, looking at the demos/classes and otherwise we can write it out.

michefabb commented 1 year ago

Dear Marmaduke, thank you for your reply, I am looking into the details and will revert asap!

michefabb commented 1 year ago

Dear @maedoc,

I worked with the above references as you suggested, to my eyes this look working, however I would kindly ask you if in your opinion the below is correct. I would also have some questions:

Steps to reproduce %matplotlib widget import matplotlib.pyplot as plt import numpy from tvb.datatypes import surfaces, connectivity, local_connectivity, equations, region_mapping from tvb.datatypes.cortex import Cortex from scipy import io as sio from tvb.simulator.lab import coupling

ctx_surface = surfaces.CorticalSurface.from_file("/TVB_output/sub-01_Cortex.zip") loc_conn = local_connectivity.LocalConnectivity(cutoff=20.0, surface=ctx_surface, matrix = None) loc_conn.equation.parameters['sigma'] = 10.0 loc_conn.equation.parameters['amp'] = 1.0 loc_conn.equation.parameters['midpoint'] = 0.0 loc_conn.equation.parameters['offset'] = 0.0

white_matter = connectivity.Connectivity.from_file('/TVB_output/sub-01_Connectome.zip') white_matter.speed = numpy.array([4.0]) white_matter_coupling = coupling.Difference(a=numpy.array([0.014]))

ctx= Cortex.from_file(region_mapping_file="/TVB_output/sub-01_region_mapping.txt") ctx.region_mapping_data.connectivity = white_matter ctx.region_mapping_data.surface = ctx_surface ctx.local_connectivity = loc_conn ctx.local_connectivity.matrix = None ctx.coupling_strength = numpy.array([0.0115]) ctx.compute_local_connectivity() ctx.configure()

ctx.local_connectivity.matrix = None #should not be needed as I stated matrix=None above

ctx.compute_local_connectivity() #should not be needed as I stated matrix=None above

sio.savemat('local_connectivity'+ '.mat', {'local_connectivity': ctx.local_connectivity.matrix})

from tvb.core.neocom import h5 from tvb.basic.profile import TvbProfile TvbProfile.set_profile(TvbProfile.COMMAND_PROFILE) h5.store_complete_to_dir(loc_conn, ".")

michefabb commented 1 year ago

A last question: could you help to clarify what the matrix n x m generated for the local connectivity represents exactly when imported in Matlab and made from sparse to full matrix?

maedoc commented 1 year ago

in case I run it more times, is the local connectivity matrix recomputed correctly every time (considering I set the parameter matrix=None)? or should I state "ctx.local_connectivity.matrix = None" and "ctx.compute_local_connectivity()" every time?

I think this should force recomputation yes; if it pauses a bit at that step then it's recomputing.

how can I give another equation (like Sigmoid) instead of the default Gaussian distribution (I did not find the correct coding ... equation=Sigmoid or equation=equations.Sigmoid did not work)?

a sigmoid is not a good connectivity kernel because it's usually not compact (ie. going to zero with radius quickly). you should look at the equations module for the other options. for the syntax it should be equations.Sigmoid() i.e. you call it.

I tried to save the local connectivity in .h5 format (see below code) and then upload in the TVB GUI version, however I get the attached error...did I something wrong?

good question but I defer to a colleague on it @liadomide can you look plz ?

could you help to clarify what the matrix n x m generated for the local connectivity represents exactly when imported in Matlab and made from sparse to full matrix?

it's the connectivity between the n vertices on the surface, due to lateral gray fiber connectivity along the cortical surface. please see Sanz Leon 2015 NeuroImage paper fro more details.

michefabb commented 1 year ago

Dear @maedoc ,

first of all let me thank for your help and kindness. I had a further look, the code above works if I load my own data but do not use my cortex: this made me double check my cortex surface (cortex.zip) that has been generated using the TVB Processing Pipeline "Convert2TVB" ( https://github.com/BrainModes/tvb-pipeline-converter/blob/master/convert2TVB_format.py ).

This script removes all the triangles and vertices that were generated by Freesurfer as pial surfaces but actually do not correspond to a cortical surface (lines 131-153 of the above referenced script), meaning they correspond to a point in space where a subcortical region or white matter (e.g. Corpus Callosum) are present.

Own_Cortex

This creates a warning also in the GUI (see attached)

GUI_Warning

Comparing it with the TVB default cortex, it looks quite evident that this latter is instead a closed surface for each hemisphere (also as shown in Sanz Leon 2015). TVB_Default_Cortex

I had a look also to the TVB Users Google Forum, finding the following discussion https://groups.google.com/g/tvb-users/c/Hs8ZmkmT59E/m/Ughj9ET4SCIJ , where a similar topic was treated. I was wondering if, in order to make the notebooks work correctly, I have to modify the script above to: 1- leave also the part of surfaces generated by Freesurfer (and currently removed by the script); 2- create a new dummy region in the Connectome Weights and Mean Tract Length (sort of "Non-cortical dummy surfaces region") assigning a value of 0 for both Weigths and Mean Length; 3- assign 0 to Areas.txt and an average position to Centres.txt; 4- assign the corresping value of the dummy region to the vertices in region_mapping.txt 5- assign a value of 0 to the EEGProjection.mat of these subcortical and white matter regions even if MNE provide a value different from zero (so that they do not contribute in the forward model to the EEG signal).

I would be grateful if you could suggest an answer to the below points:

I hope the above was not too much, and would be grateful if you could kindly reply, or point me to the proper direction.

Greatly appreciated, thanks again,

M

liadomide commented 1 year ago
  • I tried to save the local connectivity in .h5 format (see below code) and then upload in the TVB GUI version, however I get the attached error...did I something wrong? error

The error message suggests that you can not upload the LocalConnectivity alone in web GUI, but you need to export and upload the linked datatypes also. For this case, linked it would be the CorticalSurface object (same GUI are referred by the LocalConnectivity).