lyapower.power_spectra¶
Power-spectrum objects, gimlet/genpk/ascii I/O, rebinning, splicing.
lyapower.power_spectra ¶
Created on Tue Dec 3 16:29:56 2019
@author: cravoux
Power-spectrum data objects and I/O for the lyapower package.
This module defines the container classes used to represent 1D and 3D
(or k, mu binned) power spectra measured from Nyx hydrodynamical
simulations post-processed by gimlet: :class:PowerSpectrum (base
class), :class:MatterPowerSpectrum and :class:FluxPowerSpectrum.
It provides readers for gimlet / genpk / ascii power-spectrum files,
rebinning utilities (1D and 2D in k or k, mu), plotting helpers, and
the "splicing" routines (:func:splice_1D, :func:splice_3D) that
combine multiple-resolution simulation boxes into a single spliced
power spectrum, following Arinyo-i-Prats et al. 2015.
Conventions: wavenumber k is expressed in h/Mpc or 1/Mpc (see
:meth:PowerSpectrum.change_k_normalization), mu is
k_parallel / k, and power spectra are stored as P(k) (1D) or
P(k, mu) (3D, with k_array stacked as [k, mu]).
PowerSpectrum ¶
Bases: object
Base container for a measured power spectrum.
Holds the wavenumber array (1D k or stacked [k, mu] for
2D spectra), the power values, an optional error array, and
bookkeeping metadata (source file, simulation box size, and
whether k is h-normalized). Provides file readers
(:meth:init_from_genpk_file, :meth:init_from_ascii_file),
rebinning, plotting and unit-conversion utilities shared by
:class:MatterPowerSpectrum and :class:FluxPowerSpectrum.
Source code in lyapower/power_spectra.py
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 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 | |
init_from_genpk_file
classmethod
¶
init_from_genpk_file(name_file, size_box)
Load a GenPk format power spectum, plotting the DM and the neutrinos (if present) Does not plot baryons.
Source code in lyapower/power_spectra.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
init_from_ascii_file
classmethod
¶
init_from_ascii_file(name_file)
Load a power spectrum from a plain two-column ascii file.
The first row of the file is skipped (treated as a header/edge
row); column 0 is used as k and column 1 as the power.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name_file
|
Path to the ascii file with columns [k, power]. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
PowerSpectrum |
New instance with |
|
|
|
Source code in lyapower/power_spectra.py
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 | |
center_wavenumbers_1d ¶
center_wavenumbers_1d()
Replace 1D bin-edge wavenumbers with bin-center wavenumbers.
Recomputes self.k_array in place as the midpoints between
consecutive stored values, extrapolating the last bin center
from the final spacing. Sets self.edge_stored = False.
Source code in lyapower/power_spectra.py
126 127 128 129 130 131 132 133 134 135 136 | |
center_wavenumbers_2d ¶
center_wavenumbers_2d()
Replace 2D bin-edge k values with bin-center k values, per mu bin.
For each unique mu value in self.k_array[1], converts the
corresponding k edges (self.k_array[0]) to bin centers
in place, extrapolating the last center from the final spacing.
Sets self.edge_stored = False.
Source code in lyapower/power_spectra.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
compute_dmu
staticmethod
¶
compute_dmu(mu, mu_max=1.0)
Compute the bin width in mu for each entry of a sorted mu array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
1D array of mu bin-edge values (assumed sorted per group). |
required | |
mu_max
|
Upper bound of the mu range, used to close the last bin and to fix up any negative widths caused by mu wrap-around (e.g. between successive k groups). Defaults to 1.0. |
1.0
|
Returns:
| Type | Description |
|---|---|
|
numpy.ndarray: Array of mu bin widths, same length as |
Source code in lyapower/power_spectra.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
center_mu_2d ¶
center_mu_2d(mu_max=1.0)
Shift stored mu edges to mu bin centers, in place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu_max
|
Upper bound of the mu range, forwarded to
:meth: |
1.0
|
Source code in lyapower/power_spectra.py
174 175 176 177 178 179 180 181 182 183 | |
rebin_arrays ¶
rebin_arrays(nb_bin, operation='mean')
Rebin a 1D power spectrum onto a new log-spaced k grid, in place.
Builds nb_bin log-spaced k values spanning the current
k_array range and, for each resulting interval, aggregates
the power values that fall in it (falling back to the nearest
original point when an interval is empty). Updates
self.k_array and self.power_array in place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nb_bin
|
Number of new k bins (i.e. length of the new k grid). |
required | |
operation
|
Aggregation to apply within each bin: |
'mean'
|
Source code in lyapower/power_spectra.py
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 | |
rebin_2d_arrays ¶
rebin_2d_arrays(nb_bin, operation='mean', loglin=False, k_loglin=None)
Rebin a 2D (k, mu) power spectrum onto a new log-spaced k grid.
For each mu value, rebins the k axis onto nb_bin log-spaced
bins spanning the (optionally restricted) k range, aggregating
power (and error, if present) within each bin. Updates
self.k_array, self.power_array and self.error_array
in place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nb_bin
|
Number of new k bins along each mu slice. |
required | |
operation
|
Aggregation to apply within each bin: |
'mean'
|
|
loglin
|
If True, only rebin the k range above |
False
|
|
k_loglin
|
k threshold above which rebinning is applied when
|
None
|
Source code in lyapower/power_spectra.py
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 | |
cut_extremum ¶
cut_extremum(kmin, kmax)
Restrict a 2D power spectrum to a k range, in place.
Filters self.k_array, self.power_array and (if present)
self.error_array to keep only entries whose k_array[0]
lies within [kmin, kmax].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kmin
|
Lower k bound (inclusive), or None to skip the lower cut. |
required | |
kmax
|
Upper k bound (inclusive), or None to skip the upper cut. |
required |
Source code in lyapower/power_spectra.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | |
put_label ¶
put_label(ax, xunit=True, yunit=True, y_label='$P$', x_label='$k$', labelsize_x=12, labelsize_y=12, fontsize=12)
Set axis labels (with units) on a matplotlib Axes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax
|
Matplotlib Axes to label. |
required | |
xunit
|
If True, append the k unit to the x label. Defaults to True. |
True
|
|
yunit
|
If True, append the power unit to the y label. Defaults to True. |
True
|
|
y_label
|
Base y-axis label (unit suffix appended if |
'$P$'
|
|
x_label
|
Base x-axis label (unit suffix appended if |
'$k$'
|
|
labelsize_x
|
Tick label font size for the x axis. Defaults to 12. |
12
|
|
labelsize_y
|
Tick label font size for the y axis. Defaults to 12. |
12
|
|
fontsize
|
Font size for the axis labels. Defaults to 12. |
12
|
Source code in lyapower/power_spectra.py
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 | |
prepare_axes ¶
prepare_axes(kwargs)
Resolve the main and comparison Axes to plot on from kwargs.
Reads the "ax" key (list of Axes) from kwargs, defaulting
to the current figure's axes. If none are found, uses
plt.gca() for both; if one is found, it is used for both the
main plot and the comparison (ratio) panel; if two or more are
found, the first is the main plot and the second the comparison
panel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kwargs
|
Keyword-argument dict, inspected via
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
|
Source code in lyapower/power_spectra.py
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 | |
plot_1d_pk ¶
plot_1d_pk(**kwargs)
Plot this 1D power spectrum, with an optional comparison ratio panel.
Plots power_array vs k_array on the main axes and,
if a "comparison" PowerSpectrum is given, plots the relative
difference (comparison - self) / comparison (interpolated onto
the comparison's k grid) on the comparison axes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Options read via |
{}
|
Source code in lyapower/power_spectra.py
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 505 506 507 | |
plot_2d_pk ¶
plot_2d_pk(bin_edges, **kwargs)
Plot this 2D (k, mu) power spectrum, one line per mu bin.
For each value in bin_edges (matched against
self.k_array[1]), plots power (with error bars, if
error_array is set) vs k, optionally multiplied by
k**3 / (2*pi**2). If a "comparison" PowerSpectrum is
given, also plots the relative difference on a second (ratio)
axes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bin_edges
|
Sequence of mu values identifying which mu slices
of |
required | |
**kwargs
|
Options read via |
{}
|
Source code in lyapower/power_spectra.py
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 | |
plot_several_power_spectrum ¶
plot_several_power_spectrum(Pks, k_space, name, legend)
Plot several power spectra sharing the same k grid to a new figure.
Creates a new figure, log-log plots each spectrum in Pks
against k_space with a rainbow color cycle, and saves the
result as "<name>matter_power_spectrum.pdf".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Pks
|
Sequence of power arrays, one per spectrum to plot. |
required | |
k_space
|
Shared wavenumber array (x axis) for all spectra. |
required | |
name
|
Filename prefix for the saved PDF. |
required | |
legend
|
Sequence of legend labels, one per spectrum. |
required |
Source code in lyapower/power_spectra.py
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 | |
plot_comparison_spectra ¶
plot_comparison_spectra(list_spectra, label_list, diff_extremums=0.1, normalize=True)
Create a two-panel figure comparing this spectrum to others.
Builds a figure with a main panel (top, 3/4 height) showing all
spectra and a ratio panel (bottom, 1/4 height) showing each
spectrum's fractional difference to self (delegated to
:meth:add_comparison_spectra), then applies axis labels,
scales, legend and ratio-panel y-limits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list_spectra
|
Sequence of PowerSpectrum instances to compare
against |
required | |
label_list
|
Legend labels, one per plotted spectrum (including the reference, first). |
required | |
diff_extremums
|
Symmetric y-limit for the ratio panel. Defaults to 0.1. |
0.1
|
|
normalize
|
If True, plot |
True
|
Returns:
| Type | Description |
|---|---|
|
numpy.ndarray: The two-element array of matplotlib Axes |
|
|
|
Source code in lyapower/power_spectra.py
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 | |
add_comparison_spectra ¶
add_comparison_spectra(list_spectra, ax, normalize=True)
Overlay this spectrum and others on existing axes, with a ratio panel.
Plots self and each spectrum in list_spectra on
ax[0], and each spectrum's ratio to self (interpolated
onto self's k grid, minus 1) on ax[1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list_spectra
|
Sequence of PowerSpectrum instances to overlay
and compare against |
required | |
ax
|
Two-element sequence of matplotlib Axes,
|
required | |
normalize
|
If True, plot |
True
|
Source code in lyapower/power_spectra.py
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 | |
save_plot ¶
save_plot(nameout, format_out='pdf', fig=None)
Save a matplotlib figure to disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nameout
|
Output file path. |
required | |
format_out
|
File format passed to |
'pdf'
|
|
fig
|
Figure to save. Defaults to the current figure
( |
None
|
Source code in lyapower/power_spectra.py
826 827 828 829 830 831 832 833 834 835 836 837 838 | |
close_plot ¶
close_plot(fig=None)
Close the current matplotlib figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
Unused (accepted for API symmetry with :meth: |
None
|
Source code in lyapower/power_spectra.py
840 841 842 843 844 845 846 847 848 849 850 | |
open_plot ¶
open_plot(**kwargs)
Create a new matplotlib figure, optionally applying a style.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Options read via |
{}
|
Returns:
| Type | Description |
|---|---|
|
matplotlib.figure.Figure: The newly created figure. |
Source code in lyapower/power_spectra.py
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 | |
open_subplot ¶
open_subplot(x=2, y=1, figsize=(8, 6))
Create a new figure with a grid of x-by-y subplots sharing the x axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Number of subplot rows. Defaults to 2. |
2
|
|
y
|
Number of subplot columns. Defaults to 1. |
1
|
|
figsize
|
Figure size in inches. Defaults to |
(8, 6)
|
Returns:
| Type | Description |
|---|---|
|
matplotlib.figure.Figure: The newly created figure. |
Source code in lyapower/power_spectra.py
871 872 873 874 875 876 877 878 879 880 881 882 883 | |
show_plot ¶
show_plot()
Display the current matplotlib figure (plt.show()).
Source code in lyapower/power_spectra.py
885 886 887 | |
get_k_value ¶
get_k_value(k)
Interpolate and print the power at a given k value (or values).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
Wavenumber value(s) at which to evaluate the power
spectrum. Must lie within the range of |
required |
Returns:
| Type | Description |
|---|---|
|
float or numpy.ndarray: Interpolated power value(s). |
Source code in lyapower/power_spectra.py
889 890 891 892 893 894 895 896 897 898 899 900 901 902 | |
change_k_normalization ¶
change_k_normalization(wanted_h_normalized, h)
Convert k_array between h/Mpc and 1/Mpc units, in place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wanted_h_normalized
|
Target normalization: True for h/Mpc, False for 1/Mpc. |
required | |
h
|
Dimensionless Hubble parameter used for the conversion
( |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Empty tuple |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Source code in lyapower/power_spectra.py
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 | |
MatterPowerSpectrum ¶
Bases: PowerSpectrum
Matter power spectrum for a given species, 1D or 3D.
Extends :class:PowerSpectrum with a dimension ("1D" or
"3D") and a specie label (e.g. dark matter, baryons,
neutrinos), and adds gimlet-format I/O.
Source code in lyapower/power_spectra.py
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 | |
init_from_gimlet
classmethod
¶
init_from_gimlet(namefile, specie='unknown', power_weighted=False, error_estimator=None, **kwargs)
Pm(k) gimlet file contains - k: edge (higher) of the k bin considered - bincount: number of mode (pairs) computed in the bin - pwk: power weighted k - power: power of the bin
Source code in lyapower/power_spectra.py
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 | |
write_to_gimlet ¶
write_to_gimlet(name_out, power_weighted=False)
Write this spectrum to a gimlet-format Pm(k) ascii file.
Writes columns [k, bincount, pwk, power] where either k
or pwk (power-weighted k) is populated from self.k_array
depending on power_weighted, and bincount is taken from
self.error_array if present, else zeros.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name_out
|
Output file path. |
required | |
power_weighted
|
If True, store |
False
|
Source code in lyapower/power_spectra.py
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 | |
FluxPowerSpectrum ¶
Bases: PowerSpectrum
Lyman-alpha flux power spectrum, 1D or 3D (k or k, mu binned).
Extends :class:PowerSpectrum with a dimension ("1D" or
"3D") and adds gimlet-format readers/writers for both P(k) and
P(k, mu) or P(k_perp, k_par) binnings, plus multi-file averaging.
Source code in lyapower/power_spectra.py
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 | |
init_1D_from_gimlet
classmethod
¶
init_1D_from_gimlet(namefile, power_weighted=False, error_estimator=None, error_stored=False, **kwargs)
Load a 1D flux power spectrum P(k) from a gimlet ascii file.
The file is expected to have columns
[k_edge, bincount, pwk_edge, power].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namefile
|
Path to the gimlet Pf(k) ascii file. |
required | |
power_weighted
|
If True, use the power-weighted k column
( |
False
|
|
error_estimator
|
Name of the error model to pass to
|
None
|
|
error_stored
|
If True, use the |
False
|
|
**kwargs
|
Forwarded to |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
FluxPowerSpectrum |
New 1D instance with |
Source code in lyapower/power_spectra.py
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 | |
init_3D_from_gimlet
classmethod
¶
init_3D_from_gimlet(namefile, type_file, kmu=True, power_weighted=False, error_estimator=None, field_name=None, error_stored=False, **kwargs)
Load a 3D flux power spectrum P(k, mu) or P(k_perp, k_par) from gimlet.
Reads a gimlet output (plain text or HDF5 dataset) with 6
columns, parsed either as (k, mu, ...) via
:meth:init_kmu or (k_perp, k_par, ...) via
:meth:init_kperpar, and builds a 2-row k_array of
[k1, k2].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namefile
|
Path to the gimlet Pf(k, mu) file. |
required | |
type_file
|
File format, either |
required | |
kmu
|
If True, parse columns as (k, mu, bincount, pwk, pwmu,
power) via :meth: |
True
|
|
power_weighted
|
If True, use the power-weighted k1/k2 columns
as |
False
|
|
error_estimator
|
Name of the error model to pass to
|
None
|
|
field_name
|
Dataset name to read within the HDF5 file when
|
None
|
|
error_stored
|
If True, use the |
False
|
|
**kwargs
|
Forwarded to |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
FluxPowerSpectrum |
New 3D instance with |
Source code in lyapower/power_spectra.py
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 | |
init_kmu
staticmethod
¶
init_kmu(pk_array)
Pf(k,mu) gimlet file contains - k_edge: edge (lower) of the k bin considered - mu_edge: edge (lower) of the mu bin considered (mu positive) - bincount: number of mode (pairs) computed in the bin - pwk: power weighted k - pwmu: power weighted mu - power: power of the bin
Source code in lyapower/power_spectra.py
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 | |
init_kperpar
staticmethod
¶
init_kperpar(pk_array)
Pf(kperp,kpar) gimlet file contains - k_perp: edge (lower) of the k perp bin considered - k_par: edge (lower) of the k par bin considered - bincount: number of mode (pairs) computed in the bin - pwkperp: power weighted k perp - pwkpar: power weighted k par - power: power of the bin
Source code in lyapower/power_spectra.py
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 | |
compute_mean_gimlet
staticmethod
¶
compute_mean_gimlet(namefile, namemean, type_file, kmu=True, field_name=None)
Average several gimlet 3D power-spectrum files and write the result.
Reads each file in namefile (plain text or HDF5), parses it
with :meth:init_kmu or :meth:init_kperpar, sums bincount,
the two power-weighted-k columns, and power across files, then
divides the weighted-k and power sums by the number of files
(bincount is left as a raw sum) and writes the result to
namemean in gimlet ascii format
[k1_edge, k2_edge, bincount, pwk1, pwk2, power] (k edges
taken from the first file only).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
namefile
|
Sequence of input gimlet file paths to average. |
required | |
namemean
|
Output ascii file path for the averaged spectrum. |
required | |
type_file
|
File format of the inputs, |
required | |
kmu
|
If True, parse as (k, mu, ...) via :meth: |
True
|
|
field_name
|
Dataset name to read within each HDF5 file when
|
None
|
Source code in lyapower/power_spectra.py
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 | |
write_to_gimlet ¶
write_to_gimlet(name_out, power_weighted=False)
Write this 3D spectrum to a gimlet-format Pf(k, mu) ascii file.
Writes columns
[k1_edge, k2_edge, bincount, pwk1, pwk2, power] where either
the plain edges or the power-weighted columns are populated
from self.k_array depending on power_weighted, and
bincount is taken from self.error_array if present,
else zeros.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name_out
|
Output file path. |
required | |
power_weighted
|
If True, store |
False
|
Source code in lyapower/power_spectra.py
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 | |
init_spectrum ¶
init_spectrum(type_init, filename, boxsize=None)
The init_spectrum function takes in a type_init and filename, and returns a PowerSpectrum object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_init
|
Determine what type of file is being read in |
required | |
filename
|
Specify the file to read from |
required | |
boxsize
|
Specify the boxsize of the simulation |
None
|
Returns:
| Type | Description |
|---|---|
|
A powerspectrum object |
Source code in lyapower/power_spectra.py
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 | |
launch_comparison_power_spectra ¶
launch_comparison_power_spectra(list_file, type_file, label_list, name_out, diff_extremums=0.1, rebin=None, rebin_method=None, flux_factor=None, normalize=True, size_box=None, wanted_normalization=None, h_normalization=None)
Load, optionally rebin/rescale, and plot several spectra against a reference.
Loads list_file[0] as the reference spectrum and each remaining
entry of list_file as a comparison spectrum (all via
:func:init_spectrum), optionally rebins them (:meth:PowerSpectrum.rebin_arrays),
rescales power by a per-file flux_factor, and converts k
normalization, then plots them with
:meth:PowerSpectrum.plot_comparison_spectra and saves the figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list_file
|
Sequence of file paths; |
required | |
type_file
|
File type passed to :func: |
required | |
label_list
|
Legend labels, one per spectrum (including the reference). |
required | |
name_out
|
Output path for the saved comparison figure. |
required | |
diff_extremums
|
Symmetric y-limit for the ratio panel. Defaults to 0.1. |
0.1
|
|
rebin
|
Number of bins to rebin each spectrum to, or None to skip rebinning. Defaults to None. |
None
|
|
rebin_method
|
Aggregation method forwarded to
:meth: |
None
|
|
flux_factor
|
Sequence of per-file multiplicative factors applied to each spectrum's power array, or None to skip. Defaults to None. |
None
|
|
normalize
|
Forwarded to
:meth: |
True
|
|
size_box
|
Box size (Mpc/h) forwarded to :func: |
None
|
|
wanted_normalization
|
Target h-normalization forwarded to
:meth: |
None
|
|
h_normalization
|
Hubble parameter |
None
|
Source code in lyapower/power_spectra.py
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 | |
launch_comparison_power_spectra_different_ref ¶
launch_comparison_power_spectra_different_ref(list_file, type_file, label_list, name_out, diff_extremums=0.1, rebin=None, rebin_method=None, flux_factor=None, normalize=True, size_box=None, wanted_normalization=None, h_normalization=None)
Compare several groups of spectra, each against its own reference.
Like :func:launch_comparison_power_spectra, but operates on a
list of groups (list_file[j]), each with its own reference
(list_file[j][0]), rebin settings, flux factors and target
normalization (all indexed by j). All groups are overlaid on
the same figure: the first group creates the comparison figure via
:meth:PowerSpectrum.plot_comparison_spectra, subsequent groups are
added via :meth:PowerSpectrum.add_comparison_spectra. The figure
is saved using the last group's reference spectrum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list_file
|
Sequence of groups; each group is a sequence of file paths whose first entry is that group's reference spectrum. |
required | |
type_file
|
Sequence of file types (one per group), passed to
:func: |
required | |
label_list
|
Legend labels forwarded to
:meth: |
required | |
name_out
|
Output path for the saved comparison figure. |
required | |
diff_extremums
|
Symmetric y-limit for the ratio panel. Defaults to 0.1. |
0.1
|
|
rebin
|
Sequence of rebin bin counts (one per group, or None entries to skip rebinning that group). |
None
|
|
rebin_method
|
Aggregation method forwarded to
:meth: |
None
|
|
flux_factor
|
Sequence of per-group sequences of per-file power multipliers, or None entries to skip. |
None
|
|
normalize
|
Forwarded to the comparison plotting calls. Defaults to True. |
True
|
|
size_box
|
Box size (Mpc/h) forwarded to :func: |
None
|
|
wanted_normalization
|
Sequence of target h-normalizations (one per group), or None to skip conversion. |
None
|
|
h_normalization
|
Hubble parameter |
None
|
Source code in lyapower/power_spectra.py
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 | |
compute_k_extremums ¶
compute_k_extremums(power_Ll, power_Sl, power_Ss, tol=0.01)
The compute_k_extremums function takes in the power spectra of the long-long, short-long and short-short modes. It then computes a list of k_max values for each mu bin. The k_max value is defined as the maximum value of k where P(k) = P(Ss)(k). This function is used to compute an upper limit on our integration range when computing the covariance matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
power_Ll
|
Compute the k_max for each mu bin |
required | |
power_Sl
|
Compute the interpolation of power_sl |
required | |
power_Ss
|
Find the minimum and maximum k values for each mu bin |
required | |
tol
|
Compute the k_max value |
0.01
|
|
|
Compute the upper limit on our integration range when computing |
required |
Returns:
| Type | Description |
|---|---|
|
A list of k_max values for each mu bin |
Source code in lyapower/power_spectra.py
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 | |
compute_k_extremums_1D ¶
compute_k_extremums_1D(power_Ll, power_Sl, power_Ss, tol=0.01)
The compute_k_extremums_1D function computes the maximum k value for a given power spectrum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
power_Ll
|
Compute the k_max value |
required | |
power_Sl
|
Compute the maximum k value |
required | |
power_Ss
|
Compute the maximum k value |
required | |
tol
|
Determine the maximum k value |
0.01
|
|
|
Compute the maximum k value for a given power spectrum |
required |
Returns:
| Type | Description |
|---|---|
|
The maximum k value |
Source code in lyapower/power_spectra.py
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 | |
splice_1D ¶
splice_1D(power_Ll, power_Sl, power_Ss, size_small, size_large, N_large, use_nyquist=False, tol=0.01)
L,S = Large or Small size l,s = large or small number of particles/resolution elements splice the Ll box, using resolved Sl box and splicing Ss box
Source code in lyapower/power_spectra.py
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 | |
splice_3D ¶
splice_3D(power_Ll, power_Sl, power_Ss, size_small, size_large, N_large, use_nyquist=False, impose_kmin_coeff=None, impose_kmin=None, impose_kmax=None, tol=0.01, power_pwk_Ll=None, power_pwk_Sl=None)
L,S = Large or Small size l,s = large or small number of particles/resolution elements splice the Ll box, using resolved Sl box and splicing Ss box
Source code in lyapower/power_spectra.py
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 | |
verif_slicing ¶
verif_slicing(power_verif, power_spliced, mu_bins, name_out, style=None)
Plot the fractional residual between a spliced spectrum and a reference.
For each mu bin in mu_bins, interpolates power_spliced onto
power_verif's k grid and plots the fractional difference
(power_verif - power_spliced) / power_verif vs k (semilog-x),
shading a +/-5% band, marking k=8, and saving the figure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
power_verif
|
Reference (e.g. fully-resolved) FluxPowerSpectrum to validate the splicing against. |
required | |
power_spliced
|
Spliced FluxPowerSpectrum (e.g. output of
:func: |
required | |
mu_bins
|
Sequence of (up to 4) mu bin values to plot, matched
against |
required | |
name_out
|
Output file path for the saved figure. |
required | |
style
|
Optional matplotlib style name passed to
|
None
|
Source code in lyapower/power_spectra.py
1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 | |