sklears-datasets 0.2.0

Dataset utilities and generation for sklears
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! Manifold and spatial pattern dataset generators
//!
//! This module provides generators for geometric manifolds, spatial point patterns,
//! and spatial statistical data commonly used in spatial analysis and manifold learning.

use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::random::{Random, rng};
use scirs2_core::random::distributions::{Normal, StandardNormal};
use sklears_core::error::{Result, SklearsError};
use std::f64::consts::PI;

/// Trait for manifold generators
pub trait ManifoldGenerator {
    fn generate_point(&self, parameters: &[f64]) -> Array1<f64>;
    fn parameter_bounds(&self) -> Vec<(f64, f64)>;
}

/// Generate samples from a custom manifold
///
/// Creates samples lying on a manifold defined by a custom generator function.
/// This is useful for testing manifold learning algorithms on known structures.
///
/// # Parameters
/// - `n_samples`: Number of samples to generate
/// - `generator`: Custom manifold generator implementing ManifoldGenerator trait
/// - `noise_level`: Amount of noise to add to manifold samples
/// - `random_state`: Random seed for reproducibility
///
/// # Returns
/// Array of samples on the specified manifold
pub fn make_custom_manifold<T: ManifoldGenerator>(
    n_samples: usize,
    generator: &T,
    noise_level: f64,
    random_state: Option<u64>,
) -> Result<Array2<f64>> {
    if n_samples == 0 {
        return Err(SklearsError::InvalidInput(
            "n_samples must be positive".to_string(),
        ));
    }

    let mut rng = Random::from_seed(random_state.unwrap_or_else(|| rng().gen()));

    let bounds = generator.parameter_bounds();
    let n_params = bounds.len();

    if n_params == 0 {
        return Err(SklearsError::InvalidInput(
            "Manifold generator must have at least one parameter".to_string(),
        ));
    }

    // Generate first sample to determine embedding dimension
    let mut params = vec![0.0; n_params];
    for (i, &(min_val, max_val)) in bounds.iter().enumerate() {
        params[i] = rng.gen_range(min_val..max_val);
    }
    let first_point = generator.generate_point(&params);
    let embedding_dim = first_point.len();

    let mut samples = Array2::zeros((n_samples, embedding_dim));

    // Generate all samples
    for i in 0..n_samples {
        // Generate random parameters within bounds
        for (j, &(min_val, max_val)) in bounds.iter().enumerate() {
            params[j] = rng.gen_range(min_val..max_val);
        }

        let mut point = generator.generate_point(&params);

        // Add noise if specified
        if noise_level > 0.0 {
            for j in 0..embedding_dim {
                let noise = rng.sample(Normal::new(0.0, noise_level).expect("sampling should succeed"));
                point[j] += noise;
            }
        }

        for j in 0..embedding_dim {
            samples[[i, j]] = point[j];
        }
    }

    Ok(samples)
}

/// Generate samples on an n-dimensional sphere
///
/// Creates samples uniformly distributed on the surface of an n-dimensional sphere.
/// This is a fundamental manifold used in many manifold learning benchmarks.
///
/// # Parameters
/// - `n_samples`: Number of samples to generate
/// - `n_dim`: Dimension of the sphere (n_dim=2 gives unit circle, n_dim=3 gives unit sphere)
/// - `radius`: Radius of the sphere
/// - `noise_level`: Amount of noise to add
/// - `random_state`: Random seed for reproducibility
///
/// # Returns
/// Array of samples on the n-sphere
pub fn make_n_sphere(
    n_samples: usize,
    n_dim: usize,
    radius: f64,
    noise_level: f64,
    random_state: Option<u64>,
) -> Result<Array2<f64>> {
    if n_samples == 0 || n_dim == 0 {
        return Err(SklearsError::InvalidInput(
            "n_samples and n_dim must be positive".to_string(),
        ));
    }

    if radius <= 0.0 {
        return Err(SklearsError::InvalidInput(
            "radius must be positive".to_string(),
        ));
    }

    let mut rng = Random::from_seed(random_state.unwrap_or_else(|| rng().gen()));

    let mut samples = Array2::zeros((n_samples, n_dim));

    for i in 0..n_samples {
        // Generate point from normal distribution
        let mut point = Array1::zeros(n_dim);
        for j in 0..n_dim {
            point[j] = rng.sample(StandardNormal);
        }

        // Normalize to unit sphere
        let norm = point.iter().map(|&x| x * x).sum::<f64>().sqrt();
        for j in 0..n_dim {
            point[j] = radius * point[j] / norm;

            // Add noise if specified
            if noise_level > 0.0 {
                let noise = rng.sample(Normal::new(0.0, noise_level).expect("sampling should succeed"));
                point[j] += noise;
            }

            samples[[i, j]] = point[j];
        }
    }

    Ok(samples)
}

