Skip to content

Commit

Permalink
STY: add ruff-format and remove flake8 (#199)
Browse files Browse the repository at this point in the history
The maintainer of `flake8` can be a bit difficult, so nice to remove. I
will add `ruff` the linter in a separate PR, but would be nice to get
formatting done first. `ruff-format` is basically faster `black` (but
some subtle differences).

Reviewed-by: Saransh Chopra <[email protected]>
  • Loading branch information
paddyroddy authored Sep 17, 2024
1 parent ee04324 commit b42067f
Show file tree
Hide file tree
Showing 34 changed files with 793 additions and 641 deletions.
2 changes: 0 additions & 2 deletions .flake8

This file was deleted.

2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# applied ruff-format - https://github.com/glass-dev/glass/pull/199
c894d9c7d349e44ac7fd50ba35859695758bbb95
13 changes: 7 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ concurrency:

jobs:

style:
name: Style
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: pipx run flake8 glass
# TODO: replace with a more general pre-commit linter
# style:
# name: Style
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v4
# - run: pipx run flake8 glass

tests:
name: Tests
Expand Down
28 changes: 13 additions & 15 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
repos:
- repo: https://github.com/pycqa/flake8
rev: 7.1.1
hooks:
- id: flake8
additional_dependencies:
- flake8-print
- repo: https://github.com/pappasam/toml-sort
rev: v0.23.1
hooks:
- id: toml-sort-fix
args:
- --all
- --in-place
- --spaces-indent-inline-array=4
- --trailing-comma-inline-array
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.3
hooks:
- id: ruff-format
- repo: https://github.com/pappasam/toml-sort
rev: v0.23.1
hooks:
- id: toml-sort-fix
args:
- --all
- --in-place
- --spaces-indent-inline-array=4
- --trailing-comma-inline-array
42 changes: 21 additions & 21 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import datetime
from importlib import metadata

project = 'GLASS'
author = 'GLASS developers'
copyright = f'2022-{datetime.date.today().year} {author}'
project = "GLASS"
author = "GLASS developers"
copyright = f"2022-{datetime.date.today().year} {author}"
version = metadata.version("glass")
release = version

Expand All @@ -23,37 +23,37 @@
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'numpydoc',
'sphinx.ext.intersphinx',
'sphinxcontrib.katex',
'matplotlib.sphinxext.plot_directive',
'nbsphinx',
"numpydoc",
"sphinx.ext.intersphinx",
"sphinxcontrib.katex",
"matplotlib.sphinxext.plot_directive",
"nbsphinx",
"sphinx.ext.viewcode",
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
templates_path = ["_templates"]

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'furo'
html_theme = "furo"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
html_static_path = ["_static"]

html_logo = '_static/logo.png'
html_favicon = '_static/favicon.ico'
html_logo = "_static/logo.png"
html_favicon = "_static/favicon.ico"
html_css_files = []


Expand All @@ -62,14 +62,14 @@
# This config value contains the locations and names of other projects that
# should be linked to in this documentation.
intersphinx_mapping = {
'python': ('https://docs.python.org/3', None),
'numpy': ('https://numpy.org/doc/stable/', None),
"python": ("https://docs.python.org/3", None),
"numpy": ("https://numpy.org/doc/stable/", None),
}


# -- autodoc -----------------------------------------------------------------

autodoc_typehints = 'none'
autodoc_typehints = "none"


# -- numpydoc ----------------------------------------------------------------
Expand All @@ -89,12 +89,12 @@
plot_html_show_formats = False

# File formats to generate (default: ['png', 'hires.png', 'pdf']).
plot_formats = [('svg', 150), ('png', 150)]
plot_formats = [("svg", 150), ("png", 150)]

# A dictionary containing any non-standard rcParams that should be applied
# before each plot (default: {}).
plot_rcparams = {
'axes.facecolor': (1.0, 1.0, 1.0, 1.0),
'savefig.facecolor': (1.0, 1.0, 1.0, 0.5),
'savefig.transparent': False,
"axes.facecolor": (1.0, 1.0, 1.0, 1.0),
"savefig.facecolor": (1.0, 1.0, 1.0, 0.5),
"savefig.transparent": False,
}
50 changes: 31 additions & 19 deletions examples/1-basic/density.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,21 @@
"nside = lmax = 128\n",
"\n",
"# set up CAMB parameters for matter angular power spectrum\n",
"pars = camb.set_params(H0=100*h, omch2=Oc*h**2, ombh2=Ob*h**2,\n",
" NonLinear=camb.model.NonLinear_both)\n",
"pars = camb.set_params(\n",
" H0=100 * h, omch2=Oc * h**2, ombh2=Ob * h**2, NonLinear=camb.model.NonLinear_both\n",
")\n",
"\n",
"# get the cosmology from CAMB\n",
"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.)\n",
"zb = glass.shells.distance_grid(cosmo, 0.0, 1.0, dx=200.0)\n",
"\n",
"# linear radial window functions\n",
"ws = glass.shells.linear_windows(zb)\n",
"\n",
"# load the angular matter power spectra previously computed with CAMB\n",
"cls = np.load('cls.npy')"
"cls = np.load(\"cls.npy\")"
]
},
{
Expand Down Expand Up @@ -124,7 +125,7 @@
"outputs": [],
"source": [
"# constant galaxy density distribution\n",
"z = np.linspace(0., 1., 100)\n",
"z = np.linspace(0.0, 1.0, 100)\n",
"dndz = np.full_like(z, 0.01)\n",
"\n",
"# distribute the dN/dz over the linear window functions\n",
Expand Down Expand Up @@ -158,22 +159,24 @@
"source": [
"# make a cube for galaxy number in redshift\n",
"zcub = np.linspace(-zb[-1], zb[-1], 21)\n",
"cube = np.zeros((zcub.size-1,)*3)\n",
"cube = np.zeros((zcub.size - 1,) * 3)\n",
"\n",
"# simulate and add galaxies in each matter shell to cube\n",
"for i, delta_i in enumerate(matter):\n",
"\n",
" # simulate positions from matter density\n",
" for gal_lon, gal_lat, gal_count in glass.points.positions_from_delta(ngal[i], delta_i):\n",
"\n",
" for gal_lon, gal_lat, gal_count in glass.points.positions_from_delta(\n",
" ngal[i], delta_i\n",
" ):\n",
" # sample redshifts uniformly in shell\n",
" gal_z = glass.galaxies.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",
" 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(np.searchsorted(zcub[1:], [z1, z2, z3]), axis=1, return_counts=True)\n",
" 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",
" )\n",
" cube[i, j, k] += c"
]
},
Expand Down Expand Up @@ -217,19 +220,28 @@
],
"source": [
"# positions of grid cells of the cube\n",
"z = (zcub[:-1] + zcub[1:])/2\n",
"z = (zcub[:-1] + zcub[1:]) / 2\n",
"z1, z2, z3 = np.meshgrid(z, z, z)\n",
"\n",
"# plot the galaxy distribution in pseudo-3D\n",
"fig = plt.figure()\n",
"ax = fig.add_subplot(111, projection='3d', proj_type='ortho')\n",
"ax = fig.add_subplot(111, projection=\"3d\", proj_type=\"ortho\")\n",
"norm = LogNorm(vmin=np.min(cube[cube > 0]), vmax=np.max(cube), clip=True)\n",
"for i in range(len(zcub)-1):\n",
"for i in range(len(zcub) - 1):\n",
" v = norm(cube[..., i])\n",
" c = plt.cm.inferno(v)\n",
" c[..., -1] = 0.2*v\n",
" ax.plot_surface(z1[..., i], z2[..., i], z3[..., i], rstride=1, cstride=1,\n",
" facecolors=c, linewidth=0, shade=False, antialiased=False)\n",
" c[..., -1] = 0.2 * v\n",
" ax.plot_surface(\n",
" z1[..., i],\n",
" z2[..., i],\n",
" z3[..., i],\n",
" rstride=1,\n",
" cstride=1,\n",
" facecolors=c,\n",
" linewidth=0,\n",
" shade=False,\n",
" antialiased=False,\n",
" )\n",
"fig.tight_layout()\n",
"plt.show()"
]
Expand Down
41 changes: 22 additions & 19 deletions examples/1-basic/lensing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,21 @@
"nside = lmax = 256\n",
"\n",
"# set up CAMB parameters for matter angular power spectrum\n",
"pars = camb.set_params(H0=100*h, omch2=Oc*h**2, ombh2=Ob*h**2,\n",
" NonLinear=camb.model.NonLinear_both)\n",
"pars = camb.set_params(\n",
" H0=100 * h, omch2=Oc * h**2, ombh2=Ob * h**2, NonLinear=camb.model.NonLinear_both\n",
")\n",
"\n",
"# get the cosmology from CAMB\n",
"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.)\n",
"zb = glass.shells.distance_grid(cosmo, 0.0, 1.0, dx=200.0)\n",
"\n",
"# linear radial window functions\n",
"ws = glass.shells.linear_windows(zb)\n",
"\n",
"# load the angular matter power spectra previously computed with CAMB\n",
"cls = np.load('cls.npy')"
"cls = np.load(\"cls.npy\")"
]
},
{
Expand Down Expand Up @@ -156,7 +157,7 @@
"# localised redshift distribution\n",
"# the actual density per arcmin2 does not matter here, it is never used\n",
"z = np.linspace(0.0, 1.0, 101)\n",
"dndz = np.exp(-(z - 0.5)**2/(0.1)**2)\n",
"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)"
Expand Down Expand Up @@ -186,13 +187,12 @@
"outputs": [],
"source": [
"# the integrated convergence and shear field over the redshift distribution\n",
"kappa_bar = np.zeros(12*nside**2)\n",
"gamm1_bar = np.zeros(12*nside**2)\n",
"gamm2_bar = np.zeros(12*nside**2)\n",
"kappa_bar = np.zeros(12 * nside**2)\n",
"gamm1_bar = np.zeros(12 * nside**2)\n",
"gamm2_bar = np.zeros(12 * nside**2)\n",
"\n",
"# main loop to simulate the matter fields iterative\n",
"for i, delta_i in enumerate(matter):\n",
"\n",
" # add lensing plane from the window function of this shell\n",
" convergence.add_window(delta_i, ws[i])\n",
"\n",
Expand Down Expand Up @@ -255,26 +255,29 @@
],
"source": [
"# get the angular power spectra of the lensing maps\n",
"sim_cls = hp.anafast([kappa_bar, gamm1_bar, gamm2_bar],\n",
" pol=True, lmax=lmax, use_pixel_weights=True)\n",
"sim_cls = hp.anafast(\n",
" [kappa_bar, gamm1_bar, gamm2_bar], pol=True, lmax=lmax, 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 = [camb.sources.SplinedSourceWindow(z=z, W=dndz, source_type='lensing')]\n",
"pars.SourceWindows = [\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",
"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",
"plt.yscale('symlog', linthresh=1e-9, linscale=0.5, subs=[2, 3, 4, 5, 6, 7, 8, 9])\n",
"plt.xlabel(r'angular mode number $l$')\n",
"plt.ylabel(r'angular power spectrum $C_l^{\\kappa\\kappa}$')\n",
"l = np.arange(lmax + 1)\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",
"plt.yscale(\"symlog\", linthresh=1e-9, linscale=0.5, subs=[2, 3, 4, 5, 6, 7, 8, 9])\n",
"plt.xlabel(r\"angular mode number $l$\")\n",
"plt.ylabel(r\"angular power spectrum $C_l^{\\kappa\\kappa}$\")\n",
"plt.legend(frameon=False)\n",
"plt.show()"
]
Expand Down
Loading

0 comments on commit b42067f

Please sign in to comment.