From b36aa034a6d94b957212fe727bf92b555933fc41 Mon Sep 17 00:00:00 2001 From: Tim Galvin Date: Mon, 17 Aug 2026 15:33:51 +0800 Subject: [PATCH 1/3] addded logger, and to --- .pre-commit-config.yaml | 47 ++++++++++++++++++++--------------------- eye_patch/logging.py | 45 +++++++++++++++++++++++++++++++++++++++ eye_patch/masking.py | 6 ++---- 3 files changed, 70 insertions(+), 28 deletions(-) create mode 100644 eye_patch/logging.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1871e17..c8ebce6 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,16 +1,13 @@ ci: - autoupdate_commit_msg: "chore(deps): update pre-commit hooks" + autoupdate_commit_msg: "chore: update pre-commit hooks" autofix_commit_msg: "style: pre-commit fixes" - autoupdate_schedule: "monthly" - -exclude: ^.cruft.json|.copier-answers.yml$ repos: - repo: https://github.com/adamchainz/blacken-docs rev: "1.20.0" hooks: - id: blacken-docs - additional_dependencies: [black==25.*] + additional_dependencies: [black==24.*] - repo: https://github.com/pre-commit/pre-commit-hooks rev: "v6.0.0" @@ -28,24 +25,24 @@ repos: - id: requirements-txt-fixer - id: trailing-whitespace - - repo: https://github.com/pre-commit/pygrep-hooks - rev: "v1.10.0" - hooks: - - id: rst-backticks - - id: rst-directive-colons - - id: rst-inline-touching-normal + # - repo: https://github.com/pre-commit/pygrep-hooks + # rev: "v1.10.0" + # hooks: + # - id: rst-backticks + # - id: rst-directive-colons + # - id: rst-inline-touching-normal - - repo: https://github.com/rbubley/mirrors-prettier - rev: "v3.8.3" - hooks: - - id: prettier - types_or: [yaml, markdown, html, css, scss, javascript, json] - args: [--prose-wrap=always] + # - repo: https://github.com/pre-commit/mirrors-prettier + # rev: "v4.0.0-alpha.8" + # hooks: + # - id: prettier + # types_or: [yaml, markdown, html, css, scss, javascript, json] + # args: [--prose-wrap=always] - repo: https://github.com/astral-sh/ruff-pre-commit rev: "v0.15.12" hooks: - - id: ruff-check + - id: ruff args: ["--fix", "--show-fixes"] - id: ruff-format @@ -53,19 +50,21 @@ repos: rev: "v2.0.0" hooks: - id: mypy - files: eye_patch|tests + files: flint|tests args: ["--ignore-missing-imports"] additional_dependencies: - - numpy - - capn-crunch - - pytest-stub + - pytest + - types-PyYAML + + - repo: https://github.com/crate-ci/typos + rev: "v1.46.0" + hooks: + - id: typos - repo: https://github.com/codespell-project/codespell rev: "v2.4.2" hooks: - id: codespell - additional_dependencies: - - tomli; python_version<'3.11' - repo: https://github.com/shellcheck-py/shellcheck-py rev: "v0.11.0.1" diff --git a/eye_patch/logging.py b/eye_patch/logging.py new file mode 100644 index 0000000..36885f5 --- /dev/null +++ b/eye_patch/logging.py @@ -0,0 +1,45 @@ +from __future__ import annotations + +import logging + +# Create logger +logging.captureWarnings(True) +logger = logging.getLogger("eye-patch") +logger.setLevel(logging.INFO) + +# Create console handler and set level to debug +ch = logging.StreamHandler() +ch.setLevel(logging.INFO) + + +class CustomFormatter(logging.Formatter): + """A custom logger formatter""" + + grey = "\x1b[38;20m" + blue = "\x1b[34;20m" + green = "\x1b[32;20m" + yellow = "\x1b[33;20m" + red = "\x1b[31;20m" + bold_red = "\x1b[31;1m" + reset = "\x1b[0m" + format_str = "%(asctime)s.%(msecs)03d %(module)s - %(funcName)s: %(message)s" + + FORMATS = { # noqa: RUF012 + logging.DEBUG: f"{blue}%(levelname)s{reset} {format_str}", + logging.INFO: f"{green}%(levelname)s{reset} {format_str}", + logging.WARNING: f"{yellow}%(levelname)s{reset} {format_str}", + logging.ERROR: f"{red}%(levelname)s{reset} {format_str}", + logging.CRITICAL: f"{bold_red}%(levelname)s{reset} {format_str}", + } + + def format(self, record): + log_fmt = self.FORMATS.get(record.levelno) + formatter = logging.Formatter(log_fmt, "%Y-%m-%d %H:%M:%S") + return formatter.format(record) + + +# Add formatter to ch +ch.setFormatter(CustomFormatter()) + +# Add ch to logger +logger.addHandler(ch) diff --git a/eye_patch/masking.py b/eye_patch/masking.py index 536246b..d453c85 100644 --- a/eye_patch/masking.py +++ b/eye_patch/masking.py @@ -4,7 +4,6 @@ from __future__ import annotations -import logging from argparse import ArgumentParser from pathlib import Path from typing import NamedTuple, TypeAlias @@ -26,6 +25,7 @@ from scipy.ndimage import binary_fill_holes, label, maximum_filter, minimum_filter from scipy.signal import fftconvolve +from eye_patch.logging import logger from eye_patch.naming import FITSMaskNames, create_fits_mask_names # Add explicit export so mypy on tests is ok @@ -37,8 +37,6 @@ # during fits file creation. MaskLike: TypeAlias = NDArray[np.floating] -logger = logging.getLogger("__name__") - class MaskingOptions(BaseOptions): """Contains options for the creation of clean masks from some subject @@ -916,7 +914,7 @@ def create_snr_mask_from_fits( logger.info(f"Writing {mask_names.mask_fits}") fits.writeto( filename=mask_names.mask_fits, - data=mask_data, + data=mask_data.astype(np.float32), header=fits_header, overwrite=overwrite, ) From f43f1955bae8c25b4f097ecd92542761a2bf4003 Mon Sep 17 00:00:00 2001 From: Tim Galvin Date: Mon, 17 Aug 2026 15:50:52 +0800 Subject: [PATCH 2/3] added a warning --- eye_patch/masking.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/eye_patch/masking.py b/eye_patch/masking.py index d453c85..cec57f1 100644 --- a/eye_patch/masking.py +++ b/eye_patch/masking.py @@ -963,6 +963,11 @@ def convolve_image_by_scale( logger.info(f"Generating gaussian kernel for {scale=} {fwhm=:.3f} {sigma=:.3f}") pix_sigma = int(sigma * 5) + if pix_sigma < 1: + logger.warning( + f"{scale=} is too small to form a appropriately sized gaussian kernel. Setting its {pix_sigma=} to 1." + ) + pix_sigma = 1 x = np.linspace(0, pix_sigma, pix_sigma) y = np.linspace(0, pix_sigma, pix_sigma) From 90382f7eae5f5b668fd8e6e352b47d7c1ee97a0a Mon Sep 17 00:00:00 2001 From: Tim Galvin Date: Mon, 17 Aug 2026 16:24:07 +0800 Subject: [PATCH 3/3] raise error, not warning --- eye_patch/masking.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/eye_patch/masking.py b/eye_patch/masking.py index cec57f1..257d637 100644 --- a/eye_patch/masking.py +++ b/eye_patch/masking.py @@ -964,10 +964,11 @@ def convolve_image_by_scale( pix_sigma = int(sigma * 5) if pix_sigma < 1: - logger.warning( - f"{scale=} is too small to form a appropriately sized gaussian kernel. Setting its {pix_sigma=} to 1." - ) - pix_sigma = 1 + # linspace can only take integer inputs, and if sigma is too small then this array comes + # out as length zero. + msg = f"{scale=} is too small and an appropriately sized kernel can not be formed. Consider removing it. " + raise ValueError(msg) + x = np.linspace(0, pix_sigma, pix_sigma) y = np.linspace(0, pix_sigma, pix_sigma)