From ebe51d94f513b9d94a7374bd20a9aa52aab99b62 Mon Sep 17 00:00:00 2001 From: Maxime Lucas Date: Thu, 13 Apr 2023 15:50:37 +0200 Subject: [PATCH 1/4] feat: trivial hypergraph --- .../api/generators/xgi.generators.classic.rst | 3 +- tests/generators/test_classic.py | 4 ++ xgi/generators/classic.py | 38 +++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/docs/source/api/generators/xgi.generators.classic.rst b/docs/source/api/generators/xgi.generators.classic.rst index 2a142f53e..a857a763c 100644 --- a/docs/source/api/generators/xgi.generators.classic.rst +++ b/docs/source/api/generators/xgi.generators.classic.rst @@ -8,4 +8,5 @@ .. rubric:: Functions .. autofunction:: empty_hypergraph - .. autofunction:: empty_simplicial_complex \ No newline at end of file + .. autofunction:: empty_simplicial_complex + .. autofunction:: trivial_simplicial_complex \ No newline at end of file diff --git a/tests/generators/test_classic.py b/tests/generators/test_classic.py index a4294b6bb..ba4c3b618 100644 --- a/tests/generators/test_classic.py +++ b/tests/generators/test_classic.py @@ -9,3 +9,7 @@ def test_empty_hypergraph(): def test_empty_hypergraph(): SC = xgi.empty_simplicial_complex() assert (SC.num_nodes, SC.num_edges) == (0, 0) + +def test_trivial_hypergraph(): + SC = xgi.trivial_hypergraph() + assert (SC.num_nodes, SC.num_edges) == (1, 0) diff --git a/xgi/generators/classic.py b/xgi/generators/classic.py index 2d2741293..3d450fb55 100644 --- a/xgi/generators/classic.py +++ b/xgi/generators/classic.py @@ -8,6 +8,7 @@ __all__ = [ "empty_hypergraph", "empty_simplicial_complex", + "trivial_hypergraph", ] @@ -98,3 +99,40 @@ def empty_simplicial_complex(create_using=None, default=None): if default is None: default = xgi.SimplicialComplex return _empty_network(create_using, default) + + +def trivial_hypergraph(create_using=None, default=None): + """Returns a hypergraph with one node and zero edges. + + Parameters + ---------- + create_using : Hypergraph Instance, Constructor or None + If None, use the `default` constructor. + If a constructor, call it to create an empty hypergraph. + default : Hypergraph constructor (default None) + The constructor to use if create_using is None. + If None, then xgi.Hypergraph is used. + + Returns + ------- + Hypergraph object + A trivial hypergraph + + Examples + -------- + >>> import xgi + >>> H = xgi.trivial_hypergraph() + >>> H.num_nodes, H.num_edges + (1, 0) + + """ + # this import needs to happen when the function runs, not when the module is first + # imported, to avoid circular imports + import xgi + + if default is None: + default = xgi.Hypergraph + H = _empty_network(create_using, default) + H.add_node(0) + + return H From a33ee18837fa2af66b04e28949aa733dcd097b33 Mon Sep 17 00:00:00 2001 From: Maxime Lucas Date: Thu, 13 Apr 2023 15:53:13 +0200 Subject: [PATCH 2/4] fix --- docs/source/api/generators/xgi.generators.classic.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/api/generators/xgi.generators.classic.rst b/docs/source/api/generators/xgi.generators.classic.rst index a857a763c..96d832d7c 100644 --- a/docs/source/api/generators/xgi.generators.classic.rst +++ b/docs/source/api/generators/xgi.generators.classic.rst @@ -9,4 +9,4 @@ .. autofunction:: empty_hypergraph .. autofunction:: empty_simplicial_complex - .. autofunction:: trivial_simplicial_complex \ No newline at end of file + .. autofunction:: trivial_hypergraph \ No newline at end of file From 744470701418ba0583167f26fe067442eaa2b61a Mon Sep 17 00:00:00 2001 From: Maxime Lucas Date: Thu, 13 Apr 2023 16:25:14 +0200 Subject: [PATCH 3/4] review commets --- tests/generators/test_classic.py | 10 ++++++++-- xgi/generators/classic.py | 9 ++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/tests/generators/test_classic.py b/tests/generators/test_classic.py index ba4c3b618..db651f17d 100644 --- a/tests/generators/test_classic.py +++ b/tests/generators/test_classic.py @@ -11,5 +11,11 @@ def test_empty_hypergraph(): assert (SC.num_nodes, SC.num_edges) == (0, 0) def test_trivial_hypergraph(): - SC = xgi.trivial_hypergraph() - assert (SC.num_nodes, SC.num_edges) == (1, 0) + H = xgi.trivial_hypergraph() + assert (H.num_nodes, H.num_edges) == (1, 0) + + H = xgi.trivial_hypergraph(n=1) + assert (H.num_nodes, H.num_edges) == (1, 0) + + H = xgi.trivial_hypergraph(n=2) + assert (H.num_nodes, H.num_edges) == (2, 0) \ No newline at end of file diff --git a/xgi/generators/classic.py b/xgi/generators/classic.py index 3d450fb55..1d01f9a67 100644 --- a/xgi/generators/classic.py +++ b/xgi/generators/classic.py @@ -101,11 +101,13 @@ def empty_simplicial_complex(create_using=None, default=None): return _empty_network(create_using, default) -def trivial_hypergraph(create_using=None, default=None): +def trivial_hypergraph(n=1, create_using=None, default=None): """Returns a hypergraph with one node and zero edges. Parameters ---------- + n : int, optional + Number of nodes (default is 1) create_using : Hypergraph Instance, Constructor or None If None, use the `default` constructor. If a constructor, call it to create an empty hypergraph. @@ -116,7 +118,7 @@ def trivial_hypergraph(create_using=None, default=None): Returns ------- Hypergraph object - A trivial hypergraph + A trivial hypergraph with `n` nodes Examples -------- @@ -133,6 +135,7 @@ def trivial_hypergraph(create_using=None, default=None): if default is None: default = xgi.Hypergraph H = _empty_network(create_using, default) - H.add_node(0) + nodes = range(n) + H.add_nodes_from(nodes) return H From 23219c12765e06b53160be024813bf84c330c4f6 Mon Sep 17 00:00:00 2001 From: Maxime Lucas Date: Thu, 13 Apr 2023 16:43:16 +0200 Subject: [PATCH 4/4] review comments 2 --- xgi/generators/classic.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/xgi/generators/classic.py b/xgi/generators/classic.py index 1d01f9a67..d7b4245d2 100644 --- a/xgi/generators/classic.py +++ b/xgi/generators/classic.py @@ -50,6 +50,11 @@ def empty_hypergraph(create_using=None, default=None): Hypergraph object An empty hypergraph + See also + -------- + empty_simplicial_complex + trivial_hypergraph + Examples -------- >>> import xgi @@ -84,6 +89,11 @@ def empty_simplicial_complex(create_using=None, default=None): SimplicialComplex An empty simplicial complex. + See also + -------- + empty_hypergraph + trivial_hypergraph + Examples -------- >>> import xgi @@ -102,7 +112,7 @@ def empty_simplicial_complex(create_using=None, default=None): def trivial_hypergraph(n=1, create_using=None, default=None): - """Returns a hypergraph with one node and zero edges. + """Returns a hypergraph with `n` node and zero edges. Parameters ---------- @@ -120,6 +130,11 @@ def trivial_hypergraph(n=1, create_using=None, default=None): Hypergraph object A trivial hypergraph with `n` nodes + See also + -------- + empty_hypergraph + empty_simplicial_complex + Examples -------- >>> import xgi