Skip to content

Commit 9eef902

Browse files
authored
Merge pull request #245 from lpsinger/pre-commit
2 parents 03ce636 + b7af16c commit 9eef902

23 files changed

Lines changed: 795 additions & 482 deletions

.github/workflows/ci_tests.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ jobs:
2222
strategy:
2323
matrix:
2424
include:
25-
- name: Code style checks
26-
os: ubuntu-latest
27-
python: 3.x
28-
toxenv: codestyle
29-
3025
- name: Python 3.10 with minimal dependencies
3126
os: ubuntu-latest
3227
python: '3.10'

.pre-commit-config.yaml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
ci:
2+
autofix_prs: false
3+
autoupdate_schedule: 'monthly'
4+
5+
repos:
6+
- repo: https://github.com/pre-commit/pre-commit-hooks
7+
rev: v5.0.0
8+
hooks:
9+
- id: check-added-large-files
10+
args: ["--enforce-all", "--maxkb=300"]
11+
exclude: "^(\
12+
cextern/wcslib/C/flexed/.*|\
13+
CHANGES.rst|\
14+
)$"
15+
# Prevent giant files from being committed.
16+
- id: check-case-conflict
17+
# Check for files with names that would conflict on a case-insensitive
18+
# filesystem like MacOS HFS+ or Windows FAT.
19+
- id: check-json
20+
# Attempts to load all json files to verify syntax.
21+
- id: check-merge-conflict
22+
# Check for files that contain merge conflict strings.
23+
- id: check-symlinks
24+
# Checks for symlinks which do not point to anything.
25+
- id: check-toml
26+
# Attempts to load all TOML files to verify syntax.
27+
- id: check-xml
28+
# Attempts to load all xml files to verify syntax.
29+
- id: check-yaml
30+
# Attempts to load all yaml files to verify syntax.
31+
exclude: ".*(.github.*)$"
32+
- id: detect-private-key
33+
# Checks for the existence of private keys.
34+
- id: end-of-file-fixer
35+
# Makes sure files end in a newline and only a newline.
36+
exclude: ".*(data.*|extern.*|licenses.*|_static.*|_parsetab.py)$"
37+
# - id: fix-encoding-pragma # covered by pyupgrade
38+
- id: trailing-whitespace
39+
# Trims trailing whitespace.
40+
exclude_types: [python] # Covered by Ruff W291.
41+
exclude: ".*(data.*|extern.*|licenses.*|_static.*)$"
42+
43+
- repo: https://github.com/pre-commit/pygrep-hooks
44+
rev: v1.10.0
45+
hooks:
46+
- id: rst-directive-colons
47+
# Detect mistake of rst directive not ending with double colon.
48+
- id: rst-inline-touching-normal
49+
# Detect mistake of inline code touching normal text in rst.
50+
- id: text-unicode-replacement-char
51+
# Forbid files which have a UTF-8 Unicode replacement character.
52+
53+
- repo: https://github.com/codespell-project/codespell
54+
rev: v2.3.0
55+
hooks:
56+
- id: codespell
57+
args: ["--write-changes"]
58+
additional_dependencies:
59+
- tomli
60+
61+
- repo: https://github.com/astral-sh/ruff-pre-commit
62+
rev: v0.8.6
63+
hooks:
64+
- id: ruff
65+
args: ["--fix", "--show-fixes"]
66+
- id: ruff-format

astropy_healpix/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
try:
1111
from .version import version as __version__
1212
except ImportError:
13-
__version__ = ''
13+
__version__ = ""

astropy_healpix/bench.py

Lines changed: 122 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333

3434
# Copied from https://github.com/kwgoodman/bottleneck/blob/master/bottleneck/benchmark/autotimeit.py
35-
def autotimeit(stmt, setup='pass', repeat=3, mintime=0.2):
35+
def autotimeit(stmt, setup="pass", repeat=3, mintime=0.2):
3636
timer = timeit.Timer(stmt, setup)
3737
number, time1 = autoscaler(timer, mintime)
3838
time2 = timer.repeat(repeat=repeat - 1, number=number)
@@ -47,89 +47,105 @@ def autoscaler(timer, mintime):
4747
if time > mintime:
4848
return number, time
4949
number *= 10
50-
raise RuntimeError('function is too fast to test')
50+
raise RuntimeError("function is too fast to test")
5151

5252

5353
def get_import(package, function):
54-
if package == 'astropy_healpix':
55-
return f'from astropy_healpix.healpy import {function}'
54+
if package == "astropy_healpix":
55+
return f"from astropy_healpix.healpy import {function}"
5656
else:
57-
return f'from healpy import {function}'
57+
return f"from healpy import {function}"
5858

5959

