Repository navigation
Expand file tree
/
Copy pathallocator.py
More file actions
257 lines (223 loc) · 9.35 KB
/
Copy pathallocator.py
File metadata and controls
257 lines (223 loc) · 9.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
"""
=========
Allocator
=========
Reads requests from PartionedProcesses, and allocates molecules according to
process priorities.
"""
import numpy as np
from vivarium.core.process import Step
from typing import Any
from ecoli.processes.registries import topology_registry
from ecoli.library.schema import counts, numpy_schema, bulk_name_to_idx, listener_schema
# Register default topology for this process, associating it with process name
NAME = "allocator"
TOPOLOGY = {
"request": ("request",),
"allocate": ("allocate",),
"bulk": ("bulk",),
"listeners": ("listeners",),
"allocator_rng": ("allocator_rng",),
}
topology_registry.register(NAME, TOPOLOGY)
# Register "allocator-1", "allocator-2", "allocator-3" to support
# multi-tiered partitioning scheme
topology_registry.register(NAME + "-1", TOPOLOGY)
topology_registry.register(NAME + "-2", TOPOLOGY)
topology_registry.register(NAME + "-3", TOPOLOGY)
ASSERT_POSITIVE_COUNTS = True
class NegativeCountsError(Exception):
pass
class Allocator(Step):
"""Allocator Step"""
name = NAME
topology = TOPOLOGY
defaults: dict[str, Any] = {}
processes: dict[str, Any] = {}
# Constructor
def __init__(self, parameters=None):
super().__init__(parameters)
self.moleculeNames = self.parameters["molecule_names"]
self.n_molecules = len(self.moleculeNames)
self.mol_name_to_idx = {
name: idx for idx, name in enumerate(self.moleculeNames)
}
self.mol_idx_to_name = {
idx: name for idx, name in enumerate(self.moleculeNames)
}
self.processNames = self.parameters["process_names"]
self.n_processes = len(self.processNames)
self.proc_name_to_idx = {
name: idx for idx, name in enumerate(self.processNames)
}
self.proc_idx_to_name = {
idx: name for idx, name in enumerate(self.processNames)
}
self.processPriorities = np.zeros(len(self.processNames))
for process, custom_priority in self.parameters["custom_priorities"].items():
if process not in self.proc_name_to_idx.keys():
continue
self.processPriorities[self.proc_name_to_idx[process]] = custom_priority
self.seed = self.parameters["seed"]
# Helper indices for Numpy indexing
self.molecule_idx = None
def ports_schema(self):
ports = {
"bulk": numpy_schema("bulk"),
"request": {
process: {
"bulk": {
"_default": [],
"_emit": False,
"_divider": "null",
"_updater": "set",
}
}
for process in self.processNames
},
"allocate": {
process: {
"bulk": {
"_default": [],
"_emit": False,
"_divider": "null",
"_updater": "set",
}
}
for process in self.processNames
},
"listeners": {
"atp": listener_schema(
{
"atp_requested": ([0] * self.n_processes, self.processNames),
"atp_allocated_initial": (
[0] * self.n_processes,
self.processNames,
),
# Use blame functionality to get ATP consumed per process
# 'atp_allocated_final': ([0] * self.n_processes,
# self.processNames)
}
)
},
"allocator_rng": {"_default": np.random.RandomState(seed=self.seed)},
}
return ports
def next_update(self, timestep, states):
if self.molecule_idx is None:
self.molecule_idx = bulk_name_to_idx(
self.moleculeNames, states["bulk"]["id"]
)
self.atp_idx = bulk_name_to_idx("ATP[c]", states["bulk"]["id"])
total_counts = counts(states["bulk"], self.molecule_idx)
original_totals = total_counts.copy()
counts_requested = np.zeros((self.n_molecules, self.n_processes), dtype=int)
# Keep track of which process indices are in current partitioning layer
proc_idx_in_layer = []
for process in states["request"]:
proc_idx = self.proc_name_to_idx[process]
if len(states["request"][process]["bulk"]) > 0:
proc_idx_in_layer.append(proc_idx)
for req_idx, req in states["request"][process]["bulk"]:
counts_requested[req_idx, proc_idx] += req
if ASSERT_POSITIVE_COUNTS and np.any(counts_requested < 0):
raise NegativeCountsError(
"Negative value(s) in counts_requested:\n"
+ "\n".join(
"{} in {} ({})".format(
self.mol_idx_to_name[molIndex],
self.proc_idx_to_name[processIndex],
counts_requested[molIndex, processIndex],
)
for molIndex, processIndex in zip(*np.where(counts_requested < 0))
)
)
# Calculate partition
partitioned_counts = calculatePartition(
self.processPriorities,
counts_requested,
total_counts,
states["allocator_rng"],
)
partitioned_counts.astype(int, copy=False)
if ASSERT_POSITIVE_COUNTS and np.any(partitioned_counts < 0):
raise NegativeCountsError(
"Negative value(s) in partitioned_counts:\n"
+ "\n".join(
"{} in {} ({})".format(
self.mol_idx_to_name[molIndex],
self.proc_idx_to_name[processIndex],
partitioned_counts[molIndex, processIndex],
)
for molIndex, processIndex in zip(*np.where(partitioned_counts < 0))
)
)
# Ensure we are not overdrafting any molecules
counts_unallocated = original_totals - np.sum(partitioned_counts, axis=-1)
if ASSERT_POSITIVE_COUNTS and np.any(counts_unallocated < 0):
raise NegativeCountsError(
"Negative value(s) in counts_unallocated:\n"
+ "\n".join(
"{} ({})".format(
self.mol_idx_to_name[molIndex], counts_unallocated[molIndex]
)
for molIndex in np.where(counts_unallocated < 0)[0]
)
)
# Only update listener ATP counts for processes in
# current partitioning layer
non_zero_mask = counts_requested[self.atp_idx, :] != 0
curr_atp_req = np.array(states["listeners"]["atp"]["atp_requested"]).copy()
curr_atp_alloc = np.array(
states["listeners"]["atp"]["atp_allocated_initial"]
).copy()
curr_atp_req[non_zero_mask] = counts_requested[self.atp_idx, non_zero_mask]
curr_atp_alloc[non_zero_mask] = partitioned_counts[self.atp_idx, non_zero_mask]
update = {
"request": {process: {"bulk": []} for process in states["request"]},
"allocate": {
process: {"bulk": partitioned_counts[:, self.proc_name_to_idx[process]]}
for process in states["request"]
},
"listeners": {
"atp": {
"atp_requested": curr_atp_req,
"atp_allocated_initial": curr_atp_alloc,
}
},
}
return update
def calculatePartition(
process_priorities, counts_requested, total_counts, random_state
):
priorityLevels = np.sort(np.unique(process_priorities))[::-1]
partitioned_counts = np.zeros_like(counts_requested)
for priorityLevel in priorityLevels:
processHasPriority = priorityLevel == process_priorities
requests = counts_requested[:, processHasPriority].copy()
total_requested = requests.sum(axis=1)
excess_request_mask = (total_requested > total_counts) & (total_requested > 0)
# Get fractional request for molecules that have excess request
# compared to available counts
fractional_requests = (
requests[excess_request_mask, :]
* total_counts[excess_request_mask, np.newaxis]
/ total_requested[excess_request_mask, np.newaxis]
)
# Distribute fractional counts to ensure full allocation of excess
# request molecules
remainders = fractional_requests % 1
options = np.arange(remainders.shape[1])
for idx, remainder in enumerate(remainders):
total_remainder = remainder.sum()
count = int(np.round(total_remainder))
if count > 0:
allocated_indices = random_state.choice(
options, size=count, p=remainder / total_remainder, replace=False
)
fractional_requests[idx, allocated_indices] += 1
requests[excess_request_mask, :] = fractional_requests
allocations = requests.astype(np.int64)
partitioned_counts[:, processHasPriority] = allocations
total_counts -= allocations.sum(axis=1)
return partitioned_counts