Skip to content

Commit 44b3cc0

Browse files
committed
Update ruff configuration so that it runs on all files in the repository.
1 parent 50efecf commit 44b3cc0

12 files changed

+78
-113
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ jobs:
3232
source $VENV
3333
mypy ./zodipy
3434
35-
- name: Lint with ruff
35+
- name: Check linting with ruff
3636
run: |
3737
source $VENV
38-
ruff ./zodipy
38+
ruff check
3939
4040
- name: Check formating with ruff
4141
run: |
4242
source $VENV
43-
ruff format --check ./zodipy
43+
ruff format --check
4444
4545
- name: Test with pytest
4646
run: |

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Note that developers using Python 3.12 will need to upgrade their pip versions w
100100
The following tools should be run from the root of the repository with no errors. (These are ran automatically as part of the CI workflows on GitHub, but should be tested locally first)
101101

102102
- [pytest](https://docs.pytest.org/en/8.0.x/): Tests are run with pytest by simply running `pytest` in the command line in the root of the repository.
103-
- [ruff](https://github.com/astral-sh/ruff): Formating and linting is done with `ruff` by simply running `ruff check zodipy/` and `ruff format zodipy/` in the command line in the root of the repository.
103+
- [ruff](https://github.com/astral-sh/ruff): Formating and linting is done with `ruff` by simply running `ruff check` and `ruff format` in the command line in the root of the repository.
104104
- [mypy](https://mypy-lang.org/): Type checking is done with `mypy` by simply running `mypy zodipy/` in the root of the repository.
105105

106106
Remeber to add tests when implementing new features to maintain a high code coverage.

docs/examples/get_bandpass_integrated_emission.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
hp.mollview(
4040
emission_central_freq,
41-
title=f"Center frequency",
41+
title="Center frequency",
4242
unit="MJy/sr",
4343
cmap="afmhot",
4444
norm="log",

pyproject.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ lint.ignore = [
109109
"D107",
110110
"ANN101",
111111
"PLR0913",
112-
"ISC001"
112+
"ISC001",
113+
"S311",
113114
]
114-
exclude = ["tests/*", "docs/*"]
115-
lint.pydocstyle.convention = "google"
115+
# exclude = ["docs/*"]
116+
lint.pydocstyle.convention = "google"
117+
[tool.ruff.lint.per-file-ignores]
118+
"tests/*" = ["S101"]
119+
"docs/*" = ["INP001", "T201", "S101"]

tests/_strategies.py

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@
3232
MIN_FREQ = u.Quantity(10, u.GHz)
3333
MAX_FREQ = u.Quantity(0.1, u.micron).to(u.GHz, equivalencies=u.spectral())
3434
N_FREQS = 1000
35-
FREQ_LOG_RANGE = np.geomspace(
36-
np.log(MIN_FREQ.value), np.log(MAX_FREQ.value), N_FREQS
37-
).tolist()
35+
FREQ_LOG_RANGE = np.geomspace(np.log(MIN_FREQ.value), np.log(MAX_FREQ.value), N_FREQS).tolist()
3836

3937
MIN_DATE = datetime.datetime(year=1900, month=1, day=1)
4038
MAX_DATE = datetime.datetime(year=2100, month=1, day=1)
@@ -74,9 +72,7 @@ def time(draw: DrawFn) -> Time:
7472

7573
@composite
7674
def nside(draw: Callable[[SearchStrategy[int]], int]) -> int:
77-
return draw(
78-
integers(min_value=MIN_NSIDE_EXP, max_value=MAX_NSIDE_EXP).map(partial(pow, 2))
79-
)
75+
return draw(integers(min_value=MIN_NSIDE_EXP, max_value=MAX_NSIDE_EXP).map(partial(pow, 2)))
8076

8177

8278
@composite
@@ -93,9 +89,7 @@ def pixels(draw: DrawFn, nside: int) -> int | list[int] | npt.NDArray[np.integer
9389

9490

9591
@composite
96-
def angles(
97-
draw: DrawFn, lonlat: bool = False
98-
) -> tuple[u.Quantity[u.deg], u.Quantity[u.deg]]:
92+
def angles(draw: DrawFn, lonlat: bool = False) -> tuple[u.Quantity[u.deg], u.Quantity[u.deg]]:
9993
if lonlat:
10094
theta_strategy = floats(min_value=0, max_value=360)
10195
phi_strategy = floats(min_value=-90, max_value=90)
@@ -105,9 +99,9 @@ def angles(
10599

106100
shape = draw(integers(min_value=1, max_value=MAX_ANGELS_LEN))
107101

108-
theta_array_strategy = arrays(
109-
dtype=float, shape=shape, elements=theta_strategy
110-
).map(partial(u.Quantity, unit=u.deg))
102+
theta_array_strategy = arrays(dtype=float, shape=shape, elements=theta_strategy).map(
103+
partial(u.Quantity, unit=u.deg)
104+
)
111105
phi_array_strategy = arrays(dtype=float, shape=shape, elements=phi_strategy).map(
112106
partial(u.Quantity, unit=u.deg)
113107
)
@@ -116,16 +110,9 @@ def angles(
116110

117111

118112
@composite
119-
def freq(
120-
draw: DrawFn, model: zodipy.Zodipy
121-
) -> u.Quantity[u.GHz] | u.Quantity[u.micron]:
122-
113+
def freq(draw: DrawFn, model: zodipy.Zodipy) -> u.Quantity[u.GHz] | u.Quantity[u.micron]:
123114
if model.extrapolate:
124-
return draw(
125-
sampled_from(FREQ_LOG_RANGE)
126-
.map(np.exp)
127-
.map(partial(u.Quantity, unit=u.GHz))
128-
)
115+
return draw(sampled_from(FREQ_LOG_RANGE).map(np.exp).map(partial(u.Quantity, unit=u.GHz)))
129116

130117
min_freq = model._ipd_model.spectrum[0]
131118
max_freq = model._ipd_model.spectrum[-1]
@@ -140,9 +127,7 @@ def freq(
140127

141128

142129
@composite
143-
def random_freq(
144-
draw: DrawFn, unit: u.Unit | None = None, bandpass: bool = False
145-
) -> u.Quantity:
130+
def random_freq(draw: DrawFn, unit: u.Unit | None = None, bandpass: bool = False) -> u.Quantity:
146131
random_freq = draw(
147132
sampled_from(FREQ_LOG_RANGE).map(np.exp).map(partial(u.Quantity, unit=u.GHz))
148133
)
@@ -156,10 +141,9 @@ def random_freq(
156141
if bandpass:
157142
shape = draw(integers(min_value=2, max_value=100))
158143
sigma = draw(floats(min_value=0.1, max_value=0.3))
159-
freq_range = np.linspace(
144+
return np.linspace(
160145
random_freq - random_freq * sigma, random_freq + random_freq * sigma, shape
161146
)
162-
return freq_range
163147

164148
return random_freq
165149

@@ -178,40 +162,30 @@ def normalize_array(
178162
list_stategy = lists(weights_strategy, min_size=freqs.size, max_size=freqs.size)
179163
array_strategy = arrays(dtype=float, shape=freqs.size, elements=weights_strategy)
180164

181-
return draw(
182-
one_of(list_stategy, array_strategy).map(partial(normalize_array, freqs=freqs))
183-
)
165+
return draw(one_of(list_stategy, array_strategy).map(partial(normalize_array, freqs=freqs)))
184166

185167

186168
@composite
187169
def obs(draw: DrawFn, model: zodipy.Zodipy, obs_time: Time) -> str:
188170
def get_obs_dist(obs: str, obs_time: Time) -> u.Quantity[u.AU]:
189171
if obs == "semb-l2":
190172
obs_pos = (
191-
get_body("earth", obs_time)
192-
.transform_to(HeliocentricMeanEcliptic)
193-
.cartesian.xyz
173+
get_body("earth", obs_time).transform_to(HeliocentricMeanEcliptic).cartesian.xyz
194174
)
195175
obs_pos += 0.01 * u.AU
196176
else:
197-
obs_pos = (
198-
get_body(obs, obs_time)
199-
.transform_to(HeliocentricMeanEcliptic)
200-
.cartesian.xyz
201-
)
177+
obs_pos = get_body(obs, obs_time).transform_to(HeliocentricMeanEcliptic).cartesian.xyz
202178
return u.Quantity(np.linalg.norm(obs_pos.value), u.AU)
203179

204180
los_dist_cut = min(
205-
[COMPONENT_CUTOFFS[comp][1] for comp in model._ipd_model.comps.keys()],
181+
[COMPONENT_CUTOFFS[comp][1] for comp in model._ipd_model.comps],
206182
)
207183
if isinstance(los_dist_cut, dict):
208184
los_dist_cut = min(list(los_dist_cut.values()))
209185

210186
obs_list = model.supported_observers
211187
return draw(
212-
sampled_from(obs_list).filter(
213-
lambda obs: los_dist_cut > get_obs_dist(obs, obs_time).value
214-
)
188+
sampled_from(obs_list).filter(lambda obs: los_dist_cut > get_obs_dist(obs, obs_time).value)
215189
)
216190

217191

@@ -231,10 +205,8 @@ def any_obs(draw: DrawFn, model: zodipy.Zodipy) -> str:
231205
@composite
232206
def model(draw: DrawFn, **static_params: dict[str, Any]) -> zodipy.Zodipy:
233207
strategies = MODEL_STRATEGY_MAPPINGS.copy()
234-
for key in static_params.keys():
208+
for key in static_params:
235209
if key in strategies:
236210
strategies.pop(key)
237211

238-
return draw(
239-
builds(partial(zodipy.Zodipy, **static_params), **strategies)
240-
)
212+
return draw(builds(partial(zodipy.Zodipy, **static_params), **strategies))

tests/test_get_emission.py

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ def test_get_emission_pix(
3535
data: DataObject,
3636
) -> None:
3737
"""Property test for get_emission_pix."""
38-
3938
observer = data.draw(obs(model, time))
4039
frequency = data.draw(freq(model))
4140
pix = data.draw(pixels(nside))
@@ -58,7 +57,6 @@ def test_get_binned_emission_pix(
5857
data: DataObject,
5958
) -> None:
6059
"""Property test for get_binned_emission_pix."""
61-
6260
observer = data.draw(obs(model, time))
6361
frequency = data.draw(freq(model))
6462
pix = data.draw(pixels(nside))
@@ -82,7 +80,6 @@ def test_get_emission_ang(
8280
data: DataObject,
8381
) -> None:
8482
"""Property test for get_emission_ang."""
85-
8683
theta, phi = angles
8784

8885
observer = data.draw(obs(model, time))
@@ -108,7 +105,6 @@ def test_get_binned_emission_ang(
108105
data: DataObject,
109106
) -> None:
110107
"""Property test for get_binned_emission_pix."""
111-
112108
theta, phi = angles
113109

114110
observer = data.draw(obs(model, time))
@@ -134,11 +130,7 @@ def test_invalid_freq(
134130
nside: int,
135131
data: DataObject,
136132
) -> None:
137-
"""
138-
Property test checking for unsupported spectral range in models where extrapolation is
139-
set to False.
140-
"""
141-
133+
"""Property test checking for unsupported spectral range."""
142134
theta, phi = angles
143135
observer = data.draw(obs(model, time))
144136
pix = data.draw(pixels(nside))
@@ -186,23 +178,20 @@ def test_invalid_freq(
186178

187179

188180
def test_compare_to_dirbe_idl() -> None:
189-
"""
190-
Tests that ZodiPy is able to reproduce tabulated emission from the DIRBE Zoidacal Light
181+
"""Tests that ZodiPy reproduces the DIRBE software.
182+
183+
Zodipy should be able to reproduce the tabulated emission from the DIRBE Zoidacal Light
191184
Prediction Software with a maximum difference of 0.1%.
192185
"""
193-
194186
model = Zodipy("dirbe")
195-
for freq, tabulated_emission in TABULATED_DIRBE_EMISSION.items():
196-
freq *= u.micron
187+
for frequency, tabulated_emission in TABULATED_DIRBE_EMISSION.items():
197188
for idx, (day, lon, lat) in enumerate(zip(DAYS, LON, LAT)):
198189
time = DIRBE_START_DAY + TimeDelta(day - 1, format="jd")
199-
lon *= u.deg
200-
lat *= u.deg
201190

202191
emission = model.get_emission_ang(
203-
freq,
204-
theta=lon,
205-
phi=lat,
192+
frequency * u.micron,
193+
theta=lon * u.deg,
194+
phi=lat * u.deg,
206195
lonlat=True,
207196
obs="earth",
208197
obs_time=time,
@@ -219,10 +208,7 @@ def test_invalid_pixel(
219208
random_integer: int,
220209
data: DataObject,
221210
) -> None:
222-
"""
223-
Tests that an error is raised when an invalid pixel is passed to get_emission_pix.
224-
"""
225-
211+
"""Tests that an error is raised when an invalid pixel is passed to get_emission_pix."""
226212
frequency = data.draw(freq(model))
227213
observer = data.draw(obs(model, time))
228214
npix = hp.nside2npix(nside)
@@ -245,12 +231,13 @@ def test_invalid_pixel(
245231
obs=observer,
246232
)
247233

234+
248235
def test_multiprocessing() -> None:
249-
"""
250-
Testing that model with multiprocessing enabled returns the same value as
236+
"""Tests multiprocessing with for zodipy.
237+
238+
Tests that model with multiprocessing enabled returns the same value as
251239
without multiprocessing.
252240
"""
253-
254241
model = Zodipy()
255242
model_parallel = Zodipy(n_proc=4)
256243

@@ -330,11 +317,9 @@ def test_multiprocessing() -> None:
330317

331318
assert np.allclose(emission_binned_ang.value, emission_binned_ang_parallel.value)
332319

333-
def test_inner_radial_cutoff_multiprocessing() -> None:
334-
"""
335-
Testing that model with inner radial cutoffs can be parallelized.
336-
"""
337320

321+
def test_inner_radial_cutoff_multiprocessing() -> None:
322+
"""Testing that model with inner radial cutoffs can be parallelized."""
338323
model = Zodipy("RRM-experimental")
339324
model_parallel = Zodipy("RRM-experimental", n_proc=4)
340325

@@ -360,6 +345,7 @@ def test_inner_radial_cutoff_multiprocessing() -> None:
360345
)
361346
assert np.allclose(emission_pix, emission_pix_parallel)
362347

348+
363349
@given(
364350
model(extrapolate=True),
365351
time(),
@@ -411,7 +397,6 @@ def test_weights(
411397
data: DataObject,
412398
) -> None:
413399
"""Property test for bandpass weights."""
414-
415400
theta, phi = angles
416401
observer = data.draw(obs(model, time))
417402
bp_weights = data.draw(weights(freqs))
@@ -428,16 +413,14 @@ def test_weights(
428413

429414

430415
def test_custom_weights() -> None:
416+
"""Tests bandpass integration with custom weights."""
431417
model = Zodipy()
432418
time = Time("2020-01-01")
433419
nside = 32
434420
pix = np.arange(hp.nside2npix(nside))
435421
central_freq = 25
436422
sigma_freq = 3
437-
freqs = (
438-
np.linspace(central_freq - sigma_freq, central_freq + sigma_freq, 100)
439-
* u.micron
440-
)
423+
freqs = np.linspace(central_freq - sigma_freq, central_freq + sigma_freq, 100) * u.micron
441424
weights = np.random.randn(len(freqs))
442425
weights /= np.trapz(weights, freqs.value)
443426

@@ -452,6 +435,7 @@ def test_custom_weights() -> None:
452435

453436

454437
def test_custom_obs_pos() -> None:
438+
"""Tests a user specified observer position."""
455439
model = Zodipy()
456440
time = Time("2020-01-01")
457441
nside = 64

tests/test_model_registry.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
1-
from hypothesis import given
21
import hypothesis.strategies as st
32
import pytest
3+
from hypothesis import given
44

55
from zodipy import model_registry
66

77
ALL_MODELS = model_registry.models
88

99

1010
@given(st.sampled_from(ALL_MODELS))
11-
def test_get_model(model) -> None:
11+
def test_get_model(model: str) -> None:
12+
"""Tests that the model can be retrieved."""
1213
model_registry.get_model(model)
1314

1415

1516
def test_get_model_raises_error() -> None:
17+
"""Tests that an error is raised when the model is not found."""
1618
with pytest.raises(ValueError):
1719
model_registry.get_model("metins_model")
1820

1921

2022
def test_register_model() -> None:
23+
"""Tests that a model can be registered."""
2124
DIRBE_model = model_registry.get_model("DIRBE")
2225
DIRBE_model.T_0 += 250
2326
model_registry.register_model("metins_model", DIRBE_model)
2427

2528

2629
def test_register_model_raises_error() -> None:
30+
"""Tests that an error is raised when the model is already registered."""
2731
with pytest.raises(ValueError):
2832
model_registry.register_model("DIRBE", model_registry.get_model("DIRBE"))

0 commit comments

Comments
 (0)