|
1 | 1 | #!/usr/bin/env python
|
2 | 2 |
|
3 |
| -from __future__ import absolute_import |
4 |
| -from __future__ import print_function |
5 |
| -import os |
6 |
| -import subprocess |
7 |
| -import getdist |
8 |
| -import io |
9 |
| -from getdist import MCSamples, chains, IniFile |
10 |
| - |
11 |
| - |
12 |
| -def runScript(fname): |
13 |
| - subprocess.Popen(['python', fname]) |
14 |
| - |
15 |
| - |
16 |
| -def doError(msg): |
17 |
| - if __name__ == '__main__': |
18 |
| - import sys |
19 |
| - |
20 |
| - print(msg) |
21 |
| - sys.exit() |
22 |
| - raise ValueError(msg) |
23 |
| - |
24 |
| - |
25 |
| -def main(args): |
26 |
| - no_plots = False |
27 |
| - chain_root = args.chain_root |
28 |
| - if args.ini_file is None and chain_root is None: |
29 |
| - doError('Must give either a .ini file of parameters or a chain file root name. Run "GetDist.py -h" for help.') |
30 |
| - if not '.ini' in args.ini_file and chain_root is None: |
31 |
| - # use default settings acting on chain_root, no plots |
32 |
| - chain_root = args.ini_file |
33 |
| - args.ini_file = getdist.default_getdist_settings |
34 |
| - no_plots = True |
35 |
| - if not os.path.isfile(args.ini_file): |
36 |
| - doError('Parameter file does not exist: ' + args.ini_file) |
37 |
| - if chain_root and chain_root.endswith('.txt'): |
38 |
| - chain_root = chain_root[:-4] |
39 |
| - |
40 |
| - if chain_root is not None and ('*' in chain_root or '?' in chain_root): |
41 |
| - import glob, copy |
42 |
| - for f in glob.glob(chain_root + '.paramnames'): |
43 |
| - fileargs = copy.copy(args) |
44 |
| - fileargs.chain_root = f.replace('.paramnames', '') |
45 |
| - main(fileargs) |
46 |
| - return |
47 |
| - |
48 |
| - # Input parameters |
49 |
| - ini = IniFile(args.ini_file) |
50 |
| - |
51 |
| - # File root |
52 |
| - if chain_root is not None: |
53 |
| - in_root = chain_root |
54 |
| - else: |
55 |
| - in_root = ini.params['file_root'] |
56 |
| - if not in_root: |
57 |
| - doError('Chain Root file name not given ') |
58 |
| - rootname = os.path.basename(in_root) |
59 |
| - |
60 |
| - if args.ignore_rows is not None: |
61 |
| - ignorerows = args.ignore_rows |
62 |
| - else: |
63 |
| - ignorerows = ini.float('ignore_rows', 0.0) |
64 |
| - |
65 |
| - samples_are_chains = ini.bool('samples_are_chains', True) |
66 |
| - |
67 |
| - paramnames = ini.string('parameter_names', '') |
68 |
| - |
69 |
| - # Create instance of MCSamples |
70 |
| - mc = MCSamples(in_root, ini=ini, files_are_chains=samples_are_chains, paramNamesFile=paramnames) |
71 |
| - |
72 |
| - if ini.bool('adjust_priors', False) or ini.bool('map_params', False): |
73 |
| - doError( |
74 |
| - 'To adjust priors or define new parameters, use a separate python script; see the python getdist docs for examples') |
75 |
| - |
76 |
| - plot_ext = ini.string('plot_ext', 'py') |
77 |
| - finish_run_command = ini.string('finish_run_command', '') |
78 |
| - |
79 |
| - no_plots = ini.bool('no_plots', no_plots) |
80 |
| - plots_only = ini.bool('plots_only', False) |
81 |
| - no_tests = plots_only or ini.bool('no_tests', False) |
82 |
| - |
83 |
| - thin_factor = ini.int('thin_factor', 0) |
84 |
| - thin_cool = ini.float('thin_cool', 1.0) |
85 |
| - |
86 |
| - make_single_samples = ini.bool('make_single_samples', False) |
87 |
| - single_thin = ini.int('single_thin', 1) |
88 |
| - cool = ini.float('cool', 1.0) |
89 |
| - |
90 |
| - chain_exclude = ini.int_list('exclude_chain') |
91 |
| - |
92 |
| - shade_meanlikes = ini.bool('shade_meanlikes', False) |
93 |
| - plot_meanlikes = ini.bool('plot_meanlikes', False) |
94 |
| - |
95 |
| - dumpNDbins = ini.bool('dump_ND_bins', False) |
96 |
| - |
97 |
| - out_dir = ini.string('out_dir', './') |
98 |
| - if out_dir: |
99 |
| - if not os.path.isdir(out_dir): |
100 |
| - os.mkdir(out_dir) |
101 |
| - print('producing files in directory ', out_dir) |
102 |
| - mc.out_dir = out_dir |
103 |
| - |
104 |
| - out_root = ini.string('out_root', '') |
105 |
| - if out_root: |
106 |
| - rootname = out_root |
107 |
| - print('producing files with with root ', out_root) |
108 |
| - mc.rootname = rootname |
109 |
| - |
110 |
| - rootdirname = os.path.join(out_dir, rootname) |
111 |
| - mc.rootdirname = rootdirname |
112 |
| - |
113 |
| - if 'do_minimal_1d_intervals' in ini.params: |
114 |
| - doError('do_minimal_1d_intervals no longer used; set credible_interval_threshold instead') |
115 |
| - |
116 |
| - line = ini.string('PCA_params', '') |
117 |
| - if line.lower() == 'all': |
118 |
| - PCA_params = mc.paramNames.list() |
119 |
| - else: |
120 |
| - PCA_params = line.split() |
121 |
| - PCA_num = ini.int('PCA_num', len(PCA_params)) |
122 |
| - if PCA_num != 0: |
123 |
| - if PCA_num < 2: |
124 |
| - doError('Can only do PCA for 2 or more parameters') |
125 |
| - PCA_func = ini.string('PCA_func', '') |
126 |
| - # Characters representing functional mapping |
127 |
| - if PCA_func == '': |
128 |
| - PCA_func = ['N'] * PCA_num # No mapping |
129 |
| - PCA_NormParam = ini.string('PCA_normparam', '') or None |
130 |
| - |
131 |
| - make_scatter_samples = ini.bool('make_scatter_samples', False) |
132 |
| - |
133 |
| - # ============================================================================== |
134 |
| - |
135 |
| - first_chain = ini.int('first_chain', 0) |
136 |
| - last_chain = ini.int('chain_num', -1) |
137 |
| - # -1 means keep reading until one not found |
138 |
| - |
139 |
| - # Chain files |
140 |
| - chain_files = chains.chainFiles(in_root, first_chain=first_chain, last_chain=last_chain, |
141 |
| - chain_exclude=chain_exclude) |
142 |
| - |
143 |
| - mc.loadChains(in_root, chain_files) |
144 |
| - |
145 |
| - mc.removeBurnFraction(ignorerows) |
146 |
| - if chains.print_load_details: |
147 |
| - if ignorerows: |
148 |
| - print('Removed %s as burn in' % ignorerows) |
149 |
| - else: |
150 |
| - print('Removed no burn in') |
| 3 | +# Once installed this is not used, same as getdist script |
151 | 4 |
|
152 |
| - mc.deleteFixedParams() |
153 |
| - mc.makeSingle() |
154 |
| - |
155 |
| - def filterParList(namestring, num=None): |
156 |
| - if not namestring.strip(): |
157 |
| - pars = mc.paramNames.list() |
158 |
| - else: |
159 |
| - pars = [] |
160 |
| - for name in namestring.split(): |
161 |
| - if '?' in name or '*' in name: |
162 |
| - pars += mc.paramNames.getMatches(name, strings=True) |
163 |
| - elif mc.paramNames.parWithName(name): |
164 |
| - pars.append(name) |
165 |
| - if num is not None and len(pars) != num: |
166 |
| - print('%iD plot has missing parameter or wrong number of parameters: %s' % (num, pars)) |
167 |
| - pars = None |
168 |
| - return pars |
169 |
| - |
170 |
| - if cool != 1: |
171 |
| - print('Cooling chains by ', cool) |
172 |
| - mc.cool(cool) |
173 |
| - |
174 |
| - mc.updateBaseStatistics() |
175 |
| - |
176 |
| - if not no_tests: |
177 |
| - mc.getConvergeTests(mc.converge_test_limit, writeDataToFile=True, feedback=True) |
178 |
| - |
179 |
| - mc.writeCovMatrix() |
180 |
| - mc.writeCorrelationMatrix() |
181 |
| - |
182 |
| - # Output thinned data if requested |
183 |
| - # Must do this with unsorted output |
184 |
| - if thin_factor > 1: |
185 |
| - thin_ix = mc.thin_indices(thin_factor) |
186 |
| - filename = rootdirname + '_thin.txt' |
187 |
| - mc.writeThinData(filename, thin_ix, thin_cool) |
188 |
| - |
189 |
| - print(mc.getNumSampleSummaryText().strip()) |
190 |
| - if mc.likeStats: print(mc.likeStats.likeSummary().strip()) |
191 |
| - |
192 |
| - if PCA_num > 0 and not plots_only: |
193 |
| - mc.PCA(PCA_params, PCA_func, PCA_NormParam, writeDataToFile=True) |
194 |
| - |
195 |
| - if not no_plots or dumpNDbins: |
196 |
| - # set plot_data_dir before we generate the 1D densities below |
197 |
| - plot_data_dir = ini.string('plot_data_dir', default='', allowEmpty=True) |
198 |
| - if plot_data_dir and not os.path.isdir(plot_data_dir): |
199 |
| - os.mkdir(plot_data_dir) |
200 |
| - else: |
201 |
| - plot_data_dir = None |
202 |
| - mc.plot_data_dir = plot_data_dir |
203 |
| - |
204 |
| - # Do 1D bins |
205 |
| - mc._setDensitiesandMarge1D(writeDataToFile=not no_plots and plot_data_dir, meanlikes=plot_meanlikes) |
206 |
| - |
207 |
| - if not no_plots: |
208 |
| - # Output files for 1D plots |
209 |
| - if plot_data_dir: print('Calculating plot data...') |
210 |
| - |
211 |
| - plotparams = [] |
212 |
| - line = ini.string('plot_params', '') |
213 |
| - if line not in ['', '0']: |
214 |
| - plotparams = filterParList(line) |
215 |
| - |
216 |
| - line = ini.string('plot_2D_param', '').strip() |
217 |
| - plot_2D_param = None |
218 |
| - if line and line != '0': |
219 |
| - plot_2D_param = line |
220 |
| - |
221 |
| - cust2DPlots = [] |
222 |
| - if not plot_2D_param: |
223 |
| - # Use custom array of specific plots |
224 |
| - num_cust2D_plots = ini.int('plot_2D_num', 0) |
225 |
| - for i in range(1, num_cust2D_plots + 1): |
226 |
| - line = ini.string('plot' + str(i)) |
227 |
| - pars = filterParList(line, 2) |
228 |
| - if pars is not None: |
229 |
| - cust2DPlots.append(pars) |
230 |
| - else: |
231 |
| - num_cust2D_plots -= 1 |
232 |
| - |
233 |
| - triangle_params = [] |
234 |
| - triangle_plot = ini.bool('triangle_plot', False) |
235 |
| - if triangle_plot: |
236 |
| - line = ini.string('triangle_params', '') |
237 |
| - triangle_params = filterParList(line) |
238 |
| - triangle_num = len(triangle_params) |
239 |
| - triangle_plot = triangle_num > 1 |
240 |
| - |
241 |
| - num_3D_plots = ini.int('num_3D_plots', 0) |
242 |
| - plot_3D = [] |
243 |
| - for ix in range(1, num_3D_plots + 1): |
244 |
| - line = ini.string('3D_plot' + str(ix)) |
245 |
| - pars = filterParList(line, 3) |
246 |
| - if pars is not None: |
247 |
| - plot_3D.append(pars) |
248 |
| - else: |
249 |
| - num_3D_plots -= 1 |
250 |
| - |
251 |
| - # Produce file of weight-1 samples if requested |
252 |
| - if (num_3D_plots and not make_single_samples or make_scatter_samples) and not no_plots: |
253 |
| - make_single_samples = True |
254 |
| - single_thin = max(1, int(round(mc.norm / mc.max_mult)) // mc.max_scatter_points) |
255 |
| - |
256 |
| - if plot_data_dir: |
257 |
| - if make_single_samples: |
258 |
| - filename = os.path.join(plot_data_dir, rootname.strip() + '_single.txt') |
259 |
| - mc.makeSingleSamples(filename, single_thin) |
260 |
| - |
261 |
| - # Write paramNames file |
262 |
| - mc.getParamNames().saveAsText(os.path.join(plot_data_dir, rootname + '.paramnames')) |
263 |
| - mc.getBounds().saveToFile(os.path.join(plot_data_dir, rootname + '.bounds')) |
264 |
| - |
265 |
| - make_plots = ini.bool('make_plots', False) or args.make_plots |
266 |
| - |
267 |
| - done2D = {} |
268 |
| - |
269 |
| - filename = rootdirname + '.' + plot_ext |
270 |
| - mc.writeScriptPlots1D(filename, plotparams) |
271 |
| - if make_plots: runScript(filename) |
272 |
| - |
273 |
| - # Do 2D bins |
274 |
| - if plot_2D_param == 'corr': |
275 |
| - # In this case output the most correlated variable combinations |
276 |
| - print('...doing 2D plots for most correlated variables') |
277 |
| - cust2DPlots = mc.getCorrelatedVariable2DPlots() |
278 |
| - plot_2D_param = None |
279 |
| - elif plot_2D_param: |
280 |
| - mc.paramNames.parWithName(plot_2D_param, error=True) # just check |
281 |
| - |
282 |
| - if cust2DPlots or plot_2D_param: |
283 |
| - print('...producing 2D plots') |
284 |
| - filename = rootdirname + '_2D.' + plot_ext |
285 |
| - done2D = mc.writeScriptPlots2D(filename, plot_2D_param, cust2DPlots, |
286 |
| - writeDataToFile=plot_data_dir, shade_meanlikes=shade_meanlikes) |
287 |
| - if make_plots: runScript(filename) |
288 |
| - |
289 |
| - if triangle_plot: |
290 |
| - # Add the off-diagonal 2D plots |
291 |
| - print('...producing triangle plot') |
292 |
| - filename = rootdirname + '_tri.' + plot_ext |
293 |
| - mc.writeScriptPlotsTri(filename, triangle_params) |
294 |
| - for i, p2 in enumerate(triangle_params): |
295 |
| - for p1 in triangle_params[i + 1:]: |
296 |
| - if not done2D.get((p1, p2)) and plot_data_dir: |
297 |
| - mc.get2DDensityGridData(p1, p2, writeDataToFile=True, meanlikes=shade_meanlikes) |
298 |
| - if make_plots: runScript(filename) |
299 |
| - |
300 |
| - # Do 3D plots (i.e. 2D scatter plots with coloured points) |
301 |
| - if num_3D_plots: |
302 |
| - print('...producing ', num_3D_plots, '2D colored scatter plots') |
303 |
| - filename = rootdirname + '_3D.' + plot_ext |
304 |
| - mc.writeScriptPlots3D(filename, plot_3D) |
305 |
| - if make_plots: runScript(filename) |
306 |
| - |
307 |
| - if not plots_only: |
308 |
| - # Write out stats marginalized |
309 |
| - mc.getMargeStats().saveAsText(rootdirname + '.margestats') |
310 |
| - |
311 |
| - # Limits from global likelihood |
312 |
| - if mc.loglikes is not None: mc.getLikeStats().saveAsText(rootdirname + '.likestats') |
313 |
| - |
314 |
| - if dumpNDbins: |
315 |
| - mc.num_bins_ND = ini.int('num_bins_ND', 10) |
316 |
| - line = ini.string('ND_params', '') |
317 |
| - |
318 |
| - if line not in ["", '0']: |
319 |
| - ND_params = filterParList(line) |
320 |
| - print(ND_params) |
321 |
| - |
322 |
| - ND_dim = len(ND_params) |
323 |
| - print(ND_dim) |
324 |
| - |
325 |
| - mc.getRawNDDensityGridData(ND_params, writeDataToFile=True, |
326 |
| - meanlikes=shade_meanlikes) |
327 |
| - |
328 |
| - # System command |
329 |
| - if finish_run_command: |
330 |
| - finish_run_command = finish_run_command.replace('%ROOTNAME%', rootname) |
331 |
| - finish_run_command = finish_run_command.replace('%PLOTDIR%', plot_data_dir) |
332 |
| - finish_run_command = finish_run_command.replace('%PLOTROOT%', os.path.join(plot_data_dir, rootname)) |
333 |
| - os.system(finish_run_command) |
| 5 | +import sys |
| 6 | +import os |
334 | 7 |
|
| 8 | +sys.path.append(os.path.realpath(os.path.dirname(__file__))) |
335 | 9 |
|
336 |
| -if __name__ == '__main__': |
337 |
| - try: |
338 |
| - import argparse |
339 |
| - except ImportError: |
340 |
| - print('Make sure you are using python 2.7+') |
341 |
| - raise |
| 10 | +from getdist.command_line import getdist_command |
342 | 11 |
|
343 |
| - parser = argparse.ArgumentParser(description='GetDist sample analyser') |
344 |
| - parser.add_argument('ini_file', nargs='?', |
345 |
| - help='.ini file with analysis settings (optional, if omitted uses defaults)') |
346 |
| - parser.add_argument('chain_root', nargs='?', |
347 |
| - help='Root name of chain to analyse (e.g. chains/test), required unless file_root specified in ini_file') |
348 |
| - parser.add_argument('--ignore_rows', |
349 |
| - help='set initial fraction of chains to cut as burn in (fraction of total rows, or >1 number of rows); overrides any value in ini_file if set') |
350 |
| - parser.add_argument('--make_param_file', |
351 |
| - help='Produce a sample distparams.ini file that you can edit and use when running GetDist') |
352 |
| - parser.add_argument('--make_plots', action='store_true', help='Make PDFs from any requested plot script files') |
353 |
| - parser.add_argument('-V', '--version', action='version', version='%(prog)s ' + getdist.__version__) |
354 |
| - args = parser.parse_args() |
355 |
| - if args.make_param_file: |
356 |
| - content = io.open(getdist.distparam_template).read() |
357 |
| - analysis = io.open(getdist.default_getdist_settings).read() |
358 |
| - content = content.replace('%%%ANALYSIS_DEFAULTS%%%', analysis) |
359 |
| - with io.open(args.make_param_file, 'w') as f: |
360 |
| - f.write(content) |
361 |
| - print('Template .ini file written to ' + args.make_param_file) |
362 |
| - else: |
363 |
| - main(args) |
| 12 | +getdist_command() |
0 commit comments