Skip to main content

Table

Struct Table 

Source
pub struct Table { /* private fields */ }
Expand description

A parsed dataset: named columns of equal length.

A table holds one Column per source column, in source order, together with the dataset name. Table::new checks the columns, so every table a caller receives meets the guarantees in the module documentation.

§Examples

use dataset_ml::table::{Column, ColumnData, Table};
use ndarray::array;

let table = Table::new(
    "example",
    vec![
        Column::new("id", ColumnData::Integer(array![1, 2])),
        Column::new("width", ColumnData::Numeric(array![1.5, 2.5])),
    ],
)
.unwrap();

assert_eq!(table.name(), "example");
assert_eq!(table.n_samples(), 2);
assert_eq!(table.n_columns(), 2);
assert_eq!(table.names().collect::<Vec<_>>(), vec!["id", "width"]);

Implementations§

Source§

impl Table

Source

pub fn new( name: &'static str, columns: Vec<Column>, ) -> Result<Self, DatasetError>

Build a table and check its columns.

§Parameters
  • name - The dataset name. It appears in the errors this table returns.
  • columns - The columns, in the order the source lists them.
§Returns
  • Self - the new table, if the columns pass every check.
§Errors
  • DatasetError::DataFormatError with EmptyDataset - if columns is empty, or if the columns hold no sample.
  • DatasetError::DataFormatError with LengthMismatch - if two columns hold a different number of samples.
  • DatasetError::DataFormatError with InvalidValue - if two columns share a name.
Source

pub fn name(&self) -> &'static str

The dataset name.

§Returns
  • &'static str - the name the loader passed to Table::new.
Source

pub fn n_samples(&self) -> usize

The number of samples every column holds.

§Returns
  • usize - the sample count. It is always at least 1.
Source

pub fn n_columns(&self) -> usize

The number of columns.

§Returns
  • usize - the column count. It is always at least 1.
Source

pub fn columns(&self) -> &[Column]

Every column, in source order.

§Returns
  • &[Column] - a shared slice of every column.
Source

pub fn columns_mut(&mut self) -> &mut [Column]

Every column, in source order, for in-place editing.

§Returns
  • &mut [Column] - a mutable slice of every column.
§Notes

A change to a value keeps the table valid. Do not change the length of a column: the table’s guarantees no longer hold if you do.

Source

pub fn names(&self) -> impl Iterator<Item = &'static str> + '_

Every column name, in source order.

§Returns
  • impl Iterator<Item = &'static str> - the names, in source order.
Source

pub fn column(&self, name: &str) -> Option<&Column>

Find one column by name.

§Parameters
  • name - The name to look for. The comparison is exact.
§Returns
  • Some(&Column) - the column of that name.
  • None - if the table holds no column of that name.
Source

pub fn column_mut(&mut self, name: &str) -> Option<&mut Column>

Find one column by name, for in-place editing.

§Parameters
  • name - The name to look for. The comparison is exact.
§Returns
  • Some(&mut Column) - the column of that name.
  • None - if the table holds no column of that name.
§Notes

A change to a value keeps the table valid. Do not change the length of the column: the table’s guarantees no longer hold if you do.

Source

pub fn numeric_matrix( &self, names: &[&str], ) -> Result<Array2<f64>, DatasetError>

Build one f64 matrix out of the named columns.

The matrix keeps the order of names, which does not have to be the source order. A name may repeat, and the matrix then holds that column once per mention.

§Parameters
  • names - The columns to put in the matrix, in the order you want them. A ColumnData::Bytes column contributes its full row width, and every other column contributes one value.
§Returns
§Errors
  • DatasetError::DataFormatError with LengthMismatch - if names is empty.
  • DatasetError::DataFormatError with UnknownColumn - if the table holds no column of a given name.
  • DatasetError::DataFormatError with ColumnTypeMismatch - if a named column is ColumnData::String, which has no numeric reading.
§Performance

This builds a new matrix on every call, and that matrix holds n_samples × width values. Call it once and keep the result.

Trait Implementations§

Source§

impl Clone for Table

Source§

fn clone(&self) -> Table

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Table

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Table

Source§

fn eq(&self, other: &Table) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Table

Auto Trait Implementations§

§

impl Freeze for Table

§

impl RefUnwindSafe for Table

§

impl Send for Table

§

impl Sync for Table

§

impl Unpin for Table

§

impl UnsafeUnpin for Table

§

impl UnwindSafe for Table

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.