From e0cf3ea2e175398ce66c86156d392a9e73ad18a0 Mon Sep 17 00:00:00 2001 From: gnzng Date: Thu, 10 Jul 2025 14:33:47 -0700 Subject: [PATCH] Enhance line_based_frc function with detailed docstring and input validation checks --- src/cdtools/tools/analysis/analysis.py | 6 +++++- tests/tools/test_analysis.py | 10 +++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/cdtools/tools/analysis/analysis.py b/src/cdtools/tools/analysis/analysis.py index 38b1306..055f028 100644 --- a/src/cdtools/tools/analysis/analysis.py +++ b/src/cdtools/tools/analysis/analysis.py @@ -1530,7 +1530,8 @@ def calc_spectral_info(dataset, nbins=50): def line_based_frc(image1, image2, axis=1, n_bins=None, thresholds={}, pixel_size=1.0, unit="pixels"): """ - Compute line-based FRC with multiple threshold methods + Compute line-based FRC with multiple threshold methods. This works great for highly anisotropic + images, such as those with vertical or horizontal stripes. Parameters ---------- @@ -1563,6 +1564,9 @@ def line_based_frc(image1, image2, axis=1, n_bins=None, thresholds={}, pixel_siz if isinstance(image2, t.Tensor): image2 = image2.numpy() + assert isinstance(image1, np.ndarray), "image1 must be a numpy array or torch tensor" + assert isinstance(image2, np.ndarray), "image2 must be a numpy array or torch tensor" + assert image1.shape == image2.shape, "Images must have same shape" if image1.ndim != 2: diff --git a/tests/tools/test_analysis.py b/tests/tools/test_analysis.py index 265a5ea..0c84a27 100644 --- a/tests/tools/test_analysis.py +++ b/tests/tools/test_analysis.py @@ -1,7 +1,8 @@ import numpy as np +import pytest +import torch as t from scipy import linalg as la from scipy.sparse import linalg as spla -import torch as t from cdtools.tools import analysis, initializers from cdtools.tools.analysis import line_based_frc @@ -612,3 +613,10 @@ def test_line_based_frc(): assert res_dict["1/2"] > 70.0 and res_dict["1/2"] < 75.0 assert res_dict["1/4"] > 52.0 and res_dict["1/4"] < 57.0 assert res_dict["1/7"] > 45.0 and res_dict["1/7"] < 48.0 + + # Now lets check if we catch some errors correctly + # Check if it raises an error for non-2D input + with pytest.raises(ValueError): + line_based_frc(np.random.rand(30, 17, 3), np.random.rand(30, 17, 3)) + line_based_frc(np.random.rand(30, 17, 3), np.random.rand(30, 17)) + line_based_frc("string", np.random.rand(30, 17))