xgi.utils.utilities

General utilities.

Classes

IDDict

A dict that holds (node or edge) IDs.

Functions

xgi.utils.utilities.dual_dict(edge_dict)[source]

Given a dictionary with IDs as keys and sets as values, return the dual.

Parameters:

edge_dict (dict) – A dictionary where the keys are IDs and the values are sets of hashables

Returns:

A dictionary with IDs as keys and sets as values, but the reverse of the original dict.

Return type:

dict

Examples

>>> import xgi
>>> xgi.dual_dict({0 : [1, 2, 3], 1 : [0, 2]})
{1: {0}, 2: {0, 1}, 3: {0}, 0: {1}}
xgi.utils.utilities.powerset(iterable, include_empty=False, include_full=False, include_singletons=True, max_size=None)[source]

Returns all possible subsets of the elements in iterable, with options to include the empty set and the set containing all elements, and to set the maximum subset size.

Parameters:
  • iterable (list-like) – List of elements

  • include_empty (bool, default: False) – Whether to include the empty set

  • include_singletons (bool, default: True) – Whether to include singletons

  • include_full (bool, default: False) – Whether to include the set containing all elements of iterable

  • max_size (int, default: None) – Maximum size of the returned subsets.

Return type:

itertools.chain

Notes

include_empty overrides include_singletons if True: singletons will always be included if the empty set is. Likewise, max_size will override other arguments: if set to -1, no subset will be returned.

Examples

>>> import xgi
>>> list(xgi.powerset([1,2,3,4])) 
[(1,), (2,), (3,), (4,), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4),
 (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
xgi.utils.utilities.update_uid_counter(H, new_id)[source]

Helper function to make sure the uid counter is set correctly after adding an edge with a user-provided ID.

If we don’t set the start of self._edge_uid correctly, it will start at 0, which will overwrite any existing edges when calling add_edge(). First, we use the somewhat convoluted float(e).is_integer() instead of using isinstance(e, int) because there exist integer-like numeric types (such as np.int32) which fail the isinstance() check.

Parameters:
  • H (xgi.Hypergraph) – Hypergraph of which to update the uid counter

  • id (any hashable type) – User-provided ID.

xgi.utils.utilities.find_triangles(G)[source]

Returns list of 3-node cliques present in a graph

Parameters:

G (networkx Graph) – Graph to consider

Return type:

list of 3-node cliques (triangles)

xgi.utils.utilities.min_where(dicty, where)[source]

Finds the minimum value of a dictonary dicty. The dictonary where indicates which keys to take into account. The minimum is eventualy infinite.

Parameters:
  • dicty (dict) – Dictionary of values (int, float…) from which to find the minimum.

  • where (dict) – Dictionary of booleans that has the same keys as dicty. The minimum will be searched among the values for which where[key] is TRUE.

Returns:

min_val – Minimum value found in dicty. Is set to np.infty if where indicated nowhere or if all values are np.infty.

Return type:

float or np.Inf

xgi.utils.utilities.request_json_from_url(url)[source]

HTTP request json file and return as dict.

Parameters:

url (str) – The url where the json file is located.

Returns:

A dictionary of the JSON requested.

Return type:

dict

Raises:

XGIError – If the connection fails or if there is a bad HTTP request.

xgi.utils.utilities.request_json_from_url_cached(url)[source]

HTTP request json file and return as dict.

Parameters:

url (str) – The url where the json file is located.

Returns:

A dictionary of the JSON requested.

Return type:

dict

Raises:

XGIError – If the connection fails or if there is a bad HTTP request.

xgi.utils.utilities.subfaces(edges, order=None)[source]

Returns the subfaces of a list of hyperedges

Parameters:
  • edges (list of edges) – Edges to consider, as tuples of nodes

  • order ({None, -1, int}, optional) – If None, compute subfaces recursively down to nodes. If -1, compute subfaces the order below (e.g. edges for a triangle). If d > 0, compute the subfaces of order d. By default, None.

Returns:

faces – List of hyperedges that are subfaces of input hyperedges.

Return type:

list of sets

Raises:

XGIError – Raises error when order is larger than the max order of input edges

Notes

Hyperedges in the returned list are not unique, they may appear more than once if they are subfaces or more than one edge from the input edges.

Examples

>>> import xgi
>>> edges = [{1,2,3,4}, {3,4,5}]
>>> xgi.subfaces(edges) 
[(1,), (2,), (3,), (4,), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4), (1, 2, 3),
 (1, 2, 4), (1, 3, 4), (2, 3, 4), (3,), (4,), (5,), (3, 4), (3, 5), (4, 5)]
>>> xgi.subfaces(edges, order=-1)
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4), (3, 4), (3, 5), (4, 5)]
>>> xgi.subfaces(edges, order=2)
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4), (3, 4, 5)]
xgi.utils.utilities.convert_labels_to_integers(net, label_attribute='label')[source]

Relabel node and edge IDs to be sequential integers.

Parameters:
  • net (Hypergraph, DiHypergraph, or SimplicialComplex) – The higher-order network of interest

  • label_attribute (string, default: "label") – The attribute name that stores the old node and edge labels

Returns:

A new higher-order network with nodes and edges with sequential IDs starting at 0. The old IDs are stored in the “label” attribute for both nodes and edges.

Return type:

Hypergraph, DiHypergraph, or SimplicialComplex

Notes

The “relabeling” will occur even if the node/edge IDs are sequential. Because the old IDs are stored in the “label” attribute for both nodes and edges, the old “label” values (if they exist) will be overwritten.

xgi.utils.utilities.hist(vals, bins=10, bin_edges=False, density=False, log_binning=False)[source]

Return the distribution of a numpy array.

Parameters:
  • vals (Numpy array) – The array of values

  • bins (int, list, or Numpy array) – The number of bins or the bin edges. By default, 10.

  • bin_edges (bool) – Whether to also output the min and max of each bin, by default, False.

  • density (bool) – Whether to normalize the resulting distribution. By default, False.

  • log_binning (bool) – Whether to bin the values with log-sized bins. By default, False.

Returns:

A two-column table with “bin_center” and “value” columns, where “value” is a count or a probability. If bin_edges is True, outputs two additional columns, bin_lo and bin_hi, which outputs the left and right bin edges respectively.

Return type:

Pandas DataFrame

Notes

Originally from https://github.com/jkbren/networks-and-dataviz