Skip to content

Commit 41ec3ea

Browse files
committed
clean up some code formatting
1 parent 1938661 commit 41ec3ea

34 files changed

+5312
-3670
lines changed

ciw/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@
1717
import ciw.deadlock
1818
import ciw.trackers
1919
import ciw.disciplines
20-
rng = np.random.default_rng()
20+
21+
rng = np.random.default_rng()

ciw/arrival_node.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class ArrivalNode(object):
66
"""
77
Class for the arrival node of the network
88
"""
9+
910
def __init__(self, simulation):
1011
"""
1112
Initialise the arrvial node.
@@ -15,9 +16,10 @@ def __init__(self, simulation):
1516
self.number_of_individuals_per_class = [0] * self.simulation.network.number_of_classes
1617
self.number_accepted_individuals = 0
1718
self.number_accepted_individuals_per_class = [0] * self.simulation.network.number_of_classes
18-
self.event_dates_dict = {nd + 1: {clss: False for clss in range(
19-
self.simulation.network.number_of_classes)}
20-
for nd in range(self.simulation.network.number_of_nodes)}
19+
self.event_dates_dict = {
20+
nd + 1: {clss: False for clss in range(self.simulation.network.number_of_classes)
21+
} for nd in range(self.simulation.network.number_of_nodes)
22+
}
2123

2224
def initialise(self):
2325
self.initialise_event_dates_dict()
@@ -27,7 +29,7 @@ def __repr__(self):
2729
"""
2830
Representation of an arrival node.
2931
"""
30-
return 'Arrival Node'
32+
return "Arrival Node"
3133

3234
def decide_baulk(self, next_node, next_individual):
3335
"""
@@ -38,8 +40,7 @@ def decide_baulk(self, next_node, next_individual):
3840
self.send_individual(next_node, next_individual)
3941
else:
4042
rnd_num = random()
41-
if rnd_num < next_node.baulking_functions[self.next_class](
42-
next_node.number_of_individuals):
43+
if rnd_num < next_node.baulking_functions[self.next_class](next_node.number_of_individuals):
4344
self.record_baulk(next_node, next_individual)
4445
self.simulation.nodes[-1].accept(next_individual, completed=False)
4546
else:
@@ -71,24 +72,22 @@ def have_event(self):
7172
for _ in range(batch):
7273
self.number_of_individuals += 1
7374
self.number_of_individuals_per_class[self.next_class] += 1
74-
priority_class = self.simulation.network.priority_class_mapping[
75-
self.next_class]
75+
priority_class = self.simulation.network.priority_class_mapping[self.next_class]
7676
next_individual = self.simulation.IndividualType(
7777
self.number_of_individuals,
7878
self.next_class,
7979
priority_class,
80-
simulation=self.simulation)
80+
simulation=self.simulation,
81+
)
8182
if self.simulation.network.process_based:
82-
next_individual.route = self.simulation.network.customer_classes[
83-
next_individual.customer_class].routing[self.next_node - 1](next_individual)
83+
next_individual.route = self.simulation.network.customer_classes[next_individual.customer_class].routing[self.next_node - 1](next_individual)
8484
next_node = self.simulation.transitive_nodes[self.next_node - 1]
8585
self.release_individual(next_node, next_individual)
8686

87-
self.event_dates_dict[self.next_node][
88-
self.next_class] = self.increment_time(
89-
self.event_dates_dict[self.next_node][
90-
self.next_class], self.inter_arrival(
91-
self.next_node, self.next_class))
87+
self.event_dates_dict[self.next_node][self.next_class] = self.increment_time(
88+
self.event_dates_dict[self.next_node][self.next_class],
89+
self.inter_arrival(self.next_node, self.next_class),
90+
)
9291
self.find_next_event_date()
9392

9493
def increment_time(self, original, increment):
@@ -107,7 +106,7 @@ def initialise_event_dates_dict(self):
107106
if self.simulation.inter_arrival_times[nd][clss] is not None:
108107
self.event_dates_dict[nd][clss] = self.inter_arrival(nd, clss)
109108
else:
110-
self.event_dates_dict[nd][clss] = float('inf')
109+
self.event_dates_dict[nd][clss] = float("inf")
111110

