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
//! Easily read homogeneous CSV data into a 2D ndarray.
//!
//! ```rust
//! extern crate csv;
//! extern crate ndarray_csv;
//!
//! use csv::ReaderBuilder;
//! use ndarray_csv::read;
//! use std::fs::File;
//!
//! fn main() {
//!     let file = File::open("test.csv").expect("opening test.csv failed");
//!     let reader = ReaderBuilder::new().has_headers(false).from_reader(file);
//!     let array = read::<f64, _>((2, 3), reader).expect("read failed");
//!     assert_eq!(array.dim(), (2, 3));
//! }
//! ```
//!
//! This project uses [cargo-make](https://sagiegurari.github.io/cargo-make/) for builds; to build,
//! run `cargo make all`.
extern crate csv;
#[cfg(test)]
#[macro_use]
extern crate matches;
#[cfg_attr(test, macro_use(array))]
extern crate ndarray;
extern crate serde;

use csv::Reader;
use ndarray::Array2;
use std::io::Read;

#[derive(Debug)]
pub enum Error {
    Csv(csv::Error),
    TooFewRows {
        expected: usize,
        actual: usize,
    },
    // We don't want to read the whole file, so this only reports that there were too many rows
    TooManyRows {
        expected: usize,
    },
    TooFewColumns {
        at_row_index: usize,
        expected: usize,
        actual: usize,
    },
    // We don't want to read the whole row, so this only reports that there were too many columns
    TooManyColumns {
        at_row_index: usize,
        expected: usize,
    },
}

impl From<csv::Error> for Error {
    fn from(inner: csv::Error) -> Self {
        Error::Csv(inner)
    }
}

/// Read CSV data into a new ndarray with the given shape
pub fn read<A, R>(shape: (usize, usize), mut reader: Reader<R>) -> Result<Array2<A>, Error>
where
    R: Read,
    A: Copy,
    for<'de> A: serde::de::Deserialize<'de>,
{
    // This is okay because this fn will return an Err when it is unable to fill the entire array.
    // Since this array is only returned when this fn returns an Ok, the end user will never be able
    // to read unitialized memory.
    let mut array = unsafe { Array2::uninitialized(shape) };
    let (n_rows, n_columns) = shape;
    let mut max_row_read: usize = 0;
    for (r, row) in reader.deserialize().enumerate() {
        if r >= n_rows {
            return Err(Error::TooManyRows { expected: n_rows });
        }

        let row_vec: Vec<A> = row?;
        let mut max_column_read: usize = 0;
        for (c, value) in row_vec.into_iter().enumerate() {
            if c >= n_columns {
                return Err(Error::TooManyColumns {
                    at_row_index: r,
                    expected: n_columns,
                });
            }

            array[(r, c)] = value;
            max_column_read = c
        }

        let n_columns_read = max_column_read + 1;
        if n_columns_read < n_columns {
            return Err(Error::TooFewColumns {
                at_row_index: r,
                expected: n_columns,
                actual: n_columns_read,
            });
        }

        max_row_read = r;
    }
    let n_rows_read = max_row_read + 1;
    if n_rows_read < n_rows {
        Err(Error::TooFewRows {
            expected: n_rows,
            actual: n_rows_read,
        })
    } else {
        Ok(array)
    }
}

#[cfg(test)]
mod tests {
    use super::Error::*;
    use super::*;
    use csv::ReaderBuilder;
    use std::io::Cursor;

    fn in_memory_reader(content: &'static str) -> Reader<impl Read> {
        ReaderBuilder::new()
            .has_headers(false)
            .from_reader(Cursor::new(content))
    }

    fn test_reader() -> Reader<impl Read> {
        in_memory_reader("1,2,3\n4,5,6")
    }

    #[test]
    fn test_read_float() {
        let actual = read((2, 3), test_reader()).unwrap();
        let expected = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_read_integer() {
        let actual = read((2, 3), test_reader()).unwrap();
        let expected = array![[1, 2, 3], [4, 5, 6]];
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_read_csv_error() {
        assert_matches! {
            read::<i8, _>((2, 3), in_memory_reader("1,2,3\n4,x,6")),
            Err(Csv(_))
        }
    }

    #[test]
    fn test_read_too_few_rows() {
        assert_matches! {
            read::<i8, _>((3, 3), test_reader()),
            Err(TooFewRows { expected: 3, actual: 2})
        }
    }

    #[test]
    fn test_read_too_many_rows() {
        assert_matches! {
            read::<i8, _>((1, 3), test_reader()),
            Err(TooManyRows { expected: 1 })
        }
    }

    #[test]
    fn test_read_too_few_columns() {
        assert_matches! {
            read::<i8, _>((2, 4), test_reader()),
            Err(TooFewColumns { at_row_index: 0, expected: 4, actual: 3 })
        }
    }

    #[test]
    fn test_read_too_many_columns() {
        assert_matches! {
            read::<i8, _>((2, 2), test_reader()),
            Err(TooManyColumns { at_row_index: 0, expected: 2 })
        }
    }
}