/// Generate samples on an n-dimensional torus
///
/// Creates samples on the surface of an n-dimensional torus embedded in higher-dimensional space.
/// This is useful for testing manifold learning on curved manifolds with interesting topology.
///
/// # Parameters
/// - `n_samples`: Number of samples to generate
/// - `n_dim`: Intrinsic dimension of the torus
/// - `major_radius`: Major radius of the torus
/// - `minor_radius`: Minor radius of the torus
/// - `noise_level`: Amount of noise to add
/// - `random_state`: Random seed for reproducibility
///
/// # Returns
/// Array of samples on the n-torus
pub fn make_n_torus(
    n_samples: usize,
    n_dim: usize,
    major_radius: f64,
    minor_radius: f64,
    noise_level: f64,
    random_state: Option<u64>,
) -> Result<Array2<f64>> {
    if n_samples == 0 || n_dim == 0 {
        return Err(SklearsError::InvalidInput(
            "n_samples and n_dim must be positive".to_string(),
        ));
    }

    if major_radius <= 0.0 || minor_radius <= 0.0 {
        return Err(SklearsError::InvalidInput(
            "radii must be positive".to_string(),
        ));
    }

    let mut rng = Random::from_seed(random_state.unwrap_or_else(|| rng().gen()));

    // Embedding dimension is 2 * n_dim for n-torus
    let embedding_dim = 2 * n_dim;
    let mut samples = Array2::zeros((n_samples, embedding_dim));

    for i in 0..n_samples {
        // Generate angles for each dimension
        let mut angles = vec![0.0; n_dim];
        for j in 0..n_dim {
            angles[j] = rng.random_range(0.0..2.0 * PI);
        }

        // Map to embedding space (simplified n-torus embedding)
        for j in 0..n_dim {
            let cos_angle = angles[j].cos();
            let sin_angle = angles[j].sin();

            samples[[i, j * 2]] = (major_radius + minor_radius * cos_angle) * angles[j].cos();
            samples[[i, j * 2 + 1]] = (major_radius + minor_radius * cos_angle) * sin_angle;

            // Add noise if specified
            if noise_level > 0.0 {
                let noise1 = rng.sample(Normal::new(0.0, noise_level).expect("sampling should succeed"));
                let noise2 = rng.sample(Normal::new(0.0, noise_level).expect("sampling should succeed"));
                samples[[i, j * 2]] += noise1;
                samples[[i, j * 2 + 1]] += noise2;
            }
        }
    }

    Ok(samples)
}