112111
def inter_arrival(self, nd, clss):
113112
"""
@@ -123,19 +122,19 @@ def batch_size(self, nd, clss):
123122
batch = self.simulation.batch_sizes[nd][clss]._sample(t=self.simulation.current_time)
124123
if isinstance(batch, int) and batch >= 0:
125124
return batch
126-
raise ValueError('Batch sizes must be positive integers.')
125+
raise ValueError("Batch sizes must be positive integers.")
127126

128127
def record_baulk(self, next_node, individual):
129128
"""
130129
Adds an individual to the baulked dictionary.
131130
"""
132-
next_node.write_baulking_or_rejection_record(individual, record_type='baulk')
131+
next_node.write_baulking_or_rejection_record(individual, record_type="baulk")
133132

134133
def record_rejection(self, next_node, individual):
135134
"""
136135
Adds an individual to the rejection dictionary.
137136
"""
138-
next_node.write_baulking_or_rejection_record(individual, record_type='rejection')
137+
next_node.write_baulking_or_rejection_record(individual, record_type="rejection")
139138

140139
def release_individual(self, next_node, next_individual):
141140
"""

ciw/auxiliary.py

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,66 @@
22
import numpy as np
33
import ciw
44

5+
56
def seed(z):
6-
"""
7-
Sets all seeds used to generate random number streams.
8-
Currently contains:
9-
- random library
10-
- numpy random
11-
"""
12-
random.seed(z)
13-
ciw.rng = np.random.default_rng(seed=z)
7+
"""
8+
Sets all seeds used to generate random number streams.
9+
Currently contains:
10+
- random library
11+
- numpy random
12+
"""
13+
random.seed(z)
14+
ciw.rng = np.random.default_rng(seed=z)
15+
1416

1517
def random_choice(array, probs=None):
16-
"""
17-
This function takes in an array of values to make a choice from,
18-
and an pdf corresponding to those values. It returns a random choice
19-
from that array, using the probs as weights.
20-
"""
21-
# If no pdf provided, assume uniform dist:
22-
if probs == None:
23-
index = int(random.random() * len(array))
24-
return array[index]
25-
26-
# A common case, guaranteed to reach the Exit node;
27-
# No need to sample for this:
28-
if (set(probs[:-1]) == set([0.0])) and (probs[-1] == 1.0):
29-
return array[-1]
30-
31-
# Sample a random value from using pdf
32-
rdm_num = random.random()
33-
i, p = 0, probs[0]
34-
while rdm_num > p:
35-
i += 1
36-
p += probs[i]
37-
return array[i]
18+
"""
19+
This function takes in an array of values to make a choice from,
20+
and an pdf corresponding to those values. It returns a random choice
21+
from that array, using the probs as weights.
22+
"""
23+
# If no pdf provided, assume uniform dist:
24+
if probs == None:
25+
index = int(random.random() * len(array))
26+
return array[index]
27+
28+
# A common case, guaranteed to reach the Exit node;
29+
# No need to sample for this:
30+
if (set(probs[:-1]) == set([0.0])) and (probs[-1] == 1.0):
31+
return array[-1]
32+
33+
# Sample a random value from using pdf
34+
rdm_num = random.random()
35+
i, p = 0, probs[0]
36+
while rdm_num > p:
37+
i += 1
38+
p += probs[i]
39+
return array[i]
40+
3841

3942
def truncated_normal(mean, sd):
40-
"""
41-
Sample from a Normal distribution, with mean and standard
42-
deviation (sd). This truncates the distribution at 0 (lower bound
43-
of 0). If samples less than 0 are sampled, they are resampled
43+
"""
44+
Sample from a Normal distribution, with mean and standard
45+
deviation (sd). This truncates the distribution at 0 (lower bound
46+
of 0). If samples less than 0 are sampled, they are resampled
4447
until a positive value is sampled.
45-
"""
46-
sample = random.normalvariate(mean, sd)
47-
while sample <= 0.0:
48-
sample = random.normalvariate(mean, sd)
49-
return sample
48+
"""
49+
sample = random.normalvariate(mean, sd)
50+
while sample <= 0.0:
51+
sample = random.normalvariate(mean, sd)
52+
return sample
53+
5054

5155
def flatten_list(list_of_lists):
52-
flat = []
53-
for a_list in list_of_lists:
54-
flat += a_list
55-
return flat
56+
flat = []
57+
for a_list in list_of_lists:
58+
flat += a_list
59+
return flat
60+
5661

5762
def no_routing(ind):
58-
"""
59-
Process-based routing fucntion that sends the customer straight
60-
to exit node. It is a placeholder for when NoArrivals is used.
61-
"""
62-
return []
63+
"""
64+
Process-based routing fucntion that sends the customer straight
65+
to exit node. It is a placeholder for when NoArrivals is used.
66+
"""
67+
return []

ciw/data_record.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
from collections import namedtuple
22

