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-231: add ruff linting to pre-commit #232

Merged
merged 34 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
6bec081
Add `ruff` linting to `pre-commit`
paddyroddy Sep 17, 2024
6646a40
Removing the `classmethod` stuff
paddyroddy Sep 17, 2024
431c12e
Fix some `ruff` linting issues
paddyroddy Sep 17, 2024
dfb69e8
Be careful with `isort`
paddyroddy Sep 17, 2024
f41a589
Fix notebook imports
paddyroddy Sep 17, 2024
c880960
FIx import
paddyroddy Sep 17, 2024
bc26cde
Import
paddyroddy Sep 17, 2024
d9ced9d
Remove blank line
paddyroddy Sep 17, 2024
ff6f47d
Ignore more rules
paddyroddy Sep 18, 2024
8fc670d
Fix more `ruff` rules
paddyroddy Sep 18, 2024
a3f00f2
More `ruff` fixing
paddyroddy Sep 18, 2024
cdd99b6
More `ruff` fixes
paddyroddy Sep 18, 2024
1267516
More `ruff`
paddyroddy Sep 18, 2024
6c936e1
No warnings from `ruff`
paddyroddy Sep 18, 2024
9e6388e
SIlence `D205`
paddyroddy Sep 18, 2024
9162dbf
Remove duplicate `_`
paddyroddy Sep 18, 2024
2611eeb
Add equation
paddyroddy Sep 18, 2024
fb63a5a
Change to `NamedTuple`
paddyroddy Sep 18, 2024
8e6ba5e
Merge branch 'main' into paddy/ruff-lint
paddyroddy Sep 18, 2024
75e37aa
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 18, 2024
d4ccd3a
Extend title `=`
paddyroddy Sep 18, 2024
d3241fd
Merge branch 'main' into paddy/ruff-lint
paddyroddy Sep 18, 2024
0c6e6ce
Add to git blame
paddyroddy Sep 18, 2024
14563b3
STY: pre-commit fixes
pre-commit-ci[bot] Sep 18, 2024
19d1a6c
Extend docs
paddyroddy Sep 18, 2024
4d36ab5
Update docstring
paddyroddy Sep 18, 2024
b83bc79
Fix docs
paddyroddy Sep 18, 2024
2236bd1
Add second underscore
paddyroddy Sep 18, 2024
f88ddf1
Fix all doc titles
paddyroddy Sep 18, 2024
dec1bae
Merge branch 'main' into paddy/ruff-lint
Saransh-cpp Sep 19, 2024
8fb20f3
cleanup from code review
Saransh-cpp Sep 19, 2024
cf2982f
headings without period
Saransh-cpp Sep 19, 2024
7a7e6e8
docs inside class, allow yoda conditions
Saransh-cpp Sep 19, 2024
1bdcc6b
avoid one-liner conditionals
Saransh-cpp Sep 19, 2024
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
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.3
hooks:
- id: ruff
Saransh-cpp marked this conversation as resolved.
Show resolved Hide resolved
- id: ruff-format
- repo: https://github.com/pappasam/toml-sort
rev: v0.23.1
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

project = "GLASS"
author = "GLASS developers"
copyright = f"2022-{datetime.date.today().year} {author}"
copyright = f"2022-{datetime.date.today().year} {author}" # noqa: A001, DTZ011
version = metadata.version("glass")
release = version

