-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_simulation.py
117 lines (96 loc) · 3.4 KB
/
run_simulation.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
import logging
log = logging.getLogger("main")
import sys
from optparse import OptionParser
from yeasty import config, script, context, simulation
try:
from yeasty.visual_progress import Progress as VisualProgress
except:
VisualProgress = None
def configure_options():
usage = """usage: python %prog [options] <foldername>"""
parser = OptionParser(usage)
parser.add_option(
"-v", "--verbose",
action="store_true", dest="verbose",
help="show verbose (debug) output")
parser.add_option(
"-c", "--clean",
action="store_true", dest="clean",
help="Clean any previous output")
parser.add_option(
"-g", "--graphics=",
action="store_true", dest="graphics",
help="show graphics")
parser.add_option(
"-u", "--updates",
type="int", dest="updates", default=100, metavar="N",
help="show updates every N steps")
return parser
def configure_logging():
# TODO Add additional logger in the output folder
handler = logging.StreamHandler(sys.stdout)
# format = "%(name)-15s | %(levelname)-8s | %(asctime)s | %(message)s"
format = "%(name)-15s | %(levelname)-8s | %(message)s"
formatter = logging.Formatter(format)
handler.setFormatter(formatter)
root = logging.getLogger("")
root.addHandler(handler)
root.setLevel(logging.INFO)
class TextProgress(object):
def __init__(self, every=100):
self.every = every
self.running = True
self.paused = False
def begin(self, sim):
log.info("Begin {0.treatment.name}, {0.replicate} -----------".format(sim))
self.count = 0
def update(self, sim):
if self.count % self.every == 0:
text = "\tTreat:{0.treatment.name}, Rep:{0.replicate}, Step:{0.time_step}, Cell Count:{0.cell_count}".format(sim)
log.info(text)
self.count += 1
def interact(self, sim):
pass
def end(self, sim):
log.info("----------------------")
def main():
configure_logging()
parser = configure_options()
options, args = parser.parse_args()
# We should have one argument: the folder to read the configuration from
if not args:
# Otherwise exit, printing the help
parser.print_help()
return 2
script_path = args[0]
log.info("Starting up...")
# Load, using the first argument as the folder
cfg = config.Configuration(options.clean)
ctx = context.Context(cfg)
spt = script.Script(ctx)
# For now, we just turn on debugging
if options.verbose:
logging.getLogger("").setLevel(logging.DEBUG)
# This loads the script using the given context, which puts stuff into the
# config
if spt.load(script_path):
# TODO cfg.validate()
# Override settings
progress = [TextProgress(options.updates)]
if options.graphics:
if VisualProgress is None:
log.warning("Can't load graphics -- you probably need to install pygame")
else:
progress.append(VisualProgress(options.updates))
try:
cfg.experiment.run(progress)
return 0
except KeyboardInterrupt:
log.error("User interrupted the Program")
except simulation.SimulationInterrupt:
pass
return 1
if __name__ == "__main__":
# Well behaved unix programs exits with 0 on success...
sys.exit(main())