3-
DataRecord = namedtuple('Record', [
4-
'id_number',
5-
'customer_class',
6-
'original_customer_class',
7-
'node',
8-
'arrival_date',
9-
'waiting_time',
10-
'service_start_date',
11-
'service_time',
12-
'service_end_date',
13-
'time_blocked',
14-
'exit_date',
15-
'destination',
16-
'queue_size_at_arrival',
17-
'queue_size_at_departure',
18-
'server_id',
19-
'record_type'
20-
])
3+
DataRecord = namedtuple(
4+
"Record",
5+
[
6+
"id_number",
7+
"customer_class",
8+
"original_customer_class",
9+
"node",
10+
"arrival_date",
11+
"waiting_time",
12+
"service_start_date",
13+
"service_time",
14+
"service_end_date",
15+
"time_blocked",
16+
"exit_date",
17+
"destination",
18+
"queue_size_at_arrival",
19+
"queue_size_at_departure",
20+
"server_id",
21+
"record_type",
22+
],
23+
)

ciw/deadlock/deadlock_detector.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import networkx as nx
22

3+
34
class NoDetection(object):
45
"""
56
A generic class for all deadlock detector classes to inherit from.
67
Using this class is equivalent to having no deadlock detection
78
capabilities.
89
"""
10+
911
def __init__(self):
1012
"""
1113
Initialises the detection mechanism class.
@@ -23,7 +25,7 @@ def detect_deadlock(self):
2325
Returns True is deadlock is reached, False otherwise.
2426
"""
2527
return False
26-
28+
2729
def action_at_attach_server(self, node, server, individual):
2830
"""
2931
The action taken at the 'attach_server' method of the node.
@@ -54,6 +56,7 @@ class StateDigraph(NoDetection):
5456
contains k.
5557
Deadlock is equivalent to a knot in the directed graph.
5658
"""
59+
5760
def __init__(self):
5861
"""
5962
Initialises the state digraph detection mechanism class.
@@ -65,7 +68,7 @@ def initialise_at_node(self, node):
6568
Initialises the state digraph when the node is created.
6669
Adds the servers of that node if c < Inf.
6770
"""
68-
if node.c < float('Inf'):
71+
if node.c < float("Inf"):
6972
self.statedigraph.add_nodes_from([str(s) for s in node.servers])
7073

7174
def detect_deadlock(self):
@@ -103,8 +106,10 @@ def action_at_attach_server(self, node, server, individual):
103106
it needs to be added back in.
104107
"""
105108
for blq in node.blocked_queue:
106-
inds = [ind for ind in node.simulation.nodes[
107-
blq[0]].all_individuals if ind.id_number == blq[1]]
109+
inds = [
110+
ind for ind in node.simulation.nodes[blq[0]].all_individuals
111+
if ind.id_number == blq[1]
112+
]
108113
ind = inds[0]
109114
if ind != individual:
110115
self.statedigraph.add_edge(str(ind.server), str(server))
@@ -123,8 +128,6 @@ def action_at_detatch_server(self, server):
123128
- Remove any edges of servers who have been detatched.
124129
"""
125130
self.statedigraph.remove_edges_from(
126-
list(
127-
self.statedigraph.in_edges(str(server))
128-
) + list(
129-
self.statedigraph.out_edges(str(server)))
130-
)
131+
list(self.statedigraph.in_edges(str(server)))
132+
+ list(self.statedigraph.out_edges(str(server)))
133+
)
Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
from ciw.auxiliary import random_choice
22

3+
34
def FIFO(individuals):
4-
"""
5-
FIFO: First in first out / First come first served
6-
Returns the individual at the head of the queue
7-
"""
8-
return individuals[0]
5+
"""
6+
FIFO: First in first out / First come first served
7+
Returns the individual at the head of the queue
8+
"""
9+
return individuals[0]
10+
911

1012
def SIRO(individuals):
11-
"""
12-
SIRO: Service in random order
13-
Returns a random individual from the queue
14-
"""
15-
return random_choice(individuals)
13+
"""
14+
SIRO: Service in random order
15+
Returns a random individual from the queue
16+
"""
17+
return random_choice(individuals)
18+
1619

1720
def LIFO(individuals):
18-
"""
19-
LIFO: Last in first out / Last come first served
20-
Returns the individual who joined the queue most recently
21-
"""
22-
return individuals[-1]
21+
"""
22+
LIFO: Last in first out / Last come first served
23+
Returns the individual who joined the queue most recently
24+
"""
25+
return individuals[-1]

0 commit comments

Comments
 (0)