Skip to content

Commit 6a0a463

Browse files
committed
🐛 refactor logging in ridge functions and improve code readability
1 parent bcecb46 commit 6a0a463

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

clouddrift/ridges.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from typing import Any, Dict, List, Optional, Tuple, Union
23

34
import numpy as np
@@ -330,7 +331,9 @@ def find_ridge_points(
330331
)[:2]
331332
else:
332333
# Univariate case
333-
amplitude, inst_frequency = calculate_univariate_inst_moments(wavelet_transform, axis=-1)[:2]
334+
amplitude, inst_frequency = calculate_univariate_inst_moments(
335+
wavelet_transform, axis=-1
336+
)[:2]
334337

335338
# Determine ridge quantity based on ridge type
336339
if ridge_type.lower().startswith("amp"):
@@ -496,7 +499,7 @@ def ridge_shift_interpolation(
496499
"""
497500
# Skip if no ridge points
498501
if not np.any(ridge_points):
499-
print("No ridge points found, returning empty result.")
502+
logging.info("No ridge points found, returning empty result.")
500503

501504
return [np.array([], dtype=y_array.dtype) for y_array in y_arrays]
502505

@@ -1030,7 +1033,6 @@ def calculate_ridge_lengths(
10301033

10311034
# Process each ridge to calculate its length
10321035
for ridge_id in range(1, num_ridges + 1):
1033-
10341036
if ridge_id not in ridge_data:
10351037
ridge_lengths.append(0.0)
10361038
continue
@@ -1051,7 +1053,7 @@ def calculate_ridge_lengths(
10511053
)
10521054
ridge_lengths.append(ridge_length)
10531055
except Exception as e:
1054-
print(f"Error calculating length for ridge {ridge_id}: {e}")
1056+
logging.error(f"Error calculating length for ridge {ridge_id}: {e}")
10551057
ridge_lengths.append(0.0)
10561058

10571059
return ridge_lengths
@@ -1102,11 +1104,13 @@ def ridge_analysis(
11021104
"""
11031105

11041106
# Find ridge points in wavelet transform
1105-
ridge_points, ridge_quantity, processed_transform, inst_frequency = find_ridge_points(
1106-
wavelet_transform=wavelet,
1107-
scale_frequencies=freqs,
1108-
amplitude_threshold=amplitude_threshold,
1109-
ridge_type=ridge_type,
1107+
ridge_points, ridge_quantity, processed_transform, inst_frequency = (
1108+
find_ridge_points(
1109+
wavelet_transform=wavelet,
1110+
scale_frequencies=freqs,
1111+
amplitude_threshold=amplitude_threshold,
1112+
ridge_type=ridge_type,
1113+
)
11101114
)
11111115

11121116
# Create frequency meshgrid for ridge point shifting

tests/ridges_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ def test_univariate_simple(self):
7070
t = np.linspace(0, 2 * np.pi, 100)
7171
dt = t[1] - t[0] # Time step
7272
signal = np.exp(1j * t)
73-
amp, omega, upsilon, xi = calculate_univariate_inst_moments(signal, sample_rate=1 / dt, axis=0)
73+
amp, omega, upsilon, xi = calculate_univariate_inst_moments(
74+
signal, sample_rate=1 / dt, axis=0
75+
)
7476
self.assertTrue(np.allclose(amp, 1.0, atol=1e-6))
7577
self.assertTrue(np.allclose(omega, 1.0, atol=1e-2))
7678
self.assertTrue(np.allclose(upsilon, 0.0, atol=1e-2))

0 commit comments

Comments
 (0)