GetDist : rescale contours by changing limits on both x-axis and y-axis

Use of Healpix, camb, CLASS, cosmomc, compilers, etc.
Post Reply
Dournac Fabien
Posts: 43
Joined: May 18 2019
Affiliation: IRAP
Contact:

GetDist : rescale contours by changing limits on both x-axis and y-axis

Post by Dournac Fabien » May 06 2020

Hello,

I have a first example of triangle_plot which represents the joint distribution of different parameters :

Code: Select all

# Show plots inline, and load main getdist plot module and samples class
from __future__ import print_function
import sys, os
sys.path.insert(0,os.path.realpath(os.path.join(os.getcwd(),'..')))
import numpy as np
import getdist 
from getdist import plots
import matplotlib.pyplot as plt
import matplotlib as mpl
# The plotting scripts also let you plot Gaussian (or Gaussian mixture) contours 
from getdist.gaussian_mixtures import GaussianND
from getdist.gaussian_mixtures import Mixture2D
print('GetDist Version: %s, Matplotlib version: %s'%(getdist.__version__, plt.matplotlib.__version__))

## Load Fisher matrix and invert it to get Covariance array
# Load Fisher matrix
File1 = np.loadtxt('File1.dat')
# Invert to get Covariance matrix
COV_File1 = np.linalg.inv(File1)[0:7,0:7]

# Mean of each cosmo parameters : respect order of generated "Big" Fisher matrix
mean = [0.32, 0.05, -1.0, 0.0, 0.96, 0.67, 0.815] 

# Names and labels
names = ['Omega_m', 'Omega_b', 'w_0', 'w_a', 'h', 'n_s', 'sigma_8']
labels = [r'\Omega_{m}', r'\Omega_{b}', r'w_{0}', r'w_{a}', r'h', r'n_{s}', r'\sigma_{8}']

# Current method
matrix1 = GaussianND(mean, COV_File1, labels = labels, names = names)

# Plot triplot
plt.rcParams['text.usetex'] = True
plt.rc_context({'axes.autolimit_mode': 'round_numbers'})
g = plots.get_subplot_plotter()
g.settings.figure_legend_frame = True
g.settings.legend_fontsize = 24
g.settings.axes_labelsize = 24
g.settings.axes_fontsize = 20
g.settings.axis_tick_x_rotation = 45
g.settings.axis_tick_y_rotation = 45
g.settings.alpha_filled_add = 0.9
g.settings.title_limit_fontsize = 1

g.triangle_plot(matrix1, 
                names,
		filled = True,
		legend_labels = ['File1'], 
        contour_colors = ['green']
        #lims = lims4
		)
# Save triplot
g.export('output_example.png')
Below the generated plot (output_example.png) :
output_example.png
output_example.png (166.65 KiB) Viewed 174 times

Now, I would like to make increase the size of the 2D contours (and not necesseraly the likelihoods on the diagonal that are already well scaled).
To perform this, I thought about changing the options
lims = [x_min, x_max, y_min, y_max]
but I don't know how to use it.

I tried to apply 2 methods :

