Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add dataframes with polars #94

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ intel-mkl-static = ["blas", "ndarray-linalg", "intel-mkl-src/mkl-static-lp64-seq
intel-mkl-system = ["blas", "ndarray-linalg", "intel-mkl-src/mkl-dynamic-lp64-seq"]

blas = ["ndarray/blas"]
polars = ["polars-core"]

[dependencies]
num-traits = "0.2"
Expand All @@ -40,7 +41,7 @@ ndarray = { version = "0.13", default-features = false, features = ["approx"] }
ndarray-linalg = { version = "0.12.1", optional = true }

[dependencies.intel-mkl-src]
version = "0.6.0"
path = "/home/lorenz/Downloads/intel-mkl-src/intel-mkl-src"
default-features = false
optional = true

Expand All @@ -56,6 +57,11 @@ optional = true
default-features = false
features = ["cblas"]

[dependencies.polars-core]
version = "0.12"
optional = true
default-features = false

[dev-dependencies]
ndarray-rand = "0.11"
approx = { version = "0.3", default-features = false, features = ["std"] }
Expand Down
3 changes: 3 additions & 0 deletions datasets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ ndarray = { version = "0.13", default-features = false }
ndarray-csv = "0.4"
csv = "1.1"
flate2 = "1.0"
polars-core = { version = "0.12", default-features = false, optional = true }
polars-io = { version = "0.12", default-features = false, optional = true }

[dev-dependencies]
approx = { version = "0.3", default-features = false, features = ["std"] }
Expand All @@ -23,3 +25,4 @@ diabetes = []
iris = []
winequality = []
linnerud = []
income = ["polars-core", "polars-io", "linfa/polars"]
Binary file added datasets/data/income-test.tar.gz
Binary file not shown.
Binary file added datasets/data/income-train.tar.gz
Binary file not shown.
31 changes: 31 additions & 0 deletions datasets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ use linfa::Dataset;
use ndarray::prelude::*;
use ndarray_csv::Array2Reader;

#[cfg(feature = "income")]
use linfa::dataset::Dataframe;
#[cfg(feature = "income")]
use std::io::{Cursor, Read};
#[cfg(feature = "income")]
use polars_core::frame::DataFrame;
#[cfg(feature = "income")]
use polars_io::{csv::CsvReader, SerReader};

#[cfg(any(
feature = "iris",
feature = "diabetes",
Expand All @@ -58,6 +67,20 @@ fn array_from_buf(buf: &[u8]) -> Array2<f64> {
reader.deserialize_array2_dynamic().unwrap()
}

#[cfg(feature = "income")]
fn dataframe_from_buf(buf: &[u8]) -> DataFrame {
let mut file = GzDecoder::new(buf);
let mut buf: Vec<u8> = Vec::new();
file.read_to_end(&mut buf).unwrap();
let buf = Cursor::new(buf);

CsvReader::new(buf)
.infer_schema(None)
.has_header(false)
Copy link
Contributor

@bernardo-sb bernardo-sb Oct 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this option be given as a function argument has_header: bool?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, Is not it better to have direct arrow support than providing support for polars. So that any dataframe library based on arrow2 can use the preprocessing, and training methods? I am new to Rust and lifa project my question could be stupid. Thanks in advance for any clarity.

.finish()
.unwrap()
}

#[cfg(feature = "iris")]
/// Read in the iris-flower dataset from dataset path.
// The `.csv` data is two dimensional: Axis(0) denotes y-axis (rows), Axis(1) denotes x-axis (columns)
Expand Down Expand Up @@ -157,6 +180,14 @@ pub fn linnerud() -> Dataset<f64, f64> {
Dataset::new(input_array, output_array).with_feature_names(feature_names)
}

#[cfg(feature = "income")]
pub fn income() -> (Dataframe<bool>, Dataframe<bool>) {
let input_data = include_bytes!("../data/income-train.tar.gz");
let input_dataframe = dataframe_from_buf(&input_data[..]);

panic!("")
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
16 changes: 16 additions & 0 deletions src/dataset/impl_records.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use super::{DatasetBase, Float, Records};
use ndarray::{ArrayBase, Axis, Data, Dimension};

#[cfg(feature = "polars")]
use polars_core::frame::DataFrame;

/// Implement records for NdArrays
impl<F: Float, S: Data<Elem = F>, I: Dimension> Records for ArrayBase<S, I> {
type Elem = F;
Expand Down Expand Up @@ -52,3 +55,16 @@ impl<R: Records> Records for &R {
(*self).nfeatures()
}
}

#[cfg(feature = "polars")]
impl Records for DataFrame {
type Elem = ();

fn nsamples(&self) -> usize {
self.shape().0
}

fn nfeatures(&self) -> usize {
self.shape().1
}
}
7 changes: 7 additions & 0 deletions src/dataset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use ndarray::{
};
use num_traits::{FromPrimitive, NumAssignOps, Signed};

#[cfg(feature = "polars")]
use polars_core::frame::DataFrame;

use std::cmp::{Ordering, PartialOrd};
use std::collections::{HashMap, HashSet};
use std::hash::Hash;
Expand Down Expand Up @@ -147,6 +150,10 @@ pub type DatasetView<'a, D, T> = DatasetBase<ArrayView<'a, D, Ix2>, ArrayView<'a
pub type DatasetPr<D, L> =
DatasetBase<ArrayBase<OwnedRepr<D>, Ix2>, CountedTargets<L, ArrayBase<OwnedRepr<Pr>, Ix3>>>;

#[cfg(feature = "polars")]
/// Dataframe
pub type Dataframe<T> = DatasetBase<DataFrame, ArrayBase<OwnedRepr<T>, Ix2>>;

/// Record trait
pub trait Records: Sized {
type Elem;
Expand Down
3 changes: 3 additions & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ pub use crate::traits::*;
#[doc(no_inline)]
pub use crate::dataset::{AsTargets, Dataset, DatasetBase, DatasetView, Float, Records};

#[cfg(feature = "polars")]
pub use crate::dataset::Dataframe;

#[doc(no_inline)]
pub use crate::metrics_classification::{BinaryClassification, ConfusionMatrix, ToConfusionMatrix};

Expand Down