Skip to content

Commit

Permalink
Merge 6f38b5b into d613a52
Browse files Browse the repository at this point in the history
  • Loading branch information
djordon committed Dec 23, 2018
2 parents d613a52 + 6f38b5b commit 256f153
Show file tree
Hide file tree
Showing 23 changed files with 1,100 additions and 757 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Expand Up @@ -5,6 +5,8 @@ python:
- 2.7
- 3.4
- 3.5
- 3.6
- 3.7
cache: pip
env:
global:
Expand All @@ -30,6 +32,6 @@ before_script:
- "sh -e /etc/init.d/xvfb start"
- sleep 3 # give xvfb some time to start
script:
- py.test --cov=queueing_tool --cov-report term-missing --doctest-modules
- pytest --cov=queueing_tool --cov-report term-missing --doctest-modules -k "not slow"
after_success:
- coveralls
4 changes: 2 additions & 2 deletions README.rst
Expand Up @@ -37,7 +37,7 @@ Installation
------------


**Prerequisites:** Queueing-tool runs on Python 2.7 and 3.4-3.5 and it
**Prerequisites:** Queueing-tool runs on Python 2.7 and 3.4-3.7 and it
requires `networkx <http://networkx.readthedocs.org/en/stable/>`__ and
`numpy <http://www.numpy.org/>`__. If you want to plot, you will need
to install `matplotlib <http://matplotlib.org/>`__ as well.
Expand Down Expand Up @@ -73,7 +73,7 @@ The issue tracker is at https://github.com/djordon/queueing-tool/issues. Please
Copyright and license
---------------------

Code and documentation Copyright 2014-2016 Daniel Jordon. Code released
Code and documentation Copyright 2014-2019 Daniel Jordon. Code released
under the `MIT
license <https://github.com/djordon/queueing-tool/blob/master/LICENSE.txt>`__.

Expand Down
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
1.2.2
1.3.0
20 changes: 12 additions & 8 deletions queueing_tool/__init__.py
@@ -1,20 +1,24 @@
from __future__ import absolute_import

from queueing_tool.queues import *
import queueing_tool.queues as queues
from queueing_tool.common import *
import queueing_tool.common as common

from queueing_tool.graph import *
import queueing_tool.graph as graph

from queueing_tool.network import *
import queueing_tool.network as network

from queueing_tool.graph import *
import queueing_tool.graph as graph
from queueing_tool.queues import *
import queueing_tool.queues as queues


__all__ = []
__version__ = '1.2.2'
__version__ = '1.3.0'

__all__.extend(['__version__'])
__all__.extend(queues.__all__)
__all__.extend(network.__all__)
__all__.extend(['__version__', 'EdgeID', 'AgentID'])
__all__.extend(graph.__all__)
__all__.extend(network.__all__)
__all__.extend(queues.__all__)

# del queues, network, generation, graph
12 changes: 12 additions & 0 deletions queueing_tool/common.py
@@ -0,0 +1,12 @@
import collections


EdgeID = collections.namedtuple(
typename='EdgeID',
field_names=['source', 'target', 'edge_index', 'edge_type']
)

AgentID = collections.namedtuple(
typename='AgentID',
field_names=['edge_index', 'agent_qid']
)
5 changes: 4 additions & 1 deletion queueing_tool/graph/__init__.py
Expand Up @@ -7,6 +7,7 @@
generate_pagerank_graph
generate_transition_matrix
graph2dict
matrix2dict
minimal_random_graph
set_types_rank
set_types_random
Expand All @@ -15,7 +16,8 @@
"""

from queueing_tool.graph.graph_functions import (
graph2dict
graph2dict,
matrix2dict
)
from queueing_tool.graph.graph_generation import (
generate_random_graph,
Expand All @@ -42,6 +44,7 @@
'generate_pagerank_graph',
'generate_transition_matrix',
'graph2dict',
'matrix2dict',
'minimal_random_graph',
'set_types_rank',
'set_types_random',
Expand Down
53 changes: 53 additions & 0 deletions queueing_tool/graph/graph_functions.py
Expand Up @@ -86,3 +86,56 @@ def graph2dict(g, return_dict_of_dict=True):
return dict_of_dicts
else:
return {k: list(val.keys()) for k, val in dict_of_dicts.items()}


def matrix2dict(matrix, graph):
"""Takes an adjacency matrix and returns an adjacency list.
Parameters
----------
graph : :any:`networkx.DiGraph`, :any:`networkx.Graph`, etc.
Any object that networkx can turn into a
:any:`DiGraph<networkx.DiGraph>`.
matrix : :class:`~numpy.ndarray`
An matrix related to an adjacency list. The ``matrix[i, j]``
represents some relationship between the vertex ``i`` and the
vertex ``j``.
Returns
-------
adj : dict
An adjacency representation of the matrix for the graph.
Examples
--------
>>> import queueing_tool as qt
>>> import networkx as nx
>>> adj = {0: [1, 2], 1: [0], 2: [0, 3], 3: [2]}
>>> g = nx.DiGraph(adj)
>>> mat = qt.generate_transition_matrix(g, seed=123)
>>> mat # doctest: +ELLIPSIS
... # doctest: +NORMALIZE_WHITESPACE
array([[ 0. , 0.70707071, 0.29292929, 0. ],
[ 1. , 0. , 0. , 0. ],
[ 0.29113924, 0. , 0. , 0.70886076],
[ 0. , 0. , 1. , 0. ]])
>>> qt.matrix2dict(mat, g) # doctest: +ELLIPSIS
... # doctest: +NORMALIZE_WHITESPACE
{0: {1: 0.707..., 2: 0.292...},
1: {0: 1.0},
2: {0: 0.291..., 3: 0.708...},
3: {2: 1.0}}
"""
num_columns, num_rows = matrix.shape

if num_columns != num_rows or num_columns != graph.number_of_nodes():
msg = "Matrix has wrong shape, must be {} x {}"
non = graph.number_of_nodes()
raise ValueError(msg.format(non, non))

result = {}
adjacency_graph = graph2dict(graph, return_dict_of_dict=False)
for source, adjacents in adjacency_graph.items():
result[source] = {dest: matrix[source, dest] for dest in adjacents}

return result

0 comments on commit 256f153

Please sign in to comment.