Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor bug fixes and options for cosmetic changes to plots used for ma… #220

Merged
merged 10 commits into from
Mar 31, 2021
38 changes: 27 additions & 11 deletions descqa/BiasVersusRedshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import numpy as np
import scipy.optimize as op
import re

from GCR import GCRQuery
import pyccl as ccl
Expand Down Expand Up @@ -31,30 +32,42 @@ def __init__(self, **kwargs): #pylint: disable=W0231
self.fig_ylim = kwargs['fig_ylim']
self.test_name = kwargs['test_name']
self.fit_range = kwargs['fit_range']
self.font_size = kwargs.get('font_size', 16)
self.legend_size = kwargs.get('legend_size', 10)
self.ell_max = kwargs['ell_max'] if 'ell_max' in kwargs.keys() else 20000
validation_filepath = os.path.join(self.data_dir, kwargs['data_filename'])
self.validation_data = np.loadtxt(validation_filepath, skiprows=2)

self.truncate_cat_name = kwargs.get('truncate_cat_name', False)
self.title_in_legend = kwargs.get('title_in_legend', True)

def plot_bias_results(self, corr_data, corr_theory, bias, z, catalog_name, output_dir):
fig, ax = plt.subplots(1,2)
fig, ax = plt.subplots(1, 2, gridspec_kw={'width_ratios': [5, 2]})
colors = plt.cm.plasma_r(np.linspace(0.1, 1, len(self.test_samples))) # pylint: disable=no-member

for sample_name, color in zip(self.test_samples, colors):
sample_corr = corr_data[sample_name]
sample_label = self.test_sample_labels.get(sample_name)
sample_th = corr_theory[sample_name]
ax[0].loglog(sample_corr[0], sample_th, c=color, label=sample_label)
ax[0].errorbar(sample_corr[0], sample_corr[1], sample_corr[2], marker='o', ls='', c=color)

ax[0].legend(loc='best')
ax[0].set_xlabel(self.fig_xlabel)
ax[0].loglog(sample_corr[0], sample_th, c=color)
ax[0].errorbar(sample_corr[0], sample_corr[1], sample_corr[2], marker='o', ls='', c=color,
label=sample_label)

if self.title_in_legend:
lgnd_title = catalog_name
title = self.data_label
else:
lgnd_title = None
title = '{} vs. {}'.format(catalog_name, self.data_label)
ax[0].legend(loc='lower left', title=lgnd_title, framealpha=0.5, fontsize=self.legend_size)
ax[0].set_xlabel(self.fig_xlabel, size=self.font_size)
ax[0].set_ylim(*self.fig_ylim)
ax[0].set_ylabel(self.fig_ylabel)
ax[0].set_title('{} vs. {}'.format(catalog_name, self.data_label), fontsize='medium')
ax[0].set_ylabel(self.fig_ylabel, size=self.font_size)
ax[0].set_title(title, fontsize='medium')
ax[1].plot(z,bias)
ax[1].set_title('Bias vs redshift', fontsize='medium')
ax[1].set_xlabel('$z$')
ax[1].set_ylabel('$b(z)$')
ax[1].set_xlabel('$z$', size=self.font_size)
ax[1].set_ylabel('$b(z)$', size=self.font_size)
plt.subplots_adjust(wspace=.05)
fig.tight_layout()
fig.savefig(os.path.join(output_dir, '{:s}.png'.format(self.test_name)), bbox_inches='tight')
plt.close(fig)
Expand All @@ -71,6 +84,9 @@ def run_on_single_catalog(self, catalog_instance, catalog_name, output_dir):
if not catalog_data:
return TestResult(skipped=True, summary='Missing requested quantities')

if self.truncate_cat_name:
catalog_name = re.split('_', catalog_name)[0]
evevkovacs marked this conversation as resolved.
Show resolved Hide resolved

