Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new linter: ruff #481

Merged
merged 19 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .flake8

This file was deleted.

21 changes: 2 additions & 19 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,5 @@ jobs:
poetry run pre-commit install
poetry run pre-commit run --all-files

- name: Run black
run: |
poetry run black --check btk

- name: Run isort
run: |
poetry run isort --check btk

- name: Run pydocstyle
run: |
poetry run pydocstyle btk

- name: Run flake8
run: |
poetry run flake8 btk

- name: Run pylint
run: |
poetry run pylint btk
- name: Run Ruff
run: poetry run ruff check --output-format=github btk/ tests/
5 changes: 3 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: trailing-whitespace
exclude: ^.*fits
Expand All @@ -13,8 +13,9 @@ repos:
- id: check-merge-conflict
- id: end-of-file-fixer
exclude: ^.*fits

- repo: https://github.com/kynan/nbstripout
rev: 0.6.1
rev: 0.7.1
hooks:
- id: nbstripout
args:
Expand Down
29 changes: 0 additions & 29 deletions .pylintrc

This file was deleted.

10 changes: 2 additions & 8 deletions btk/blend_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,16 +432,10 @@ def load(cls, path: str, batch_number: int = 0):
catalog_list.append(read_table_hdf5(f, path=f"catalog_list/{ii}"))

# load segmentation
if "segmentation" in f.keys():
segmentation = f["segmentation"][:]
else:
segmentation = None
segmentation = f["segmentation"][:] if "segmentation" in f.keys() else None

# load deblended images
if "deblended_images" in f.keys():
deblended_images = f["deblended_images"][:]
else:
deblended_images = None
deblended_images = f["deblended_images"][:] if "deblended_images" in f.keys() else None

# load general info about blend
batch_size = f.attrs["batch_size"]
Expand Down
41 changes: 27 additions & 14 deletions btk/deblend.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
from numpy.linalg import LinAlgError
from skimage.feature import peak_local_max

from btk.blend_batch import BlendBatch, DeblendBatch, DeblendExample, MultiResolutionBlendBatch
from btk.blend_batch import (
BlendBatch,
DeblendBatch,
DeblendExample,
MultiResolutionBlendBatch,
)
from btk.draw_blends import DrawBlendsGenerator
from btk.multiprocess import multiprocess

Expand All @@ -39,6 +44,8 @@
Args:
ii: The index of the example in the batch.
blend_batch: Instance of `BlendBatch` class.
njobs: Number of processes to use.
kwargs: Additional arguments to pass to deblender call.

Returns:
Instance of `DeblendedExample` class.
Expand Down Expand Up @@ -72,7 +79,8 @@

Args:
blend_batch: Instance of `BlendBatch` class
njobs: Number of njobs to paralelize across
njobs: Number of jobs to paralelize across
kwargs: Additional keyword arguments to pass to each deblend call.

Returns:
Instance of `DeblendedBatch` class
Expand Down Expand Up @@ -126,6 +134,7 @@
Args:
ii: The index of the example in the batch.
mr_batch: Instance of `MultiResolutionBlendBatch` class
njobs: Number of processes to use.

Returns:
Instance of `DeblendedExample` class
Expand Down Expand Up @@ -211,10 +220,7 @@
def deblend(self, ii: int, blend_batch: BlendBatch) -> DeblendExample:
"""Performs measurement on the ii-th example from the batch."""
blend_image = blend_batch.blend_images[ii]
if self.use_mean:
image = np.mean(blend_image, axis=0)
else:
image = blend_image[self.use_band]
image = np.mean(blend_image, axis=0) if self.use_mean else blend_image[self.use_band]

Check warning on line 223 in btk/deblend.py

View check run for this annotation

Codecov / codecov/patch

btk/deblend.py#L223

Added line #L223 was not covered by tests

# compute threshold value
threshold = self.threshold_scale * np.std(image)
Expand Down Expand Up @@ -281,15 +287,16 @@
"""Performs measurement on the i-th example from the batch."""
# get a 1-channel input for sep
blend_image = blend_batch.blend_images[ii]
if self.use_mean:
image = np.mean(blend_image, axis=0)
else:
image = blend_image[self.use_band]
image = np.mean(blend_image, axis=0) if self.use_mean else blend_image[self.use_band]

# run source extractor
bkg = sep.Background(image)
catalog, segmentation = sep.extract(
image, self.thresh, err=bkg.globalrms, segmentation_map=True, minarea=self.min_area
image,
self.thresh,
err=bkg.globalrms,
segmentation_map=True,
minarea=self.min_area,
)

