teeny_data/dataset/loader.rs
1/*
2 * Copyright (c) 2026 Teenygrad.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use std::str::FromStr;
18
19use ndarray::{Array2, ArrayBase, Ix2, OwnedRepr};
20
21use crate::error::{Error, Result};
22
23/// Downloads the CSV at `url` and parses every field as `T`, returning it as a 2D `ndarray`
24/// (rows × columns). `delimiter` is the field separator byte (e.g. `b','`).
25///
26/// Errors if the download/parse fails, any cell fails to parse as `T`, or the CSV has no rows.
27pub async fn load_csv<T: FromStr>(
28 url: &str,
29 delimiter: u8,
30) -> Result<ArrayBase<OwnedRepr<T>, Ix2>> {
31 let response = reqwest::get(url).await?;
32 let body = response.text().await?;
33 let mut reader = csv::ReaderBuilder::new()
34 .delimiter(delimiter)
35 .from_reader(body.as_bytes());
36 let mut data = Vec::new();
37 for result in reader.records() {
38 let record = result?;
39 let record = record
40 .iter()
41 .map(|s| s.parse::<T>())
42 .collect::<std::result::Result<Vec<_>, _>>()
43 .map_err(|_| Error::ParseValueError(format!("{record:?}")))?;
44 data.push(record);
45 }
46
47 if data.is_empty() {
48 return Err(Error::ParseValueError("No data found".to_string()).into());
49 }
50
51 let shape = (data.len(), data[0].len());
52 let data = data.into_iter().flatten().collect();
53
54 Ok(Array2::from_shape_vec(shape, data)?)
55}