numrs2 0.3.2

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! Comprehensive tests for NumRS2 I/O functionality
//!
//! This test suite covers all I/O functions including text file I/O (loadtxt, savetxt, genfromtxt),
//! binary file I/O (NPY/NPZ), and various serialization formats.

use numrs2::io::text::*;
use numrs2::io::SerializeFormat;
use numrs2::prelude::*;
use std::fs;
use std::io::Write;
use tempfile::NamedTempFile;

#[test]
fn test_loadtxt_basic_functionality() {
    // Create a temporary file with test data
    let mut temp_file = NamedTempFile::new().unwrap();
    writeln!(temp_file, "1.0 2.0 3.0").unwrap();
    writeln!(temp_file, "4.0 5.0 6.0").unwrap();
    writeln!(temp_file, "7.0 8.0 9.0").unwrap();

    let array = loadtxt::<f64>(temp_file.path(), LoadTxtOptions::default()).unwrap();
    assert_eq!(array.shape(), &[3, 3]);
    assert_eq!(
        array.to_vec(),
        vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
    );
}

#[test]
fn test_loadtxt_with_comments_and_skip() {
    let mut temp_file = NamedTempFile::new().unwrap();
    writeln!(temp_file, "# This is a header comment").unwrap();
    writeln!(temp_file, "# x y z").unwrap();
    writeln!(temp_file, "1.0 2.0 3.0").unwrap();
    writeln!(temp_file, "4.0 5.0 6.0").unwrap();
    writeln!(temp_file, "# This is a comment in the middle").unwrap();
    writeln!(temp_file, "7.0 8.0 9.0").unwrap();

    let options = LoadTxtOptions {
        skiprows: 0, // Don't skip any rows, just ignore comments
        ..Default::default()
    };

    let array = loadtxt::<f64>(temp_file.path(), options).unwrap();
    assert_eq!(array.shape(), &[3, 3]);
    assert_eq!(
        array.to_vec(),
        vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
    );
}

#[test]
fn test_loadtxt_with_delimiter() {
    let mut temp_file = NamedTempFile::new().unwrap();
    writeln!(temp_file, "1.0,2.0,3.0").unwrap();
    writeln!(temp_file, "4.0,5.0,6.0").unwrap();

    let options = LoadTxtOptions {
        delimiter: Some(",".to_string()),
        ..Default::default()
    };

    let array = loadtxt::<f64>(temp_file.path(), options).unwrap();
    assert_eq!(array.shape(), &[2, 3]);
    assert_eq!(array.to_vec(), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
}

#[test]
fn test_loadtxt_with_usecols() {
    let mut temp_file = NamedTempFile::new().unwrap();
    writeln!(temp_file, "1.0 2.0 3.0 4.0").unwrap();
    writeln!(temp_file, "5.0 6.0 7.0 8.0").unwrap();

    let options = LoadTxtOptions {
        usecols: Some(vec![0, 2]), // Only columns 0 and 2
        ..Default::default()
    };

    let array = loadtxt::<f64>(temp_file.path(), options).unwrap();
    assert_eq!(array.shape(), &[2, 2]);
    assert_eq!(array.to_vec(), vec![1.0, 3.0, 5.0, 7.0]);
}

#[test]
fn test_loadtxt_with_max_rows() {
    let mut temp_file = NamedTempFile::new().unwrap();
    writeln!(temp_file, "1.0 2.0").unwrap();
    writeln!(temp_file, "3.0 4.0").unwrap();
    writeln!(temp_file, "5.0 6.0").unwrap();
    writeln!(temp_file, "7.0 8.0").unwrap();

    let options = LoadTxtOptions {
        max_rows: Some(2),
        ..Default::default()
    };

    let array = loadtxt::<f64>(temp_file.path(), options).unwrap();
    assert_eq!(array.shape(), &[2, 2]);
    assert_eq!(array.to_vec(), vec![1.0, 2.0, 3.0, 4.0]);
}

#[test]
fn test_loadtxt_integer_types() {
    let mut temp_file = NamedTempFile::new().unwrap();
    writeln!(temp_file, "1 2 3").unwrap();
    writeln!(temp_file, "4 5 6").unwrap();

    let array = loadtxt::<i32>(temp_file.path(), LoadTxtOptions::default()).unwrap();
    assert_eq!(array.shape(), &[2, 3]);
    assert_eq!(array.to_vec(), vec![1, 2, 3, 4, 5, 6]);
}

#[test]
fn test_savetxt_basic_functionality() {
    let array = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0]).reshape(&[2, 2]);
    let temp_file = NamedTempFile::new().unwrap();

    savetxt(temp_file.path(), &array, SaveTxtOptions::default()).unwrap();

    let content = fs::read_to_string(temp_file.path()).unwrap();
    let lines: Vec<&str> = content.trim().split('\n').collect();
    assert_eq!(lines.len(), 2);
    assert!(lines[0].contains("1") && lines[0].contains("2"));
    assert!(lines[1].contains("3") && lines[1].contains("4"));
}

