Skip to content

Commit 4e766ee

Browse files
committedFeb 6, 2025
final figures
1 parent cb7929e commit 4e766ee

File tree

3 files changed

+446
-228
lines changed

3 files changed

+446
-228
lines changed
 

‎bimod-figures-func.ipynb

+347-221
Large diffs are not rendered by default.

‎bimod_plots.py

+94-5
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,67 @@ def draw_self_loop(
105105
return axes
106106

107107

108+
def draw_smaller_self_loop(
109+
axes: Axes,
110+
posx: float,
111+
posy: float,
112+
size: float,
113+
rad: float = 0.4,
114+
offset: float = 0.01,
115+
mutation_scale: int = 20,
116+
onearrow: bool = True,
117+
) -> Axes:
118+
119+
all_arrows_pos = [
120+
(posx, posy, posx, posy - size, rad),
121+
(posx - offset, posy - size + offset, posx + size, posy - size, rad),
122+
(posx + size - offset, posy - size - offset, posx + size, posy, rad),
123+
]
124+
125+
all_arrows_pos = [
126+
(posx, posy, posx, posy - size, rad),
127+
(
128+
posx - offset,
129+
posy - size + offset,
130+
posx + size + offset,
131+
posy - size + offset,
132+
rad,
133+
),
134+
(posx + size, posy - size, posx + size, posy, rad),
135+
]
136+
137+
for pos_i, arr_pos in enumerate(all_arrows_pos):
138+
px, py, sx, sy, rad = arr_pos
139+
140+
style = "-"
141+
if onearrow:
142+
if pos_i == len(all_arrows_pos) - 1:
143+
style = "-|>"
144+
elif pos_i % 2:
145+
style = "-|>"
146+
147+
arrow = FancyArrowPatch(
148+
(px, py),
149+
(sx, sy),
150+
arrowstyle=style,
151+
mutation_scale=mutation_scale,
152+
linewidth=2,
153+
color="k",
154+
connectionstyle=f"arc3,rad={rad}",
155+
)
156+
157+
axes.add_patch(arrow)
158+
159+
return axes
160+
161+
108162
def plot_community_scheme(
109163
ax: Optional[Axes] = None,
110164
use_cmap: bool = True,
111165
title_letter: str = "",
112166
fontscale: float = 1.2,
113167
com_names: Optional[np.ndarray] = None,
168+
com_names_yoffset: float = 0,
114169
x_names: [str, str] = ["Sending 1", "Sending 2"],
115170
y_names: [str, str] = ["Receiving 1", "Receiving 2"],
116171
arrow_colors: Optional[np.ndarray] = None,
@@ -186,7 +241,8 @@ def plot_community_scheme(
186241

187242
for pos, com_name in zip(xy_pos, com_names):
188243
ax.text(
189-
*pos,
244+
pos[0],
245+
pos[1] + com_names_yoffset,
190246
com_name,
191247
fontsize=24 * fontscale,
192248
fontweight="bold",
@@ -386,10 +442,13 @@ def plot_spectrum(
386442
markeredgecolor="k",
387443
markeredgewidth=4,
388444
)
445+
angle = (V.T @ U)[vector_id, vector_id]
389446
axes[0].text(
390447
x_id + 1,
391448
S[vector_id],
392-
f"$\\mu_{{{x_id+1}}}={S[vector_id]:2.2f}, R^2={r_squared:1.2f}$",
449+
# f"$\\mu_{{{x_id+1}}}={S[vector_id]:2.2f}, R^2={r_squared:1.2f}$",
450+
# f"$\\mu_{{{x_id+1}}}={S[vector_id]:2.2f},\,\\mathbf{{v}}_{{{vector_id+1}}}^{{T}}\\mathbf{{u}}_{{{vector_id+1}}}={angle:1.2f}$",
451+
f"$\\mu_{{{x_id+1}}}={S[vector_id]:2.2f}$\n$\\mathbf{{v}}_{{{vector_id+1}}}^{{T}}\\mathbf{{u}}_{{{vector_id+1}}}={angle:1.2f}$",
393452
# f"$s_{{{x_id}}}={S[vector_id]:2.2f}, R^2={r_squared:1.2f}$",
394453
fontsize=18 * fontscale,
395454
ha="left",
@@ -511,6 +570,7 @@ def plot_graph_embedding(
511570
ax: Optional[Axes] = None,
512571
use_cmap: bool = True,
513572
cmap: str = "plasma",
573+
static_color: str = "tab:blue",
514574
write_label: bool = False,
515575
write_var: bool = False,
516576
label_lw: int = 3,
@@ -556,7 +616,7 @@ def plot_graph_embedding(
556616
except ValueError:
557617
colors = [cmap] * n_nodes
558618
else:
559-
colors = "tab:blue"
619+
colors = static_color
560620

561621
ax.scatter(
562622
U[:, vector_id],
@@ -576,6 +636,29 @@ def plot_graph_embedding(
576636
f"$R^2={expl_var:1.3f}$",
577637
transform=ax.transAxes,
578638
fontsize=16 * fontscale,
639+
path_effects=[
640+
withStroke(
641+
linewidth=label_lw,
642+
foreground="w",
643+
alpha=0.8,
644+
)
645+
],
646+
)
647+
648+
angle = (V.T @ U)[vector_id, vector_id]
649+
ax.text(
650+
0.05,
651+
0.85,
652+
f"$\\mathbf{{v}}^T\\mathbf{{u}}={angle:1.2f}$",
653+
transform=ax.transAxes,
654+
fontsize=16 * fontscale,
655+
path_effects=[
656+
withStroke(
657+
linewidth=label_lw,
658+
foreground="w",
659+
alpha=0.8,
660+
)
661+
],
579662
)
580663

581664
if write_label:
@@ -756,17 +839,20 @@ def plot_all_bicommunity(
756839
scatter_only=False,
757840
titles=None,
758841
nrows=1,
842+
ncols=None,
759843
draw_legend=True,
760844
legend_on_ax=False,
761845
cmap=None,
762846
gspec_wspace=0,
763847
gspec_hspace=0.05,
764848
**kwargs,
765849
):
850+
if ncols is None:
851+
ncols = len(send_com) // nrows + (len(send_com) % nrows)
766852

767853
com_gs = GridSpecFromSubplotSpec(
768854
nrows=nrows,
769-
ncols=len(send_com) // nrows + (len(send_com) % nrows),
855+
ncols=ncols,
770856
# ncols=len(send_com) // nrows,
771857
subplot_spec=axes,
772858
wspace=gspec_wspace,
@@ -782,7 +868,10 @@ def plot_all_bicommunity(
782868
U, _, Vh = dgsp.sorted_SVD(dgsp.modularity_matrix(adjacency))
783869
V = Vh.T
784870

785-
graph_pos = {i: (U[i, 0], V[i, 0]) for i, _ in enumerate(adjacency)}
871+
if np.allclose(adjacency, adjacency.T):
872+
graph_pos = {i: (U[i, 0], V[i, 1]) for i, _ in enumerate(adjacency)}
873+
else:
874+
graph_pos = {i: (U[i, 0], V[i, 0]) for i, _ in enumerate(adjacency)}
786875
else:
787876
graph_pos = nx.spring_layout(nx.DiGraph(adjacency))
788877

‎dgsp.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,11 @@ def edge_bicommunities(
9999
if scale_S is None:
100100
scale_S = np.ones(n_components)
101101

102-
u_features = U[:, :n_components] * np.sqrt(scale_S)
103-
v_features = V[:, :n_components] * np.sqrt(scale_S)
102+
# u_features = U[:, :n_components] * np.sqrt(scale_S)
103+
# v_features = V[:, :n_components] * np.sqrt(scale_S)
104+
105+
u_features = U[:, :n_components] * scale_S
106+
v_features = V[:, :n_components] * scale_S
104107

105108
if method in ["partition", "sign"]:
106109
u_features = np.sign(u_features).astype(int)

0 commit comments

Comments
 (0)
Please sign in to comment.