Skip to content

Commit 8e698b2

Browse files
committed
add zh doc vis
1 parent e6df249 commit 8e698b2

File tree

2 files changed

+343
-18
lines changed

2 files changed

+343
-18
lines changed

docs/source/zh/tutorial/vis_feature.rst

Lines changed: 189 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,198 @@
11
特征可视化
22
=========================
33

4-
即将上线!
4+
.. hint::
55

6-
.. Basic Usages
7-
.. ---------------
6+
- 作者: 唐青梅
7+
- 翻译:颜杰龙
8+
- 校对: `丰一帆 <https://fengyifan.site/>`_
89

9-
.. draw feature in Euclidean space, draw feature in manifold space
10+
Basic Usages
11+
---------------
12+
DHG provides an interface to visualize the distribution of feaures:
1013

11-
.. Make Animation
12-
.. -------------------------
14+
1. Input the features and lable (optional);
15+
2. Specify parameters (*i.e.*, `the dimensionality of the visualisation`, `point size`, `color` and `the method of dimensionality reduction`);
16+
3. Call ``plt.show()`` funtion to show the figure/animation.
1317

18+
19+
.. note:: The ``plt`` is short for ``matplotlib.pyplot`` module.
20+
21+
22+
Visualization of Features in Euclidean Space
23+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
25+
.. image:: ../_static/img/vis_ft_euclidean.png
26+
:align: center
27+
:alt: Visualization of Features in Euclidean Space
28+
:height: 400px
29+
30+
31+
.. code-block:: python
32+
33+
>>> import dhg
34+
>>> import numpy as np
35+
>>> import matplotlib.pyplot as plt
36+
>>> import dhg.visualization as vis
37+
>>> lbl = (np.random.rand(200)*10).astype(int)
38+
>>> ft = dhg.random.normal_features(lbl)
39+
>>> vis.draw_in_euclidean_space(ft, lbl)
40+
>>> plt.show()
41+
42+
43+
Visualization of Features in Poincare Space
44+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
45+
46+
.. image:: ../_static/img/vis_ft_poincare.png
47+
:align: center
48+
:alt: Visualization of Features in Poincare Space
49+
:height: 400px
50+
51+
52+
.. code-block:: python
53+
54+
>>> import dhg
55+
>>> import numpy as np
56+
>>> import matplotlib.pyplot as plt
57+
>>> import dhg.visualization as vis
58+
>>> lbl = (np.random.rand(200)*10).astype(int)
59+
>>> ft = dhg.random.normal_features(lbl)
60+
>>> vis.draw_in_poincare_space(ft, lbl)
61+
>>> plt.show()
62+
63+
64+
Make Animation
65+
-------------------------
66+
67+
We provide functions to make 3D rotation animation for feature visualization.
68+
69+
Rotating Visualization of Features in Euclidean Space
70+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
71+
72+
.. image:: ../_static/img/vis_ft_euclidean_ani.png
73+
:align: center
74+
:alt: Rotating Visualization of Features in Euclidean Space
75+
:height: 400px
76+
77+
78+
.. code-block:: python
79+
80+
>>> import dhg
81+
>>> import numpy as np
82+
>>> import matplotlib.pyplot as plt
83+
>>> import dhg.visualization as vis
84+
>>> lbl = (np.random.rand(200)*10).astype(int)
85+
>>> ft = dhg.random.normal_features(lbl)
86+
>>> vis.animation_of_3d_euclidean_space(ft, lbl)
87+
>>> plt.show()
88+
89+
..
90+
>>> import numpy as np
91+
>>> from dhg.visualization.feature import animation_of_3d_euclidean_ball
92+
>>> ile_dir = "data/modelnet40/test_img_feat_4.npy"
93+
>>> save_dir = None # None for show now or file name to save
94+
>>> label = np.load("data/modelnet40/test_label.npy")
95+
>>> ft = np.load(file_dir)
96+
>>> d = 3
97+
>>> low_demen_method = "tsne" # vis for poincare_ball: pca or tsne
98+
>>> show_method = "Rotation" # None for 2d or Rotation and Drag for 3d
99+
>>> animation_of_3d_euclidean_ball(
100+
ft, save_dir, d, label, reduce_method=low_demen_method, auto_play=show_method
101+
)
102+
103+
Rotating Visualization of Features in Poincare Space
104+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
105+
106+
.. image:: ../_static/img/vis_ft_poincare_ani.png
107+
:align: center
108+
:alt: Rotating Visualization of Features in Poincare Space
109+
:height: 400px
110+
111+
112+
.. code-block:: python
113+
114+
>>> import dhg
115+
>>> import numpy as np
116+
>>> import matplotlib.pyplot as plt
117+
>>> import dhg.visualization as vis
118+
>>> lbl = (np.random.rand(200)*10).astype(int)
119+
>>> ft = dhg.random.normal_features(lbl)
120+
>>> vis.animation_of_3d_poincare_ball(ft, lbl)
121+
>>> plt.show()
122+
123+
..
124+
>>> import numpy as np
125+
>>> from dhg.visualization.feature import animation_of_3d_poincare_ball
126+
>>> file_dir = "data/modelnet40/test_img_feat_4.npy" #This varies depending on the situation
127+
>>> save_dir = None # None for show now or file name to save
128+
>>> label = np.load("data/modelnet40/test_label.npy")
129+
>>> ft = np.load(file_dir)
130+
>>> d = 3
131+
>>> low_demen_method = "tsne" # vis for poincare_ball, pca or tsne
132+
>>> show_method = "Rotation" # None for 2d or Rotation and Drag for 3d
133+
>>> animation_of_3d_poincare_ball(
134+
ft, save_dir, d, label, reduce_method=low_demen_method, auto_play=show_method
135+
)
136+
137+
138+
139+
Mathamatical Principles of Hyperbolic Space
140+
--------------------------------------------------
141+
142+
The hyperbolic space is a manifold with constant Gaussian constant negative curvature everywhere,
143+
which has several models. We base our work on the Poincaré ball model for its well-suited for gradient-based optimization.
144+
145+
The Poincaré ball model with constant negative curvature :math:`-1 / k(k>0)` corresponds to the
146+
Riemannian manifold
147+
:math:`\left(\mathbb{P}^{n,k}, g_{\mathbf{x}}^{\mathbb{P}}\right)`.
148+
:math:`\mathbb{P}^{n,k} = \left\{\mathbf{x} \in \mathbb{R}^{n}: \| \mathbf{x}\|<1 \right\}` is an open :math:`n`-demensionsional unit ball,
149+
where :math:`\|. \|` denotes the Euclidean norm. Its metric tensor is :math:`g_{\mathbf{x}}^{\mathbb{P}} = \lambda_{\mathbf{x}}^{2} g^{E}`,
150+
where :math:`\lambda_{\mathbf{x}} = \frac{2} {1- k\|\mathbf{x}\|^{2} }` is the conformal factor and :math:`g^{E}=\mathbf{I}_{n}` is the Euclidean metric tensor.
151+
For two points :math:`\mathbf{x}, \mathbf{y} \in \mathbb{P}^{n,k}`, we ues the Möbius addition :math:`\oplus` operate adding
152+
by connecting the gyrospace framework with Riemannian geometry:
153+
154+
.. math::
155+
156+
\mathbf{x} \oplus_{k} \mathbf{y} =\frac{\left(1+2k\langle\mathbf{x}, \mathbf{y}\rangle+k\|\mathbf{y}\|^{2}\right) \mathbf{x}+\left(1-k\|\mathbf{x}\|^{2}\right) \mathbf{y}}{1+2k\langle\mathbf{x}, \mathbf{y}\rangle+k^{2}\|\mathbf{x}\|^{2}\|\mathbf{y}\|^{2}} .
157+
158+
The distance between two points :math:`\mathbf{x}, \mathbf{y} \in \mathbb{P}^{n,k}` is calculated by integration of the metric tensor, which is given as:
159+
160+
.. math::
161+
162+
d_{\mathbb{P}}^{k} (\mathbf{x}, \mathbf{y}) = (2 / \sqrt{K}) \tanh ^{-1}\left(\sqrt{k}\left\|-x \oplus_{k} y\right\|\right) .
163+
164+
165+
Denote point :math:`\mathbf{z} \in \mathcal{T}_{\mathrm{x}} \mathbb{P}^{n,k}` the tangent (Euclidean) space centered at any point :math:`\mathbf{x}` in the hyperbolic space.
166+
For the tangent vector :math:`\mathbf{z} \neq \mathbf{0}` and the point :math:`\mathbf{y} \neq \mathbf{0}`,
167+
the exponential map :math:`\exp _{\mathbf{x}}: \mathcal{T}_{\mathbf{x}} \mathbb{P}^{n,k} \rightarrow \mathbb{P}^{n,k}` and
168+
the logarithmic map :math:`\log_{\mathbf{x}}: \mathbb{P}^{n,k} \rightarrow \mathcal{T}_{\mathbf{x}} \mathbb{P}^{n,k}` are given for
169+
:math:`\mathbf{y} \neq \mathbf{x}` by:
170+
171+
.. math::
172+
173+
\exp _{\mathbf{x}}^{k}(\mathbf{z})=\mathbf{x} \oplus_{k}\left(\tanh \left(\sqrt{k} \frac{\lambda_{\mathbf{x}}^{k}\|\mathbf{z}\|}{2}\right) \frac{\mathbf{z}}{\sqrt{k}\|\mathbf{z}\|}\right),
174+
175+
and
176+
177+
.. math::
178+
179+
\log _{\mathbf{x}}^{k}(\mathbf{y})=\frac{2}{\sqrt{k} \lambda_{\mathbf{x}}^{k}} \tanh ^{-1}\left(\sqrt{k}\left\|-\mathbf{x} \oplus_{k} \mathbf{y}\right\|\right) \frac{-\mathbf{x} \oplus_{k} \mathbf{y}}{\left\|-\mathbf{x} \oplus_{k} \mathbf{y}\right\|} .
180+
181+
It is noted that our initial data are on Euclidean space and need to be converted to embeddings on hyperbolic space, so first project the data on the previously obtained Euclidean space onto the hyperbolic manifold space
182+
in order to use the Spectral-based hypergraph hyperbolic convolutional network to learn the information to update the node embeddings.
183+
Set :math:`t:=\{\sqrt{K}, 0, 0, \dots, 0\}\in\mathbb{P}^{d, K}` as a reference point to perform tangent space operations,
184+
where :math:`-1/K` is the negative curvature of hyperbolic model.
185+
The above premise makes :math:`\langle(0, \mathbf{x}^{0, E}), t\rangle=0` hold,
186+
so :math:`(0, \mathbf{x}^{0, E})` can be regarded as the initial embedding representation of the hypergraph structure on the tangent plane
187+
of the hyperbolic manifold space :math:`\mathcal{T}_t\mathbb{P}^{d, K}`. The initial hypergraph structure embedding is
188+
then mapped onto the hyperbolic manifold space :math:`\mathbb{P}` using the following equation:
189+
190+
.. math::
191+
192+
\mathbf{x}^{0, \mathbb{P}} &=\exp _{t}^{K}\left(\left(0, \mathbf{x}^{0, \mathrm{E}}\right)\right) \\
193+
&=\left(\sqrt{K} \cosh \left(\frac{\left\|\mathbf{x}^{0, \mathbb{E}}\right\|_{2}}{\sqrt{K}}\right),
194+
\sqrt{K} \sinh \left(\frac{\left\|\mathbf{x}^{0, \mathbb{E}}\right\|_{2}}{\sqrt{K}}\right) \frac{\mathbf{x}^{0, \mathbb{E}}}{\left\|\mathbf{x}^{0, \mathbb{E}}\right\|_{2}}\right).
195+
196+
The hyperbolic operation is accomplished by means of a feature mapping between Euclidean space and Hyperbolic space.
14197

