forked from cta-sst-1m/CTS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunManager.py
executable file
·181 lines (148 loc) · 6.6 KB
/
RunManager.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
#!/usr/bin/env python3
# external modules
from optparse import OptionParser
from yaml import load, dump, load_all
import logging, sys
from setup_fsm.master_fsm import MasterFsm
import time, os
import fysom
# internal modules
from utils import logger
try:
import IPython
except ImportError:
import code
def load_configuration(options):
"""
Merge the interactive options with the yaml options to configure the data taking
:param options:
:return:
"""
# Load the YAML configuration
options_yaml = {}
with open(options.yaml_config) as f:
for data in load_all(f):
options_yaml[data['type']] = {}
for k, v in data.items():
if k == 'type': continue
options_yaml[data['type']][k] = v
# Update with interactive options
for key, val in options.__dict__.items():
if not (key in options_yaml['steering'].keys()):
options_yaml['steering'][key] = val
else:
options_yaml['steering'][key] = val
return options_yaml
def create_directory(options):
"""
Create the output directory containing the logs and output file from the writer
:param options:
:return:
"""
# Check if the directory name was specified, otherwise replace by the steering name
if not options['steering']['output_directory']:
options['steering']['output_directory'] = options['steering']['name']
_tmp_dir = time.strftime(options['steering']['output_directory_basis'] + '/%Y%m%d', time.gmtime())
# If no directory exists for today, then create it
if not os.path.isdir(_tmp_dir):
os.mkdir(_tmp_dir)
options['steering']['output_directory'] = _tmp_dir + '/' + options['steering']['output_directory']
# Create the new directory. If it exist, increment
if not os.path.isdir(options['steering']['output_directory']):
os.mkdir(options['steering']['output_directory'])
else:
list_dir = os.listdir(os.path.dirname(options['steering']['output_directory']))
n_dir = 0
for d in list_dir:
if d.count(os.path.basename(options['steering']['output_directory'])) > 0.5: n_dir += 1
options['steering']['output_directory'] += '_%d' % n_dir
os.mkdir(options['steering']['output_directory'])
if __name__ == '__main__':
"""
The main call
"""
parser = OptionParser()
# Job configuration (the only mandatory option)
parser.add_option("-y", "--yaml_config", dest="yaml_config",
help="full path of the yaml configuration function",
default='/data/software/CTS/setup_config/mts_protocol.yaml')
# Other options allows to overwrite the steering part of the yaml_config interactively
# Output level
parser.add_option("-v", "--verbose",
action="store_false", dest="verbose", default=True,
help="move to debug")
# Steering of the passes
parser.add_option("-o", "--output_directory", dest="output_directory",
help="Where to store the data from this run")
# Steering of the passes
parser.add_option("-b", "--output_directory_basis", dest="output_directory_basis",
help="Basis directory for commissioning data storage", default='/data/datasets/DATA')
# Parse the options
# Parse the options
(options, args) = parser.parse_args()
# Merge the configurations of yaml with the inteactive one
options = load_configuration(options)
# Rename the process
__name__ = 'RunManager'#options['steering']['name']
# Create the directory
create_directory(options)
# Start the loggers
logger.initialise_logger(logname=sys.modules['__main__'].__name__, verbose=options['steering']['verbose'],
logfile='%s/%s.log' % (options['steering']['output_directory'],
options['steering']['name']))
# Some logging
log = logging.getLogger(sys.modules['__main__'].__name__)
log.info('\t\t-|----------------------------------------------------------------------------')
log.info('\t\t-|> Run %s protocol' % options['steering']['name'])
log.info('\t\t-|----------------------------------------------------------------------------')
log.info('\t\t-|> Main configuration :')
log.info('\t\t-|--|> output directory: %s' % options['steering']['output_directory'])
for key, val in options.items():
log.info('\t\t |--|> %s :' % (key))
for key_sub, val_sub in val.items():
log.info('\t\t |----|> %s : \t %s' % (key_sub, val_sub))
log.info('\t\t-|----------------------------------------------------------------------------')
log.info('\033[1m\033[91m\t\t-|> Build the experimental setup\033[0m' )
# Start the master FSM
masterfsm = MasterFsm(options=options, logger_name=sys.modules['__main__'].__name__)
# if a protocol has been defined
if 'protocol_configuration' in options.keys():
if isinstance(options['protocol_configuration']['name'], list ):
for protocol_config in options['protocol_configuration']['name']:
protocol = __import__('setup_protocols.%s' % protocol_config,
locals=None,
globals=None,
fromlist=[None],
level=0)
if protocol.run(masterfsm):
log.info('\t\t-|----------------------------------------------------------------------------')
else:
protocol = __import__('setup_protocols.%s' % options['protocol_configuration']['name'],
locals=None,
globals=None,
fromlist=[None],
level=0)
if protocol.run(masterfsm):
log.info('\t\t-|----------------------------------------------------------------------------')
log.info('\t\t-|> Ready to analyse')
while True:
print('\x1b[?5h')
time.sleep(0.4)
print('\x1b[?5l')
i = input("Press a key")
if not i:
break
else:
try:
masterfsm.allocate()
masterfsm.configure()
masterfsm.start_run()
masterfsm.start_trigger()
time.sleep(30)
masterfsm.stop_trigger()
masterfsm.stop_run()
masterfsm.reset()
masterfsm.deallocate()
IPython.embed()
finally:
print('\t\t-|> Done')