Expand Down
21 changes: 13 additions & 8 deletions examples/1-basic/density.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,19 @@
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from matplotlib.colors import LogNorm\n",
"\n",
"# use the CAMB cosmology that generated the matter power spectra\n",
"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",
"\n",
"import glass.points\n",
"import glass.shells\n",
"\n",
"# cosmology for the simulation\n",
"h = 0.7\n",
Expand All @@ -59,7 +58,10 @@
"\n",
"# set up CAMB parameters for matter angular power spectrum\n",
"pars = camb.set_params(\n",
" H0=100 * h, omch2=Oc * h**2, ombh2=Ob * h**2, NonLinear=camb.model.NonLinear_both\n",
" H0=100 * h,\n",
" omch2=Oc * h**2,\n",
" ombh2=Ob * h**2,\n",
" NonLinear=camb.model.NonLinear_both,\n",
")\n",
"\n",
"# get the cosmology from CAMB\n",
Expand Down Expand Up @@ -165,7 +167,8 @@
"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",
" ngal[i],\n",
" delta_i,\n",
" ):\n",
" # sample redshifts uniformly in shell\n",
" gal_z = glass.galaxies.redshifts(gal_count, ws[i])\n",
Expand All @@ -174,8 +177,10 @@
" z1 = gal_z * np.cos(np.deg2rad(gal_lon)) * np.cos(np.deg2rad(gal_lat))\n",
" z2 = gal_z * np.sin(np.deg2rad(gal_lon)) * np.cos(np.deg2rad(gal_lat))\n",
" z3 = gal_z * np.sin(np.deg2rad(gal_lat))\n",
" (i, j, k), c = np.unique(\n",
" np.searchsorted(zcub[1:], [z1, z2, z3]), axis=1, return_counts=True\n",
" (i, j, k), c = np.unique( # noqa: PLW2901\n",
" np.searchsorted(zcub[1:], [z1, z2, z3]),\n",
" axis=1,\n",
" return_counts=True,\n",
" )\n",
" cube[i, j, k] += c"
]
Expand Down
21 changes: 13 additions & 8 deletions examples/1-basic/lensing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,19 @@
},
"outputs": [],
"source": [
"import numpy as np\n",
"import healpy as hp\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"# use the CAMB cosmology that generated the matter power spectra\n",
"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",
"\n",
"import glass.lensing\n",
"import glass.shells\n",
"\n",
"# cosmology for the simulation\n",
"h = 0.7\n",
Expand All @@ -63,7 +62,10 @@
"\n",
"# set up CAMB parameters for matter angular power spectrum\n",
"pars = camb.set_params(\n",
" H0=100 * h, omch2=Oc * h**2, ombh2=Ob * h**2, NonLinear=camb.model.NonLinear_both\n",
" H0=100 * h,\n",
" omch2=Oc * h**2,\n",
" ombh2=Ob * h**2,\n",
" NonLinear=camb.model.NonLinear_both,\n",
")\n",
"\n",
"# get the cosmology from CAMB\n",
Expand Down Expand Up @@ -256,22 +258,25 @@
"source": [
"# get the angular power spectra of the lensing maps\n",
"sim_cls = hp.anafast(\n",
" [kappa_bar, gamm1_bar, gamm2_bar], pol=True, lmax=lmax, use_pixel_weights=True\n",
" [kappa_bar, gamm1_bar, gamm2_bar],\n",
" pol=True,\n",
" lmax=lmax,\n",
" use_pixel_weights=True,\n",
")\n",
"\n",
"# get the expected cls from CAMB\n",
"pars.min_l = 1\n",
"pars.set_for_lmax(lmax)\n",
"pars.SourceWindows = [\n",
" camb.sources.SplinedSourceWindow(z=z, W=dndz, source_type=\"lensing\")\n",
" camb.sources.SplinedSourceWindow(z=z, W=dndz, source_type=\"lensing\"),\n",
"]\n",
"theory_cls = camb.get_results(pars).get_source_cls_dict(lmax=lmax, raw_cl=True)\n",
"\n",
"# get the HEALPix pixel window function, since the lensing fields have it\n",
"pw = hp.pixwin(nside, lmax=lmax)\n",
"\n",
"# plot the realised and expected cls\n",
"l = np.arange(lmax + 1)\n",
"l = np.arange(lmax + 1) # noqa: E741\n",
"plt.plot(l, sim_cls[0], \"-k\", lw=2, label=\"simulation\")\n",
"plt.plot(l, theory_cls[\"W1xW1\"] * pw**2, \"-r\", lw=2, label=\"expectation\")\n",
"plt.xscale(\"symlog\", linthresh=10, linscale=0.5, subs=[2, 3, 4, 5, 6, 7, 8, 9])\n",
Expand Down
12 changes: 7 additions & 5 deletions examples/1-basic/matter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,17 @@
},
"outputs": [],
"source": [
"import numpy as np\n",
"import healpy as hp\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"# uses the CAMB cosmology which produced the cls\n",
"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",
"\n",
"import glass.shells\n",
"\n",
"# cosmology for the simulation\n",
"h = 0.7\n",
Expand All @@ -58,7 +57,10 @@
"\n",
"# set up CAMB parameters for matter angular power spectrum\n",
"pars = camb.set_params(\n",
" H0=100 * h, omch2=Oc * h**2, ombh2=Ob * h**2, NonLinear=camb.model.NonLinear_both\n",
" H0=100 * h,\n",
" omch2=Oc * h**2,\n",
" ombh2=Ob * h**2,\n",
" NonLinear=camb.model.NonLinear_both,\n",
")\n",
"\n",
"# get the cosmology from CAMB\n",
Expand Down Expand Up @@ -136,7 +138,7 @@
" theta, phi = hp.vec2ang(np.transpose([x[g] / zmax, y[g] / zmax, zg]))\n",
" grid[g] = hp.get_interp_val(delta_i, theta, phi)\n",
" ax.add_patch(\n",
" plt.Circle((0, 0), zmax / zend, fc=\"none\", ec=\"k\", lw=0.5, alpha=0.5, zorder=1)\n",
" plt.Circle((0, 0), zmax / zend, fc=\"none\", ec=\"k\", lw=0.5, alpha=0.5, zorder=1),\n",
" )\n",
"\n",
"# show the grid of shells\n",
Expand Down
3 changes: 1 addition & 2 deletions examples/1-basic/photoz.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
},
"outputs": [],
"source": [
"import numpy as np\n",
"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",
Expand All @@ -45,7 +45,6 @@
"# how many arcmin2 over the entire sphere\n",
"from glass.core.constants import ARCMIN2_SPHERE\n",
"\n",
"\n",
"# galaxy density\n",
"n_arcmin2 = 1e-4\n",
"\n",
Expand Down
11 changes: 7 additions & 4 deletions examples/1-basic/shells.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"import camb\n",
"from cosmology import Cosmology\n",
"\n",
"import glass.shells\n",
"import glass.ext.camb\n",
"\n",
"import glass.shells\n",
"\n",
"# cosmology for the simulation\n",
"h = 0.7\n",
Expand All @@ -57,7 +57,10 @@
"\n",
"# set up CAMB parameters for matter angular power spectrum\n",
"pars = camb.set_params(\n",
" H0=100 * h, omch2=Oc * h**2, ombh2=Ob * h**2, NonLinear=camb.model.NonLinear_both\n",
" H0=100 * h,\n",
" omch2=Oc * h**2,\n",
" ombh2=Ob * h**2,\n",
" NonLinear=camb.model.NonLinear_both,\n",
")\n",
"\n",
"# get the cosmology from CAMB\n",
Expand Down
25 changes: 16 additions & 9 deletions examples/2-advanced/cosmic_shear.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,23 @@
},
"outputs": [],
"source": [
"import numpy as np\n",
"import healpy as hp\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"# use the CAMB cosmology that generated the matter power spectra\n",
"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.galaxies\n",
"import glass.lensing\n",
"import glass.points\n",
"import glass.shapes\n",
"import glass.lensing\n",
"import glass.galaxies\n",
"import glass.shells\n",
"from glass.core.constants import ARCMIN2_SPHERE\n",
"\n",
"\n",
"# cosmology for the simulation\n",
"h = 0.7\n",
"Oc = 0.25\n",
Expand All @@ -61,7 +60,10 @@
"\n",
"# set up CAMB parameters for matter angular power spectrum\n",
"pars = camb.set_params(\n",
" H0=100 * h, omch2=Oc * h**2, ombh2=Ob * h**2, NonLinear=camb.model.NonLinear_both\n",
" H0=100 * h,\n",
" omch2=Oc * h**2,\n",
" ombh2=Ob * h**2,\n",
" NonLinear=camb.model.NonLinear_both,\n",
")\n",
"\n",
"# get the cosmology from CAMB\n",
Expand Down Expand Up @@ -220,7 +222,12 @@
"\n",
" # apply the shear fields to the ellipticities\n",
" gal_she = glass.galaxies.galaxy_shear(\n",
" gal_lon, gal_lat, gal_eps, kappa_i, gamm1_i, gamm2_i\n",
" gal_lon,\n",
" gal_lat,\n",
" gal_eps,\n",
" kappa_i,\n",
" gamm1_i,\n",
" gamm2_i,\n",
" )\n",
"\n",
" # map the galaxy shears to a HEALPix map; this is opaque but works\n",
Expand Down Expand Up @@ -287,12 +294,12 @@
"pars.min_l = 1\n",
"pars.set_for_lmax(lmax)\n",
"pars.SourceWindows = [\n",
" camb.sources.SplinedSourceWindow(z=z, W=dndz, source_type=\"lensing\")\n",
" camb.sources.SplinedSourceWindow(z=z, W=dndz, source_type=\"lensing\"),\n",
"]\n",
"theory_cls = camb.get_results(pars).get_source_cls_dict(lmax=lmax, raw_cl=True)\n",
"\n",
"# factor transforming convergence to shear\n",
"l = np.arange(lmax + 1)\n",
"l = np.arange(lmax + 1) # noqa: E741\n",
"fl = (l + 2) * (l + 1) * l * (l - 1) / np.clip(l**2 * (l + 1) ** 2, 1, None)\n",
"\n",
"# the noise level from discrete observations with shape noise\n",
Expand Down
30 changes: 20 additions & 10 deletions examples/2-advanced/stage_4_galaxies.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,23 @@
},
"outputs": [],
"source": [
"import numpy as np\n",
"import healpy as hp\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"# use the CAMB cosmology that generated the matter power spectra\n",
"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.ext.camb\n",
"import glass.fields\n",
"import glass.points\n",
"import glass.shapes\n",
"import glass.lensing\n",
"import glass.galaxies\n",
"import glass.lensing\n",
"import glass.observations\n",
"import glass.ext.camb\n",
"\n",
"import glass.points\n",
"import glass.shapes\n",
"import glass.shells\n",
"\n",
"# cosmology for the simulation\n",
"h = 0.7\n",
Expand All @@ -65,7 +64,10 @@
"\n",
"# set up CAMB parameters for matter angular power spectrum\n",
"pars = camb.set_params(\n",
" H0=100 * h, omch2=Oc * h**2, ombh2=Ob * h**2, NonLinear=camb.model.NonLinear_both\n",
" H0=100 * h,\n",
" omch2=Oc * h**2,\n",
" ombh2=Ob * h**2,\n",
" NonLinear=camb.model.NonLinear_both,\n",
")\n",
"\n",
"# get the cosmology from CAMB\n",
Expand Down Expand Up @@ -280,7 +282,10 @@
"\n",
" # generate galaxy positions from the matter density contrast\n",
" for gal_lon, gal_lat, gal_count in glass.points.positions_from_delta(\n",
" ngal[i], delta_i, bias, vis\n",
" ngal[i],\n",
" delta_i,\n",
" bias,\n",
" vis,\n",
" ):\n",
" # generate random redshifts over the given shell\n",
" gal_z = glass.galaxies.redshifts(gal_count, ws[i])\n",
Expand All @@ -296,7 +301,12 @@
"\n",
" # apply the shear fields to the ellipticities\n",
" gal_she = glass.galaxies.galaxy_shear(\n",
" gal_lon, gal_lat, gal_eps, kappa_i, gamm1_i, gamm2_i\n",
" gal_lon,\n",
" gal_lat,\n",
" gal_eps,\n",
" kappa_i,\n",
" gamm1_i,\n",
" gamm2_i,\n",
" )\n",
"\n",
" # make a mini-catalogue for the new rows\n",
Expand Down
8 changes: 5 additions & 3 deletions glass/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
try:
"""GLASS package."""

import contextlib

with contextlib.suppress(ModuleNotFoundError):
from ._version import __version__, __version_tuple__ # noqa: F401
except ModuleNotFoundError:
pass
4 changes: 2 additions & 2 deletions glass/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# author: Nicolas Tessore <n.tessore@ucl.ac.uk>
# license: MIT
"""
Core functions (:mod:`glass.core`)
Core functions (:mod:`glass.core`).
Saransh-cpp marked this conversation as resolved.
Show resolved Hide resolved
Saransh-cpp marked this conversation as resolved.
Show resolved Hide resolved
==================================

.. currentmodule:: glass.core

The :mod:`glass.core` module contains core functionality for developing
GLASS modules.

"""
""" # noqa: D205
Loading
Loading