segmentation_exp = np.zeros((self.max_n_sources, *image.shape), dtype=bool)
Expand Down Expand Up @@ -380,10 +387,16 @@

# add new predictions, masking those that are closer than threshold
ra_coordinates = np.concatenate(
[ra_coordinates, ra_detections[distance2d > self.matching_threshold]]
[
ra_coordinates,
ra_detections[distance2d > self.matching_threshold],
]
)
dec_coordinates = np.concatenate(
[dec_coordinates, dec_detections[distance2d > self.matching_threshold]]
[
dec_coordinates,
dec_detections[distance2d > self.matching_threshold],
]
)
else:
ra_coordinates = np.concatenate([ra_coordinates, ra_detections])
Expand Down Expand Up @@ -441,7 +454,7 @@
Args:
ii: The index of the example in the batch.
blend_batch: Instance of `BlendBatch` class.
reference_catalog: Reference catalog to use for deblending. If None, the
reference_catalogs: Reference catalog to use for deblending. If None, the
truth catalog is used.

Returns:
Expand Down
3 changes: 1 addition & 2 deletions btk/draw_blends.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ def get_catsim_galaxy(
agn = galsim.Gaussian(flux=agn_flux, sigma=1e-8)
components.append(agn)

profile = galsim.Add(components)
return profile
return galsim.Add(components)


class DrawBlendsGenerator(ABC):
Expand Down
2 changes: 2 additions & 0 deletions btk/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ def __init__(self, pixel_max_sep=5.0, **kwargs) -> None:

Args:
pixel_max_sep: the maximum separation in pixels to be considered a match
kwargs: Additional arguments for parent class.
"""
super().__init__(**kwargs)
self.distance_function = pixel_l2_distance_matrix
Expand Down Expand Up @@ -236,6 +237,7 @@ def __init__(self, arcsec_max_sep=2.0, **kwargs) -> None:

Args:
arcsec_max_sep: the maximum separation in arcsec to be considered a match
kwargs: Additional arguments for parent class.
"""
super().__init__(**kwargs)
self.max_sep = arcsec_max_sep
Expand Down
2 changes: 1 addition & 1 deletion btk/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
) -> np.ndarray:
"""Return ellipticities of both true and detected galaxies, assuming they are matched."""
# psf is assumed to be the same for the entire batch and correspond to selected band.
assert len(images.shape) == 4 # (batch_size, max_n_sources, H, W)
assert images.ndim == 4 # (batch_size, max_n_sources, H, W)

Check warning on line 45 in btk/measure.py

View check run for this annotation

Codecov / codecov/patch

btk/measure.py#L45

