Skip to content

Commit

Permalink
Merge branch 'main' into paddy/ruff-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
paddyroddy committed Sep 18, 2024
2 parents d4ccd3a + 2c4a712 commit d3241fd
Show file tree
Hide file tree
Showing 20 changed files with 198 additions and 161 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ on:

jobs:
check-pr-title:
if: ${{ github.actor != 'pre-commit-ci[bot]' }}
runs-on: ubuntu-latest
steps:
- uses: thehanimo/[email protected]
Expand Down
4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
ci:
autoupdate_commit_msg: "MNT: update pre-commit hooks"
autofix_commit_msg: "STY: pre-commit fixes"

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
Expand Down
6 changes: 3 additions & 3 deletions docs/user/how-glass-works.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ Radial discretisation
The discretisation in the radial (line of sight) direction is done in *GLASS*
using the concept of a :term:`radial window`, which consists of a window
function :math:`W` that assigns a weight :math:`W(z)` to each redshift
:math:`z`. In the *GLASS* code, the :class:`~glass.shells.RadialWindow` named
:math:`z`. In the *GLASS* code, the :class:`~glass.RadialWindow` named
tuple is used to define radial windows.

A sequence :math:`W_1, W_2, \ldots` of such window functions defines the shells
of the simulation. For example, the :func:`~glass.shells.tophat_windows`
of the simulation. For example, the :func:`~glass.tophat_windows`
function takes redshift boundaries and returns a sequence of top hat windows,
which are flat and non-overlapping.

Expand Down Expand Up @@ -169,7 +169,7 @@ shells:
In short, the requirements say that each shell has an effective redshift which
partitions the window functions of all other shells. In *GLASS*, it is stored
as the ``zeff`` attribute of :class:`~glass.shells.RadialWindow`. Functions
as the ``zeff`` attribute of :class:`~glass.RadialWindow`. Functions
that construct a list of windows for shells should ensure these requirements
are met.