6060
def bench_pix2ang(size=None, nside=None, nest=None, package=None, fast=False):
61-
setup = '\n'.join([
62-
get_import(package, 'pix2ang'),
63-
'import numpy as np',
64-
f'nside={nside}',
65-
f'ipix=(np.random.random({size}) * 12 * nside ** 2).astype(np.int64)',
66-
f'nest={nest}'])
67-
68-
stmt = 'pix2ang(nside, ipix, nest)'
61+
setup = "\n".join(
62+
[
63+
get_import(package, "pix2ang"),
64+
"import numpy as np",
65+
f"nside={nside}",
66+
f"ipix=(np.random.random({size}) * 12 * nside ** 2).astype(np.int64)",
67+
f"nest={nest}",
68+
]
69+
)
70+
71+
stmt = "pix2ang(nside, ipix, nest)"
6972

7073
return autotimeit(stmt=stmt, setup=setup, repeat=1, mintime=0 if fast else 0.1)
7174

7275

7376
def bench_ang2pix(size=None, nside=None, nest=None, package=None, fast=False):
74-
setup = '\n'.join([
75-
get_import(package, 'ang2pix'),
76-
'import numpy as np',
77-
f'nside={nside}',
78-
f'lon=360 * np.random.random({size})',
79-
f'lat=180 * np.random.random({size}) - 90',
80-
f'nest={nest}'])
81-
82-
stmt = 'ang2pix(nside, lon, lat, nest, lonlat=True)'
77+
setup = "\n".join(
78+
[
79+
get_import(package, "ang2pix"),
80+
"import numpy as np",
81+
f"nside={nside}",
82+
f"lon=360 * np.random.random({size})",
83+
f"lat=180 * np.random.random({size}) - 90",
84+
f"nest={nest}",
85+
]
86+
)
87+
88+
stmt = "ang2pix(nside, lon, lat, nest, lonlat=True)"
8389

8490
return autotimeit(stmt=stmt, setup=setup, repeat=1, mintime=0 if fast else 0.1)
8591

8692

8793
def bench_nest2ring(size=None, nside=None, package=None, fast=False):
88-
setup = '\n'.join([
89-
get_import(package, 'nest2ring'),
90-
'import numpy as np',
91-
f'nside={nside}',
92-
f'ipix=(np.random.random({size}) * 12 * nside ** 2).astype(np.int64)'])
94+
setup = "\n".join(
95+
[
96+
get_import(package, "nest2ring"),
97+
"import numpy as np",
98+
f"nside={nside}",
99+
f"ipix=(np.random.random({size}) * 12 * nside ** 2).astype(np.int64)",
100+
]
101+
)
93102

94-
stmt = 'nest2ring(nside, ipix)'
103+
stmt = "nest2ring(nside, ipix)"
95104

96105
return autotimeit(stmt=stmt, setup=setup, repeat=1, mintime=0 if fast else 0.1)
97106

98107

99108
def bench_ring2nest(size=None, nside=None, package=None, fast=False):
100-
setup = '\n'.join([
101-
get_import(package, 'ring2nest'),
102-
'import numpy as np',
103-
f'nside={nside}',
104-
f'ipix=(np.random.random({size}) * 12 * nside ** 2).astype(np.int64)'])
109+
setup = "\n".join(
110+
[
111+
get_import(package, "ring2nest"),
112+
"import numpy as np",
113+
f"nside={nside}",
114+
f"ipix=(np.random.random({size}) * 12 * nside ** 2).astype(np.int64)",
115+
]
116+
)
105117

106-
stmt = 'ring2nest(nside, ipix)'
118+
stmt = "ring2nest(nside, ipix)"
107119

108120
return autotimeit(stmt=stmt, setup=setup, repeat=1, mintime=0 if fast else 0.1)
109121

110122

111-
def bench_get_interp_weights(size=None, nside=None, nest=None, package=None, fast=False):
112-
setup = '\n'.join([
113-
get_import(package, 'get_interp_weights'),
114-
'import numpy as np',
115-
f'nside={nside}',
116-
f'lon=360 * np.random.random({size})',
117-
f'lat=180 * np.random.random({size}) - 90',
118-
f'nest={nest}'])
123+
def bench_get_interp_weights(
124+
size=None, nside=None, nest=None, package=None, fast=False
125+
):
126+
setup = "\n".join(
127+
[
128+
get_import(package, "get_interp_weights"),
129+
"import numpy as np",
130+
f"nside={nside}",
131+
f"lon=360 * np.random.random({size})",
132+
f"lat=180 * np.random.random({size}) - 90",
133+
f"nest={nest}",
134+
]
135+
)
119136

120-
stmt = 'get_interp_weights(nside, lon, lat, nest=nest, lonlat=True)'
137+
stmt = "get_interp_weights(nside, lon, lat, nest=nest, lonlat=True)"
121138

