Utilities

This module contains functions useful throughout PyBEL Tools

pybel_tools.utils.pairwise(iterable)[source]

Iterate over pairs in list s -> (s0,s1), (s1,s2), (s2, s3), …

Return type

Iterable[Tuple[~X, ~X]]

pybel_tools.utils.count_defaultdict(dict_of_lists)[source]

Count the number of elements in each value of the dictionary.

Return type

Mapping[~X, Counter[~Y]]

pybel_tools.utils.count_dict_values(dict_of_counters)[source]

Count the number of elements in each value (can be list, Counter, etc).

Parameters

dict_of_counters (Mapping[~X, Sized]) – A dictionary of things whose lengths can be measured (lists, Counters, dicts)

Return type

Counter[~X]

Returns

A Counter with the same keys as the input but the count of the length of the values list/tuple/set/Counter

pybel_tools.utils.set_percentage(x, y)[source]

What percentage of x is contained within y?

Parameters
  • x (set) – A set

  • y (set) – Another set

Return type

float

Returns

The percentage of x contained within y

pybel_tools.utils.tanimoto_set_similarity(x, y)[source]

Calculate the tanimoto set similarity.

Return type

float

pybel_tools.utils.min_tanimoto_set_similarity(x, y)[source]

Calculate the tanimoto set similarity using the minimum size.

Parameters
  • x (set) – A set

  • y (set) – Another set

Return type

float

Returns

The similarity between

pybel_tools.utils.calculate_single_tanimoto_set_distances(target, dict_of_sets)[source]

Return a dictionary of distances keyed by the keys in the given dict.

Distances are calculated based on pairwise tanimoto similarity of the sets contained

Parameters
  • target (set) – A set

  • dict_of_sets (dict) – A dict of {x: set of y}

Returns

A similarity dicationary based on the set overlap (tanimoto) score between the target set and the sets in dos

Return type

dict

pybel_tools.utils.calculate_tanimoto_set_distances(dict_of_sets)[source]

Return a distance matrix keyed by the keys in the given dict.

Distances are calculated based on pairwise tanimoto similarity of the sets contained.

Parameters

dict_of_sets (Mapping[~X, Set[~T]]) – A dict of {x: set of y}

Return type

Mapping[~X, Mapping[~X, float]]

Returns

A similarity matrix based on the set overlap (tanimoto) score between each x as a dict of dicts

pybel_tools.utils.calculate_global_tanimoto_set_distances(dict_of_sets)[source]

Calculate an alternative distance matrix based on the following equation.

\[distance(A, B)=1- \|A \cup B\| / \| \cup_{s \in S} s\|\]
Parameters

dict_of_sets (Mapping[~X, Set[~T]]) – A dict of {x: set of y}

Return type

Mapping[~X, Mapping[~X, float]]

Returns

A similarity matrix based on the alternative tanimoto distance as a dict of dicts

pybel_tools.utils.barh(d, plt, title=None)[source]

A convenience function for plotting a horizontal bar plot from a Counter

pybel_tools.utils.barv(d, plt, title=None, rotation='vertical')[source]

A convenience function for plotting a vertical bar plot from a Counter

pybel_tools.utils.safe_add_edge(graph, u, v, key, attr_dict, **attr)[source]

Adds an edge while preserving negative keys, and paying no respect to positive ones

Parameters
  • graph (pybel.BELGraph) – A BEL Graph

  • u (tuple) – The source BEL node

  • v (tuple) – The target BEL node

  • key (int) – The edge key. If less than zero, corresponds to an unqualified edge, else is disregarded

  • attr_dict (dict) – The edge data dictionary

  • attr (dict) – Edge data to assign via keyword arguments

pybel_tools.utils.prepare_c3(data, y_axis_label='y', x_axis_label='x')[source]

Prepares C3 JSON for making a bar chart from a Counter

Parameters
  • data (Union[List[Tuple[str, int]], Mapping[str, int]]) – A dictionary of {str: int} to display as bar chart

  • y_axis_label (str) – The Y axis label

  • x_axis_label (str) – X axis internal label. Should be left as default ‘x’)

Return type

str

Returns

A JSON dictionary for making a C3 bar chart

pybel_tools.utils.prepare_c3_time_series(data, y_axis_label='y', x_axis_label='x')[source]

Prepare C3 JSON string dump for a time series.

Parameters
  • data (List[Tuple[int, int]]) – A list of tuples [(year, count)]

  • y_axis_label (str) – The Y axis label

  • x_axis_label (str) – X axis internal label. Should be left as default ‘x’)

Return type

str

pybel_tools.utils.calculate_betweenness_centality(graph, number_samples=200)[source]

Calculate the betweenness centrality over nodes in the graph.

Tries to do it with a certain number of samples, but then tries a complete approach if it fails.

Return type

Counter

pybel_tools.utils.get_circulations(elements)[source]

Iterate over all possible circulations of an ordered collection (tuple or list).

Example:

>>> list(get_circulations([1, 2, 3]))
[[1, 2, 3], [2, 3, 1], [3, 1, 2]]
Return type

Iterable[~T]

pybel_tools.utils.canonical_circulation(elements, key=None)[source]

Get get a canonical representation of the ordered collection by finding its minimum circulation with the given sort key

Return type

~T

pybel_tools.utils.get_version()[source]

Get the current PyBEL Tools version.

Return type

str