#[test]
fn test_savetxt_with_delimiter() {
    let array = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0]).reshape(&[2, 2]);
    let temp_file = NamedTempFile::new().unwrap();

    let options = SaveTxtOptions {
        delimiter: ",".to_string(),
        ..Default::default()
    };

    savetxt(temp_file.path(), &array, options).unwrap();

    let content = fs::read_to_string(temp_file.path()).unwrap();
    assert!(content.contains(","));
}

#[test]
fn test_savetxt_with_header_footer() {
    let array = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0]).reshape(&[2, 2]);
    let temp_file = NamedTempFile::new().unwrap();

    let options = SaveTxtOptions {
        header: Some("x y".to_string()),
        footer: Some("End of data".to_string()),
        ..Default::default()
    };

    savetxt(temp_file.path(), &array, options).unwrap();

    let content = fs::read_to_string(temp_file.path()).unwrap();
    assert!(content.contains("# x y"));
    assert!(content.contains("# End of data"));
}

#[test]
fn test_savetxt_1d_array() {
    let array = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0]);
    let temp_file = NamedTempFile::new().unwrap();

    savetxt(temp_file.path(), &array, SaveTxtOptions::default()).unwrap();

    let content = fs::read_to_string(temp_file.path()).unwrap();
    let lines: Vec<&str> = content.trim().split('\n').collect();
    assert_eq!(lines.len(), 4); // 1D array should be saved as column vector
}

#[test]
fn test_genfromtxt_with_missing_values() {
    let mut temp_file = NamedTempFile::new().unwrap();
    writeln!(temp_file, "1.0 2.0 3.0").unwrap();
    writeln!(temp_file, "4.0 nan 6.0").unwrap();
    writeln!(temp_file, "7.0 8.0 N/A").unwrap();
    writeln!(temp_file, "10.0 NULL 12.0").unwrap();

    let array = genfromtxt::<f64>(temp_file.path(), GenFromTxtOptions::default()).unwrap();
    assert_eq!(array.shape(), &[4, 3]);
    // Missing values should be replaced with 0.0
    let expected = vec![1.0, 2.0, 3.0, 4.0, 0.0, 6.0, 7.0, 8.0, 0.0, 10.0, 0.0, 12.0];
    assert_eq!(array.to_vec(), expected);
}

#[test]
fn test_genfromtxt_with_skip_header_footer() {
    let mut temp_file = NamedTempFile::new().unwrap();
    writeln!(temp_file, "Header line 1").unwrap();
    writeln!(temp_file, "Header line 2").unwrap();
    writeln!(temp_file, "1.0 2.0").unwrap();
    writeln!(temp_file, "3.0 4.0").unwrap();
    writeln!(temp_file, "Footer line").unwrap();

    let options = GenFromTxtOptions {
        skip_header: 2,
        skip_footer: 1,
        ..Default::default()
    };

    let array = genfromtxt::<f64>(temp_file.path(), options).unwrap();
    assert_eq!(array.shape(), &[2, 2]);
    assert_eq!(array.to_vec(), vec![1.0, 2.0, 3.0, 4.0]);
}