122139
return autotimeit(stmt=stmt, setup=setup, repeat=1, mintime=0 if fast else 0.1)
123140

124141

125142
def run_single(name, benchmark, fast=False, **kwargs):
126-
127-
time_self = benchmark(package='astropy_healpix', fast=fast, **kwargs)
143+
time_self = benchmark(package="astropy_healpix", fast=fast, **kwargs)
128144
results_single = dict(function=name, time_self=time_self, **kwargs)
129145

130146
if HEALPY_INSTALLED:
131-
time_healpy = bench_ang2pix(package='healpy', fast=fast, **kwargs)
132-
results_single['time_healpy'] = time_healpy
147+
time_healpy = bench_ang2pix(package="healpy", fast=fast, **kwargs)
148+
results_single["time_healpy"] = time_healpy
133149

134150
return results_single
135151

@@ -146,60 +162,99 @@ def bench_run(fast=False):
146162
for nest in [True, False]:
147163
for size in SIZES:
148164
for nside in [1, 128]:
149-
results.append(run_single('pix2ang', bench_pix2ang, fast=fast,
150-
size=size, nside=nside, nest=nest))
165+
results.append(
166+
run_single(
167+
"pix2ang",
168+
bench_pix2ang,
169+
fast=fast,
170+
size=size,
171+
nside=nside,
172+
nest=nest,
173+
)
174+
)
151175

152176
for nest in [True, False]:
153177
for size in SIZES:
154178
for nside in [1, 128]:
155-
results.append(run_single('ang2pix', bench_ang2pix, fast=fast,
156-
size=size, nside=nside, nest=nest))
179+
results.append(
180+
run_single(
181+
"ang2pix",
182+
bench_ang2pix,
183+
fast=fast,
184+
size=size,
185+
nside=nside,
186+
nest=nest,
187+
)
188+
)
157189

158190
for size in SIZES:
159191
for nside in [1, 128]:
160-
results.append(run_single('nest2ring', bench_nest2ring, fast=fast,
161-
size=size, nside=nside))
192+
results.append(
193+
run_single(
194+
"nest2ring", bench_nest2ring, fast=fast, size=size, nside=nside
195+
)
196+
)
162197

163198
for size in SIZES:
164199
for nside in [1, 128]:
165-
results.append(run_single('ring2nest', bench_ring2nest, fast=fast,
166-
size=size, nside=nside))
200+
results.append(
201+
run_single(
202+
"ring2nest", bench_ring2nest, fast=fast, size=size, nside=nside
203+
)
204+
)
167205

168206
for nest in [True, False]:
169207
for size in SIZES:
170208
for nside in [1, 128]:
171-
results.append(run_single('get_interp_weights', bench_get_interp_weights,
172-
fast=fast, size=size,
173-
nside=nside, nest=nest))
209+
results.append(
210+
run_single(
211+
"get_interp_weights",
212+
bench_get_interp_weights,
213+
fast=fast,
214+
size=size,
215+
nside=nside,
216+
nest=nest,
217+
)
218+
)
174219

175220
return results
176221

177222

178223
def bench_report(results):
179224
"""Print a report for given benchmark results to the console."""
180225

181-
table = Table(names=['function', 'nest', 'nside', 'size',
182-
'time_healpy', 'time_self', 'ratio'],
183-
dtype=['S20', bool, int, int, float, float, float], masked=True)
226+
table = Table(
227+
names=[
228+
"function",
229+
"nest",
230+
"nside",
231+
"size",
232+
"time_healpy",
233+
"time_self",
234+
"ratio",
235+
],
236+
dtype=["S20", bool, int, int, float, float, float],
237+
masked=True,
238+
)
184239
for row in results:
185240
table.add_row(row)
186241

187-
table['time_self'].format = '10.7f'
242+
table["time_self"].format = "10.7f"
188243

189244
if HEALPY_INSTALLED:
190-
table['ratio'] = table['time_self'] / table['time_healpy']
191-
table['time_healpy'].format = '10.7f'
192-
table['ratio'].format = '7.2f'
245+
table["ratio"] = table["time_self"] / table["time_healpy"]
246+
table["time_healpy"].format = "10.7f"
247+
table["ratio"].format = "7.2f"
193248

194249
table.pprint(max_lines=-1)
195250

196251

197252
def main(fast=False):
198253
"""Run all benchmarks and print report to the console."""
199-
print('Running benchmarks...\n')
254+
print("Running benchmarks...\n")
200255
results = bench_run(fast=fast)
201256
bench_report(results)
202257

203258

204-
if __name__ == '__main__':
259+
if __name__ == "__main__":
205260
main()

0 commit comments

Comments
 (0)