rssn 0.2.9

A comprehensive scientific computing library for Rust, aiming for feature parity with NumPy and SymPy.
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
//! # Numerical Input/Output Utilities
//!
//! This module provides input/output utilities for numerical data, primarily focusing
//! on reading and writing `ndarray` arrays to/from `.npy` files. It also includes
//! functions to convert between `Expr::Matrix` and `ndarray::Array2<f64>` for seamless
//! integration with symbolic and numerical computations.

use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Write;
use std::path::Path;

use ndarray::Array2;
#[cfg(feature = "output")]
use ndarray_npy::read_npy;
#[cfg(feature = "output")]
use ndarray_npy::write_npy;
use serde_json;

use crate::prelude::Expr;

/// Writes a 2D `ndarray::Array` to a `.npy` file.
///
/// # Arguments
/// * `filename` - The path to the `.npy` file.
/// * `arr` - The array to write.
///
/// # Errors
///
/// This function will return an error if the file cannot be written to.
///
/// # Panics
/// Panics if the write fails.
#[cfg(feature = "output")]
pub fn write_npy_file<P: AsRef<Path>>(
    filename: P,
    arr: &Array2<f64>,
) -> Result<(), String> {
    write_npy(filename, arr).map_err(|e| e.to_string())
}

#[cfg(not(feature = "output"))]
pub fn write_npy_file<P: AsRef<Path>>(
    _filename: P,
    _arr: &Array2<f64>,
) -> Result<(), String> {
    Err("Feature 'output' is required \
         for .npy support"
        .to_string())
}

/// Reads a 2D `ndarray::Array` from a `.npy` file.
///
/// # Arguments
/// * `filename` - The path to the `.npy` file.
///
/// # Returns
/// The read array as an `ndarray::Array2<f64>`.
///
/// # Errors
///
/// This function will return an error if the file cannot be read.
///
/// # Panics
/// Panics if the read fails.
#[cfg(feature = "output")]
pub fn read_npy_file<P: AsRef<Path>>(filename: P) -> Result<Array2<f64>, String> {
    read_npy(filename).map_err(|e| e.to_string())
}

#[cfg(not(feature = "output"))]
pub fn read_npy_file<P: AsRef<Path>>(_filename: P) -> Result<Array2<f64>, String> {
    Err("Feature 'output' is required \
         for .npy support"
        .to_string())
}

/// Writes a 2D `ndarray::Array` to a CSV file.
///
/// # Errors
///
/// This function will return an error if the file cannot be written to.
pub fn write_csv_file<P: AsRef<Path>>(
    filename: P,
    arr: &Array2<f64>,
) -> Result<(), String> {
    let mut file = File::create(filename).map_err(|e| e.to_string())?;

    for row in arr.outer_iter() {
        let line = row
            .iter()
            .map(|&v| v.to_string())
            .collect::<Vec<_>>()
            .join(",");

        writeln!(file, "{line}").map_err(|e| e.to_string())?;
    }

    Ok(())
}

/// Reads a 2D `ndarray::Array` from a CSV file.
///
/// # Errors
///
/// This function will return an error if the file cannot be read or parsed.
pub fn read_csv_file<P: AsRef<Path>>(filename: P) -> Result<Array2<f64>, String> {
    let file = File::open(filename).map_err(|e| e.to_string())?;

    let reader = BufReader::new(file);

    let mut data = Vec::new();

    let mut rows = 0;

    let mut cols = 0;

    for line_res in reader.lines() {
        let line = line_res.map_err(|e| e.to_string())?;

        let line = line.trim();

        if line.is_empty() {
            continue;
        }

        let row_data: Vec<f64> = line
            .split(',')
            .map(|s| s.trim().parse::<f64>().map_err(|e| e.to_string()))
            .collect::<Result<Vec<_>, String>>()?;

        if rows == 0 {
            cols = row_data.len();
        } else if row_data.len() != cols {
            return Err("Inconsistent column \
                 count in CSV"
                .to_string());
        }

        data.extend(row_data);

        rows += 1;
    }

    if rows == 0 {
        return Ok(Array2::zeros((0, 0)));
    }

    Array2::from_shape_vec((rows, cols), data).map_err(|e| e.to_string())
}

