giotto-ai / giotto-tda

A high-performance topological machine learning toolbox in Python
https://giotto-ai.github.io/gtda-docs
Other
837 stars 173 forks source link

Can apply it to 3D graph? #566

Closed tanjia123456 closed 3 years ago

tanjia123456 commented 3 years ago

Hello, thank you for your works! I currently have three-dimensional node coordinates and triangle, so I can use the tri_ surf builds a graph, I want to use this library for topology data analysis of my data. My topic is to segment the brain by graph neural network. The goal is to get continuous two-dimensional code and Betti number to process the holes on the graph. Which examples are more suitable?

ulupo commented 3 years ago

Hi! I am not 100% sure I understand the question but tell me if this helps: Given a set of points in 3D (or in any number of dimensions in that matter) and a set of edges between these points which you can use to define a graph, you have a few options depending on what exactly you want to achieve and how much of the structure in the data you want to use.

Simplest: number of holes in the unweighted graph

Here you wouldn't use persistent homology or even giotto-tda at all! If all you care about is the number of holes in the graph G, then you can use the Euler-Poincaré formula as follows: since you only have a graph and no triangles (no "faces"), the formula says that

V - E = β0 - β1

where V is the total number of vertices, E is the total number of edges, β0 is the number of connected components (zeroth Betti number) and β1 is the number of holes (first Betti number). Since V and E are known, and there are many algorithms for computing β0 (see e.g. https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csgraph.connected_components.html), you can rearrange this formula to get

β1 = E - V + β0

Number of holes in a simplicial complex constructed from the graph

You can add triangles, tetrahedra, etc to the initial graph (which includes only vertices and edges) in several ways but arguably the simplest is the flag complex: whenever you have three vertices p, q and r and the (undirected) edges {p, q}, {p, r}, {q, r} all exist, then you declare that triangle {p, q, r} also exists! Now you are dealing with an object more general than a graph (a simplicial complex) and you may want to compute Betti numbers β0, β1 and β2, but the Euler-Poincaré formula is no longer sufficient to determine them. You need a real homology algorithm. You can achieve this with giotto-tda using ripser, and it will be very fast, but a bit indirect (I can explain if needed). The pyflagser library (https://github.com/giotto-ai/pyflagser), on the other hand, has a simpler API for this kind of thing. The function flagser_unweighted will return precisely the sequence of Betti numbers β0, β1 and β2. It is documented here.

Note. When you "complete" a graph using the flag complex method (with flagser_unweighted, for example), loops surrounding triangles in the graph no longer contribute to the count of holes, because in the flag complex construction we actually fill the loop with a two-dimensional triangle, so there is no hole.

Persistent homology version: persistence diagram of a filtered flag complex

Here, as above, you use the flag complex idea, but this time you apply it to thresholded versions of the graph, where you imagine that each edge comes with a weight. Once you have weights, you can use this. Euclidean distances between vertices could be weights, for example. giotto-tda is particularly suited for this. I recommend reading this tutorial if you are interested in this route.

Final remarks

I am not sure what you mean by "continuous two-dimensional code".

tanjia123456 commented 3 years ago

hello, thank you for your reply! Let me elaborate on my problem: I have vertice,edge,triangle(see:https://github.com/tanjia123456/Brain_area_PH/blob/main/lhvertices.txt https://github.com/tanjia123456/Brain_area_PH/blob/main/lhedges.txt https://github.com/tanjia123456/Brain_area_PH/blob/main/triangles.txt) which can use plt.tri_surf to create a graph(3D).

My research topic is to use graph neural network to segment the brain. I want to use TDA to optimize my segmentation results https://github.com/tanjia123456/Brain_ area PH/blob/main/ pred.png My real ground map is: https://github.com/tanjia123456/Brain area_ PH/blob/main/ GT.png For example, I want to remove the dark red areas of the brain.

I was going to use the gudhi library to deal with it. The author said that I would use the extended persistence graph to deal with it, but there was a problem.https://github.com/tanjia123456/Brain_area_PH/blob/main/compute_betti_gudhi3.py

SO, could you plaese help me solve my problem???

ulupo commented 3 years ago

Thanks for the clarification! At the moment, giotto-tda cannot deal with triangulation input of the kind you describe, so if this is extremely important to you then your best bet for now is indeed to use GUDHI for this.

However, if you are happy for giotto-tda to do the triangulation for you, then you may want to look at the WeakAlphaPersistence transformer here. It will contruct the Delaunay triangulation (using scipy) and then compute persistent homology of a simple filtered flag complex constructed from the triangulation. See https://giotto-ai.github.io/gtda-docs/0.4.0/modules/generated/homology/gtda.homology.WeakAlphaPersistence.html#gtda.homology.WeakAlphaPersistence. It is extremely fast.

ulupo commented 3 years ago

@tanjia123456 did my last message help?

tanjia123456 commented 3 years ago

Hi, your answer is helpful. I was going to have the code adjusted before I told you. Thank you very much for sharing.

------------------ 原始邮件 ------------------ 发件人: "giotto-ai/giotto-tda" @.>; 发送时间: 2021年3月17日(星期三) 凌晨0:03 @.>; @.**@.>; 主题: Re: [giotto-ai/giotto-tda] Can apply it to 3D graph? (#566)

@tanjia123456 did my last message help?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

ulupo commented 3 years ago

Closing due to inactivity, but feel free to reopen if you have news, @tanjia123456!