#[test]
fn test_genfromtxt_with_custom_missing_values() {
    let mut temp_file = NamedTempFile::new().unwrap();
    writeln!(temp_file, "1.0 2.0 3.0").unwrap();
    writeln!(temp_file, "4.0 MISSING 6.0").unwrap();
    writeln!(temp_file, "7.0 8.0 EMPTY").unwrap();

    let options = GenFromTxtOptions {
        default_missing: vec!["MISSING".to_string(), "EMPTY".to_string()],
        ..Default::default()
    };

    let array = genfromtxt::<f64>(temp_file.path(), options).unwrap();
    assert_eq!(array.shape(), &[3, 3]);
    let expected = vec![1.0, 2.0, 3.0, 4.0, 0.0, 6.0, 7.0, 8.0, 0.0];
    assert_eq!(array.to_vec(), expected);
}

#[test]
fn test_detect_delimiter() {
    // Test CSV format detection
    let mut temp_file = NamedTempFile::new().unwrap();
    writeln!(temp_file, "1,2,3").unwrap();
    writeln!(temp_file, "4,5,6").unwrap();
    writeln!(temp_file, "7,8,9").unwrap();

    let delimiter = detect_delimiter(temp_file.path(), Some(3)).unwrap();
    assert_eq!(delimiter, ",");

    // Test tab-separated format detection
    let mut temp_file2 = NamedTempFile::new().unwrap();
    writeln!(temp_file2, "1\t2\t3").unwrap();
    writeln!(temp_file2, "4\t5\t6").unwrap();

    let delimiter2 = detect_delimiter(temp_file2.path(), Some(2)).unwrap();
    assert_eq!(delimiter2, "\t");
}

#[test]
fn test_npy_format_f64() {
    let array = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0]).reshape(&[2, 2]);
    let temp_file = NamedTempFile::new().unwrap();

    // Save as NPY
    array
        .to_file(temp_file.path(), SerializeFormat::Npy)
        .unwrap();

    // Load back from NPY
    let loaded_array = Array::<f64>::from_file(temp_file.path(), SerializeFormat::Npy).unwrap();
    assert_eq!(loaded_array.shape(), array.shape());
    assert_eq!(loaded_array.to_vec(), array.to_vec());
}

#[test]
fn test_npy_format_i32() {
    let array = Array::from_vec(vec![1, 2, 3, 4]).reshape(&[2, 2]);
    let temp_file = NamedTempFile::new().unwrap();

    // Save as NPY
    array
        .to_file(temp_file.path(), SerializeFormat::Npy)
        .unwrap();

    // Load back from NPY
    let loaded_array = Array::<i32>::from_file(temp_file.path(), SerializeFormat::Npy).unwrap();
    assert_eq!(loaded_array.shape(), array.shape());
    assert_eq!(loaded_array.to_vec(), array.to_vec());
}

#[test]
fn test_npy_format_bool() {
    let array = Array::from_vec(vec![true, false, true, false]).reshape(&[2, 2]);
    let temp_file = NamedTempFile::new().unwrap();

    // Save as NPY
    array
        .to_file(temp_file.path(), SerializeFormat::Npy)
        .unwrap();

    // Load back from NPY
    let loaded_array = Array::<bool>::from_file(temp_file.path(), SerializeFormat::Npy).unwrap();
    assert_eq!(loaded_array.shape(), array.shape());
    assert_eq!(loaded_array.to_vec(), array.to_vec());
}

#[test]
fn test_npy_format_u8() {
    let array = Array::from_vec(vec![1u8, 2u8, 3u8, 4u8]).reshape(&[2, 2]);
    let temp_file = NamedTempFile::new().unwrap();

    // Save as NPY
    array
        .to_file(temp_file.path(), SerializeFormat::Npy)
        .unwrap();

    // Load back from NPY
    let loaded_array = Array::<u8>::from_file(temp_file.path(), SerializeFormat::Npy).unwrap();
    assert_eq!(loaded_array.shape(), array.shape());
    assert_eq!(loaded_array.to_vec(), array.to_vec());
}

#[test]
fn test_npz_format_f64() {
    let array = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0]).reshape(&[2, 2]);
    let temp_file = NamedTempFile::new().unwrap();

    // Save as NPZ
    array
        .to_file(temp_file.path(), SerializeFormat::Npz)
        .unwrap();

    // Load back from NPZ
    let loaded_array = Array::<f64>::from_file(temp_file.path(), SerializeFormat::Npz).unwrap();
    assert_eq!(loaded_array.shape(), array.shape());
    assert_eq!(loaded_array.to_vec(), array.to_vec());
}

