Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ pub mod neighbors;
pub(crate) mod optimization;
/// Preprocessing utilities
pub mod preprocessing;
/// Reading in Data.
pub mod readers;
/// Support Vector Machines
pub mod svm;
/// Supervised tree-based learning methods
Expand Down
100 changes: 100 additions & 0 deletions src/linalg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ use high_order::HighOrderOperations;
use lu::LUDecomposableMatrix;
use qr::QRDecomposableMatrix;
use stats::{MatrixPreprocessing, MatrixStats};
use std::fs;
use svd::SVDDecomposableMatrix;

use crate::readers;

/// Column or row vector
pub trait BaseVector<T: RealNumber>: Clone + Debug {
/// Get an element of a vector
Expand Down Expand Up @@ -298,9 +301,60 @@ pub trait BaseMatrix<T: RealNumber>: Clone + Debug {
/// represents a row in this matrix.
type RowVector: BaseVector<T> + Clone + Debug;

/// Create a matrix from a csv file.
/// ```
/// use smartcore::linalg::naive::dense_matrix::DenseMatrix;
/// use smartcore::linalg::BaseMatrix;
/// use smartcore::readers::csv;
/// use std::fs;
///
/// fs::write("identity.csv", "header\n1.0,0.0\n0.0,1.0");
/// assert_eq!(
/// DenseMatrix::<f64>::from_csv("identity.csv", csv::CSVDefinition::default()).unwrap(),
/// DenseMatrix::from_row_vectors(vec![vec![1.0, 0.0], vec![0.0, 1.0]]).unwrap()
/// );
/// fs::remove_file("identity.csv");
/// ```
fn from_csv(
path: &str,
definition: readers::csv::CSVDefinition<'_>,
) -> Result<Self, readers::ReadingError> {
readers::csv::matrix_from_csv_source(fs::File::open(path)?, definition)
}

/// Transforms row vector `vec` into a 1xM matrix.
fn from_row_vector(vec: Self::RowVector) -> Self;

/// Transforms Vector of n rows with dimension m into
/// a matrix nxm.
/// ```
/// use smartcore::linalg::naive::dense_matrix::DenseMatrix;
/// use crate::smartcore::linalg::BaseMatrix;
///
/// let eye = DenseMatrix::from_row_vectors(vec![vec![1., 0., 0.], vec![0., 1., 0.], vec![0., 0., 1.]])
/// .unwrap();
///
/// assert_eq!(
/// eye,
/// DenseMatrix::from_2d_vec(&vec![
/// vec![1.0, 0.0, 0.0],
/// vec![0.0, 1.0, 0.0],
/// vec![0.0, 0.0, 1.0],
/// ])
/// );
fn from_row_vectors(rows: Vec<Self::RowVector>) -> Option<Self> {
if let Some(first_row) = rows.first().cloned() {
return Some(rows.iter().skip(1).cloned().fold(
Self::from_row_vector(first_row),
|current_matrix, new_row| {
current_matrix.v_stack(&BaseMatrix::from_row_vector(new_row))
},
));
} else {
None
}
}

/// Transforms 1-d matrix of 1xM into a row vector.
fn to_row_vector(self) -> Self::RowVector;

Expand Down Expand Up @@ -782,4 +836,50 @@ mod tests {
"The second column was not extracted correctly"
);
}
mod matrix_from_csv {

use crate::linalg::naive::dense_matrix::DenseMatrix;
use crate::linalg::BaseMatrix;
use crate::readers::csv;
use crate::readers::io_testing;
use crate::readers::ReadingError;

#[test]
fn simple_read_default_csv() {
let test_csv_file = io_testing::TemporaryTextFile::new(
"'sepal.length','sepal.width','petal.length','petal.width'\n\
5.1,3.5,1.4,0.2\n\
4.9,3,1.4,0.2\n\
4.7,3.2,1.3,0.2",
);

assert_eq!(
DenseMatrix::<f64>::from_csv(
test_csv_file
.expect("Temporary file could not be written.")
.path(),
csv::CSVDefinition::default()
),
Ok(DenseMatrix::from_2d_array(&[
&[5.1, 3.5, 1.4, 0.2],
&[4.9, 3.0, 1.4, 0.2],
&[4.7, 3.2, 1.3, 0.2],
]))
)
}

#[test]
fn non_existant_input_file() {
let potential_error =
DenseMatrix::<f64>::from_csv("/invalid/path", csv::CSVDefinition::default());
// The exact message is operating system dependant, therefore, I only test that the correct type
// error was returned.
assert_eq!(
potential_error.clone(),
Err(ReadingError::CouldNotReadFileSystem {
msg: String::from(potential_error.err().unwrap().message().unwrap())
})
)
}
}
}
12 changes: 12 additions & 0 deletions src/math/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rand::prelude::*;
use std::fmt::{Debug, Display};
use std::iter::{Product, Sum};
use std::ops::{AddAssign, DivAssign, MulAssign, SubAssign};
use std::str::FromStr;

/// Defines real number
/// <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_CHTML"></script>
Expand All @@ -22,6 +23,7 @@ pub trait RealNumber:
+ SubAssign
+ MulAssign
+ DivAssign
+ FromStr
{
/// Copy sign from `sign` - another real number
fn copysign(self, sign: Self) -> Self;
Expand Down Expand Up @@ -143,4 +145,14 @@ mod tests {
assert_eq!(41.0.sigmoid(), 1.);
assert_eq!((-41.0).sigmoid(), 0.);
}

#[test]
fn f32_from_string() {
assert_eq!(f32::from_str("1.111111").unwrap(), 1.111111)
}

#[test]
fn f64_from_string() {
assert_eq!(f64::from_str("1.111111111").unwrap(), 1.111111111)
}
}
Loading