Skip to content

Commit

Permalink
Minor code quality changes
Browse files Browse the repository at this point in the history
  • Loading branch information
djordon committed Apr 15, 2016
1 parent c0a5785 commit af0205e
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 57 deletions.
7 changes: 0 additions & 7 deletions queueing_tool/graph/graph_generation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numbers
import copy

import networkx as nx
import numpy as np
Expand Down Expand Up @@ -120,7 +119,6 @@ def generate_random_graph(nVertices=250, prob_loop=0.5, **kwargs):
typically used for terminal edges.
"""
g = minimal_random_graph(nVertices, **kwargs)
nNodes = g.number_of_nodes()
for v in g.nodes():
e = (v, v)
if not g.is_edge(e):
Expand Down Expand Up @@ -203,7 +201,6 @@ def minimal_random_graph(nVertices, seed=None, **kwargs):
np.random.seed(seed)

points = np.random.random((nVertices, 2)) * 10
nEdges = nVertices * (nVertices - 1) // 2
edges = []

for k in range(nVertices-1):
Expand All @@ -219,7 +216,6 @@ def minimal_random_graph(nVertices, seed=None, **kwargs):
g = nx.Graph()

for n1, n2, d in edges:
max_space = d
unionF.union(n1, n2)
g.add_edge(n1, n2)
if unionF.nClusters == 1:
Expand Down Expand Up @@ -292,17 +288,14 @@ def set_types_random(g, proportions=None, loop_proportions=None, seed=None,
if loop_proportions is None:
loop_proportions = {k : 1. / 4 for k in range(4)}

nEdges = g.number_of_edges()
edges = [e for e in g.edges() if e[0] != e[1]]
loops = [e for e in g.edges() if e[0] == e[1]]
props = list(proportions.values())
lprops = list(loop_proportions.values())

if not np.isclose(sum(props), 1.0):
msg = "proportions values must sum to one."
raise ValueError("proportions values must sum to one.")
if not np.isclose(sum(lprops), 1.0):
msg = "loop_proportions values must sum to one."
raise ValueError("loop_proportions values must sum to one.")

eTypes = {}
Expand Down
7 changes: 1 addition & 6 deletions queueing_tool/graph/graph_preparation.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import numbers

import networkx as nx
import numpy as np

from queueing_tool.graph.graph_functions import (
graph2dict,
_test_graph
)
from queueing_tool.graph.graph_functions import _test_graph
from queueing_tool.graph.graph_wrapper import (
adjacency2graph,
QueueNetworkDiGraph
Expand Down
6 changes: 2 additions & 4 deletions queueing_tool/graph/graph_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _adjacency_adjust(adjacency, adjust, is_directed):
"""

for v, adj in adjacency.items():
for u, properties in adj.items():
for properties in adj.values():
if properties.get('edge_type') is None:
properties['edge_type'] = 1

Expand Down Expand Up @@ -155,8 +155,7 @@ def adjacency2graph(adjacency, edge_type=None, adjust=1, **kwargs):
2: {},
3: {0: {'edge_type': 1}}}
"""
import copy
dd = copy.deepcopy(adjacency)

if isinstance(adjacency, np.ndarray):
adjacency = _matrix2dict(adjacency)
elif isinstance(adjacency, dict):
Expand Down Expand Up @@ -490,7 +489,6 @@ def lines_scatter_args(self, line_kwargs=None, scatter_kwargs=None, pos=None):
'zorder': 0,
'facecolors': None,
'norm': None,
'antialiaseds': None,
'offsets': None,
'offset_position': 'screen',
'hatch': None,
Expand Down
4 changes: 1 addition & 3 deletions queueing_tool/network/queue_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import copy
import array

import networkx as nx
import numpy as np
from numpy.random import uniform

Expand All @@ -18,7 +17,7 @@
except ImportError:
HAS_MATPLOTLIB = False

from queueing_tool.graph import _prepare_graph, QueueNetworkDiGraph
from queueing_tool.graph import _prepare_graph
from queueing_tool.queues import (
NullQueue,
QueueServer,
Expand Down Expand Up @@ -1320,7 +1319,6 @@ def _simulate_next_event(self, slow=True):
e2 = q1._departures[0].desired_destination(self, q1.edge)
q2 = self.edge2queue[e2]
q2k = q2._key()
q2t = q2k[0]

if q2.at_capacity() and e2 != e1:
q2.nBlocked += 1
Expand Down
9 changes: 3 additions & 6 deletions queueing_tool/queues/queue_extentions.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import copy
from heapq import heappush, heappop

from numpy.random import randint, exponential
from numpy.random import exponential
from numpy import logical_or, infty
import numpy as np

from queueing_tool.queues.agents import Agent
from queueing_tool.queues.queue_servers import (
QueueServer,
LossQueue
)
from queueing_tool.queues.queue_servers import LossQueue


class ResourceAgent(Agent):
Expand Down Expand Up @@ -202,7 +199,7 @@ def _current_color(self, which=0):

color = [i * tmp / 0.9 for i in self.colors['edge_loop_color']]
color[3] = 0.0

elif which == 2:
color = self.colors['vertex_pen_color']
else:
Expand Down
10 changes: 5 additions & 5 deletions queueing_tool/queues/queue_servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,19 +763,19 @@ def simulate(self, n=1, t=None, nA=None, nD=None):
"""
if t is None and nD is None and nA is None:
for k in range(n):
tmp = self.next_event()
self.next_event()
elif t is not None:
then = self._current_t + t
while self._current_t < then and self._time < infty:
tmp = self.next_event()
self.next_event()
elif nD is not None:
nDepartures = self.nDepartures + nD
while self.nDepartures < nDepartures and self._time < infty:
tmp = self.next_event()
self.next_event()
elif nA is not None:
nArrivals = self._oArrivals + nA
while self._oArrivals < nArrivals and self._time < infty:
tmp = self.next_event()
self.next_event()