# Initialize catalog's cosmology
cosmo = ccl.Cosmology(Omega_c=catalog_instance.cosmology.Om0-catalog_instance.cosmology.Ob0,
Omega_b=catalog_instance.cosmology.Ob0,
Expand Down
16 changes: 12 additions & 4 deletions descqa/CheckColors.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ def __init__(self, **kwargs): # pylint: disable=W0231
self.mag_fields_val = kwargs['mag_fields_val']
self.path_val = kwargs['path_val']
self.truncate_cat_name = kwargs.get('truncate_cat_name', False)
self.truncate_z_label = kwargs.get('truncate_z_label', False)
self.truncate_color_labels = kwargs.get('truncate_color_labels', False)


if len(kwargs['xcolor']) != 2 or len(kwargs['ycolor']) != 2:
Expand Down Expand Up @@ -376,10 +378,16 @@ def run_on_single_catalog(self, catalog_instance, catalog_name, output_dir):
MMD, pValue = obj.compute(iterations=self.kernel_iterations)
print("MMD statistics is {}".format(MMD))
print("The p-value of the test is {}".format(pValue))

ax.set_xlabel('{} - {}'.format(mag_field.format(self.xcolor[0]), mag_field.format(self.xcolor[1])))
ax.set_ylabel('{} - {}'.format(mag_field.format(self.ycolor[0]), mag_field.format(self.ycolor[1])))
title = "{} = {:.2} - {:.2}".format(self.redshift_cut, zlo, zhi)
if self.truncate_color_labels:
ax.set_xlabel('${} - {}$'.format(self.xcolor[0], self.xcolor[1]))
ax.set_ylabel('${} - {}$'.format(self.ycolor[0], self.ycolor[1]))
else:
ax.set_xlabel('{} - {}'.format(mag_field.format(self.xcolor[0]), mag_field.format(self.xcolor[1])))
ax.set_ylabel('{} - {}'.format(mag_field.format(self.ycolor[0]), mag_field.format(self.ycolor[1])))
if self.truncate_z_label:
title = '${:.2} < z < {:.2}$'.format(zlo, zhi)
else:
title = "{} = {:.2} - {:.2}".format(self.redshift_cut, zlo, zhi)
ax.text(0.05, 0.95, title, transform=ax.transAxes,
verticalalignment='top', color='black', fontsize='small')
title1 = "Compare metric {:.4} +- {:.4}".format(cd[0],cd[1])
Expand Down
34 changes: 24 additions & 10 deletions descqa/ColorRedshiftTest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import unicode_literals, absolute_import, division
import os
import re
import numpy as np
import matplotlib.colors as clr
from .base import BaseValidationTest, TestResult
Expand All @@ -22,6 +23,9 @@ def __init__(self, **kwargs):
super(ColorRedshiftTest, self).__init__()
# load test config options
self.kwargs = kwargs
self.truncate_cat_name = kwargs.get('truncate_cat_name', False)
self.title_in_legend = kwargs.get('title_in_legend', True)
self.font_size = kwargs.get('font_size', 16)
with open(os.path.join(self.data_dir, 'README.md')) as f:
self.validation_data = f.readline().strip()
self.plot_list = kwargs.get("plot_list", [])
Expand Down Expand Up @@ -55,6 +59,9 @@ def post_process_plot(self, ax):

def run_on_single_catalog(self, catalog_instance, catalog_name, output_dir):
plot_num = 0
if self.truncate_cat_name:
catalog_name = re.split('_', catalog_name)[0]

for plot_param in self.plot_list:
plot_num += 1
if plot_param["frame"] == "rest":
Expand All @@ -81,6 +88,7 @@ def run_on_single_catalog(self, catalog_instance, catalog_name, output_dir):
slct, title = self._get_selection_and_title(catalog_instance, title, plot_param,
redshift_limit=plot_param['redshift_limit'],
redshift_block_limit=plot_param['redshift_block_limit'])
print(title)
fig, ax = plt.subplots()
# for ax_this in (ax, self.summary_ax):
if plot_param['redshift_limit'] is not None:
Expand All @@ -93,17 +101,22 @@ def run_on_single_catalog(self, catalog_instance, catalog_name, output_dir):
h, xbins, ybins = np.histogram2d(redshift[slct], clr_val[slct],
bins=(redshift_bins, np.linspace(-0.4, 2.2, 256)))
if plot_param["log_scale"]:
pc = ax.pcolor(xbins, ybins, h.T+3.0, norm=clr.LogNorm())
fig.colorbar(pc, ax=ax).set_label("Population Density + 3")
pc = ax.pcolor(xbins, ybins, h.T+1.0, norm=clr.LogNorm())
fig.colorbar(pc, ax=ax).set_label("Population Density + 1")
else:
pc = ax.pcolor(xbins, ybins, h.T)
fig.colorbar(pc, ax=ax).set_label("Population Density")
ax.set_ylabel('{} - {}'.format(mag1_str, mag2_str))
ax.set_xlabel('redshift')
mag1 = re.split('_', mag1_str)[1] #get filter
mag2 = re.split('_', mag2_str)[1] #get filter
ax.set_ylabel('{} - {}'.format(mag1, mag2), size=self.font_size)
ax.set_xlabel('z', size=self.font_size)
if self.title_in_legend:
title = '{}\n{}'.format(catalog_name, title)
else:
ax.set_title(catalog_name)
ax.text(0.05, 0.95, title, transform=ax.transAxes,
verticalalignment='top', color='white',
fontsize='small')
ax.set_title(catalog_name)
fig.savefig(os.path.join(output_dir, 'plot_{}.png'.format(plot_num)))
plt.close(fig)

Expand Down Expand Up @@ -179,7 +192,7 @@ def _get_selection_and_title(self, catalog_instance, title, plot_param,
redshift_limit=redshift_limit,
redshift_block_limit=redshift_block_limit)
slct = slct & (np.log10(sm) > plot_param["stellar_mass_cut"])
title += "M$_{{*}}$ > {}, ".format(plot_param["stellar_mass_cut"])
title += "$\\log_{{10}}(M_{{*}}/M_\\odot) > {}$, ".format(plot_param["stellar_mass_cut"])
title_elem += 1
if title_elem % title_elem_per_line == 0:
title += "\n"
Expand All @@ -188,7 +201,7 @@ def _get_selection_and_title(self, catalog_instance, title, plot_param,
redshift_limit=redshift_limit,
redshift_block_limit=redshift_block_limit)
slct = slct & (np.log10(halo_mass) > plot_param["halo_mass_cut"])
title += "M$_{{halo}}$ > {}, ".format(plot_param["halo_mass_cut"])
title += "$\\log_{{10}}(M_{{halo}}/M_\\odot) > {}$, ".format(plot_param["halo_mass_cut"])
title_elem += 1
if title_elem % title_elem_per_line == 0:
title += "\n"
Expand All @@ -208,15 +221,16 @@ def _get_selection_and_title(self, catalog_instance, title, plot_param,
redshift_limit=redshift_limit,
redshift_block_limit=redshift_block_limit)
slct = slct & (rs == plot_param["red_sequence_cut"])
title += "red seq = {}, ".format(plot_param["red_sequence_cut"])
title += "red sequence = {}, ".format(plot_param["red_sequence_cut"])
title_elem += 1
if title_elem % title_elem_per_line == 0:
title += "\n"

#remove trailing ", "
title = title[0:-2]

return slct, title



def conclude_test(self, output_dir):
# self.post_process_plot(self.summary_ax)
# self.summary_fig.savefig(os.path.join(output_dir, 'summary.png'))
Expand Down
Loading