-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchive.py
295 lines (232 loc) · 8.25 KB
/
archive.py
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#! /usr/bin/env python3
# I, Robert Rozanski, the copyright holder of this work, release this work into the public domain. This applies worldwide. In some countries this may not be legally possible; if so: I grant anyone the right to use this work for any purpose, without any conditions, unless such conditions are required by law.
from time import time
from random import choice
from sys import stdout
class Archive:
def __init__(self):
self.development_history = []
self.working_models = set([])
self.known_results = [] # container for Experiment type objects not Result type
self.chosen_experiment_descriptions = []
self.new_result = None # stored here before it's accepted
self.error_flag = False
self.revflag = False
self.all_models_equivalent = False
self.all_models_equivalent_counter = 0 # counts in row; restarts after succesfull design
self.mnm_compartments = [] # to keep track of mnm elements; info for exp design
self.mnm_entities = [] # to keep track of mnm elements
self.mnm_activities = [] # to keep track of mnm elements; info for revision and exp design
self.import_activities = []
self.start_time = time()
self._results_counter = 0
self._models_counter = 0
self.model_of_ref = None
def record(self, event):
event.timestamp = time() - self.start_time
self.development_history.append(event)
if isinstance(event, ChosenExperiment):
self.chosen_experiment_descriptions = event.experiment_descriptions
self.all_models_equivalent_counter = 0
elif isinstance(event, ExpDesignFail):
self.error_flag = True
elif isinstance(event, NewResults):
self.chosen_experiment_descriptions = [] # clearing
event.experiment.ID = self.get_new_exp_id()
for res in event.experiment.results:
res.ID = self.get_new_res_id()
self.new_result = event.experiment
elif isinstance(event, AcceptedResults):
self.new_result = None # clearing
self.known_results.append(event.experiment)
elif isinstance(event, RefutedModels):
self.working_models = self.working_models - set(event.refuted_models)
elif isinstance(event, RevisedModel):
for model in event.revised_models:
model.ID = self.get_new_model_id()
self.working_models = self.working_models | set(event.revised_models)
elif isinstance(event, RedundantModel):
pass # not added, so no need to remove
elif isinstance(event, AllModelsEmpiricallyEquivalent):
self.all_models_equivalent_counter += 1
self.all_models_equivalent = True
max_quality = max([m.quality for m in event.models])
best_models = [m for m in event.models if m.quality == max_quality]
chosen_model = choice(best_models)
self.working_models = set([chosen_model]) # was list, not set
event.model_left = chosen_model
print('all models equivalent!')
stdout.flush()
elif isinstance(event, RevisionFail):
self.revflag = True
self.error_flag = True
elif isinstance(event, RevisedIgnoredUpdate):
pass
elif isinstance(event, CheckPointSuccess):
pass
elif isinstance(event, AdditionalModels):
for model in event.additional_models:
model.ID = self.get_new_model_id()
self.working_models = self.working_models | set(event.additional_models)
elif isinstance(event, AdditModProdFail):
self.revflag = True
self.error_flag = True
elif isinstance(event, UpdatedModelQuality):
pass
elif isinstance(event, InitialModels):
for model in event.models:
model.ID = self.get_new_model_id()
self.working_models = self.working_models | set(event.models)
elif isinstance(event, InitialResults):
for exp in event.experiments:
exp.ID = self.get_new_exp_id()
for exp in event.experiments:
for res in exp.results:
res.ID = self.get_new_res_id()
self.known_results.extend(event.experiments)
elif isinstance(event, CheckPointFail):
self.error_flag = True
else:
raise(TypeError, "Archive: event's type unknown: %s" % type(event))
def get_model_origin_event(self, model): # number of new results covered
for event in self.development_history:
if not (isinstance(event, InitialModels) or isinstance(event, RevisedModel) or isinstance(event, AdditionalModels)):
continue
elif (isinstance(event, InitialModels) and (model in event.models)):
return event
elif (isinstance(event, RevisedModel) and (model in event.revised_models)):
return event
elif (isinstance(event, AdditionalModels) and (model in event.additional_models)):
return event
else:
pass
# if not found
raise ValueError("get_model_origin_event: matching event not found; model: %s" % model.ID)
def get_events_after_event(self, event):
index = self.development_history.index(event)
return self.development_history[index+1:]
def get_results_after_model(self, model):
# doesn't include initial results
event = self.get_model_origin_event(model)
events_after = self.get_events_after_event(event)
results_sets = [event.experiment.results for event in events_after if isinstance(event, NewResults)]
results = []
for res_set in results_sets:
results.extend(list(res_set))
return results
def get_matching_element(self, element_id, element_version=None):
for element in self.mnm_activities:
if element.ID == element_id:
return element
else:
pass
for element in self.mnm_entities:
if ((element.ID == element_id) and (element.version == element_version)):
return element
else:
pass
for element in self.mnm_compartments:
if element.ID == element_id:
return element
else:
pass
for element in self.import_activities:
if element.ID == element_id:
return element
else:
pass
raise ValueError("get_matching_element: matching element not found: ID: %s" % element_id)
def get_matching_result(self, res_id):
for exp in self.known_results:
for res in exp.results:
if res.ID == res_id:
return res
else:
pass
raise ValueError("get_matching_result: matching element not found: ID: %s" % res_id)
def get_new_model_id(self):
ID = 'm_%s' % self._models_counter
self._models_counter += 1
return ID
def get_new_exp_id(self):
return 'exp_%s' % len(self.known_results)
def get_new_res_id(self):
ID = 'res_%s' % self._results_counter
self._results_counter += 1
return ID
def get_new_ent_id(self):
return 'ent_%s' % len(self.mnm_entities)
def get_new_act_id(self):
return 'act_%s' % len(self.mnm_activities + self.import_activities)
class Event:
def __init__(self):
self.timestamp = None
class InitialModels(Event):
# recorded them initial results
def __init__(self, models):
Event.__init__(self)
self.models = models
class InitialResults(Event):
# recorded first
# full experiments
def __init__(self, exps):
Event.__init__(self)
self.experiments = exps
class ChosenExperiment(Event):
# experiment descriptions
def __init__(self, expDs):
Event.__init__(self)
self.experiment_descriptions = expDs
class NewResults(Event):
# full experiment with results
def __init__(self, exp):
Event.__init__(self)
self.experiment = exp
class AcceptedResults(Event):
def __init__(self, exp):
Event.__init__(self)
self.experiment = exp
class RefutedModels(Event):
def __init__(self, models):
Event.__init__(self)
self.refuted_models = frozenset(models)
class RevisedModel(Event):
def __init__(self, old_model, revised_models):
Event.__init__(self)
self.old_model = old_model
self.revised_models = frozenset(revised_models)
class UpdatedModelQuality(Event):
def __init__(self, model, new_quality):
Event.__init__(self)
self.model = model
self.new_quality = new_quality
class AdditionalModels(Event):
def __init__(self, models):
Event.__init__(self)
self.additional_models = frozenset(models)
class RevisionFail(Event):
def __init__(self):
pass
class AdditModProdFail(Event):
def __init__(self):
pass
class ExpDesignFail(Event):
def __init__(self):
pass
class CheckPointFail(Event):
def __init__(self, criterion):
self.criterion = criterion
class CheckPointSuccess(Event):
def __init__(self):
pass
class RevisedIgnoredUpdate(Event):
def __init__(self, model):
self.model = model
class RedundantModel(Event):
def __init__(self, base_model, model):
self.base_model = base_model
self.model = model
class AllModelsEmpiricallyEquivalent(Event):
def __init__(self, models):
self.models = list(models)
self.model_left = None