1) First method : I introduced a "factor" in the computing of bounds of each subplot and inject it (called
lims4
directly into
GaussianND
like this :

Code: Select all

matrix1 = GaussianND(mean, COV_File1, labels = labels, names = names, lims = lims4)
So the script looks like :

Code: Select all

# Show plots inline, and load main getdist plot module and samples class
from __future__ import print_function
import sys, os
sys.path.insert(0,os.path.realpath(os.path.join(os.getcwd(),'..')))
import numpy as np
import getdist 
from getdist import plots
import matplotlib.pyplot as plt
import matplotlib as mpl
# The plotting scripts also let you plot Gaussian (or Gaussian mixture) contours 
from getdist.gaussian_mixtures import GaussianND
from getdist.gaussian_mixtures import Mixture2D
print('GetDist Version: %s, Matplotlib version: %s'%(getdist.__version__, plt.matplotlib.__version__))

## Load Fisher matrix and invert it to get Covariance array
# Load Fisher matrix
File1 = np.loadtxt('File1.dat')
# Invert to get Covariance matrix
COV_File1 = np.linalg.inv(File1)[0:7,0:7]

# Mean of each cosmo parameters : respect order of generated "Big" Fisher matrix
mean = [0.32, 0.05, -1.0, 0.0, 0.96, 0.67, 0.815] 

# Compute lower and upper limit here with rescaling factor = 0.9 (this is just an example) on standard deviation : 
#this factor must be equal to 1.0 to match all the subplot sides. If this factor is lower than 1.0, only a part of 
#ellipsis filled will be displayed since I have decreased the interval, so zooming it. But it may also produce some problems,
# I don't know exactly the "auto-range" behavior.

lim1m = np.array([mean-0.9*np.sqrt(np.diag(COV_File1))])
lim1p = np.array([mean+0.9*np.sqrt(np.diag(COV_File1))])

# Build limits array
lims3 = np.dstack([lim1m[0,0:7], lim1p[0,0:7], lim1m[0,0:7], lim1p[0,0:7]])
lims4 = np.copy(lims3)

# DEBUG
print('limits array = ',lims4)

# Names and labels
names = ['Omega_m', 'Omega_b', 'w_0', 'w_a', 'h', 'n_s', 'sigma_8']
labels = [r'\Omega_{m}', r'\Omega_{b}', r'w_{0}', r'w_{a}', r'h', r'n_{s}', r'\sigma_{8}']

# Try to set limit below into GaussianND
matrix1 = GaussianND(mean, COV_File1, labels = labels, names = names, lims = lims4)

# First example 
#matrix1 = GaussianND(mean, COV_File1, labels = labels, names = names)

# Plot triplot
plt.rcParams['text.usetex'] = True
plt.rc_context({'axes.autolimit_mode': 'round_numbers'})
g = plots.get_subplot_plotter()
g.settings.figure_legend_frame = True
g.settings.legend_fontsize = 24
g.settings.axes_labelsize = 24
g.settings.axes_fontsize = 20
g.settings.axis_tick_x_rotation = 45
g.settings.axis_tick_y_rotation = 45
g.settings.alpha_filled_add = 0.9
g.settings.title_limit_fontsize = 1

g.triangle_plot(matrix1, 
                names,
		filled = True,
		legend_labels = ['File1'], 
        contour_colors = ['green']
		)
# Save triplot
g.export('output_example.png')
But I get the following error :
GetDist Version: 1.1.0, Matplotlib version: 3.2.1
limits array = [[[ 0.31861471 0.32138529 0.31861471 0.32138529]
[ 0.04961034 0.05038966 0.04961034 0.05038966]
[-1.01656593 -0.98343407 -1.01656593 -0.98343407]
[-0.05256537 0.05256537 -0.05256537 0.05256537]
[ 0.9596142 0.9603858 0.9596142 0.9603858 ]
[ 0.66913931 0.67086069 0.66913931 0.67086069]
[ 0.81350059 0.81649941 0.81350059 0.81649941]]]

Traceback (most recent call last):
File "example_cosmocoffee_mean_issue_debug_one_matrix.py", line 64, in <module>
contour_colors = ['green']
File "/Users/fab/Library/Python/3.7/lib/python/site-packages/getdist/plots.py", line 2398, in triangle_plot
lims=param_limits.get(param.name), ax=ax, _ret_range=True, **diag1d_kwargs)
File "/Users/fab/Library/Python/3.7/lib/python/site-packages/getdist/plots.py", line 1558, in plot_1d
ax=ax, **line_args)
File "/Users/fab/Library/Python/3.7/lib/python/site-packages/getdist/plots.py", line 946, in add_1d
density = root.density1D(param.name)
File "/Users/fab/Library/Python/3.7/lib/python/site-packages/getdist/gaussian_mixtures.py", line 182, in density1D
mn, mx = self.autoRanges(sigma_max)[index]
File "/Users/fab/Library/Python/3.7/lib/python/site-packages/getdist/gaussian_mixtures.py", line 106, in autoRanges
for i, (mn, mx) in enumerate(lims):
ValueError: too many values to unpack (expected 2)


The issue is that I don't know which limits to set for the y-axis of Likelihood : is Likelihood normalized ? In this case, can it be enough to put for the 7th row of limits :

Code: Select all

limits array =  [[[ 0.31861471  0.32138529  0.31861471  0.32138529]
  [ 0.04961034  0.05038966  0.04961034  0.05038966]
  [-1.01656593 -0.98343407 -1.01656593 -0.98343407]
  [-0.05256537  0.05256537 -0.05256537  0.05256537]
  [ 0.9596142   0.9603858   0.9596142   0.9603858 ]
  [ 0.66913931  0.67086069  0.66913931  0.67086069]
  [ 0.81350059  0.81649941  0.0  1.0]]]
I can't see how to change these 2 values or fix it the issue of bounds for likelihood.

2) Second method : I tried to put the limits directly as argument of triangle_plots like this :

Code: Select all

g.triangle_plot(matrix1,
                names,
        filled = True,
        legend_labels = ['File1'],
        contour_colors = ['green'],
        lims = lims4
        )

with [quote]lims4[/quote] computed like the first method :

So, the script this second try is :

[code]
# Show plots inline, and load main getdist plot module and samples class
from __future__ import print_function
import sys, os
sys.path.insert(0,os.path.realpath(os.path.join(os.getcwd(),'..')))
import numpy as np
import getdist 
from getdist import plots
import matplotlib.pyplot as plt
import matplotlib as mpl
# The plotting scripts also let you plot Gaussian (or Gaussian mixture) contours 
from getdist.gaussian_mixtures import GaussianND
from getdist.gaussian_mixtures import Mixture2D
print('GetDist Version: %s, Matplotlib version: %s'%(getdist.__version__, plt.matplotlib.__version__))

