Skip to content

Commit

Permalink
Merge pull request #4 from CosmoStat/develop
Browse files Browse the repository at this point in the history
Merge Martin's and Lucie's commits
  • Loading branch information
AndreasTersenov authored Sep 1, 2023
2 parents 346cd19 + aae4f48 commit 222a1c3
Show file tree
Hide file tree
Showing 13 changed files with 513 additions and 41 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
build
sp_peaks.egg-info/
.ipynb_checkpoints
example/test_slics.ipynb
__pycache__
79 changes: 47 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,67 @@
## The impact of systematic errors on weak-lensing peak counts for UNIONS
# Weak-lensing peak counts with ShapePipe processed data

This repository containts the results of the paper "UNIONS: The impact of systematic errors on weak-lensing peak counts".
This python package contains methods for cosmological parameter inference from weak-lensing peak counts.
It works with galaxy catalogue data processed with ShapePipe, but is general to be used with other data.

Authors: Emma Ayçoberry, Virginia Ajani, Axel Guinot, Martin Kilbinger, Valeria Pettorino, Samuel Farrens, Jean-Luc Starck, Raphaël Gavazzi, Michael J. Hudson
To use this code, download this repository by clicking on the `Clone` button above.

The easiest way to install the required software is using `conda`.
You can follow the instructions to install Anaconda [here](https://docs.anaconda.com/anaconda/install/index.html) or miniconda [here](https://conda.io/projects/conda/en/latest/user-guide/install/index.html).

The Ultraviolet Near-Infrared Optical Northern Survey (UNIONS) is an ongoing deep photometric multi-band survey of the Northern sky. As part of UNIONS, the Canada-France Imaging Survey (CFIS) provides r-band data with a median seeing of 0.65 arcsec, which we use to study weak-lensing peak counts for cosmological inference.
This work aims to assess systematics effects for weak-lensing peak counts and their impact on cosmological parameters for the UNIONS survey. In particular, we present results on local calibration, metacalibration shear bias, baryonic feedback, the source galaxy redshift estimate, intrinsic alignment, and the cluster member dilution. We expect constraints to become more reliable with future (larger) data catalogues, for which the current pipeline will provide a starting point. This paper investigates for the first time with UNIONS weak-lensing data and peak counts the impact of the local calibration, residual multiplicative shear bias, redshift uncertainty, baryonic feedback, intrinsic alignment and cluster member dilution. The value of matter density parameter is the most impacted and can shift up to ~ 0.03 which corresponds to 0.5 sigma depending on the choices for each systematics. We expect constraints to become more reliable with future (larger) data catalogues, for which the current code provides a starting point.
Included is code to reproduce results and plots from Ayçoberry et al. (2022); see below.

## Content

1. [The python library](#the-python-library)
1. [Installation](#installation)
1. [Usage](#usage)
1. [Examples](#examples)
1. [The impact of systematic errors on weak-lensing peak counts for UNIONS](#the-impact-of-systematic-errors-on-weak-lensing-peak-counts-for-unions)
1. [Installation](#installation)
1. [Description of the input files](#description-of-the-input-files)
1. [Example](#example)

## The python library

### Installation

### Content
To create the conda environment type
```bash
conda create -f environment.yml
```

1. [Dependencies](#dependencies)
2. [Description of the input files](#description-of-the-input-files)
3. [Example](#example)
### Usage

Once installed, any library files can be used via `import` in a python script or notebook, e.g.:
```python
import sp_peaks
from sp_peaks import slics
```

#### Dependencies
### Examples

To be able to run the example, the following python packages should be installed with their specific dependencies in a `python==3.6` environment.
You can follow the instructions to install Anaconda [here](https://docs.anaconda.com/anaconda/install/index.html) or miniconda [here](https://conda.io/projects/conda/en/latest/user-guide/install/index.html) and create an environment as
Example notebooks and associated python scripts can be run in the `notebooks` folder.

``conda create --name <your_env> python==3.6.13``

then install the following packages:
## The impact of systematic errors on weak-lensing peak counts for UNIONS

This part reproduces the results of the paper "UNIONS: The impact of systematic errors on weak-lensing peak counts".

Authors: Emma Ayçoberry, Virginia Ajani, Axel Guinot, Martin Kilbinger, Valeria Pettorino, Samuel Farrens, Jean-Luc Starck, Raphaël Gavazzi, Michael J. Hudson

- [numpy](https://numpy.org/install/)
- [astropy](https://www.astropy.org)
- [lenspack](https://github.com/CosmoStat/lenspack.git)
- [scipy](https://scipy.org/install/)
- [scikit-learn](https://scikit-learn.org/stable/install.html)==0.20
- [matplotlib](https://matplotlib.org/stable/users/installing/index.html)
- [emcee](https://emcee.readthedocs.io/en/v2.2.1/user/install/)==3.1.1
- [joblib](https://joblib.readthedocs.io/en/latest/installing.html)
- [tqdm](https://github.com/tqdm/tqdm#installation)
- [chainconsumer](https://samreay.github.io/ChainConsumer/)
- [getdist](https://getdist.readthedocs.io/en/latest/intro.html#getting-started)
- [zenodo_get](https://github.com/dvolgyes/zenodo_get)
- [jupyter](https://jupyter.org/install)
Abstract:
The Ultraviolet Near-Infrared Optical Northern Survey (UNIONS) is an ongoing deep photometric multi-band survey of the Northern sky. As part of UNIONS, the Canada-France Imaging Survey (CFIS) provides r-band data with a median seeing of 0.65 arcsec, which we use to study weak-lensing peak counts for cosmological inference.
This work aims to assess systematics effects for weak-lensing peak counts and their impact on cosmological parameters for the UNIONS survey. In particular, we present results on local calibration, metacalibration shear bias, baryonic feedback, the source galaxy redshift estimate, intrinsic alignment, and cluster member dilution. We expect constraints to become more reliable with future (larger) data catalogues, for which the current pipeline will provide a starting point. This paper investigates for the first time with UNIONS weak-lensing data and peak counts the impact of the local calibration, residual multiplicative shear bias, redshift uncertainty, baryonic feedback, intrinsic alignment and cluster member dilution. The value of matter density parameter is the most impacted and can shift up to ~ 0.03 which corresponds to 0.5 sigma depending on the choices for each systematics. We expect constraints to become more reliable with future (larger) data catalogues, for which the current code provides a starting point.



### Installation

To create the conda environment type
```bash
conda create -f environment_3.6.yml
```
Note that python version 3.6.13 is required.


#### Description of the input files
Expand Down Expand Up @@ -138,11 +157,7 @@ param_cut = 19,

#### Example

To run the example you need to check that you have propery installed the [Dependencies](#dependencies) listed above in your environment and then git clone this repository as

`git clone https://github.com/CosmoStat/shear-pipe-peaks.git`

ad then go to the example folder and launch the jupyter notebook [constraints_CFIS-P3.ipynb](https://github.com/CosmoStat/shear-pipe-peaks/blob/main/example/constraints_CFIS-P3.ipynb) by doing:
After installing the package, see [Dependencies](#dependencies), go to the example folder and launch the jupyter notebook [constraints_CFIS-P3.ipynb](https://github.com/CosmoStat/shear-pipe-peaks/blob/main/example/constraints_CFIS-P3.ipynb) by typing:

`cd example`

Expand Down
15 changes: 15 additions & 0 deletions environment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: peaks
channels:
- conda-forge
dependencies:
- python>3.10
- pip>=21
- numpy
- astropy
- scipy
- scikit-learn
- matplotlib
- joblib
- tqdm
- pip:
- -r requirements.txt
15 changes: 15 additions & 0 deletions environment_3.6.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: peaks_3.6
channels:
- conda-forge
dependencies:
- python==3.6.13
- pip>=21
- numpy
- astropy
- scipy
- scikit-learn==0.20
- matplotlib
- joblib
- tqdm
- pip:
- -r requirements.txt
139 changes: 139 additions & 0 deletions example/constraints_CFIS-P3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import argparse
import numpy as np
import emcee
import numpy.linalg as la
import matplotlib.pyplot as plt
from sklearn.externals.joblib import Parallel, delayed, cpu_count
from multiprocessing import cpu_count, Pool
import time
from chainconsumer import ChainConsumer
from utils import *
from likelihood import *
import os
import sys

parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
SCRIPT NAME:
constraints_CFIS-P3.py
EXAMPLE:
python constraints_CFIS-P3.py '/home/baumont/software/shear-pipe-peaks' --nproc 4
"""
)
parser.add_argument('maindir', help='work directory containing input and output directories')
parser.add_argument('--nproc', help='number of processes', type=int, default=1)

args = parser.parse_args()

#for tex in ChainConsumer
pref = os.environ['CONDA_PREFIX']
os.environ["PATH"] += os.pathsep + '/Library/TeX/texbin'

#Specify the free parameters for redshift, shear calibration, baryonic correction and cut in S/N
# Check the README to know the possibility for the different cases and reproduce the plots
param_z = '065'
param_z_cov = '0.65'
param_cal = 'dm_1deg'
param_baryonic_correction = 'Fid'
param_cut = 19
n_patches = 13
maindir = args.maindir
nproc = args.nproc
#Load the file names for peaks file
file_name_peaks_Maps = (np.array(np.loadtxt(maindir+'/input/list_cosmo_peaks_z{}.txt'.format(param_z), usecols=0,dtype=str)))

#Extract cosmological parameters values from the simulations associated to eachpeak counts file
params = np.array([takes_params(f) for f in file_name_peaks_Maps])

#identifies fiducial cosmology index
index_fiducial = 15
params_fiducial = params[index_fiducial]

#load the peaks distributions for the theoretical prediction
Peaks_Maps_DM = np.array([np.load(maindir+'/input/peaks_z{}/%s'.format(param_z)%(fn), mmap_mode='r') for fn in file_name_peaks_Maps])[:,:,:param_cut]

# load baryon corrections
bar_corr = np.load(maindir+'/input/{}_correction.npy'.format(param_baryonic_correction))[:param_cut]

#Apply the baryonic correction
Peaks_Maps = np.copy(Peaks_Maps_DM)

if(param_baryonic_correction == 'no'):
# no baryonic correction
for i in range(Peaks_Maps_DM.shape[0]):
for j in range(Peaks_Maps_DM.shape[1]):
Peaks_Maps[i,j,:] = Peaks_Maps_DM[i,j,:] * 1
else:
# apply the choosen baryonic correction
for i in range(Peaks_Maps_DM.shape[0]):
for j in range(Peaks_Maps_DM.shape[1]):
Peaks_Maps[i,j,:] = Peaks_Maps_DM[i,j,:] * bar_corr

# take the mean over realizations
Peaks_Maps_mean=np.mean(Peaks_Maps, axis=1)

# create array for snr histogram
snr_array=np.linspace(-2,6,31)
snr_centers=0.5*(snr_array[1:]+snr_array[:-1])

#Load peaks distribution for data
peaks_data = np.load(maindir+'/input/peaks_mean_{}.npy'.format(param_cal), mmap_mode='r')[:param_cut]

#Train Gaussian Processes regressor
#this returns a tuple consisting of (list of GP, scaling)
ncpu = cpu_count()
gp_scaling=np.array([Parallel(n_jobs = ncpu, verbose = 5)(delayed(gp_train)(index_bin, params_train = params, obs_train = Peaks_Maps) for index_bin in range(Peaks_Maps.shape[2]))]).reshape(Peaks_Maps.shape[2], 2)

gp_list=gp_scaling[:,0]
scaling=gp_scaling[:,1]

#Covariance matrix
#Load peaks to compute covariance matrix
cov_peaks_DM = np.load(maindir+'/input/convergence_gal_mnv0.00000_om0.30000_As2.1000_peaks_2arcmin_{}_b030_snr_min_max_ngal_7.npy'.format(param_z_cov),\
mmap_mode='r')[:,:param_cut]

#Apply Baryonic Correction
cov_peaks = np.copy(cov_peaks_DM)
for i in range(Peaks_Maps_DM.shape[1]):
cov_peaks[i,:] = cov_peaks_DM[i,:] * bar_corr

#Compute covariance matrix and scale it for sky coverage
cov=(1/n_patches)*np.cov(cov_peaks.T)

#compute the inverse of the covariance
icov=la.inv(cov)

# get constraints with MCMC
# compute hartlap factor
n_real=Peaks_Maps_DM.shape[1]
n_bins=len(peaks_data)
norm=(n_real-n_bins-2)/(n_real-1)

#Define priors (range of sims)
M_nu_min = 0.06 # minimum from oscillation experiments
M_nu_max = 0.62
Omega_m_min = 0.18
Omega_m_max = 0.42
A_s_min = 1.29
A_s_max = 2.91

#Specify number of dimensions for parameter space, number of walkers, initial position
ndim, nwalkers = 3,250
pos = [params_fiducial + 1e-3*np.random.randn(ndim) for i in range(nwalkers)]

print("{0} CPUs".format(ncpu))

with Pool(processes=nproc) as pool:
sampler = emcee.EnsembleSampler(nwalkers, ndim, lnpost, pool=pool, args=[peaks_data, icov,gp_list, scaling, norm,M_nu_min, M_nu_max, Omega_m_min, Omega_m_max, A_s_min, A_s_max])
start = time.time()
sampler.run_mcmc(pos, 6500, progress=True)
end = time.time()
multi_time = end - start
print("Multiprocessing took {0:.1f} seconds".format(multi_time))
# remove burn-in
samples = sampler.chain[:,200:, :].reshape((-1, ndim))
#save results
np.save(maindir+'/output/constraints_z{}_{}_{}corr_{}snr.npy'.format(param_z,param_cal,param_baryonic_correction,param_cut),samples)
27 changes: 18 additions & 9 deletions example/cosmoSLICS_bookkeeping.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
"outputs": [],
"source": [
"import numpy as np\n",
"# this should go in SLICS interface module- check martin's existing part of SLICS interface module, this may be carried out in a simplier way\n",
"\n",
"def process_files(file_paths):\n",
"def read_SLICS_cats(file_paths):\n",
" \"\"\"Reads the information in the filename.\n",
"\n",
" Parameters\n",
Expand Down Expand Up @@ -92,7 +93,7 @@
" file_paths = file.readlines()\n",
" file_paths = [path.strip() for path in file_paths]\n",
"\n",
"data = process_files(file_paths)"
"data = read_SLICS_cats(file_paths)"
]
},
{
Expand All @@ -101,7 +102,8 @@
"metadata": {},
"outputs": [],
"source": [
"def read_cosmo_params(file_path):\n",
"# this should go in SLICS interface module\n",
"def read_SLICS_cosmo_params(file_path):\n",
" \"\"\"Reads the cosmological parameters from the .dat file.\n",
"\n",
" Parameters\n",
Expand Down Expand Up @@ -145,11 +147,13 @@
"metadata": {},
"outputs": [],
"source": [
"# this should be a function\n",
"\n",
"# Path to the .dat file\n",
"dat_file_path = \"/home/tersenov/shear-pipe-peaks/example/CosmoTable.dat\"\n",
"\n",
"# Read the cosmological parameters from the .dat file\n",
"cosmo_params = read_cosmo_params(dat_file_path)\n",
"cosmo_params = read_SLICS_cosmo_params(dat_file_path)\n",
"\n",
"# Map the IDs in the recarray to the corresponding cosmological parameters\n",
"mapped_params = []\n",
Expand Down Expand Up @@ -229,6 +233,7 @@
"metadata": {},
"outputs": [],
"source": [
"# I feel like we already have this function with the SLICS interface\n",
"def read_catalog_data(catalog_data):\n",
" ra = catalog_data[0]\n",
" dec = catalog_data[1]\n",
Expand All @@ -237,6 +242,10 @@
" kappa_sim = catalog_data[8]\n",
" return ra, dec, g1_sim, g2_sim, kappa_sim\n",
"\n",
"# we should discuss this- the following functions will go into a mass-map module that will \n",
"# contain the different methods. do they all require binned data?, \n",
"# at any rate, we should have a separate binning function\n",
"\n",
"def create_kappa_map(ra, dec, g1_sim, g2_sim, size_x_deg=10, size_y_deg=10, pixel_size_emap_amin=0.4):\n",
" x, y = radec2xy(np.mean(ra), np.mean(dec), ra, dec) # Project (ra,dec) -> (x,y)\n",
"\n",
Expand All @@ -249,6 +258,7 @@
" kappaE, kappaB = ks93(e1map, -e2map) # make kappa map (the minus sign has to be here for our data conventions)\n",
" return kappaE, kappaB\n",
"\n",
"\n",
"def add_noise_to_kappa_map(kappa_map, shape_noise, n_gal, pix_arcmin):\n",
" sigma_noise_CFIS = shape_noise / (np.sqrt(2 * n_gal * pix_arcmin**2))\n",
" noise_map_CFIS_z05 = sigma_noise_CFIS * np.random.randn(kappa_map.shape[0], kappa_map.shape[1]) # generate noise map\n",
Expand Down Expand Up @@ -296,7 +306,7 @@
" snr_maps.append(snr_map)\n",
" \n",
" return snr_maps\n",
"\n",
"# this should go into the summary statistics module that will have single scale peak counts, multi-scale peak counts, l1 norm, etc\n",
"def compute_single_scale_peak_counts(snr_map, kappa_snr):\n",
" \"\"\"\n",
" Compute peak counts for a single SNR map.\n",
Expand Down Expand Up @@ -626,7 +636,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "base",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -640,9 +650,8 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
},
"orig_nbformat": 4
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
Expand Down
Loading

0 comments on commit 222a1c3

Please sign in to comment.