Skip to content

Commit

Permalink
maint: add pytest and numpy ruff rules
Browse files Browse the repository at this point in the history
this helps prepare the support of numpy 2.0
  • Loading branch information
ManonMarchand committed Sep 10, 2024
1 parent c7feb48 commit 88766df
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 77 deletions.
23 changes: 4 additions & 19 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,12 @@ repos:
exclude_types:
- "svg"
description: "Remove trailing whitespaces"
# Run Black on files
- repo: https://github.com/psf/black
rev: 24.4.2
hooks:
- id: black
# Fast linter to replace flake8
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: 'v0.5.5'
rev: 'v0.6.4'
hooks:
# linter
- id: ruff
fail_fast: true
# Same tools but for Jupyter notebooks
- repo: https://github.com/nbQA-dev/nbQA
rev: 1.8.5
hooks:
- id: nbqa-black
name: nbqa-black
description: "Run 'black' on a Jupyter Notebook"
language: python
require_serial: true
- id: nbqa-ruff
name: nbqa-ruff
description: "Run ruff on a Jupyter notebook"
fail_fast: true
# formatter
- id: ruff-format
27 changes: 14 additions & 13 deletions notebooks/01-Creating_MOCs_from_shapes.ipynb

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions notebooks/compute_moc_borders.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion notebooks/from_astropy_table.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
")\n",
"moc.border(ax=ax, wcs=galactic_wcs, color=\"k\")\n",
"\n",
"ax.grid(True)"
"ax.grid(visible=True)"
]
}
],
Expand Down
4 changes: 2 additions & 2 deletions notebooks/tmoc.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
"metadata": {},
"outputs": [],
"source": [
"from mocpy import TimeMOC\n",
"from astropy.time import Time, TimeDelta\n",
"from astroquery.vizier import Vizier"
"from astroquery.vizier import Vizier\n",
"from mocpy import TimeMOC"
]
},
{
Expand Down
10 changes: 3 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ frozen = false
# Require Cargo.lock is up to date
locked = false

[tool.nbqa.addopts]
ruff = ["--ignore=B018,FBT003,T201"]

[tool.ruff]
fix = true
show-fixes = true
Expand All @@ -94,14 +91,13 @@ extend-include = ["*.ipynb"]
target-version = "py38"

[tool.ruff.lint]
ignore-init-module-imports = true
extend-select = ["SIM", "FBT", "D", "UP", "N", "S", "B", "A",
"C4", "ICN", "RET", "ARG", "PD", "PGH",
"RUF", "YTT", "BLE", "COM", "DTZ", "EXE",
"ISC", "ICN001", "PIE", "PTH", "W", "I", "T20"
"ISC", "ICN001", "PIE", "PTH", "W", "I", "T20",
"PYI", "PT", "SIM", "NPY"
]
extend-ignore = ["E501", "D203", "D213", "D100", "N816", "D105", "N806", "N803", "FBT002", "N802"]
# E501: line length (done by black in our case)
extend-ignore = ["D203", "D213", "D100", "N816", "D105", "N806", "N803", "FBT002", "N802"]
# N806, N816: variables names should be lowercase (would require to change all API)
# D203: 1 blank line required before class docstring (not compatible with numpy docstyle)
# FBT002: Boolean default in function definition (would require to change all API)
Expand Down
6 changes: 3 additions & 3 deletions python/mocpy/tests/test_abstract_moc.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def moc():
return MOC.from_str("0/0-11")


@pytest.fixture()
@pytest.fixture
def path(tmp_path):
return tmp_path / "path"

Expand All @@ -50,10 +50,10 @@ def test_failing_instantiation():

def test_failing_save(moc, path):
"""All the pitfalls in the save method."""
moc.save(path)
assert path.is_file()
with pytest.raises(OSError, match=r"File *"):
moc.save(path)
assert path.is_file()
moc.save(path)
with pytest.raises(ValueError, match=r"The ``fold`` argument*"):
moc.save(path, format="fits", overwrite=True, fold=80)
with pytest.raises(ValueError, match=r"The ``fits_keyword`` argument*"):
Expand Down
45 changes: 24 additions & 21 deletions python/mocpy/tests/test_moc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

from ..moc import MOC, WCS

rng = np.random.default_rng()

@pytest.fixture()

@pytest.fixture
def isets():
a = MOC.from_depth29_ranges(
29,
Expand Down Expand Up @@ -106,6 +108,10 @@ def test_n_cells():
match=f"The depth should be comprised between 0 and {MOC.MAX_ORDER}*",
):
MOC.n_cells(-2)
with pytest.raises(
ValueError,
match=f"The depth should be comprised between 0 and {MOC.MAX_ORDER}*",
):
MOC.n_cells(MOC.MAX_ORDER + 1)
assert MOC.n_cells(6) == 4 * MOC.n_cells(5)

Expand Down Expand Up @@ -207,31 +213,28 @@ def test_new_empty_serialization():

def get_random_skycoords(size):
return SkyCoord(
ra=np.random.uniform(0, 360, size),
dec=np.random.uniform(-90, 90, size),
ra=rng.random(size) * 360,
dec=rng.random(size) * 180 - 90,
unit="deg",
)


@pytest.fixture()
@pytest.fixture
def skycoords_gen_f():
def gen_f(size):
return SkyCoord(
np.random.uniform(0, 360, size),
np.random.uniform(-90, 90, size),
ra=rng.random(size) * 360,
dec=rng.random(size) * 180 - 90,
unit="deg",
)

return gen_f


@pytest.fixture()
@pytest.fixture
def lonlat_gen_f():
def gen_f(size):
return (
np.random.uniform(0, 360, size) * u.deg,
np.random.uniform(-90, 90, size) * u.deg,
)
return ((rng.random(size) * 360) * u.deg, (rng.random(size) * 180 - 90) * u.deg)

return gen_f

Expand Down Expand Up @@ -356,15 +359,15 @@ def test_from_fits_image_without_cdelt():
MOC.from_fits_images(["resources/horsehead.fits"], max_norder=15)


@pytest.fixture()
@pytest.fixture
def moc_from_fits_image():
image_path = "resources/image_with_mask.fits.gz"

with fits.open(image_path) as hdulist:
return MOC.from_fits_image(hdu=hdulist[0], max_norder=7, mask=hdulist[0].data)


@pytest.fixture()
@pytest.fixture
def moc_from_json():
return MOC.from_json({"8": [45, 78], "4": [42, 57]})

Expand All @@ -380,7 +383,7 @@ def test_moc_serialize_and_from_json(moc_from_json):


@pytest.mark.parametrize(
"expected, moc_str",
("expected", "moc_str"),
[
(
MOC.from_json(
Expand All @@ -406,7 +409,7 @@ def test_from_str(expected, moc_str):


@pytest.mark.parametrize(
"expected, moc_str",
("expected", "moc_str"),
[
(
MOC.from_json(
Expand Down Expand Up @@ -458,7 +461,7 @@ def test_moc_serialize_to_json(moc_from_fits_image):


@pytest.mark.parametrize(
"moc, expected",
("moc", "expected"),
[
(
MOC.from_json(
Expand Down Expand Up @@ -526,7 +529,7 @@ def test_mpl_border():
def test_moc_contains(order):
# defines 20 random healpix cells of the required order
size = 20
healpix_arr = np.random.randint(0, 12 * 4**order, size, dtype="uint64")
healpix_arr = rng.integers(0, 12 * 4**order, size, dtype="uint64", endpoint=True)
# defines a moc containing the 20 points
moc = MOC.from_json(json_moc={str(order): np.unique(healpix_arr).tolist()})
# the complementary should not contain them
Expand Down Expand Up @@ -773,7 +776,7 @@ def test_from_cones():
MOC.from_cones(lon, lat, radius=radii, max_depth=14, union_strategy="big_cones")


@pytest.fixture()
@pytest.fixture
def mocs():
moc1 = {"1": [0]}
moc1_increased = {"0": [0], "1": [17, 19, 22, 23, 35]}
Expand Down Expand Up @@ -812,7 +815,7 @@ def test_neighbours(mocs):


# --- TESTING MOC operations ---#
@pytest.fixture()
@pytest.fixture
def mocs_op():
moc1 = MOC.from_json({"0": [0, 2, 3, 4, 5]})
moc2 = MOC.from_json({"0": [0, 1, 7, 4, 3]})
Expand Down Expand Up @@ -862,7 +865,7 @@ def test_from_fits_old():


@pytest.mark.parametrize(
"input_MOC, expected",
("input_MOC", "expected"),
[
(
MOC.from_json({"0": [1, 3]}),
Expand Down Expand Up @@ -931,7 +934,7 @@ def test_from_valued_healpix_cells():


@pytest.mark.parametrize(
"cumul_from, cumul_to",
("cumul_from", "cumul_to"),
[(-5.0, 1.0), (np.nan, np.inf), (np.nan, np.nan), (np.inf, np.nan), (-10.0, -5.0)],
)
def test_from_valued_healpix_cells_weird_values(cumul_from, cumul_to):
Expand Down
4 changes: 2 additions & 2 deletions python/mocpy/tests/test_stmoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ..tmoc import TimeMOC


@pytest.fixture()
@pytest.fixture
def stmoc_2mass():
two_mass_data = Table.read(
"resources/STMOC/2MASS-list-images.fits.gz",
Expand All @@ -33,7 +33,7 @@ def stmoc_2mass():
)


@pytest.fixture()
@pytest.fixture
def stmoc_xmm_dr8():
xmm_dr8_data = Table.read("resources/STMOC/vizier_votable.b64")
times_xmm = Time(xmm_dr8_data["MJD0"].data, format="mjd", scale="tdb")
Expand Down
15 changes: 11 additions & 4 deletions python/mocpy/tests/test_tmoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def test_n_cells():
match=f"The depth should be comprised between 0 and {TimeMOC.MAX_ORDER}*",
):
TimeMOC.n_cells(-1)
with pytest.raises(
ValueError,
match=f"The depth should be comprised between 0 and {TimeMOC.MAX_ORDER}*",
):
TimeMOC.n_cells(TimeMOC.MAX_ORDER + 1)
assert TimeMOC.n_cells(10) == 2 * TimeMOC.n_cells(9)

Expand Down Expand Up @@ -134,7 +138,10 @@ def test_tmoc_from_time_ranges():
assert tmoc.max_time == tmoc2.max_time
assert tmoc == tmoc2

with pytest.raises(ValueError):
with pytest.raises(
ValueError,
match=r"Mismatch between min\_times and max\_times of shapes \(0,\) and \(1,\)",
):
TimeMOC.from_time_ranges(
Time([], format="jd", scale="tdb"),
Time([3], format="jd", scale="tdb"),
Expand Down Expand Up @@ -234,7 +241,7 @@ def test_contains():


@pytest.mark.parametrize(
"a, b, expect",
("a", "b", "expect"),
[
(
TimeMOC.from_times(Time(np.array([0, 2]), format="jd", scale="tdb")),
Expand All @@ -260,7 +267,7 @@ def test_union(a, b, expect):


@pytest.mark.parametrize(
"a, b, expect",
("a", "b", "expect"),
[
(
TimeMOC.from_times(Time(np.array([0, 2]), format="jd", scale="tdb")),
Expand All @@ -286,7 +293,7 @@ def test_difference(a, b, expect):


@pytest.mark.parametrize(
"a, b, expect",
("a", "b", "expect"),
[
(
TimeMOC.from_times(Time(np.array([0, 1, 2]), format="jd", scale="tdb")),
Expand Down

0 comments on commit 88766df

Please sign in to comment.