Added line #L45 was not covered by tests
batch_size, max_n_sources, _, _ = images.shape
ellipticities = np.zeros((batch_size, max_n_sources, 2))
for ii in range(batch_size):
Expand Down
4 changes: 2 additions & 2 deletions btk/metrics/reconstruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def _get_recon_metric(
images2 = iso_images2[ii, :n_sources]
mets = metric_func(images1, images2)
assert mets.shape == (n_sources,)
for ii in range(n_sources):
results.append(mets[ii])
for jj in range(n_sources):
results.append(mets[jj])

return np.array(results)

Expand Down
28 changes: 14 additions & 14 deletions btk/metrics/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
return np.where(is_on, seg, np.nan)


def mse(image1: np.ndarray, image2: np.ndarray) -> np.ndarray:
def mse(images1: np.ndarray, images2: np.ndarray) -> np.ndarray:
"""Computes mean-squared-error (MSE) between two images.

Args:
Expand All @@ -39,7 +39,7 @@
Returns:
Returns MSE between each corresponding iamge as an array of shape `*`.
"""
return np.sqrt(np.power((image1 - image2), 2).mean(axis=(-1, -2)))
return np.sqrt(np.power((images1 - images2), 2).mean(axis=(-1, -2)))


def iou(seg1: np.ndarray, seg2: np.ndarray) -> np.ndarray:
Expand All @@ -65,7 +65,7 @@
return i / u


def psnr(image1: np.ndarray, image2: np.ndarray) -> np.ndarray:
def psnr(images1: np.ndarray, images2: np.ndarray) -> np.ndarray:
"""Compute peak-signal-to-noise-ratio from skimage.

Args:
Expand All @@ -77,18 +77,18 @@
Returns:
Returns PSNR between each corresponding iamge as an array of shape `N`.
"""
n, h, w = image1.shape
assert image1.min() >= 0 and image2.min() >= 0
assert (n, h, w) == image2.shape
n, h, w = images1.shape
assert images1.min() >= 0 and images2.min() >= 0
assert (n, h, w) == images2.shape

Check warning on line 82 in btk/metrics/utils.py

View check run for this annotation

Codecov / codecov/patch

btk/metrics/utils.py#L80-L82

Added lines #L80 - L82 were not covered by tests
psnr_ = np.zeros(n)
for ii in range(n):
im1 = image1[ii] / image1[ii].max()
im2 = image2[ii] / image2[ii].max()
im1 = images1[ii] / images1[ii].max()
im2 = images2[ii] / images2[ii].max()

Check warning on line 86 in btk/metrics/utils.py

View check run for this annotation

Codecov / codecov/patch

btk/metrics/utils.py#L85-L86

Added lines #L85 - L86 were not covered by tests
psnr_[ii] = peak_signal_noise_ratio(im1, im2, data_range=1)
return psnr_


def struct_sim(image1: np.ndarray, image2: np.ndarray, **kwargs) -> np.ndarray:
def struct_sim(images1: np.ndarray, images2: np.ndarray, **kwargs) -> np.ndarray:
"""Compute structural similarity index from skimage.

By default, we normalize the images to be between 0 and 1. So that the
Expand All @@ -106,13 +106,13 @@
Returns structural similarity index between each corresponding iamge as
an array of shape `N`.
"""
assert image1.min() >= 0 and image2.min() >= 0
n, h, w = image1.shape
assert (n, h, w) == image2.shape
assert images1.min() >= 0 and images2.min() >= 0
n, h, w = images1.shape
assert (n, h, w) == images2.shape

Check warning on line 111 in btk/metrics/utils.py

View check run for this annotation

Codecov / codecov/patch

btk/metrics/utils.py#L109-L111

Added lines #L109 - L111 were not covered by tests
ssim = np.zeros(n)
for ii in range(n):
im1 = image1[ii] / image1[ii].max()
im2 = image2[ii] / image2[ii].max()
im1 = images1[ii] / images1[ii].max()
im2 = images2[ii] / images2[ii].max()

Check warning on line 115 in btk/metrics/utils.py

View check run for this annotation

Codecov / codecov/patch

btk/metrics/utils.py#L114-L115

Added lines #L114 - L115 were not covered by tests
ssim[ii] = structural_similarity(im1, im2, data_range=1, **kwargs)
return ssim

Expand Down
4 changes: 2 additions & 2 deletions btk/multiprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import multiprocessing as mp
from itertools import repeat, starmap
from typing import Callable, Iterable
from typing import Callable, Iterable, Optional


def _apply_args_and_kwargs(func: Callable, args, kwargs):
Expand All @@ -29,7 +29,7 @@ def get_current_process() -> int:
def multiprocess(
func: Callable,
args_iter: Iterable,
kwargs_iter: Iterable = None,
kwargs_iter: Optional[Iterable] = None,
njobs: int = 1,
verbose: bool = False,
):
Expand Down
2 changes: 1 addition & 1 deletion btk/sampling_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def __init__(
self.stamp_size = stamp_size
self.min_mag, self.max_mag = min_mag, max_mag
self.mag_name = mag_name
self.max_shift = self.stamp_size / 2 if not max_shift else max_shift
self.max_shift = max_shift if max_shift else self.stamp_size / 2

# only within area where sources are allowed
self.exp_count = density * (self.max_shift * 2 / 60) ** 2
Expand Down
7 changes: 3 additions & 4 deletions btk/survey.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@


def get_surveys(
names: Union[str, List[str]], psf_func: Callable = None
names: Union[str, List[str]], psf_func: Optional[Callable] = None
) -> Union[Survey, List[Survey]]:
"""Return specified surveys from galcheat extended to contain PSF information.

Expand Down Expand Up @@ -205,12 +205,11 @@
else:
raise RuntimeError(f"No psf files found in '{psf_dir}'.")
psf_array = fits.getdata(psf_dir + "/" + psf_file)
psf_model = galsim.InterpolatedImage(

return galsim.InterpolatedImage(

Check warning on line 209 in btk/survey.py

View check run for this annotation

Codecov / codecov/patch

btk/survey.py#L209

Added line #L209 was not covered by tests
galsim.Image(psf_array), scale=survey.pixel_scale.to_value("arcsec")
).withFlux(1.0)

return psf_model


def make_wcs(
pixel_scale: float,
Expand Down
Loading
Loading