xgi.core.diviews.DiIDView

class xgi.core.diviews.DiIDView(network, ids=None)[source]

Bases: Mapping, Set

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

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

Parameters:
  • id_dict (dict) – The original dict this is a view of.

  • id_attrs (dict) – The original attribute dict this is a view of.

  • 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.

filterby

Filter the IDs in this view by a statistic.

filterby_attr

Filter the IDs in this view by an attribute.

filterby(stat, val, mode='eq')[source]

Filter the IDs in this view by a statistic.

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

  • 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.DiHypergraph([([1, 2, 3], [2, 3, 4, 5]), ([3, 4, 5], [])])
>>> n = H.nodes
>>> n.filterby('degree', 2)
DiNodeView((3, 4, 5))

Can choose other comparison methods via mode.

>>> n.filterby('degree', 2, 'eq')
DiNodeView((3, 4, 5))
>>> n.filterby('degree', 2, 'neq')
DiNodeView((1, 2))
>>> n.filterby('degree', 2, 'lt')
DiNodeView((1, 2))
>>> n.filterby('degree', 2, 'gt')
DiNodeView(())
>>> n.filterby('degree', 2, 'leq')
DiNodeView((1, 2, 3, 4, 5))
>>> n.filterby('degree', 2, 'geq')
DiNodeView((3, 4, 5))
>>> n.filterby('degree', (2, 3), 'between')
DiNodeView((3, 4, 5))
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

DiIDView.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 initialze 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:

DiIDView

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.