15-
.. Mathamatical Principles
16-
.. ---------------------------
17198

18-
.. hyperbolic space

docs/source/zh/tutorial/vis_structure.rst

Lines changed: 154 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,163 @@
11
关联结构可视化
22
=============================
33

4-
即将上线!
4+
.. hint::
55

6-
.. Basic Usages
7-
.. --------------
8-
.. init a structure, call the draw method, and plt.show(), and plt.savefig().
6+
- 作者: 张欣炜
7+
- 翻译:颜杰龙
8+
- 校对: `丰一帆 <https://fengyifan.site/>`_
99

10+
Basic Usages
11+
--------------
12+
DHG provides a simple interface to visualize the correlation structures:
13+
14+
1. Create a structure object (*i.e.*, :py:class:`dhg.Graph`, :py:class:`dhg.BiGraph`, :py:class:`dhg.DiGraph`, and :py:class:`dhg.Hypergraph`);
15+
2. Call the ``draw()`` method of the object;
16+
3. Call ``plt.show()`` to show the figure or ``plt.savefig()`` to save the figure.
17+
18+
.. note:: The ``plt`` is short for ``matplotlib.pyplot`` module.
19+
20+
21+
Visualization of Graph
22+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23+
24+
.. image:: ../_static/img/vis_graph.png
25+
:align: center
26+
:alt: Visualization of Graph
27+
:height: 400px
28+
29+
30+
.. code-block:: python
31+
32+
>>> import matplotlib.pyplot as plt
33+
>>> from dhg.random import graph_Gnm
34+
>>> g = graph_Gnm(10, 12)
35+
>>> g.draw()
36+
>>> plt.show()
37+
38+
39+
Visualization of Directed Graph
40+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
41+
42+
.. image:: ../_static/img/vis_digraph.png
43+
:align: center
44+
:alt: Visualization of Directed Graph
45+
:height: 400px
46+
47+
.. code-block:: python
48+
49+
>>> import matplotlib.pyplot as plt
50+
>>> from dhg.random import digraph_Gnm
51+
>>> g = digraph_Gnm(12, 18)
52+
>>> g.draw()
53+
>>> plt.show()
54+
55+
56+
Visualization of Bipartite Graph
57+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
58+
59+
60+
.. image:: ../_static/img/vis_bigraph.png
61+
:align: center
62+
:alt: Visualization of Bipartite Graph
63+
:height: 400px
64+
65+
.. code-block:: python
66+
67+
>>> import matplotlib.pyplot as plt
68+
>>> from dhg.random import bigraph_Gnm
69+
>>> g = bigraph_Gnm(30, 40, 20)
70+
>>> g.draw()
71+
>>> plt.show()
72+
73+
74+
Visualization of Hypergraph
75+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
76+
77+
.. image:: ../_static/img/vis_hypergraph.png
78+
:align: center
79+
:alt: Visualization of Hypergraph
80+
:height: 400px
81+
82+
.. code-block:: python
83+
84+
>>> import matplotlib.pyplot as plt
85+
>>> from dhg.random import hypergraph_Gnm
86+
>>> hg = hypergraph_Gnm(10, 8, method='low_order_first')
87+
>>> hg.draw()
88+
>>> plt.show()
89+
90+
91+
92+
Advanced Usages
93+
---------------------
94+
95+
Customize Labels
96+
^^^^^^^^^^^^^^^^^^^^^^^^^
97+
The labels of the vertices could be customized by the ``v_label`` argument. The ``v_label`` could be a list of strings. The labels of the vertices are the strings in the list.
98+
For example, the following code shows how to customize the labels of the vertices of a graph.
99+
If the ``v_label`` is not specified, no labels will be shown in the figure.
100+
The ``font_size`` argument for ``dhg.Graph``, ``dhg.DiGraph``, and ``dhg.Hypergraph``, as well as ``u_font_size`` and ``v_font_size`` for ``dhg.BiGraph`` is used to specify the relative size of the font of the labels, and the default value is ``1.0``.
101+
The ``font_family`` argument is used to specify the font family of the labels, and the default value is ``'sans-serif'``.
102+
103+
.. code-block:: python
104+
105+
>>> import matplotlib.pyplot as plt
106+
>>> from dhg.random import graph_Gnm
107+
>>> g = graph_Gnm(10, 12)
108+
>>> labels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
109+
>>> g.draw(v_label=labels, font_size=1.5, font_family='serif')
110+
>>> plt.show()
111+
112+
.. image:: ../_static/img/custom_label.png
113+
:align: center
114+
:alt: Customize label
115+
:height: 400px
116+
117+
118+
Customize Colors
119+
^^^^^^^^^^^^^^^^^^^^^^^^^
120+
For ``dhg.Graph``, ``dhg.DiGraph``, and ``dhg.Hypergraph``, the colors of the vertices could be customized by the ``v_color`` argument and the colors of the edges could be customized by the ``e_color`` argument. While for ``dhg.BiGraph``, the colors of the vertices in :math:`\mathcal{U}` could be customized by the ``u_color`` argument and the colors of the vertices in :math:`\mathcal{V}` could be customized by the ``v_color`` argument. Both the ``v_color``, ``u_color``, and ``e_color`` could be a string or list of strings. If a string is provided, all the vertices or edges will be colored by the string. If a list of strings is provided, the colors of the vertices or edges are the strings in the list. For example, the following code shows how to customize the colors of the vertices and edges of a hypergraph.
121+
122+
.. code-block:: python
123+
124+
>>> import matplotlib.pyplot as plt
125+
>>> from dhg.random import hypergraph_Gnm
126+
>>> hg = hypergraph_Gnm(10, 8, method='low_order_first')
127+
>>> hg.draw(v_color='cyan', e_color='grey')
128+
>>> plt.show()
129+
130+
.. image:: ../_static/img/custom_color.png
131+
:align: center
132+
:alt: Customize color
133+
:height: 400px
134+
135+
136+
Customize Sizes
137+
^^^^^^^^^^^^^^^^^^^^^^^^^
138+
For ``dhg.Graph``, ``dhg.DiGraph``, and ``dhg.Hypergraph``, the sizes of the vertices could be customized by the ``v_size`` argument and the sizes of the edges could be customized by the ``e_size`` argument. While for ``dhg.BiGraph``, the sizes of the vertices in :math:`\mathcal{U}` could be customized by the ``u_size`` argument and the sizes of the vertices in :math:`\mathcal{V}` could be customized by the ``v_size`` argument. Both the ``v_size``, ``u_size``, and ``e_size`` could be a float or list of float. If a float is provided, all the vertices or edges will be sized by the float. If a list of floats is provided, the sizes of the vertices or edges are the floats in the list. ``v_line_width`` represents the width of the surrounding line of the vertices. ``e_line_width`` represents the width of the line of the edges.
139+
All of the sizes above represent the relative size, and the default values are ``1.0``. For example, the following code shows how to customize the sizes of the vertices and edges of a hypergraph.
140+
141+
.. code-block:: python
142+
143+
>>> import matplotlib.pyplot as plt
144+
>>> from dhg.random import graph_Gnm
145+
>>> g = graph_Gnm(10, 12)
146+
>>> g.draw(v_size=1.5, v_line_width=1.5, e_line_width=1.5)
147+
>>> plt.show()
148+
149+
.. image:: ../_static/img/custom_size.png
150+
:align: center
151+
:alt: Customize size
152+
:height: 400px
153+
154+
155+
156+
Customize Layout
157+
^^^^^^^^^^^^^^^^^^^^^^^^^
158+
The layout of the structures is based on the modified directed-force layout algorithm. There are four forces to determine the position of the nodes, *i.e.*, node attraction force :math:`f_{na}`, node repulsion force :math:`f_{nr}`, edge repulsion force :math:`f_{er}`, and the center force :math:`f_c`. :math:`f_{na}` is the spring force which attracts the adjacent nodes. :math:`f_{nr}` is used to repel the nodes from each other. :math:`f_{er}` is used to repel the hyperedges from each other, which only make sense for hypergraph visualization. :math:`f_c` is used to attract the nodes to the center (two centers for the bipartite graphs).
159+
The strength of the forces could be customized by ``forces`` argument, which is a dictionary with the keys ``Simulator.NODE_ATTRACTION``, ``Simulator.NODE_REPULSION``, ``Simulator.EDGE_REPULSION``, and ``Simulator.CENTER_GRAVITY``. The default values of the forces are ``1.0``.
10160

11-
.. Advanced Usages
12-
.. ---------------------
13161

14162
.. different style, change size, change color, change opacity
15163
@@ -28,6 +176,3 @@
28176
29177
.. Hypergraph
30178
.. ~~~~~~~~~~~~~~~~~~
31-
32-
33-

0 commit comments

Comments
 (0)