Skip to content

Commit

Permalink
Fixing things in drawing functions (#476)
Browse files Browse the repository at this point in the history
* clean: removed unused cmap argument

* fix: settings can now be changed with single values

* removed as not needed anymore

* feat: len(stat) #455

* feat ! : rewrite of draw_dihypgraph

* style: cleaning

* fix: tests

* fix: test

* tests: more

* docstring: better

* feat: add iterations argument to layout

* tuto: new indepth

* reran notebook

* reran notebook

* review reply - removed unused lines

* response to review
  • Loading branch information
maximelucas authored Oct 25, 2023
1 parent 16e39f6 commit 84187d6
Show file tree
Hide file tree
Showing 9 changed files with 1,117 additions and 228 deletions.
6 changes: 3 additions & 3 deletions docs/source/api/tutorials/In Depth 1 - Drawing nodes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"id": "2f6ce3a3",
"metadata": {},
"source": [
"# In depth: drawing nodes\n",
"# In Depth 1 - Drawing nodes\n",
"\n",
"Here we show the fuctionalities and parameters of `xgi.draw_nodes()`. It is similar to the [networkx](https://networkx.org/documentation/stable/reference/drawing.html) corresponding function (+ some bonus) and heavily relies on [matplotlib](https://matplotlib.org/)'s scatter function."
]
Expand Down Expand Up @@ -533,7 +533,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "base",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -547,7 +547,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.9.16"
},
"toc": {
"base_numbering": 1,
Expand Down
705 changes: 705 additions & 0 deletions docs/source/api/tutorials/In Depth 3 - Drawing DiHypergraphs.ipynb

Large diffs are not rendered by default.

45 changes: 23 additions & 22 deletions docs/source/api/tutorials/Tutorial 8 - Directed Hypergraphs.ipynb

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions docs/source/api/tutorials/in_depth.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
##################
In depth tutorials
In Depth tutorials
##################

.. toctree::
:maxdepth: 1

In Depth 1 - Drawing nodes
In Depth 2 - Drawing hyperedges
In Depth 2 - Drawing hyperedges
In Depth 3 - Drawing DiHypergraphs
94 changes: 72 additions & 22 deletions tests/drawing/test_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,30 +461,81 @@ def test_correct_number_of_collections_draw_multilayer(edgelist8):
def test_draw_dihypergraph(diedgelist2, edgelist8):
DH = xgi.DiHypergraph(diedgelist2)

fig, ax1 = plt.subplots()
ax1 = xgi.draw_dihypergraph(DH, ax=ax1)
fig1, ax1 = plt.subplots()
ax1, collections = xgi.draw_dihypergraph(DH, ax=ax1)
node_coll, phantom_node_coll = collections
fig2, ax2 = plt.subplots()
ax2, collections2 = xgi.draw_dihypergraph(
DH,
ax=ax2,
node_fc="red",
node_ec="blue",
node_lw=2,
node_size=20,
lines_fc="blue",
lines_lw=2,
edge_marker_fc="red",
edge_marker_lw=2,
edge_marker_size=20,
)
node_coll2, phantom_node_coll2 = collections2

# number of elements
assert len(ax1.lines) == 7 # number of source nodes
assert len(ax1.patches) == 4 # number of target nodes
assert len(ax1.collections) == DH.num_edges + 1 - len(
DH.edges.filterby("size", 1)
) # hyperedges markers + nodes
assert len(node_coll.get_offsets()) == 6 # number of original nodes
assert len(phantom_node_coll.get_offsets()) == 3 # number of original edges
assert len(ax1.patches) == 11 # number of lines

# node face colors
assert np.all(node_coll.get_facecolor() == np.array([[1, 1, 1, 1]])) # white
assert np.all(node_coll2.get_facecolor() == np.array([[1, 0, 0, 1]])) # red

# node edge colors
assert np.all(node_coll.get_edgecolor() == np.array([[0, 0, 0, 1]])) # black
assert np.all(node_coll2.get_edgecolor() == np.array([[0, 0, 1, 1]])) # blue

# node_lw
assert np.all(node_coll.get_linewidth() == np.array([1]))
assert np.all(node_coll2.get_linewidth() == np.array([2]))

# node_size
assert np.all(node_coll.get_sizes() == np.array([15**2]))
assert np.all(node_coll2.get_sizes() == np.array([20**2]))

# edge face colors
assert np.all(phantom_node_coll2.get_facecolor() == np.array([[1, 0, 0, 1]])) # red

# edge _lw
assert np.all(phantom_node_coll.get_linewidth() == np.array([1]))
assert np.all(phantom_node_coll2.get_linewidth() == np.array([2]))

# edge_size
assert np.all(phantom_node_coll.get_sizes() == np.array([15**2]))
assert np.all(phantom_node_coll2.get_sizes() == np.array([20**2]))

# line lw
for patch in ax1.patches: # lines
assert np.all(patch.get_linewidth() == np.array([1.5]))
for patch in ax2.patches: # lines
assert np.all(patch.get_linewidth() == np.array([2]))

# line fc
for patch in ax2.patches: # lines
assert np.all(patch.get_facecolor() == np.array([[0, 0, 1, 1]]))

# zorder
for line, z in zip(ax1.lines, [1, 1, 1, 1, 0, 0, 0]): # lines for source nodes
assert line.get_zorder() == z
for patch, z in zip(ax1.patches, [1, 1, 0, 0]): # arrows for target nodes
assert patch.get_zorder() == z
for collection in ax1.collections:
assert collection.get_zorder() == 3 # nodes and hyperedges markers
assert node_coll.get_zorder() == 4
assert phantom_node_coll.get_zorder() == 2
for patch in ax1.patches: # lines
assert patch.get_zorder() == 0

plt.close()
plt.close("all")

# test toggle for edges
fig, ax2 = plt.subplots()
ax2 = xgi.draw_dihypergraph(DH, edge_marker_toggle=False, ax=ax2)
ax2, collections = xgi.draw_dihypergraph(DH, edge_marker_toggle=False, ax=ax2)
node_coll, phantom_node_coll = collections
assert len(ax2.collections) == 1
assert phantom_node_coll is None

plt.close()

Expand All @@ -498,18 +549,17 @@ def test_draw_dihypergraph(diedgelist2, edgelist8):

def test_draw_dihypergraph_with_str_labels_and_isolated_nodes():
DH1 = xgi.DiHypergraph()
DH1.add_nodes_from(["one", "two", "three", "four", "five", "six"])
DH1.add_edges_from(
[
[{"one"}, {"two", "three"}],
[{"two", "three"}, {"four", "five"}],
[{"six"}, {}],
]
)

fig, ax4 = plt.subplots()
ax4 = xgi.draw_dihypergraph(DH1, ax=ax4)
assert len(ax4.lines) == 3
assert len(ax4.patches) == 4
assert len(ax4.collections) == DH1.num_edges + 1 - len(
DH1.edges.filterby("size", 1)
)
ax4, collections4 = xgi.draw_dihypergraph(DH1, ax=ax4)
node_coll4, phantom_node_coll4 = collections4
assert len(node_coll4.get_offsets()) == 6 # number of original nodes
assert len(phantom_node_coll4.get_offsets()) == 2 # number of original edges
assert len(ax4.patches) == 7 # number of lines
Loading

0 comments on commit 84187d6

Please sign in to comment.