def __deepcopy__(self, memo):
Expand Down Expand Up @@ -966,7 +966,7 @@ def _add_arrival(self, agent=None):
else:
self.data[agent.agent_id].append([agent._time, 0, 0, 0, 0])

def delay_service(self):
def delay_service(self, *args, **kwargs):
pass

def next_event_description(self):
Expand Down
14 changes: 2 additions & 12 deletions tests/test_graph_generation.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import unittest
try:
import unittest.mock as mock
except ImportError:
import mock

from numpy.random import randint
import networkx as nx
Expand Down Expand Up @@ -45,8 +41,8 @@ def test_graph2dict(self):


def test_add_edge_lengths(self):
g = qt.generate_pagerank_graph(10)
g2 = qt.add_edge_lengths(g)
g = qt.generate_pagerank_graph(10)
g = qt.add_edge_lengths(g)

edge_props = set()
for key in g.edge_properties():
Expand Down Expand Up @@ -79,12 +75,6 @@ def test_adjacency2graph_matrix_adjacency(self):
g = qt.adjacency2graph(adj, edge_type=ety, adjust=2)
ans = qt.graph2dict(g)

expected_response = {
0: {1: {'edge_type': 5}},
1: {2: {'edge_type': 9}, 3: {'edge_type': 0}},
2: {0: {'edge_type': 1}},
3: {}
}
self.assertTrue(ans == self.expected_response1)


Expand Down
4 changes: 2 additions & 2 deletions tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_QueueNetwork_blocking(self):
5: qt.ResourceQueue,
6: qt.QueueServer
}

q_arg = {
3: {'net_size': g.number_of_edges()},
4: {'nServers': 500},
Expand Down Expand Up @@ -210,7 +210,7 @@ def test_QueueNetwork_drawing_animation_error(self):
def test_QueueNetwork_init_error(self):
g = qt.generate_pagerank_graph(7)
with self.assertRaises(TypeError):
qn = qt.QueueNetwork(g, blocking=2)
qt.QueueNetwork(g, blocking=2)

def test_QueueNetwork_get_agent_data(self):

Expand Down
2 changes: 0 additions & 2 deletions tests/test_qndigraph.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import os
import sys
import unittest
try:
import unittest.mock as mock
except ImportError:
import mock

from numpy.random import randint
import networkx as nx
import numpy as np

Expand Down
8 changes: 1 addition & 7 deletions tests/test_queue_server.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import os
import unittest
try:
import unittest.mock as mock
except ImportError:
import mock

import networkx as nx
import numpy as np
Expand Down Expand Up @@ -49,7 +44,7 @@ def test_QueueServer_set_nServers_errors(self):
q.set_nServers(0)

def test_QueueServer_set_inactive(self):

q = qt.QueueServer()
q.set_active()

Expand Down Expand Up @@ -203,7 +198,6 @@ def test_LossQueue_blocking(self):
else :
q.simulate(n=1)

tmp = np.ones(5)
self.assertTrue( ans.all() )


Expand Down
4 changes: 1 addition & 3 deletions tests/test_statistical_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ def test_QueueServer_Littleslaw(self):
q.clear()

ind = data[:,2] > 0
souj = data[ind, 2] - data[ind, 0]
wait = data[ind, 1] - data[ind, 0]
ans = np.mean(wait) * self.lam - np.mean(data[:, 3]) * self.rho

Expand Down Expand Up @@ -135,9 +134,8 @@ def test_poisson_random_measure(self):
# Poisson random variables using a chi-squared test (testing the
# composite null hypothesis). It does not look for independence of the
# random variables.
# This test should fail some percentage of the time

# This test should fail about

rate = lambda t: 0.5 + 4 * np.sin(np.pi * t / 12)**2
arr_f = lambda t: qt.poisson_random_measure(rate, 4.5, t)

Expand Down

0 comments on commit af0205e

Please sign in to comment.