/// Writes a 2D `ndarray::Array` to a JSON file.
///
/// # Errors
///
/// This function will return an error if the file cannot be written to.
pub fn write_json_file<P: AsRef<Path>>(
    filename: P,
    arr: &Array2<f64>,
) -> Result<(), String> {
    let file = File::create(filename).map_err(|e| e.to_string())?;

    serde_json::to_writer_pretty(file, arr).map_err(|e| e.to_string())
}

/// Reads a 2D `ndarray::Array` from a JSON file.
///
/// # Errors
///
/// This function will return an error if the file cannot be read or parsed.
pub fn read_json_file<P: AsRef<Path>>(filename: P) -> Result<Array2<f64>, String> {
    let file = File::open(filename).map_err(|e| e.to_string())?;

    let reader = BufReader::new(file);

    serde_json::from_reader(reader).map_err(|e| e.to_string())
}

#[cfg(test)]
mod tests {

    use std::fs;

    use ndarray::array;
    use proptest::prelude::*;

    use super::*;

    #[test]
    fn test_write_read_npy() {
        #[cfg(feature = "output")]
        {
            let arr = array![[1.0, 2.0], [3.0, 4.0]];

            let filename = "test_array.npy";

            let _ = write_npy_file(filename, &arr).unwrap();

            let read_arr = read_npy_file(filename).unwrap();

            assert_eq!(arr, read_arr);

            let _ = fs::remove_file(filename);
        }
    }

    #[test]
    fn test_write_read_csv() {
        let arr = array![[1.1, 2.2], [3.3, 4.4]];

        let filename = "test_array.csv";

        write_csv_file(filename, &arr).unwrap();

        let read_arr = read_csv_file(filename).unwrap();

        assert_eq!(arr.shape(), read_arr.shape());

        for (a, b) in arr.iter().zip(read_arr.iter()) {
            assert!((a - b).abs() < 1e-10);
        }

        let _ = fs::remove_file(filename);
    }

    #[test]
    fn test_write_read_json() {
        let arr = array![[1.0, 2.0], [3.0, 4.0]];

        let filename = "test_array.json";

        write_json_file(filename, &arr).unwrap();

        let read_arr = read_json_file(filename).unwrap();

        assert_eq!(arr.shape(), read_arr.shape());

        for (a, b) in arr.iter().zip(read_arr.iter()) {
            assert!((a - b).abs() < 1e-10);
        }

        let _ = fs::remove_file(filename);
    }

    proptest! {
    #[test]
            fn prop_csv_roundtrip(
                rows in 1..20usize,
                cols in 1..20usize,
                data in prop::collection::vec(0.1..100.0f64, 1..400)
            ) {
                prop_assume!(data.len() >= rows * cols);
                let actual_data = data.iter().take(rows * cols).cloned().collect::<Vec<_>>();
                let arr = Array2::from_shape_vec((rows, cols), actual_data).unwrap();
                let filename = "test_prop_csv.csv";
                write_csv_file(filename, &arr).unwrap();
                let read_arr = read_csv_file(filename).unwrap();

                assert_eq!(arr.shape(), read_arr.shape());
                for (a, b) in arr.iter().zip(read_arr.iter()) {
                    assert!((a - b).abs() < 1e-10);
                }
                let _ = fs::remove_file(filename);
            }

    #[test]
            fn prop_json_roundtrip(
                rows in 1..20usize,
                cols in 1..20usize,
                data in prop::collection::vec(0.1..100.0f64, 1..400)
            ) {
                prop_assume!(data.len() >= rows * cols);
                let actual_data = data.iter().take(rows * cols).cloned().collect::<Vec<_>>();
                let arr = Array2::from_shape_vec((rows, cols), actual_data).unwrap();
                let filename = "test_prop_json.json";
                write_json_file(filename, &arr).unwrap();
                let read_arr = read_json_file(filename).unwrap();

                assert_eq!(arr.shape(), read_arr.shape());
                for (a, b) in arr.iter().zip(read_arr.iter()) {
                    assert!((a - b).abs() < 1e-10);
                }
                let _ = fs::remove_file(filename);
            }
        }

