Skip to content

Commit cc32ef6

Browse files
authored
Merge pull request #676 from xgi-org/speedup-imports
Speedup imports
2 parents a962e0e + 990a365 commit cc32ef6

11 files changed

Lines changed: 55 additions & 40 deletions

File tree

HOW_TO_CONTRIBUTE.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ Please note we have a [code of conduct](/CODE_OF_CONDUCT.md), please follow it i
2424
2. notebooks by running `nbqa isort .` to sort any new import statements in the source code and tutorials.
2525
4. Format the source code and notebooks by running `black .` for consistent styling.
2626

27+
## Profile importing XGI
28+
29+
Install the `test` dependencis and visualize the import times using [tuna](https://github.com/nschloe/tuna):
30+
```python
31+
python3 -X importtime -c "import xgi" 2> test.log
32+
tuna test.log
33+
```
34+
2735
## New Version process
2836

2937
1. Make sure that the Github Actions workflow runs without any errors.

examples/advanced/plot_colormaps_colorbars.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
node_fc=H.nodes.degree,
2525
edge_fc=H.edges.size,
2626
edge_fc_cmap="viridis",
27-
node_fc_cmap="mako_r",
27+
node_fc_cmap="plasma",
2828
)
2929

3030
node_col, _, edge_col = collections

examples/stats/plot_color_stat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
pos=pos,
2121
node_fc=H.nodes.degree,
2222
edge_fc="grey",
23-
node_fc_cmap="mako_r",
23+
node_fc_cmap="plasma",
2424
)
2525

2626
node_col, _, edge_col = collections

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ dependencies = [
4848
"networkx>=2.7",
4949
"requests>=2.0",
5050
"matplotlib>=3.4",
51-
"seaborn>=0.10",
5251
]
5352