## Load Fisher matrix and invert it to get Covariance array
# Load Fisher matrix
File1 = np.loadtxt('File1.dat')
# Invert to get Covariance matrix
COV_File1 = np.linalg.inv(File1)[0:7,0:7]

# Mean of each cosmo parameters : respect order of generated "Big" Fisher matrix
mean = [0.32, 0.05, -1.0, 0.0, 0.96, 0.67, 0.815] 

# Compute lower and upper limit with rescaling factor = 0.3 on standard deviation : this factor may also be greater than 1.0 to increase the size of contours

lim1m = np.array([mean-0.3*np.sqrt(np.diag(COV_File1))])
lim1p = np.array([mean+0.3*np.sqrt(np.diag(COV_File1))])

# Build limits array
lims3 = list(np.dstack([lim1m[0,0:7], lim1p[0,0:7], lim1m[0,0:7], lim1p[0,0:7]]))
lims4 = np.copy(lims3)

# DEBUG
print('limits array = ',lims4)

# Names and labels
names = ['Omega_m', 'Omega_b', 'w_0', 'w_a', 'h', 'n_s', 'sigma_8']
labels = [r'\Omega_{m}', r'\Omega_{b}', r'w_{0}', r'w_{a}', r'h', r'n_{s}', r'\sigma_{8}']

# Current method
matrix1 = GaussianND(mean, COV_File1, labels = labels, names = names)

# Plot triplot
plt.rcParams['text.usetex'] = True
plt.rc_context({'axes.autolimit_mode': 'round_numbers'})
g = plots.get_subplot_plotter()
g.settings.figure_legend_frame = True
g.settings.legend_fontsize = 24
g.settings.axes_labelsize = 24
g.settings.axes_fontsize = 20
g.settings.axis_tick_x_rotation = 45
g.settings.axis_tick_y_rotation = 45
g.settings.alpha_filled_add = 0.9
g.settings.title_limit_fontsize = 1

g.triangle_plot(matrix1, 
                names,
		filled = True,
		legend_labels = ['File1'], 
        contour_colors = ['green'],
        lims = lims4
		)
# Save triplot
g.export('output_example.png')
In this case, I get the following error :

Code: Select all

GetDist Version: 1.1.0, Matplotlib version: 3.2.1
limits array =  [[[ 0.31861471  0.32138529  0.31861471  0.32138529]
  [ 0.04961034  0.05038966  0.04961034  0.05038966]
  [-1.01656593 -0.98343407 -1.01656593 -0.98343407]
  [-0.05256537  0.05256537 -0.05256537  0.05256537]
  [ 0.9596142   0.9603858   0.9596142   0.9603858 ]
  [ 0.66913931  0.67086069  0.66913931  0.67086069]
  [ 0.81350059  0.81649941  0.81350059  0.81649941]]]
Traceback (most recent call last):
  File "/Users/fab/Library/Python/3.7/lib/python/site-packages/matplotlib/axes/_base.py", line 1666, in axis
    xmin, xmax, ymin, ymax = limits
ValueError: not enough values to unpack (expected 4, got 1)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "example_cosmocoffee_mean_issue_debug_one_matrix.py", line 65, in <module>
    lims = lims4
  File "/Users/fab/Library/Python/3.7/lib/python/site-packages/getdist/plots.py", line 2441, in triangle_plot
    add_legend_proxy=i == 0 and i2 == 1, contour_args=contour_args, ax=ax, **kwargs)
  File "/Users/fab/Library/Python/3.7/lib/python/site-packages/getdist/plots.py", line 1663, in plot_2d
    self.set_axes(param_pair, ax=ax, **kwargs)
  File "/Users/fab/Library/Python/3.7/lib/python/site-packages/getdist/plots.py", line 1453, in set_axes
    ax.axis(lims)
  File "/Users/fab/Library/Python/3.7/lib/python/site-packages/matplotlib/axes/_base.py", line 1668, in axis
    raise TypeError('the first argument to axis() must be an '
TypeError: the first argument to axis() must be an interable of the form [xmin, xmax, ymin, ymax]
It seems there are bad passing parameters, especially for the
lims
parameter. I did naively the assignment :

Code: Select all

lims = lims4


with array
lims4
which is printed during execution and equals to :

Code: Select all

limits array =  [[[ 0.31861471  0.32138529  0.31861471  0.32138529]
  [ 0.04961034  0.05038966  0.04961034  0.05038966]
  [-1.01656593 -0.98343407 -1.01656593 -0.98343407]
  [-0.05256537  0.05256537 -0.05256537  0.05256537]
  [ 0.9596142   0.9603858   0.9596142   0.9603858 ]
  [ 0.66913931  0.67086069  0.66913931  0.67086069]
  [ 0.81350059  0.81649941  0.81350059  0.81649941]]]
So, as conclusion, I woud like to debug this issue and be able to increase the size of contours for each subplot (by applying a factor lower than 1.0 like I said above), either with first method 1) or second one 2), or another way.

Regards.

Post Reply