Git Product home page Git Product logo

hypothesis-networkx's Introduction

Build Status codecov

Hypothesis-networkx

This module provides a Hypothesis strategy for generating networkx graphs. This can be used to efficiently and thoroughly test your code.

Installation

This module can be installed via pip:

pip install hypothesis-networkx

User guide

The module exposes a single function: graph_builder. This function is a hypothesis composite strategy for building graphs. You can use it as follows:

from hypothesis_networkx import graph_builder
from hypothesis import strategies as st
import networkx as nx

node_data = st.fixed_dictionaries({'name': st.text(),
                                   'number': st.integers()})
edge_data = st.fixed_dictionaries({'weight': st.floats(allow_nan=False,
                                                       allow_infinity=False)})


builder = graph_builder(graph_type=nx.Graph,
                        node_keys=st.integers(),
                        node_data=node_data,
                        edge_data=edge_data,
                        min_nodes=2, max_nodes=10,
                        min_edges=1, max_edges=None,
                        self_loops=False,
                        connected=True)

graph = builder.example()
print(graph.nodes(data=True))
print(graph.edges(data=True))

Of course this builder is a valid hypothesis strategy, and using it to just make examples is not super useful. Instead, you can (and should) use it in your testing framework:

from hypothesis import given

@given(graph=builder)
def test_my_function(graph):
    assert my_function(graph) == known_function(graph)

The meaning of the arguments given to graph_builder are pretty self-explanatory, but they must be given as keyword arguments.

  • node_data: The strategy from which node attributes will be drawn.
  • edge_data: The strategy from which edge attributes will be drawn.
  • node_keys: Either the strategy from which node keys will be draw, or None. If None, node keys will be integers from the range (0, number of nodes).
  • min_nodes and max_nodes: The minimum and maximum number of nodes the produced graphs will contain.
  • min_edges and max_edges: The minimum and maximum number of edges the produced graphs will contain. Note that less edges than min_edges may be added if there are not enough nodes, and more than max_edges if connected is True.
  • graph_type: This function (or class) will be called without arguments to create an empty initial graph.
  • connected: If True, the generated graph is guaranteed to be a single connected component.
  • self_loops: If False, there will be no self-loops in the generated graph. Self-loops are edges between a node and itself.

Known limitations

There are a few (minor) outstanding issues with this module:

  • Graph generation may be slow for large graphs.
  • The min_edges argument is not always respected when the produced graph is too small.
  • The max_edges argument is not always respected if connected is True.
  • It currently works for Python 2.7, but this is considered deprecated and may stop working without notice.

See also

Networkx

Hypothesis

hypothesis-networkx's People

Contributors

gmacon avatar pckroon avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.