Skip to content

Commit

Permalink
gh-248: fix all remaining possible ruff # noqa statements (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
paddyroddy authored Nov 7, 2024
1 parent 74c5ac8 commit 9075c79
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 31 deletions.
4 changes: 2 additions & 2 deletions examples/1-basic/density.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@
" 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( # noqa: PLW2901\n",
" indices, count = np.unique(\n",
" np.searchsorted(zcub[1:], [z1, z2, z3]),\n",
" axis=1,\n",
" return_counts=True,\n",
" )\n",
" cube[i, j, k] += c"
" cube[*indices] += count"
]
},
{
Expand Down
6 changes: 3 additions & 3 deletions examples/1-basic/lensing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,9 @@
"pw = hp.pixwin(nside, lmax=lmax)\n",
"\n",
"# plot the realised and expected cls\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",
"ell = np.arange(lmax + 1)\n",
"plt.plot(ell, sim_cls[0], \"-k\", lw=2, label=\"simulation\")\n",
"plt.plot(ell, 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",
Expand Down
13 changes: 7 additions & 6 deletions examples/2-advanced/cosmic_shear.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -297,22 +297,23 @@
"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) # noqa: E741\n",
"fl = (l + 2) * (l + 1) * l * (l - 1) / np.clip(l**2 * (l + 1) ** 2, 1, None)\n",
"ell = np.arange(lmax + 1)\n",
"fl = (ell + 2) * (ell + 1) * ell * (ell - 1) / np.clip(ell**2 * (ell + 1) ** 2, 1, None)\n",
"\n",
"# the noise level from discrete observations with shape noise\n",
"nl = 4 * np.pi / (nbar * npix) * sigma_e**2 * (l >= 2)\n",
"ntot = nbar * npix\n",
"nl = 4 * np.pi / ntot * sigma_e**2 * (ell >= 2)\n",
"\n",
"# mixing matrix for uniform distribution of points\n",
"mm = (1 - 1 / (nbar * npix)) * np.eye(lmax + 1, lmax + 1) + (l + 1 / 2) / (nbar * npix)\n",
"mm = (1 - 1 / ntot) * np.eye(lmax + 1, lmax + 1) + (ell + 1 / 2) / ntot\n",
"mm[:2, :] = mm[:, :2] = 0\n",
"\n",
"# the shear pixel window function for HEALPix\n",
"_, pw = hp.pixwin(nside, lmax=lmax, pol=True)\n",
"\n",
"# plot the realised and expected cls\n",
"plt.plot(l, cls[1] - nl, \"-k\", lw=2, label=\"simulation\")\n",
"plt.plot(l, pw**2 * mm @ (fl * theory_cls[\"W1xW1\"]), \"-r\", lw=2, label=\"expectation\")\n",
"plt.plot(ell, cls[1] - nl, \"-k\", lw=2, label=\"simulation\")\n",
"plt.plot(ell, pw**2 * mm @ (fl * theory_cls[\"W1xW1\"]), \"-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(\"angular mode number $l$\")\n",
Expand Down
17 changes: 8 additions & 9 deletions glass/lensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@ def from_convergence( # noqa: PLR0913
alm = hp.map2alm(kappa, lmax=lmax, pol=False, use_pixel_weights=True)

# mode number; all conversions are factors of this
l = np.arange(lmax + 1) # noqa: E741
ell = np.arange(lmax + 1)

# this tuple will be returned
results: tuple[npt.NDArray[np.float64], ...] = ()

# convert convergence to potential
fl = np.divide(-2, l * (l + 1), where=(l > 0), out=np.zeros(lmax + 1))
fl = np.divide(-2, ell * (ell + 1), where=(ell > 0), out=np.zeros(lmax + 1))
hp.almxfl(alm, fl, inplace=True)

# if potential is requested, compute map and add to output
Expand All @@ -190,9 +190,8 @@ def from_convergence( # noqa: PLR0913
blm = np.zeros_like(alm)

# compute deflection alms in place
fl = np.sqrt(l * (l + 1))
# TODO(ntessore): missing spin-1 pixel window function here # noqa: FIX002
# https://github.com/glass-dev/glass/issues/243
fl = np.sqrt(ell * (ell + 1))
# missing spin-1 pixel window function here
hp.almxfl(alm, fl, inplace=True)

# if deflection is requested, compute spin-1 maps and add to output
Expand All @@ -207,7 +206,7 @@ def from_convergence( # noqa: PLR0913

# compute shear alms in place
# if discretised, factor out spin-0 kernel and apply spin-2 kernel
fl = np.sqrt((l - 1) * (l + 2), where=(l > 0), out=np.zeros(lmax + 1))
fl = np.sqrt((ell - 1) * (ell + 2), where=(ell > 0), out=np.zeros(lmax + 1))
fl /= 2
if discretized:
pw0, pw2 = hp.pixwin(nside, lmax=lmax, pol=True)
Expand Down Expand Up @@ -250,9 +249,9 @@ def shear_from_convergence(
blm = np.zeros_like(alm)

# factor to convert convergence alm to shear alm
l = np.arange(lmax + 1) # noqa: E741
fl = np.sqrt((l + 2) * (l + 1) * l * (l - 1))
fl /= np.clip(l * (l + 1), 1, None)
ell = np.arange(lmax + 1)
fl = np.sqrt((ell + 2) * (ell + 1) * ell * (ell - 1))
fl /= np.clip(ell * (ell + 1), 1, None)
fl *= -1

# if discretised, factor out spin-0 kernel and apply spin-2 kernel
Expand Down
1 change: 0 additions & 1 deletion glass/observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
.. autofunction:: vmap_galactic_ecliptic
""" # noqa: D205, D400

from __future__ import annotations
Expand Down
8 changes: 4 additions & 4 deletions glass/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ def triaxial_axis_ratio(
x2 = np.square(xi)

# eq. (11) multiplied by xi^2 zeta^2
A = (1 + z2m1 * sin2_phi) * cos2_theta + x2 * sin2_theta # noqa: N806
B2 = 4 * z2m1**2 * cos2_theta * sin2_phi * cos2_phi # noqa: N806
C = 1 + z2m1 * cos2_phi # noqa: N806
a = (1 + z2m1 * sin2_phi) * cos2_theta + x2 * sin2_theta
b2 = 4 * z2m1**2 * cos2_theta * sin2_phi * cos2_phi
c = 1 + z2m1 * cos2_phi

# eq. (12)
return np.sqrt( # type: ignore[no-any-return]
(A + C - np.sqrt((A - C) ** 2 + B2)) / (A + C + np.sqrt((A - C) ** 2 + B2)),
(a + c - np.sqrt((a - c) ** 2 + b2)) / (a + c + np.sqrt((a - c) ** 2 + b2)),
)


Expand Down
1 change: 1 addition & 0 deletions glass/shells.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
.. autofunction:: distance_weight
.. autofunction:: volume_weight
.. autofunction:: density_weight
""" # noqa: D205, D400

from __future__ import annotations
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ show-fixes = true
src = [
"glass",
]
lint.allowed-confusables = [
"α",
"γ",
]
lint.ignore = [
"COM812", # missing-trailing-comma (ruff-format recommended)
"D203", # one-blank-line-before-class
Expand Down
31 changes: 25 additions & 6 deletions tests/test_fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,32 +60,51 @@ def _test_append(

@pytest.mark.skipif(not HAVE_FITSIO, reason="test requires fitsio")
def test_write_exception(tmp_path: pathlib.Path) -> None:
class TestWriteError(Exception):
"""Custom exception for controlled testing."""

def raise_error(msg: str) -> None:
"""Raise a custom exception for controlled testing.
Parameters
----------
msg
A message to be passed to the exception.
Raises
------
TestWriteError
A custom exception for controlled testing.
"""
raise TestWriteError(msg)

try:
with user.write_catalog(tmp_path / filename, ext="CATALOG") as out:
for i in range(my_max):
if i == except_int:
msg = "Unhandled exception"
raise Exception(msg) # noqa: TRY002, TRY301
raise_error(msg)
array = np.arange(i, i + 1, delta) # array of size 1/delta
array2 = np.arange(i + 1, i + 2, delta) # array of size 1/delta
out.write(RA=array, RB=array2)

except Exception: # noqa: BLE001
except TestWriteError:
import fitsio

with fitsio.FITS(tmp_path / filename) as hdul:
data = hdul[1].read()
assert data["RA"].size == except_int / delta
assert data["RB"].size == except_int / delta

fitsMat = data["RA"].reshape(except_int, int(1 / delta)) # noqa: N806
fitsMat2 = data["RB"].reshape(except_int, int(1 / delta)) # noqa: N806
fits_mat = data["RA"].reshape(except_int, int(1 / delta))
fits_mat2 = data["RB"].reshape(except_int, int(1 / delta))
for i in range(except_int):
array = np.arange(
i,
i + 1,
delta,
) # re-create array to compare to read data
array2 = np.arange(i + 1, i + 2, delta)
assert array.tolist() == fitsMat[i].tolist()
assert array2.tolist() == fitsMat2[i].tolist()
assert array.tolist() == fits_mat[i].tolist()
assert array2.tolist() == fits_mat2[i].tolist()

0 comments on commit 9075c79

Please sign in to comment.