Skip to main content

Collection of network-related utilities for python.

Project description

Build Status

Pelote

Pelote is a python library full of network-related functions that can be used to complement networkx for higher-level tasks.

It mainly helps with the following things:

  • Conversion of tabular data to networks (bipartites, citation etc. in the spirit of Table2Net)
  • Conversion of networks to tabular data
  • Monopartite projections of bipartite graphs
  • Miscellaneous graph helper functions (filtering out nodes, edges etc.)
  • Sparsification of networks
  • Reading & writing of graph formats not found in networkx (such as graphology JSON)

As such it is the perfect companion to ipysigma, our Jupyter widget that can render interactive graphs directly within your notebooks.

Installation

You can install pelote with pip with the following command:

pip install pelote

If you want to be able to use the library with pandas, you will need to install it also:

pip install pandas

Usage


Tabular to network

to_bipartite_graph

Function creating a bipartite graph from the given tabular data.

Arguments

  • table Iterable[Indexable] or pd.DataFrame - input tabular data. It can be a large variety of things as long as it is 1. iterable and 2. yields indexable values such as dicts or lists. This can for instance be a list of dicts, a csv.DictReader stream etc. It also supports pandas DataFrame if the library is installed.
  • first_part_col str or int - the name of the column containing the value representing a node in the resulting graph's first part. It could be the index if your rows are lists or a key if your rows are dicts instead.
  • second_par_col str or int - the name of the column containing the value representing a node in the resulting graph's second part. It could be the index if your rows are lists or a key if your rows are dicts instead.
  • node_part_attr str, optional "part" - name of the node attribute containing the part it belongs to.
  • edge_weight_attr str, optional "weight" - name of the edge attribute containing its weight, i.e. the number of times it was found in the table.
  • first_part_data Sequence or Callable, optional None - sequence (i.e. list, tuple etc.) of column from rows to keep as node attributes for the graph's first part. Can also be a function returning a dict of those attributes. Note that the first row containing a given node will take precedence over subsequent ones regarding data to include.
  • second_part_data Sequence or Callable, optional None - sequence (i.e. list, tuple etc.) of column from rows to keep as node attributes for the graph's second part. Can also be a function returning a dict of those attributes. Note that the first row containing a given node will take precedence over subsequent ones regarding data to include.
  • disjoint_keys bool, optional False - set this to True as an optimization mechanism if you know your part keys are disjoint, i.e. if no value for first_part_col can also be found in second_part_col. If you enable this option wrongly, the result can be incorrect.

Network to tabular

to_nodes_dataframe

Function converting the given networkx graph into a pandas DataFrame of its nodes.

from pelote import to_nodes_dataframe

df = to_nodes_dataframe(graph)

Arguments

  • nx.AnyGraph - a networkx graph instance
  • node_key_col str, optional "key" - name of the DataFrame column containing the node keys. If None, the node keys will be used as the DataFrame index.

Returns

pd.DataFrame - A pandas DataFrame

to_edges_dataframe

Function converting the given networkx graph into a pandas DataFrame of its edges.

Arguments

  • nx.AnyGraph - a networkx graph instance
  • edge_source_col str, optional "source" - name of the DataFrame column containing the edge source.
  • edge_target_col str, optional "target" - name of the DataFrame column containing the edge target.

Returns

pd.DataFrame - A pandas DataFrame

to_dataframes

Function converting the given networkx graph into two pandas DataFrames: one for its nodes, one for its edges.

Arguments

  • nx.AnyGraph - a networkx graph instance
  • node_key_col str, optional "key" - name of the node DataFrame column containing the node keys. If None, the node keys will be used as the DataFrame index.
  • edge_source_col str, optional "source" - name of the edge DataFrame column containing the edge source.
  • edge_target_col str, optional "target" - name of the edge DataFrame column containing the edge target.

Returns

None - (pd.DataFrame, pd.DataFrame)


Graph projection

monopartite_projection

Arguments


Graph utilities

largest_connected_component

Function returning the largest connected component of given networkx graph as a set of nodes.

Arguments

  • graph nx.AnyGraph - target graph.

Returns

set - set of nodes representing the largest connected component.

crop_to_largest_connected_components

Function mutating the given networkx graph in order to keep only the largest connected component.

Arguments

  • graph nx.AnyGraph - target graph.

remove_edges

Function removing all edges that do not pass a predicate function from a given networkx graph.

Note that this function mutates the given graph.

Arguments

  • graph nx.AnyGraph - a networkx graph.
  • predicate callable - a function taking each edge source, target and attributes and returning True if you want to keep the edge or False if you want to remove it.

Reading & Writing

read_graphology_json

Function reading and parsing the given json file as a networkx graph.

Arguments

  • target str or Path or file or dict - target to read and parse. Can be a string path, a Path instance, a file buffer or already parsed JSON data as a dict.

Returns

nx.AnyGraph - a networkx graph instance.


Metrics

edge_disparity

Function computing the disparity score of each edge in the given graph. This score is typically used to extract the multiscale backbone of a weighted graph.

Arguments

  • graph nx.AnyGraph - target graph.
  • edge_weight_attr str, optional "weight" - name of the edge attribute containing its weight.

Returns

dict - Dictionnary with edges - (source, target) tuples - as keys and the disparity scores as values

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pelote-0.2.0.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

pelote-0.2.0-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file pelote-0.2.0.tar.gz.

File metadata

  • Download URL: pelote-0.2.0.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.12

File hashes

Hashes for pelote-0.2.0.tar.gz
Algorithm Hash digest
SHA256 4ad534381046d598cca5bb6bf72edfda830ed12891df69c542000ddc126aa369
MD5 df62d1269da85e9298a71f008c07a219
BLAKE2b-256 fde41f7e344c036f76b133eef4ba7c3926fbdcf50a8d72ecd232a27d6bbcc47d

See more details on using hashes here.

File details

Details for the file pelote-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pelote-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.6.12

File hashes

Hashes for pelote-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f1c1ba85cf4ccb1a43fc07de09c699f9e4917181b095a5bc4a01b5fa7810b220
MD5 dadd10f93929b4ab59bde3bc0837eb74
BLAKE2b-256 0f105ff2248ebce5bb441582f33468d52a530bdf27148206104971574a72e282

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page