-
Notifications
You must be signed in to change notification settings - Fork 1
/
comparison.py
154 lines (135 loc) · 5.8 KB
/
comparison.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division, print_function
import sys
import os
import shutil
from ConfigParser import SafeConfigParser
from optparse import OptionParser
from scripts.component_handler import Component
import scripts.config_parser_helper as ch
from scripts.comparison_plotter import ComparisonPlotter
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("-n", "--config_name",
dest="config_name",
type="str",
default=None)
parser.add_option("-c", "--config_file",
dest="config_file",
type="str",
default=None)
parser.add_option("-p", "--show_possible", action="store_true",
dest="possible",
default=False)
parser.add_option("-f", "--force", action="store_true",
dest="force",
default=False)
(opts, args) = parser.parse_args()
if opts.config_name is not None:
config_name = opts.config_name
config_file = os.path.realpath('configs/%s.ini' % opts.config_name)
elif opts.config_file is not None:
config_file = os.path.realpath(opts.config_file)
config_name = os.path.basename(config_file)
config_name = config_name.replace('.ini', '')
else:
raise ValueError('Either option -c or -n is needed!')
assert os.path.isfile(config_file), '%s not found'
config = SafeConfigParser()
l = config.read(config_file)
outpath = None
if config.has_option('General', 'Outpath'):
outpath_opt = config.get('General', 'Outpath')
if outpath_opt not in ['None', 'Default']:
outpath_opt = os.path.realpath(outpath_opt)
outpath = outpath_opt
if outpath is None:
outpath = os.path.realpath('output/%s' % config_name)
if not opts.force:
if os.path.isdir(outpath):
i = 1
while os.path.isdir(outpath + '_(%d)' % i):
i += 1
outpath = outpath + '_(%d)' % i
os.makedirs(outpath)
else:
if not os.path.isdir(outpath):
os.makedirs(outpath)
shutil.copy(config_file, outpath)
components = []
components_opts = ch.convert_list(config.get('General', 'Components'))
for c in components_opts:
assert config.has_section(c), \
'For each component a section is neede\'%s\' missing!' % c
component_dict = dict(config.items(c))
curr_component = Component(c, component_dict)
if curr_component not in components:
components.append(curr_component)
if curr_component.aggregation is not None:
for i, c_a in enumerate(curr_component.aggregation.participant):
if c_a not in components:
component_dict = dict(config.items(c_a))
components.append(Component(c_a, component_dict))
index = components.index(c_a)
if not curr_component.aggregation.keep_components:
components[index].show = False
curr_component.aggregation.participant[i] = components[index]
if config.has_option('General', 'IDKeys'):
id_keys_opts = config.get('General', 'IDKeys')
id_keys = ch.convert_list(id_keys_opts)
else:
id_keys = []
obs = '*'
if config.has_option('General', 'Observables'):
obs_opts = config.get('General', 'Observables')
if obs_opts not in ['all', '*']:
obs = ch.convert_list(obs_opts)
if config.has_option('General', 'Uncertainties'):
uncertainties_opts = config.get('General', 'Uncertainties')
uncertainties = ch.convert_list(uncertainties_opts)
for c in uncertainties:
components[components.index(c)].calc_uncert(True)
alphas_ops = config.get('General', 'Alphas')
alphas = [float(a) for a in ch.convert_list(alphas_ops)]
else:
alphas = []
comp_plotter = ComparisonPlotter(components,
id_keys,
match=False,
n_bins=50)
if config.has_option('General', 'Title'):
title = config.get('General', 'Title')
else:
title = ''
if opts.possible or obs == '*':
blacklist = {'obs': [],
'tabs': [],
'cols': []}
if config.has_section('Blacklist'):
if config.has_option('Blacklist', 'Observables'):
blacklist_obs_opts = config.get('Blacklist',
'Observables')
if blacklist_obs_opts != ['None', 'none']:
blacklist['obs'].extend(
ch.convert_list(blacklist_obs_opts))
if config.has_option('Blacklist', 'Tables'):
blacklist_tabs_opts = config.get('Blacklist',
'Tables')
if blacklist_tabs_opts != ['None', 'none']:
blacklist['tabs'].extend(
ch.convert_list(blacklist_tabs_opts))
if config.has_option('Blacklist', 'Columns'):
blacklist_tabs_opts = config.get('Blacklist',
'Columns')
if blacklist_tabs_opts != ['None', 'none']:
blacklist['cols'].extend(
ch.convert_list(blacklist_tabs_opts))
obs = comp_plotter.get_possiblites(outpath=outpath,
blacklist=blacklist)
if opts.possible:
sys.exit()
comp_plotter.plot(title=title,
observables=obs,
outpath=outpath,
alphas=alphas)