Skip to content

Commit a104648

Browse files
raphaelstyclaude
andcommitted
Fix unused variables warnings and formatting
- Prefix unused subset_indices with underscore - Add #[allow(dead_code)] for KMeansResult and serial functions - Run cargo fmt for consistent formatting 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 66746c5 commit a104648

File tree

3 files changed

+55
-26
lines changed

3 files changed

+55
-26
lines changed

src/algorithm.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rand_chacha::ChaCha8Rng;
1010
use std::time::Instant;
1111

1212
/// Result of the k-means algorithm
13+
#[allow(dead_code)]
1314
pub struct KMeansResult {
1415
pub centroids: Array2<f32>,
1516
pub labels: Array1<i64>,
@@ -30,7 +31,9 @@ pub fn kmeans_double_chunked(
3031

3132
// Validate inputs
3233
if k == 0 {
33-
return Err(KMeansError::InvalidK("k must be greater than 0".to_string()));
34+
return Err(KMeansError::InvalidK(
35+
"k must be greater than 0".to_string(),
36+
));
3437
}
3538

3639
if n_samples < k {
@@ -44,7 +47,7 @@ pub fn kmeans_double_chunked(
4447
let mut rng = ChaCha8Rng::seed_from_u64(config.seed);
4548

4649
// Subsample if needed
47-
let (data_subset, subset_indices) = subsample_data(data, config, &mut rng)?;
50+
let (data_subset, _subset_indices) = subsample_data(data, config, &mut rng)?;
4851
let n_samples_used = data_subset.nrows();
4952

5053
if config.verbose {
@@ -146,10 +149,7 @@ pub fn kmeans_double_chunked(
146149
}
147150

148151
if config.verbose {
149-
eprintln!(
150-
" Reinitialized {} empty clusters",
151-
empty_clusters.len()
152-
);
152+
eprintln!(" Reinitialized {} empty clusters", empty_clusters.len());
153153
}
154154
}
155155

src/distance.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub fn compute_squared_norms(data: &ArrayView2<f32>) -> Array1<f32> {
2424

2525
/// Compute squared L2 norms for each row of a 2D array (non-parallel version for small arrays)
2626
#[inline]
27+
#[allow(dead_code)]
2728
pub fn compute_squared_norms_serial(data: &ArrayView2<f32>) -> Array1<f32> {
2829
let n_samples = data.nrows();
2930
let mut norms = Array1::zeros(n_samples);
@@ -109,6 +110,7 @@ pub fn find_nearest_centroids_chunked(
109110
}
110111

111112
/// Find the nearest centroid for each data point (serial version for small arrays)
113+
#[allow(dead_code)]
112114
pub fn find_nearest_centroids_serial(
113115
data_chunk: &ArrayView2<f32>,
114116
data_norms: &ArrayView1<f32>,
@@ -145,7 +147,10 @@ pub fn find_nearest_centroids_serial(
145147
}
146148

147149
/// Compute centroid shift (sum of L2 norms of centroid movements)
148-
pub fn compute_centroid_shift(old_centroids: &ArrayView2<f32>, new_centroids: &ArrayView2<f32>) -> f64 {
150+
pub fn compute_centroid_shift(
151+
old_centroids: &ArrayView2<f32>,
152+
new_centroids: &ArrayView2<f32>,
153+
) -> f64 {
149154
let k = old_centroids.nrows();
150155

151156
let shifts: f64 = (0..k)
@@ -199,7 +204,7 @@ mod tests {
199204

200205
assert_eq!(labels[0], 0); // (0,0) closest to centroid 0
201206
assert_eq!(labels[1], 1); // (10,10) closest to centroid 1
202-
// (5,5) is equidistant, but we take the first one found (0)
207+
// (5,5) is equidistant, but we take the first one found (0)
203208
}
204209

205210
#[test]

tests/integration_tests.rs

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ fn generate_clustered_data(
1515
let mut rng = ChaCha8Rng::seed_from_u64(seed);
1616

1717
// Generate random cluster centers
18-
let centers = Array2::random_using((n_clusters, n_features), Uniform::new(-10.0, 10.0), &mut rng);
18+
let centers = Array2::random_using(
19+
(n_clusters, n_features),
20+
Uniform::new(-10.0, 10.0),
21+
&mut rng,
22+
);
1923

2024
// Generate points around each center
2125
let samples_per_cluster = n_samples / n_clusters;
@@ -32,7 +36,8 @@ fn generate_clustered_data(
3236
for i in start_idx..end_idx {
3337
for j in 0..n_features {
3438
// Add Gaussian noise around the center
35-
let noise: f32 = Array2::random_using((1, 1), Uniform::new(-0.5, 0.5), &mut rng)[[0, 0]];
39+
let noise: f32 =
40+
Array2::random_using((1, 1), Uniform::new(-0.5, 0.5), &mut rng)[[0, 0]];
3641
data[[i, j]] = center[j] + noise;
3742
}
3843
}
@@ -43,11 +48,7 @@ fn generate_clustered_data(
4348

4449
/// Calculate Normalized Mutual Information (NMI) between true and predicted labels
4550
/// This is a simplified implementation for testing purposes
46-
fn calculate_cluster_purity(
47-
data: &ArrayView2<f32>,
48-
labels: &[i64],
49-
n_clusters: usize,
50-
) -> f32 {
51+
fn calculate_cluster_purity(data: &ArrayView2<f32>, labels: &[i64], n_clusters: usize) -> f32 {
5152
// Calculate cluster sizes and verify non-empty clusters
5253
let mut cluster_counts = vec![0usize; n_clusters];
5354
for &label in labels {
@@ -115,11 +116,18 @@ fn test_basic_train() {
115116

116117
let result = kmeans.train(&data.view());
117118
assert!(result.is_ok(), "Training should succeed");
118-
assert!(kmeans.centroids().is_some(), "Centroids should be set after training");
119+
assert!(
120+
kmeans.centroids().is_some(),
121+
"Centroids should be set after training"
122+
);
119123

120124
let centroids = kmeans.centroids().unwrap();
121125
assert_eq!(centroids.nrows(), 10, "Should have k centroids");
122-
assert_eq!(centroids.ncols(), 128, "Centroids should have correct dimensions");
126+
assert_eq!(
127+
centroids.ncols(),
128+
128,
129+
"Centroids should have correct dimensions"
130+
);
123131
}
124132

125133
#[test]
@@ -129,7 +137,10 @@ fn test_basic_fit() {
129137

130138
let result = kmeans.fit(&data.view());
131139
assert!(result.is_ok(), "Fit should succeed");
132-
assert!(kmeans.centroids().is_some(), "Centroids should be set after fit");
140+
assert!(
141+
kmeans.centroids().is_some(),
142+
"Centroids should be set after fit"
143+
);
133144
}
134145

135146
#[test]
@@ -183,7 +194,11 @@ fn test_clustering_quality_synthetic() {
183194

184195
// Check clustering quality
185196
let purity = calculate_cluster_purity(&data.view(), labels.as_slice().unwrap(), 5);
186-
assert!(purity > 0.0, "Clustering should produce non-zero purity: {}", purity);
197+
assert!(
198+
purity > 0.0,
199+
"Clustering should produce non-zero purity: {}",
200+
purity
201+
);
187202
}
188203

189204
#[test]
@@ -302,7 +317,10 @@ fn test_different_seeds_produce_different_results() {
302317
break;
303318
}
304319
}
305-
assert!(!all_equal, "Different seeds should produce different results");
320+
assert!(
321+
!all_equal,
322+
"Different seeds should produce different results"
323+
);
306324
}
307325

308326
// ============================================================================
@@ -455,8 +473,8 @@ fn test_small_chunk_sizes() {
455473
tol: 1e-8,
456474
seed: 42,
457475
max_points_per_centroid: None,
458-
chunk_size_data: 50, // Very small
459-
chunk_size_centroids: 3, // Very small
476+
chunk_size_data: 50, // Very small
477+
chunk_size_centroids: 3, // Very small
460478
verbose: false,
461479
};
462480

@@ -476,8 +494,8 @@ fn test_large_chunk_sizes() {
476494
tol: 1e-8,
477495
seed: 42,
478496
max_points_per_centroid: None,
479-
chunk_size_data: 100_000, // Larger than data
480-
chunk_size_centroids: 100_000, // Larger than k
497+
chunk_size_data: 100_000, // Larger than data
498+
chunk_size_centroids: 100_000, // Larger than k
481499
verbose: false,
482500
};
483501

@@ -497,12 +515,18 @@ fn test_centroids_getter() {
497515
let mut kmeans = FastKMeans::new(8, 5);
498516

499517
// Before training
500-
assert!(kmeans.centroids().is_none(), "Centroids should be None before training");
518+
assert!(
519+
kmeans.centroids().is_none(),
520+
"Centroids should be None before training"
521+
);
501522

502523
// After training
503524
kmeans.train(&data.view()).unwrap();
504525
let centroids = kmeans.centroids();
505-
assert!(centroids.is_some(), "Centroids should be Some after training");
526+
assert!(
527+
centroids.is_some(),
528+
"Centroids should be Some after training"
529+
);
506530

507531
let c = centroids.unwrap();
508532
assert_eq!(c.nrows(), 5);

0 commit comments

Comments
 (0)