#[test]
fn test_json_serialization() {
    let array = Array::from_vec(vec![1, 2, 3, 4]).reshape(&[2, 2]);
    let temp_file = NamedTempFile::new().unwrap();

    // Save as JSON
    array
        .to_file(temp_file.path(), SerializeFormat::Json)
        .unwrap();

    // Load back from JSON
    let loaded_array = Array::<i32>::from_file(temp_file.path(), SerializeFormat::Json).unwrap();
    assert_eq!(loaded_array.shape(), array.shape());
    assert_eq!(loaded_array.to_vec(), array.to_vec());
}

#[test]
fn test_csv_serialization() {
    let array = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0]).reshape(&[2, 2]);
    let temp_file = NamedTempFile::new().unwrap();

    // Save as CSV
    array
        .to_file(temp_file.path(), SerializeFormat::Csv)
        .unwrap();

    // Load back from CSV
    let loaded_array = Array::<f64>::from_file(temp_file.path(), SerializeFormat::Csv).unwrap();

    assert_eq!(loaded_array.shape(), array.shape());
    assert_eq!(loaded_array.to_vec(), array.to_vec());
}

#[test]
#[ignore = "Binary serialization I/O issue unrelated to SCIRS2 migration"]
fn test_binary_serialization() {
    let array = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0]).reshape(&[2, 2]);
    let temp_file = NamedTempFile::new().unwrap();

    // Save as Binary
    array
        .to_file(temp_file.path(), SerializeFormat::Binary)
        .unwrap();

    // Load back from Binary
    let loaded_array = Array::<f64>::from_file(temp_file.path(), SerializeFormat::Binary).unwrap();
    assert_eq!(loaded_array.shape(), array.shape());
    assert_eq!(loaded_array.to_vec(), array.to_vec());
}

#[test]
fn test_roundtrip_different_shapes() {
    // Test 1D array
    let array_1d = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0]);
    let temp_file_1d = NamedTempFile::new().unwrap();
    array_1d
        .to_file(temp_file_1d.path(), SerializeFormat::Npy)
        .unwrap();
    let loaded_1d = Array::<f64>::from_file(temp_file_1d.path(), SerializeFormat::Npy).unwrap();
    assert_eq!(loaded_1d.shape(), array_1d.shape());
    assert_eq!(loaded_1d.to_vec(), array_1d.to_vec());

    // Test 3D array
    let array_3d =
        Array::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]).reshape(&[2, 2, 2]);
    let temp_file_3d = NamedTempFile::new().unwrap();
    array_3d
        .to_file(temp_file_3d.path(), SerializeFormat::Npy)
        .unwrap();
    let loaded_3d = Array::<f64>::from_file(temp_file_3d.path(), SerializeFormat::Npy).unwrap();
    assert_eq!(loaded_3d.shape(), array_3d.shape());
    assert_eq!(loaded_3d.to_vec(), array_3d.to_vec());
}

#[test]
fn test_large_array_io() {
    // Test with a larger array to ensure memory efficiency
    let size = 1000;
    let data: Vec<f64> = (0..size).map(|i| i as f64).collect();
    let array = Array::from_vec(data.clone()).reshape(&[10, 100]);
    let temp_file = NamedTempFile::new().unwrap();

    // Save and load
    array
        .to_file(temp_file.path(), SerializeFormat::Npy)
        .unwrap();
    let loaded_array = Array::<f64>::from_file(temp_file.path(), SerializeFormat::Npy).unwrap();

    assert_eq!(loaded_array.shape(), array.shape());
    assert_eq!(loaded_array.to_vec(), data);
}

#[test]
fn test_pickle_serialization() {
    let array = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0]).reshape(&[2, 2]);
    let temp_file = NamedTempFile::new().unwrap();

    // Save as Pickle
    array
        .to_file(temp_file.path(), SerializeFormat::Pickle)
        .unwrap();

    // Load back from Pickle
    let loaded_array = Array::<f64>::from_file(temp_file.path(), SerializeFormat::Pickle).unwrap();
    assert_eq!(loaded_array.shape(), array.shape());
    assert_eq!(loaded_array.to_vec(), array.to_vec());
}