    #[test]
    fn test_dispatchers() {
        let arr = array![[1.0, 2.0], [3.0, 4.0]];

        let expr = Expr::Matrix(vec![
            vec![Expr::Constant(1.0), Expr::Constant(2.0)],
            vec![Expr::Constant(3.0), Expr::Constant(4.0)],
        ]);

        // CSV
        let csv_file = "test_dispatch.csv";

        save_expr(csv_file, &expr).unwrap();

        let loaded_csv = load_expr(csv_file).unwrap();

        if let Expr::Matrix(rows) = loaded_csv {
            assert_eq!(rows.len(), 2);

            assert_eq!(rows[0][0].to_f64().unwrap(), 1.0);
        }

        let _ = fs::remove_file(csv_file);

        // JSON
        let json_file = "test_dispatch.json";

        save_expr(json_file, &expr).unwrap();

        let loaded_json = load_expr(json_file).unwrap();

        assert_eq!(expr, loaded_json);

        let _ = fs::remove_file(json_file);

        #[cfg(feature = "output")]
        {
            // NPY
            let npy_file = "test_dispatch.npy";

            save_expr(npy_file, &expr).unwrap();

            let loaded_npy = load_expr(npy_file).unwrap();

            assert_eq!(expr, loaded_npy);

            let _ = fs::remove_file(npy_file);
        }
    }
}

/// Converts an `Expr::Matrix` to an `ndarray::Array2<f64>` and saves it as a `.npy` file.
///
/// This function acts as a bridge to the existing `ndarray-npy` functionality.
///
/// # Arguments
/// * `path` - The path to the `.npy` file.
/// * `matrix_expr` - The `Expr::Matrix` to save.
///
/// # Returns
/// A `Result` indicating success or an error string if the input is not a matrix
/// or contains non-numerical elements.
///
/// # Errors
///
/// This function will return an error if the input expression is not a matrix,
/// contains non-numerical elements, or if the file write operation fails.
pub fn save_expr_as_npy<P: AsRef<Path>>(
    path: P,
    matrix_expr: &Expr,
) -> Result<(), String> {
    if let Expr::Matrix(rows) = matrix_expr {
        if rows.is_empty() {
            let arr: Array2<f64> = Array2::zeros((0, 0));

            write_npy_file(path, &arr)?;

            return Ok(());
        }

        let num_rows = rows.len();

        let num_cols = rows[0].len();

        let mut arr = Array2::zeros((num_rows, num_cols));

        for (i, row) in rows.iter().enumerate() {
            if row.len() != num_cols {
                return Err("All rows must \
                     have the same \
                     number of columns"
                    .to_string());
            }

            for (j, elem) in row.iter().enumerate() {
                let val = elem
                    .to_f64()
                    .ok_or_else(|| format!("Matrix element at ({i},{j}) is not a number"))?;

                arr[[i, j]] = val;
            }
        }

        write_npy_file(path, &arr)?;

        Ok(())
    } else {
        Err("Input expression is not \
             a matrix"
            .to_string())
    }
}

/// Converts an `Expr::Matrix` to an `ndarray::Array2<f64>` and saves it as a CSV file.
///
/// # Errors
///
/// This function will return an error if the expression is not a matrix or
/// if the file cannot be written to.
pub fn save_expr_as_csv<P: AsRef<Path>>(
    path: P,
    matrix_expr: &Expr,
) -> Result<(), String> {
    if let Expr::Matrix(rows) = matrix_expr {
        if rows.is_empty() {
            let arr: Array2<f64> = Array2::zeros((0, 0));

            return write_csv_file(path, &arr);
        }

        let num_rows = rows.len();

        let num_cols = rows[0].len();

        let mut arr = Array2::zeros((num_rows, num_cols));

        for (i, row) in rows.iter().enumerate() {
            if row.len() != num_cols {
                return Err("Inconsistent \
                     columns"
                    .to_string());
            }

            for (j, elem) in row.iter().enumerate() {
                arr[[i, j]] = elem.to_f64().ok_or("Not a number")?;
            }
        }

        write_csv_file(path, &arr)
    } else {
        Err("Not a matrix".to_string())
    }
}

/// Reads a CSV file into an `ndarray::Array2<f64>` and converts it to an `Expr::Matrix`.
///
/// # Errors
///
/// This function will return an error if the file cannot be read or parsed.
pub fn load_csv_as_expr<P: AsRef<Path>>(path: P) -> Result<Expr, String> {
    let arr = read_csv_file(path)?;

    let mut rows = Vec::new();

    for row in arr.outer_iter() {
        rows.push(row.iter().map(|&v| Expr::Constant(v)).collect());
    }

    Ok(Expr::Matrix(rows))
}