Expand Down
24 changes: 9 additions & 15 deletions examples/1-basic/density.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,8 @@
"import camb\n",
"from cosmology import Cosmology\n",
"\n",
"# GLASS imports: matter, random fields, random points, galaxies\n",
"import glass.fields\n",
"import glass.galaxies\n",
"import glass.points\n",
"import glass.shells\n",
"# import GLASS for matter, random fields, random points, galaxies\n",
"import glass\n",
"\n",
"# cosmology for the simulation\n",
"h = 0.7\n",
Expand All @@ -68,10 +65,10 @@
"cosmo = Cosmology.from_camb(pars)\n",
"\n",
"# shells of 200 Mpc in comoving distance spacing\n",
"zb = glass.shells.distance_grid(cosmo, 0.0, 1.0, dx=200.0)\n",
"zb = glass.distance_grid(cosmo, 0.0, 1.0, dx=200.0)\n",
"\n",
"# linear radial window functions\n",
"ws = glass.shells.linear_windows(zb)\n",
"ws = glass.linear_windows(zb)\n",
"\n",
"# load the angular matter power spectra previously computed with CAMB\n",
"cls = np.load(\"cls.npy\")"
Expand Down Expand Up @@ -99,10 +96,10 @@
"outputs": [],
"source": [
"# compute Gaussian cls for lognormal fields for 3 correlated shells\n",
"gls = glass.fields.lognormal_gls(cls, nside=nside, lmax=lmax, ncorr=3)\n",
"gls = glass.lognormal_gls(cls, nside=nside, lmax=lmax, ncorr=3)\n",
"\n",
"# generator for lognormal matter fields\n",
"matter = glass.fields.generate_lognormal(gls, nside, ncorr=3)"
"matter = glass.generate_lognormal(gls, nside, ncorr=3)"
]
},
{
Expand Down Expand Up @@ -131,7 +128,7 @@
"dndz = np.full_like(z, 0.01)\n",
"\n",
"# distribute the dN/dz over the linear window functions\n",
"ngal = glass.shells.partition(z, dndz, ws)"
"ngal = glass.partition(z, dndz, ws)"
]
},
{
Expand Down Expand Up @@ -166,12 +163,9 @@
"# simulate and add galaxies in each matter shell to cube\n",
"for i, delta_i in enumerate(matter):\n",
" # simulate positions from matter density\n",
" for gal_lon, gal_lat, gal_count in glass.points.positions_from_delta(\n",
" ngal[i],\n",
" delta_i,\n",
" ):\n",
" for gal_lon, gal_lat, gal_count in glass.positions_from_delta(ngal[i], delta_i):\n",
" # sample redshifts uniformly in shell\n",
" gal_z = glass.galaxies.redshifts(gal_count, ws[i])\n",
" gal_z = glass.redshifts(gal_count, ws[i])\n",
"\n",
" # add counts to cube\n",
" z1 = gal_z * np.cos(np.deg2rad(gal_lon)) * np.cos(np.deg2rad(gal_lat))\n",
Expand Down
21 changes: 9 additions & 12 deletions examples/1-basic/lensing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,8 @@
"import camb\n",
"from cosmology import Cosmology\n",
"\n",
"# GLASS imports\n",
"import glass.fields\n",
"import glass.galaxies\n",
"import glass.lensing\n",
"import glass.shells\n",
"# import GLASS\n",
"import glass\n",
"\n",
"# cosmology for the simulation\n",
"h = 0.7\n",
Expand All @@ -72,10 +69,10 @@
"cosmo = Cosmology.from_camb(pars)\n",
"\n",
"# shells of 200 Mpc in comoving distance spacing\n",
"zb = glass.shells.distance_grid(cosmo, 0.0, 1.0, dx=200.0)\n",
"zb = glass.distance_grid(cosmo, 0.0, 1.0, dx=200.0)\n",
"\n",
"# linear radial window functions\n",
"ws = glass.shells.linear_windows(zb)\n",
"ws = glass.linear_windows(zb)\n",
"\n",
"# load the angular matter power spectra previously computed with CAMB\n",
"cls = np.load(\"cls.npy\")"
Expand Down Expand Up @@ -104,10 +101,10 @@
"source": [
"# compute Gaussian cls for lognormal fields for 3 correlated shells\n",
"# putting nside here means that the HEALPix pixel window function is applied\n",
"gls = glass.fields.lognormal_gls(cls, nside=nside, lmax=lmax, ncorr=3)\n",
"gls = glass.lognormal_gls(cls, nside=nside, lmax=lmax, ncorr=3)\n",
"\n",
"# generator for lognormal matter fields\n",
"matter = glass.fields.generate_lognormal(gls, nside, ncorr=3)"
"matter = glass.generate_lognormal(gls, nside, ncorr=3)"
]
},
{
Expand All @@ -132,7 +129,7 @@
"outputs": [],
"source": [
"# this will compute the convergence field iteratively\n",
"convergence = glass.lensing.MultiPlaneConvergence(cosmo)"
"convergence = glass.MultiPlaneConvergence(cosmo)"
]
},
{
Expand Down Expand Up @@ -162,7 +159,7 @@
"dndz = np.exp(-((z - 0.5) ** 2) / (0.1) ** 2)\n",
"\n",
"# distribute dN/dz over the radial window functions\n",
"ngal = glass.shells.partition(z, dndz, ws)"
"ngal = glass.partition(z, dndz, ws)"
]
},
{
Expand Down Expand Up @@ -202,7 +199,7 @@
" kappa_i = convergence.kappa\n",
"\n",
" # compute shear field\n",
" gamm1_i, gamm2_i = glass.lensing.shear_from_convergence(kappa_i)\n",
" gamm1_i, gamm2_i = glass.shear_from_convergence(kappa_i)\n",
"\n",
" # add to mean fields using the galaxy number density as weight\n",
" kappa_bar += ngal[i] * kappa_i\n",
Expand Down
11 changes: 5 additions & 6 deletions examples/1-basic/matter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@
"import camb\n",
"from cosmology import Cosmology\n",
"\n",
"# these are the GLASS imports: matter and random fields\n",
"import glass.fields\n",
"import glass.shells\n",
"# import GLASS for matter and random fields\n",
"import glass\n",
"\n",
"# cosmology for the simulation\n",
"h = 0.7\n",
Expand All @@ -67,16 +66,16 @@
"cosmo = Cosmology.from_camb(pars)\n",
"\n",
"# shells of 200 Mpc in comoving distance spacing\n",
"zb = glass.shells.distance_grid(cosmo, 0.0, 1.0, dx=200.0)\n",
"zb = glass.distance_grid(cosmo, 0.0, 1.0, dx=200.0)\n",
"\n",
"# load precomputed angular matter power spectra\n",
"cls = np.load(\"cls.npy\")\n",
"\n",
"# compute Gaussian cls for lognormal fields with 3 correlated shells\n",
"gls = glass.fields.lognormal_gls(cls, ncorr=3, nside=nside)\n",
"gls = glass.lognormal_gls(cls, ncorr=3, nside=nside)\n",
"\n",
"# this generator will yield the matter fields in each shell\n",
"matter = glass.fields.generate_lognormal(gls, nside, ncorr=3)"
"matter = glass.generate_lognormal(gls, nside, ncorr=3)"
]
},
{
Expand Down
19 changes: 9 additions & 10 deletions examples/1-basic/photoz.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"# GLASS imports: matter shells, galaxies, random points, and observational\n",
"import glass.galaxies\n",
"import glass.observations\n",
"# import GLASS for matter shells, galaxies, random points, and observational\n",
"import glass\n",
"\n",
"# how many arcmin2 over the entire sphere\n",
"from glass.core.constants import ARCMIN2_SPHERE\n",
Expand All @@ -53,7 +52,7 @@
"\n",
"# parametric galaxy redshift distribution\n",
"z = np.linspace(0, 3, 301)\n",
"dndz = n_arcmin2 * glass.observations.smail_nz(z, 1.0, 2.2, 1.5)\n",
"dndz = n_arcmin2 * glass.smail_nz(z, 1.0, 2.2, 1.5)\n",
"\n",
"# compute the over galaxy number density on the sphere\n",
"ngal = np.trapz(dndz, z)"
Expand Down Expand Up @@ -86,10 +85,10 @@
"n = np.random.poisson(ngal * ARCMIN2_SPHERE)\n",
"\n",
"# sample n true redshifts\n",
"ztrue = glass.galaxies.redshifts_from_nz(n, z, dndz)\n",
"ztrue = glass.redshifts_from_nz(n, z, dndz)\n",
"\n",
"# sample n photometric redshifts\n",
"zphot = glass.galaxies.gaussian_phz(ztrue, phz_sigma_0)"
"zphot = glass.gaussian_phz(ztrue, phz_sigma_0)"
]
},
{
Expand Down Expand Up @@ -146,7 +145,7 @@
"metadata": {},
"source": [
"Now define a number of photometric redshift bins. They are chosen by the\n",
":func:`~glass.observations.equal_dens_zbins` function to produce the same\n",
":func:`~glass.equal_dens_zbins` function to produce the same\n",
"number of galaxies in each bin.\n",
"\n"
]
Expand All @@ -166,7 +165,7 @@
"outputs": [],
"source": [
"nbins = 5\n",
"zbins = glass.observations.equal_dens_zbins(z, dndz, nbins)"
"zbins = glass.equal_dens_zbins(z, dndz, nbins)"
]
},
{
Expand All @@ -175,7 +174,7 @@
"source": [
"After the photometric bins are defined, make histograms of the *true* redshift\n",
"distribution $n(z)$ using the *photometric* redshifts for binning. Use\n",
"the :func:`~glass.observations.tomo_nz_gausserr()` function to also plot the\n",
"the :func:`~glass.tomo_nz_gausserr()` function to also plot the\n",
"expected tomographic redshift distributions with the same model.\n",
"\n"
]
Expand Down Expand Up @@ -210,7 +209,7 @@
}
],
"source": [
"tomo_nz = glass.observations.tomo_nz_gausserr(z, dndz, phz_sigma_0, zbins)\n",
"tomo_nz = glass.tomo_nz_gausserr(z, dndz, phz_sigma_0, zbins)\n",
"tomo_nz *= ARCMIN2_SPHERE * (z[-1] - z[0]) / 40\n",
"\n",
"for (z1, z2), nz in zip(zbins, tomo_nz):\n",
Expand Down
6 changes: 3 additions & 3 deletions examples/1-basic/shells.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
"import camb\n",
"from cosmology import Cosmology\n",
"\n",
"import glass\n",
"import glass.ext.camb\n",
"import glass.shells\n",
"\n",
"# cosmology for the simulation\n",
"h = 0.7\n",
Expand All @@ -67,10 +67,10 @@
"cosmo = Cosmology.from_camb(pars)\n",
"\n",
"# shells of 200 Mpc in comoving distance spacing\n",
"zgrid = glass.shells.distance_grid(cosmo, 0.0, 1.0, dx=200.0)\n",
"zgrid = glass.distance_grid(cosmo, 0.0, 1.0, dx=200.0)\n",
"\n",
"# triangular radial windows, equivalent to linear interpolation of n(z)\n",
"shells = glass.shells.linear_windows(zgrid)\n",
"shells = glass.linear_windows(zgrid)\n",
"\n",
"# compute angular matter power spectra with CAMB\n",
"cls = glass.ext.camb.matter_cls(pars, lmax, shells)"
Expand Down
34 changes: 12 additions & 22 deletions examples/2-advanced/cosmic_shear.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@
"from cosmology import Cosmology\n",
"\n",
"# GLASS modules: cosmology and everything in the glass namespace\n",
"import glass.fields\n",
"import glass.galaxies\n",
"import glass.lensing\n",
"import glass.points\n",
"import glass.shapes\n",
"import glass.shells\n",
"import glass\n",
"from glass.core.constants import ARCMIN2_SPHERE\n",
"\n",
"# cosmology for the simulation\n",
Expand All @@ -70,10 +65,10 @@
"cosmo = Cosmology.from_camb(pars)\n",
"\n",
"# shells of 200 Mpc in comoving distance spacing\n",
"zb = glass.shells.distance_grid(cosmo, 0.0, 1.0, dx=200.0)\n",
"zb = glass.distance_grid(cosmo, 0.0, 1.0, dx=200.0)\n",
"\n",
"# linear window function for shells\n",
"ws = glass.shells.linear_windows(zb)\n",
"ws = glass.linear_windows(zb)\n",
"\n",
"# load the angular matter power spectra previously computed with CAMB\n",
"cls = np.load(\"../1-basic/cls.npy\")"
Expand Down Expand Up @@ -102,10 +97,10 @@
"source": [
"# compute Gaussian cls for lognormal fields for 3 correlated shells\n",
"# putting nside here means that the HEALPix pixel window function is applied\n",
"gls = glass.fields.lognormal_gls(cls, nside=nside, lmax=lmax, ncorr=3)\n",
"gls = glass.lognormal_gls(cls, nside=nside, lmax=lmax, ncorr=3)\n",
"\n",
"# generator for lognormal matter fields\n",
"matter = glass.fields.generate_lognormal(gls, nside, ncorr=3)"
"matter = glass.generate_lognormal(gls, nside, ncorr=3)"
]
},
{
Expand All @@ -130,7 +125,7 @@
"outputs": [],
"source": [
"# this will compute the convergence field iteratively\n",
"convergence = glass.lensing.MultiPlaneConvergence(cosmo)"
"convergence = glass.MultiPlaneConvergence(cosmo)"
]
},
{
Expand Down Expand Up @@ -167,7 +162,7 @@
"dndz *= n_arcmin2 / np.trapz(dndz, z)\n",
"\n",
"# distribute dN/dz over the radial window functions\n",
"ngal = glass.shells.partition(z, dndz, ws)"
"ngal = glass.partition(z, dndz, ws)"
]
},
{
Expand Down Expand Up @@ -213,21 +208,16 @@
" # compute the lensing maps for this shell\n",
" convergence.add_window(delta_i, ws[i])\n",
" kappa_i = convergence.kappa\n",
" gamm1_i, gamm2_i = glass.lensing.shear_from_convergence(kappa_i)\n",
" gamm1_i, gamm2_i = glass.shear_from_convergence(kappa_i)\n",
"\n",
" # generate galaxy positions uniformly over the sphere\n",
" for gal_lon, gal_lat, gal_count in glass.points.uniform_positions(ngal[i]):\n",
" for gal_lon, gal_lat, gal_count in glass.uniform_positions(ngal[i]):\n",
" # generate galaxy ellipticities from the chosen distribution\n",
" gal_eps = glass.shapes.ellipticity_intnorm(gal_count, sigma_e)\n",
" gal_eps = glass.ellipticity_intnorm(gal_count, sigma_e)\n",
"\n",
" # apply the shear fields to the ellipticities\n",
" gal_she = glass.galaxies.galaxy_shear(\n",
" gal_lon,\n",
" gal_lat,\n",
" gal_eps,\n",
" kappa_i,\n",
" gamm1_i,\n",
" gamm2_i,\n",
" gal_she = glass.galaxy_shear(\n",
" gal_lon, gal_lat, gal_eps, kappa_i, gamm1_i, gamm2_i\n",
" )\n",
"\n",
" # map the galaxy shears to a HEALPix map; this is opaque but works\n",
Expand Down
Loading

0 comments on commit d3241fd

Please sign in to comment.