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

Migrating the Legacy Blinding Code from the other repo into this one #6

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Author - Stuart McAlpine - [email protected] - Jan 2023
name: DESC Legacy Blinding for Cosmosis

# How does the workflow get triggered?
on:
# Triggers when push/pull-request made to the main branch.
pull_request:
branches:
- main
push:
branches:
- main
- develop

# List of jobs for this workflow.

jobs:
# Our pytest job.
legacy_blinding-pytest:
# Our strategy lists the OS and Python versions we want to test on.
strategy:
# Don't quit all jobs if only one job fails.
fail-fast: false
matrix:
python-version: ["3.9"]
os: [ubuntu-20.04, macos-latest]
include:
- os: ubuntu-20.04
INSTALL_DEPS: sudo apt-get update && sudo apt-get -y install gfortran-7 swig libopenmpi-dev openmpi-bin libopenblas-dev && sudo ln -s `which gfortran-7` /usr/local/bin/gfortran
- os: macos-latest
INSTALL_DEPS: brew update-reset && HOMEBREW_NO_AUTO_UPDATE=1 HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=1 brew install gcc swig libomp open-mpi openblas && if [ ! -f /usr/local/bin/gfortran ]; then ln -s /usr/local/bin/gfortran-$(brew list --versions gcc | awk '{print $2}' | cut -d. -f1) /usr/local/bin/gfortran; fi

# What operating system is this job running on?
runs-on: ${{ matrix.os }}

# Our CI steps for this job.
steps:
# Check out this repository code.
- name: Check out repository code
uses: actions/checkout@v3

# Install Python.
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: ${{ matrix.INSTALL_DEPS }}

# Install our package.
- name: Install legacy_blinding
run: |
python -m pip install --upgrade pip
python -m pip install .
pytest ./tests
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
__pycache__/
*.py[cod]
*$py.class
*.DS_Store

# C extensions
*.so
Expand Down
42 changes: 40 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,40 @@
# legacy_blinding
Repository with Legacy Cosmology Blinding for 3x2pt data-vector blinding using Cosmosis v2
# Legacy Cosmosis Blinding for 2pt Data-Vectors using Cosmosis