/// Converts an `Expr::Matrix` to an `ndarray::Array2<f64>` and saves it as a JSON file.
///
/// # Errors
///
/// This function will return an error if the expression is not a matrix or
/// if the file cannot be written to.
pub fn save_expr_as_json<P: AsRef<Path>>(
    path: P,
    matrix_expr: &Expr,
) -> Result<(), String> {
    if let Expr::Matrix(rows) = matrix_expr {
        if rows.is_empty() {
            let arr: Array2<f64> = Array2::zeros((0, 0));

            return write_json_file(path, &arr);
        }

        let num_rows = rows.len();

        let num_cols = rows[0].len();

        let mut arr = Array2::zeros((num_rows, num_cols));

        for (i, row) in rows.iter().enumerate() {
            if row.len() != num_cols {
                return Err("Inconsistent \
                     columns"
                    .to_string());
            }

            for (j, elem) in row.iter().enumerate() {
                arr[[i, j]] = elem.to_f64().ok_or("Not a number")?;
            }
        }

        write_json_file(path, &arr)
    } else {
        Err("Not a matrix".to_string())
    }
}

/// Reads a JSON file into an `ndarray::Array2<f64>` and converts it to an `Expr::Matrix`.
///
/// # Errors
///
/// This function will return an error if the file cannot be read or parsed.
pub fn load_json_as_expr<P: AsRef<Path>>(path: P) -> Result<Expr, String> {
    let arr = read_json_file(path)?;

    let mut rows = Vec::new();

    for row in arr.outer_iter() {
        rows.push(row.iter().map(|&v| Expr::Constant(v)).collect());
    }

    Ok(Expr::Matrix(rows))
}

/// Reads a `.npy` file into an `ndarray::Array2<f64>` and converts it to an `Expr::Matrix`.
///
/// # Arguments
/// * `path` - The path to the `.npy` file.
///
/// # Returns
/// A `Result` containing the `Expr::Matrix` representation of the loaded array,
/// or an error string if the read fails.
///
/// # Errors
///
/// This function will return an error if the file cannot be read or if the
/// data cannot be converted to an `Expr::Matrix`.
pub fn load_npy_as_expr<P: AsRef<Path>>(path: P) -> Result<Expr, String> {
    let arr = read_npy_file(path)?;

    let mut rows = Vec::new();

    for row in arr.outer_iter() {
        let mut expr_row = Vec::new();

        for val in row {
            expr_row.push(Expr::Constant(*val));
        }

        rows.push(expr_row);
    }

    Ok(Expr::Matrix(rows))
}

/// Automatically dispatches to the correct saver based on the file extension.
///
/// # Errors
///
/// This function will return an error if the file extension is not supported
/// or if the save operation fails.
pub fn save_expr<P: AsRef<Path>>(
    path: P,
    expr: &Expr,
) -> Result<(), String> {
    let path_ref = path.as_ref();

    let extension = path_ref
        .extension()
        .and_then(|s| s.to_str())
        .map(str::to_lowercase)
        .unwrap_or_default();

    match extension.as_str() {
        | "npy" => save_expr_as_npy(path, expr),
        | "csv" => save_expr_as_csv(path, expr),
        | "json" => save_expr_as_json(path, expr),
        | _ => {
            Err(format!(
                "Unsupported file \
                 format: .{extension}"
            ))
        },
    }
}

/// Automatically dispatches to the correct loader based on the file extension.
///
/// # Errors
///
/// This function will return an error if the file extension is not supported
/// or if the load operation fails.
pub fn load_expr<P: AsRef<Path>>(path: P) -> Result<Expr, String> {
    let path_ref = path.as_ref();

    let extension = path_ref
        .extension()
        .and_then(|s| s.to_str())
        .map(str::to_lowercase)
        .unwrap_or_default();

    match extension.as_str() {
        | "npy" => load_npy_as_expr(path),
        | "csv" => load_csv_as_expr(path),
        | "json" => load_json_as_expr(path),
        | _ => {
            Err(format!(
                "Unsupported file \
                 format: .{extension}"
            ))
        },
    }
}