#[test]
fn test_pickle_different_types() {
    // Test with integers
    let int_array = Array::from_vec(vec![1, 2, 3, 4]).reshape(&[2, 2]);
    let temp_file_int = NamedTempFile::new().unwrap();

    int_array
        .to_file(temp_file_int.path(), SerializeFormat::Pickle)
        .unwrap();
    let loaded_int =
        Array::<i32>::from_file(temp_file_int.path(), SerializeFormat::Pickle).unwrap();
    assert_eq!(loaded_int.shape(), int_array.shape());
    assert_eq!(loaded_int.to_vec(), int_array.to_vec());

    // Test with booleans
    let bool_array = Array::from_vec(vec![true, false, true, false]).reshape(&[2, 2]);
    let temp_file_bool = NamedTempFile::new().unwrap();

    bool_array
        .to_file(temp_file_bool.path(), SerializeFormat::Pickle)
        .unwrap();
    let loaded_bool =
        Array::<bool>::from_file(temp_file_bool.path(), SerializeFormat::Pickle).unwrap();
    assert_eq!(loaded_bool.shape(), bool_array.shape());
    assert_eq!(loaded_bool.to_vec(), bool_array.to_vec());
}

#[test]
fn test_pickle_large_array() {
    // Test with a larger array to ensure memory efficiency
    let size = 1000;
    let data: Vec<f64> = (0..size).map(|i| i as f64).collect();
    let array = Array::from_vec(data.clone()).reshape(&[10, 100]);
    let temp_file = NamedTempFile::new().unwrap();

    // Save and load
    array
        .to_file(temp_file.path(), SerializeFormat::Pickle)
        .unwrap();
    let loaded_array = Array::<f64>::from_file(temp_file.path(), SerializeFormat::Pickle).unwrap();

    assert_eq!(loaded_array.shape(), array.shape());
    assert_eq!(loaded_array.to_vec(), data);
}

#[test]
fn test_pickle_error_handling() {
    // Test that string serialization/deserialization is not supported
    let array = Array::from_vec(vec![1.0, 2.0]);
    let result = array.to_string(SerializeFormat::Pickle);
    assert!(result.is_err());

    let result = Array::<f64>::from_string("", SerializeFormat::Pickle);
    assert!(result.is_err());

    // Test loading invalid pickle file
    let mut temp_file = NamedTempFile::new().unwrap();
    writeln!(temp_file, "This is not a valid pickle file").unwrap();
    let result = Array::<f64>::from_file(temp_file.path(), SerializeFormat::Pickle);
    assert!(result.is_err());
}

#[test]
fn test_error_handling() {
    // Test invalid file path
    let array = Array::from_vec(vec![1.0, 2.0]);
    let result = array.to_file(
        std::path::Path::new("/invalid/path/file.npy"),
        SerializeFormat::Npy,
    );
    assert!(result.is_err());

    // Test loading non-existent file
    let result = Array::<f64>::from_file(
        std::path::Path::new("/non/existent/file.npy"),
        SerializeFormat::Npy,
    );
    assert!(result.is_err());

    // Test loading invalid NPY file
    let mut temp_file = NamedTempFile::new().unwrap();
    writeln!(temp_file, "This is not a valid NPY file").unwrap();
    let result = Array::<f64>::from_file(temp_file.path(), SerializeFormat::Npy);
    assert!(result.is_err());
}

#[test]
fn test_text_io_roundtrip() {
    // Create test data
    let array = Array::from_vec(vec![
        1.5,
        2.7,
        std::f64::consts::PI,
        4.0,
        5.5,
        std::f64::consts::TAU,
    ])
    .reshape(&[2, 3]);
    let temp_file = NamedTempFile::new().unwrap();

    // Save using savetxt
    let save_options = SaveTxtOptions {
        delimiter: " ".to_string(),
        ..Default::default()
    };
    savetxt(temp_file.path(), &array, save_options).unwrap();

    // Load using loadtxt
    let load_options = LoadTxtOptions {
        delimiter: None, // Use whitespace as delimiter
        ..Default::default()
    };
    let loaded_array = loadtxt::<f64>(temp_file.path(), load_options).unwrap();

    assert_eq!(loaded_array.shape(), array.shape());
    // Check that values are approximately equal (floating point precision)
    for (original, loaded) in array.to_vec().iter().zip(loaded_array.to_vec().iter()) {
        assert!((original - loaded).abs() < 1e-10);
    }
}