/// Generate spatial point pattern with specified characteristics
///
/// Creates spatial point patterns following various statistical processes
/// commonly used in spatial statistics and ecology.
///
/// # Parameters
/// - `n_points`: Number of points to generate
/// - `pattern_type`: Type of spatial pattern ("random", "clustered", "regular", "inhibited")
/// - `intensity`: Average intensity (points per unit area)
/// - `clustering_parameter`: Parameter controlling clustering/regularity
/// - `domain_size`: Size of the spatial domain (square domain of this size)
/// - `random_state`: Random seed for reproducibility
///
/// # Returns
/// Array of 2D spatial coordinates
pub fn make_spatial_point_pattern(
    n_points: usize,
    pattern_type: &str,
    intensity: f64,
    clustering_parameter: f64,
    domain_size: f64,
    random_state: Option<u64>,
) -> Result<Array2<f64>> {
    if n_points == 0 {
        return Err(SklearsError::InvalidInput(
            "n_points must be positive".to_string(),
        ));
    }

    if intensity <= 0.0 || domain_size <= 0.0 {
        return Err(SklearsError::InvalidInput(
            "intensity and domain_size must be positive".to_string(),
        ));
    }

    let mut rng = Random::from_seed(random_state.unwrap_or_else(|| rng().gen()));

    let mut points = Array2::zeros((n_points, 2));

    match pattern_type {
        "random" => {
            // Complete spatial randomness (Poisson process)
            for i in 0..n_points {
                points[[i, 0]] = rng.random_range(0.0..domain_size);
                points[[i, 1]] = rng.random_range(0.0..domain_size);
            }
        }

        "clustered" => {
            // Clustered pattern using cluster centers
            let n_clusters = (n_points as f64 / clustering_parameter).max(1.0) as usize;
            let points_per_cluster = n_points / n_clusters;

            // Generate cluster centers
            let mut cluster_centers = Array2::zeros((n_clusters, 2));
            for i in 0..n_clusters {
                cluster_centers[[i, 0]] = rng.random_range(0.0..domain_size);
                cluster_centers[[i, 1]] = rng.random_range(0.0..domain_size);
            }

            // Generate points around cluster centers
            for i in 0..n_points {
                let cluster_idx = i / points_per_cluster;
                let cluster_idx = cluster_idx.min(n_clusters - 1);

                let center_x = cluster_centers[[cluster_idx, 0]];
                let center_y = cluster_centers[[cluster_idx, 1]];

                // Add points with Gaussian dispersion around center
                let cluster_std = domain_size / (10.0 * clustering_parameter.sqrt());
                let dx = rng.sample(Normal::new(0.0, cluster_std).expect("sampling should succeed"));
                let dy = rng.sample(Normal::new(0.0, cluster_std).expect("sampling should succeed"));

                points[[i, 0]] = (center_x + dx).clamp(0.0, domain_size);
                points[[i, 1]] = (center_y + dy).clamp(0.0, domain_size);
            }
        }

        "regular" => {
            // Regular pattern using grid with perturbation
            let grid_size = (n_points as f64).sqrt() as usize;
            let cell_size = domain_size / grid_size as f64;
            let perturbation = cell_size * (1.0 - clustering_parameter) / 4.0;

            for i in 0..n_points {
                let row = i / grid_size;
                let col = i % grid_size;

                let base_x = (col as f64 + 0.5) * cell_size;
                let base_y = (row as f64 + 0.5) * cell_size;

                let dx = rng.gen_range(-perturbation..perturbation);
                let dy = rng.gen_range(-perturbation..perturbation);

                points[[i, 0]] = (base_x + dx).clamp(0.0, domain_size);
                points[[i, 1]] = (base_y + dy).clamp(0.0, domain_size);
            }
        }

        "inhibited" => {
            // Inhibited pattern using simple sequential inhibition
            let min_distance = clustering_parameter * domain_size / (n_points as f64).sqrt();
            let mut placed_points = Vec::new();

            for i in 0..n_points {
                let mut attempts = 0;
                let max_attempts = 1000;

                loop {
                    let x = rng.random_range(0.0..domain_size);
                    let y = rng.random_range(0.0..domain_size);

                    // Check minimum distance to existing points
                    let mut valid = true;
                    for &(px, py) in &placed_points {
                        let dist = ((x - px).powi(2) + (y - py).powi(2)).sqrt();
                        if dist < min_distance {
                            valid = false;
                            break;
                        }
                    }

                    if valid || attempts > max_attempts {
                        points[[i, 0]] = x;
                        points[[i, 1]] = y;
                        placed_points.push((x, y));
                        break;
                    }

                    attempts += 1;
                }
            }
        }

        _ => {
            return Err(SklearsError::InvalidInput(format!(
                "Unknown pattern_type: {}. Use 'random', 'clustered', 'regular', or 'inhibited'",
                pattern_type
            )));
        }
    }

    Ok(points)
}

