Skip to content
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: 5 additions & 0 deletions .markdownlint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ MD013:
MD033:
allowed_elements:
- div

# Exclude files
ignore:
- LICENSE
- LICENSE.md
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ repos:
hooks:
- id: black
language_version: python3
exclude: (^docs/|^notebooks/|^LICENSE$)
exclude: (^docs/|^notebooks/|^LICENSE$|build/)

# --- Mypy (type checking) ---
- repo: https://github.com/pre-commit/mirrors-mypy
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name = "pnanolocz_lib"
description = "A Python library for AFM, HS-AFM, and Localization AFM data analysis. Based on NanoLocz"
authors = [
{ name = "George Heath", email = "G.R.Heath@leeds.ac.uk" },
{ name = "D. E. Rollins", email = "d.e.rollins@leeds.ac.uk" }
{ name = "Daniel E. Rollins", email = "d.e.rollins@leeds.ac.uk" }
]
dynamic = ["version"]
readme = "README.md"
Expand Down
19 changes: 12 additions & 7 deletions src/pnanolocz_lib/level.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
from typing import Any, Literal, Optional

import numpy as np
from numpy.polynomial.polyutils import RankWarning # type: ignore[attr-defined]
from scipy.optimize import curve_fit

# Constants
Expand Down Expand Up @@ -193,7 +192,8 @@ def level_plane(
# ========== X DIRECTION ==========
# Column-wise masked mean with NaN-outside semantics
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=RuntimeWarning)
warnings.simplefilter("ignore", RuntimeWarning)
warnings.filterwarnings("ignore", message=".*[Rr]ank.*")
column_means = np.nanmean(np.where(m, arr, np.nan), axis=0)

valid_columns = ~np.isnan(column_means)
Expand All @@ -213,7 +213,8 @@ def level_plane(
standardized_columns = (column_indices - col_centroid) / col_scale

with warnings.catch_warnings():
warnings.simplefilter("ignore", RankWarning)
warnings.simplefilter("ignore", RuntimeWarning)
warnings.filterwarnings("ignore", message=".*[Rr]ank.*")
x_coeffs = np.polyfit(standardized_columns, column_means[valid_columns], polyx)

# Evaluate polynomial at every column (1..W) using the same mu
Expand All @@ -226,7 +227,8 @@ def level_plane(
# ========== Y DIRECTION ==========
# Row-wise masked mean after X subtraction (NaN-outside semantics)
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=RuntimeWarning)
warnings.simplefilter("ignore", RuntimeWarning)
warnings.filterwarnings("ignore", message=".*[Rr]ank.*")
row_means = np.nanmean(np.where(m, leveled, np.nan), axis=1)

valid_rows = ~np.isnan(row_means)
Expand All @@ -245,7 +247,8 @@ def level_plane(
standardized_rows = (row_indices - row_centroid) / row_scale

with warnings.catch_warnings():
warnings.simplefilter("ignore", RankWarning)
warnings.simplefilter("ignore", RuntimeWarning)
warnings.filterwarnings("ignore", message=".*[Rr]ank.*")
y_coeffs = np.polyfit(standardized_rows, row_means[valid_rows], polyy)

# Evaluate polynomial at every row (1..H) with the same mu
Expand Down Expand Up @@ -333,7 +336,8 @@ def level_line(
xs = (x_idx - mu) / sd # xs: standardized x indices used for fitting

with warnings.catch_warnings():
warnings.simplefilter("ignore", RankWarning)
warnings.simplefilter("ignore", RuntimeWarning)
warnings.filterwarnings("ignore", message=".*[Rr]ank.*")
p_coeff = np.polyfit(xs, y_vals, polyx)

all_cols = (np.arange(img_width) + 1).astype(np.float64)
Expand Down Expand Up @@ -378,7 +382,8 @@ def level_line(
ys = (yl - mu) / sd # ys: standardized y indices used for fitting

with warnings.catch_warnings():
warnings.simplefilter("ignore", RankWarning)
warnings.simplefilter("ignore", RuntimeWarning)
warnings.filterwarnings("ignore", message=".*[Rr]ank.*")
p_coeff = np.polyfit(ys, y_vals, polyy)

all_rows = (np.arange(img_height) + 1).astype(np.float64)
Expand Down
10 changes: 6 additions & 4 deletions src/pnanolocz_lib/level_weighted.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
from typing import Any, List, Optional, Tuple

import numpy as np
from numpy.polynomial.polyutils import RankWarning # type: ignore[attr-defined]
from scipy import ndimage

# ---------------------
Expand Down Expand Up @@ -151,7 +150,8 @@ def _polyfit_centered(
std_x = (x - centroid) / scale

with warnings.catch_warnings():
warnings.simplefilter("ignore", RankWarning)
warnings.simplefilter("ignore", RuntimeWarning)
warnings.filterwarnings("ignore", message=".*[Rr]ank.*")
coeffs = np.polyfit(std_x, y, order)

return np.asarray(coeffs, dtype=np.float64), (centroid, scale)
Expand Down Expand Up @@ -314,7 +314,8 @@ def level_weighted_plane(

# X-direction: mean of each column within region
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=RuntimeWarning)
warnings.simplefilter("ignore", RuntimeWarning)
warnings.filterwarnings("ignore", message=".*[Rr]ank.*")
mean_by_col = np.nanmean(
region_masked, axis=0
) # Nanolocz-mean_by_col is xp
Expand All @@ -336,7 +337,8 @@ def level_weighted_plane(

# Y-direction: mean of each row within region
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=RuntimeWarning)
warnings.simplefilter("ignore", RuntimeWarning)
warnings.filterwarnings("ignore", message=".*[Rr]ank.*")
mean_by_row = np.nanmean(region_masked, axis=1)
valid_rows = ~np.isnan(mean_by_row)
row_values = mean_by_row[valid_rows]
Expand Down
2 changes: 1 addition & 1 deletion src/pnanolocz_lib/thresholder.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
Authors
-------
George Heath, University of Leeds (MATLAB reference implementation)
D. E. Rollins, University of Leeds (Python implementation)
Daniel E. Rollins, University of Leeds (Python implementation)
Part of the pNanoLocz-Lib Python library for AFM analysis.
"""
Expand Down