Skip to content

Commit

Permalink
Merge branch 'main' into saransh/test_fields
Browse files Browse the repository at this point in the history
  • Loading branch information
paddyroddy authored Nov 7, 2024
2 parents e334b34 + 42e5a10 commit 1d343cd
Show file tree
Hide file tree
Showing 17 changed files with 222 additions and 436 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,14 @@ jobs:
run: nox -s examples
env:
FORCE_COLOR: 1

- name: Generate HTML
run: nox -s examples -- html
env:
FORCE_COLOR: 1

- name: Upload artefact
uses: actions/upload-artifact@v4
with:
name: Examples
path: examples/**/*.html
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ dist
.env
.coverage*
coverage*
*.html
.ipynb_checkpoints
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,9 @@ repos:
files: ^glass/
additional_dependencies:
- numpy
- repo: https://github.com/kynan/nbstripout
rev: 0.7.1
hooks:
- id: nbstripout
args:
- --keep-output
90 changes: 26 additions & 64 deletions examples/1-basic/density.ipynb

Large diffs are not rendered by default.

96 changes: 23 additions & 73 deletions examples/1-basic/lensing.ipynb

Large diffs are not rendered by default.

42 changes: 12 additions & 30 deletions examples/1-basic/matter.ipynb

Large diffs are not rendered by default.

68 changes: 14 additions & 54 deletions examples/1-basic/photoz.ipynb

Large diffs are not rendered by default.

37 changes: 10 additions & 27 deletions examples/1-basic/shells.ipynb

Large diffs are not rendered by default.

96 changes: 26 additions & 70 deletions examples/2-advanced/cosmic_shear.ipynb

Large diffs are not rendered by default.

120 changes: 29 additions & 91 deletions examples/2-advanced/stage_4_galaxies.ipynb

Large diffs are not rendered by default.

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
29 changes: 22 additions & 7 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,29 @@ def doctests(session: nox.Session) -> None:

@nox.session
def examples(session: nox.Session) -> None:
"""Run the example notebooks."""
"""Run the example notebooks. Pass "html" to build html."""
session.install("-e", ".[examples]")
session.run(
"jupyter",
"execute",
*Path().glob("examples/**/*.ipynb"),
*session.posargs,
)

if session.posargs:
if "html" in session.posargs:
print("Generating HTML for the example notebooks")
session.run(
"jupyter",
"nbconvert",
"--to",
"html",
"--embed-images",
"examples/**/*.ipynb",
)
else:
print("Unsupported argument to examples")
else:
session.run(
"jupyter",
"execute",
*Path().glob("examples/**/*.ipynb"),
*session.posargs,
)


@nox.session
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 1d343cd

Please sign in to comment.