PCA Plotting module
pca
PCA and dimensionality reduction visualization functions.
This module provides utilities for visualizing principal component analysis results, including component distributions, variance explanations, and dataset separation.
correlation_matrix(corr_matrix, k_start=0.001, k_end=0.23, nbins=20, figsize=(10, 8))
Plot correlation matrix between bins.
| PARAMETER | DESCRIPTION |
|---|---|
corr_matrix
|
Correlation matrix from analyze_bin_correlations()
TYPE:
|
k_start
|
Minimum \(k\) for binning
TYPE:
|
k_end
|
Maximum \(k\) for binning
TYPE:
|
nbins
|
Number of bins
TYPE:
|
figsize
|
Figure size
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Matplotlib Figure object. |
Source code in src/primefeat/plots/pca.py
dataset_separation_PC(results, chains_dict=None, pc_x=1, pc_y=2, figsize=(10, 8), colors=None, chain_entries=None, plot_type='scatter', filled=False, contour_levels=None, legend_loc=(0.5, 1.15))
Plot samples from different datasets in principal component space.
This shows whether different datasets have distinct feature preferences (separation in PC space) or if they agree (overlap in PC space).
| PARAMETER | DESCRIPTION |
|---|---|
results
|
PCAResults from perform_pca()
TYPE:
|
chains_dict
|
Dictionary of chains (for labels); auto-extracted from chain_entries if provided
TYPE:
|
pc_x
|
Which PC to plot on x-axis (default: 1)
TYPE:
|
pc_y
|
Which PC to plot on y-axis (default: 2)
TYPE:
|
figsize
|
Figure size (default: (10, 8))
TYPE:
|
colors
|
Optional list of colors for each dataset; auto-extracted from chain_entries if provided
TYPE:
|
chain_entries
|
ChainsCollection or list of ChainEntry objects; colors and chains auto-extracted
TYPE:
|
plot_type
|
"scatter" for scatter plot or "contour" for getdist contours (default: "scatter")
TYPE:
|
filled
|
Whether to use filled contours (only for plot_type="contour", default: False)
TYPE:
|
contour_levels
|
Confidence levels for contours (default: [0.68, 0.95] for \(1\sigma\) and \(2\sigma\))
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Matplotlib Figure object. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If chains_dict is not provided and chain_entries not usable. |
Source code in src/primefeat/plots/pca.py
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 | |
rectangle_PC_scores(results, chains_dict, pcs_x=[1, 3], pcs_y=[2, 4], colors=None, filled=False, **kwargs)
Plot PC scores in a rectangle grid using getdist contours.
Creates MCSamples from PC scores and uses the rectangle() function to display a grid of 2D marginalized contours.
| PARAMETER | DESCRIPTION |
|---|---|
results
|
PCAResults from perform_pca()
TYPE:
|
chains_dict
|
Dictionary of chains (for labels)
TYPE:
|
pcs_x
|
Which PCs for x-axis (default: [1, 3])
TYPE:
|
pcs_y
|
Which PCs for y-axis (default: [2, 4])
TYPE:
|
colors
|
Optional list of colors for each dataset
TYPE:
|
filled
|
Whether to use filled contours (default: False)
TYPE:
|
**kwargs
|
Additional arguments passed to rectangle()
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
|
GetDistPlotter object. |
Source code in src/primefeat/plots/pca.py
plot_principal_components(results, k_start=0.001, k_end=0.23, nbins=20, n_components=5, figsize=(10, 10), binning=None)
Visualize principal components as functions in \(k\)-space.
This shows what each PC "looks like" as a pattern in the primordial power spectrum deviations.
| PARAMETER | DESCRIPTION |
|---|---|
results
|
PCAResults from perform_pca()
TYPE:
|
k_start
|
Minimum \(k\) for binning (default: 0.001 Mpc\(^{-1}\))
TYPE:
|
k_end
|
Maximum \(k\) for binning (default: 0.23 Mpc\(^{-1}\))
TYPE:
|
nbins
|
Number of bins (default: 20)
TYPE:
|
n_components
|
Number of PCs to plot (default: 5)
TYPE:
|
figsize
|
Figure size (default: (10, 10))
TYPE:
|
binning
|
optional BinningScheme object (supersedes k_start, k_end, nbins if provided)
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Matplotlib Figure object. |
Source code in src/primefeat/plots/pca.py
plot_variance_explained(results, figsize=(12, 5))
Plot variance explained by principal components.
| PARAMETER | DESCRIPTION |
|---|---|
results
|
PCAResults from perform_pca()
TYPE:
|
figsize
|
Figure size (default: (12, 5))
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
fig
|
matplotlib Figure object |
Source code in src/primefeat/plots/pca.py
ridgeline_delta(chains, nbins=20, k_start=0.001, k_end=0.23, overlap=0.7, cmap='coolwarm', alpha=0.8, linewidth=1.0, xlim=(-0.6, 0.6), bw_method=None, fig_kw=None, binning=None)
Ridgeline plot of the marginal posterior distributions of the delta_i bin amplitudes.
Each ridge is the KDE of the posterior samples for one k-bin, normalized independently to its own peak so that the shape of every distribution is visible regardless of how much the posterior widths vary across bins. Bins are stacked bottom (k_start) to top (k_end).
| PARAMETER | DESCRIPTION |
|---|---|
chains
|
Dict mapping dataset label to MCSamples, or a single MCSamples object. When a dict is given, each dataset gets its own panel (column).
|
nbins
|
Number of delta bins (default: 20).
DEFAULT:
|
k_start
|
Lower edge of the binning range in Mpc^-1 (default: 0.001).
DEFAULT:
|
k_end
|
Upper edge of the binning range in Mpc^-1 (default: 0.23).
DEFAULT:
|
overlap
|
Ridge height as a fraction of the inter-ridge spacing (default: 0.7). Values > 1 produce overlapping ridges.
DEFAULT:
|
cmap
|
Colormap applied across ridges to encode k value (default: "coolwarm").
DEFAULT:
|
alpha
|
Fill opacity (default: 0.8).
DEFAULT:
|
linewidth
|
Ridge outline width (default: 1.0).
DEFAULT:
|
xlim
|
x-axis limits for the delta_i axis (default: (-0.6, 0.6)).
DEFAULT:
|
bw_method
|
Bandwidth method forwarded to
DEFAULT:
|
fig_kw
|
Keyword arguments forwarded to
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
fig
|
Matplotlib figure. |
Example
chains = pf.get_chains("figure2") fig = pf.plot.ridgeline_delta(chains, nbins=20)
Source code in src/primefeat/plots/pca.py
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 | |
RMSE_variance(pca_result, error_data, **fig_kw)
Plot RMSE and variance explained vs number of PCA components.
| PARAMETER | DESCRIPTION |
|---|---|
pca_result
|
PCAResults object containing effective_dim
|
error_data
|
List of dicts with keys: n_components, mean_rmse, median_rmse, percentile_95, variance_explained
|
**fig_kw
|
Keyword arguments passed to plt.subplots()
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Matplotlib Figure object. |