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

gh-107: add all public functions/classes under glass namespace #221

Merged
merged 6 commits into from
Sep 18, 2024
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
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
23 changes: 9 additions & 14 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.shells\n",
"import glass.fields\n",
"import glass.points\n",
"import glass.galaxies\n",
"# import GLASS for matter, random fields, random points, galaxies\n",
"import glass\n",
"\n",
"\n",
"# cosmology for the simulation\n",
Expand All @@ -66,10 +63,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 @@ -97,10 +94,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 @@ -129,7 +126,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 @@ -164,11 +161,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], 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.shells\n",
"import glass.fields\n",
"import glass.lensing\n",
"import glass.galaxies\n",
"# import GLASS\n",
"import glass\n",
"\n",
"\n",
"# cosmology for the simulation\n",
Expand All @@ -70,10 +67,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 @@ -102,10 +99,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 +127,7 @@
"outputs": [],
"source": [
"# this will compute the convergence field iteratively\n",
"convergence = glass.lensing.MultiPlaneConvergence(cosmo)"
"convergence = glass.MultiPlaneConvergence(cosmo)"
]
},
{
Expand Down Expand Up @@ -160,7 +157,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 @@ -200,7 +197,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.shells\n",
"import glass.fields\n",
"# import GLASS for matter and random fields\n",
"import glass\n",
"\n",
"\n",
"# cosmology for the simulation\n",
Expand All @@ -65,16 +64,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 numpy as np\n",
"import matplotlib.pyplot as plt\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 @@ -54,7 +53,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 @@ -87,10 +86,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 @@ -147,7 +146,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 @@ -167,7 +166,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 @@ -176,7 +175,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 @@ -211,7 +210,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 @@ -43,7 +43,7 @@
"import camb\n",
"from cosmology import Cosmology\n",
"\n",
"import glass.shells\n",
"import glass\n",
"import glass.ext.camb\n",
"\n",
"\n",
Expand All @@ -64,10 +64,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
28 changes: 11 additions & 17 deletions examples/2-advanced/cosmic_shear.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,7 @@
"import camb\n",
"from cosmology import Cosmology\n",
"\n",
"# GLASS modules: cosmology and everything in the glass namespace\n",
"import glass.shells\n",
"import glass.fields\n",
"import glass.points\n",
"import glass.shapes\n",
"import glass.lensing\n",
"import glass.galaxies\n",
"import glass\n",
"from glass.core.constants import ARCMIN2_SPHERE\n",
"\n",
"\n",
Expand All @@ -68,10 +62,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 @@ -100,10 +94,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 @@ -128,7 +122,7 @@
"outputs": [],
"source": [
"# this will compute the convergence field iteratively\n",
"convergence = glass.lensing.MultiPlaneConvergence(cosmo)"
"convergence = glass.MultiPlaneConvergence(cosmo)"
]
},
{
Expand Down Expand Up @@ -165,7 +159,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 @@ -211,15 +205,15 @@
" # 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_she = glass.galaxy_shear(\n",
" gal_lon, gal_lat, gal_eps, kappa_i, gamm1_i, gamm2_i\n",
" )\n",
"\n",
Expand Down
Loading