This repository contains an implementation of [Muir et al. 2020](https://arxiv.org/abs/1911.05929) data-vector blinding strategy using Cosmosis V2.
The package in this repository is a direct adaptation of Jessie Muir's DES Y3 blinding scripts to the DESC context.

For the DESC Blinding Library, see [here](https://github.com/LSSTDESC/Blinding).

## Installation
- Install cosmosis via `conda install -c conda-forge cosmosis`
- Activate the cosmosis configuration via `source cosmosis-configure`
- Install the cosmosis standard library via `conda install -c conda-forge cosmosis-build-standard-library`
- Install the standard library in the correct cosmosis folder `cosmosis-build-standard-library -i`

### Legacy Blinding installation
Simply install with
```bash
python -m pip install -e .
```

### Test the installation
To run the unit tests:
```bash
cd tests
pytest .
```

## Usage
> Don't forget to `source cosmosis-configure`

To get the docstring on the command-line interface:
```
python -m blind_2pt_cosmosis --help
```

To run an example:
```bash
python -m blind_2pt_cosmosis -u src/blind_2pt_cosmosis/cosmosis_files/sim_fiducial.fits [--log-level DEBUG]
```

[!] Note: You can also include all the command line arguments in a `.config` file and call `python -m blind_2pt_cosmosis @example.config` instead. This will be adapted to use `yaml` files in the near future.
37 changes: 37 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "legacy_blinding"
description = "Legacy Cosmosis Blinding for DESC. This is a Cosmosis V2 port of Jessie Muir's legacy_blinding scripts for DES-Y3."
readme = "README.md"
requires-python = ">=3.8"
license = {file = "LICENSE"}
maintainers = [
{name = "Arthur Loureiro", email = "[email protected]"},
{name = "Jessie Muir", email = "[email protected]"},
]
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
]
dependencies = [
"numpy>=1.24.0",
"scipy>=1.9.0",
"cosmosis>=2.5",
"astropy>=5.1",
"pytest>=7.2.5",
"argparse>=1.0",
"sacc>=0.12",
]
keywords = ["desc", "python", "blinding", "cosmosis"]
dynamic = ["version"]

[tool.setuptools.packages.find]
where = ["src"]

[tool.setuptools.package-data]
"blind_2pt_cosmosis" = ["cosmosis_files/*.txt", "cosmosis_files/*.ini"]

Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/blind_2pt_cosmosis/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions src/blind_2pt_cosmosis/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Author: Arthur Loureiro <[email protected]> & Jessie Muir <[email protected]>
from ._version import __version__
49 changes: 49 additions & 0 deletions src/blind_2pt_cosmosis/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import logging
from .param_shifts import draw_flat_param_shift, get_factordict
from .io import get_parser, get_stored_seed_and_tag
from .run_cosmosis_2pt import run_cosmosis_togen_2ptdict
from .twopt_utils import apply_2pt_blinding_and_save_fits


def main():
"""
main function to be called from command line
"""
# gets the parser from the io module
parser = get_parser()
args = parser.parse_args()

# Configure the logger level based on the input argument
log_level = getattr(logging, args.log_level)
logging.basicConfig(level=log_level, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# Create a logger for the __main__ module
logger = logging.getLogger("2pt_blinding")
logger.debug(args)

params_shifts = draw_flat_param_shift(args.seed, args.paramshifts)
logger.debug(f"Parameters shifts are: {params_shifts}")

#get blinding factors
reff_dict = run_cosmosis_togen_2ptdict(inifile=args.ini) #args.ini is a template
logger.debug(f"Calculated Reference Dict")
# FIXME: Add nz_file and angles_file to args
shift_dict = run_cosmosis_togen_2ptdict(inifile=args.ini, pdict=params_shifts,
nz_file=None, angles_file=None)
logger.debug(f"Calculated Shifted Dict")
#gets the blinding factors data vectors
factor_dict = get_factordict(reff_dict, shift_dict, bftype = args.bftype)
logger.debug(f"Calculated Blinded Data Vectors")

# Gets some I/O information
storeseed, tagstr = get_stored_seed_and_tag(args)

# applies the shift to the 2pt data-vector:
#FIXME: This function is also saving the file, we want to split it!
apply_2pt_blinding_and_save_fits(factor_dict, origfitsfile=args.origfits,
outfname=args.outfname, outftag=tagstr,
bftype=args.bftype, storeseed = storeseed)


if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions src/blind_2pt_cosmosis/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "2023.10.11"
69 changes: 69 additions & 0 deletions src/blind_2pt_cosmosis/cosmosis_files/blinding_values_template.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
[cosmological_parameters]
omega_m = 0.1 0.3 0.9
h0 = 0.55 0.69 0.91
omega_b = 0.03 0.048 0.12
n_s = 0.87 0.97 1.07
sigma_8_input = 0.2 0.843 1.6
A_s = 0.5e-09 2.19e-9 5.0e-09
; omnuh2 = 0.0006 0.00083 0.01
omnuh2 = 0.0
massive_nu = 0
massless_nu = 3.046
omega_k = 0.0
tau = 0.0697186
w = -1.0
;Helium mass fraction. Needed for Planck
yhe = 0.245341

; %include ${METHODSDIR}/cosmosis/de_models/${DEMODEL}.ini

[shear_calibration_parameters]
m1 = -0.1 0.0 0.1
m2 = -0.1 0.0 0.1
m3 = -0.1 0.0 0.1
m4 = -0.1 0.0 0.1

[wl_photoz_errors]
bias_1 = -0.1 0.0 0.1
bias_2 = -0.1 0.0 0.1
bias_3 = -0.1 0.0 0.1
bias_4 = -0.1 0.0 0.1

[lens_photoz_errors]
bias_1 = -0.05 0.0 0.05
bias_2 = -0.05 0.0 0.05
bias_3 = -0.05 0.0 0.05
bias_4 = -0.05 0.0 0.05
bias_5 = -0.05 0.0 0.05

[bias_lens]
b1 = 0.8 1.7 3.0
b2 = 0.8 1.7 3.0
b3 = 0.8 1.7 3.0
b4 = 0.8 2.0 3.0
b5 = 0.8 2.0 3.0

[mag_alpha_lens]
; alpha_1 = -4. 0.903125 4.
; alpha_2 = -4. 0.68572965 4.
; alpha_3 = -4. 0.65340057 4.
; alpha_4 = -4. 1.58867862 4.
; alpha_5 = -4. 1.93754879 4.
alpha_1 = 0.903125
alpha_2 = 0.68572965
alpha_3 = 0.65340057
alpha_4 = 1.58867862
alpha_5 = 1.93754879

[intrinsic_alignment_parameters]
z_piv = 0.62
A1 = -5.0 0.7 5.0
A2 = -5.0 -1.36 5.0
alpha1=-5.0 -1.7 5.0
alpha2=-5.0 -2.5 5.0
bias_ta = 0.0 1.0 2.0
#bias_tt = 1.0




Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
; ############################################################
; This is a template file that is referenced by blind_2pt_usingcosmosis.py
; It is used as a template for running cosmosis to generate
; blinding factors to be applied to 2pt data vectors. To adapt
; for a specific pipeline:
; 1. Edit the %include params.ini line to point towards your baseline pipeline file.
; 2. The values file should be edited to match your fiducial run. Since we want
; to blind sigma8, sigma8_input needs to be in the cosmological parameters,
; so it may need to be a values file written specifically for blinding. You could
; always set it up to %include your standard values file though.
; >> the start values for each parameter is what will be used as reference vals
; when generating the blinding factors.
; 3. Make sure the file in 2PT_FILE points to a file with the format expected by
; your pipeline (with the same binning, etc.)
; 4. The modules list in [pipeline] should match your standard pipeline with a one
; addition: you need sigma8_rescale to be able to include sigma8_input as a parameter
; ############################################################


; import baseline parameter file; change this to point towards the params.ini file for
; whatever main pipeline you want to mimic
%include ./src/blind_2pt_cosmosis/cosmosis_files/params.ini

[DEFAULT]
; this 2PT file is where we'll pull the n(z) info for computing blinding factors
2PT_FILE = ./src/blind_2pt_cosmosis/cosmosis_files/sim_fiducial.fits
;2PT_FILE = pipeline/2pt_sim_1110_baseline_Y3cov.fits
;should this be my 2pt measurement DV?

[pipeline]
; THIS MODULE LIST SHOULD MATCH YOUR SETTINGS FOR YOUR STANDARD ANALYSIS PIPELINE
quiet=T
timing=F
debug=F
priors = ./src/blind_2pt_cosmosis/cosmosis_files/priors.ini
; sigma8_rescale included so we can set sigma8 as input
modules = consistency camb extrapolate fits_nz lens_photoz_bias fast_pt source_photoz_bias unbiased_galaxies IA pk_to_cl_gg pk_to_cl add_magnification add_intrinsic 2pt_shear 2pt_gal 2pt_gal_shear shear_m_bias 2pt_like
; if you want to blind sigma_8, set up values file to have sigma8_input as a parameter rather than A_s
values = ./src/blind_2pt_cosmosis/cosmosis_files/blinding_values_template.ini


[sigma8_rescale]
file = cosmosis-standard-library/utility/sample_sigma8/sigma8_rescale.py

[test]
; if the sampler happens to be set to test, make sure it doesn't
; save any output when we do blinding. We don't want to see the cosmology
; and 2pt functions that go into making the blinding factor!
save_dir=


Loading