GithubHelp home page GithubHelp logo

partitions's Introduction

partitions

Project goals

  • Compatibility with the pandas/numpy/scipy stack
  • Intuitive interface. The user can work with nodes, edges, neighbors instead of directly with CSR matrices.
  • Keep the door open for extensions (including with Cython) by providing the sparse adjacency matrices as part of the public interface.
  • Prioritize efficiency in "big" operations (like generating spanning trees) over "small" and mutable operations (like adding an edge to the graph)

Graphs

>>> import pandas
>>> from partitions import Graph
>>> graph = Graph.from_edges(
...     [(0, 1), (1, 2), (0, 2)],
...     data=pandas.DataFrame(
...         {"population": [100, 200, 50], "votes": [50, 60, 40]},
...         index=[0, 1, 2]
...     )
... )
>>> graph
<Graph ['population', 'votes']>

Nodes and edges

graph
>>> set(graph.nodes) == {0, 1, 2}
True
>>> set(graph.edges) == {(0, 1), (1, 2), (0, 2)}
True
>>> list(graph)
[0, 1, 2]
>>> len(graph.nodes)
3
>>> len(graph.edges)
3

Edges are undirected

>>> (0, 1) in graph.edges
True
>>> (1, 0) in graph.edges
True

Data

>>> graph.data["population"]
0    100
1    200
2     50
Name: population, dtype: int64
>>> graph.data
   population  votes
0         100     50
1         200     60
2          50     40

Neighbors

>>> set(graph.neighbors[0]) == {1, 2}
True
>>> set(graph.neighbors[1]) == {0, 2}
True
>>> graph.neighbors[0]
array([1, 2], dtype=int32)

Subgraph

>>> subgraph = graph.subgraph({1, 2})
>>> subgraph.data["population"]
0    200
1     50
Name: population, dtype: int64

>>> list(subgraph.edges)
[(0, 1)]
>>> subgraph
<EmbeddedGraph [2 nodes]>
>>> subgraph.image
array([1, 2])

>>> subgraph.image[0]
1

>>> subgraph.image[[0, 1]]
array([1, 2])

Partitioning a graph

>>> from partitions import Partition
>>> partition = Partition.from_assignment(graph, {0: "a", 1: "b", 2: "b"})
>>> partition
<Partition [2]>
>>> for part in partition:
...     print(set(part.image[part.nodes]))
{0}
{1, 2}

>>> partition.data["population"]
a    100
b    250
Name: population, dtype: int64

>>> set(partition["a"].cut_edges)
{(0, 1), (0, 2)}

>>> set(partition["a"].boundary.nodes)
{0}

>>> set(partition["a"].boundary.neighbors)
{1, 2}

partitions's People

Contributors

maxhully avatar

Watchers

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