/// Generate geostatistical data with spatial correlation
///
/// Creates spatially correlated data following a specified covariance structure.
/// This is useful for testing spatial interpolation and geostatistical methods.
///
/// # Parameters
/// - `n_points`: Number of spatial locations
/// - `domain_size`: Size of the spatial domain
/// - `correlation_range`: Range parameter for spatial correlation
/// - `nugget`: Nugget effect (measurement error variance)
/// - `sill`: Sill parameter (total variance)
/// - `correlation_function`: Type of correlation function ("exponential", "gaussian", "spherical")
/// - `random_state`: Random seed for reproducibility
///
/// # Returns
/// Tuple of (spatial_coordinates, correlated_values)
pub fn make_geostatistical_data(
    n_points: usize,
    domain_size: f64,
    correlation_range: f64,
    nugget: f64,
    sill: f64,
    correlation_function: &str,
    random_state: Option<u64>,
) -> Result<(Array2<f64>, Array1<f64>)> {
    if n_points == 0 {
        return Err(SklearsError::InvalidInput(
            "n_points must be positive".to_string(),
        ));
    }

    if domain_size <= 0.0 || correlation_range <= 0.0 || sill <= 0.0 {
        return Err(SklearsError::InvalidInput(
            "domain_size, correlation_range, and sill must be positive".to_string(),
        ));
    }

    let mut rng = Random::from_seed(random_state.unwrap_or_else(|| rng().gen()));

    // Generate spatial coordinates
    let mut coordinates = Array2::zeros((n_points, 2));
    for i in 0..n_points {
        coordinates[[i, 0]] = rng.random_range(0.0..domain_size);
        coordinates[[i, 1]] = rng.random_range(0.0..domain_size);
    }

    // Calculate correlation matrix
    let mut correlation_matrix = Array2::zeros((n_points, n_points));
    for i in 0..n_points {
        for j in 0..n_points {
            let dx = coordinates[[i, 0]] - coordinates[[j, 0]];
            let dy = coordinates[[i, 1]] - coordinates[[j, 1]];
            let distance = (dx * dx + dy * dy).sqrt();

            let correlation = match correlation_function {
                "exponential" => {
                    if i == j {
                        sill
                    } else {
                        (sill - nugget) * (-distance / correlation_range).exp() +
                        if i == j { nugget } else { 0.0 }
                    }
                }
                "gaussian" => {
                    if i == j {
                        sill
                    } else {
                        (sill - nugget) * (-(distance / correlation_range).powi(2)).exp() +
                        if i == j { nugget } else { 0.0 }
                    }
                }
                "spherical" => {
                    if i == j {
                        sill
                    } else if distance <= correlation_range {
                        let h_over_a = distance / correlation_range;
                        (sill - nugget) * (1.0 - 1.5 * h_over_a + 0.5 * h_over_a.powi(3))
                    } else {
                        nugget
                    }
                }
                _ => {
                    return Err(SklearsError::InvalidInput(format!(
                        "Unknown correlation_function: {}. Use 'exponential', 'gaussian', or 'spherical'",
                        correlation_function
                    )));
                }
            };

            correlation_matrix[[i, j]] = correlation;
        }
    }

    // Generate correlated values using Cholesky decomposition (simplified)
    let mut values = Array1::zeros(n_points);
    for i in 0..n_points {
        values[i] = rng.sample(StandardNormal);
    }

    // Apply correlation structure (simplified approach)
    let mut correlated_values = Array1::zeros(n_points);
    for i in 0..n_points {
        let mut sum = 0.0;
        for j in 0..=i {
            sum += correlation_matrix[[i, j]].sqrt() * values[j];
        }
        correlated_values[i] = sum / (i + 1) as f64; // Simplified normalization
    }

    Ok((coordinates, correlated_values))
}