xgi.core.views.IDView

class xgi.core.views.IDView(network, ids=None)[source]

Bases: Mapping, Set

Base View class for accessing the ids (nodes or edges) of a Hypergraph.

Can optionally keep track of a subset of ids. By default all node ids or all edge ids are kept track of.

Parameters:
  • network (Hypergraph or Simplicial Complex) – The underlying network

  • ids (iterable) – A subset of the keys in id_dict to keep track of.

Raises:

XGIError – If ids is not a subset of the keys of id_dict.

Methods

from_view

Create a view from another view.

neighbors

Find the neighbors of an ID.

duplicates

Find IDs that have a duplicate.

lookup

Find IDs with the specified bipartite neighbors.

filterby

Filter the IDs in this view by a statistic.

filterby_attr

Filter the IDs in this view by an attribute.

duplicates()[source]

Find IDs that have a duplicate.

An ID has a ‘duplicate’ if there exists another ID with the same bipartite neighbors.

Returns:

A view containing only those IDs with a duplicate.

Return type:

IDView

Raises:

TypeError – When IDs are of different types. For example, (“a”, 1).

Notes

The IDs returned are in an arbitrary order, that is duplicates are not guaranteed to be consecutive. For IDs with the same bipartite neighbors, only the first ID added is not a duplicate.

See also

IDView.lookup

Examples

>>> import xgi
>>> H = xgi.Hypergraph([[0, 1, 2], [3, 4, 2], [0, 1, 2]])
>>> H.edges.duplicates()
EdgeView((2,))

Order does not matter:

>>> H = xgi.Hypergraph([[2, 1, 0], [0, 1, 2]])
>>> H.edges.duplicates()
EdgeView((1,))

Repetitions matter:

>>> H = xgi.Hypergraph([[0, 1], [1, 0]])
>>> H.edges.duplicates()
EdgeView((1,))
filterby(stat, val, mode='eq')[source]

Filter the IDs in this view by a statistic.

Parameters:
  • stat (str or xgi.stats.NodeStat/xgi.stats.EdgeStat) – NodeStat/EdgeStat object, or name of a NodeStat/EdgeStat.

  • val (Any) – Value of the statistic. Usually a single numeric value. When mode is ‘between’, must be a tuple of exactly two values.

  • mode (str or function, optional) –

    How to compare each value to val. Can be one of the following.

    • ’eq’ (default): Return IDs whose value is exactly equal to val.

    • ’neq’: Return IDs whose value is not equal to val.

    • ’lt’: Return IDs whose value is less than val.

    • ’gt’: Return IDs whose value is greater than val.

    • ’leq’: Return IDs whose value is less than or equal to val.

    • ’geq’: Return IDs whose value is greater than or equal to val.

    • ’between’: In this mode, val must be a tuple (val1, val2). Return IDs whose value v satisfies val1 <= v <= val2.

    • function, must be able to call mode(statistic, val) and have it map to a bool.

See also

IDView.filterby_attr : For more details, see the tutorial.

Examples

By default, return the IDs whose value of the statistic is exactly equal to val.

>>> import xgi
>>> H = xgi.Hypergraph([[1, 2, 3], [2, 3, 4, 5], [3, 4, 5]])
>>> n = H.nodes
>>> n.filterby('degree', 2)
NodeView((2, 4, 5))

Can choose other comparison methods via mode.

>>> n.filterby('degree', 2, 'eq')
NodeView((2, 4, 5))
>>> n.filterby('degree', 2, 'neq')
NodeView((1, 3))
>>> n.filterby('degree', 2, 'lt')
NodeView((1,))
>>> n.filterby('degree', 2, 'gt')
NodeView((3,))
>>> n.filterby('degree', 2, 'leq')
NodeView((1, 2, 4, 5))
>>> n.filterby('degree', 2, 'geq')
NodeView((2, 3, 4, 5))
>>> n.filterby('degree', (2, 3), 'between')
NodeView((2, 3, 4, 5))

Can also pass a NodeStat object.

>>> n.filterby(n.degree(order=2), 2)
NodeView((3,))
filterby_attr(attr, val, mode='eq', missing=None)[source]

Filter the IDs in this view by an attribute.

Parameters:
  • attr (string) – The name of the attribute

  • val (Any) – A single value or, in the case of ‘between’, a list of length 2

  • mode (str or function, optional) – Comparison mode. Valid options are ‘eq’ (default), ‘neq’, ‘lt’, ‘gt’, ‘leq’, ‘geq’, or ‘between’. If a function, must be able to call mode(attribute, val) and have it map to a bool.

  • missing (Any, optional) – The default value if the attribute is missing. If None (default), ignores those IDs.

See also

IDView.filterby : Identical method. For more details, see the tutorial.

Notes

Beware of using comparison modes (“lt”, “gt”, “leq”, “geq”) when the attribute is a string. For example, the string comparison ‘10’ < ‘9’ evaluates to True.

classmethod from_view(view, bunch=None)[source]

Create a view from another view.

Allows to create a view with the same underlying data but with a different bunch.

Parameters:
  • view (IDView) – The view used to initialize the new object

  • bunch (iterable) – IDs the new view will keep track of

Returns:

A view that is identical to view but keeps track of different IDs.

Return type:

IDView

property ids

The ids in this view.

Notes

Do not use this property for membership check. Instead of x in view.ids, always use x in view. The latter is always faster.

lookup(neighbors)[source]

Find IDs with the specified bipartite neighbors.

Parameters:

neighbors (Iterable) – An iterable of IDs.

Returns:

A view containing only those IDs whose bipartite neighbors match neighbors.

Return type:

IDView

Examples

>>> import xgi
>>> H = xgi.Hypergraph([[0, 1, 2], [3, 4], [3, 4, 2]])
>>> H.edges.lookup([3, 4])
EdgeView((1,))
>>> H.add_edge([3, 4])
>>> H.edges.lookup([3, 4])
EdgeView((1, 3))

Can be used as a boolean check for edge existence:

>>> if H.edges.lookup([3, 4]): print('An edge with members [3, 4] exists')
An edge with members [3, 4] exists

Can also be used to check for nodes that belong to a particular set of edges:

>>> H = xgi.Hypergraph([['a', 'b', 'c'], ['a', 'd', 'e'], ['c', 'd', 'e']])
>>> H.nodes.lookup([0, 1])
NodeView(('a',))
neighbors(id, s=1)[source]

Find the neighbors of an ID.

The neighbors of an ID are those IDs that share at least one bipartite ID.

Parameters:
  • id (hashable) – ID to find neighbors of.

  • s (int, optional) – The intersection size s for two edges or nodes to be considered neighbors. By default, 1.

Returns:

A set of the neighboring IDs

Return type:

set

See also

edge_neighborhood

Examples

>>> import xgi
>>> hyperedge_list = [[1, 2], [2, 3, 4]]
>>> H = xgi.Hypergraph(hyperedge_list)
>>> H.nodes.neighbors(1)
{2}
>>> H.nodes.neighbors(2)
{1, 3, 4}