5453
[project.urls]
@@ -91,10 +90,12 @@ release = [
9190
test = [
9291
"pytest>=7.2",
9392
"pytest-cov>=4.0",
93+
"tuna",
9494
]
9595
tutorial = [
9696
"jupyter>=1.0",
9797
"ipython<=8.12.0",
98+
"seaborn>=0.10"
9899
]
99100
all = ["xgi[benchmark,developer,docs,release,test,tutorial]"]
100101

requirements/default.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ pandas>=1.3
66
networkx>=2.7
77
requests>=2.0
88
matplotlib>=3.4
9-
seaborn>=0.10

requirements/test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
# Do not edit this file; modify pyproject.toml instead.
33
pytest>=7.2
44
pytest-cov>=4.0
5+
tuna

tests/drawing/test_draw.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import matplotlib.pyplot as plt
44
import numpy as np
55
import pytest
6-
import seaborn as sb
76

87
import xgi
98
from xgi.exception import XGIError
@@ -295,7 +294,7 @@ def test_draw_hyperedges_fc_cmap(edgelist8):
295294
ax, collections = xgi.draw_hyperedges(H, ax=ax)
296295
(dyad_collection, edge_collection) = collections
297296
assert dyad_collection.get_cmap() == plt.cm.Greys
298-
assert edge_collection.get_cmap() == sb.color_palette("crest_r", as_cmap=True)
297+
assert edge_collection.get_cmap() == xgi.crest_r()
299298
plt.close("all")
300299

301300
# set cmap

tutorials/focus/Tutorial 5 - Plotting.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@
387387
" node_fc=H.nodes.degree,\n",
388388
" edge_fc=H.edges.size,\n",
389389
" edge_fc_cmap=\"viridis\",\n",
390-
" node_fc_cmap=\"mako_r\",\n",
390+
" node_fc_cmap=\"plasma\",\n",
391391
")\n",
392392
"\n",
393393
"node_col, _, edge_col = collections\n",

xgi/drawing/draw.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import matplotlib as mpl
77
import matplotlib.pyplot as plt
88
import numpy as np
9-
import seaborn as sb # This cannot be removed because it is used for cmap "crest"
109
from matplotlib import cm
1110
from matplotlib.colors import is_color_like
1211
from matplotlib.patches import FancyArrowPatch
@@ -25,7 +24,7 @@
2524
from ..convert import to_bipartite_edgelist
2625
from ..core import DiHypergraph, Hypergraph, SimplicialComplex
2726
from ..exception import XGIError
28-
from ..utils import subfaces
27+
from ..utils import crest_r, subfaces
2928
from .draw_utils import (
3029
_CCW_sort,
3130
_draw_arg_to_arr,
@@ -70,7 +69,7 @@ def draw(
7069
dyad_vmin=None,
7170
dyad_vmax=None,
7271
edge_fc=None,
73-
edge_fc_cmap="crest_r",
72+
edge_fc_cmap=crest_r(),
7473
edge_vmin=None,
7574
edge_vmax=None,
7675
edge_ec=None,
@@ -298,7 +297,6 @@ def draw(
298297
)
299298

300299
elif isinstance(H, Hypergraph):
301-
302300
ax, (dyad_collection, edge_collection) = draw_hyperedges(
303301
H=H,
304302
pos=pos,
@@ -510,8 +508,8 @@ def draw_nodes(
510508

511509
# plot
512510
node_collection = ax.scatter(
513-
x=xy[:, 0],
514-
y=xy[:, 1],
511+
xy[:, 0],
512+
xy[:, 1],
515513
s=node_size,
516514
marker=node_shape,
517515
c=node_fc,
@@ -552,7 +550,7 @@ def draw_hyperedges(
552550
dyad_vmin=None,
553551
dyad_vmax=None,
554552
edge_fc=None,
555-
edge_fc_cmap="crest_r",
553+
edge_fc_cmap=crest_r(),
556554
edge_vmin=None,
557555
edge_vmax=None,
558556
edge_ec=None,
@@ -614,7 +612,7 @@ def draw_hyperedges(
614612
615613
If None (default), color by edge size.
616614
edge_fc_cmap: matplotlib colormap
617-
Colormap used to map the edge colors. By default, "crest_r".
615+
Colormap used to map the edge colors. By default, crest_r().
618616
edge_vmin, edge_vmax : float, optional
619617
Minimum and maximum for edge colormap scaling. By default, None.
620618
edge_ec : color or list of colors or array-like or dict or EdgeStat, optional
@@ -775,7 +773,6 @@ def draw_hyperedges(
775773
edge_ec = edge_ec[ids_sorted] if len(edge_ec) > 1 else edge_ec # reorder
776774

777775
if edge_ec_to_map: # edgecolors need to be manually mapped
778-
779776
# create scalarmappable to map floats to colors
780777
# we use the same vmin, vmax, and cmap as for edge_fc
781778
norm = mpl.colors.Normalize(vmin=edge_vmin, vmax=edge_vmax)
@@ -849,7 +846,7 @@ def draw_simplices(
849846
dyad_vmin=None,
850847
dyad_vmax=None,
851848
edge_fc=None,
852-
edge_fc_cmap="crest_r",
849+
edge_fc_cmap=crest_r(),
853850
edge_vmin=None,
854851
edge_vmax=None,
855852
alpha=0.4,
@@ -908,7 +905,7 @@ def draw_simplices(
908905
909906
If None (default), color by edge size.
910907
edge_fc_cmap: matplotlib colormap
911-
Colormap used to map the edge colors. By default, "crest_r".
908+
Colormap used to map the edge colors. By default, crest_r().
912909
edge_vmin, edge_vmax : float, optional
913910
Minimum and maximum for edge colormap scaling. By default, None.
914911
alpha : float, optional
@@ -1259,12 +1256,12 @@ def draw_multilayer(
12591256
dyad_lw=1.5,
12601257
dyad_style="solid",
12611258
edge_fc=None,
1262-
edge_fc_cmap="crest_r",
1259+
edge_fc_cmap=crest_r(),
12631260
edge_vmin=None,
12641261
edge_vmax=None,
12651262
alpha=0.4,
12661263
layer_color="grey",
1267-
layer_cmap="crest_r",
1264+
layer_cmap=crest_r(),
12681265
max_order=None,
12691266
conn_lines=True,
12701267
conn_lines_style="dotted",
@@ -1347,7 +1344,7 @@ def draw_multilayer(
13471344
13481345
If None (default), color by edge size.
13491346
edge_fc_cmap: matplotlib colormap, optional
1350-
Colormap used to map the edge colors. By default, "crest_r".
1347+
Colormap used to map the edge colors. By default, crest_r().
13511348
edge_vmin, edge_vmax : float, optional
13521349
Minimum and maximum for edge colormap scaling. By default, None.
13531350
alpha : float, optional
@@ -1356,7 +1353,7 @@ def draw_multilayer(
13561353
Color of layers. By default, "grey".
13571354
layer_cmap : colormap, optional
13581355
Colormap to use in case of numerical values in layer_color. Ignored if layer_color
1359-
does not contain numerical values to be mapped. By default, "crest_r", but ignored.
1356+
does not contain numerical values to be mapped. By default, crest_r(), but ignored.
13601357
max_order : int, optional
13611358
Maximum of hyperedges to plot. If None (default), plots all orders.
13621359
conn_lines : bool, optional
@@ -1485,7 +1482,6 @@ def draw_multilayer(
14851482

14861483
# plot layers
14871484
for jj, d in enumerate(orders):
1488-
14891485
z = [sep * d] * H.num_nodes
14901486

14911487
# draw surfaces corresponding to the different orders
@@ -1585,7 +1581,6 @@ def draw_multilayer(
15851581

15861582
# draw nodes (last)
15871583
for d in orders:
1588-
15891584
z = [sep * d] * H.num_nodes
15901585

15911586
node_collection = ax.scatter(
@@ -1629,12 +1624,12 @@ def draw_bipartite(
16291624
edge_marker_lw=1,
16301625
edge_marker_size=7,
16311626
edge_marker_shape="s",
1632-
edge_marker_fc_cmap="crest_r",
1627+
edge_marker_fc_cmap=crest_r(),
16331628
max_order=None,
16341629
dyad_color=None,
16351630
dyad_lw=1,
16361631
dyad_style="solid",
1637-
dyad_color_cmap="crest_r",
1632+
dyad_color_cmap=crest_r(),
16381633
node_labels=None,
16391634
hyperedge_labels=None,
16401635
arrowsize=10,
@@ -1952,7 +1947,7 @@ def draw_undirected_dyads(
19521947
dyad_color=None,
19531948
dyad_lw=1,
19541949
dyad_style="solid",
1955-
dyad_color_cmap="crest_r",
1950+
dyad_color_cmap=crest_r(),
19561951
rescale_sizes=True,
19571952
**kwargs,
19581953
):
@@ -2114,7 +2109,7 @@ def draw_directed_dyads(
21142109
dyad_color=None,
21152110
dyad_lw=1,
21162111
dyad_style="solid",
2117-
dyad_color_cmap="crest_r",
2112+
dyad_color_cmap=crest_r(),
21182113
arrowsize=10,
21192114
arrowstyle="->",
21202115
connectionstyle="arc3",
@@ -2336,7 +2331,6 @@ def to_marker_edge(marker_size, marker):
23362331
ems = edge_marker_size
23372332

23382333
for n in tail: # lines going towards the center
2339-
23402334
xy_source = node_pos[n]
23412335
xy_target = edge_pos[e]
23422336

xgi/utils/tensor.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
## Tensor times same vector in all but one (TTSV1) and all but two (TTSV2)
2+
import math
23
from collections import defaultdict
34
from itertools import combinations
4-
from math import factorial
55

66
import numpy as np
7-
from numpy import prod
8-
from scipy.signal import convolve
9-
from scipy.sparse import coo_array
10-
from scipy.special import binom as binomial
117

128
__all__ = [
139
"pairwise_incidence",
@@ -78,9 +74,10 @@ def banerjee_coeff(size, max_size):
7874
Sinan Aksoy, Ilya Amburg, Stephen Young,
7975
https://doi.org/10.1137/23M1584472
8076
"""
77+
from scipy.special import binom
78+
8179
return sum(
82-
((-1) ** j) * binomial(size, j) * (size - j) ** max_size
83-
for j in range(size + 1)
80+
((-1) ** j) * binom(size, j) * (size - j) ** max_size for j in range(size + 1)
8481
)
8582

8683

@@ -119,15 +116,15 @@ def ttsv1(node_dict, edge_dict, r, a):
119116
"""
120117
n = len(node_dict)
121118
s = np.zeros(n)
122-
r_minus_1_factorial = factorial(r - 1)
119+
r_minus_1_factorial = math.factorial(r - 1)
123120
for node, edges in node_dict.items():
124121
c = 0
125122
for e in edges:
126123
l = len(edge_dict[e])
127124
alpha = banerjee_coeff(l, r)
128125
edge_without_node = [v for v in edge_dict[e] if v != node]
129126
if l == r:
130-
gen_fun_coef = prod(a[edge_without_node])
127+
gen_fun_coef = np.prod(a[edge_without_node])
131128
elif 2 ** (l - 1) < r * (l - 1):
132129
gen_fun_coef = _get_gen_coef_subset_expansion(
133130
a[edge_without_node], a[node], r - 1
@@ -175,8 +172,11 @@ def ttsv2(pair_dict, edge_dict, r, a, n):
175172
Sinan Aksoy, Ilya Amburg, Stephen Young,
176173
https://doi.org/10.1137/23M1584472
177174
"""
175+
from scipy.signal import convolve
176+
from scipy.sparse import coo_array
177+
178178
s = {}
179-
r_minus_2_factorial = factorial(r - 2)
179+
r_minus_2_factorial = math.factorial(r - 2)
180180
for (v1, v2), edges in pair_dict.items():
181181
c = 0
182182
for e in edges:
@@ -282,7 +282,7 @@ def _get_gen_coef_subset_expansion(edge_values, node_value, r):
282282
for i in range(len(subset_lengths))
283283
]
284284
)
285-
return total / factorial(r)
285+
return total / math.factorial(r)
286286

287287

288288
def _get_gen_coef_fft_fast_array(edge_without_node, a, node, l, r):
@@ -317,6 +317,8 @@ def _get_gen_coef_fft_fast_array(edge_without_node, a, node, l, r):
317317
Sinan Aksoy, Ilya Amburg, Stephen Young,
318318
https://doi.org/10.1137/23M1584472
319319
"""
320+
from scipy.signal import convolve
321+
320322
coefs = [1]
321323
for i in range(1, r):
322324
coefs.append(coefs[-1] * a[node] / i)

0 commit comments

Comments
 (0)