forked from lesgourg/class_public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPU.py
executable file
·504 lines (433 loc) · 17.3 KB
/
CPU.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
"""
.. module:: CPU
:synopsis: CPU, a CLASS Plotting Utility
.. moduleauthor:: Benjamin Audren <[email protected]>
.. version:: 2.0
This is a small python program aimed to gain time when comparing two spectra,
i.e. from CAMB and CLASS, or a non-linear spectrum to a linear one. It is
designed to be used in a command line fashion, not being restricted to your
CLASS directory, though it recognized mainly CLASS output format. Far from
perfect, or complete, it could use any suggestion for enhancing it, just to
avoid losing time on useless matters for others. Be warned that, when
comparing with other format, the following is assumed: there are no empty line
(especially at the end of file). Gnuplot comment lines (starting with a # are
allowed). This issue will cause a non-very descriptive error in CPU, any
suggestion for testing it is welcome. Example of use: To superimpose two
different spectra and see their global shape :
python CPU.py output/lcdm_z2_pk.dat output/lncdm_z2_pk.dat
To see in details their ratio:
python CPU.py output/lcdm_z2_pk.dat output/lncdm_z2_pk.dat -r
"""
import numpy as np
from numpy import ma
MaskedArray = ma.MaskedArray
from scipy.interpolate import splrep, splev
import os
import matplotlib.pyplot as plt
import sys
import argparse
import itertools
from math import floor
from matplotlib import scale as mscale
from matplotlib.transforms import Transform
from matplotlib.ticker import FixedLocator
START_LINE = {}
START_LINE['error'] = [r' /|\ ',
r'/_o_\ ',
r' ']
START_LINE['warning'] = [r' /!\ ',
r' ']
START_LINE['info'] = [r' /!\ ',
r' ']
STANDARD_LENGTH = 80 # standard, increase if you have a big screen
def create_parser():
parser = argparse.ArgumentParser(
description=(
'CPU, a CLASS Plotting Utility, specify wether you want\n'
'to superimpose, or plot the ratio of different files.'),
epilog=(
'A standard usage would be, for instance:\n'
'python CPU.py output/test_pk.dat output/test_pk_nl_density.dat'
' -r\npython CPU.py output/wmap_cl.dat output/planck_cl.dat'),
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'files', type=str, nargs='*', help='Files to plot')
parser.add_argument('-r', '--ratio', dest='ratio', action='store_true',
help='Plot the ratio of the spectra')
parser.add_argument('-s', '--selection', dest='selection',
nargs='+',
help='specify the fields you want to plot.')
parser.add_argument('--scale', type=str,
choices=['lin', 'loglog', 'loglin', 'george'],
help='Specify the scale to use for the plot')
parser.add_argument('--xlim', dest='xlim', nargs='+', type=float,
default=[], help='Specify the x range')
parser.add_argument(
'-p, --print',
dest='printfile', action='store_true', default=False,
help='print the graph directly in a .pdf file')
parser.add_argument(
'-r, --repeat',
dest='repeat', action='store_true', default=False,
help='repeat the step for all redshifts with same base name')
return parser
# Helper code from cosmo_mini_toolbox, by Jesus Torrado, available fully at
# https://github.com/JesusTorrado/cosmo_mini_toolbox, to use the log then
# linear scale for the multipole axis when plotting Cl.
nonpos = "mask"
change = 50.0
factor = 500.
def _mask_nonpos(a):
"""
Return a Numpy masked array where all non-positive 1 are
masked. If there are no non-positive, the original array
is returned.
"""
mask = a <= 0.0
if mask.any():
return ma.MaskedArray(a, mask=mask)
return a
def _clip_smaller_than_one(a):
a[a <= 0.0] = 1e-300
return a
class PlanckScale(mscale.ScaleBase):
"""
Scale used by the Planck collaboration to plot Temperature power spectra:
base-10 logarithmic up to l=50, and linear from there on.
Care is taken so non-positive values are not plotted.
"""
name = 'planck'
def __init__(self, axis, **kwargs):
pass
def set_default_locators_and_formatters(self, axis):
axis.set_major_locator(
FixedLocator(
np.concatenate((np.array([2, 10, change]),
np.arange(500, 2500, 500)))))
axis.set_minor_locator(
FixedLocator(
np.concatenate((np.arange(2, 10),
np.arange(10, 50, 10),
np.arange(floor(change/100), 2500, 100)))))
def get_transform(self):
"""
Return a :class:`~matplotlib.transforms.Transform` instance
appropriate for the given logarithm base.
"""
return self.PlanckTransform(nonpos)
def limit_range_for_scale(self, vmin, vmax, minpos):
"""
Limit the domain to positive values.
"""
return (vmin <= 0.0 and minpos or vmin,
vmax <= 0.0 and minpos or vmax)
class PlanckTransform(Transform):
input_dims = 1
output_dims = 1
is_separable = True
has_inverse = True
def __init__(self, nonpos):
Transform.__init__(self)
if nonpos == 'mask':
self._handle_nonpos = _mask_nonpos
else:
self._handle_nonpos = _clip_nonpos
def transform_non_affine(self, a):
lower = a[np.where(a<=change)]
greater = a[np.where(a> change)]
if lower.size:
lower = self._handle_nonpos(lower * 10.0)/10.0
if isinstance(lower, MaskedArray):
lower = ma.log10(lower)
else:
lower = np.log10(lower)
lower = factor*lower
if greater.size:
greater = (factor*np.log10(change) + (greater-change))
# Only low
if not(greater.size):
return lower
# Only high
if not(lower.size):
return greater
return np.concatenate((lower, greater))
def inverted(self):
return PlanckScale.InvertedPlanckTransform()
class InvertedPlanckTransform(Transform):
input_dims = 1
output_dims = 1
is_separable = True
has_inverse = True
def transform_non_affine(self, a):
lower = a[np.where(a<=factor*np.log10(change))]
greater = a[np.where(a> factor*np.log10(change))]
if lower.size:
if isinstance(lower, MaskedArray):
lower = ma.power(10.0, lower/float(factor))
else:
lower = np.power(10.0, lower/float(factor))
if greater.size:
greater = (greater + change - factor*np.log10(change))
# Only low
if not(greater.size):
return lower
# Only high
if not(lower.size):
return greater
return np.concatenate((lower, greater))
def inverted(self):
return PlanckTransform()
# Finished. Register the scale!
mscale.register_scale(PlanckScale)
def plot_CLASS_output(files, selection, ratio=False, printing=False,
output_name='', extension='', x_variable='',
scale='lin', xlim=[]):
"""
Load the data to numpy arrays, write a Python script and plot them.
Inspired heavily by the matlab version by Thomas Tram
Parameters
----------
files : list
List of files to plot
selection : list, or string
List of items to plot, which should match the way they appear in the
file, for instance: ['TT', 'BB]
Keyword Arguments
-----------------
ratio : bool
If set to yes, plots the ratio of the files, taking as a reference the
first one
output_name : str
Specify a different name for the produced figure (by default, it takes
the name of the first file, and replace the .dat by .pdf)
extension : str
"""
# Load all the graphs
data = []
for data_file in files:
data.append(np.loadtxt(data_file))
# Create the python script, and initialise it
python_script_path = files[0]+'.py'
pdf_path = files[0]+'.pdf'
text = '''
import matplotlib.pyplot as plt
import numpy as np\n'''
# Create the full_path_files list, that contains the absolute path, so that
# the future python script can import them directly.
full_path_files = [os.path.abspath(elem) for elem in files]
# Recover the base name of the files, everything before the .
roots = [elem.split(os.path.sep)[-1].split('.')[0] for elem in files]
text += '''files = %s\n''' % full_path_files
text += '''
data = []
for data_file in files:
data.append(np.loadtxt(data_file))\n'''
# Recover the number of columns in the first file, as well as their title.
num_columns, names, tex_names = extract_headers(files[0])
# Check if everything is in order
if num_columns == 2:
selection = [names[1]]
elif num_columns > 2:
# in case selection was only a string, cast it to a list
if isinstance(selection, str):
selection = [selection]
for elem in selection:
if elem not in names:
raise InputError(
"The entry 'selection' must contain names of the fields "
"in the specified files. You asked for %s " % elem +
"where I only found %s." % names)
# Store the selected text and tex_names to the script
text += 'selection = %s\n' % selection
text += 'tex_names = %s\n' % [elem for (elem, name) in
zip(tex_names, names) if name in selection]
# Create the figure and ax objects
fig, ax = plt.subplots()
text += '\nfig, ax = plt.subplots()\n'
# if ratio is not set, then simply plot them all
if not ratio:
loc = ''
text += 'for curve in data:\n'
for idx, curve in enumerate(data):
_, curve_names, _ = extract_headers(files[idx])
for selec in selection:
index = curve_names.index(selec)
text += ' ax.'
if scale == 'lin':
text += 'plot(curve[:, 0], curve[:, %i])\n' % index
ax.plot(curve[:, 0], curve[:, index])
elif scale == 'loglog':
text += 'loglog(curve[:, 0], curve[:, %i])\n' % index
ax.loglog(curve[:, 0], curve[:, index])
elif scale == 'loglin':
text += 'semilogx(curve[:, 0], curve[:, %i])\n' % index
ax.semilogx(curve[:, 0], curve[:, index])
elif scale == 'george':
text += 'plot(curve[:, 0], curve[:, %i])\n' % index
ax.plot(curve[:, 0], curve[:, index])
ax.set_xscale('planck')
loc = 'upper right'
if not loc:
loc = 'lower right'
ax.legend([root+': '+elem for (root, elem) in
itertools.product(roots, selection)], loc=loc)
else:
ref = data[0]
_, ref_curve_names, _ = extract_headers(files[0])
for idx in range(1, len(data)):
current = data[idx]
_, curve_names, _ = extract_headers(files[idx])
for selec in selection:
# Do the interpolation
axis = ref[:, 0]
reference = ref[:, ref_curve_names.index(selec)]
interpolated = splrep(current[:, 0],
current[:, curve_names.index(selec)])
if scale == 'lin':
ax.plot(axis, splev(ref[:, 0], interpolated)/reference-1)
elif scale == 'loglin':
ax.semilogx(axis, splev(ref[:, 0],
interpolated)/reference-1)
elif scale == 'loglog':
raise InputError(
"loglog plot is not available for ratios")
#if np.allclose(current[0], ref[0]):
#ax.plot(current[0], current[colnum]/ref[colnum])
if 'TT' in curve_names:
ax.set_xlabel('$\ell$', fontsize=20)
elif 'P' in curve_names:
ax.set_xlabel('$k$ [$h$/Mpc]', fontsize=20)
if xlim:
if len(xlim) > 1:
ax.set_xlim(xlim)
else:
ax.set_xlim(xlim[0])
ax.set_ylim()
text += 'plt.show()\n'
plt.show()
# Write to the python file all the issued commands. You can then reproduce
# the plot by running "python output/something_cl.dat.py"
with open(python_script_path, 'w') as python_script:
python_script.write(text)
# If the use wants to print the figure to a file
if printing:
fig.savefig(pdf_path)
class FormatError(Exception):
"""Format not recognised"""
pass
class TypeError(Exception):
"""Spectrum type not recognised"""
pass
class NumberOfFilesError(Exception):
"""Invalid number of files"""
pass
class InputError(Exception):
"""Incompatible input requirements"""
pass
def replace_scale(string):
"""
This assumes that the string starts with "(.)", which will be replaced by
(8piG/3)
>>> print replace_scale('(.)toto')
>>> '(8\\pi G/3)toto'
"""
string_list = list(string)
string_list.pop(1)
string_list[1:1] = list('8\\pi G/3')
return ''.join(string_list)
def process_long_names(long_names):
"""
Given the names extracted from the header, return two arrays, one with the
short version, and one tex version
>>> names, tex_names = process_long_names(['(.)toto', 'proper time [Gyr]'])
>>> print names
>>> ['toto', 'proper time']
>>> print tex_names
>>> ['(8\\pi G/3)toto, 'proper time [Gyr]']
"""
names = []
tex_names = []
# First pass, to remove the leading scales
for name in long_names:
# This can happen in the background file
if name.startswith('(.)', 0):
temp_name = name[3:]
names.append(temp_name)
tex_names.append(replace_scale(name))
# Otherwise, we simply
else:
names.append(name)
tex_names.append(name)
# Second pass, to remove from the short names the indication of scale,
# which should look like something between parenthesis, or square brackets,
# and located at the end of the string
for index, name in enumerate(names):
if name.find('(') != -1:
names[index] = name[:name.index('(')]
elif name.find('[') != -1:
names[index] = name[:name.index('[')]
# Finally, remove any extra spacing
names = [''.join(elem.split()) for elem in names]
return names, tex_names
def extract_headers(header_path):
with open(header_path, 'r') as header_file:
header = [line for line in header_file if line[0] == '#']
header = header[-1]
# Count the number of columns in the file, and recover their name. Thanks
# Thomas Tram for the trick
indices = [i+1 for i in range(len(header)) if
header.startswith(':', i)]
num_columns = len(indices)
long_names = [header[indices[i]:indices[(i+1)]-3].strip() if i < num_columns-1
else header[indices[i]:].strip()
for i in range(num_columns)]
# Process long_names further to handle special cases, and extract names,
# which will correspond to the tags specified in "selection".
names, tex_names = process_long_names(long_names)
return num_columns, names, tex_names
def main():
print '~~~ Running CPU, a CLASS Plotting Utility ~~~'
parser = create_parser()
# Parse the command line arguments
args = parser.parse_args()
# if there are no argument in the input, print usage
if len(args.files) == 0:
parser.print_usage()
return
# if the first file name contains cl or pk, infer the type of desired
# spectrum
if not args.selection:
if args.files[0].rfind('cl') != -1:
selection = ['TT']
scale = 'loglog'
elif args.files[0].rfind('pk') != -1:
selection = ['P']
scale = 'loglog'
else:
raise TypeError(
"Please specify a field to plot")
args.selection = selection
else:
scale = ''
if not args.scale:
if scale:
args.scale = scale
else:
args.scale = 'lin'
# Remove extra spacing in the selection list
args.selection = [''.join(elem.split()) for elem in args.selection]
# If ratio is asked, but only one file was passed in argument, politely
# complain
if args.ratio:
if len(args.files) < 2:
raise NumberOfFilesError(
"If you want me to compute a ratio between two files, "
"I strongly encourage you to give me at least two of them.")
# actual plotting. By default, a simple superposition of the graph is
# performed. If asked to be divided, the ratio is shown - whether a need
# for interpolation arises or not.
plot_CLASS_output(args.files, args.selection, ratio=args.ratio,
printing=args.printfile, scale=args.scale,
xlim=args.xlim)
if __name__ == '__main__':
sys.exit(main())